From nobody Thu Nov 14 06:37:28 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D61CBC74A5B for ; Fri, 17 Mar 2023 08:12:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231451AbjCQIMm (ORCPT ); Fri, 17 Mar 2023 04:12:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231355AbjCQIMQ (ORCPT ); Fri, 17 Mar 2023 04:12:16 -0400 Received: from mailgw01.mediatek.com (unknown [60.244.123.138]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1BE03D5152; Fri, 17 Mar 2023 01:11:29 -0700 (PDT) X-UUID: 3781f7b4c49b11ed91027fb02e0f1d65-20230317 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=mMpfXTEQJ9Ytroylcl1siFLt80SppSy24Prqwo7sAbE=; b=o4TI7kp7c+8QU3LFhgYsv6ARjiglo/IvSsxVqZzngJcKYPclG+9zGTrmIarMpjn0troBttqcuvZG+R+EeLWJrhf0O9jiRp4HVchnxf7pTzwyT8QK0GLy6QoPKPMoNyDNnUAkM87z4RjYEZPdmOvRe6fI73d2f22aI2vjMOtEBsA=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.21,REQID:1c53ea38-01c1-43b0-81a4-d6f64839eb98,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:95,FILE:0,BULK:0,RULE:Release_Ham,ACTI ON:release,TS:70 X-CID-INFO: VERSION:1.1.21,REQID:1c53ea38-01c1-43b0-81a4-d6f64839eb98,IP:0,URL :0,TC:0,Content:-25,EDM:0,RT:0,SF:95,FILE:0,BULK:0,RULE:Spam_GS981B3D,ACTI ON:quarantine,TS:70 X-CID-META: VersionHash:83295aa,CLOUDID:7033ac28-564d-42d9-9875-7c868ee415ec,B ulkID:230317161047NHITUCQG,BulkQuantity:0,Recheck:0,SF:38|29|28|17|19|48,T C:nil,Content:0,EDM:-3,IP:nil,URL:11|1,File:nil,Bulk:nil,QS:nil,BEC:nil,CO L:0,OSI:0,OSA:0,AV:0 X-CID-BVR: 0,NGT X-CID-BAS: 0,NGT,0,_ X-UUID: 3781f7b4c49b11ed91027fb02e0f1d65-20230317 Received: from mtkmbs10n1.mediatek.inc [(172.21.101.34)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 2028494913; Fri, 17 Mar 2023 16:10:44 +0800 Received: from mtkmbs11n2.mediatek.inc (172.21.101.187) by mtkmbs13n2.mediatek.inc (172.21.101.108) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Fri, 17 Mar 2023 16:10:43 +0800 Received: from mcddlt001.gcn.mediatek.inc (10.19.240.15) by mtkmbs11n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.25 via Frontend Transport; Fri, 17 Mar 2023 16:10:41 +0800 From: Yanchao Yang To: Loic Poulain , Sergey Ryazanov , Johannes Berg , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , netdev ML , kernel ML CC: Intel experts , Chetan , MTK ML , Liang Lu , Haijun Liu , Hua Yang , Ting Wang , Felix Chen , Mingliang Xu , Min Dong , Aiden Wang , Guohao Zhang , Chris Feng , Yanchao Yang , Lambert Wang , Mingchuang Qiao , Xiayu Zhang , Haozhe Chang Subject: [PATCH net-next v4 01/10] net: wwan: tmi: Add PCIe core Date: Fri, 17 Mar 2023 16:09:33 +0800 Message-ID: <20230317080942.183514-2-yanchao.yang@mediatek.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20230317080942.183514-1-yanchao.yang@mediatek.com> References: <20230317080942.183514-1-yanchao.yang@mediatek.com> MIME-Version: 1.0 X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Registers the TMI 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 handshak= e, PM and port enumeration. * RGU provides interrupt channels to generate notifications from the device so that the TMI 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. Signed-off-by: Yanchao Yang Signed-off-by: Ting Wang --- drivers/net/wwan/Kconfig | 14 + drivers/net/wwan/Makefile | 1 + drivers/net/wwan/mediatek/Makefile | 8 + drivers/net/wwan/mediatek/mtk_dev.h | 203 ++++++ drivers/net/wwan/mediatek/pcie/mtk_pci.c | 887 +++++++++++++++++++++++ drivers/net/wwan/mediatek/pcie/mtk_pci.h | 144 ++++ drivers/net/wwan/mediatek/pcie/mtk_reg.h | 69 ++ 7 files changed, 1326 insertions(+) create mode 100644 drivers/net/wwan/mediatek/Makefile create mode 100644 drivers/net/wwan/mediatek/mtk_dev.h create mode 100644 drivers/net/wwan/mediatek/pcie/mtk_pci.c create mode 100644 drivers/net/wwan/mediatek/pcie/mtk_pci.h create mode 100644 drivers/net/wwan/mediatek/pcie/mtk_reg.h diff --git a/drivers/net/wwan/Kconfig b/drivers/net/wwan/Kconfig index 410b0245114e..085562aa8682 100644 --- a/drivers/net/wwan/Kconfig +++ b/drivers/net/wwan/Kconfig @@ -120,6 +120,20 @@ config MTK_T7XX =20 If unsure, say N. =20 +config MTK_TMI + tristate "TMI Driver for Mediatek T-series Device" + depends on (PCI && ACPI) + select RELAY if WWAN_DEBUGFS + help + This driver enables Mediatek T-series WWAN Device communication. + The TMI is intended for t8xx T-series modem chips. Currently, t7xx + is not supported. + + If you have one of those Mediatek T-series WWAN Modules and wish to + use it in Linux say Y/M here. + + If unsure, say N. + endif # WWAN =20 endmenu diff --git a/drivers/net/wwan/Makefile b/drivers/net/wwan/Makefile index 3960c0ae2445..198d8074851f 100644 --- a/drivers/net/wwan/Makefile +++ b/drivers/net/wwan/Makefile @@ -14,3 +14,4 @@ obj-$(CONFIG_QCOM_BAM_DMUX) +=3D qcom_bam_dmux.o obj-$(CONFIG_RPMSG_WWAN_CTRL) +=3D rpmsg_wwan_ctrl.o obj-$(CONFIG_IOSM) +=3D iosm/ obj-$(CONFIG_MTK_T7XX) +=3D t7xx/ +obj-$(CONFIG_MTK_TMI) +=3D mediatek/ diff --git a/drivers/net/wwan/mediatek/Makefile b/drivers/net/wwan/mediatek= /Makefile new file mode 100644 index 000000000000..b74192eee432 --- /dev/null +++ b/drivers/net/wwan/mediatek/Makefile @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: BSD-3-Clause-Clear + +MODULE_NAME :=3D mtk_tmi + +mtk_tmi-y =3D \ + pcie/mtk_pci.o + +obj-$(CONFIG_MTK_TMI) +=3D mtk_tmi.o diff --git a/drivers/net/wwan/mediatek/mtk_dev.h b/drivers/net/wwan/mediate= k/mtk_dev.h new file mode 100644 index 000000000000..acda9ccf05f1 --- /dev/null +++ b/drivers/net/wwan/mediatek/mtk_dev.h @@ -0,0 +1,203 @@ +/* SPDX-License-Identifier: BSD-3-Clause-Clear + * + * Copyright (c) 2022, MediaTek Inc. + */ + +#ifndef __MTK_DEV_H__ +#define __MTK_DEV_H__ + +#include +#include + +#define MTK_DEV_STR_LEN 16 + +enum mtk_irq_src { + MTK_IRQ_SRC_MIN, + MTK_IRQ_SRC_MHCCIF, + MTK_IRQ_SRC_SAP_RGU, + MTK_IRQ_SRC_DPMAIF, + MTK_IRQ_SRC_DPMAIF2, + MTK_IRQ_SRC_CLDMA0, + MTK_IRQ_SRC_CLDMA1, + MTK_IRQ_SRC_CLDMA2, + MTK_IRQ_SRC_CLDMA3, + MTK_IRQ_SRC_PM_LOCK, + MTK_IRQ_SRC_DPMAIF3, + MTK_IRQ_SRC_MAX +}; + +enum mtk_user_id { + MTK_USER_HW, + MTK_USER_CTRL, + MTK_USER_DPMAIF, + MTK_USER_PM, + MTK_USER_EXCEPT, + MTK_USER_MAX +}; + +enum mtk_l1ss_grp { + L1SS_PM, + L1SS_EXT_EVT, +}; + +#define L1SS_BIT_L1(grp) BIT(((grp) << 2) + 1) +#define L1SS_BIT_L1_1(grp) BIT(((grp) << 2) + 2) +#define L1SS_BIT_L1_2(grp) BIT(((grp) << 2) + 3) + +struct mtk_md_dev; + +/** + * struct mtk_hw_ops - The HW layer operations provided to transaction lay= er. + * @read32: Callback to read 32-bit register. + * Read value from MD. For PCIe, it's BAR 2/3 MMIO read. + * @write32: Callback to write 32-bit register. + * Write value to MD. For PCIe, it's BAR 2/3 MMIO write. + * @get_dev_state: Callback to get the device's state. + * @ack_dev_state: Callback to acknowledge device state. + * @get_irq_id: Callback to get the irq id specific IP on a chip. + * @get_virq_id: Callback to get the system virtual IRQ. + * @register_irq: Callback to register callback function to specific har= dware IP. + * @unregister_irq: Callback to unregister callback function to specific h= ardware IP. + * @mask_irq: Callback to mask the interrupt of specific hardware IP. + * @unmask_irq: Callback to unmask the interrupt of specific hardware = IP. + * @clear_irq: Callback to clear the interrupt of specific hardware I= P. + * @register_ext_evt:Callback to register HW Layer external event. + * @unregister_ext_evt:Callback to unregister HW Layer external event. + * @mask_ext_evt: Callback to mask HW Layer external event. + * @unmask_ext_evt: Callback to unmask HW Layer external event. + * @clear_ext_evt: Callback to clear HW Layer external event status. + * @send_ext_evt: Callback to send HW Layer external event. + * @get_ext_evt_status:Callback to get HW Layer external event status. + * @get_hp_status: Callback to get link hotplug status. + */ +struct mtk_hw_ops { + u32 (*read32)(struct mtk_md_dev *mdev, u64 addr); + void (*write32)(struct mtk_md_dev *mdev, u64 addr, u32 val); + u32 (*get_dev_state)(struct mtk_md_dev *mdev); + void (*ack_dev_state)(struct mtk_md_dev *mdev, u32 state); + int (*get_irq_id)(struct mtk_md_dev *mdev, enum mtk_irq_src irq_src); + int (*get_virq_id)(struct mtk_md_dev *mdev, int irq_id); + int (*register_irq)(struct mtk_md_dev *mdev, int irq_id, + int (*irq_cb)(int irq_id, void *data), void *data); + int (*unregister_irq)(struct mtk_md_dev *mdev, int irq_id); + int (*mask_irq)(struct mtk_md_dev *mdev, int irq_id); + int (*unmask_irq)(struct mtk_md_dev *mdev, int irq_id); + int (*clear_irq)(struct mtk_md_dev *mdev, int irq_id); + int (*register_ext_evt)(struct mtk_md_dev *mdev, u32 chs, + int (*evt_cb)(u32 status, void *data), void *data); + int (*unregister_ext_evt)(struct mtk_md_dev *mdev, u32 chs); + void (*mask_ext_evt)(struct mtk_md_dev *mdev, u32 chs); + void (*unmask_ext_evt)(struct mtk_md_dev *mdev, u32 chs); + void (*clear_ext_evt)(struct mtk_md_dev *mdev, u32 chs); + int (*send_ext_evt)(struct mtk_md_dev *mdev, u32 ch); + u32 (*get_ext_evt_status)(struct mtk_md_dev *mdev); + int (*get_hp_status)(struct mtk_md_dev *mdev); +}; + +struct mtk_md_dev { + struct device *dev; + const struct mtk_hw_ops *hw_ops; + void *hw_priv; + u32 hw_ver; + int msi_nvecs; + char dev_str[MTK_DEV_STR_LEN]; +}; + +static inline u32 mtk_hw_read32(struct mtk_md_dev *mdev, u64 addr) +{ + return mdev->hw_ops->read32(mdev, addr); +} + +static inline void mtk_hw_write32(struct mtk_md_dev *mdev, u64 addr, u32 v= al) +{ + mdev->hw_ops->write32(mdev, addr, val); +} + +static inline u32 mtk_hw_get_dev_state(struct mtk_md_dev *mdev) +{ + return mdev->hw_ops->get_dev_state(mdev); +} + +static inline void mtk_hw_ack_dev_state(struct mtk_md_dev *mdev, u32 state) +{ + mdev->hw_ops->ack_dev_state(mdev, state); +} + +static inline int mtk_hw_get_irq_id(struct mtk_md_dev *mdev, enum mtk_irq_= src irq_src) +{ + return mdev->hw_ops->get_irq_id(mdev, irq_src); +} + +static inline int mtk_hw_get_virq_id(struct mtk_md_dev *mdev, int irq_id) +{ + return mdev->hw_ops->get_virq_id(mdev, irq_id); +} + +static inline int mtk_hw_register_irq(struct mtk_md_dev *mdev, int irq_id, + int (*irq_cb)(int irq_id, void *data), void *data) +{ + return mdev->hw_ops->register_irq(mdev, irq_id, irq_cb, data); +} + +static inline int mtk_hw_unregister_irq(struct mtk_md_dev *mdev, int irq_i= d) +{ + return mdev->hw_ops->unregister_irq(mdev, irq_id); +} + +static inline int mtk_hw_mask_irq(struct mtk_md_dev *mdev, int irq_id) +{ + return mdev->hw_ops->mask_irq(mdev, irq_id); +} + +static inline int mtk_hw_unmask_irq(struct mtk_md_dev *mdev, int irq_id) +{ + return mdev->hw_ops->unmask_irq(mdev, irq_id); +} + +static inline int mtk_hw_clear_irq(struct mtk_md_dev *mdev, int irq_id) +{ + return mdev->hw_ops->clear_irq(mdev, irq_id); +} + +static inline int mtk_hw_register_ext_evt(struct mtk_md_dev *mdev, u32 chs, + int (*evt_cb)(u32 status, void *data), void *data) +{ + return mdev->hw_ops->register_ext_evt(mdev, chs, evt_cb, data); +} + +static inline int mtk_hw_unregister_ext_evt(struct mtk_md_dev *mdev, u32 c= hs) +{ + return mdev->hw_ops->unregister_ext_evt(mdev, chs); +} + +static inline void mtk_hw_mask_ext_evt(struct mtk_md_dev *mdev, u32 chs) +{ + mdev->hw_ops->mask_ext_evt(mdev, chs); +} + +static inline void mtk_hw_unmask_ext_evt(struct mtk_md_dev *mdev, u32 chs) +{ + mdev->hw_ops->unmask_ext_evt(mdev, chs); +} + +static inline void mtk_hw_clear_ext_evt(struct mtk_md_dev *mdev, u32 chs) +{ + mdev->hw_ops->clear_ext_evt(mdev, chs); +} + +static inline int mtk_hw_send_ext_evt(struct mtk_md_dev *mdev, u32 ch) +{ + return mdev->hw_ops->send_ext_evt(mdev, ch); +} + +static inline u32 mtk_hw_get_ext_evt_status(struct mtk_md_dev *mdev) +{ + return mdev->hw_ops->get_ext_evt_status(mdev); +} + +static inline int mtk_hw_get_hp_status(struct mtk_md_dev *mdev) +{ + return mdev->hw_ops->get_hp_status(mdev); +} + +#endif /* __MTK_DEV_H__ */ diff --git a/drivers/net/wwan/mediatek/pcie/mtk_pci.c b/drivers/net/wwan/me= diatek/pcie/mtk_pci.c new file mode 100644 index 000000000000..f8590d7319a8 --- /dev/null +++ b/drivers/net/wwan/mediatek/pcie/mtk_pci.c @@ -0,0 +1,887 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +/* + * Copyright (c) 2022, MediaTek Inc. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mtk_pci.h" +#include "mtk_reg.h" + +#define MTK_PCI_TRANSPARENT_ATR_SIZE (0x3F) + +static u32 mtk_pci_mac_read32(struct mtk_pci_priv *priv, u64 addr) +{ + return ioread32(priv->mac_reg_base + addr); +} + +static void mtk_pci_mac_write32(struct mtk_pci_priv *priv, u64 addr, u32 v= al) +{ + iowrite32(val, priv->mac_reg_base + addr); +} + +static void mtk_pci_set_msix_merged(struct mtk_pci_priv *priv, int irq_cnt) +{ + mtk_pci_mac_write32(priv, REG_PCIE_CFG_MSIX, ffs(irq_cnt) * 2 - 1); +} + +static int mtk_pci_setup_atr(struct mtk_md_dev *mdev, struct mtk_atr_cfg *= cfg) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + u32 addr, val, size_h, size_l; + int atr_size, pos, offset; + + if (cfg->transparent) { + atr_size =3D MTK_PCI_TRANSPARENT_ATR_SIZE; /* No address conversion is p= erformed */ + } else { + if (cfg->src_addr & (cfg->size - 1)) { + dev_err(mdev->dev, "Invalid atr src addr is not aligned to size\n"); + return -EFAULT; + } + if (cfg->trsl_addr & (cfg->size - 1)) { + dev_err(mdev->dev, + "Invalid atr trsl addr is not aligned to size, %llx, %llx\n", + cfg->trsl_addr, + cfg->size - 1); + return -EFAULT; + } + + size_l =3D FIELD_GET(GENMASK_ULL(31, 0), cfg->size); + size_h =3D FIELD_GET(GENMASK_ULL(63, 32), cfg->size); + + pos =3D ffs(size_l); + if (pos) { + /* Address Translate Space Size is equal to 2^(atr_size+1). + * "-2" means "-1-1", the first "-1" is because of the atr_size registe= r, + * the second is because of the ffs() will increase by one. + */ + atr_size =3D pos - 2; + } else { + pos =3D ffs(size_h); + /* "+30" means "+32-1-1", the meaning of "-1-1" is same as above, + * "+32" is because atr_size is large, exceeding 32-bits. + */ + atr_size =3D pos + 30; + } + } + + /* Calculate table offset */ + offset =3D ATR_PORT_OFFSET * cfg->port + ATR_TABLE_OFFSET * cfg->table; + /* SRC_ADDR_H */ + addr =3D REG_ATR_PCIE_WIN0_T0_SRC_ADDR_MSB + offset; + val =3D (u32)(cfg->src_addr >> 32); + mtk_pci_mac_write32(priv, addr, val); + /* SRC_ADDR_L */ + addr =3D REG_ATR_PCIE_WIN0_T0_SRC_ADDR_LSB + offset; + val =3D (u32)(cfg->src_addr & 0xFFFFF000) | (atr_size << 1) | 0x1; + mtk_pci_mac_write32(priv, addr, val); + + /* TRSL_ADDR_H */ + addr =3D REG_ATR_PCIE_WIN0_T0_TRSL_ADDR_MSB + offset; + val =3D (u32)(cfg->trsl_addr >> 32); + mtk_pci_mac_write32(priv, addr, val); + /* TRSL_ADDR_L */ + addr =3D REG_ATR_PCIE_WIN0_T0_TRSL_ADDR_LSB + offset; + val =3D (u32)(cfg->trsl_addr & 0xFFFFF000); + mtk_pci_mac_write32(priv, addr, val); + + /* TRSL_PARAM */ + addr =3D REG_ATR_PCIE_WIN0_T0_TRSL_PARAM + offset; + val =3D (cfg->trsl_param << 16) | cfg->trsl_id; + mtk_pci_mac_write32(priv, addr, val); + + return 0; +} + +static void mtk_pci_atr_disable(struct mtk_pci_priv *priv) +{ + int port, tbl, offset; + + /* Disable all ATR table for all ports */ + for (port =3D ATR_SRC_PCI_WIN0; port <=3D ATR_SRC_AXIS_3; port++) + for (tbl =3D 0; tbl < ATR_TABLE_NUM_PER_ATR; tbl++) { + /* Calculate table offset */ + offset =3D ATR_PORT_OFFSET * port + ATR_TABLE_OFFSET * tbl; + /* Disable table by SRC_ADDR_L */ + mtk_pci_mac_write32(priv, REG_ATR_PCIE_WIN0_T0_SRC_ADDR_LSB + offset, 0= ); + } +} + +static int mtk_pci_atr_init(struct mtk_md_dev *mdev) +{ + struct pci_dev *pdev =3D to_pci_dev(mdev->dev); + struct mtk_pci_priv *priv =3D mdev->hw_priv; + struct mtk_atr_cfg cfg; + int port, ret; + + mtk_pci_atr_disable(priv); + + /* Config ATR for RC to access device's register */ + cfg.src_addr =3D pci_resource_start(pdev, MTK_BAR_2_3_IDX); + cfg.size =3D ATR_PCIE_REG_SIZE; + cfg.trsl_addr =3D ATR_PCIE_REG_TRSL_ADDR; + cfg.type =3D ATR_PCI2AXI; + cfg.port =3D ATR_PCIE_REG_PORT; + cfg.table =3D ATR_PCIE_REG_TABLE_NUM; + cfg.trsl_id =3D ATR_PCIE_REG_TRSL_PORT; + cfg.trsl_param =3D 0x0; + cfg.transparent =3D 0x0; + ret =3D mtk_pci_setup_atr(mdev, &cfg); + if (ret) + return ret; + + /* Config ATR for MHCCIF */ + cfg.src_addr =3D pci_resource_start(pdev, MTK_BAR_2_3_IDX); + cfg.src_addr +=3D priv->cfg->mhccif_rc_base_addr - ATR_PCIE_REG_TRSL_ADDR; + cfg.size =3D priv->cfg->mhccif_trsl_size; + cfg.trsl_addr =3D priv->cfg->mhccif_rc_reg_trsl_addr; + cfg.type =3D ATR_PCI2AXI; + cfg.port =3D ATR_PCIE_REG_PORT; + cfg.table =3D ART_PCIE_REG_MHCCIF_TABLE_NUM; + cfg.trsl_id =3D ATR_PCIE_REG_TRSL_PORT; + cfg.trsl_param =3D 0x0; + cfg.transparent =3D 0x0; + ret =3D mtk_pci_setup_atr(mdev, &cfg); + if (ret) + return ret; + + /* Config ATR for EP to access RC's memory */ + for (port =3D ATR_PCIE_DEV_DMA_PORT_START; port <=3D ATR_PCIE_DEV_DMA_POR= T_END; port++) { + cfg.src_addr =3D ATR_PCIE_DEV_DMA_SRC_ADDR; + cfg.size =3D ATR_PCIE_DEV_DMA_SIZE; + cfg.trsl_addr =3D ATR_PCIE_DEV_DMA_TRSL_ADDR; + cfg.type =3D ATR_AXI2PCI; + cfg.port =3D port; + cfg.table =3D ATR_PCIE_DEV_DMA_TABLE_NUM; + cfg.trsl_id =3D ATR_DST_PCI_TRX; + cfg.trsl_param =3D 0x0; + /* Enable transparent translation */ + cfg.transparent =3D ATR_PCIE_DEV_DMA_TRANSPARENT; + ret =3D mtk_pci_setup_atr(mdev, &cfg); + if (ret) + return ret; + } + + return 0; +} + +static u32 mtk_pci_read32(struct mtk_md_dev *mdev, u64 addr) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + + return ioread32(priv->ext_reg_base + addr); +} + +static void mtk_pci_write32(struct mtk_md_dev *mdev, u64 addr, u32 val) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + + iowrite32(val, priv->ext_reg_base + addr); +} + +static u32 mtk_pci_get_dev_state(struct mtk_md_dev *mdev) +{ + return mtk_pci_mac_read32(mdev->hw_priv, REG_PCIE_DEBUG_DUMMY_7); +} + +static void mtk_pci_ack_dev_state(struct mtk_md_dev *mdev, u32 state) +{ + mtk_pci_mac_write32(mdev->hw_priv, REG_PCIE_DEBUG_DUMMY_7, state); +} + +static int mtk_pci_get_irq_id(struct mtk_md_dev *mdev, enum mtk_irq_src ir= q_src) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + const int *irq_tbl =3D priv->cfg->irq_tbl; + int irq_id =3D -EINVAL; + + if (irq_src > MTK_IRQ_SRC_MIN && irq_src < MTK_IRQ_SRC_MAX) { + irq_id =3D irq_tbl[irq_src]; + if (unlikely(irq_id < 0 || irq_id >=3D MTK_IRQ_CNT_MAX)) + irq_id =3D -EINVAL; + } + + return irq_id; +} + +static int mtk_pci_get_virq_id(struct mtk_md_dev *mdev, int irq_id) +{ + struct pci_dev *pdev =3D to_pci_dev(mdev->dev); + int nr =3D 0; + + if (pdev->msix_enabled) + nr =3D irq_id % mdev->msi_nvecs; + + return pci_irq_vector(pdev, nr); +} + +static int mtk_pci_register_irq(struct mtk_md_dev *mdev, int irq_id, + int (*irq_cb)(int irq_id, void *data), void *data) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + + if (unlikely((irq_id < 0 || irq_id >=3D MTK_IRQ_CNT_MAX) || !irq_cb)) + return -EINVAL; + + if (priv->irq_cb_list[irq_id]) { + dev_err(mdev->dev, + "Unable to register irq, irq_id=3D%d, it's already been register by %ps= .\n", + irq_id, priv->irq_cb_list[irq_id]); + return -EFAULT; + } + priv->irq_cb_list[irq_id] =3D irq_cb; + priv->irq_cb_data[irq_id] =3D data; + + return 0; +} + +static int mtk_pci_unregister_irq(struct mtk_md_dev *mdev, int irq_id) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + + if (unlikely(irq_id < 0 || irq_id >=3D MTK_IRQ_CNT_MAX)) + return -EINVAL; + + if (!priv->irq_cb_list[irq_id]) { + dev_err(mdev->dev, "irq_id=3D%d has not been registered\n", irq_id); + return -EFAULT; + } + priv->irq_cb_list[irq_id] =3D NULL; + priv->irq_cb_data[irq_id] =3D NULL; + + return 0; +} + +static int mtk_pci_mask_irq(struct mtk_md_dev *mdev, int irq_id) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + + if (unlikely((irq_id < 0 || irq_id >=3D MTK_IRQ_CNT_MAX) || priv->irq_typ= e !=3D PCI_IRQ_MSIX)) { + dev_err(mdev->dev, "Failed to mask irq: input irq_id=3D%d\n", irq_id); + return -EINVAL; + } + + mtk_pci_mac_write32(priv, REG_IMASK_HOST_MSIX_CLR_GRP0_0, BIT(irq_id)); + + return 0; +} + +static int mtk_pci_unmask_irq(struct mtk_md_dev *mdev, int irq_id) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + + if (unlikely((irq_id < 0 || irq_id >=3D MTK_IRQ_CNT_MAX) || priv->irq_typ= e !=3D PCI_IRQ_MSIX)) { + dev_err(mdev->dev, "Failed to unmask irq: input irq_id=3D%d\n", irq_id); + return -EINVAL; + } + + mtk_pci_mac_write32(priv, REG_IMASK_HOST_MSIX_SET_GRP0_0, BIT(irq_id)); + + return 0; +} + +static int mtk_pci_clear_irq(struct mtk_md_dev *mdev, int irq_id) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + + if (unlikely((irq_id < 0 || irq_id >=3D MTK_IRQ_CNT_MAX) || priv->irq_typ= e !=3D PCI_IRQ_MSIX)) { + dev_err(mdev->dev, "Failed to clear irq: input irq_id=3D%d\n", irq_id); + return -EINVAL; + } + + mtk_pci_mac_write32(priv, REG_MSIX_ISTATUS_HOST_GRP0_0, BIT(irq_id)); + + return 0; +} + +static int mtk_mhccif_register_evt(struct mtk_md_dev *mdev, u32 chs, + int (*evt_cb)(u32 status, void *data), void *data) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + struct mtk_mhccif_cb *cb; + unsigned long flag; + int ret =3D 0; + + if (!chs || !evt_cb) + return -EINVAL; + + spin_lock_irqsave(&priv->mhccif_lock, flag); + list_for_each_entry(cb, &priv->mhccif_cb_list, entry) { + if (cb->chs & chs) { + ret =3D -EFAULT; + dev_err(mdev->dev, + "Unable to register evt, chs=3D0x%08X&0x%08X registered_cb=3D%ps\n", + chs, cb->chs, cb->evt_cb); + goto err; + } + } + cb =3D devm_kzalloc(mdev->dev, sizeof(*cb), GFP_ATOMIC); + if (!cb) { + ret =3D -ENOMEM; + goto err; + } + cb->evt_cb =3D evt_cb; + cb->data =3D data; + cb->chs =3D chs; + list_add_tail(&cb->entry, &priv->mhccif_cb_list); + +err: + spin_unlock_irqrestore(&priv->mhccif_lock, flag); + + return ret; +} + +static int mtk_mhccif_unregister_evt(struct mtk_md_dev *mdev, u32 chs) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + struct mtk_mhccif_cb *cb, *next; + unsigned long flag; + int ret =3D 0; + + if (!chs) + return -EINVAL; + + spin_lock_irqsave(&priv->mhccif_lock, flag); + list_for_each_entry_safe(cb, next, &priv->mhccif_cb_list, entry) { + if (cb->chs =3D=3D chs) { + list_del(&cb->entry); + devm_kfree(mdev->dev, cb); + goto out; + } + } + ret =3D -EFAULT; + dev_warn(mdev->dev, "Unable to unregister evt, no chs=3D0x%08X has been r= egistered.\n", chs); +out: + spin_unlock_irqrestore(&priv->mhccif_lock, flag); + + return ret; +} + +static void mtk_mhccif_mask_evt(struct mtk_md_dev *mdev, u32 chs) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + + mtk_pci_write32(mdev, priv->cfg->mhccif_rc_base_addr + + MHCCIF_EP2RC_SW_INT_EAP_MASK_SET, chs); +} + +static void mtk_mhccif_unmask_evt(struct mtk_md_dev *mdev, u32 chs) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + + mtk_pci_write32(mdev, priv->cfg->mhccif_rc_base_addr + + MHCCIF_EP2RC_SW_INT_EAP_MASK_CLR, chs); +} + +static void mtk_mhccif_clear_evt(struct mtk_md_dev *mdev, u32 chs) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + + mtk_pci_write32(mdev, priv->cfg->mhccif_rc_base_addr + + MHCCIF_EP2RC_SW_INT_ACK, chs); +} + +static int mtk_mhccif_send_evt(struct mtk_md_dev *mdev, u32 ch) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + u32 rc_base; + + rc_base =3D priv->cfg->mhccif_rc_base_addr; + /* Only allow one ch to be triggered at a time */ + if ((ch & (ch - 1)) || !ch) { + dev_err(mdev->dev, "Unsupported ext evt ch=3D0x%08X\n", ch); + return -EINVAL; + } + + mtk_pci_write32(mdev, rc_base + MHCCIF_RC2EP_SW_BSY, ch); + mtk_pci_write32(mdev, rc_base + MHCCIF_RC2EP_SW_TCHNUM, ffs(ch) - 1); + + return 0; +} + +static u32 mtk_mhccif_get_evt_status(struct mtk_md_dev *mdev) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + + return mtk_pci_read32(mdev, priv->cfg->mhccif_rc_base_addr + MHCCIF_EP2RC= _SW_INT_STS); +} + +static int mtk_pci_acpi_reset(struct mtk_md_dev *mdev, char *fn_name) +{ +#ifdef CONFIG_ACPI + struct acpi_buffer buffer =3D { ACPI_ALLOCATE_BUFFER, NULL }; + acpi_status acpi_ret; + acpi_handle handle; + int ret =3D 0; + + handle =3D ACPI_HANDLE(mdev->dev); + if (!handle) { + dev_err(mdev->dev, "Unsupported, acpi handle isn't found\n"); + ret =3D -ENODEV; + goto err; + } + if (!acpi_has_method(handle, fn_name)) { + dev_err(mdev->dev, "Unsupported, _RST method isn't found\n"); + ret =3D -ENODEV; + goto err; + } + acpi_ret =3D acpi_evaluate_object(handle, fn_name, NULL, &buffer); + if (ACPI_FAILURE(acpi_ret)) { + dev_err(mdev->dev, "Failed to execute %s method: %s\n", + fn_name, + acpi_format_exception(acpi_ret)); + ret =3D -EFAULT; + goto err; + } + dev_info(mdev->dev, "FLDR execute successfully\n"); + acpi_os_free(buffer.pointer); +err: + return ret; +#else + dev_err(mdev->dev, "Unsupported, CONFIG ACPI hasn't been set to 'y'\n"); + return -ENODEV; +#endif +} + +static int mtk_pci_fldr(struct mtk_md_dev *mdev) +{ + return mtk_pci_acpi_reset(mdev, "_RST"); +} + +static bool mtk_pci_link_check(struct mtk_md_dev *mdev) +{ + return !pci_device_is_present(to_pci_dev(mdev->dev)); +} + +static int mtk_pci_get_hp_status(struct mtk_md_dev *mdev) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + + return priv->rc_hp_on; +} + +static const struct mtk_hw_ops mtk_pci_ops =3D { + .read32 =3D mtk_pci_read32, + .write32 =3D mtk_pci_write32, + .get_dev_state =3D mtk_pci_get_dev_state, + .ack_dev_state =3D mtk_pci_ack_dev_state, + .get_irq_id =3D mtk_pci_get_irq_id, + .get_virq_id =3D mtk_pci_get_virq_id, + .register_irq =3D mtk_pci_register_irq, + .unregister_irq =3D mtk_pci_unregister_irq, + .mask_irq =3D mtk_pci_mask_irq, + .unmask_irq =3D mtk_pci_unmask_irq, + .clear_irq =3D mtk_pci_clear_irq, + .register_ext_evt =3D mtk_mhccif_register_evt, + .unregister_ext_evt =3D mtk_mhccif_unregister_evt, + .mask_ext_evt =3D mtk_mhccif_mask_evt, + .unmask_ext_evt =3D mtk_mhccif_unmask_evt, + .clear_ext_evt =3D mtk_mhccif_clear_evt, + .send_ext_evt =3D mtk_mhccif_send_evt, + .get_ext_evt_status =3D mtk_mhccif_get_evt_status, + .get_hp_status =3D mtk_pci_get_hp_status, +}; + +static void mtk_mhccif_isr_work(struct work_struct *work) +{ + struct mtk_pci_priv *priv =3D container_of(work, struct mtk_pci_priv, mhc= cif_work); + struct mtk_md_dev *mdev =3D priv->irq_desc->mdev; + struct mtk_mhccif_cb *cb; + unsigned long flag; + u32 stat, mask; + + stat =3D mtk_mhccif_get_evt_status(mdev); + mask =3D mtk_pci_read32(mdev, priv->cfg->mhccif_rc_base_addr + + MHCCIF_EP2RC_SW_INT_EAP_MASK); + dev_dbg(mdev->dev, "External events: mhccif_stat=3D0x%08X mask=3D0x%08X\n= ", stat, mask); + + if (unlikely(stat =3D=3D U32_MAX && mtk_pci_link_check(mdev))) { + /* When link failed, we don't need to unmask/clear. */ + dev_err(mdev->dev, "Failed to check link in MHCCIF handler.\n"); + return; + } + + stat &=3D ~mask; + spin_lock_irqsave(&priv->mhccif_lock, flag); + list_for_each_entry(cb, &priv->mhccif_cb_list, entry) { + if (cb->chs & stat) + cb->evt_cb(cb->chs & stat, cb->data); + } + spin_unlock_irqrestore(&priv->mhccif_lock, flag); + + mtk_pci_clear_irq(mdev, priv->mhccif_irq_id); + mtk_pci_unmask_irq(mdev, priv->mhccif_irq_id); +} + +static const struct mtk_pci_dev_cfg mtk_dev_cfg_0800 =3D { + .mhccif_rc_base_addr =3D 0x10012000, + .mhccif_trsl_size =3D 0x2000, + .mhccif_rc_reg_trsl_addr =3D 0x12020000, + .istatus_host_ctrl_addr =3D REG_ISTATUS_HOST_CTRL_NEW, + .irq_tbl =3D { + [MTK_IRQ_SRC_DPMAIF] =3D 24, + [MTK_IRQ_SRC_CLDMA0] =3D 25, + [MTK_IRQ_SRC_CLDMA1] =3D 26, + [MTK_IRQ_SRC_CLDMA2] =3D 27, + [MTK_IRQ_SRC_MHCCIF] =3D 28, + [MTK_IRQ_SRC_DPMAIF2] =3D 29, + [MTK_IRQ_SRC_SAP_RGU] =3D 30, + [MTK_IRQ_SRC_CLDMA3] =3D 31, + [MTK_IRQ_SRC_PM_LOCK] =3D 0, + [MTK_IRQ_SRC_DPMAIF3] =3D 7, + }, +}; + +static const struct pci_device_id mtk_pci_ids[] =3D { + MTK_PCI_DEV_CFG(0x0800, mtk_dev_cfg_0800), + { /* end: all zeroes */ } +}; +MODULE_DEVICE_TABLE(pci, mtk_pci_ids); + +static int mtk_pci_bar_init(struct mtk_md_dev *mdev) +{ + struct pci_dev *pdev =3D to_pci_dev(mdev->dev); + struct mtk_pci_priv *priv =3D mdev->hw_priv; + int ret; + + ret =3D pcim_iomap_regions(pdev, MTK_REQUESTED_BARS, mdev->dev_str); + if (ret) { + dev_err(mdev->dev, "Failed to init MMIO. ret=3D%d\n", ret); + return ret; + } + + /* get ioremapped memory */ + priv->mac_reg_base =3D pcim_iomap_table(pdev)[MTK_BAR_0_1_IDX]; + priv->bar23_addr =3D pcim_iomap_table(pdev)[MTK_BAR_2_3_IDX]; + dev_info(mdev->dev, "BAR0/1 Addr=3D0x%p, BAR2/3 Addr=3D0x%p\n", + priv->mac_reg_base, priv->bar23_addr); + /* We use MD view base address "0" to observe registers */ + priv->ext_reg_base =3D priv->bar23_addr - ATR_PCIE_REG_TRSL_ADDR; + + return 0; +} + +static void mtk_pci_bar_exit(struct mtk_md_dev *mdev) +{ + pcim_iounmap_regions(to_pci_dev(mdev->dev), MTK_REQUESTED_BARS); +} + +static int mtk_mhccif_irq_cb(int irq_id, void *data) +{ + struct mtk_md_dev *mdev =3D data; + struct mtk_pci_priv *priv; + + priv =3D mdev->hw_priv; + queue_work(system_highpri_wq, &priv->mhccif_work); + + return 0; +} + +static int mtk_mhccif_init(struct mtk_md_dev *mdev) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + int ret; + + INIT_LIST_HEAD(&priv->mhccif_cb_list); + spin_lock_init(&priv->mhccif_lock); + INIT_WORK(&priv->mhccif_work, mtk_mhccif_isr_work); + + ret =3D mtk_pci_get_irq_id(mdev, MTK_IRQ_SRC_MHCCIF); + if (ret < 0) { + dev_err(mdev->dev, "Failed to get mhccif_irq_id. ret=3D%d\n", ret); + goto err; + } + priv->mhccif_irq_id =3D ret; + + ret =3D mtk_pci_register_irq(mdev, priv->mhccif_irq_id, mtk_mhccif_irq_cb= , mdev); + if (ret) { + dev_err(mdev->dev, "Failed to register mhccif_irq callback\n"); + goto err; + } + + /* To check if the device rebooted. + * The reboot of some PC doesn't cause the device power cycle. + */ + mtk_pci_read32(mdev, priv->cfg->mhccif_rc_base_addr + MHCCIF_EP2RC_SW_INT= _EAP_MASK); + +err: + return ret; +} + +static void mtk_mhccif_exit(struct mtk_md_dev *mdev) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + + mtk_pci_unregister_irq(mdev, priv->mhccif_irq_id); + cancel_work_sync(&priv->mhccif_work); +} + +static irqreturn_t mtk_pci_irq_handler(struct mtk_md_dev *mdev, u32 irq_st= ate) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + int irq_id; + + /* Check whether each set bit has a callback, if has, call it */ + do { + irq_id =3D fls(irq_state) - 1; + irq_state &=3D ~BIT(irq_id); + if (likely(priv->irq_cb_list[irq_id])) + priv->irq_cb_list[irq_id](irq_id, priv->irq_cb_data[irq_id]); + else + dev_err(mdev->dev, "Unhandled irq_id=3D%d, no callback for it.\n", irq_= id); + } while (irq_state); + + return IRQ_HANDLED; +} + +static irqreturn_t mtk_pci_irq_msix(int irq, void *data) +{ + struct mtk_pci_irq_desc *irq_desc =3D data; + struct mtk_md_dev *mdev =3D irq_desc->mdev; + struct mtk_pci_priv *priv; + u32 irq_state, irq_enable; + + priv =3D mdev->hw_priv; + irq_state =3D mtk_pci_mac_read32(priv, REG_MSIX_ISTATUS_HOST_GRP0_0); + irq_enable =3D mtk_pci_mac_read32(priv, REG_IMASK_HOST_MSIX_GRP0_0); + dev_dbg(mdev->dev, "irq_state=3D0x%08X, irq_enable=3D0x%08X, msix_bits=3D= 0x%08X\n", + irq_state, irq_enable, irq_desc->msix_bits); + irq_state &=3D irq_enable; + + if (unlikely(!irq_state) || + unlikely(!((irq_state % GENMASK(priv->irq_cnt - 1, 0)) & irq_desc->ms= ix_bits))) + return IRQ_NONE; + + /* Mask the bit and user needs to unmask by itself */ + mtk_pci_mac_write32(priv, REG_IMASK_HOST_MSIX_CLR_GRP0_0, irq_state & ~BI= T(30)); + + return mtk_pci_irq_handler(mdev, irq_state); +} + +static int mtk_pci_request_irq_msix(struct mtk_md_dev *mdev, int irq_cnt_a= llocated) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + struct mtk_pci_irq_desc *irq_desc; + struct pci_dev *pdev; + int irq_cnt; + int ret, i; + + /* calculate the nearest 2's power number */ + irq_cnt =3D BIT(fls(irq_cnt_allocated) - 1); + pdev =3D to_pci_dev(mdev->dev); + irq_desc =3D priv->irq_desc; + for (i =3D 0; i < irq_cnt; i++) { + irq_desc[i].mdev =3D mdev; + irq_desc[i].msix_bits =3D BIT(i); + snprintf(irq_desc[i].name, MTK_IRQ_NAME_LEN, "msix%d-%s", + i, pci_name(to_pci_dev(mdev->dev))); + ret =3D pci_request_irq(pdev, i, mtk_pci_irq_msix, NULL, + &irq_desc[i], irq_desc[i].name); + if (ret) { + dev_err(mdev->dev, "Failed to request %s: ret=3D%d\n", irq_desc[i].name= , ret); + for (i--; i >=3D 0; i--) + pci_free_irq(pdev, i, &irq_desc[i]); + return ret; + } + } + priv->irq_cnt =3D irq_cnt; + priv->irq_type =3D PCI_IRQ_MSIX; + + if (irq_cnt !=3D MTK_IRQ_CNT_MAX) + mtk_pci_set_msix_merged(priv, irq_cnt); + + return 0; +} + +static int mtk_pci_request_irq(struct mtk_md_dev *mdev) +{ + struct pci_dev *pdev =3D to_pci_dev(mdev->dev); + int irq_cnt; + + irq_cnt =3D pci_alloc_irq_vectors(pdev, MTK_IRQ_CNT_MIN, MTK_IRQ_CNT_MAX,= PCI_IRQ_MSIX); + mdev->msi_nvecs =3D irq_cnt; + + if (irq_cnt < MTK_IRQ_CNT_MIN) { + dev_err(mdev->dev, + "Unable to alloc pci irq vectors. ret=3D%d maxirqcnt=3D%d irqtype=3D0x%= x\n", + irq_cnt, MTK_IRQ_CNT_MAX, PCI_IRQ_MSIX); + return -EFAULT; + } + + return mtk_pci_request_irq_msix(mdev, irq_cnt); +} + +static void mtk_pci_free_irq(struct mtk_md_dev *mdev) +{ + struct pci_dev *pdev =3D to_pci_dev(mdev->dev); + struct mtk_pci_priv *priv =3D mdev->hw_priv; + int i; + + for (i =3D 0; i < priv->irq_cnt; i++) + pci_free_irq(pdev, i, &priv->irq_desc[i]); + + pci_free_irq_vectors(pdev); +} + +static int mtk_pci_probe(struct pci_dev *pdev, const struct pci_device_id = *id) +{ + struct device *dev =3D &pdev->dev; + struct mtk_pci_priv *priv; + struct mtk_md_dev *mdev; + int ret; + + mdev =3D devm_kzalloc(dev, sizeof(*mdev), GFP_KERNEL); + if (!mdev) { + ret =3D -ENOMEM; + goto exit; + } + + priv =3D devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) { + ret =3D -ENOMEM; + goto free_cntx_data; + } + + pci_set_drvdata(pdev, mdev); + priv->cfg =3D (struct mtk_pci_dev_cfg *)id->driver_data; + priv->mdev =3D mdev; + mdev->hw_ver =3D pdev->device; + mdev->hw_ops =3D &mtk_pci_ops; + mdev->hw_priv =3D priv; + mdev->dev =3D dev; + snprintf(mdev->dev_str, MTK_DEV_STR_LEN, "%02x%02x%d", + pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)); + + dev_info(mdev->dev, "Start probe 0x%x, state_saved[%d]\n", + mdev->hw_ver, pdev->state_saved); + + if (pdev->state_saved) + pci_restore_state(pdev); + + /* enable host to device access. */ + ret =3D pcim_enable_device(pdev); + if (ret) { + dev_err(mdev->dev, "Failed to enable pci device.\n"); + goto free_priv_data; + } + + ret =3D dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); + if (ret) { + dev_err(mdev->dev, "Failed to set DMA Mask and Coherent. (ret=3D%d)\n", = ret); + goto disable_device; + } + + ret =3D mtk_pci_bar_init(mdev); + if (ret) + goto disable_device; + + ret =3D mtk_pci_atr_init(mdev); + if (ret) + goto free_bar; + + ret =3D mtk_mhccif_init(mdev); + if (ret) + goto free_bar; + + ret =3D mtk_pci_request_irq(mdev); + if (ret) + goto free_mhccif; + + /* enable device to host interrupt. */ + pci_set_master(pdev); + + mtk_pci_unmask_irq(mdev, priv->mhccif_irq_id); + + dev_info(mdev->dev, "Probe done hw_ver=3D0x%x\n", mdev->hw_ver); + return 0; + +free_mhccif: + mtk_mhccif_exit(mdev); +free_bar: + mtk_pci_bar_exit(mdev); +disable_device: + pci_disable_device(pdev); +free_priv_data: + devm_kfree(dev, priv); +free_cntx_data: + devm_kfree(dev, mdev); +exit: + + return ret; +} + +static void mtk_pci_remove(struct pci_dev *pdev) +{ + struct mtk_md_dev *mdev =3D pci_get_drvdata(pdev); + struct mtk_pci_priv *priv =3D mdev->hw_priv; + struct device *dev =3D &pdev->dev; + int ret; + + mtk_pci_mask_irq(mdev, priv->mhccif_irq_id); + + ret =3D mtk_pci_fldr(mdev); + if (ret) + mtk_mhccif_send_evt(mdev, EXT_EVT_H2D_DEVICE_RESET); + + pci_clear_master(pdev); + mtk_mhccif_exit(mdev); + mtk_pci_free_irq(mdev); + mtk_pci_bar_exit(mdev); + pci_disable_device(pdev); + pci_load_and_free_saved_state(pdev, &priv->saved_state); + devm_kfree(dev, priv); + devm_kfree(dev, mdev); + dev_info(dev, "Remove done, state_saved[%d]\n", pdev->state_saved); +} + +static pci_ers_result_t mtk_pci_error_detected(struct pci_dev *pdev, + pci_channel_state_t state) +{ + return PCI_ERS_RESULT_NEED_RESET; +} + +static pci_ers_result_t mtk_pci_slot_reset(struct pci_dev *pdev) +{ + return PCI_ERS_RESULT_RECOVERED; +} + +static void mtk_pci_io_resume(struct pci_dev *pdev) +{ +} + +static const struct pci_error_handlers mtk_pci_err_handler =3D { + .error_detected =3D mtk_pci_error_detected, + .slot_reset =3D mtk_pci_slot_reset, + .resume =3D mtk_pci_io_resume, +}; + +static struct pci_driver mtk_pci_drv =3D { + .name =3D "mtk_pci_drv", + .id_table =3D mtk_pci_ids, + + .probe =3D mtk_pci_probe, + .remove =3D mtk_pci_remove, + + .err_handler =3D &mtk_pci_err_handler +}; + +static int __init mtk_drv_init(void) +{ + return pci_register_driver(&mtk_pci_drv); +} +module_init(mtk_drv_init); + +static void __exit mtk_drv_exit(void) +{ + pci_unregister_driver(&mtk_pci_drv); +} +module_exit(mtk_drv_exit); + +MODULE_LICENSE("GPL"); diff --git a/drivers/net/wwan/mediatek/pcie/mtk_pci.h b/drivers/net/wwan/me= diatek/pcie/mtk_pci.h new file mode 100644 index 000000000000..b487ca9b302e --- /dev/null +++ b/drivers/net/wwan/mediatek/pcie/mtk_pci.h @@ -0,0 +1,144 @@ +/* SPDX-License-Identifier: BSD-3-Clause-Clear + * + * Copyright (c) 2022, MediaTek Inc. + */ + +#ifndef __MTK_PCI_H__ +#define __MTK_PCI_H__ + +#include +#include + +#include "../mtk_dev.h" + +enum mtk_atr_type { + ATR_PCI2AXI =3D 0, + ATR_AXI2PCI +}; + +enum mtk_atr_src_port { + ATR_SRC_PCI_WIN0 =3D 0, + ATR_SRC_AXIS_0 =3D 2, + ATR_SRC_AXIS_2 =3D 3, + ATR_SRC_AXIS_3 =3D 4, +}; + +enum mtk_atr_dst_port { + ATR_DST_PCI_TRX =3D 0, + ATR_DST_AXIM_0 =3D 4, +}; + +#define ATR_PORT_OFFSET 0x100 +#define ATR_TABLE_OFFSET 0x20 +#define ATR_TABLE_NUM_PER_ATR 8 +#define ATR_WIN0_SRC_ADDR_LSB_DEFT 0x0000007f +#define ATR_PCIE_REG_TRSL_ADDR 0x10000000 +#define ATR_PCIE_REG_SIZE 0x00400000 +#define ATR_PCIE_REG_PORT ATR_SRC_PCI_WIN0 +#define ATR_PCIE_REG_TABLE_NUM 1 +#define ART_PCIE_REG_MHCCIF_TABLE_NUM 0 +#define ATR_PCIE_REG_TRSL_PORT ATR_DST_AXIM_0 +#define ATR_PCIE_DEV_DMA_PORT_START ATR_SRC_AXIS_0 +#define ATR_PCIE_DEV_DMA_PORT_END ATR_SRC_AXIS_2 +#define ATR_PCIE_DEV_DMA_SRC_ADDR 0x00000000 +#define ATR_PCIE_DEV_DMA_TRANSPARENT 1 +#define ATR_PCIE_DEV_DMA_SIZE 0 +#define ATR_PCIE_DEV_DMA_TABLE_NUM 0 +#define ATR_PCIE_DEV_DMA_TRSL_ADDR 0x00000000 + +#define MTK_BAR_0_1_IDX 0 +#define MTK_BAR_2_3_IDX 2 +/* Only use BAR0/1 and 2/3, so we should input 0b0101 for the two bar, + * Input 0xf would cause error. + */ +#define MTK_REQUESTED_BARS ((1 << MTK_BAR_0_1_IDX) | (1 << MTK_BAR_2_3= _IDX)) + +#define MTK_IRQ_CNT_MIN 1 +#define MTK_IRQ_CNT_MAX 32 +#define MTK_IRQ_NAME_LEN 20 + +#define MTK_INVAL_IRQ_SRC -1 + +#define MTK_FORCE_MAC_ACTIVE_BIT BIT(6) +#define MTK_DS_LOCK_REG_BIT BIT(7) + +/* mhccif registers */ +#define MHCCIF_RC2EP_SW_BSY 0x4 +#define MHCCIF_RC2EP_SW_INT_START 0x8 +#define MHCCIF_RC2EP_SW_TCHNUM 0xC +#define MHCCIF_EP2RC_SW_INT_STS 0x10 +#define MHCCIF_EP2RC_SW_INT_ACK 0x14 +#define MHCCIF_EP2RC_SW_INT_EAP_MASK 0x20 +#define MHCCIF_EP2RC_SW_INT_EAP_MASK_SET 0x30 +#define MHCCIF_EP2RC_SW_INT_EAP_MASK_CLR 0x40 +#define MHCCIF_RC2EP_PCIE_PM_COUNTER 0x12C + +#define MTK_PCI_CLASS 0x0D4000 +#define MTK_PCI_VENDOR_ID 0x14C3 + +#define MTK_DISABLE_DS_BIT(grp) BIT(grp) +#define MTK_ENABLE_DS_BIT(grp) BIT((grp) << 8) + +#define MTK_PCI_DEV_CFG(id, cfg) \ +{ \ + PCI_DEVICE(MTK_PCI_VENDOR_ID, id), \ + MTK_PCI_CLASS, PCI_ANY_ID, \ + .driver_data =3D (kernel_ulong_t)&(cfg), \ +} + +struct mtk_pci_dev_cfg { + u32 flag; + u32 mhccif_rc_base_addr; + u32 mhccif_rc_reg_trsl_addr; + u32 mhccif_trsl_size; + u32 istatus_host_ctrl_addr; + int irq_tbl[MTK_IRQ_SRC_MAX]; +}; + +struct mtk_pci_irq_desc { + struct mtk_md_dev *mdev; + u32 msix_bits; + char name[MTK_IRQ_NAME_LEN]; +}; + +struct mtk_pci_priv { + const struct mtk_pci_dev_cfg *cfg; + void *mdev; + void __iomem *bar23_addr; + void __iomem *mac_reg_base; + void __iomem *ext_reg_base; + int rc_hp_on; /* Bridge hotplug status */ + int irq_cnt; + int irq_type; + void *irq_cb_data[MTK_IRQ_CNT_MAX]; + + int (*irq_cb_list[MTK_IRQ_CNT_MAX])(int irq_id, void *data); + struct mtk_pci_irq_desc irq_desc[MTK_IRQ_CNT_MAX]; + struct list_head mhccif_cb_list; + /* mhccif_lock: lock to protect mhccif_cb_list */ + spinlock_t mhccif_lock; + struct work_struct mhccif_work; + int mhccif_irq_id; + struct pci_saved_state *saved_state; +}; + +struct mtk_mhccif_cb { + struct list_head entry; + int (*evt_cb)(u32 status, void *data); + void *data; + u32 chs; +}; + +struct mtk_atr_cfg { + u64 src_addr; + u64 trsl_addr; + u64 size; + u32 type; /* Port type */ + u32 port; /* Port number */ + u32 table; /* Table number (8 tables for each port) */ + u32 trsl_id; + u32 trsl_param; + u32 transparent; +}; + +#endif /* __MTK_PCI_H__ */ diff --git a/drivers/net/wwan/mediatek/pcie/mtk_reg.h b/drivers/net/wwan/me= diatek/pcie/mtk_reg.h new file mode 100644 index 000000000000..23fa7fd9518e --- /dev/null +++ b/drivers/net/wwan/mediatek/pcie/mtk_reg.h @@ -0,0 +1,69 @@ +/* SPDX-License-Identifier: BSD-3-Clause-Clear + * + * Copyright (c) 2022, MediaTek Inc. + */ + +#ifndef __MTK_REG_H__ +#define __MTK_REG_H__ + +enum mtk_ext_evt_h2d { + EXT_EVT_H2D_EXCEPT_ACK =3D 1 << 1, + EXT_EVT_H2D_EXCEPT_CLEARQ_ACK =3D 1 << 2, + EXT_EVT_H2D_PCIE_DS_LOCK =3D 1 << 3, + EXT_EVT_H2D_RESERVED_FOR_DPMAIF =3D 1 << 8, + EXT_EVT_H2D_PCIE_PM_SUSPEND_REQ =3D 1 << 9, + EXT_EVT_H2D_PCIE_PM_RESUME_REQ =3D 1 << 10, + EXT_EVT_H2D_PCIE_PM_SUSPEND_REQ_AP =3D 1 << 11, + EXT_EVT_H2D_PCIE_PM_RESUME_REQ_AP =3D 1 << 12, + EXT_EVT_H2D_DEVICE_RESET =3D 1 << 13, +}; + +#define REG_PCIE_SW_TRIG_INT 0x00BC +#define REG_PCIE_LTSSM_STATUS 0x0150 +#define REG_IMASK_LOCAL 0x0180 +#define REG_ISTATUS_LOCAL 0x0184 +#define REG_INT_ENABLE_HOST 0x0188 +#define REG_ISTATUS_HOST 0x018C +#define REG_PCIE_LOW_POWER_CTRL 0x0194 +#define REG_ISTATUS_HOST_CTRL 0x01AC +#define REG_ISTATUS_PENDING_ADT 0x01D4 +#define REG_INT_ENABLE_HOST_SET 0x01F0 +#define REG_INT_ENABLE_HOST_CLR 0x01F4 +#define REG_PCIE_DMA_DUMMY_0 0x01F8 +#define REG_ISTATUS_HOST_CTRL_NEW 0x031C +#define REG_PCIE_MISC_CTRL 0x0348 +#define REG_PCIE_DUMMY_0 0x03C0 +#define REG_SW_TRIG_INTR_SET 0x03C8 +#define REG_SW_TRIG_INTR_CLR 0x03CC +#define REG_PCIE_CFG_MSIX 0x03EC +#define REG_ATR_PCIE_WIN0_T0_SRC_ADDR_LSB 0x0600 +#define REG_ATR_PCIE_WIN0_T0_SRC_ADDR_MSB 0x0604 +#define REG_ATR_PCIE_WIN0_T0_TRSL_ADDR_LSB 0x0608 +#define REG_ATR_PCIE_WIN0_T0_TRSL_ADDR_MSB 0x060C +#define REG_ATR_PCIE_WIN0_T0_TRSL_PARAM 0x0610 +#define REG_PCIE_DEBUG_DUMMY_0 0x0D00 +#define REG_PCIE_DEBUG_DUMMY_1 0x0D04 +#define REG_PCIE_DEBUG_DUMMY_2 0x0D08 +#define REG_PCIE_DEBUG_DUMMY_3 0x0D0C +#define REG_PCIE_DEBUG_DUMMY_4 0x0D10 +#define REG_PCIE_DEBUG_DUMMY_5 0x0D14 +#define REG_PCIE_DEBUG_DUMMY_6 0x0D18 +#define REG_PCIE_DEBUG_DUMMY_7 0x0D1C +#define REG_PCIE_RESOURCE_STATUS 0x0D28 +#define REG_RC2EP_SW_TRIG_LOCAL_INTR_STAT 0x0D94 +#define REG_RC2EP_SW_TRIG_LOCAL_INTR_SET 0x0D98 +#define REG_RC2EP_SW_TRIG_LOCAL_INTR_CLR 0x0D9C +#define REG_DIS_ASPM_LOWPWR_SET_0 0x0E50 +#define REG_DIS_ASPM_LOWPWR_CLR_0 0x0E54 +#define REG_DIS_ASPM_LOWPWR_SET_1 0x0E58 +#define REG_DIS_ASPM_LOWPWR_CLR_1 0x0E5C +#define REG_DIS_ASPM_LOWPWR_STS_0 0x0E60 +#define REG_DIS_ASPM_LOWPWR_STS_1 0x0E64 +#define REG_PCIE_PEXTP_MAC_SLEEP_CTRL 0x0E70 +#define REG_MSIX_SW_TRIG_SET_GRP0_0 0x0E80 +#define REG_MSIX_ISTATUS_HOST_GRP0_0 0x0F00 +#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 +#endif /* __MTK_REG_H__ */ --=20 2.32.0 From nobody Thu Nov 14 06:37:28 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 40858C6FD1D for ; Fri, 17 Mar 2023 08:14:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229875AbjCQIOB (ORCPT ); Fri, 17 Mar 2023 04:14:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230134AbjCQINk (ORCPT ); Fri, 17 Mar 2023 04:13:40 -0400 Received: from mailgw01.mediatek.com (unknown [60.244.123.138]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4EB52D5886; Fri, 17 Mar 2023 01:13:00 -0700 (PDT) X-UUID: 4b095fdec49b11ed91027fb02e0f1d65-20230317 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=3entTdCvHNayAO/y47dlHOH6X33xCBWJvBs6qAEu2Hk=; b=HyZwMKh45fckb4dCT3RIUTQ24PG348rIropfhFETeMKGLJ9RI9oU767TRzV5N49cm/N+WRANNvZsRDpAyCILzqfVSWlI59DW1SfGnuXnS03AimGJOcdk73HCyurfpSO6tQ9HBknR5MW1AV0aWCns1rTWmdz6v42yGtGd/yrlsL4=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.21,REQID:d3c9387b-c00e-402b-8269-bc4267768b25,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-25 X-CID-META: VersionHash:83295aa,CLOUDID:beb51af6-ddba-41c3-91d9-10eeade8eac7,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:0,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-UUID: 4b095fdec49b11ed91027fb02e0f1d65-20230317 Received: from mtkmbs10n2.mediatek.inc [(172.21.101.183)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1617362656; Fri, 17 Mar 2023 16:11:17 +0800 Received: from mtkmbs11n2.mediatek.inc (172.21.101.187) by mtkmbs11n2.mediatek.inc (172.21.101.187) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Fri, 17 Mar 2023 16:11:17 +0800 Received: from mcddlt001.gcn.mediatek.inc (10.19.240.15) by mtkmbs11n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.25 via Frontend Transport; Fri, 17 Mar 2023 16:11:15 +0800 From: Yanchao Yang To: Loic Poulain , Sergey Ryazanov , Johannes Berg , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , netdev ML , kernel ML CC: Intel experts , Chetan , MTK ML , Liang Lu , Haijun Liu , Hua Yang , Ting Wang , Felix Chen , Mingliang Xu , Min Dong , Aiden Wang , Guohao Zhang , Chris Feng , Yanchao Yang , Lambert Wang , Mingchuang Qiao , Xiayu Zhang , Haozhe Chang Subject: [PATCH net-next v4 02/10] net: wwan: tmi: Add control plane transaction layer Date: Fri, 17 Mar 2023 16:09:34 +0800 Message-ID: <20230317080942.183514-3-yanchao.yang@mediatek.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20230317080942.183514-1-yanchao.yang@mediatek.com> References: <20230317080942.183514-1-yanchao.yang@mediatek.com> MIME-Version: 1.0 X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The control plane implements TX services that reside in the transaction lay= er. The services receive the packets from the port layer and call the correspon= ding DMA components to transmit data to the device. Meanwhile, TX services recei= ve and manage the port control commands from the port layer. The control plane implements RX services that reside in the transaction lay= er. The services receive the downlink packets from the modem and transfer the packets to the corresponding port layer interfaces. Signed-off-by: Yanchao Yang Signed-off-by: Mingliang Xu --- drivers/net/wwan/mediatek/Makefile | 4 +- drivers/net/wwan/mediatek/mtk_ctrl_plane.c | 36 +++++++++++++ drivers/net/wwan/mediatek/mtk_ctrl_plane.h | 30 +++++++++++ drivers/net/wwan/mediatek/mtk_dev.c | 25 +++++++++ drivers/net/wwan/mediatek/mtk_dev.h | 3 ++ drivers/net/wwan/mediatek/pcie/mtk_pci.c | 62 ++++++++++++++++++++++ 6 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 drivers/net/wwan/mediatek/mtk_ctrl_plane.c create mode 100644 drivers/net/wwan/mediatek/mtk_ctrl_plane.h create mode 100644 drivers/net/wwan/mediatek/mtk_dev.c diff --git a/drivers/net/wwan/mediatek/Makefile b/drivers/net/wwan/mediatek= /Makefile index b74192eee432..c93c81597821 100644 --- a/drivers/net/wwan/mediatek/Makefile +++ b/drivers/net/wwan/mediatek/Makefile @@ -3,6 +3,8 @@ MODULE_NAME :=3D mtk_tmi =20 mtk_tmi-y =3D \ - pcie/mtk_pci.o + pcie/mtk_pci.o \ + mtk_dev.o \ + mtk_ctrl_plane.o =20 obj-$(CONFIG_MTK_TMI) +=3D mtk_tmi.o diff --git a/drivers/net/wwan/mediatek/mtk_ctrl_plane.c b/drivers/net/wwan/= mediatek/mtk_ctrl_plane.c new file mode 100644 index 000000000000..1dbfcf8587a1 --- /dev/null +++ b/drivers/net/wwan/mediatek/mtk_ctrl_plane.c @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +/* + * Copyright (c) 2022, MediaTek Inc. + */ + +#include +#include +#include +#include +#include +#include + +#include "mtk_ctrl_plane.h" + +int mtk_ctrl_init(struct mtk_md_dev *mdev) +{ + struct mtk_ctrl_blk *ctrl_blk; + + ctrl_blk =3D devm_kzalloc(mdev->dev, sizeof(*ctrl_blk), GFP_KERNEL); + if (!ctrl_blk) + return -ENOMEM; + + ctrl_blk->mdev =3D mdev; + mdev->ctrl_blk =3D ctrl_blk; + + return 0; +} + +int mtk_ctrl_exit(struct mtk_md_dev *mdev) +{ + struct mtk_ctrl_blk *ctrl_blk =3D mdev->ctrl_blk; + + devm_kfree(mdev->dev, ctrl_blk); + + return 0; +} diff --git a/drivers/net/wwan/mediatek/mtk_ctrl_plane.h b/drivers/net/wwan/= mediatek/mtk_ctrl_plane.h new file mode 100644 index 000000000000..77af4248cb74 --- /dev/null +++ b/drivers/net/wwan/mediatek/mtk_ctrl_plane.h @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: BSD-3-Clause-Clear + * + * Copyright (c) 2022, MediaTek Inc. + */ + +#ifndef __MTK_CTRL_PLANE_H__ +#define __MTK_CTRL_PLANE_H__ + +#include +#include + +#include "mtk_dev.h" + +#define VQ_MTU_3_5K (0xE00) +#define VQ_MTU_63K (0xFC00) + +struct mtk_ctrl_trans { + struct mtk_ctrl_blk *ctrl_blk; + struct mtk_md_dev *mdev; +}; + +struct mtk_ctrl_blk { + struct mtk_md_dev *mdev; + struct mtk_ctrl_trans *trans; +}; + +int mtk_ctrl_init(struct mtk_md_dev *mdev); +int mtk_ctrl_exit(struct mtk_md_dev *mdev); + +#endif /* __MTK_CTRL_PLANE_H__ */ diff --git a/drivers/net/wwan/mediatek/mtk_dev.c b/drivers/net/wwan/mediate= k/mtk_dev.c new file mode 100644 index 000000000000..f63c7e04df6a --- /dev/null +++ b/drivers/net/wwan/mediatek/mtk_dev.c @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +/* + * Copyright (c) 2022, MediaTek Inc. + */ + +#include "mtk_ctrl_plane.h" +#include "mtk_dev.h" + +int mtk_dev_init(struct mtk_md_dev *mdev) +{ + int ret; + + ret =3D mtk_ctrl_init(mdev); + if (ret) + goto err_ctrl_init; + + return 0; +err_ctrl_init: + return ret; +} + +void mtk_dev_exit(struct mtk_md_dev *mdev) +{ + mtk_ctrl_exit(mdev); +} diff --git a/drivers/net/wwan/mediatek/mtk_dev.h b/drivers/net/wwan/mediate= k/mtk_dev.h index acda9ccf05f1..d48fc55ddef0 100644 --- a/drivers/net/wwan/mediatek/mtk_dev.h +++ b/drivers/net/wwan/mediatek/mtk_dev.h @@ -101,8 +101,11 @@ struct mtk_md_dev { u32 hw_ver; int msi_nvecs; char dev_str[MTK_DEV_STR_LEN]; + void *ctrl_blk; }; =20 +int mtk_dev_init(struct mtk_md_dev *mdev); +void mtk_dev_exit(struct mtk_md_dev *mdev); static inline u32 mtk_hw_read32(struct mtk_md_dev *mdev, u64 addr) { return mdev->hw_ops->read32(mdev, addr); diff --git a/drivers/net/wwan/mediatek/pcie/mtk_pci.c b/drivers/net/wwan/me= diatek/pcie/mtk_pci.c index f8590d7319a8..b3de9634a62c 100644 --- a/drivers/net/wwan/mediatek/pcie/mtk_pci.c +++ b/drivers/net/wwan/mediatek/pcie/mtk_pci.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include =20 @@ -16,6 +17,9 @@ #include "mtk_reg.h" =20 #define MTK_PCI_TRANSPARENT_ATR_SIZE (0x3F) +#define MTK_PCI_LOCK_L1SS_STS_MASK (0x1F) +#define MTK_PCI_LOCK_L1SS_POLL_STEP (10) +#define MTK_PCI_LOCK_L1SS_POLL_TIMEOUT (10000) =20 static u32 mtk_pci_mac_read32(struct mtk_pci_priv *priv, u64 addr) { @@ -196,6 +200,41 @@ static void mtk_pci_ack_dev_state(struct mtk_md_dev *m= dev, u32 state) mtk_pci_mac_write32(mdev->hw_priv, REG_PCIE_DEBUG_DUMMY_7, state); } =20 +static void mtk_pci_force_mac_active(struct mtk_md_dev *mdev, bool enable) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + u32 reg; + + reg =3D mtk_pci_mac_read32(priv, REG_PCIE_MISC_CTRL); + if (enable) + reg |=3D MTK_FORCE_MAC_ACTIVE_BIT; + else + reg &=3D ~MTK_FORCE_MAC_ACTIVE_BIT; + mtk_pci_mac_write32(priv, REG_PCIE_MISC_CTRL, reg); +} + +static u32 mtk_pci_get_ds_status(struct mtk_md_dev *mdev) +{ + u32 reg; + + mtk_pci_force_mac_active(mdev, true); + reg =3D mtk_pci_mac_read32(mdev->hw_priv, REG_PCIE_RESOURCE_STATUS); + mtk_pci_force_mac_active(mdev, false); + + return reg; +} + +static void mtk_pci_set_l1ss(struct mtk_md_dev *mdev, u32 type, bool enabl= e) +{ + struct mtk_pci_priv *priv =3D mdev->hw_priv; + u32 addr =3D REG_DIS_ASPM_LOWPWR_SET_0; + + if (enable) + addr =3D REG_DIS_ASPM_LOWPWR_CLR_0; + + mtk_pci_mac_write32(priv, addr, type); +} + static int mtk_pci_get_irq_id(struct mtk_md_dev *mdev, enum mtk_irq_src ir= q_src) { struct mtk_pci_priv *priv =3D mdev->hw_priv; @@ -736,6 +775,7 @@ static int mtk_pci_probe(struct pci_dev *pdev, const st= ruct pci_device_id *id) struct mtk_pci_priv *priv; struct mtk_md_dev *mdev; int ret; + int val; =20 mdev =3D devm_kzalloc(dev, sizeof(*mdev), GFP_KERNEL); if (!mdev) { @@ -794,6 +834,25 @@ static int mtk_pci_probe(struct pci_dev *pdev, const s= truct pci_device_id *id) if (ret) goto free_mhccif; =20 + /* lock l1ss and check result */ + mtk_pci_set_l1ss(mdev, L1SS_BIT_L1(L1SS_PM), false); + ret =3D read_poll_timeout(mtk_pci_get_ds_status, val, + (val & MTK_PCI_LOCK_L1SS_STS_MASK) =3D=3D MTK_PCI_LOCK_L1SS_STS_MASK, + MTK_PCI_LOCK_L1SS_POLL_STEP, + MTK_PCI_LOCK_L1SS_POLL_TIMEOUT, + true, + mdev); + if (ret) { + dev_err(mdev->dev, "Failed to lock L1ss!\n"); + goto free_irq; + } + + ret =3D mtk_dev_init(mdev); + if (ret) { + dev_err(mdev->dev, "Failed to init dev.\n"); + goto free_irq; + } + /* enable device to host interrupt. */ pci_set_master(pdev); =20 @@ -802,6 +861,8 @@ static int mtk_pci_probe(struct pci_dev *pdev, const st= ruct pci_device_id *id) dev_info(mdev->dev, "Probe done hw_ver=3D0x%x\n", mdev->hw_ver); return 0; =20 +free_irq: + mtk_pci_free_irq(mdev); free_mhccif: mtk_mhccif_exit(mdev); free_bar: @@ -825,6 +886,7 @@ static void mtk_pci_remove(struct pci_dev *pdev) int ret; =20 mtk_pci_mask_irq(mdev, priv->mhccif_irq_id); + mtk_dev_exit(mdev); =20 ret =3D mtk_pci_fldr(mdev); if (ret) --=20 2.32.0 From nobody Thu Nov 14 06:37:28 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8D722C7618A for ; Fri, 17 Mar 2023 08:13:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231395AbjCQINz (ORCPT ); Fri, 17 Mar 2023 04:13:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34428 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230043AbjCQINi (ORCPT ); Fri, 17 Mar 2023 04:13:38 -0400 Received: from mailgw01.mediatek.com (unknown [60.244.123.138]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E0E2DCA63; Fri, 17 Mar 2023 01:12:52 -0700 (PDT) X-UUID: 5b4421c2c49b11ed91027fb02e0f1d65-20230317 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=XoQr9HUQ4axj/amyB6m5vlMihsnldnzC4FKkGQF6jFo=; b=qaPOMYDPWZImu/SVapeaKzh2MpYcGRlhe/ht7ORqjOtjTAFsmc1CuazXmLjyHMgeA1R11zuM8bMhnrNVUgTlELp/4gXldf37q3TNQGjn7MkUv9HZxweUNWZtNp0oBeb5RSfnJ73heRWtud4sUsNpWexaSIsDsjjOun+caDuMDJI=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.21,REQID:946eb9e7-26de-4d11-adfb-b7b7395be3d4,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:95,FILE:0,BULK:0,RULE:Release_Ham,ACTI ON:release,TS:70 X-CID-INFO: VERSION:1.1.21,REQID:946eb9e7-26de-4d11-adfb-b7b7395be3d4,IP:0,URL :0,TC:0,Content:-25,EDM:0,RT:0,SF:95,FILE:0,BULK:0,RULE:Spam_GS981B3D,ACTI ON:quarantine,TS:70 X-CID-META: VersionHash:83295aa,CLOUDID:ff1a8db3-beed-4dfc-bd9c-e1b22fa6ccc4,B ulkID:230317161147VRPO4P3D,BulkQuantity:0,Recheck:0,SF:38|29|28|17|19|48,T C:nil,Content:0,EDM:-3,IP:nil,URL:0,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0 ,OSI:0,OSA:0,AV:0 X-CID-BVR: 0,NGT X-CID-BAS: 0,NGT,0,_ X-UUID: 5b4421c2c49b11ed91027fb02e0f1d65-20230317 Received: from mtkmbs10n1.mediatek.inc [(172.21.101.34)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 473486588; Fri, 17 Mar 2023 16:11:44 +0800 Received: from mtkmbs11n2.mediatek.inc (172.21.101.187) by mtkmbs10n2.mediatek.inc (172.21.101.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Fri, 17 Mar 2023 16:11:44 +0800 Received: from mcddlt001.gcn.mediatek.inc (10.19.240.15) by mtkmbs11n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.25 via Frontend Transport; Fri, 17 Mar 2023 16:11:42 +0800 From: Yanchao Yang To: Loic Poulain , Sergey Ryazanov , Johannes Berg , "David S . Miller" , Eric Dumazet , "Jakub Kicinski" , Paolo Abeni , netdev ML , kernel ML CC: Intel experts , Chetan , MTK ML , Liang Lu , Haijun Liu , Hua Yang , Ting Wang , Felix Chen , Mingliang Xu , Min Dong , Aiden Wang , Guohao Zhang , Chris Feng , "Yanchao Yang" , Lambert Wang , Mingchuang Qiao , Xiayu Zhang , Haozhe Chang Subject: [PATCH net-next v4 03/10] net: wwan: tmi: Add control DMA interface Date: Fri, 17 Mar 2023 16:09:35 +0800 Message-ID: <20230317080942.183514-4-yanchao.yang@mediatek.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20230317080942.183514-1-yanchao.yang@mediatek.com> References: <20230317080942.183514-1-yanchao.yang@mediatek.com> MIME-Version: 1.0 X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Cross Layer Direct Memory Access(CLDMA) is the hardware interface used by t= he control plane and designated to translate data between the host and the dev= ice. 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 da= ta. 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 message= s) or the port layer. After CLDMA opening is finished, All RX GPDs are fulfilled and ready to rec= eive data from the device. Signed-off-by: Yanchao Yang Signed-off-by: Min Dong --- drivers/net/wwan/mediatek/Makefile | 6 +- drivers/net/wwan/mediatek/mtk_cldma.c | 260 +++++ drivers/net/wwan/mediatek/mtk_cldma.h | 158 +++ drivers/net/wwan/mediatek/mtk_ctrl_plane.h | 48 + .../wwan/mediatek/pcie/mtk_cldma_drv_t800.c | 930 ++++++++++++++++++ .../wwan/mediatek/pcie/mtk_cldma_drv_t800.h | 20 + 6 files changed, 1420 insertions(+), 2 deletions(-) create mode 100644 drivers/net/wwan/mediatek/mtk_cldma.c create mode 100644 drivers/net/wwan/mediatek/mtk_cldma.h create mode 100644 drivers/net/wwan/mediatek/pcie/mtk_cldma_drv_t800.c create mode 100644 drivers/net/wwan/mediatek/pcie/mtk_cldma_drv_t800.h diff --git a/drivers/net/wwan/mediatek/Makefile b/drivers/net/wwan/mediatek= /Makefile index c93c81597821..23a1cbc06ef6 100644 --- a/drivers/net/wwan/mediatek/Makefile +++ b/drivers/net/wwan/mediatek/Makefile @@ -4,7 +4,9 @@ MODULE_NAME :=3D mtk_tmi =20 mtk_tmi-y =3D \ pcie/mtk_pci.o \ - mtk_dev.o \ - mtk_ctrl_plane.o + mtk_dev.o \ + mtk_ctrl_plane.o \ + mtk_cldma.o \ + pcie/mtk_cldma_drv_t800.o =20 obj-$(CONFIG_MTK_TMI) +=3D mtk_tmi.o diff --git a/drivers/net/wwan/mediatek/mtk_cldma.c b/drivers/net/wwan/media= tek/mtk_cldma.c new file mode 100644 index 000000000000..a1c5564e670f --- /dev/null +++ b/drivers/net/wwan/mediatek/mtk_cldma.c @@ -0,0 +1,260 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +/* + * Copyright (c) 2022, MediaTek Inc. + */ + +#include +#include +#include +#include + +#include "mtk_cldma.h" +#include "pcie/mtk_cldma_drv_t800.h" + +/** + * mtk_cldma_init() - Initialize CLDMA + * @trans: pointer to transaction structure + * + * Return: + * * 0 - OK + * * -ENOMEM - out of memory + */ +static int mtk_cldma_init(struct mtk_ctrl_trans *trans) +{ + struct cldma_dev *cd; + + cd =3D devm_kzalloc(trans->mdev->dev, sizeof(*cd), GFP_KERNEL); + if (!cd) + return -ENOMEM; + + cd->trans =3D trans; + cd->hw_ops.init =3D mtk_cldma_hw_init_t800; + cd->hw_ops.exit =3D mtk_cldma_hw_exit_t800; + cd->hw_ops.txq_alloc =3D mtk_cldma_txq_alloc_t800; + cd->hw_ops.rxq_alloc =3D mtk_cldma_rxq_alloc_t800; + cd->hw_ops.txq_free =3D mtk_cldma_txq_free_t800; + cd->hw_ops.rxq_free =3D mtk_cldma_rxq_free_t800; + cd->hw_ops.start_xfer =3D mtk_cldma_start_xfer_t800; + + trans->dev[CLDMA_CLASS_ID] =3D cd; + + return 0; +} + +/** + * mtk_cldma_exit() - De-Initialize CLDMA + * @trans: pointer to transaction structure + * + * Return: + * * 0 - OK + */ +static int mtk_cldma_exit(struct mtk_ctrl_trans *trans) +{ + struct cldma_dev *cd; + + cd =3D trans->dev[CLDMA_CLASS_ID]; + if (!cd) + return 0; + + devm_kfree(trans->mdev->dev, cd); + + return 0; +} + +/** + * mtk_cldma_open() - Initialize CLDMA hardware queue + * @cd: pointer to CLDMA device + * @skb: pointer to socket buffer + * + * Return: + * * 0 - OK + * * -EBUSY - hardware queue is busy + * * -EIO - failed to initialize hardware queue + * * -EINVAL - invalid input parameters + */ +static int mtk_cldma_open(struct cldma_dev *cd, struct sk_buff *skb) +{ + struct trb_open_priv *trb_open_priv =3D (struct trb_open_priv *)skb->data; + struct trb *trb =3D (struct trb *)skb->cb; + struct cldma_hw *hw; + struct virtq *vq; + struct txq *txq; + struct rxq *rxq; + int err =3D 0; + + vq =3D cd->trans->vq_tbl + trb->vqno; + hw =3D cd->cldma_hw[vq->hif_id & HIF_ID_BITMASK]; + trb_open_priv->tx_mtu =3D vq->tx_mtu; + trb_open_priv->rx_mtu =3D vq->rx_mtu; + if (unlikely(vq->rxqno < 0 || vq->rxqno >=3D HW_QUEUE_NUM) || + unlikely(vq->txqno < 0 || vq->txqno >=3D HW_QUEUE_NUM)) { + err =3D -EINVAL; + goto exit; + } + + if (hw->txq[vq->txqno] || hw->rxq[vq->rxqno]) { + err =3D -EBUSY; + goto exit; + } + + txq =3D cd->hw_ops.txq_alloc(hw, skb); + if (!txq) { + err =3D -EIO; + goto exit; + } + + rxq =3D cd->hw_ops.rxq_alloc(hw, skb); + if (!rxq) { + err =3D -EIO; + cd->hw_ops.txq_free(hw, trb->vqno); + goto exit; + } + +exit: + trb->status =3D err; + trb->trb_complete(skb); + + return err; +} + +/** + * mtk_cldma_tx() - start CLDMA TX transaction + * @cd: pointer to CLDMA device + * @skb: pointer to socket buffer + * + * Return: + * * 0 - OK + * * -EPIPE - hardware queue is broken + */ +static int mtk_cldma_tx(struct cldma_dev *cd, struct sk_buff *skb) +{ + struct trb *trb =3D (struct trb *)skb->cb; + struct cldma_hw *hw; + struct virtq *vq; + struct txq *txq; + + vq =3D cd->trans->vq_tbl + trb->vqno; + hw =3D cd->cldma_hw[vq->hif_id & HIF_ID_BITMASK]; + txq =3D hw->txq[vq->txqno]; + if (txq->is_stopping) + return -EPIPE; + + cd->hw_ops.start_xfer(hw, vq->txqno); + + return 0; +} + +/** + * mtk_cldma_close() - De-Initialize CLDMA hardware queue + * @cd: pointer to CLDMA device + * @skb: pointer to socket buffer + * + * Return: + * * 0 - OK + */ +static int mtk_cldma_close(struct cldma_dev *cd, struct sk_buff *skb) +{ + struct trb *trb =3D (struct trb *)skb->cb; + struct cldma_hw *hw; + struct virtq *vq; + + vq =3D cd->trans->vq_tbl + trb->vqno; + hw =3D cd->cldma_hw[vq->hif_id & HIF_ID_BITMASK]; + + cd->hw_ops.txq_free(hw, trb->vqno); + cd->hw_ops.rxq_free(hw, trb->vqno); + + trb->status =3D 0; + trb->trb_complete(skb); + + return 0; +} + +static int mtk_cldma_submit_tx(void *dev, struct sk_buff *skb) +{ + struct trb *trb =3D (struct trb *)skb->cb; + struct cldma_dev *cd =3D dev; + dma_addr_t data_dma_addr; + struct cldma_hw *hw; + struct tx_req *req; + struct virtq *vq; + struct txq *txq; + int err; + + vq =3D cd->trans->vq_tbl + trb->vqno; + hw =3D cd->cldma_hw[vq->hif_id & HIF_ID_BITMASK]; + txq =3D hw->txq[vq->txqno]; + + if (!txq->req_budget) + return -EAGAIN; + + data_dma_addr =3D dma_map_single(hw->mdev->dev, skb->data, skb->len, DMA_= TO_DEVICE); + err =3D dma_mapping_error(hw->mdev->dev, data_dma_addr); + if (unlikely(err)) { + dev_err(hw->mdev->dev, "Failed to map dma!\n"); + return err; + } + + mutex_lock(&txq->lock); + txq->req_budget--; + mutex_unlock(&txq->lock); + + req =3D txq->req_pool + txq->wr_idx; + req->gpd->tx_gpd.debug_id =3D 0x01; + req->gpd->tx_gpd.data_buff_ptr_h =3D cpu_to_le32((u64)(data_dma_addr) >> = 32); + req->gpd->tx_gpd.data_buff_ptr_l =3D cpu_to_le32(data_dma_addr); + req->gpd->tx_gpd.data_buff_len =3D cpu_to_le16(skb->len); + req->gpd->tx_gpd.gpd_flags =3D CLDMA_GPD_FLAG_IOC | CLDMA_GPD_FLAG_HWO; + + req->data_vm_addr =3D skb->data; + req->data_dma_addr =3D data_dma_addr; + req->data_len =3D skb->len; + req->skb =3D skb; + txq->wr_idx =3D (txq->wr_idx + 1) % txq->req_pool_size; + + wmb(); /* ensure GPD setup done before HW start */ + + return 0; +} + +/** + * mtk_cldma_trb_process() - Dispatch trb request to low-level CLDMA routi= ne + * @dev: pointer to CLDMA device + * @skb: pointer to socket buffer + * + * Return: + * * 0 - OK + * * -EBUSY - hardware queue is busy + * * -EINVAL - invalid input + * * -EIO - failed to initialize hardware queue + * * -EPIPE - hardware queue is broken + */ +static int mtk_cldma_trb_process(void *dev, struct sk_buff *skb) +{ + struct trb *trb =3D (struct trb *)skb->cb; + struct cldma_dev *cd =3D dev; + int err; + + switch (trb->cmd) { + case TRB_CMD_ENABLE: + err =3D mtk_cldma_open(cd, skb); + break; + case TRB_CMD_TX: + err =3D mtk_cldma_tx(cd, skb); + break; + case TRB_CMD_DISABLE: + err =3D mtk_cldma_close(cd, skb); + break; + default: + err =3D -EINVAL; + } + + return err; +} + +struct hif_ops cldma_ops =3D { + .init =3D mtk_cldma_init, + .exit =3D mtk_cldma_exit, + .trb_process =3D mtk_cldma_trb_process, + .submit_tx =3D mtk_cldma_submit_tx, +}; diff --git a/drivers/net/wwan/mediatek/mtk_cldma.h b/drivers/net/wwan/media= tek/mtk_cldma.h new file mode 100644 index 000000000000..4fd5f826bcf6 --- /dev/null +++ b/drivers/net/wwan/mediatek/mtk_cldma.h @@ -0,0 +1,158 @@ +/* SPDX-License-Identifier: BSD-3-Clause-Clear + * + * Copyright (c) 2022, MediaTek Inc. + */ + +#ifndef __MTK_CLDMA_H__ +#define __MTK_CLDMA_H__ + +#include + +#include "mtk_ctrl_plane.h" +#include "mtk_dev.h" + +#define HW_QUEUE_NUM 8 +#define ALLQ (0XFF) +#define LINK_ERROR_VAL (0XFFFFFFFF) + +#define CLDMA_CLASS_ID 0 + +#define NR_CLDMA 2 +#define CLDMA0 (((CLDMA_CLASS_ID) << HIF_CLASS_SHIFT) + 0) +#define CLDMA1 (((CLDMA_CLASS_ID) << HIF_CLASS_SHIFT) + 1) + +#define TXQ(N) (N) +#define RXQ(N) (N) + +#define CLDMA_GPD_FLAG_HWO BIT(0) +#define CLDMA_GPD_FLAG_IOC BIT(7) + +enum mtk_ip_busy_src { + IP_BUSY_TXDONE =3D 0, + IP_BUSY_RXDONE =3D 24, +}; + +enum mtk_intr_type { + QUEUE_XFER_DONE =3D 0, + QUEUE_ERROR =3D 16, + INVALID_TYPE +}; + +enum mtk_tx_rx { + DIR_TX, + DIR_RX, + INVALID_DIR +}; + +union gpd { + struct { + u8 gpd_flags; + u8 non_used1; + __le16 data_allow_len; + __le32 next_gpd_ptr_h; + __le32 next_gpd_ptr_l; + __le32 data_buff_ptr_h; + __le32 data_buff_ptr_l; + __le16 data_recv_len; + u8 non_used2; + u8 debug_id; + } rx_gpd; + + struct { + u8 gpd_flags; + u8 non_used1; + u8 non_used2; + u8 debug_id; + __le32 next_gpd_ptr_h; + __le32 next_gpd_ptr_l; + __le32 data_buff_ptr_h; + __le32 data_buff_ptr_l; + __le16 data_buff_len; + __le16 non_used3; + } tx_gpd; +}; + +struct rx_req { + union gpd *gpd; + int mtu; + struct sk_buff *skb; + size_t data_len; + dma_addr_t gpd_dma_addr; + dma_addr_t data_dma_addr; +}; + +struct rxq { + struct cldma_hw *hw; + int rxqno; + int vqno; + struct virtq *vq; + struct work_struct rx_done_work; + struct rx_req *req_pool; + int req_pool_size; + int free_idx; + unsigned short rx_done_cnt; + void *arg; + int (*rx_done)(struct sk_buff *skb, int len, void *priv); +}; + +struct tx_req { + union gpd *gpd; + int mtu; + void *data_vm_addr; + size_t data_len; + dma_addr_t data_dma_addr; + dma_addr_t gpd_dma_addr; + struct sk_buff *skb; + int (*trb_complete)(struct sk_buff *skb); +}; + +struct txq { + struct cldma_hw *hw; + int txqno; + int vqno; + struct virtq *vq; + struct mutex lock; /* protect structure fields */ + struct work_struct tx_done_work; + struct tx_req *req_pool; + int req_pool_size; + int req_budget; + int wr_idx; + int free_idx; + bool tx_started; + bool is_stopping; + unsigned short tx_done_cnt; +}; + +struct cldma_dev; +struct cldma_hw; + +struct cldma_hw_ops { + int (*init)(struct cldma_dev *cd, int hif_id); + int (*exit)(struct cldma_dev *cd, int hif_id); + struct txq* (*txq_alloc)(struct cldma_hw *hw, struct sk_buff *skb); + struct rxq* (*rxq_alloc)(struct cldma_hw *hw, struct sk_buff *skb); + int (*txq_free)(struct cldma_hw *hw, int vqno); + int (*rxq_free)(struct cldma_hw *hw, int vqno); + int (*start_xfer)(struct cldma_hw *hw, int qno); +}; + +struct cldma_hw { + int hif_id; + int base_addr; + int pci_ext_irq_id; + struct mtk_md_dev *mdev; + struct cldma_dev *cd; + struct txq *txq[HW_QUEUE_NUM]; + struct rxq *rxq[HW_QUEUE_NUM]; + struct dma_pool *dma_pool; + struct workqueue_struct *wq; +}; + +struct cldma_dev { + struct cldma_hw *cldma_hw[NR_CLDMA]; + struct mtk_ctrl_trans *trans; + struct cldma_hw_ops hw_ops; +}; + +extern struct hif_ops cldma_ops; +#endif diff --git a/drivers/net/wwan/mediatek/mtk_ctrl_plane.h b/drivers/net/wwan/= mediatek/mtk_ctrl_plane.h index 77af4248cb74..32cd8dc7bdb7 100644 --- a/drivers/net/wwan/mediatek/mtk_ctrl_plane.h +++ b/drivers/net/wwan/mediatek/mtk_ctrl_plane.h @@ -14,7 +14,55 @@ #define VQ_MTU_3_5K (0xE00) #define VQ_MTU_63K (0xFC00) =20 +#define HIF_CLASS_NUM (1) +#define HIF_CLASS_SHIFT (8) +#define HIF_ID_BITMASK (0x01) + +enum mtk_trb_cmd_type { + TRB_CMD_ENABLE =3D 1, + TRB_CMD_TX, + TRB_CMD_DISABLE, +}; + +struct trb_open_priv { + u16 tx_mtu; + u16 rx_mtu; + int (*rx_done)(struct sk_buff *skb, int len, void *priv); +}; + +struct trb { + u8 vqno; + enum mtk_trb_cmd_type cmd; + int status; + struct kref kref; + void *priv; + int (*trb_complete)(struct sk_buff *skb); +}; + +struct virtq { + int vqno; + int hif_id; + int txqno; + int rxqno; + int tx_mtu; + int rx_mtu; + int tx_req_num; + int rx_req_num; +}; + +struct mtk_ctrl_trans; + +struct hif_ops { + int (*init)(struct mtk_ctrl_trans *trans); + int (*exit)(struct mtk_ctrl_trans *trans); + int (*submit_tx)(void *dev, struct sk_buff *skb); + int (*trb_process)(void *dev, struct sk_buff *skb); +}; + struct mtk_ctrl_trans { + struct virtq *vq_tbl; + void *dev[HIF_CLASS_NUM]; + struct hif_ops *ops[HIF_CLASS_NUM]; struct mtk_ctrl_blk *ctrl_blk; struct mtk_md_dev *mdev; }; diff --git a/drivers/net/wwan/mediatek/pcie/mtk_cldma_drv_t800.c b/drivers/= net/wwan/mediatek/pcie/mtk_cldma_drv_t800.c new file mode 100644 index 000000000000..635991b9b9b7 --- /dev/null +++ b/drivers/net/wwan/mediatek/pcie/mtk_cldma_drv_t800.c @@ -0,0 +1,930 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +/* + * Copyright (c) 2022, MediaTek Inc. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../mtk_ctrl_plane.h" +#include "../mtk_dev.h" +#include "mtk_cldma_drv_t800.h" +#include "mtk_reg.h" + +#define DMA_POOL_NAME_LEN 64 + +#define CLDMA_STOP_HW_WAIT_TIME_MS (20) +#define CLDMA_STOP_HW_POLLING_MAX_CNT (10) + +#define CLDMA0_BASE_ADDR (0x1021C000) +#define CLDMA1_BASE_ADDR (0x1021E000) + +/* CLDMA IN(Tx) */ +#define REG_CLDMA_UL_START_ADDRL_0 (0x0004) +#define REG_CLDMA_UL_START_ADDRH_0 (0x0008) +#define REG_CLDMA_UL_STATUS (0x0084) +#define REG_CLDMA_UL_START_CMD (0x0088) +#define REG_CLDMA_UL_RESUME_CMD (0x008C) +#define REG_CLDMA_UL_STOP_CMD (0x0090) +#define REG_CLDMA_UL_ERROR (0x0094) +#define REG_CLDMA_UL_CFG (0x0098) +#define REG_CLDMA_UL_DUMMY_0 (0x009C) + +/* CLDMA OUT(Rx) */ +#define REG_CLDMA_SO_START_CMD (0x0400 + 0x01BC) +#define REG_CLDMA_SO_RESUME_CMD (0x0400 + 0x01C0) +#define REG_CLDMA_SO_STOP_CMD (0x0400 + 0x01C4) +#define REG_CLDMA_SO_DUMMY_0 (0x0400 + 0x0108) +#define REG_CLDMA_SO_CFG (0x0400 + 0x0004) +#define REG_CLDMA_SO_START_ADDRL_0 (0x0400 + 0x0078) +#define REG_CLDMA_SO_START_ADDRH_0 (0x0400 + 0x007C) +#define REG_CLDMA_SO_CUR_ADDRL_0 (0x0400 + 0x00B8) +#define REG_CLDMA_SO_CUR_ADDRH_0 (0x0400 + 0x00BC) +#define REG_CLDMA_SO_STATUS (0x0400 + 0x00F8) + +/* CLDMA MISC */ +#define REG_CLDMA_L2TISAR0 (0x0800 + 0x0010) +#define REG_CLDMA_L2TISAR1 (0x0800 + 0x0014) +#define REG_CLDMA_L2TIMR0 (0x0800 + 0x0018) +#define REG_CLDMA_L2TIMR1 (0x0800 + 0x001C) +#define REG_CLDMA_L2TIMCR0 (0x0800 + 0x0020) +#define REG_CLDMA_L2TIMCR1 (0x0800 + 0x0024) +#define REG_CLDMA_L2TIMSR0 (0x0800 + 0x0028) +#define REG_CLDMA_L2TIMSR1 (0x0800 + 0x002C) +#define REG_CLDMA_L3TISAR0 (0x0800 + 0x0030) +#define REG_CLDMA_L3TISAR1 (0x0800 + 0x0034) +#define REG_CLDMA_L3TIMR0 (0x0800 + 0x0038) +#define REG_CLDMA_L3TIMR1 (0x0800 + 0x003C) +#define REG_CLDMA_L3TIMCR0 (0x0800 + 0x0040) +#define REG_CLDMA_L3TIMCR1 (0x0800 + 0x0044) +#define REG_CLDMA_L3TIMSR0 (0x0800 + 0x0048) +#define REG_CLDMA_L3TIMSR1 (0x0800 + 0x004C) +#define REG_CLDMA_L2RISAR0 (0x0800 + 0x0050) +#define REG_CLDMA_L2RISAR1 (0x0800 + 0x0054) +#define REG_CLDMA_L3RISAR0 (0x0800 + 0x0070) +#define REG_CLDMA_L3RISAR1 (0x0800 + 0x0074) +#define REG_CLDMA_L3RIMR0 (0x0800 + 0x0078) +#define REG_CLDMA_L3RIMR1 (0x0800 + 0x007C) +#define REG_CLDMA_L3RIMCR0 (0x0800 + 0x0080) +#define REG_CLDMA_L3RIMCR1 (0x0800 + 0x0084) +#define REG_CLDMA_L3RIMSR0 (0x0800 + 0x0088) +#define REG_CLDMA_L3RIMSR1 (0x0800 + 0x008C) +#define REG_CLDMA_IP_BUSY (0x0800 + 0x00B4) +#define REG_CLDMA_L3TISAR2 (0x0800 + 0x00C0) +#define REG_CLDMA_L3TIMR2 (0x0800 + 0x00C4) +#define REG_CLDMA_L3TIMCR2 (0x0800 + 0x00C8) +#define REG_CLDMA_L3TIMSR2 (0x0800 + 0x00CC) + +#define REG_CLDMA_L2RIMR0 (0x0800 + 0x00E8) +#define REG_CLDMA_L2RIMR1 (0x0800 + 0x00EC) +#define REG_CLDMA_L2RIMCR0 (0x0800 + 0x00F0) +#define REG_CLDMA_L2RIMCR1 (0x0800 + 0x00F4) +#define REG_CLDMA_L2RIMSR0 (0x0800 + 0x00F8) +#define REG_CLDMA_L2RIMSR1 (0x0800 + 0x00FC) + +#define REG_CLDMA_INT_EAP_USIP_MASK (0x0800 + 0x011C) +#define REG_CLDMA_RQ1_GPD_DONE_CNT (0x0800 + 0x0174) +#define REG_CLDMA_TQ1_GPD_DONE_CNT (0x0800 + 0x0184) + +#define REG_CLDMA_IP_BUSY_TO_PCIE_MASK (0x0800 + 0x0194) +#define REG_CLDMA_IP_BUSY_TO_PCIE_MASK_SET (0x0800 + 0x0198) +#define REG_CLDMA_IP_BUSY_TO_PCIE_MASK_CLR (0x0800 + 0x019C) + +#define REG_CLDMA_IP_BUSY_TO_AP_MASK (0x0800 + 0x0200) +#define REG_CLDMA_IP_BUSY_TO_AP_MASK_SET (0x0800 + 0x0204) +#define REG_CLDMA_IP_BUSY_TO_AP_MASK_CLR (0x0800 + 0x0208) + +/* CLDMA RESET */ +#define REG_INFRA_RST0_SET (0x120) +#define REG_INFRA_RST0_CLR (0x124) +#define REG_CLDMA0_RST_SET_BIT (8) +#define REG_CLDMA0_RST_CLR_BIT (8) + +static void mtk_cldma_setup_start_addr(struct mtk_md_dev *mdev, int base, + enum mtk_tx_rx dir, int qno, dma_addr_t addr) +{ + unsigned int addr_l; + unsigned int addr_h; + + if (dir =3D=3D DIR_TX) { + addr_l =3D base + REG_CLDMA_UL_START_ADDRL_0 + qno * HW_QUEUE_NUM; + addr_h =3D base + REG_CLDMA_UL_START_ADDRH_0 + qno * HW_QUEUE_NUM; + } else { + addr_l =3D base + REG_CLDMA_SO_START_ADDRL_0 + qno * HW_QUEUE_NUM; + addr_h =3D base + REG_CLDMA_SO_START_ADDRH_0 + qno * HW_QUEUE_NUM; + } + + mtk_hw_write32(mdev, addr_l, (u32)addr); + mtk_hw_write32(mdev, addr_h, (u32)((u64)addr >> 32)); +} + +static void mtk_cldma_mask_intr(struct mtk_md_dev *mdev, int base, + enum mtk_tx_rx dir, int qno, enum mtk_intr_type type) +{ + u32 addr; + u32 val; + + if (dir =3D=3D DIR_TX) + addr =3D base + REG_CLDMA_L2TIMSR0; + else + addr =3D base + REG_CLDMA_L2RIMSR0; + + if (qno =3D=3D ALLQ) + val =3D qno << type; + else + val =3D BIT(qno) << type; + + mtk_hw_write32(mdev, addr, val); +} + +static void mtk_cldma_unmask_intr(struct mtk_md_dev *mdev, int base, + enum mtk_tx_rx dir, int qno, enum mtk_intr_type type) +{ + u32 addr; + u32 val; + + if (dir =3D=3D DIR_TX) + addr =3D base + REG_CLDMA_L2TIMCR0; + else + addr =3D base + REG_CLDMA_L2RIMCR0; + + if (qno =3D=3D ALLQ) + val =3D qno << type; + else + val =3D BIT(qno) << type; + + mtk_hw_write32(mdev, addr, val); +} + +static void mtk_cldma_clr_intr_status(struct mtk_md_dev *mdev, int base, + int dir, int qno, enum mtk_intr_type type) +{ + u32 addr; + u32 val; + + if (type =3D=3D QUEUE_ERROR) { + if (dir =3D=3D DIR_TX) { + val =3D mtk_hw_read32(mdev, base + REG_CLDMA_L3TISAR0); + mtk_hw_write32(mdev, base + REG_CLDMA_L3TISAR0, val); + val =3D mtk_hw_read32(mdev, base + REG_CLDMA_L3TISAR1); + mtk_hw_write32(mdev, base + REG_CLDMA_L3TISAR1, val); + } else { + val =3D mtk_hw_read32(mdev, base + REG_CLDMA_L3RISAR0); + mtk_hw_write32(mdev, base + REG_CLDMA_L3RISAR0, val); + val =3D mtk_hw_read32(mdev, base + REG_CLDMA_L3RISAR1); + mtk_hw_write32(mdev, base + REG_CLDMA_L3RISAR1, val); + } + } + + if (dir =3D=3D DIR_TX) + addr =3D base + REG_CLDMA_L2TISAR0; + else + addr =3D base + REG_CLDMA_L2RISAR0; + + if (qno =3D=3D ALLQ) + val =3D qno << type; + else + val =3D BIT(qno) << type; + + mtk_hw_write32(mdev, addr, val); + val =3D mtk_hw_read32(mdev, addr); +} + +static u32 mtk_cldma_check_intr_status(struct mtk_md_dev *mdev, int base, + int dir, int qno, enum mtk_intr_type type) +{ + u32 addr; + u32 val; + u32 sta; + + if (dir =3D=3D DIR_TX) + addr =3D base + REG_CLDMA_L2TISAR0; + else + addr =3D base + REG_CLDMA_L2RISAR0; + + val =3D mtk_hw_read32(mdev, addr); + if (val =3D=3D LINK_ERROR_VAL) + sta =3D val; + else if (qno =3D=3D ALLQ) + sta =3D (val >> type) & 0xFF; + else + sta =3D (val >> type) & BIT(qno); + return sta; +} + +static void mtk_cldma_start_queue(struct mtk_md_dev *mdev, int base, enum = mtk_tx_rx dir, int qno) +{ + u32 val =3D BIT(qno); + u32 addr; + + if (dir =3D=3D DIR_TX) + addr =3D base + REG_CLDMA_UL_START_CMD; + else + addr =3D base + REG_CLDMA_SO_START_CMD; + + mtk_hw_write32(mdev, addr, val); +} + +static void mtk_cldma_resume_queue(struct mtk_md_dev *mdev, int base, enum= mtk_tx_rx dir, int qno) +{ + u32 val =3D BIT(qno); + u32 addr; + + if (dir =3D=3D DIR_TX) + addr =3D base + REG_CLDMA_UL_RESUME_CMD; + else + addr =3D base + REG_CLDMA_SO_RESUME_CMD; + + mtk_hw_write32(mdev, addr, val); +} + +static u32 mtk_cldma_queue_status(struct mtk_md_dev *mdev, int base, enum = mtk_tx_rx dir, int qno) +{ + u32 addr; + u32 val; + + if (dir =3D=3D DIR_TX) + addr =3D base + REG_CLDMA_UL_STATUS; + else + addr =3D base + REG_CLDMA_SO_STATUS; + + val =3D mtk_hw_read32(mdev, addr); + + if (qno =3D=3D ALLQ || val =3D=3D LINK_ERROR_VAL) + return val; + else + return val & BIT(qno); +} + +static void mtk_cldma_mask_ip_busy_to_pci(struct mtk_md_dev *mdev, + int base, int qno, enum mtk_ip_busy_src type) +{ + if (qno =3D=3D ALLQ) + mtk_hw_write32(mdev, base + REG_CLDMA_IP_BUSY_TO_PCIE_MASK_SET, qno << t= ype); + else + mtk_hw_write32(mdev, base + REG_CLDMA_IP_BUSY_TO_PCIE_MASK_SET, BIT(qno)= << type); +} + +static void mtk_cldma_unmask_ip_busy_to_pci(struct mtk_md_dev *mdev, + int base, int qno, enum mtk_ip_busy_src type) +{ + if (qno =3D=3D ALLQ) + mtk_hw_write32(mdev, base + REG_CLDMA_IP_BUSY_TO_PCIE_MASK_CLR, qno << t= ype); + else + mtk_hw_write32(mdev, base + REG_CLDMA_IP_BUSY_TO_PCIE_MASK_CLR, BIT(qno)= << type); +} + +static void mtk_cldma_stop_queue(struct mtk_md_dev *mdev, int base, enum m= tk_tx_rx dir, int qno) +{ + u32 val =3D (qno =3D=3D ALLQ) ? qno : BIT(qno); + u32 addr; + + if (dir =3D=3D DIR_TX) + addr =3D base + REG_CLDMA_UL_STOP_CMD; + else + addr =3D base + REG_CLDMA_SO_STOP_CMD; + + mtk_hw_write32(mdev, addr, val); +} + +static void mtk_cldma_clear_ip_busy(struct mtk_md_dev *mdev, int base) +{ + mtk_hw_write32(mdev, base + REG_CLDMA_IP_BUSY, 0x01); +} + +static void mtk_cldma_hw_init(struct mtk_md_dev *mdev, int base) +{ + u32 val =3D mtk_hw_read32(mdev, base + REG_CLDMA_UL_CFG); + + val =3D (val & (~(0x7 << 5))) | ((0x4) << 5); + mtk_hw_write32(mdev, base + REG_CLDMA_UL_CFG, val); + + val =3D mtk_hw_read32(mdev, base + REG_CLDMA_SO_CFG); + val =3D (val & (~(0x7 << 10))) | ((0x4) << 10) | (1 << 2); + mtk_hw_write32(mdev, base + REG_CLDMA_SO_CFG, val); + + mtk_hw_write32(mdev, base + REG_CLDMA_IP_BUSY_TO_PCIE_MASK_CLR, 0); + mtk_hw_write32(mdev, base + REG_CLDMA_IP_BUSY_TO_AP_MASK_CLR, 0); + + /* enable interrupt to PCIe */ + mtk_hw_write32(mdev, base + REG_CLDMA_INT_EAP_USIP_MASK, 0); + + /* disable illegal memory check */ + mtk_hw_write32(mdev, base + REG_CLDMA_UL_DUMMY_0, 1); + mtk_hw_write32(mdev, base + REG_CLDMA_SO_DUMMY_0, 1); +} + +static void mtk_cldma_tx_done_work(struct work_struct *work) +{ + struct txq *txq =3D container_of(work, struct txq, tx_done_work); + struct mtk_md_dev *mdev =3D txq->hw->mdev; + struct tx_req *req; + unsigned int state; + struct trb *trb; + int i; + +again: + for (i =3D 0; i < txq->req_pool_size; i++) { + req =3D txq->req_pool + txq->free_idx; + if ((req->gpd->tx_gpd.gpd_flags & CLDMA_GPD_FLAG_HWO) || !req->data_vm_a= ddr) + break; + + dma_unmap_single(mdev->dev, req->data_dma_addr, req->data_len, DMA_TO_DE= VICE); + + trb =3D (struct trb *)req->skb->cb; + trb->status =3D 0; + trb->trb_complete(req->skb); + + req->data_vm_addr =3D NULL; + req->data_dma_addr =3D 0; + req->data_len =3D 0; + + txq->free_idx =3D (txq->free_idx + 1) % txq->req_pool_size; + mutex_lock(&txq->lock); + txq->req_budget++; + mutex_unlock(&txq->lock); + } + mtk_cldma_unmask_ip_busy_to_pci(mdev, txq->hw->base_addr, txq->txqno, IP_= BUSY_TXDONE); + state =3D mtk_cldma_check_intr_status(mdev, txq->hw->base_addr, + DIR_TX, txq->txqno, QUEUE_XFER_DONE); + if (state) { + if (unlikely(state =3D=3D LINK_ERROR_VAL)) + return; + + mtk_cldma_clr_intr_status(mdev, txq->hw->base_addr, DIR_TX, + txq->txqno, QUEUE_XFER_DONE); + + if (need_resched()) { + mtk_cldma_mask_ip_busy_to_pci(mdev, txq->hw->base_addr, + txq->txqno, IP_BUSY_TXDONE); + cond_resched(); + mtk_cldma_unmask_ip_busy_to_pci(mdev, txq->hw->base_addr, + txq->txqno, IP_BUSY_TXDONE); + } + + goto again; + } + + mtk_cldma_unmask_intr(mdev, txq->hw->base_addr, DIR_TX, txq->txqno, QUEUE= _XFER_DONE); + mtk_cldma_clear_ip_busy(mdev, txq->hw->base_addr); +} + +static void mtk_cldma_rx_done_work(struct work_struct *work) +{ + struct rxq *rxq =3D container_of(work, struct rxq, rx_done_work); + struct cldma_hw *hw =3D rxq->hw; + u32 curr_addr_h, curr_addr_l; + struct mtk_md_dev *mdev; + struct rx_req *req; + u64 curr_addr; + int i, err; + u32 state; + u64 addr; + + mdev =3D hw->mdev; + + do { + for (i =3D 0; i < rxq->req_pool_size; i++) { + req =3D rxq->req_pool + rxq->free_idx; + if ((req->gpd->rx_gpd.gpd_flags & CLDMA_GPD_FLAG_HWO)) { + addr =3D hw->base_addr + REG_CLDMA_SO_CUR_ADDRH_0 + + (u64)rxq->rxqno * HW_QUEUE_NUM; + curr_addr_h =3D mtk_hw_read32(mdev, addr); + addr =3D hw->base_addr + REG_CLDMA_SO_CUR_ADDRL_0 + + (u64)rxq->rxqno * HW_QUEUE_NUM; + curr_addr_l =3D mtk_hw_read32(mdev, addr); + curr_addr =3D ((u64)curr_addr_h << 32) | curr_addr_l; + + if (req->gpd_dma_addr =3D=3D curr_addr && + (req->gpd->rx_gpd.gpd_flags & CLDMA_GPD_FLAG_HWO)) + break; + } + + dma_unmap_single(mdev->dev, req->data_dma_addr, req->mtu, DMA_FROM_DEVI= CE); + + rxq->rx_done(req->skb, le16_to_cpu(req->gpd->rx_gpd.data_recv_len), + rxq->arg); + + rxq->free_idx =3D (rxq->free_idx + 1) % rxq->req_pool_size; + req->skb =3D __dev_alloc_skb(rxq->vq->rx_mtu, GFP_KERNEL); + if (!req->skb) + break; + + req->data_dma_addr =3D dma_map_single(mdev->dev, + req->skb->data, + req->mtu, + DMA_FROM_DEVICE); + err =3D dma_mapping_error(mdev->dev, req->data_dma_addr); + if (unlikely(err)) { + dev_err(mdev->dev, "Failed to map dma!\n"); + dev_kfree_skb_any(req->skb); + break; + } + + req->gpd->rx_gpd.data_recv_len =3D 0; + req->gpd->rx_gpd.data_buff_ptr_h =3D + cpu_to_le32((u64)req->data_dma_addr >> 32); + req->gpd->rx_gpd.data_buff_ptr_l =3D cpu_to_le32(req->data_dma_addr); + req->gpd->rx_gpd.gpd_flags =3D CLDMA_GPD_FLAG_IOC | CLDMA_GPD_FLAG_HWO; + } + + mtk_cldma_resume_queue(mdev, rxq->hw->base_addr, DIR_RX, rxq->rxqno); + state =3D mtk_cldma_check_intr_status(mdev, rxq->hw->base_addr, + DIR_RX, rxq->rxqno, QUEUE_XFER_DONE); + + if (!state) + break; + + mtk_cldma_clr_intr_status(mdev, rxq->hw->base_addr, DIR_RX, + rxq->rxqno, QUEUE_XFER_DONE); + + if (need_resched()) + cond_resched(); + } while (true); + + mtk_cldma_unmask_intr(mdev, rxq->hw->base_addr, DIR_RX, rxq->rxqno, QUEUE= _XFER_DONE); + mtk_cldma_mask_ip_busy_to_pci(mdev, rxq->hw->base_addr, rxq->rxqno, IP_BU= SY_RXDONE); + mtk_cldma_clear_ip_busy(mdev, rxq->hw->base_addr); +} + +static int mtk_cldma_isr(int irq_id, void *param) +{ + u32 txq_xfer_done, rxq_xfer_done; + struct cldma_hw *hw =3D param; + u32 tx_mask, rx_mask; + u32 txq_err, rxq_err; + u32 tx_sta, rx_sta; + struct txq *txq; + struct rxq *rxq; + int i; + + tx_sta =3D mtk_hw_read32(hw->mdev, hw->base_addr + REG_CLDMA_L2TISAR0); + tx_mask =3D mtk_hw_read32(hw->mdev, hw->base_addr + REG_CLDMA_L2TIMR0); + rx_sta =3D mtk_hw_read32(hw->mdev, hw->base_addr + REG_CLDMA_L2RISAR0); + rx_mask =3D mtk_hw_read32(hw->mdev, hw->base_addr + REG_CLDMA_L2RIMR0); + + tx_sta =3D tx_sta & (~tx_mask); + rx_sta =3D rx_sta & (~rx_mask); + + if (tx_sta) { + /* TX mask */ + mtk_hw_write32(hw->mdev, hw->base_addr + REG_CLDMA_L2TIMSR0, tx_sta); + + txq_err =3D (tx_sta >> QUEUE_ERROR) & 0xFF; + if (txq_err) { + mtk_cldma_clr_intr_status(hw->mdev, hw->base_addr, + DIR_TX, ALLQ, QUEUE_ERROR); + mtk_hw_write32(hw->mdev, hw->base_addr + REG_CLDMA_L2TIMCR0, + (txq_err << QUEUE_ERROR)); + } + + /* TX clear */ + mtk_hw_write32(hw->mdev, hw->base_addr + REG_CLDMA_L2TISAR0, tx_sta); + + txq_xfer_done =3D (tx_sta >> QUEUE_XFER_DONE) & 0xFF; + if (txq_xfer_done) { + for (i =3D 0; i < HW_QUEUE_NUM; i++) { + if (txq_xfer_done & (1 << i)) { + txq =3D hw->txq[i]; + queue_work(hw->wq, &txq->tx_done_work); + } + } + } + } + + if (rx_sta) { + /* RX mask */ + mtk_hw_write32(hw->mdev, hw->base_addr + REG_CLDMA_L2RIMSR0, rx_sta); + + rxq_err =3D (rx_sta >> QUEUE_ERROR) & 0xFF; + if (rxq_err) { + mtk_cldma_clr_intr_status(hw->mdev, hw->base_addr, + DIR_RX, ALLQ, QUEUE_ERROR); + mtk_hw_write32(hw->mdev, hw->base_addr + REG_CLDMA_L2RIMCR0, + (rxq_err << QUEUE_ERROR)); + } + + /* RX clear */ + mtk_hw_write32(hw->mdev, hw->base_addr + REG_CLDMA_L2RISAR0, rx_sta); + + rxq_xfer_done =3D (rx_sta >> QUEUE_XFER_DONE) & 0xFF; + if (rxq_xfer_done) { + for (i =3D 0; i < HW_QUEUE_NUM; i++) { + if (rxq_xfer_done & (1 << i)) { + rxq =3D hw->rxq[i]; + queue_work(hw->wq, &rxq->rx_done_work); + } + } + } + } + + mtk_hw_clear_irq(hw->mdev, hw->pci_ext_irq_id); + mtk_hw_unmask_irq(hw->mdev, hw->pci_ext_irq_id); + + return IRQ_HANDLED; +} + +int mtk_cldma_hw_init_t800(struct cldma_dev *cd, int hif_id) +{ + char pool_name[DMA_POOL_NAME_LEN]; + struct cldma_hw *hw; + unsigned int flag; + + if (cd->cldma_hw[hif_id]) + return 0; + + hw =3D devm_kzalloc(cd->trans->mdev->dev, sizeof(*hw), GFP_KERNEL); + if (!hw) + return -ENOMEM; + + hw->cd =3D cd; + hw->mdev =3D cd->trans->mdev; + hw->hif_id =3D ((CLDMA_CLASS_ID) << 8) + hif_id; + snprintf(pool_name, DMA_POOL_NAME_LEN, "cldma%d_pool_%s", hw->hif_id, hw-= >mdev->dev_str); + hw->dma_pool =3D dma_pool_create(pool_name, hw->mdev->dev, sizeof(union g= pd), 64, 0); + if (!hw->dma_pool) + goto free_mem; + + switch (hif_id) { + case CLDMA0: + hw->pci_ext_irq_id =3D mtk_hw_get_irq_id(hw->mdev, MTK_IRQ_SRC_CLDMA0); + hw->base_addr =3D CLDMA0_BASE_ADDR; + break; + case CLDMA1: + hw->pci_ext_irq_id =3D mtk_hw_get_irq_id(hw->mdev, MTK_IRQ_SRC_CLDMA1); + hw->base_addr =3D CLDMA1_BASE_ADDR; + break; + default: + break; + } + + flag =3D WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_HIGHPRI; + hw->wq =3D alloc_workqueue("cldma%d_workq_%s", flag, 0, hif_id, hw->mdev-= >dev_str); + + mtk_cldma_hw_init(hw->mdev, hw->base_addr); + + /* mask/clear PCI CLDMA L1 interrupt */ + mtk_hw_mask_irq(hw->mdev, hw->pci_ext_irq_id); + mtk_hw_clear_irq(hw->mdev, hw->pci_ext_irq_id); + + /* register CLDMA interrupt handler */ + mtk_hw_register_irq(hw->mdev, hw->pci_ext_irq_id, mtk_cldma_isr, hw); + + /* unmask PCI CLDMA L1 interrupt */ + mtk_hw_unmask_irq(hw->mdev, hw->pci_ext_irq_id); + + cd->cldma_hw[hif_id] =3D hw; + return 0; + +free_mem: + devm_kfree(hw->mdev->dev, hw); + + return -EIO; +} + +int mtk_cldma_hw_exit_t800(struct cldma_dev *cd, int hif_id) +{ + struct mtk_md_dev *mdev; + struct cldma_hw *hw; + int i; + + if (!cd->cldma_hw[hif_id]) + return 0; + + /* free cldma descriptor */ + hw =3D cd->cldma_hw[hif_id]; + mdev =3D cd->trans->mdev; + mtk_hw_mask_irq(mdev, hw->pci_ext_irq_id); + for (i =3D 0; i < HW_QUEUE_NUM; i++) { + if (hw->txq[i]) + cd->hw_ops.txq_free(hw, hw->txq[i]->vqno); + if (hw->rxq[i]) + cd->hw_ops.rxq_free(hw, hw->rxq[i]->vqno); + } + + flush_workqueue(hw->wq); + destroy_workqueue(hw->wq); + dma_pool_destroy(hw->dma_pool); + mtk_hw_unregister_irq(mdev, hw->pci_ext_irq_id); + + devm_kfree(mdev->dev, hw); + cd->cldma_hw[hif_id] =3D NULL; + + return 0; +} + +struct txq *mtk_cldma_txq_alloc_t800(struct cldma_hw *hw, struct sk_buff *= skb) +{ + struct trb *trb =3D (struct trb *)skb->cb; + struct tx_req *next; + struct tx_req *req; + struct txq *txq; + int i; + + txq =3D devm_kzalloc(hw->mdev->dev, sizeof(*txq), GFP_KERNEL); + if (!txq) + return NULL; + + txq->hw =3D hw; + txq->vqno =3D trb->vqno; + txq->vq =3D hw->cd->trans->vq_tbl + trb->vqno; + txq->txqno =3D txq->vq->txqno; + txq->req_pool_size =3D txq->vq->tx_req_num; + txq->req_budget =3D txq->vq->tx_req_num; + txq->is_stopping =3D false; + mutex_init(&txq->lock); + if (unlikely(txq->txqno < 0 || txq->txqno >=3D HW_QUEUE_NUM)) + goto free_txq; + + txq->req_pool =3D devm_kcalloc(hw->mdev->dev, txq->req_pool_size, sizeof(= *req), GFP_KERNEL); + if (!txq->req_pool) + goto free_txq; + + for (i =3D 0; i < txq->req_pool_size; i++) { + req =3D txq->req_pool + i; + req->mtu =3D txq->vq->tx_mtu; + req->gpd =3D dma_pool_zalloc(hw->dma_pool, GFP_KERNEL, &req->gpd_dma_add= r); + if (!req->gpd) + goto free_req; + } + + for (i =3D 0; i < txq->req_pool_size; i++) { + req =3D txq->req_pool + i; + next =3D txq->req_pool + ((i + 1) % txq->req_pool_size); + req->gpd->tx_gpd.next_gpd_ptr_h =3D cpu_to_le32((u64)(next->gpd_dma_addr= ) >> 32); + req->gpd->tx_gpd.next_gpd_ptr_l =3D cpu_to_le32(next->gpd_dma_addr); + } + + INIT_WORK(&txq->tx_done_work, mtk_cldma_tx_done_work); + + mtk_cldma_stop_queue(hw->mdev, hw->base_addr, DIR_TX, txq->txqno); + txq->tx_started =3D false; + mtk_cldma_setup_start_addr(hw->mdev, hw->base_addr, DIR_TX, txq->txqno, + txq->req_pool[0].gpd_dma_addr); + mtk_cldma_unmask_intr(hw->mdev, hw->base_addr, DIR_TX, txq->txqno, QUEUE_= ERROR); + mtk_cldma_unmask_intr(hw->mdev, hw->base_addr, DIR_TX, txq->txqno, QUEUE_= XFER_DONE); + + hw->txq[txq->txqno] =3D txq; + return txq; + +free_req: + for (i--; i >=3D 0; i--) { + req =3D txq->req_pool + i; + dma_pool_free(hw->dma_pool, req->gpd, req->gpd_dma_addr); + } + + devm_kfree(hw->mdev->dev, txq->req_pool); +free_txq: + devm_kfree(hw->mdev->dev, txq); + return NULL; +} + +int mtk_cldma_txq_free_t800(struct cldma_hw *hw, int vqno) +{ + struct virtq *vq =3D hw->cd->trans->vq_tbl + vqno; + unsigned int active; + struct tx_req *req; + struct txq *txq; + struct trb *trb; + int cnt =3D 0; + int irq_id; + int txqno; + int i; + + txqno =3D vq->txqno; + if (unlikely(txqno < 0 || txqno >=3D HW_QUEUE_NUM)) + return -EINVAL; + txq =3D hw->txq[txqno]; + if (!txq) + return -EINVAL; + + /* stop HW tx transaction */ + mtk_cldma_stop_queue(hw->mdev, hw->base_addr, DIR_TX, txqno); + txq->tx_started =3D false; + do { + active =3D mtk_cldma_queue_status(hw->mdev, hw->base_addr, DIR_TX, txqno= ); + if (active =3D=3D LINK_ERROR_VAL) + break; + msleep(CLDMA_STOP_HW_WAIT_TIME_MS); /* ensure HW tx transaction done */ + cnt++; + } while (active && cnt < CLDMA_STOP_HW_POLLING_MAX_CNT); + + irq_id =3D mtk_hw_get_virq_id(hw->mdev, hw->pci_ext_irq_id); + synchronize_irq(irq_id); + + flush_work(&txq->tx_done_work); + mtk_cldma_mask_intr(hw->mdev, hw->base_addr, DIR_TX, txqno, QUEUE_XFER_DO= NE); + mtk_cldma_mask_intr(hw->mdev, hw->base_addr, DIR_TX, txqno, QUEUE_ERROR); + + /* free tx req resource */ + for (i =3D 0; i < txq->req_pool_size; i++) { + req =3D txq->req_pool + i; + if (req->data_dma_addr && req->data_len) { + dma_unmap_single(hw->mdev->dev, + req->data_dma_addr, + req->data_len, + DMA_TO_DEVICE); + trb =3D (struct trb *)req->skb->cb; + trb->status =3D -EPIPE; + trb->trb_complete(req->skb); + } + dma_pool_free(hw->dma_pool, req->gpd, req->gpd_dma_addr); + } + + devm_kfree(hw->mdev->dev, txq->req_pool); + devm_kfree(hw->mdev->dev, txq); + hw->txq[txqno] =3D NULL; + + return 0; +} + +struct rxq *mtk_cldma_rxq_alloc_t800(struct cldma_hw *hw, struct sk_buff *= skb) +{ + struct trb_open_priv *trb_open_priv =3D (struct trb_open_priv *)skb->data; + struct trb *trb =3D (struct trb *)skb->cb; + struct rx_req *next; + struct rx_req *req; + struct rxq *rxq; + int err; + int i; + + rxq =3D devm_kzalloc(hw->mdev->dev, sizeof(*rxq), GFP_KERNEL); + if (!rxq) + return NULL; + + rxq->hw =3D hw; + rxq->vqno =3D trb->vqno; + rxq->vq =3D hw->cd->trans->vq_tbl + trb->vqno; + rxq->rxqno =3D rxq->vq->rxqno; + rxq->req_pool_size =3D rxq->vq->rx_req_num; + rxq->arg =3D trb->priv; + rxq->rx_done =3D trb_open_priv->rx_done; + if (unlikely(rxq->rxqno < 0 || rxq->rxqno >=3D HW_QUEUE_NUM)) + goto free_rxq; + + rxq->req_pool =3D devm_kcalloc(hw->mdev->dev, rxq->req_pool_size, sizeof(= *req), GFP_KERNEL); + if (!rxq->req_pool) + goto free_rxq; + + /* setup rx request */ + for (i =3D 0; i < rxq->req_pool_size; i++) { + req =3D rxq->req_pool + i; + req->mtu =3D rxq->vq->rx_mtu; + req->gpd =3D dma_pool_zalloc(hw->dma_pool, GFP_KERNEL, &req->gpd_dma_add= r); + if (!req->gpd) + goto free_req; + + req->skb =3D __dev_alloc_skb(rxq->vq->rx_mtu, GFP_KERNEL); + if (!req->skb) { + dma_pool_free(hw->dma_pool, req->gpd, req->gpd_dma_addr); + goto free_req; + } + + req->data_dma_addr =3D dma_map_single(hw->mdev->dev, + req->skb->data, + req->mtu, + DMA_FROM_DEVICE); + err =3D dma_mapping_error(hw->mdev->dev, req->data_dma_addr); + if (unlikely(err)) { + dev_err(hw->mdev->dev, "Failed to map dma!\n"); + i++; + goto free_req; + } + } + + for (i =3D 0; i < rxq->req_pool_size; i++) { + req =3D rxq->req_pool + i; + next =3D rxq->req_pool + ((i + 1) % rxq->req_pool_size); + req->gpd->rx_gpd.gpd_flags =3D CLDMA_GPD_FLAG_IOC | CLDMA_GPD_FLAG_HWO; + req->gpd->rx_gpd.data_allow_len =3D cpu_to_le16(req->mtu); + req->gpd->rx_gpd.next_gpd_ptr_h =3D cpu_to_le32((u64)(next->gpd_dma_addr= ) >> 32); + req->gpd->rx_gpd.next_gpd_ptr_l =3D cpu_to_le32(next->gpd_dma_addr); + req->gpd->rx_gpd.data_buff_ptr_h =3D cpu_to_le32((u64)(req->data_dma_add= r) >> 32); + req->gpd->rx_gpd.data_buff_ptr_l =3D cpu_to_le32(req->data_dma_addr); + } + + INIT_WORK(&rxq->rx_done_work, mtk_cldma_rx_done_work); + + hw->rxq[rxq->rxqno] =3D rxq; + mtk_cldma_stop_queue(hw->mdev, hw->base_addr, DIR_RX, rxq->rxqno); + mtk_cldma_setup_start_addr(hw->mdev, hw->base_addr, DIR_RX, + rxq->rxqno, rxq->req_pool[0].gpd_dma_addr); + mtk_cldma_start_queue(hw->mdev, hw->base_addr, DIR_RX, rxq->rxqno); + mtk_cldma_unmask_intr(hw->mdev, hw->base_addr, DIR_RX, rxq->rxqno, QUEUE_= ERROR); + mtk_cldma_unmask_intr(hw->mdev, hw->base_addr, DIR_RX, rxq->rxqno, QUEUE_= XFER_DONE); + + return rxq; + +free_req: + for (i--; i >=3D 0; i--) { + req =3D rxq->req_pool + i; + dma_unmap_single(hw->mdev->dev, req->data_dma_addr, req->mtu, DMA_FROM_D= EVICE); + dma_pool_free(hw->dma_pool, req->gpd, req->gpd_dma_addr); + if (req->skb) + dev_kfree_skb_any(req->skb); + } + + devm_kfree(hw->mdev->dev, rxq->req_pool); +free_rxq: + devm_kfree(hw->mdev->dev, rxq); + return NULL; +} + +int mtk_cldma_rxq_free_t800(struct cldma_hw *hw, int vqno) +{ + struct mtk_md_dev *mdev; + unsigned int active; + struct rx_req *req; + struct virtq *vq; + struct rxq *rxq; + int cnt =3D 0; + int irq_id; + int rxqno; + int i; + + mdev =3D hw->mdev; + vq =3D hw->cd->trans->vq_tbl + vqno; + rxqno =3D vq->rxqno; + if (unlikely(rxqno < 0 || rxqno >=3D HW_QUEUE_NUM)) + return -EINVAL; + rxq =3D hw->rxq[rxqno]; + if (!rxq) + return -EINVAL; + + mtk_cldma_stop_queue(mdev, hw->base_addr, DIR_RX, rxqno); + do { + /* check CLDMA HW state register */ + active =3D mtk_cldma_queue_status(mdev, hw->base_addr, DIR_RX, rxqno); + if (active =3D=3D LINK_ERROR_VAL) + break; + msleep(CLDMA_STOP_HW_WAIT_TIME_MS); /* ensure HW rx transaction done */ + cnt++; + } while (active && cnt < CLDMA_STOP_HW_POLLING_MAX_CNT); + + irq_id =3D mtk_hw_get_virq_id(hw->mdev, hw->pci_ext_irq_id); + synchronize_irq(irq_id); + + flush_work(&rxq->rx_done_work); + mtk_cldma_mask_intr(mdev, hw->base_addr, DIR_RX, rxqno, QUEUE_XFER_DONE); + mtk_cldma_mask_intr(mdev, hw->base_addr, DIR_RX, rxqno, QUEUE_ERROR); + + /* free rx req resource */ + for (i =3D 0; i < rxq->req_pool_size; i++) { + req =3D rxq->req_pool + i; + if (!(req->gpd->rx_gpd.gpd_flags & CLDMA_GPD_FLAG_HWO) && + le16_to_cpu(req->gpd->rx_gpd.data_recv_len)) { + dma_unmap_single(mdev->dev, req->data_dma_addr, req->mtu, DMA_FROM_DEVI= CE); + rxq->rx_done(req->skb, le16_to_cpu(req->gpd->rx_gpd.data_recv_len), + rxq->arg); + req->skb =3D NULL; + } + + dma_pool_free(hw->dma_pool, req->gpd, req->gpd_dma_addr); + if (req->skb) { + dev_kfree_skb_any(req->skb); + dma_unmap_single(mdev->dev, req->data_dma_addr, req->mtu, DMA_FROM_DEVI= CE); + } + } + + devm_kfree(mdev->dev, rxq->req_pool); + devm_kfree(mdev->dev, rxq); + hw->rxq[rxqno] =3D NULL; + + return 0; +} + +int mtk_cldma_start_xfer_t800(struct cldma_hw *hw, int qno) +{ + struct txq *txq; + u32 addr, val; + int idx; + + txq =3D hw->txq[qno]; + addr =3D hw->base_addr + REG_CLDMA_UL_START_ADDRL_0 + qno * HW_QUEUE_NUM; + val =3D mtk_hw_read32(hw->mdev, addr); + if (unlikely(!val)) { + mtk_cldma_hw_init(hw->mdev, hw->base_addr); + txq =3D hw->txq[qno]; + idx =3D (txq->wr_idx + txq->req_pool_size - 1) % txq->req_pool_size; + mtk_cldma_setup_start_addr(hw->mdev, hw->base_addr, DIR_TX, qno, + txq->req_pool[idx].gpd_dma_addr); + mtk_cldma_start_queue(hw->mdev, hw->base_addr, DIR_TX, qno); + txq->tx_started =3D true; + } else { + if (unlikely(!txq->tx_started)) { + mtk_cldma_start_queue(hw->mdev, hw->base_addr, DIR_TX, qno); + txq->tx_started =3D true; + } else { + mtk_cldma_resume_queue(hw->mdev, hw->base_addr, DIR_TX, qno); + } + } + + return 0; +} diff --git a/drivers/net/wwan/mediatek/pcie/mtk_cldma_drv_t800.h b/drivers/= net/wwan/mediatek/pcie/mtk_cldma_drv_t800.h new file mode 100644 index 000000000000..21e2f62acce2 --- /dev/null +++ b/drivers/net/wwan/mediatek/pcie/mtk_cldma_drv_t800.h @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: BSD-3-Clause-Clear + * + * Copyright (c) 2022, MediaTek Inc. + */ + +#ifndef __MTK_CLDMA_DRV_T800_H__ +#define __MTK_CLDMA_DRV_T800_H__ + +#include + +#include "../mtk_cldma.h" + +int mtk_cldma_hw_init_t800(struct cldma_dev *cd, int hif_id); +int mtk_cldma_hw_exit_t800(struct cldma_dev *cd, int hif_id); +struct txq *mtk_cldma_txq_alloc_t800(struct cldma_hw *hw, struct sk_buff *= skb); +int mtk_cldma_txq_free_t800(struct cldma_hw *hw, int vqno); +struct rxq *mtk_cldma_rxq_alloc_t800(struct cldma_hw *hw, struct sk_buff *= skb); +int mtk_cldma_rxq_free_t800(struct cldma_hw *hw, int vqno); +int mtk_cldma_start_xfer_t800(struct cldma_hw *hw, int qno); +#endif --=20 2.32.0 From nobody Thu Nov 14 06:37:28 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D3478C74A5B for ; Fri, 17 Mar 2023 08:44:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231683AbjCQIoX (ORCPT ); Fri, 17 Mar 2023 04:44:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231678AbjCQIoA (ORCPT ); Fri, 17 Mar 2023 04:44:00 -0400 Received: from mailgw02.mediatek.com (unknown [210.61.82.184]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 507CB76163; Fri, 17 Mar 2023 01:42:51 -0700 (PDT) X-UUID: 6cc269e0c49b11edbd2e61cc88cc8f98-20230317 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=SeTkJe4RimMKIh4IP5gC25RuWqFcccQZT3rHpT2NLww=; b=XtaQHGa6PAG/c8zigtl0IoPnEfQyx6hbclbPiWG8Wdpq+/rQPRXMd12N55c4lmP7t/TehvGTc+kaqPrBdWew4MfYimZIgD/QXgOFFtRwpQxZKH0MR7b1BB6Jf4QGO72YcravTQlKhOPR5OVIAqoNKa2D0RIL9v/KtyWAIDSBx9E=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.21,REQID:26cba884-4ffa-4769-9ddb-c9744e258692,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:95,FILE:0,BULK:0,RULE:Release_Ham,ACTI ON:release,TS:70 X-CID-INFO: VERSION:1.1.21,REQID:26cba884-4ffa-4769-9ddb-c9744e258692,IP:0,URL :0,TC:0,Content:-25,EDM:0,RT:0,SF:95,FILE:0,BULK:0,RULE:Spam_GS981B3D,ACTI ON:quarantine,TS:70 X-CID-META: VersionHash:83295aa,CLOUDID:b1be1af6-ddba-41c3-91d9-10eeade8eac7,B ulkID:230317161216ULHBSGAC,BulkQuantity:0,Recheck:0,SF:38|29|28|17|19|48,T C:nil,Content:0,EDM:-3,IP:nil,URL:11|1,File:nil,Bulk:nil,QS:nil,BEC:nil,CO L:0,OSI:0,OSA:0,AV:0 X-CID-BVR: 0,NGT X-CID-BAS: 0,NGT,0,_ X-UUID: 6cc269e0c49b11edbd2e61cc88cc8f98-20230317 Received: from mtkmbs10n1.mediatek.inc [(172.21.101.34)] by mailgw02.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1808890449; Fri, 17 Mar 2023 16:12:14 +0800 Received: from mtkmbs11n2.mediatek.inc (172.21.101.187) by mtkmbs13n2.mediatek.inc (172.21.101.108) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Fri, 17 Mar 2023 16:12:13 +0800 Received: from mcddlt001.gcn.mediatek.inc (10.19.240.15) by mtkmbs11n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.25 via Frontend Transport; Fri, 17 Mar 2023 16:12:11 +0800 From: Yanchao Yang To: Loic Poulain , Sergey Ryazanov , Johannes Berg , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , netdev ML , kernel ML CC: Intel experts , Chetan , MTK ML , Liang Lu , Haijun Liu , Hua Yang , Ting Wang , Felix Chen , Mingliang Xu , Min Dong , Aiden Wang , Guohao Zhang , Chris Feng , Yanchao Yang , Lambert Wang , Mingchuang Qiao , Xiayu Zhang , Haozhe Chang Subject: [PATCH net-next v4 04/10] net: wwan: tmi: Add control port Date: Fri, 17 Mar 2023 16:09:36 +0800 Message-ID: <20230317080942.183514-5-yanchao.yang@mediatek.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20230317080942.183514-1-yanchao.yang@mediatek.com> References: <20230317080942.183514-1-yanchao.yang@mediatek.com> MIME-Version: 1.0 X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The 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. Signed-off-by: Yanchao Yang Signed-off-by: Felix Chen --- drivers/net/wwan/mediatek/Makefile | 4 +- drivers/net/wwan/mediatek/mtk_ctrl_plane.c | 117 +++ drivers/net/wwan/mediatek/mtk_ctrl_plane.h | 28 +- drivers/net/wwan/mediatek/mtk_dev.c | 4 +- drivers/net/wwan/mediatek/mtk_port.c | 844 +++++++++++++++++++++ drivers/net/wwan/mediatek/mtk_port.h | 171 +++++ drivers/net/wwan/mediatek/mtk_port_io.c | 253 ++++++ drivers/net/wwan/mediatek/mtk_port_io.h | 34 + drivers/net/wwan/mediatek/pcie/mtk_pci.c | 2 + 9 files changed, 1452 insertions(+), 5 deletions(-) create mode 100644 drivers/net/wwan/mediatek/mtk_port.c create mode 100644 drivers/net/wwan/mediatek/mtk_port.h create mode 100644 drivers/net/wwan/mediatek/mtk_port_io.c create mode 100644 drivers/net/wwan/mediatek/mtk_port_io.h diff --git a/drivers/net/wwan/mediatek/Makefile b/drivers/net/wwan/mediatek= /Makefile index 23a1cbc06ef6..e3afd8ecb494 100644 --- a/drivers/net/wwan/mediatek/Makefile +++ b/drivers/net/wwan/mediatek/Makefile @@ -7,6 +7,8 @@ mtk_tmi-y =3D \ mtk_dev.o \ mtk_ctrl_plane.o \ mtk_cldma.o \ - pcie/mtk_cldma_drv_t800.o + pcie/mtk_cldma_drv_t800.o \ + mtk_port.o \ + mtk_port_io.o =20 obj-$(CONFIG_MTK_TMI) +=3D mtk_tmi.o diff --git a/drivers/net/wwan/mediatek/mtk_ctrl_plane.c b/drivers/net/wwan/= mediatek/mtk_ctrl_plane.c index 1dbfcf8587a1..8adb1f53ec64 100644 --- a/drivers/net/wwan/mediatek/mtk_ctrl_plane.c +++ b/drivers/net/wwan/mediatek/mtk_ctrl_plane.c @@ -10,11 +10,118 @@ #include #include =20 +#include "mtk_cldma.h" #include "mtk_ctrl_plane.h" +#include "mtk_port.h" + +static int mtk_ctrl_get_hif_id(unsigned char peer_id) +{ + if (peer_id =3D=3D MTK_PEER_ID_SAP) + return CLDMA0; + else if (peer_id =3D=3D MTK_PEER_ID_MD) + return CLDMA1; + else + return -EINVAL; +} + +int mtk_ctrl_vq_search(struct mtk_ctrl_blk *ctrl_blk, unsigned char peer_i= d, + unsigned char tx_hwq, unsigned char rx_hwq) +{ + struct mtk_port_mngr *port_mngr =3D ctrl_blk->port_mngr; + struct mtk_ctrl_trans *trans =3D ctrl_blk->trans; + int hif_id =3D mtk_ctrl_get_hif_id(peer_id); + struct virtq *vq; + int vq_num =3D 0; + + if (hif_id < 0) + return -EINVAL; + + do { + vq =3D trans->vq_tbl + vq_num; + if (port_mngr->vq_info[vq_num].color && vq->txqno =3D=3D tx_hwq && + vq->rxqno =3D=3D rx_hwq && vq->hif_id =3D=3D hif_id) + return vq_num; + + vq_num++; + } while (vq_num < VQ_NUM); + + return -ENOENT; +} + +int mtk_ctrl_vq_color_paint(struct mtk_ctrl_blk *ctrl_blk, unsigned char p= eer_id, + unsigned char tx_hwq, unsigned char rx_hwq, + unsigned int tx_mtu, unsigned int rx_mtu) +{ + struct mtk_port_mngr *port_mngr =3D ctrl_blk->port_mngr; + struct mtk_ctrl_trans *trans =3D ctrl_blk->trans; + int hif_id =3D mtk_ctrl_get_hif_id(peer_id); + struct virtq *vq; + int vq_num =3D 0; + + if (hif_id < 0) + return -EINVAL; + + do { + vq =3D trans->vq_tbl + vq_num; + if (vq->hif_id =3D=3D hif_id && vq->txqno =3D=3D tx_hwq && vq->rxqno =3D= =3D rx_hwq && + vq->tx_mtu <=3D tx_mtu && vq->rx_mtu >=3D rx_mtu) + port_mngr->vq_info[vq_num].color =3D true; + + vq_num++; + } while (vq_num < VQ_NUM); + + return 0; +} + +int mtk_ctrl_vq_color_cleanup(struct mtk_ctrl_blk *ctrl_blk, unsigned char= peer_id) +{ + struct mtk_port_mngr *port_mngr =3D ctrl_blk->port_mngr; + struct mtk_ctrl_trans *trans =3D ctrl_blk->trans; + int hif_id =3D mtk_ctrl_get_hif_id(peer_id); + struct virtq *vq; + int vq_num =3D 0; + + if (hif_id < 0) + return -EINVAL; + + do { + vq =3D trans->vq_tbl + vq_num; + if (vq->hif_id =3D=3D hif_id) + port_mngr->vq_info[vq_num].color =3D false; + + vq_num++; + } while (vq_num < VQ_NUM); + + return 0; +} + +int mtk_ctrl_trb_submit(struct mtk_ctrl_blk *blk, struct sk_buff *skb) +{ + struct mtk_ctrl_trans *trans =3D blk->trans; + struct trb *trb; + int vqno; + + trb =3D (struct trb *)skb->cb; + if (trb->vqno >=3D VQ_NUM) + return -EINVAL; + + if (!atomic_read(&trans->available)) + return -EIO; + + vqno =3D trb->vqno; + if (VQ_LIST_FULL(trans, vqno) && trb->cmd !=3D TRB_CMD_DISABLE) + return -EAGAIN; + + /* This function will implement in next patch */ + wake_up(&trans->trb_srv->trb_waitq); + + return 0; +} =20 int mtk_ctrl_init(struct mtk_md_dev *mdev) { struct mtk_ctrl_blk *ctrl_blk; + int err; =20 ctrl_blk =3D devm_kzalloc(mdev->dev, sizeof(*ctrl_blk), GFP_KERNEL); if (!ctrl_blk) @@ -23,13 +130,23 @@ int mtk_ctrl_init(struct mtk_md_dev *mdev) ctrl_blk->mdev =3D mdev; mdev->ctrl_blk =3D ctrl_blk; =20 + err =3D mtk_port_mngr_init(ctrl_blk); + if (err) + goto err_free_mem; + return 0; + +err_free_mem: + devm_kfree(mdev->dev, ctrl_blk); + + return err; } =20 int mtk_ctrl_exit(struct mtk_md_dev *mdev) { struct mtk_ctrl_blk *ctrl_blk =3D mdev->ctrl_blk; =20 + mtk_port_mngr_exit(ctrl_blk); devm_kfree(mdev->dev, ctrl_blk); =20 return 0; diff --git a/drivers/net/wwan/mediatek/mtk_ctrl_plane.h b/drivers/net/wwan/= mediatek/mtk_ctrl_plane.h index 32cd8dc7bdb7..2e1f21d43644 100644 --- a/drivers/net/wwan/mediatek/mtk_ctrl_plane.h +++ b/drivers/net/wwan/mediatek/mtk_ctrl_plane.h @@ -11,13 +11,20 @@ =20 #include "mtk_dev.h" =20 +#define VQ(N) (N) +#define VQ_NUM (2) + #define VQ_MTU_3_5K (0xE00) #define VQ_MTU_63K (0xFC00) =20 +#define SKB_LIST_MAX_LEN (16) + #define HIF_CLASS_NUM (1) #define HIF_CLASS_SHIFT (8) #define HIF_ID_BITMASK (0x01) =20 +#define VQ_LIST_FULL(trans, vqno) ((trans)->skb_list[vqno].qlen >=3D SKB_L= IST_MAX_LEN) + enum mtk_trb_cmd_type { TRB_CMD_ENABLE =3D 1, TRB_CMD_TX, @@ -39,6 +46,14 @@ struct trb { int (*trb_complete)(struct sk_buff *skb); }; =20 +struct trb_srv { + int vq_cnt; + int vq_start; + struct mtk_ctrl_trans *trans; + wait_queue_head_t trb_waitq; + struct task_struct *trb_thread; +}; + struct virtq { int vqno; int hif_id; @@ -50,8 +65,6 @@ struct virtq { int rx_req_num; }; =20 -struct mtk_ctrl_trans; - struct hif_ops { int (*init)(struct mtk_ctrl_trans *trans); int (*exit)(struct mtk_ctrl_trans *trans); @@ -60,18 +73,29 @@ struct hif_ops { }; =20 struct mtk_ctrl_trans { + struct sk_buff_head skb_list[VQ_NUM]; + struct trb_srv *trb_srv; struct virtq *vq_tbl; void *dev[HIF_CLASS_NUM]; struct hif_ops *ops[HIF_CLASS_NUM]; struct mtk_ctrl_blk *ctrl_blk; struct mtk_md_dev *mdev; + atomic_t available; }; =20 struct mtk_ctrl_blk { struct mtk_md_dev *mdev; + struct mtk_port_mngr *port_mngr; struct mtk_ctrl_trans *trans; }; =20 +int mtk_ctrl_vq_search(struct mtk_ctrl_blk *ctrl_blk, unsigned char peer_i= d, + unsigned char tx_hwq, unsigned char rx_hwq); +int mtk_ctrl_vq_color_paint(struct mtk_ctrl_blk *ctrl_blk, unsigned char p= eer_id, + unsigned char tx_hwq, unsigned char rx_hwq, + unsigned int tx_mtu, unsigned int rx_mtu); +int mtk_ctrl_vq_color_cleanup(struct mtk_ctrl_blk *ctrl_blk, unsigned char= peer_id); +int mtk_ctrl_trb_submit(struct mtk_ctrl_blk *blk, struct sk_buff *skb); int mtk_ctrl_init(struct mtk_md_dev *mdev); int mtk_ctrl_exit(struct mtk_md_dev *mdev); =20 diff --git a/drivers/net/wwan/mediatek/mtk_dev.c b/drivers/net/wwan/mediate= k/mtk_dev.c index f63c7e04df6a..d34c3933e84d 100644 --- a/drivers/net/wwan/mediatek/mtk_dev.c +++ b/drivers/net/wwan/mediatek/mtk_dev.c @@ -12,10 +12,10 @@ int mtk_dev_init(struct mtk_md_dev *mdev) =20 ret =3D mtk_ctrl_init(mdev); if (ret) - goto err_ctrl_init; + goto exit; =20 return 0; -err_ctrl_init: +exit: return ret; } =20 diff --git a/drivers/net/wwan/mediatek/mtk_port.c b/drivers/net/wwan/mediat= ek/mtk_port.c new file mode 100644 index 000000000000..12097a279aa0 --- /dev/null +++ b/drivers/net/wwan/mediatek/mtk_port.c @@ -0,0 +1,844 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +/* + * Copyright (c) 2022, MediaTek Inc. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "mtk_port.h" +#include "mtk_port_io.h" + +#define MTK_DFLT_TRB_TIMEOUT (5 * HZ) +#define MTK_DFLT_TRB_STATUS (0x1) +#define MTK_CHECK_RX_SEQ_MASK (0x7fff) + +#define MTK_PORT_SEARCH_FROM_RADIX_TREE(p, s) ({\ + struct mtk_port *_p; \ + _p =3D radix_tree_deref_slot(s); \ + if (!_p) \ + continue; \ + p =3D _p; \ +}) + +#define MTK_PORT_INTERNAL_NODE_CHECK(p, s, i) ({\ + if (radix_tree_is_internal_node(p)) { \ + s =3D radix_tree_iter_retry(&(i));\ + continue; \ + } \ +}) + +static LIST_HEAD(stale_list_grp); +/* mutex lock for stale_list_group */ +DEFINE_MUTEX(port_mngr_grp_mtx); + +static DEFINE_IDA(ccci_dev_ids); + +static const struct mtk_port_cfg port_cfg[] =3D { + {CCCI_CONTROL_TX, CCCI_CONTROL_RX, VQ(1), PORT_TYPE_INTERNAL, "MDCTRL", P= ORT_F_ALLOW_DROP}, + {CCCI_SAP_CONTROL_TX, CCCI_SAP_CONTROL_RX, VQ(0), PORT_TYPE_INTERNAL, "SA= PCTRL", + PORT_F_ALLOW_DROP}, +}; + +void mtk_port_release(struct kref *port_kref) +{ + struct mtk_stale_list *s_list; + struct mtk_port *port; + + port =3D container_of(port_kref, struct mtk_port, kref); + if (!test_bit(PORT_S_ON_STALE_LIST, &port->status)) + goto port_exit; + + list_del(&port->stale_entry); + list_for_each_entry(s_list, &stale_list_grp, entry) { + if (!strncmp(s_list->dev_str, port->dev_str, MTK_DEV_STR_LEN) && + list_empty(&s_list->ports) && s_list->dev_id >=3D 0) { + pr_info("Free dev id of stale list(%s)\n", s_list->dev_str); + ida_free(&ccci_dev_ids, s_list->dev_id); + s_list->dev_id =3D -1; + break; + } + } + +port_exit: + ports_ops[port->info.type]->exit(port); + kfree(port); +} + +static int mtk_port_tbl_add(struct mtk_port_mngr *port_mngr, struct mtk_po= rt *port) +{ + int ret; + + ret =3D radix_tree_insert(&port_mngr->port_tbl[MTK_PORT_TBL_TYPE(port->in= fo.rx_ch)], + port->info.rx_ch & 0xFFF, port); + if (ret) + dev_err(port_mngr->ctrl_blk->mdev->dev, + "port(%s) add to port_tbl failed, return %d\n", + port->info.name, ret); + + return ret; +} + +static void mtk_port_tbl_del(struct mtk_port_mngr *port_mngr, struct mtk_p= ort *port) +{ + radix_tree_delete(&port_mngr->port_tbl[MTK_PORT_TBL_TYPE(port->info.rx_ch= )], + port->info.rx_ch & 0xFFF); +} + +static struct mtk_port *mtk_port_get_from_stale_list(struct mtk_port_mngr = *port_mngr, + struct mtk_stale_list *s_list, + int rx_ch) +{ + struct mtk_port *port, *next_port; + int ret; + + mutex_lock(&port_mngr_grp_mtx); + list_for_each_entry_safe(port, next_port, &s_list->ports, stale_entry) { + if (port->info.rx_ch =3D=3D rx_ch) { + kref_get(&port->kref); + list_del(&port->stale_entry); + ret =3D mtk_port_tbl_add(port_mngr, port); + if (ret) { + list_add_tail(&port->stale_entry, &s_list->ports); + kref_put(&port->kref, mtk_port_release); + mutex_unlock(&port_mngr_grp_mtx); + dev_err(port_mngr->ctrl_blk->mdev->dev, + "Failed when adding (%s) to port mngr\n", + port->info.name); + return ERR_PTR(ret); + } + + port->port_mngr =3D port_mngr; + clear_bit(PORT_S_ON_STALE_LIST, &port->status); + mutex_unlock(&port_mngr_grp_mtx); + return port; + } + } + mutex_unlock(&port_mngr_grp_mtx); + + return NULL; +} + +static struct mtk_port *mtk_port_alloc_or_restore(struct mtk_port_mngr *po= rt_mngr, + struct mtk_port_cfg *dflt_info, + struct mtk_stale_list *s_list) +{ + struct mtk_port *port; + int ret; + + port =3D mtk_port_get_from_stale_list(port_mngr, s_list, dflt_info->rx_ch= ); + if (IS_ERR(port)) + return port; + + if (port) { + ports_ops[port->info.type]->reset(port); + dev_info(port_mngr->ctrl_blk->mdev->dev, + "Port(%s) move from stale list\n", port->info.name); + goto return_port; + } + + port =3D kzalloc(sizeof(*port), GFP_KERNEL); + if (!port) { + ret =3D -ENOMEM; + goto exit_err; + } + + memcpy(port, dflt_info, sizeof(*dflt_info)); + ret =3D mtk_port_tbl_add(port_mngr, port); + if (ret < 0) { + dev_err(port_mngr->ctrl_blk->mdev->dev, + "Failed to add port(%s) to port tbl\n", dflt_info->name); + goto free_port; + } + + port->port_mngr =3D port_mngr; + ports_ops[port->info.type]->init(port); + dev_info(port_mngr->ctrl_blk->mdev->dev, + "Port(%s) alloc and init\n", port->info.name); + +return_port: + return port; +free_port: + kfree(port); +exit_err: + return ERR_PTR(ret); +} + +static void mtk_port_free_or_backup(struct mtk_port_mngr *port_mngr, + struct mtk_port *port, struct mtk_stale_list *s_list) +{ + mutex_lock(&port_mngr_grp_mtx); + mtk_port_tbl_del(port_mngr, port); + if (port->info.type !=3D PORT_TYPE_INTERNAL) { + if (test_bit(PORT_S_OPEN, &port->status)) { + list_add_tail(&port->stale_entry, &s_list->ports); + set_bit(PORT_S_ON_STALE_LIST, &port->status); + dev_info(port_mngr->ctrl_blk->mdev->dev, + "Port(%s) move to stale list\n", port->info.name); + memcpy(port->dev_str, port_mngr->ctrl_blk->mdev->dev_str, MTK_DEV_STR_L= EN); + port->port_mngr =3D NULL; + } + kref_put(&port->kref, mtk_port_release); + } else { + mtk_port_release(&port->kref); + } + mutex_unlock(&port_mngr_grp_mtx); +} + +static struct mtk_port *mtk_port_search_by_id(struct mtk_port_mngr *port_m= ngr, int rx_ch) +{ + int tbl_type =3D MTK_PORT_TBL_TYPE(rx_ch); + + if (tbl_type < PORT_TBL_SAP || tbl_type >=3D PORT_TBL_MAX) + return NULL; + + return radix_tree_lookup(&port_mngr->port_tbl[tbl_type], MTK_CH_ID(rx_ch)= ); +} + +struct mtk_port *mtk_port_search_by_name(struct mtk_port_mngr *port_mngr, = char *name) +{ + int tbl_type =3D PORT_TBL_SAP; + struct radix_tree_iter iter; + struct mtk_port *port; + void __rcu **slot; + + do { + radix_tree_for_each_slot(slot, &port_mngr->port_tbl[tbl_type], &iter, 0)= { + MTK_PORT_SEARCH_FROM_RADIX_TREE(port, slot); + MTK_PORT_INTERNAL_NODE_CHECK(port, slot, iter); + if (!strncmp(port->info.name, name, strlen(port->info.name))) + return port; + } + tbl_type++; + } while (tbl_type < PORT_TBL_MAX); + return NULL; +} + +static int mtk_port_tbl_create(struct mtk_port_mngr *port_mngr, struct mtk= _port_cfg *cfg, + const int port_cnt, struct mtk_stale_list *s_list) +{ + struct mtk_port_cfg *dflt_port; + struct mtk_port *port; + int i, ret; + + INIT_RADIX_TREE(&port_mngr->port_tbl[PORT_TBL_SAP], GFP_KERNEL); + INIT_RADIX_TREE(&port_mngr->port_tbl[PORT_TBL_MD], GFP_KERNEL); + + for (i =3D 0; i < port_cnt; i++) { + dflt_port =3D cfg + i; + port =3D mtk_port_alloc_or_restore(port_mngr, dflt_port, s_list); + if (IS_ERR(port)) { + ret =3D PTR_ERR(port); + goto free_ports; + } + } + return 0; + +free_ports: + for (i--; i >=3D 0; i--) { + dflt_port =3D cfg + i; + port =3D mtk_port_search_by_id(port_mngr, dflt_port->rx_ch); + if (port) + mtk_port_free_or_backup(port_mngr, port, s_list); + } + + return ret; +} + +static void mtk_port_tbl_destroy(struct mtk_port_mngr *port_mngr, struct m= tk_stale_list *s_list) +{ + struct radix_tree_iter iter; + struct mtk_port *port; + void __rcu **slot; + int tbl_type; + + tbl_type =3D PORT_TBL_SAP; + do { + radix_tree_for_each_slot(slot, &port_mngr->port_tbl[tbl_type], &iter, 0)= { + MTK_PORT_SEARCH_FROM_RADIX_TREE(port, slot); + MTK_PORT_INTERNAL_NODE_CHECK(port, slot, iter); + ports_ops[port->info.type]->disable(port); + } + tbl_type++; + } while (tbl_type < PORT_TBL_MAX); + + tbl_type =3D PORT_TBL_SAP; + do { + radix_tree_for_each_slot(slot, &port_mngr->port_tbl[tbl_type], &iter, 0)= { + MTK_PORT_SEARCH_FROM_RADIX_TREE(port, slot); + MTK_PORT_INTERNAL_NODE_CHECK(port, slot, iter); + mtk_port_free_or_backup(port_mngr, port, s_list); + } + tbl_type++; + } while (tbl_type < PORT_TBL_MAX); +} + +static struct mtk_stale_list *mtk_port_stale_list_create(struct mtk_port_m= ngr *port_mngr) +{ + struct mtk_stale_list *s_list; + + s_list =3D kzalloc(sizeof(*s_list), GFP_KERNEL); + if (!s_list) + return NULL; + + memcpy(s_list->dev_str, port_mngr->ctrl_blk->mdev->dev_str, MTK_DEV_STR_L= EN); + s_list->dev_id =3D -1; + INIT_LIST_HEAD(&s_list->ports); + + mutex_lock(&port_mngr_grp_mtx); + list_add_tail(&s_list->entry, &stale_list_grp); + mutex_unlock(&port_mngr_grp_mtx); + + return s_list; +} + +static void mtk_port_stale_list_destroy(struct mtk_stale_list *s_list) +{ + mutex_lock(&port_mngr_grp_mtx); + list_del(&s_list->entry); + mutex_unlock(&port_mngr_grp_mtx); + kfree(s_list); +} + +static struct mtk_stale_list *mtk_port_stale_list_search(const char *dev_s= tr) +{ + struct mtk_stale_list *tmp, *s_list =3D NULL; + + mutex_lock(&port_mngr_grp_mtx); + list_for_each_entry(tmp, &stale_list_grp, entry) { + if (!strncmp(tmp->dev_str, dev_str, MTK_DEV_STR_LEN)) { + s_list =3D tmp; + break; + } + } + mutex_unlock(&port_mngr_grp_mtx); + + return s_list; +} + +void mtk_port_stale_list_grp_cleanup(void) +{ + struct mtk_stale_list *s_list, *next_s_list; + struct mtk_port *port, *next_port; + + mutex_lock(&port_mngr_grp_mtx); + list_for_each_entry_safe(s_list, next_s_list, &stale_list_grp, entry) { + list_del(&s_list->entry); + + list_for_each_entry_safe(port, next_port, &s_list->ports, stale_entry) { + list_del(&port->stale_entry); + mtk_port_release(&port->kref); + } + + kfree(s_list); + } + mutex_unlock(&port_mngr_grp_mtx); +} + +static struct mtk_stale_list *mtk_port_stale_list_init(struct mtk_port_mng= r *port_mngr) +{ + struct mtk_stale_list *s_list; + + s_list =3D mtk_port_stale_list_search(port_mngr->ctrl_blk->mdev->dev_str); + if (!s_list) { + dev_info(port_mngr->ctrl_blk->mdev->dev, "Create stale list\n"); + s_list =3D mtk_port_stale_list_create(port_mngr); + if (unlikely(!s_list)) + return NULL; + } else { + dev_info(port_mngr->ctrl_blk->mdev->dev, "Reuse old stale list\n"); + } + + mutex_lock(&port_mngr_grp_mtx); + if (s_list->dev_id < 0) { + port_mngr->dev_id =3D ida_alloc_range(&ccci_dev_ids, 0, + MTK_DFLT_MAX_DEV_CNT - 1, + GFP_KERNEL); + } else { + port_mngr->dev_id =3D s_list->dev_id; + s_list->dev_id =3D -1; + } + mutex_unlock(&port_mngr_grp_mtx); + + return s_list; +} + +static void mtk_port_stale_list_exit(struct mtk_port_mngr *port_mngr, stru= ct mtk_stale_list *s_list) +{ + mutex_lock(&port_mngr_grp_mtx); + if (list_empty(&s_list->ports)) { + ida_free(&ccci_dev_ids, port_mngr->dev_id); + mutex_unlock(&port_mngr_grp_mtx); + mtk_port_stale_list_destroy(s_list); + dev_info(port_mngr->ctrl_blk->mdev->dev, "Destroy stale list\n"); + } else { + s_list->dev_id =3D port_mngr->dev_id; + mutex_unlock(&port_mngr_grp_mtx); + dev_info(port_mngr->ctrl_blk->mdev->dev, "Reserve stale list\n"); + } +} + +static void mtk_port_trb_init(struct mtk_port *port, struct trb *trb, enum= mtk_trb_cmd_type cmd, + int (*trb_complete)(struct sk_buff *skb)) +{ + kref_init(&trb->kref); + trb->vqno =3D port->info.vq_id; + trb->status =3D MTK_DFLT_TRB_STATUS; + trb->priv =3D port; + trb->cmd =3D cmd; + trb->trb_complete =3D trb_complete; +} + +static void mtk_port_trb_free(struct kref *trb_kref) +{ + struct trb *trb =3D container_of(trb_kref, struct trb, kref); + struct sk_buff *skb; + + skb =3D container_of((char *)trb, struct sk_buff, cb[0]); + dev_kfree_skb_any(skb); +} + +static int mtk_port_open_trb_complete(struct sk_buff *skb) +{ + struct trb_open_priv *trb_open_priv =3D (struct trb_open_priv *)skb->data; + struct trb *trb =3D (struct trb *)skb->cb; + struct mtk_port *port =3D trb->priv; + struct mtk_port_mngr *port_mngr; + + port_mngr =3D port->port_mngr; + + if (trb->status && trb->status !=3D -EBUSY) + goto out; + + if (!trb->status) { + port_mngr->vq_info[trb->vqno].tx_mtu =3D trb_open_priv->tx_mtu; + port_mngr->vq_info[trb->vqno].rx_mtu =3D trb_open_priv->rx_mtu; + } + + port->tx_mtu =3D port_mngr->vq_info[trb->vqno].tx_mtu; + port->rx_mtu =3D port_mngr->vq_info[trb->vqno].rx_mtu; + + port->tx_mtu -=3D MTK_CCCI_H_ELEN; + port->rx_mtu -=3D MTK_CCCI_H_ELEN; + +out: + wake_up_interruptible_all(&port->trb_wq); + + dev_info(port->port_mngr->ctrl_blk->mdev->dev, + "Open VQ TRB:status:%d, vq:%d, port:%s, tx_mtu:%d. rx_mtu:%d\n", + trb->status, trb->vqno, port->info.name, port->tx_mtu, port->rx_mtu); + kref_put(&trb->kref, mtk_port_trb_free); + return 0; +} + +static int mtk_port_close_trb_complete(struct sk_buff *skb) +{ + struct trb *trb =3D (struct trb *)skb->cb; + struct mtk_port *port =3D trb->priv; + + wake_up_interruptible_all(&port->trb_wq); + dev_info(port->port_mngr->ctrl_blk->mdev->dev, + "Close VQ TRB: trb->status:%d, vq:%d, port:%s\n", + trb->status, trb->vqno, port->info.name); + kref_put(&trb->kref, mtk_port_trb_free); + + return 0; +} + +static int mtk_port_tx_complete(struct sk_buff *skb) +{ + struct trb *trb =3D (struct trb *)skb->cb; + struct mtk_port *port =3D trb->priv; + + if (trb->status < 0) + dev_warn(port->port_mngr->ctrl_blk->mdev->dev, + "Failed to send data: trb->status:%d, vq:%d, port:%s\n", + trb->status, trb->vqno, port->info.name); + + if (port->info.flags & PORT_F_BLOCKING) + wake_up_interruptible_all(&port->trb_wq); + + kref_put(&trb->kref, mtk_port_trb_free); + + return 0; +} + +static int mtk_port_status_check(struct mtk_port *port) +{ + if (!test_bit(PORT_S_ENABLE, &port->status)) { + pr_err("[TMI]Unable to use port: (%s) disabled. Caller: %ps\n", + port->info.name, __builtin_return_address(0)); + return -ENODEV; + } + + if (!test_bit(PORT_S_OPEN, &port->status) || test_bit(PORT_S_FLUSH, &port= ->status) || + !test_bit(PORT_S_RDWR, &port->status)) { + dev_err(port->port_mngr->ctrl_blk->mdev->dev, + "Unable to use port: (%s), port status =3D 0x%lx. Caller: %ps\n", + port->info.name, port->status, __builtin_return_address(0)); + + return -EBADF; + } + + return 0; +} + +int mtk_port_send_data(struct mtk_port *port, void *data) +{ + struct mtk_port_mngr *port_mngr; + struct mtk_ctrl_trans *trans; + struct sk_buff *skb =3D data; + struct trb *trb; + int ret, len; + + port_mngr =3D port->port_mngr; + trans =3D port_mngr->ctrl_blk->trans; + + trb =3D (struct trb *)skb->cb; + mtk_port_trb_init(port, trb, TRB_CMD_TX, mtk_port_tx_complete); + len =3D skb->len; + kref_get(&trb->kref); /* kref count 1->2 */ + +submit_trb: + mutex_lock(&port->write_lock); + ret =3D mtk_port_status_check(port); + if (!ret) + ret =3D mtk_ctrl_trb_submit(port_mngr->ctrl_blk, skb); + mutex_unlock(&port->write_lock); + + if (ret =3D=3D -EAGAIN && port->info.flags & PORT_F_BLOCKING) { + dev_warn(port_mngr->ctrl_blk->mdev->dev, + "Failed to submit trb for port(%s), ret=3D%d\n", port->info.name, ret); + wait_event_interruptible(port->trb_wq, !VQ_LIST_FULL(trans, trb->vqno)); + goto submit_trb; + } else if (ret < 0) { + dev_warn(port_mngr->ctrl_blk->mdev->dev, + "Failed to submit trb for port(%s), ret=3D%d\n", port->info.name, ret); + kref_put(&trb->kref, mtk_port_trb_free); /* kref count 2->1 */ + dev_kfree_skb_any(skb); + goto end; + } + + if (!(port->info.flags & PORT_F_BLOCKING)) { + kref_put(&trb->kref, mtk_port_trb_free); + ret =3D len; + goto end; + } +start_wait: + ret =3D wait_event_interruptible_timeout(port->trb_wq, + trb->status <=3D 0 || + test_bit(PORT_S_FLUSH, &port->status), + MTK_DFLT_TRB_TIMEOUT); + + if (ret =3D=3D -ERESTARTSYS) + goto start_wait; + else if (test_bit(PORT_S_FLUSH, &port->status)) + ret =3D -EBUSY; + else if (!ret) + ret =3D -ETIMEDOUT; + else + ret =3D (!trb->status) ? len : trb->status; + + kref_put(&trb->kref, mtk_port_trb_free); + +end: + return ret; +} + +static int mtk_port_check_rx_seq(struct mtk_port *port, struct mtk_ccci_he= ader *ccci_h) +{ + u16 seq_num, assert_bit; + + seq_num =3D FIELD_GET(MTK_HDR_FLD_SEQ, le32_to_cpu(ccci_h->status)); + assert_bit =3D FIELD_GET(MTK_HDR_FLD_AST, le32_to_cpu(ccci_h->status)); + if (assert_bit && port->rx_seq && + ((seq_num - port->rx_seq) & MTK_CHECK_RX_SEQ_MASK) !=3D 1) { + dev_err(port->port_mngr->ctrl_blk->mdev->dev, + " seq num out-of-order %d->%d", + FIELD_GET(MTK_HDR_FLD_CHN, le32_to_cpu(ccci_h->status)), + seq_num, port->rx_seq); + return -EPROTO; + } + + return 0; +} + +static int mtk_port_rx_dispatch(struct sk_buff *skb, int len, void *priv) +{ + struct mtk_port_mngr *port_mngr; + struct mtk_ccci_header *ccci_h; + struct mtk_port *port =3D priv; + int ret =3D -EPROTO; + u16 channel; + + if (!skb || !priv) { + pr_err("[TMI] Invalid input value in rx dispatch\n"); + ret =3D -EINVAL; + goto end; + } + + port_mngr =3D port->port_mngr; + + skb->len =3D 0; + skb_reset_tail_pointer(skb); + skb_put(skb, len); + + ccci_h =3D mtk_port_strip_header(skb); + if (unlikely(!ccci_h)) { + dev_warn(port_mngr->ctrl_blk->mdev->dev, + "Unsupported: skb length(%d) is less than ccci header\n", + skb->len); + goto drop_data; + } + + dev_dbg(port_mngr->ctrl_blk->mdev->dev, + "RX header:%08x %08x\n", ccci_h->packet_len, ccci_h->status); + + channel =3D FIELD_GET(MTK_HDR_FLD_CHN, le32_to_cpu(ccci_h->status)); + port =3D mtk_port_search_by_id(port_mngr, channel); + if (unlikely(!port)) { + dev_warn(port_mngr->ctrl_blk->mdev->dev, + "Failed to find port by channel:%d\n", channel); + goto drop_data; + } + + ret =3D mtk_port_check_rx_seq(port, ccci_h); + if (unlikely(ret)) + goto drop_data; + + port->rx_seq =3D FIELD_GET(MTK_HDR_FLD_SEQ, le32_to_cpu(ccci_h->status)); + + ret =3D ports_ops[port->info.type]->recv(port, skb); + + return ret; + +drop_data: + dev_kfree_skb_any(skb); +end: + return ret; +} + +int mtk_port_add_header(struct sk_buff *skb) +{ + struct mtk_ccci_header *ccci_h; + struct mtk_port *port; + struct trb *trb; + int ret =3D 0; + + trb =3D (struct trb *)skb->cb; + if (trb->status =3D=3D 0xADDED) + goto end; + + port =3D trb->priv; + if (!port) { + ret =3D -EINVAL; + goto end; + } + + ccci_h =3D skb_push(skb, sizeof(*ccci_h)); + + ccci_h->packet_header =3D cpu_to_le32(0); + ccci_h->packet_len =3D cpu_to_le32(skb->len); + ccci_h->ex_msg =3D cpu_to_le32(0); + ccci_h->status =3D cpu_to_le32(FIELD_PREP(MTK_HDR_FLD_CHN, port->info.tx_= ch) | + FIELD_PREP(MTK_HDR_FLD_SEQ, port->tx_seq++) | + FIELD_PREP(MTK_HDR_FLD_AST, 1)); + + trb->status =3D 0xADDED; +end: + return ret; +} + +struct mtk_ccci_header *mtk_port_strip_header(struct sk_buff *skb) +{ + struct mtk_ccci_header *ccci_h; + + if (skb->len < sizeof(*ccci_h)) { + pr_err("[TMI] Invalid input value\n"); + return NULL; + } + + ccci_h =3D (struct mtk_ccci_header *)skb->data; + skb_pull(skb, sizeof(*ccci_h)); + + return ccci_h; +} + +int mtk_port_mngr_vq_status_check(struct sk_buff *skb) +{ + struct trb *trb =3D (struct trb *)skb->cb; + struct trb_open_priv *trb_open_priv; + struct mtk_port *port =3D trb->priv; + struct mtk_port_mngr *port_mngr; + int ret =3D 0; + + port_mngr =3D port->port_mngr; + switch (trb->cmd) { + case TRB_CMD_ENABLE: + port_mngr->vq_info[trb->vqno].port_cnt++; + if (port_mngr->vq_info[trb->vqno].port_cnt =3D=3D 1) { + trb_open_priv =3D (struct trb_open_priv *)skb->data; + trb_open_priv->rx_done =3D mtk_port_rx_dispatch; + break; + } + + trb->status =3D -EBUSY; + trb->trb_complete(skb); + ret =3D -EBUSY; + break; + case TRB_CMD_DISABLE: + port_mngr->vq_info[trb->vqno].port_cnt--; + if (!port_mngr->vq_info[trb->vqno].port_cnt) + break; + + dev_info(port_mngr->ctrl_blk->mdev->dev, + "VQ(%d) still has %d port, skip to handle close skb\n", + trb->vqno, port_mngr->vq_info[trb->vqno].port_cnt); + trb->status =3D -EBUSY; + trb->trb_complete(skb); + ret =3D -EBUSY; + break; + default: + dev_err(port_mngr->ctrl_blk->mdev->dev, "Invalid trb command(%d)\n", trb= ->cmd); + ret =3D -EINVAL; + break; + } + return ret; +} + +int mtk_port_vq_enable(struct mtk_port *port) +{ + struct mtk_port_mngr *port_mngr =3D port->port_mngr; + struct sk_buff *skb; + int ret =3D -ENOMEM; + struct trb *trb; + + skb =3D __dev_alloc_skb(port->tx_mtu, GFP_KERNEL); + if (!skb) + goto end; + + skb_put(skb, sizeof(struct trb_open_priv)); + trb =3D (struct trb *)skb->cb; + mtk_port_trb_init(port, trb, TRB_CMD_ENABLE, mtk_port_open_trb_complete); + kref_get(&trb->kref); + + ret =3D mtk_ctrl_trb_submit(port_mngr->ctrl_blk, skb); + if (ret) { + dev_err(port_mngr->ctrl_blk->mdev->dev, + "Failed to submit trb for port(%s), ret=3D%d\n", port->info.name, ret); + kref_put(&trb->kref, mtk_port_trb_free); + mtk_port_trb_free(&trb->kref); + goto end; + } + +start_wait: + ret =3D wait_event_interruptible_timeout(port->trb_wq, trb->status <=3D 0, + MTK_DFLT_TRB_TIMEOUT); + if (ret =3D=3D -ERESTARTSYS) + goto start_wait; + else if (!ret) + ret =3D -ETIMEDOUT; + else + ret =3D trb->status; + + kref_put(&trb->kref, mtk_port_trb_free); + +end: + return ret; +} + +int mtk_port_vq_disable(struct mtk_port *port) +{ + struct mtk_port_mngr *port_mngr =3D port->port_mngr; + struct sk_buff *skb; + int ret =3D -ENOMEM; + struct trb *trb; + + skb =3D __dev_alloc_skb(port->tx_mtu, GFP_KERNEL); + if (!skb) + goto end; + + skb_put(skb, sizeof(struct trb_open_priv)); + trb =3D (struct trb *)skb->cb; + mtk_port_trb_init(port, trb, TRB_CMD_DISABLE, mtk_port_close_trb_complete= ); + kref_get(&trb->kref); + + mutex_lock(&port->write_lock); + ret =3D mtk_ctrl_trb_submit(port_mngr->ctrl_blk, skb); + mutex_unlock(&port->write_lock); + if (ret) { + dev_warn(port_mngr->ctrl_blk->mdev->dev, + "Failed to submit trb for port(%s), ret=3D%d\n", port->info.name, ret); + kref_put(&trb->kref, mtk_port_trb_free); + mtk_port_trb_free(&trb->kref); + goto end; + } + +start_wait: + ret =3D wait_event_interruptible(port->trb_wq, trb->status <=3D 0); + if (ret =3D=3D -ERESTARTSYS) + goto start_wait; + + ret =3D trb->status; + kref_put(&trb->kref, mtk_port_trb_free); + +end: + return ret; +} + +int mtk_port_mngr_init(struct mtk_ctrl_blk *ctrl_blk) +{ + struct mtk_port_mngr *port_mngr; + struct mtk_stale_list *s_list; + int ret =3D -ENOMEM; + + port_mngr =3D devm_kzalloc(ctrl_blk->mdev->dev, sizeof(*port_mngr), GFP_K= ERNEL); + if (unlikely(!port_mngr)) { + dev_err(ctrl_blk->mdev->dev, "Failed to alloc memory for port_mngr\n"); + goto end; + } + + port_mngr->ctrl_blk =3D ctrl_blk; + + s_list =3D mtk_port_stale_list_init(port_mngr); + if (!s_list) { + dev_err(ctrl_blk->mdev->dev, "Failed to init mtk_stale_list\n"); + goto free_port_mngr; + } + + ret =3D mtk_port_tbl_create(port_mngr, (struct mtk_port_cfg *)port_cfg, + ARRAY_SIZE(port_cfg), s_list); + if (unlikely(ret)) { + dev_err(ctrl_blk->mdev->dev, "Failed to create port_tbl\n"); + goto exit_stale_list; + } + ctrl_blk->port_mngr =3D port_mngr; + dev_info(ctrl_blk->mdev->dev, "Initialize port_mngr successfully\n"); + + return ret; + +exit_stale_list: + mtk_port_stale_list_exit(port_mngr, s_list); +free_port_mngr: + devm_kfree(ctrl_blk->mdev->dev, port_mngr); +end: + return ret; +} + +void mtk_port_mngr_exit(struct mtk_ctrl_blk *ctrl_blk) +{ + struct mtk_port_mngr *port_mngr =3D ctrl_blk->port_mngr; + struct mtk_stale_list *s_list; + + s_list =3D mtk_port_stale_list_search(port_mngr->ctrl_blk->mdev->dev_str); + mtk_port_tbl_destroy(port_mngr, s_list); + mtk_port_stale_list_exit(port_mngr, s_list); + devm_kfree(ctrl_blk->mdev->dev, port_mngr); + ctrl_blk->port_mngr =3D NULL; + dev_info(ctrl_blk->mdev->dev, "Exit port_mngr successfully\n"); +} diff --git a/drivers/net/wwan/mediatek/mtk_port.h b/drivers/net/wwan/mediat= ek/mtk_port.h new file mode 100644 index 000000000000..dd6d47092028 --- /dev/null +++ b/drivers/net/wwan/mediatek/mtk_port.h @@ -0,0 +1,171 @@ +/* SPDX-License-Identifier: BSD-3-Clause-Clear + * + * Copyright (c) 2022, MediaTek Inc. + */ + +#ifndef __MTK_PORT_H__ +#define __MTK_PORT_H__ + +#include +#include +#include +#include +#include + +#include "mtk_ctrl_plane.h" +#include "mtk_dev.h" + +#define MTK_PEER_ID_MASK (0xF000) +#define MTK_PEER_ID_SHIFT (12) +#define MTK_PEER_ID(ch) (((ch) & MTK_PEER_ID_MASK) >> MTK_PEER_ID_SHIFT) +#define MTK_PEER_ID_SAP (0x1) +#define MTK_PEER_ID_MD (0x2) +#define MTK_CH_ID_MASK (0x0FFF) +#define MTK_CH_ID(ch) ((ch) & MTK_CH_ID_MASK) +#define MTK_DFLT_MAX_DEV_CNT (10) +#define MTK_DFLT_PORT_NAME_LEN (20) + +#define MTK_PORT_TBL_TYPE(ch) (MTK_PEER_ID(ch) - 1) + +#define MTK_CCCI_H_ELEN (128) + +#define MTK_HDR_FLD_AST ((u32)BIT(31)) +#define MTK_HDR_FLD_SEQ GENMASK(30, 16) +#define MTK_HDR_FLD_CHN GENMASK(15, 0) + +#define MTK_INFO_FLD_EN ((u16)BIT(15)) +#define MTK_INFO_FLD_CHID GENMASK(14, 0) + +enum mtk_port_status { + PORT_S_DFLT =3D 0, + PORT_S_ENABLE, + PORT_S_OPEN, + PORT_S_RDWR, + PORT_S_FLUSH, + PORT_S_ON_STALE_LIST, +}; + +enum mtk_ccci_ch { + CCCI_SAP_CONTROL_RX =3D 0x1000, + CCCI_SAP_CONTROL_TX =3D 0x1001, + CCCI_CONTROL_RX =3D 0x2000, + CCCI_CONTROL_TX =3D 0x2001, +}; + +enum mtk_port_flag { + PORT_F_DFLT =3D 0, + PORT_F_BLOCKING =3D BIT(1), + PORT_F_ALLOW_DROP =3D BIT(2), +}; + +enum mtk_port_tbl { + PORT_TBL_SAP, + PORT_TBL_MD, + PORT_TBL_MAX +}; + +enum mtk_port_type { + PORT_TYPE_INTERNAL, + PORT_TYPE_MAX +}; + +struct mtk_internal_port { + void *arg; + int (*recv_cb)(void *arg, struct sk_buff *skb); +}; + +union mtk_port_priv { + struct mtk_internal_port i_priv; +}; + +struct mtk_port_cfg { + enum mtk_ccci_ch tx_ch; + enum mtk_ccci_ch rx_ch; + unsigned char vq_id; + enum mtk_port_type type; + char name[MTK_DFLT_PORT_NAME_LEN]; + unsigned char flags; +}; + +struct mtk_port { + struct mtk_port_cfg info; + struct kref kref; + bool enable; + unsigned long status; + unsigned int minor; + unsigned short tx_seq; + unsigned short rx_seq; + unsigned int tx_mtu; + unsigned int rx_mtu; + struct sk_buff_head rx_skb_list; + unsigned int rx_data_len; + unsigned int rx_buf_size; + wait_queue_head_t trb_wq; + wait_queue_head_t rx_wq; + /* Use write_lock to lock user's write and disable thread */ + struct mutex write_lock; + /* Used to lock user's read thread */ + struct mutex read_buf_lock; + struct list_head stale_entry; + char dev_str[MTK_DEV_STR_LEN]; + struct mtk_port_mngr *port_mngr; + union mtk_port_priv priv; +}; + +struct mtk_vq_info { + int tx_mtu; + int rx_mtu; + unsigned int port_cnt; + bool color; +}; + +struct mtk_port_mngr { + struct mtk_ctrl_blk *ctrl_blk; + struct radix_tree_root port_tbl[PORT_TBL_MAX]; + struct mtk_vq_info vq_info[VQ_NUM]; + struct kobject *port_attr_kobj; + int dev_id; +}; + +struct mtk_stale_list { + struct list_head entry; + struct list_head ports; + char dev_str[MTK_DEV_STR_LEN]; + int dev_id; +}; + +struct mtk_port_info { + __le16 channel; + __le16 reserved; +} __packed; + +struct mtk_port_enum_msg { + __le32 head_pattern; + __le16 port_cnt; + __le16 version; + __le32 tail_pattern; + u8 data[]; +} __packed; + +struct mtk_ccci_header { + __le32 packet_header; + __le32 packet_len; + __le32 status; + __le32 ex_msg; +}; + +extern const struct port_ops *ports_ops[PORT_TYPE_MAX]; + +void mtk_port_release(struct kref *port_kref); +struct mtk_port *mtk_port_search_by_name(struct mtk_port_mngr *port_mngr, = char *name); +void mtk_port_stale_list_grp_cleanup(void); +int mtk_port_add_header(struct sk_buff *skb); +struct mtk_ccci_header *mtk_port_strip_header(struct sk_buff *skb); +int mtk_port_send_data(struct mtk_port *port, void *data); +int mtk_port_vq_enable(struct mtk_port *port); +int mtk_port_vq_disable(struct mtk_port *port); +int mtk_port_mngr_vq_status_check(struct sk_buff *skb); +int mtk_port_mngr_init(struct mtk_ctrl_blk *ctrl_blk); +void mtk_port_mngr_exit(struct mtk_ctrl_blk *ctrl_blk); + +#endif /* __MTK_PORT_H__ */ diff --git a/drivers/net/wwan/mediatek/mtk_port_io.c b/drivers/net/wwan/med= iatek/mtk_port_io.c new file mode 100644 index 000000000000..2ddd131dfe16 --- /dev/null +++ b/drivers/net/wwan/mediatek/mtk_port_io.c @@ -0,0 +1,253 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +/* + * Copyright (c) 2022, MediaTek Inc. + */ + +#include "mtk_port_io.h" + +#define MTK_DFLT_READ_TIMEOUT (1 * HZ) + +static int mtk_port_get_locked(struct mtk_port *port) +{ + int ret =3D 0; + + mutex_lock(&port_mngr_grp_mtx); + if (!port) { + mutex_unlock(&port_mngr_grp_mtx); + pr_err("[TMI] Port does not exist\n"); + return -ENODEV; + } + kref_get(&port->kref); + mutex_unlock(&port_mngr_grp_mtx); + + return ret; +} + +static void mtk_port_put_locked(struct mtk_port *port) +{ + mutex_lock(&port_mngr_grp_mtx); + kref_put(&port->kref, mtk_port_release); + mutex_unlock(&port_mngr_grp_mtx); +} + +static void mtk_port_struct_init(struct mtk_port *port) +{ + port->tx_seq =3D 0; + port->rx_seq =3D -1; + clear_bit(PORT_S_ENABLE, &port->status); + kref_init(&port->kref); + skb_queue_head_init(&port->rx_skb_list); + port->rx_buf_size =3D MTK_RX_BUF_SIZE; + init_waitqueue_head(&port->trb_wq); + init_waitqueue_head(&port->rx_wq); + mutex_init(&port->read_buf_lock); +} + +static int mtk_port_internal_init(struct mtk_port *port) +{ + mtk_port_struct_init(port); + port->enable =3D false; + + return 0; +} + +static int mtk_port_internal_exit(struct mtk_port *port) +{ + if (test_bit(PORT_S_ENABLE, &port->status)) + ports_ops[port->info.type]->disable(port); + + return 0; +} + +static int mtk_port_reset(struct mtk_port *port) +{ + port->tx_seq =3D 0; + port->rx_seq =3D -1; + + return 0; +} + +static int mtk_port_internal_enable(struct mtk_port *port) +{ + int ret; + + if (test_bit(PORT_S_ENABLE, &port->status)) { + dev_info(port->port_mngr->ctrl_blk->mdev->dev, + "Skip to enable port( %s )\n", port->info.name); + return 0; + } + + ret =3D mtk_port_vq_enable(port); + if (ret && ret !=3D -EBUSY) + return ret; + + set_bit(PORT_S_RDWR, &port->status); + set_bit(PORT_S_ENABLE, &port->status); + dev_info(port->port_mngr->ctrl_blk->mdev->dev, + "Port(%s) enable is complete\n", port->info.name); + + return 0; +} + +static int mtk_port_internal_disable(struct mtk_port *port) +{ + if (!test_and_clear_bit(PORT_S_ENABLE, &port->status)) { + dev_info(port->port_mngr->ctrl_blk->mdev->dev, + "Skip to disable port(%s)\n", port->info.name); + return 0; + } + + clear_bit(PORT_S_RDWR, &port->status); + mtk_port_vq_disable(port); + + dev_info(port->port_mngr->ctrl_blk->mdev->dev, + "Port(%s) disable is complete\n", port->info.name); + + return 0; +} + +static int mtk_port_internal_recv(struct mtk_port *port, struct sk_buff *s= kb) +{ + struct mtk_internal_port *priv; + int ret =3D -ENXIO; + + if (!test_bit(PORT_S_OPEN, &port->status)) { + dev_warn_ratelimited(port->port_mngr->ctrl_blk->mdev->dev, + "Unabled to recv: (%s) not opened\n", port->info.name); + goto drop_data; + } + + priv =3D &port->priv.i_priv; + if (!priv->recv_cb || !priv->arg) { + dev_warn_ratelimited(port->port_mngr->ctrl_blk->mdev->dev, + "Invalid (%s) recv_cb, drop packet\n", port->info.name); + goto drop_data; + } + + ret =3D priv->recv_cb(priv->arg, skb); + return ret; + +drop_data: + dev_kfree_skb_any(skb); + return ret; +} + +static int mtk_port_common_open(struct mtk_port *port) +{ + int ret =3D 0; + + if (!test_bit(PORT_S_ENABLE, &port->status)) { + pr_err("[TMI] Failed to open: (%s) is disabled\n", port->info.name); + ret =3D -ENODEV; + goto end; + } + + if (test_bit(PORT_S_OPEN, &port->status)) { + dev_warn(port->port_mngr->ctrl_blk->mdev->dev, + "Unabled to open port(%s) twice\n", port->info.name); + ret =3D -EBUSY; + goto end; + } + + dev_info(port->port_mngr->ctrl_blk->mdev->dev, "Open port %s\n", port->in= fo.name); + skb_queue_purge(&port->rx_skb_list); + set_bit(PORT_S_OPEN, &port->status); + +end: + return ret; +} + +static void mtk_port_common_close(struct mtk_port *port) +{ + dev_info(port->port_mngr->ctrl_blk->mdev->dev, "Close port %s\n", port->i= nfo.name); + + clear_bit(PORT_S_OPEN, &port->status); + + skb_queue_purge(&port->rx_skb_list); +} + +void *mtk_port_internal_open(struct mtk_md_dev *mdev, char *name, int flag) +{ + struct mtk_port_mngr *port_mngr; + struct mtk_ctrl_blk *ctrl_blk; + struct mtk_port *port; + int ret; + + ctrl_blk =3D mdev->ctrl_blk; + port_mngr =3D ctrl_blk->port_mngr; + + port =3D mtk_port_search_by_name(port_mngr, name); + ret =3D mtk_port_get_locked(port); + if (ret) + goto end; + + ret =3D mtk_port_common_open(port); + if (ret) { + mtk_port_put_locked(port); + goto end; + } + + if (flag & O_NONBLOCK) + port->info.flags &=3D ~PORT_F_BLOCKING; + else + port->info.flags |=3D PORT_F_BLOCKING; +end: + return port; +} + +int mtk_port_internal_close(void *i_port) +{ + struct mtk_port *port =3D i_port; + int ret =3D 0; + + if (!port) { + ret =3D -EINVAL; + goto end; + } + + if (!test_bit(PORT_S_OPEN, &port->status)) { + pr_err("[TMI] Port(%s) has been closed\n", port->info.name); + ret =3D -EBADF; + goto end; + } + + mtk_port_common_close(port); + mtk_port_put_locked(port); +end: + return ret; +} + +int mtk_port_internal_write(void *i_port, struct sk_buff *skb) +{ + struct mtk_port *port =3D i_port; + + if (!port) + return -EINVAL; + + return mtk_port_send_data(port, skb); +} + +void mtk_port_internal_recv_register(void *i_port, + int (*cb)(void *priv, struct sk_buff *skb), + void *arg) +{ + struct mtk_port *port =3D i_port; + struct mtk_internal_port *priv; + + priv =3D &port->priv.i_priv; + priv->arg =3D arg; + priv->recv_cb =3D cb; +} + +static const struct port_ops port_internal_ops =3D { + .init =3D mtk_port_internal_init, + .exit =3D mtk_port_internal_exit, + .reset =3D mtk_port_reset, + .enable =3D mtk_port_internal_enable, + .disable =3D mtk_port_internal_disable, + .recv =3D mtk_port_internal_recv, +}; + +const struct port_ops *ports_ops[PORT_TYPE_MAX] =3D { + &port_internal_ops, +}; diff --git a/drivers/net/wwan/mediatek/mtk_port_io.h b/drivers/net/wwan/med= iatek/mtk_port_io.h new file mode 100644 index 000000000000..30e1d4149881 --- /dev/null +++ b/drivers/net/wwan/mediatek/mtk_port_io.h @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: BSD-3-Clause-Clear + * + * Copyright (c) 2022, MediaTek Inc. + */ + +#ifndef __MTK_PORT_IO_H__ +#define __MTK_PORT_IO_H__ + +#include +#include + +#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); + int (*exit)(struct mtk_port *port); + int (*reset)(struct mtk_port *port); + int (*enable)(struct mtk_port *port); + int (*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); + +#endif /* __MTK_PORT_IO_H__ */ diff --git a/drivers/net/wwan/mediatek/pcie/mtk_pci.c b/drivers/net/wwan/me= diatek/pcie/mtk_pci.c index b3de9634a62c..d1cf4bf10f6a 100644 --- a/drivers/net/wwan/mediatek/pcie/mtk_pci.c +++ b/drivers/net/wwan/mediatek/pcie/mtk_pci.c @@ -13,6 +13,7 @@ #include #include =20 +#include "../mtk_port_io.h" #include "mtk_pci.h" #include "mtk_reg.h" =20 @@ -943,6 +944,7 @@ module_init(mtk_drv_init); static void __exit mtk_drv_exit(void) { pci_unregister_driver(&mtk_pci_drv); + mtk_port_stale_list_grp_cleanup(); } module_exit(mtk_drv_exit); =20 --=20 2.32.0 From nobody Thu Nov 14 06:37:28 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 096D0C74A5B for ; Fri, 17 Mar 2023 08:45:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231221AbjCQIpm (ORCPT ); Fri, 17 Mar 2023 04:45:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39730 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231289AbjCQIoi (ORCPT ); Fri, 17 Mar 2023 04:44:38 -0400 Received: from mailgw01.mediatek.com (unknown [60.244.123.138]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0FBFD1CF4B; Fri, 17 Mar 2023 01:43:20 -0700 (PDT) X-UUID: 7fb09306c49b11ed91027fb02e0f1d65-20230317 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=agO2MFAf3Y8zU6zIJM4IUfnlOog4jrKmih9LFxBZdQ0=; b=ntvxAD58k1UvJw34VC72N08DBkZhRhVyVRIHdBzhPD42arBHAiTUR2lQMkaHsT5WvsAbO1MkytJ/hgkHRAwrQxc5DSXmGp1g1eS6/R7cu+P+lpREPEm2lDQg+NhJYOq0q6+lOTvZOLm0CQu9jJZWVdd8Y/3jVzqAcI8jf+p5J6c=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.21,REQID:589272e6-c625-4b30-944f-b02096d1f2f6,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-25 X-CID-META: VersionHash:83295aa,CLOUDID:1ac31af6-ddba-41c3-91d9-10eeade8eac7,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:11|1,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-UUID: 7fb09306c49b11ed91027fb02e0f1d65-20230317 Received: from mtkmbs10n2.mediatek.inc [(172.21.101.183)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 700838892; Fri, 17 Mar 2023 16:12:45 +0800 Received: from mtkmbs11n2.mediatek.inc (172.21.101.187) by mtkmbs10n2.mediatek.inc (172.21.101.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Fri, 17 Mar 2023 16:12:44 +0800 Received: from mcddlt001.gcn.mediatek.inc (10.19.240.15) by mtkmbs11n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.25 via Frontend Transport; Fri, 17 Mar 2023 16:12:42 +0800 From: Yanchao Yang To: Loic Poulain , Sergey Ryazanov , Johannes Berg , "David S . Miller" , Eric Dumazet , "Jakub Kicinski" , Paolo Abeni , netdev ML , kernel ML CC: Intel experts , Chetan , MTK ML , Liang Lu , Haijun Liu , Hua Yang , Ting Wang , Felix Chen , Mingliang Xu , Min Dong , Aiden Wang , Guohao Zhang , Chris Feng , "Yanchao Yang" , Lambert Wang , Mingchuang Qiao , Xiayu Zhang , Haozhe Chang Subject: [PATCH net-next v4 05/10] net: wwan: tmi: Add FSM thread Date: Fri, 17 Mar 2023 16:09:37 +0800 Message-ID: <20230317080942.183514-6-yanchao.yang@mediatek.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20230317080942.183514-1-yanchao.yang@mediatek.com> References: <20230317080942.183514-1-yanchao.yang@mediatek.com> MIME-Version: 1.0 X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The 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. Signed-off-by: Yanchao Yang Signed-off-by: Mingliang Xu --- drivers/net/wwan/mediatek/Makefile | 3 +- drivers/net/wwan/mediatek/mtk_cldma.c | 20 + drivers/net/wwan/mediatek/mtk_cldma.h | 2 + drivers/net/wwan/mediatek/mtk_common.h | 30 + drivers/net/wwan/mediatek/mtk_ctrl_plane.c | 264 +++++- drivers/net/wwan/mediatek/mtk_ctrl_plane.h | 9 + drivers/net/wwan/mediatek/mtk_dev.c | 19 +- drivers/net/wwan/mediatek/mtk_dev.h | 3 + drivers/net/wwan/mediatek/mtk_fsm.c | 835 ++++++++++++++++++ drivers/net/wwan/mediatek/mtk_fsm.h | 145 +++ drivers/net/wwan/mediatek/mtk_port.c | 189 +++- drivers/net/wwan/mediatek/mtk_port.h | 6 + drivers/net/wwan/mediatek/mtk_port_io.c | 5 +- .../wwan/mediatek/pcie/mtk_cldma_drv_t800.h | 2 + drivers/net/wwan/mediatek/pcie/mtk_pci.c | 9 + drivers/net/wwan/mediatek/pcie/mtk_reg.h | 11 + 16 files changed, 1523 insertions(+), 29 deletions(-) create mode 100644 drivers/net/wwan/mediatek/mtk_common.h create mode 100644 drivers/net/wwan/mediatek/mtk_fsm.c create mode 100644 drivers/net/wwan/mediatek/mtk_fsm.h diff --git a/drivers/net/wwan/mediatek/Makefile b/drivers/net/wwan/mediatek= /Makefile index e3afd8ecb494..c3f13c81b6b0 100644 --- a/drivers/net/wwan/mediatek/Makefile +++ b/drivers/net/wwan/mediatek/Makefile @@ -9,6 +9,7 @@ mtk_tmi-y =3D \ mtk_cldma.o \ pcie/mtk_cldma_drv_t800.o \ mtk_port.o \ - mtk_port_io.o + mtk_port_io.o \ + mtk_fsm.o =20 obj-$(CONFIG_MTK_TMI) +=3D mtk_tmi.o diff --git a/drivers/net/wwan/mediatek/mtk_cldma.c b/drivers/net/wwan/media= tek/mtk_cldma.c index a1c5564e670f..6530b61b67db 100644 --- a/drivers/net/wwan/mediatek/mtk_cldma.c +++ b/drivers/net/wwan/mediatek/mtk_cldma.c @@ -252,9 +252,29 @@ static int mtk_cldma_trb_process(void *dev, struct sk_= buff *skb) return err; } =20 +static void mtk_cldma_fsm_state_listener(struct mtk_fsm_param *param, stru= ct mtk_ctrl_trans *trans) +{ + struct cldma_dev *cd =3D trans->dev[CLDMA_CLASS_ID]; + int i; + + switch (param->to) { + case FSM_STATE_BOOTUP: + for (i =3D 0; i < NR_CLDMA; i++) + cd->hw_ops.init(cd, i); + break; + case FSM_STATE_OFF: + for (i =3D 0; i < NR_CLDMA; i++) + cd->hw_ops.exit(cd, i); + break; + default: + break; + } +} + struct hif_ops cldma_ops =3D { .init =3D mtk_cldma_init, .exit =3D mtk_cldma_exit, .trb_process =3D mtk_cldma_trb_process, .submit_tx =3D mtk_cldma_submit_tx, + .fsm_state_listener =3D mtk_cldma_fsm_state_listener, }; diff --git a/drivers/net/wwan/mediatek/mtk_cldma.h b/drivers/net/wwan/media= tek/mtk_cldma.h index 4fd5f826bcf6..c9656aa31455 100644 --- a/drivers/net/wwan/mediatek/mtk_cldma.h +++ b/drivers/net/wwan/mediatek/mtk_cldma.h @@ -10,6 +10,7 @@ =20 #include "mtk_ctrl_plane.h" #include "mtk_dev.h" +#include "mtk_fsm.h" =20 #define HW_QUEUE_NUM 8 #define ALLQ (0XFF) @@ -134,6 +135,7 @@ struct cldma_hw_ops { int (*txq_free)(struct cldma_hw *hw, int vqno); int (*rxq_free)(struct cldma_hw *hw, int vqno); int (*start_xfer)(struct cldma_hw *hw, int qno); + void (*fsm_state_listener)(struct mtk_fsm_param *param, struct cldma_hw *= hw); }; =20 struct cldma_hw { diff --git a/drivers/net/wwan/mediatek/mtk_common.h b/drivers/net/wwan/medi= atek/mtk_common.h new file mode 100644 index 000000000000..516d3d9e02cf --- /dev/null +++ b/drivers/net/wwan/mediatek/mtk_common.h @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: BSD-3-Clause-Clear + * + * Copyright (c) 2022, MediaTek Inc. + */ + +#ifndef _MTK_COMMON_H +#define _MTK_COMMON_H + +#include + +#define MTK_UEVENT_INFO_LEN 128 + +/* MTK uevent */ +enum mtk_uevent_id { + MTK_UEVENT_FSM =3D 1, + MTK_UEVENT_MAX +}; + +static inline void mtk_uevent_notify(struct device *dev, enum mtk_uevent_i= d id, const char *info) +{ + char buf[MTK_UEVENT_INFO_LEN]; + char *ext[2] =3D {NULL, NULL}; + + snprintf(buf, MTK_UEVENT_INFO_LEN, "%s:event_id=3D%d, info=3D%s", + dev->kobj.name, id, info); + ext[0] =3D buf; + kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, ext); +} + +#endif /* _MTK_COMMON_H */ diff --git a/drivers/net/wwan/mediatek/mtk_ctrl_plane.c b/drivers/net/wwan/= mediatek/mtk_ctrl_plane.c index 8adb1f53ec64..f5ac493f0146 100644 --- a/drivers/net/wwan/mediatek/mtk_ctrl_plane.c +++ b/drivers/net/wwan/mediatek/mtk_ctrl_plane.c @@ -14,6 +14,11 @@ #include "mtk_ctrl_plane.h" #include "mtk_port.h" =20 +static const struct virtq vq_tbl[] =3D { + {VQ(0), CLDMA0, TXQ(0), RXQ(0), VQ_MTU_3_5K, VQ_MTU_3_5K, TX_REQ_NUM, RX_= REQ_NUM}, + {VQ(1), CLDMA1, TXQ(0), RXQ(0), VQ_MTU_3_5K, VQ_MTU_3_5K, TX_REQ_NUM, RX_= REQ_NUM}, +}; + static int mtk_ctrl_get_hif_id(unsigned char peer_id) { if (peer_id =3D=3D MTK_PEER_ID_SAP) @@ -95,6 +100,160 @@ int mtk_ctrl_vq_color_cleanup(struct mtk_ctrl_blk *ctr= l_blk, unsigned char peer_ return 0; } =20 +static bool mtk_ctrl_vqs_is_empty(struct trb_srv *srv) +{ + int i; + + for (i =3D srv->vq_start; i < srv->vq_cnt; i++) { + if (!skb_queue_empty(&srv->trans->skb_list[i])) + return false; + } + + return true; +} + +static void mtk_ctrl_vq_flush(struct trb_srv *srv, int vqno) +{ + struct mtk_ctrl_trans *trans =3D srv->trans; + struct sk_buff *skb; + struct trb *trb; + + while (!skb_queue_empty(&trans->skb_list[vqno])) { + skb =3D skb_dequeue(&trans->skb_list[vqno]); + trb =3D (struct trb *)skb->cb; + trb->status =3D -EIO; + trb->trb_complete(skb); + } +} + +static void mtk_ctrl_vqs_flush(struct trb_srv *srv) +{ + int i; + + for (i =3D srv->vq_start; i < srv->vq_cnt; i++) + mtk_ctrl_vq_flush(srv, i); +} + +static void mtk_ctrl_trb_process(struct trb_srv *srv) +{ + struct mtk_ctrl_trans *trans =3D srv->trans; + struct sk_buff *skb, *skb_next; + struct trb *trb, *trb_next; + int tx_burst_cnt =3D 0; + struct virtq *vq; + int loop; + int idx; + int err; + int i; + + for (i =3D srv->vq_start; i < srv->vq_cnt; i++) { + loop =3D 0; + do { + if (skb_queue_empty(&trans->skb_list[i])) + break; + + skb =3D skb_peek(&trans->skb_list[i]); + trb =3D (struct trb *)skb->cb; + vq =3D trans->vq_tbl + trb->vqno; + idx =3D (vq->hif_id >> HIF_CLASS_SHIFT) & (HIF_CLASS_WIDTH - 1); + if (idx < 0 || idx >=3D HIF_CLASS_NUM) + break; + + switch (trb->cmd) { + case TRB_CMD_ENABLE: + case TRB_CMD_DISABLE: + skb_unlink(skb, &trans->skb_list[i]); + err =3D mtk_port_mngr_vq_status_check(skb); + if (!err && trb->cmd =3D=3D TRB_CMD_DISABLE) + mtk_ctrl_vq_flush(srv, i); + break; + case TRB_CMD_TX: + mtk_port_add_header(skb); + err =3D trans->ops[idx]->submit_tx(trans->dev[idx], skb); + if (err) + break; + + tx_burst_cnt++; + if (tx_burst_cnt >=3D TX_BURST_MAX_CNT || + skb_queue_is_last(&trans->skb_list[i], skb)) { + tx_burst_cnt =3D 0; + } else { + skb_next =3D skb_peek_next(skb, &trans->skb_list[i]); + trb_next =3D (struct trb *)skb_next->cb; + if (trb_next->cmd !=3D TRB_CMD_TX) + tx_burst_cnt =3D 0; + } + + skb_unlink(skb, &trans->skb_list[i]); + err =3D tx_burst_cnt; + break; + default: + err =3D -EFAULT; + } + + if (!err) + trans->ops[idx]->trb_process(trans->dev[idx], skb); + + loop++; + } while (loop < TRB_NUM_PER_ROUND); + } +} + +static int mtk_ctrl_trb_thread(void *args) +{ + struct trb_srv *srv =3D args; + + while (!kthread_should_stop()) { + if (mtk_ctrl_vqs_is_empty(srv)) + wait_event_freezable(srv->trb_waitq, + !mtk_ctrl_vqs_is_empty(srv) || + kthread_should_stop() || kthread_should_park()); + + if (kthread_should_stop()) + break; + + if (kthread_should_park()) + kthread_parkme(); + + do { + mtk_ctrl_trb_process(srv); + if (need_resched()) + cond_resched(); + } while (!mtk_ctrl_vqs_is_empty(srv) && !kthread_should_stop() && + !kthread_should_park()); + } + mtk_ctrl_vqs_flush(srv); + return 0; +} + +static int mtk_ctrl_trb_srv_init(struct mtk_ctrl_trans *trans) +{ + struct trb_srv *srv; + + srv =3D devm_kzalloc(trans->mdev->dev, sizeof(*srv), GFP_KERNEL); + if (!srv) + return -ENOMEM; + + srv->trans =3D trans; + srv->vq_start =3D 0; + srv->vq_cnt =3D VQ_NUM; + + init_waitqueue_head(&srv->trb_waitq); + srv->trb_thread =3D kthread_run(mtk_ctrl_trb_thread, srv, "mtk_trb_srv_%s= ", + trans->mdev->dev_str); + trans->trb_srv =3D srv; + + return 0; +} + +static void mtk_ctrl_trb_srv_exit(struct mtk_ctrl_trans *trans) +{ + struct trb_srv *srv =3D trans->trb_srv; + + kthread_stop(srv->trb_thread); + devm_kfree(trans->mdev->dev, srv); +} + int mtk_ctrl_trb_submit(struct mtk_ctrl_blk *blk, struct sk_buff *skb) { struct mtk_ctrl_trans *trans =3D blk->trans; @@ -112,12 +271,105 @@ int mtk_ctrl_trb_submit(struct mtk_ctrl_blk *blk, st= ruct sk_buff *skb) if (VQ_LIST_FULL(trans, vqno) && trb->cmd !=3D TRB_CMD_DISABLE) return -EAGAIN; =20 - /* This function will implement in next patch */ + if (trb->cmd =3D=3D TRB_CMD_DISABLE) + skb_queue_head(&trans->skb_list[vqno], skb); + else + skb_queue_tail(&trans->skb_list[vqno], skb); + wake_up(&trans->trb_srv->trb_waitq); =20 return 0; } =20 +static int mtk_ctrl_trans_init(struct mtk_ctrl_blk *ctrl_blk) +{ + struct mtk_ctrl_trans *trans; + int err; + int i; + + trans =3D devm_kzalloc(ctrl_blk->mdev->dev, sizeof(*trans), GFP_KERNEL); + if (!trans) + return -ENOMEM; + + trans->ctrl_blk =3D ctrl_blk; + trans->vq_tbl =3D (struct virtq *)vq_tbl; + trans->ops[CLDMA_CLASS_ID] =3D &cldma_ops; + trans->mdev =3D ctrl_blk->mdev; + + for (i =3D 0; i < VQ_NUM; i++) + skb_queue_head_init(&trans->skb_list[i]); + + for (i =3D 0; i < HIF_CLASS_NUM; i++) { + err =3D trans->ops[i]->init(trans); + if (err) + goto err_exit; + } + + err =3D mtk_ctrl_trb_srv_init(trans); + if (err) + goto err_exit; + + ctrl_blk->trans =3D trans; + atomic_set(&trans->available, 1); + + return 0; + +err_exit: + for (i--; i >=3D 0; i--) + trans->ops[i]->exit(trans); + + devm_kfree(ctrl_blk->mdev->dev, trans); + return err; +} + +static int mtk_ctrl_trans_exit(struct mtk_ctrl_blk *ctrl_blk) +{ + struct mtk_ctrl_trans *trans =3D ctrl_blk->trans; + int i; + + atomic_set(&trans->available, 0); + mtk_ctrl_trb_srv_exit(trans); + + for (i =3D 0; i < HIF_CLASS_NUM; i++) + trans->ops[i]->exit(trans); + + devm_kfree(ctrl_blk->mdev->dev, trans); + return 0; +} + +static void mtk_ctrl_trans_fsm_state_handler(struct mtk_fsm_param *param, + struct mtk_ctrl_blk *ctrl_blk) +{ + int i; + + switch (param->to) { + case FSM_STATE_OFF: + for (i =3D 0; i < HIF_CLASS_NUM; i++) + ctrl_blk->trans->ops[i]->fsm_state_listener(param, ctrl_blk->trans); + mtk_ctrl_trans_exit(ctrl_blk); + break; + case FSM_STATE_ON: + mtk_ctrl_trans_init(ctrl_blk); + fallthrough; + default: + for (i =3D 0; i < HIF_CLASS_NUM; i++) + ctrl_blk->trans->ops[i]->fsm_state_listener(param, ctrl_blk->trans); + } +} + +static void mtk_ctrl_fsm_state_listener(struct mtk_fsm_param *param, void = *data) +{ + struct mtk_ctrl_blk *ctrl_blk =3D data; + + if (param->to =3D=3D FSM_STATE_BOOTUP) { + mtk_ctrl_trans_fsm_state_handler(param, ctrl_blk); + mtk_port_mngr_fsm_state_handler(param, ctrl_blk->port_mngr); + } else { + mtk_port_mngr_fsm_state_handler(param, ctrl_blk->port_mngr); + mtk_ctrl_trans_fsm_state_handler(param, ctrl_blk); + } +} + int mtk_ctrl_init(struct mtk_md_dev *mdev) { struct mtk_ctrl_blk *ctrl_blk; @@ -134,8 +386,17 @@ int mtk_ctrl_init(struct mtk_md_dev *mdev) if (err) goto err_free_mem; =20 + err =3D mtk_fsm_notifier_register(mdev, MTK_USER_CTRL, mtk_ctrl_fsm_state= _listener, + ctrl_blk, FSM_PRIO_1, false); + if (err) { + dev_err(mdev->dev, "Fail to register fsm notification(ret =3D %d)\n", er= r); + goto err_port_exit; + } + return 0; =20 +err_port_exit: + mtk_port_mngr_exit(ctrl_blk); err_free_mem: devm_kfree(mdev->dev, ctrl_blk); =20 @@ -146,6 +407,7 @@ int mtk_ctrl_exit(struct mtk_md_dev *mdev) { struct mtk_ctrl_blk *ctrl_blk =3D mdev->ctrl_blk; =20 + mtk_fsm_notifier_unregister(mdev, MTK_USER_CTRL); mtk_port_mngr_exit(ctrl_blk); devm_kfree(mdev->dev, ctrl_blk); =20 diff --git a/drivers/net/wwan/mediatek/mtk_ctrl_plane.h b/drivers/net/wwan/= mediatek/mtk_ctrl_plane.h index 2e1f21d43644..0885a434616e 100644 --- a/drivers/net/wwan/mediatek/mtk_ctrl_plane.h +++ b/drivers/net/wwan/mediatek/mtk_ctrl_plane.h @@ -10,17 +10,25 @@ #include =20 #include "mtk_dev.h" +#include "mtk_fsm.h" =20 #define VQ(N) (N) #define VQ_NUM (2) +#define TX_REQ_NUM (16) +#define RX_REQ_NUM (TX_REQ_NUM) =20 +#define VQ_MTU_2K (0x800) #define VQ_MTU_3_5K (0xE00) +#define VQ_MTU_7K (0x1C00) #define VQ_MTU_63K (0xFC00) =20 +#define TRB_NUM_PER_ROUND (16) #define SKB_LIST_MAX_LEN (16) +#define TX_BURST_MAX_CNT (5) =20 #define HIF_CLASS_NUM (1) #define HIF_CLASS_SHIFT (8) +#define HIF_CLASS_WIDTH (8) #define HIF_ID_BITMASK (0x01) =20 #define VQ_LIST_FULL(trans, vqno) ((trans)->skb_list[vqno].qlen >=3D SKB_L= IST_MAX_LEN) @@ -70,6 +78,7 @@ struct hif_ops { int (*exit)(struct mtk_ctrl_trans *trans); int (*submit_tx)(void *dev, struct sk_buff *skb); int (*trb_process)(void *dev, struct sk_buff *skb); + void (*fsm_state_listener)(struct mtk_fsm_param *param, struct mtk_ctrl_t= rans *trans); }; =20 struct mtk_ctrl_trans { diff --git a/drivers/net/wwan/mediatek/mtk_dev.c b/drivers/net/wwan/mediate= k/mtk_dev.c index d34c3933e84d..db41acc5e733 100644 --- a/drivers/net/wwan/mediatek/mtk_dev.c +++ b/drivers/net/wwan/mediatek/mtk_dev.c @@ -5,21 +5,38 @@ =20 #include "mtk_ctrl_plane.h" #include "mtk_dev.h" +#include "mtk_fsm.h" =20 int mtk_dev_init(struct mtk_md_dev *mdev) { int ret; =20 - ret =3D mtk_ctrl_init(mdev); + ret =3D mtk_fsm_init(mdev); if (ret) goto exit; =20 + ret =3D mtk_ctrl_init(mdev); + if (ret) + goto free_fsm; + return 0; +free_fsm: + mtk_fsm_exit(mdev); exit: return ret; } =20 void mtk_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_ctrl_exit(mdev); + mtk_fsm_exit(mdev); +} + +int mtk_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; } diff --git a/drivers/net/wwan/mediatek/mtk_dev.h b/drivers/net/wwan/mediate= k/mtk_dev.h index d48fc55ddef0..23cedb93e51a 100644 --- a/drivers/net/wwan/mediatek/mtk_dev.h +++ b/drivers/net/wwan/mediatek/mtk_dev.h @@ -101,11 +101,14 @@ struct mtk_md_dev { u32 hw_ver; int msi_nvecs; char dev_str[MTK_DEV_STR_LEN]; + + struct mtk_md_fsm *fsm; void *ctrl_blk; }; =20 int mtk_dev_init(struct mtk_md_dev *mdev); void mtk_dev_exit(struct mtk_md_dev *mdev); +int mtk_dev_start(struct mtk_md_dev *mdev); static inline u32 mtk_hw_read32(struct mtk_md_dev *mdev, u64 addr) { return mdev->hw_ops->read32(mdev, addr); diff --git a/drivers/net/wwan/mediatek/mtk_fsm.c b/drivers/net/wwan/mediate= k/mtk_fsm.c new file mode 100644 index 000000000000..be63f8adcb15 --- /dev/null +++ b/drivers/net/wwan/mediatek/mtk_fsm.c @@ -0,0 +1,835 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +/* + * Copyright (c) 2022, MediaTek Inc. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mtk_common.h" +#include "mtk_fsm.h" +#include "mtk_port.h" +#include "mtk_port_io.h" +#include "pcie/mtk_reg.h" + +#define EVT_TF_GATECLOSED (1) + +#define FSM_HS_START_MASK (FSM_F_SAP_HS_START | FSM_F_MD_HS_START) +#define FSM_HS2_DONE_MASK (FSM_F_SAP_HS2_DONE | FSM_F_MD_HS2_DONE) + +#define RTFT_DATA_SIZE (3 * 1024) + +#define REGION_BITMASK 0xF +#define DEVICE_CFG_SHIFT 24 +#define DEVICE_CFG_REGION_MASK 0x3 + +enum device_stage { + DEV_STAGE_LINUX =3D 4, + DEV_STAGE_MAX +}; + +enum device_cfg { + DEV_CFG_NORMAL =3D 0, + DEV_CFG_MD_ONLY, +}; + +enum runtime_feature_support_type { + RTFT_TYPE_NOT_EXIST =3D 0, + RTFT_TYPE_NOT_SUPPORT =3D 1, + RTFT_TYPE_MUST_SUPPORT =3D 2, + RTFT_TYPE_OPTIONAL_SUPPORT =3D 3, + RTFT_TYPE_SUPPORT_BACKWARD_COMPAT =3D 4, +}; + +enum runtime_feature_id { + RTFT_ID_MD_PORT_ENUM =3D 0, + RTFT_ID_SAP_PORT_ENUM =3D 1, + RTFT_ID_MD_PORT_CFG =3D 2, + RTFT_ID_MAX +}; + +enum ctrl_msg_id { + CTRL_MSG_HS1 =3D 0, + CTRL_MSG_HS2 =3D 1, + CTRL_MSG_HS3 =3D 2, +}; + +struct ctrl_msg_header { + __le32 id; + __le32 ex_msg; + __le32 data_len; + u8 reserved[]; +} __packed; + +struct runtime_feature_entry { + u8 feature_id; + struct runtime_feature_info support_info; + u8 reserved[2]; + __le32 data_len; + u8 data[]; +}; + +struct feature_query { + __le32 head_pattern; + struct runtime_feature_info ft_set[FEATURE_CNT]; + __le32 tail_pattern; +}; + +static int mtk_fsm_send_hs1_msg(struct fsm_hs_info *hs_info) +{ + struct mtk_md_fsm *fsm =3D container_of(hs_info, struct mtk_md_fsm, hs_in= fo[hs_info->id]); + struct ctrl_msg_header *ctrl_msg_h; + struct feature_query *ft_query; + struct sk_buff *skb; + int ret, msg_size; + + msg_size =3D sizeof(*ctrl_msg_h) + sizeof(*ft_query); + skb =3D __dev_alloc_skb(msg_size, GFP_KERNEL); + if (!skb) { + ret =3D -ENOMEM; + goto hs_err; + } + + skb_put(skb, msg_size); + ctrl_msg_h =3D (struct ctrl_msg_header *)skb->data; + ctrl_msg_h->id =3D cpu_to_le32(CTRL_MSG_HS1); + ctrl_msg_h->ex_msg =3D 0; + ctrl_msg_h->data_len =3D cpu_to_le32(sizeof(*ft_query)); + + ft_query =3D (struct feature_query *)(skb->data + sizeof(*ctrl_msg_h)); + ft_query->head_pattern =3D cpu_to_le32(FEATURE_QUERY_PATTERN); + memcpy(ft_query->ft_set, hs_info->query_ft_set, sizeof(hs_info->query_ft_= set)); + ft_query->tail_pattern =3D cpu_to_le32(FEATURE_QUERY_PATTERN); + + ret =3D mtk_port_internal_write(hs_info->ctrl_port, skb); + if (ret > 0) + return 0; +hs_err: + dev_err(fsm->mdev->dev, "Failed to send handshake1 message,ret=3D%d\n", r= et); + return ret; +} + +static int mtk_fsm_feature_set_match(enum runtime_feature_support_type *cu= r_ft_spt, + struct runtime_feature_info rtft_info_st, + struct runtime_feature_info rtft_info_cfg) +{ + int ret =3D 0; + + switch (FIELD_GET(FEATURE_TYPE, rtft_info_cfg.feature)) { + case RTFT_TYPE_NOT_EXIST: + *cur_ft_spt =3D RTFT_TYPE_NOT_SUPPORT; + break; + case RTFT_TYPE_MUST_SUPPORT: + if (FIELD_GET(FEATURE_TYPE, rtft_info_st.feature) !=3D RTFT_TYPE_MUST_SU= PPORT) + ret =3D -EPROTO; + else + *cur_ft_spt =3D RTFT_TYPE_MUST_SUPPORT; + break; + case RTFT_TYPE_OPTIONAL_SUPPORT: + *cur_ft_spt =3D FIELD_PREP(FEATURE_TYPE, rtft_info_st.feature); + break; + default: + ret =3D -EPROTO; + } + + return ret; +} + +static int (*rtft_action[FEATURE_CNT])(struct mtk_md_dev *mdev, void *rt_d= ata) =3D { + [RTFT_ID_MD_PORT_ENUM] =3D mtk_port_status_update, + [RTFT_ID_SAP_PORT_ENUM] =3D mtk_port_status_update, +}; + +static int mtk_fsm_parse_hs2_msg(struct fsm_hs_info *hs_info) +{ + struct mtk_md_fsm *fsm =3D container_of(hs_info, struct mtk_md_fsm, hs_in= fo[hs_info->id]); + char *rt_data =3D ((struct sk_buff *)hs_info->rt_data)->data; + enum runtime_feature_support_type cur_ft_spt; + struct runtime_feature_entry *rtft_entry; + int ft_id, ret =3D 0, offset; + + offset =3D sizeof(struct feature_query); + for (ft_id =3D 0; ft_id < FEATURE_CNT && offset < hs_info->rt_data_len; f= t_id++) { + rtft_entry =3D (struct runtime_feature_entry *)(rt_data + offset); + ret =3D mtk_fsm_feature_set_match(&cur_ft_spt, + rtft_entry->support_info, + hs_info->query_ft_set[ft_id]); + if (ret < 0) + break; + + if (cur_ft_spt =3D=3D RTFT_TYPE_MUST_SUPPORT) + if (rtft_action[ft_id]) + ret =3D rtft_action[ft_id](fsm->mdev, rtft_entry->data); + if (ret < 0) + break; + + offset +=3D sizeof(rtft_entry) + le32_to_cpu(rtft_entry->data_len); + } + + if (ft_id !=3D FEATURE_CNT) { + dev_err(fsm->mdev->dev, "Unable to handle mistake hs2 message,fd_id=3D%d= \n", ft_id); + ret =3D -EPROTO; + } + + return ret; +} + +static int mtk_fsm_append_rtft_entries(struct mtk_md_dev *mdev, void *feat= ure_data, + unsigned int *len, struct fsm_hs_info *hs_info) +{ + char *rt_data =3D ((struct sk_buff *)hs_info->rt_data)->data; + struct runtime_feature_entry *rtft_entry; + int ft_id, ret =3D 0, rtdata_len =3D 0; + struct feature_query *ft_query; + + ft_query =3D (struct feature_query *)rt_data; + if (le32_to_cpu(ft_query->head_pattern) !=3D FEATURE_QUERY_PATTERN || + le32_to_cpu(ft_query->tail_pattern) !=3D FEATURE_QUERY_PATTERN) { + dev_err(mdev->dev, + "Failed to match ft_query pattern: head=3D0x%x,tail=3D0x%x\n", + le32_to_cpu(ft_query->head_pattern), le32_to_cpu(ft_query->tail_pattern= )); + ret =3D -EPROTO; + goto hs_err; + } + + rtft_entry =3D feature_data; + for (ft_id =3D 0; ft_id < FEATURE_CNT && rtdata_len < RTFT_DATA_SIZE; ft_= id++) { + rtft_entry->feature_id =3D ft_id; + rtft_entry->data_len =3D 0; + + switch (FIELD_GET(FEATURE_TYPE, ft_query->ft_set[ft_id].feature)) { + case RTFT_TYPE_NOT_EXIST: + fallthrough; + case RTFT_TYPE_NOT_SUPPORT: + fallthrough; + case RTFT_TYPE_MUST_SUPPORT: + rtft_entry->support_info =3D ft_query->ft_set[ft_id]; + break; + case RTFT_TYPE_OPTIONAL_SUPPORT: + fallthrough; + case RTFT_TYPE_SUPPORT_BACKWARD_COMPAT: + rtft_entry->support_info.feature =3D FEATURE_TYPE_NOT; + rtft_entry->support_info.feature |=3D FEATURE_VER_0; + break; + } + + rtdata_len +=3D sizeof(*rtft_entry) + le32_to_cpu(rtft_entry->data_len); + rtft_entry =3D (struct runtime_feature_entry *)(feature_data + rtdata_le= n); + } + *len =3D rtdata_len; + return 0; +hs_err: + *len =3D 0; + return ret; +} + +static int mtk_fsm_send_hs3_msg(struct fsm_hs_info *hs_info) +{ + struct mtk_md_fsm *fsm =3D container_of(hs_info, struct mtk_md_fsm, hs_in= fo[hs_info->id]); + unsigned int data_len, msg_size =3D 0; + struct ctrl_msg_header *ctrl_msg_h; + struct sk_buff *skb; + int ret; + + skb =3D __dev_alloc_skb(RTFT_DATA_SIZE, GFP_KERNEL); + if (!skb) { + ret =3D -ENOMEM; + goto hs_err; + } + + msg_size +=3D sizeof(*ctrl_msg_h); + ctrl_msg_h =3D (struct ctrl_msg_header *)skb->data; + ctrl_msg_h->id =3D cpu_to_le32(CTRL_MSG_HS3); + ctrl_msg_h->ex_msg =3D 0; + ret =3D mtk_fsm_append_rtft_entries(fsm->mdev, + skb->data + sizeof(*ctrl_msg_h), + &data_len, hs_info); + if (ret) + goto hs_err; + + ctrl_msg_h->data_len =3D cpu_to_le32(data_len); + msg_size +=3D data_len; + skb_put(skb, msg_size); + + ret =3D mtk_port_internal_write(hs_info->ctrl_port, skb); + if (ret > 0) + return 0; +hs_err: + dev_err(fsm->mdev->dev, "Failed to send handshake3 message:ret=3D%d\n", r= et); + return ret; +} + +static int mtk_fsm_sap_ctrl_msg_handler(void *__fsm, struct sk_buff *skb) +{ + struct ctrl_msg_header *ctrl_msg_h; + struct mtk_md_fsm *fsm =3D __fsm; + struct fsm_hs_info *hs_info; + + ctrl_msg_h =3D (struct ctrl_msg_header *)skb->data; + skb_pull(skb, sizeof(*ctrl_msg_h)); + + hs_info =3D &fsm->hs_info[HS_ID_SAP]; + if (le32_to_cpu(ctrl_msg_h->id) !=3D CTRL_MSG_HS2) + return -EPROTO; + + hs_info->rt_data =3D skb; + hs_info->rt_data_len =3D skb->len; + mtk_fsm_evt_submit(fsm->mdev, FSM_EVT_STARTUP, + hs_info->fsm_flag_hs2, hs_info, sizeof(*hs_info), 0); + + return 0; +} + +static int mtk_fsm_md_ctrl_msg_handler(void *__fsm, struct sk_buff *skb) +{ + struct ctrl_msg_header *ctrl_msg_h; + struct mtk_md_fsm *fsm =3D __fsm; + struct fsm_hs_info *hs_info; + bool need_free_data =3D true; + int ret =3D 0; + + ctrl_msg_h =3D (struct ctrl_msg_header *)skb->data; + hs_info =3D &fsm->hs_info[HS_ID_MD]; + switch (le32_to_cpu(ctrl_msg_h->id)) { + case CTRL_MSG_HS2: + need_free_data =3D false; + skb_pull(skb, sizeof(*ctrl_msg_h)); + hs_info->rt_data =3D skb; + hs_info->rt_data_len =3D skb->len; + mtk_fsm_evt_submit(fsm->mdev, FSM_EVT_STARTUP, + hs_info->fsm_flag_hs2, hs_info, sizeof(*hs_info), 0); + break; + default: + dev_err(fsm->mdev->dev, "Invalid control message id\n"); + } + + if (need_free_data) + dev_kfree_skb(skb); + + return ret; +} + +static int (*ctrl_msg_handler[HS_ID_MAX])(void *__fsm, struct sk_buff *skb= ) =3D { + [HS_ID_MD] =3D mtk_fsm_md_ctrl_msg_handler, + [HS_ID_SAP] =3D mtk_fsm_sap_ctrl_msg_handler, +}; + +static void mtk_fsm_linux_evt_handler(struct mtk_md_dev *mdev, + u32 dev_state, struct mtk_md_fsm *fsm) +{ + u32 dev_cfg =3D dev_state >> DEVICE_CFG_SHIFT & DEVICE_CFG_REGION_MASK; + int hs_id; + + if (dev_cfg =3D=3D DEV_CFG_MD_ONLY) + fsm->hs_done_flag =3D FSM_F_MD_HS_START | FSM_F_MD_HS2_DONE; + else + fsm->hs_done_flag =3D FSM_HS_START_MASK | FSM_HS2_DONE_MASK; + + for (hs_id =3D 0; hs_id < HS_ID_MAX; hs_id++) + mtk_hw_unmask_ext_evt(mdev, fsm->hs_info[hs_id].mhccif_ch); +} + +static int mtk_fsm_early_bootup_handler(u32 status, void *__fsm) +{ + struct mtk_md_fsm *fsm =3D __fsm; + struct mtk_md_dev *mdev; + u32 dev_state, dev_stage; + + mdev =3D fsm->mdev; + mtk_hw_mask_ext_evt(mdev, status); + mtk_hw_clear_ext_evt(mdev, status); + + dev_state =3D mtk_hw_get_dev_state(mdev); + dev_stage =3D dev_state & REGION_BITMASK; + if (dev_stage >=3D DEV_STAGE_MAX) { + dev_err(mdev->dev, "Invalid dev state 0x%x\n", dev_state); + return -ENXIO; + } + + if (dev_state =3D=3D fsm->last_dev_state) + goto exit; + dev_info(mdev->dev, "Device stage change 0x%x->0x%x\n", fsm->last_dev_sta= te, dev_state); + fsm->last_dev_state =3D dev_state; + + if (dev_stage =3D=3D DEV_STAGE_LINUX) + mtk_fsm_linux_evt_handler(mdev, dev_state, fsm); + +exit: + if (dev_stage !=3D DEV_STAGE_LINUX) + mtk_hw_unmask_ext_evt(mdev, EXT_EVT_D2H_BOOT_FLOW_SYNC); + + return 0; +} + +static int mtk_fsm_ctrl_ch_start(struct mtk_md_fsm *fsm, struct fsm_hs_inf= o *hs_info) +{ + hs_info->ctrl_port =3D mtk_port_internal_open(fsm->mdev, hs_info->port_na= me, 0); + if (!hs_info->ctrl_port) { + dev_err(fsm->mdev->dev, "Failed to open ctrl port(%s)\n", + hs_info->port_name); + return -ENODEV; + } + mtk_port_internal_recv_register(hs_info->ctrl_port, + ctrl_msg_handler[hs_info->id], fsm); + + return 0; +} + +static void mtk_fsm_ctrl_ch_stop(struct mtk_md_fsm *fsm) +{ + struct fsm_hs_info *hs_info; + int hs_id; + + for (hs_id =3D 0; hs_id < HS_ID_MAX; hs_id++) { + hs_info =3D &fsm->hs_info[hs_id]; + mtk_port_internal_close(hs_info->ctrl_port); + } +} + +static void mtk_fsm_switch_state(struct mtk_md_fsm *fsm, + enum mtk_fsm_state to_state, struct mtk_fsm_evt *event) +{ + char uevent_info[MTK_UEVENT_INFO_LEN]; + struct mtk_fsm_notifier *nt; + struct mtk_fsm_param param; + + param.from =3D fsm->state; + param.to =3D to_state; + param.evt_id =3D event->id; + param.fsm_flag =3D event->fsm_flag; + + list_for_each_entry(nt, &fsm->pre_notifiers, entry) + nt->cb(¶m, nt->data); + + fsm->state =3D to_state; + fsm->fsm_flag |=3D event->fsm_flag; + dev_info(fsm->mdev->dev, "FSM transited to state=3D%d, fsm_flag=3D0x%x\n", + to_state, fsm->fsm_flag); + + snprintf(uevent_info, MTK_UEVENT_INFO_LEN, + "state=3D%d, fsm_flag=3D0x%x", to_state, fsm->fsm_flag); + mtk_uevent_notify(fsm->mdev->dev, MTK_UEVENT_FSM, uevent_info); + + list_for_each_entry(nt, &fsm->post_notifiers, entry) + nt->cb(¶m, nt->data); +} + +static int mtk_fsm_startup_act(struct mtk_md_fsm *fsm, struct mtk_fsm_evt = *event) +{ + enum mtk_fsm_state to_state =3D FSM_STATE_BOOTUP; + struct fsm_hs_info *hs_info =3D event->data; + struct mtk_md_dev *mdev =3D fsm->mdev; + int ret =3D 0; + + if (fsm->state !=3D FSM_STATE_ON && fsm->state !=3D FSM_STATE_BOOTUP) { + ret =3D -EPROTO; + goto hs_err; + } + + if (event->fsm_flag & FSM_HS_START_MASK) { + mtk_fsm_switch_state(fsm, to_state, event); + + ret =3D mtk_fsm_ctrl_ch_start(fsm, hs_info); + if (!ret) + ret =3D mtk_fsm_send_hs1_msg(hs_info); + if (ret) + goto hs_err; + } else if (event->fsm_flag & FSM_HS2_DONE_MASK) { + ret =3D mtk_fsm_parse_hs2_msg(hs_info); + if (!ret) { + mtk_fsm_switch_state(fsm, to_state, event); + ret =3D mtk_fsm_send_hs3_msg(hs_info); + } + dev_kfree_skb(hs_info->rt_data); + if (ret) + goto hs_err; + } + + if (((fsm->fsm_flag | event->fsm_flag) & fsm->hs_done_flag) =3D=3D fsm->h= s_done_flag) { + to_state =3D FSM_STATE_READY; + mtk_fsm_switch_state(fsm, to_state, event); + } + + return 0; +hs_err: + dev_err(mdev->dev, "Failed to handshake with device %d:0x%x", fsm->state,= fsm->fsm_flag); + return ret; +} + +static int mtk_fsm_dev_add_act(struct mtk_md_fsm *fsm, struct mtk_fsm_evt = *event) +{ + struct mtk_md_dev *mdev =3D fsm->mdev; + + if (fsm->state !=3D FSM_STATE_OFF && fsm->state !=3D FSM_STATE_INVALID) { + dev_err(mdev->dev, "Unable to handle the event in the state %d\n", fsm->= state); + return -EPROTO; + } + + mtk_fsm_switch_state(fsm, FSM_STATE_ON, event); + mtk_hw_unmask_ext_evt(mdev, EXT_EVT_D2H_BOOT_FLOW_SYNC); + + return 0; +} + +static void mtk_fsm_evt_release(struct kref *kref) +{ + struct mtk_fsm_evt *event =3D container_of(kref, struct mtk_fsm_evt, kref= ); + + devm_kfree(event->mdev->dev, event); +} + +static void mtk_fsm_evt_put(struct mtk_fsm_evt *event) +{ + kref_put(&event->kref, mtk_fsm_evt_release); +} + +static void mtk_fsm_evt_finish(struct mtk_md_fsm *fsm, + struct mtk_fsm_evt *event, int retval) +{ + if (event->mode & EVT_MODE_BLOCKING) { + event->status =3D retval; + wake_up_interruptible(&fsm->evt_waitq); + } + mtk_fsm_evt_put(event); +} + +static void mtk_fsm_evt_cleanup(struct mtk_md_fsm *fsm, struct list_head *= evtq) +{ + struct mtk_fsm_evt *event, *tmp; + + list_for_each_entry_safe(event, tmp, evtq, entry) { + mtk_fsm_evt_finish(fsm, event, FSM_EVT_RET_FAIL); + list_del(&event->entry); + } +} + +static int mtk_fsm_enter_off_state(struct mtk_md_fsm *fsm, struct mtk_fsm_= evt *event) +{ + struct mtk_md_dev *mdev =3D fsm->mdev; + + if (fsm->state =3D=3D FSM_STATE_OFF || fsm->state =3D=3D FSM_STATE_INVALI= D) { + dev_err(mdev->dev, "Unable to handle the event in the state %d\n", fsm->= state); + return -EPROTO; + } + + mtk_hw_mask_ext_evt(mdev, EXT_EVT_D2H_BOOT_FLOW_SYNC); + mtk_fsm_ctrl_ch_stop(fsm); + mtk_fsm_switch_state(fsm, FSM_STATE_OFF, event); + + return 0; +} + +static int mtk_fsm_dev_rm_act(struct mtk_md_fsm *fsm, struct mtk_fsm_evt *= event) +{ + unsigned long flags; + + spin_lock_irqsave(&fsm->evtq_lock, flags); + set_bit(EVT_TF_GATECLOSED, &fsm->t_flag); + mtk_fsm_evt_cleanup(fsm, &fsm->evtq); + spin_unlock_irqrestore(&fsm->evtq_lock, flags); + + return mtk_fsm_enter_off_state(fsm, event); +} + +static int mtk_fsm_hs1_handler(u32 status, void *__hs_info) +{ + struct fsm_hs_info *hs_info =3D __hs_info; + struct mtk_md_dev *mdev; + struct mtk_md_fsm *fsm; + + fsm =3D container_of(hs_info, struct mtk_md_fsm, hs_info[hs_info->id]); + mdev =3D fsm->mdev; + mtk_fsm_evt_submit(mdev, FSM_EVT_STARTUP, + hs_info->fsm_flag_hs1, hs_info, sizeof(*hs_info), 0); + mtk_hw_mask_ext_evt(mdev, hs_info->mhccif_ch); + mtk_hw_clear_ext_evt(mdev, hs_info->mhccif_ch); + + return 0; +} + +static void mtk_fsm_hs_info_init(struct mtk_md_fsm *fsm) +{ + struct mtk_md_dev *mdev =3D fsm->mdev; + struct fsm_hs_info *hs_info; + int hs_id; + + for (hs_id =3D 0; hs_id < HS_ID_MAX; hs_id++) { + hs_info =3D &fsm->hs_info[hs_id]; + hs_info->id =3D hs_id; + switch (hs_id) { + case HS_ID_MD: + snprintf(hs_info->port_name, PORT_NAME_LEN, "MDCTRL"); + hs_info->mhccif_ch =3D EXT_EVT_D2H_ASYNC_HS_NOTIFY_MD; + hs_info->fsm_flag_hs1 =3D FSM_F_MD_HS_START; + hs_info->fsm_flag_hs2 =3D FSM_F_MD_HS2_DONE; + hs_info->query_ft_set[RTFT_ID_MD_PORT_ENUM].feature =3D FEATURE_TYPE_MU= ST; + hs_info->query_ft_set[RTFT_ID_MD_PORT_ENUM].feature |=3D FEATURE_VER_0; + hs_info->query_ft_set[RTFT_ID_MD_PORT_CFG].feature =3D FEATURE_TYPE_OPT= IONAL; + hs_info->query_ft_set[RTFT_ID_MD_PORT_CFG].feature |=3D FEATURE_VER_0; + break; + case HS_ID_SAP: + snprintf(hs_info->port_name, PORT_NAME_LEN, "SAPCTRL"); + hs_info->mhccif_ch =3D EXT_EVT_D2H_ASYNC_HS_NOTIFY_SAP; + hs_info->fsm_flag_hs1 =3D FSM_F_SAP_HS_START; + hs_info->fsm_flag_hs2 =3D FSM_F_SAP_HS2_DONE; + hs_info->query_ft_set[RTFT_ID_SAP_PORT_ENUM].feature =3D FEATURE_TYPE_M= UST; + hs_info->query_ft_set[RTFT_ID_SAP_PORT_ENUM].feature |=3D FEATURE_VER_0; + break; + } + mtk_hw_register_ext_evt(mdev, hs_info->mhccif_ch, + mtk_fsm_hs1_handler, hs_info); + } +} + +static void mtk_fsm_hs_info_exit(struct mtk_md_fsm *fsm) +{ + struct mtk_md_dev *mdev =3D fsm->mdev; + struct fsm_hs_info *hs_info; + int hs_id; + + for (hs_id =3D 0; hs_id < HS_ID_MAX; hs_id++) { + hs_info =3D &fsm->hs_info[hs_id]; + mtk_hw_unregister_ext_evt(mdev, hs_info->mhccif_ch); + } +} + +static int (*evts_act_tbl[FSM_EVT_MAX])(struct mtk_md_fsm *__fsm, struct m= tk_fsm_evt *event) =3D { + [FSM_EVT_STARTUP] =3D mtk_fsm_startup_act, + [FSM_EVT_DEV_RM] =3D mtk_fsm_dev_rm_act, + [FSM_EVT_DEV_ADD] =3D mtk_fsm_dev_add_act, +}; + +int mtk_fsm_start(struct mtk_md_dev *mdev) +{ + struct mtk_md_fsm *fsm =3D mdev->fsm; + + wake_up_process(fsm->fsm_handler); + return 0; +} + +static void mkt_fsm_notifier_cleanup(struct mtk_md_dev *mdev, struct list_= head *ntq) +{ + struct mtk_fsm_notifier *nt, *tmp; + + list_for_each_entry_safe(nt, tmp, ntq, entry) { + list_del(&nt->entry); + devm_kfree(mdev->dev, nt); + } +} + +static void mtk_fsm_notifier_insert(struct mtk_fsm_notifier *notifier, str= uct list_head *head) +{ + struct mtk_fsm_notifier *nt; + + list_for_each_entry(nt, head, entry) { + if (notifier->prio > nt->prio) { + list_add(¬ifier->entry, nt->entry.prev); + return; + } + } + list_add_tail(¬ifier->entry, head); +} + +int mtk_fsm_notifier_register(struct mtk_md_dev *mdev, enum mtk_user_id id, + void (*cb)(struct mtk_fsm_param *, void *data), + void *data, enum mtk_fsm_prio prio, bool is_pre) +{ + struct mtk_md_fsm *fsm =3D mdev->fsm; + struct mtk_fsm_notifier *notifier; + + if (!fsm) + return -EINVAL; + + notifier =3D devm_kzalloc(mdev->dev, sizeof(*notifier), GFP_KERNEL); + if (!notifier) + return -ENOMEM; + + INIT_LIST_HEAD(¬ifier->entry); + notifier->id =3D id; + notifier->cb =3D cb; + notifier->data =3D data; + notifier->prio =3D prio; + + if (is_pre) + mtk_fsm_notifier_insert(notifier, &fsm->pre_notifiers); + else + mtk_fsm_notifier_insert(notifier, &fsm->post_notifiers); + + return 0; +} + +int mtk_fsm_notifier_unregister(struct mtk_md_dev *mdev, enum mtk_user_id = id) +{ + struct mtk_md_fsm *fsm =3D mdev->fsm; + struct mtk_fsm_notifier *nt, *tmp; + + if (!fsm) + return -EINVAL; + + list_for_each_entry_safe(nt, tmp, &fsm->pre_notifiers, entry) { + if (nt->id =3D=3D id) { + list_del(&nt->entry); + devm_kfree(mdev->dev, nt); + break; + } + } + list_for_each_entry_safe(nt, tmp, &fsm->post_notifiers, entry) { + if (nt->id =3D=3D id) { + list_del(&nt->entry); + devm_kfree(mdev->dev, nt); + break; + } + } + return 0; +} + +int mtk_fsm_evt_submit(struct mtk_md_dev *mdev, + enum mtk_fsm_evt_id id, enum mtk_fsm_flag flag, + void *data, unsigned int len, unsigned char mode) +{ + struct mtk_md_fsm *fsm =3D mdev->fsm; + struct mtk_fsm_evt *event; + unsigned long flags; + int ret =3D 0; + + event =3D devm_kzalloc(mdev->dev, sizeof(*event), + (in_irq() || in_softirq() || irqs_disabled()) ? + GFP_ATOMIC : GFP_KERNEL); + if (!event) + return FSM_EVT_RET_FAIL; + + kref_init(&event->kref); + event->mdev =3D mdev; + event->id =3D id; + event->fsm_flag =3D flag; + event->status =3D FSM_EVT_RET_ONGOING; + event->data =3D data; + event->len =3D len; + event->mode =3D mode; + dev_info(mdev->dev, "Event%d(with mode 0x%x) is appended\n", + event->id, event->mode); + + spin_lock_irqsave(&fsm->evtq_lock, flags); + if (test_bit(EVT_TF_GATECLOSED, &fsm->t_flag)) { + spin_unlock_irqrestore(&fsm->evtq_lock, flags); + mtk_fsm_evt_put(event); + dev_err(mdev->dev, "Failed to add event, fsm dev has been removed!\n"); + return FSM_EVT_RET_FAIL; + } + + kref_get(&event->kref); + if (mode & EVT_MODE_TOHEAD) + list_add(&event->entry, &fsm->evtq); + else + list_add_tail(&event->entry, &fsm->evtq); + spin_unlock_irqrestore(&fsm->evtq_lock, flags); + + wake_up_process(fsm->fsm_handler); + if (mode & EVT_MODE_BLOCKING) { + wait_event_interruptible(fsm->evt_waitq, (event->status !=3D 0)); + ret =3D event->status; + } + mtk_fsm_evt_put(event); + + return ret; +} + +static int mtk_fsm_evt_handler(void *__fsm) +{ + struct mtk_md_fsm *fsm =3D __fsm; + struct mtk_fsm_evt *event; + unsigned long flags; + +wake_up: + while (!kthread_should_stop() && !list_empty(&fsm->evtq)) { + spin_lock_irqsave(&fsm->evtq_lock, flags); + event =3D list_first_entry(&fsm->evtq, struct mtk_fsm_evt, entry); + list_del(&event->entry); + spin_unlock_irqrestore(&fsm->evtq_lock, flags); + + dev_info(fsm->mdev->dev, "Event%d(0x%x) is under handling\n", + event->id, event->fsm_flag); + if (event->id < FSM_EVT_MAX && evts_act_tbl[event->id](fsm, event) !=3D = 0) + mtk_fsm_evt_finish(fsm, event, FSM_EVT_RET_FAIL); + else + mtk_fsm_evt_finish(fsm, event, FSM_EVT_RET_DONE); + } + + if (kthread_should_stop()) + return 0; + + set_current_state(TASK_INTERRUPTIBLE); + schedule(); + + if (fatal_signal_pending(current)) { + mtk_fsm_evt_cleanup(fsm, &fsm->evtq); + return -ERESTARTSYS; + } + goto wake_up; +} + +int mtk_fsm_init(struct mtk_md_dev *mdev) +{ + struct mtk_md_fsm *fsm; + int ret; + + fsm =3D devm_kzalloc(mdev->dev, sizeof(*fsm), GFP_KERNEL); + if (!fsm) + return -ENOMEM; + + fsm->fsm_handler =3D kthread_create(mtk_fsm_evt_handler, fsm, "fsm_evt_th= read%d_%s", + mdev->hw_ver, mdev->dev_str); + if (IS_ERR(fsm->fsm_handler)) { + ret =3D PTR_ERR(fsm->fsm_handler); + goto exit; + } + + fsm->mdev =3D mdev; + fsm->state =3D FSM_STATE_INVALID; + fsm->fsm_flag =3D FSM_F_DFLT; + + INIT_LIST_HEAD(&fsm->evtq); + spin_lock_init(&fsm->evtq_lock); + init_waitqueue_head(&fsm->evt_waitq); + + INIT_LIST_HEAD(&fsm->pre_notifiers); + INIT_LIST_HEAD(&fsm->post_notifiers); + + mtk_fsm_hs_info_init(fsm); + mtk_hw_register_ext_evt(mdev, EXT_EVT_D2H_BOOT_FLOW_SYNC, + mtk_fsm_early_bootup_handler, fsm); + mdev->fsm =3D fsm; + return 0; +exit: + devm_kfree(mdev->dev, fsm); + return ret; +} + +int mtk_fsm_exit(struct mtk_md_dev *mdev) +{ + struct mtk_md_fsm *fsm =3D mdev->fsm; + unsigned long flags; + + kthread_stop(fsm->fsm_handler); + + spin_lock_irqsave(&fsm->evtq_lock, flags); + if (WARN_ON(!list_empty(&fsm->evtq))) + mtk_fsm_evt_cleanup(fsm, &fsm->evtq); + spin_unlock_irqrestore(&fsm->evtq_lock, flags); + + mkt_fsm_notifier_cleanup(mdev, &fsm->pre_notifiers); + mkt_fsm_notifier_cleanup(mdev, &fsm->post_notifiers); + + mtk_hw_unregister_ext_evt(mdev, EXT_EVT_D2H_BOOT_FLOW_SYNC); + mtk_fsm_hs_info_exit(fsm); + + devm_kfree(mdev->dev, fsm); + return 0; +} diff --git a/drivers/net/wwan/mediatek/mtk_fsm.h b/drivers/net/wwan/mediate= k/mtk_fsm.h new file mode 100644 index 000000000000..35509a1738dd --- /dev/null +++ b/drivers/net/wwan/mediatek/mtk_fsm.h @@ -0,0 +1,145 @@ +/* SPDX-License-Identifier: BSD-3-Clause-Clear + * + * Copyright (c) 2022, MediaTek Inc. + */ + +#ifndef __MTK_FSM_H__ +#define __MTK_FSM_H__ + +#include "mtk_dev.h" + +#define FEATURE_CNT (64) +#define FEATURE_QUERY_PATTERN (0x49434343) + +#define FEATURE_TYPE GENMASK(3, 0) +#define FEATURE_VER GENMASK(7, 4) + +#define FEATURE_TYPE_NOT FIELD_PREP(FEATURE_TYPE, RTFT_TYPE_NOT_SUPPORT) +#define FEATURE_TYPE_MUST FIELD_PREP(FEATURE_TYPE, RTFT_TYPE_MUST_SUPPORT) +#define FEATURE_TYPE_OPTIONAL FIELD_PREP(FEATURE_TYPE, RTFT_TYPE_OPTIONAL_= SUPPORT) +#define FEATURE_VER_0 FIELD_PREP(FEATURE_VER, 0) + +#define EVT_MODE_BLOCKING (0x01) +#define EVT_MODE_TOHEAD (0x02) + +#define FSM_EVT_RET_FAIL (-1) +#define FSM_EVT_RET_ONGOING (0) +#define FSM_EVT_RET_DONE (1) + +enum mtk_fsm_flag { + FSM_F_DFLT =3D 0, + FSM_F_SAP_HS_START =3D BIT(0), + FSM_F_SAP_HS2_DONE =3D BIT(1), + FSM_F_MD_HS_START =3D BIT(2), + FSM_F_MD_HS2_DONE =3D BIT(3), +}; + +enum mtk_fsm_state { + FSM_STATE_INVALID =3D 0, + FSM_STATE_OFF, + FSM_STATE_ON, + FSM_STATE_BOOTUP, + FSM_STATE_READY, +}; + +enum mtk_fsm_evt_id { + FSM_EVT_STARTUP =3D 0, + FSM_EVT_DEV_RM, + FSM_EVT_DEV_ADD, + FSM_EVT_MAX +}; + +enum mtk_fsm_prio { + FSM_PRIO_0 =3D 0, + FSM_PRIO_1 =3D 1, + FSM_PRIO_MAX +}; + +struct mtk_fsm_param { + enum mtk_fsm_state from; + enum mtk_fsm_state to; + enum mtk_fsm_evt_id evt_id; + enum mtk_fsm_flag fsm_flag; +}; + +#define PORT_NAME_LEN 20 + +enum handshake_info_id { + HS_ID_MD =3D 0, + HS_ID_SAP, + HS_ID_MAX +}; + +struct runtime_feature_info { + u8 feature; +}; + +struct fsm_hs_info { + unsigned char id; + void *ctrl_port; + char port_name[PORT_NAME_LEN]; + unsigned int mhccif_ch; + unsigned int fsm_flag_hs1; + unsigned int fsm_flag_hs2; + /* the feature that the device should support */ + struct runtime_feature_info query_ft_set[FEATURE_CNT]; + /* runtime data from device need to be parsed by host */ + void *rt_data; + unsigned int rt_data_len; +}; + +struct mtk_md_fsm { + struct mtk_md_dev *mdev; + struct task_struct *fsm_handler; + struct fsm_hs_info hs_info[HS_ID_MAX]; + unsigned int hs_done_flag; + unsigned long t_flag; + u32 last_dev_state; + enum mtk_fsm_state state; + unsigned int fsm_flag; + struct list_head evtq; + /* protect evtq */ + spinlock_t evtq_lock; + /* waitq for fsm blocking submit */ + wait_queue_head_t evt_waitq; + struct list_head pre_notifiers; + struct list_head post_notifiers; +}; + +struct mtk_fsm_evt { + struct list_head entry; + struct kref kref; + struct mtk_md_dev *mdev; + enum mtk_fsm_evt_id id; + unsigned int fsm_flag; + /* event handling status + * -1: fail, + * 0: on-going, + * 1: successfully + */ + int status; + unsigned char mode; + unsigned int len; + void *data; +}; + +struct mtk_fsm_notifier { + struct list_head entry; + enum mtk_user_id id; + void (*cb)(struct mtk_fsm_param *param, void *data); + void *data; + enum mtk_fsm_prio prio; +}; + +int mtk_fsm_init(struct mtk_md_dev *mdev); +int mtk_fsm_exit(struct mtk_md_dev *mdev); +int mtk_fsm_start(struct mtk_md_dev *mdev); +int mtk_fsm_notifier_register(struct mtk_md_dev *mdev, enum mtk_user_id id, + void (*cb)(struct mtk_fsm_param *, void *data), + void *data, enum mtk_fsm_prio prio, bool is_pre); +int mtk_fsm_notifier_unregister(struct mtk_md_dev *mdev, enum mtk_user_id = id); +int mtk_fsm_evt_submit(struct mtk_md_dev *mdev, + enum mtk_fsm_evt_id id, enum mtk_fsm_flag flag, + void *data, unsigned int len, unsigned char mode); + +#endif /* __MTK_FSM_H__ */ diff --git a/drivers/net/wwan/mediatek/mtk_port.c b/drivers/net/wwan/mediat= ek/mtk_port.c index 12097a279aa0..208817225645 100644 --- a/drivers/net/wwan/mediatek/mtk_port.c +++ b/drivers/net/wwan/mediatek/mtk_port.c @@ -14,6 +14,9 @@ #include "mtk_port.h" #include "mtk_port_io.h" =20 +#define MTK_PORT_ENUM_VER (0) +#define MTK_PORT_ENUM_HEAD_PATTERN (0x5a5a5a5a) +#define MTK_PORT_ENUM_TAIL_PATTERN (0xa5a5a5a5) #define MTK_DFLT_TRB_TIMEOUT (5 * HZ) #define MTK_DFLT_TRB_STATUS (0x1) #define MTK_CHECK_RX_SEQ_MASK (0x7fff) @@ -423,8 +426,10 @@ static int mtk_port_open_trb_complete(struct sk_buff *= skb) port->tx_mtu =3D port_mngr->vq_info[trb->vqno].tx_mtu; port->rx_mtu =3D port_mngr->vq_info[trb->vqno].rx_mtu; =20 - port->tx_mtu -=3D MTK_CCCI_H_ELEN; - port->rx_mtu -=3D MTK_CCCI_H_ELEN; + if (!(port->info.flags & PORT_F_RAW_DATA)) { + port->tx_mtu -=3D MTK_CCCI_H_ELEN; + port->rx_mtu -=3D MTK_CCCI_H_ELEN; + } =20 out: wake_up_interruptible_all(&port->trb_wq); @@ -588,30 +593,32 @@ static int mtk_port_rx_dispatch(struct sk_buff *skb, = int len, void *priv) skb_reset_tail_pointer(skb); skb_put(skb, len); =20 - ccci_h =3D mtk_port_strip_header(skb); - if (unlikely(!ccci_h)) { - dev_warn(port_mngr->ctrl_blk->mdev->dev, - "Unsupported: skb length(%d) is less than ccci header\n", - skb->len); - goto drop_data; - } + if (!(port->info.flags & PORT_F_RAW_DATA)) { + ccci_h =3D mtk_port_strip_header(skb); + if (unlikely(!ccci_h)) { + dev_warn(port_mngr->ctrl_blk->mdev->dev, + "Unsupported: skb length(%d) is less than ccci header\n", + skb->len); + goto drop_data; + } =20 - dev_dbg(port_mngr->ctrl_blk->mdev->dev, - "RX header:%08x %08x\n", ccci_h->packet_len, ccci_h->status); + dev_dbg(port_mngr->ctrl_blk->mdev->dev, + "RX header:%08x %08x\n", ccci_h->packet_len, ccci_h->status); =20 - channel =3D FIELD_GET(MTK_HDR_FLD_CHN, le32_to_cpu(ccci_h->status)); - port =3D mtk_port_search_by_id(port_mngr, channel); - if (unlikely(!port)) { - dev_warn(port_mngr->ctrl_blk->mdev->dev, - "Failed to find port by channel:%d\n", channel); - goto drop_data; - } + channel =3D FIELD_GET(MTK_HDR_FLD_CHN, le32_to_cpu(ccci_h->status)); + port =3D mtk_port_search_by_id(port_mngr, channel); + if (unlikely(!port)) { + dev_warn(port_mngr->ctrl_blk->mdev->dev, + "Failed to find port by channel:%d\n", channel); + goto drop_data; + } =20 - ret =3D mtk_port_check_rx_seq(port, ccci_h); - if (unlikely(ret)) - goto drop_data; + ret =3D mtk_port_check_rx_seq(port, ccci_h); + if (unlikely(ret)) + goto drop_data; =20 - port->rx_seq =3D FIELD_GET(MTK_HDR_FLD_SEQ, le32_to_cpu(ccci_h->status)); + port->rx_seq =3D FIELD_GET(MTK_HDR_FLD_SEQ, le32_to_cpu(ccci_h->status)); + } =20 ret =3D ports_ops[port->info.type]->recv(port, skb); =20 @@ -623,6 +630,43 @@ static int mtk_port_rx_dispatch(struct sk_buff *skb, i= nt len, void *priv) return ret; } =20 +static int mtk_port_enable(struct mtk_port_mngr *port_mngr) +{ + int tbl_type =3D PORT_TBL_SAP; + struct radix_tree_iter iter; + struct mtk_port *port; + void __rcu **slot; + + do { + radix_tree_for_each_slot(slot, &port_mngr->port_tbl[tbl_type], &iter, 0)= { + MTK_PORT_SEARCH_FROM_RADIX_TREE(port, slot); + MTK_PORT_INTERNAL_NODE_CHECK(port, slot, iter); + if (port->enable) + ports_ops[port->info.type]->enable(port); + } + tbl_type++; + } while (tbl_type < PORT_TBL_MAX); + return 0; +} + +static void mtk_port_disable(struct mtk_port_mngr *port_mngr) +{ + int tbl_type =3D PORT_TBL_SAP; + struct radix_tree_iter iter; + struct mtk_port *port; + void __rcu **slot; + + do { + radix_tree_for_each_slot(slot, &port_mngr->port_tbl[tbl_type], &iter, 0)= { + MTK_PORT_SEARCH_FROM_RADIX_TREE(port, slot); + MTK_PORT_INTERNAL_NODE_CHECK(port, slot, iter); + port->enable =3D false; + ports_ops[port->info.type]->disable(port); + } + tbl_type++; + } while (tbl_type < PORT_TBL_MAX); +} + int mtk_port_add_header(struct sk_buff *skb) { struct mtk_ccci_header *ccci_h; @@ -669,6 +713,55 @@ struct mtk_ccci_header *mtk_port_strip_header(struct s= k_buff *skb) return ccci_h; } =20 +int mtk_port_status_update(struct mtk_md_dev *mdev, void *data) +{ + struct mtk_port_enum_msg *msg =3D data; + struct mtk_port_info *port_info; + struct mtk_port_mngr *port_mngr; + struct mtk_ctrl_blk *ctrl_blk; + struct mtk_port *port; + int port_id; + int ret =3D 0; + u16 ch_id; + + if (unlikely(!mdev || !msg)) { + ret =3D -EINVAL; + goto end; + } + + ctrl_blk =3D mdev->ctrl_blk; + port_mngr =3D ctrl_blk->port_mngr; + if (le16_to_cpu(msg->version) !=3D MTK_PORT_ENUM_VER) { + ret =3D -EPROTO; + goto end; + } + + if (le32_to_cpu(msg->head_pattern) !=3D MTK_PORT_ENUM_HEAD_PATTERN || + le32_to_cpu(msg->tail_pattern) !=3D MTK_PORT_ENUM_TAIL_PATTERN) { + ret =3D -EPROTO; + goto end; + } + + for (port_id =3D 0; port_id < le16_to_cpu(msg->port_cnt); port_id++) { + port_info =3D (struct mtk_port_info *)(msg->data + + (sizeof(*port_info) * port_id)); + if (!port_info) { + dev_err(mdev->dev, "Invalid port info, the index %d\n", port_id); + ret =3D -EINVAL; + goto end; + } + ch_id =3D FIELD_GET(MTK_INFO_FLD_CHID, le16_to_cpu(port_info->channel)); + port =3D mtk_port_search_by_id(port_mngr, ch_id); + if (!port) { + dev_err(mdev->dev, "Failed to find the port 0x%x\n", ch_id); + continue; + } + port->enable =3D FIELD_GET(MTK_INFO_FLD_EN, le16_to_cpu(port_info->chann= el)); + } +end: + return ret; +} + int mtk_port_mngr_vq_status_check(struct sk_buff *skb) { struct trb *trb =3D (struct trb *)skb->cb; @@ -791,6 +884,58 @@ int mtk_port_vq_disable(struct mtk_port *port) return ret; } =20 +void mtk_port_mngr_fsm_state_handler(struct mtk_fsm_param *fsm_param, void= *arg) +{ + struct mtk_port_mngr *port_mngr; + struct mtk_port *port; + int flag; + + if (!fsm_param || !arg) { + pr_err("[TMI] Invalid input fsm_param or arg\n"); + return; + } + + port_mngr =3D arg; + flag =3D fsm_param->fsm_flag; + + dev_info(port_mngr->ctrl_blk->mdev->dev, "Fsm state %d & fsm flag 0x%x\n", + fsm_param->to, flag); + + switch (fsm_param->to) { + case FSM_STATE_BOOTUP: + if (flag & FSM_F_MD_HS_START) { + port =3D mtk_port_search_by_id(port_mngr, CCCI_CONTROL_RX); + if (!port) { + dev_err(port_mngr->ctrl_blk->mdev->dev, + "Failed to find MD ctrl port\n"); + goto end; + } + ports_ops[port->info.type]->enable(port); + } else if (flag & FSM_F_SAP_HS_START) { + port =3D mtk_port_search_by_id(port_mngr, CCCI_SAP_CONTROL_RX); + if (!port) { + dev_err(port_mngr->ctrl_blk->mdev->dev, + "Failed to find sAP ctrl port\n"); + goto end; + } + ports_ops[port->info.type]->enable(port); + } + break; + case FSM_STATE_READY: + mtk_port_enable(port_mngr); + break; + case FSM_STATE_OFF: + mtk_port_disable(port_mngr); + break; + default: + dev_warn(port_mngr->ctrl_blk->mdev->dev, + "Unsupported fsm state %d & fsm flag 0x%x\n", fsm_param->to, flag); + break; + } +end: + return; +} + int mtk_port_mngr_init(struct mtk_ctrl_blk *ctrl_blk) { struct mtk_port_mngr *port_mngr; diff --git a/drivers/net/wwan/mediatek/mtk_port.h b/drivers/net/wwan/mediat= ek/mtk_port.h index dd6d47092028..55ab640e1c8b 100644 --- a/drivers/net/wwan/mediatek/mtk_port.h +++ b/drivers/net/wwan/mediatek/mtk_port.h @@ -14,6 +14,7 @@ =20 #include "mtk_ctrl_plane.h" #include "mtk_dev.h" +#include "mtk_fsm.h" =20 #define MTK_PEER_ID_MASK (0xF000) #define MTK_PEER_ID_SHIFT (12) @@ -22,6 +23,7 @@ #define MTK_PEER_ID_MD (0x2) #define MTK_CH_ID_MASK (0x0FFF) #define MTK_CH_ID(ch) ((ch) & MTK_CH_ID_MASK) +#define MTK_PORT_NAME_HDR "wwanD" #define MTK_DFLT_MAX_DEV_CNT (10) #define MTK_DFLT_PORT_NAME_LEN (20) =20 @@ -54,6 +56,7 @@ enum mtk_ccci_ch { =20 enum mtk_port_flag { PORT_F_DFLT =3D 0, + PORT_F_RAW_DATA =3D BIT(0), PORT_F_BLOCKING =3D BIT(1), PORT_F_ALLOW_DROP =3D BIT(2), }; @@ -75,6 +78,7 @@ struct mtk_internal_port { }; =20 union mtk_port_priv { + struct cdev *cdev; struct mtk_internal_port i_priv; }; =20 @@ -162,8 +166,10 @@ void mtk_port_stale_list_grp_cleanup(void); int mtk_port_add_header(struct sk_buff *skb); struct mtk_ccci_header *mtk_port_strip_header(struct sk_buff *skb); 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_vq_enable(struct mtk_port *port); int mtk_port_vq_disable(struct mtk_port *port); +void mtk_port_mngr_fsm_state_handler(struct mtk_fsm_param *fsm_param, void= *arg); int mtk_port_mngr_vq_status_check(struct sk_buff *skb); int mtk_port_mngr_init(struct mtk_ctrl_blk *ctrl_blk); void mtk_port_mngr_exit(struct mtk_ctrl_blk *ctrl_blk); diff --git a/drivers/net/wwan/mediatek/mtk_port_io.c b/drivers/net/wwan/med= iatek/mtk_port_io.c index 2ddd131dfe16..0a59c2049a4a 100644 --- a/drivers/net/wwan/mediatek/mtk_port_io.c +++ b/drivers/net/wwan/mediatek/mtk_port_io.c @@ -187,10 +187,7 @@ void *mtk_port_internal_open(struct mtk_md_dev *mdev, = char *name, int flag) goto end; } =20 - if (flag & O_NONBLOCK) - port->info.flags &=3D ~PORT_F_BLOCKING; - else - port->info.flags |=3D PORT_F_BLOCKING; + port->info.flags |=3D PORT_F_BLOCKING; end: return port; } diff --git a/drivers/net/wwan/mediatek/pcie/mtk_cldma_drv_t800.h b/drivers/= net/wwan/mediatek/pcie/mtk_cldma_drv_t800.h index 21e2f62acce2..03b7c73927b2 100644 --- a/drivers/net/wwan/mediatek/pcie/mtk_cldma_drv_t800.h +++ b/drivers/net/wwan/mediatek/pcie/mtk_cldma_drv_t800.h @@ -9,6 +9,7 @@ #include =20 #include "../mtk_cldma.h" +#include "../mtk_fsm.h" =20 int mtk_cldma_hw_init_t800(struct cldma_dev *cd, int hif_id); int mtk_cldma_hw_exit_t800(struct cldma_dev *cd, int hif_id); @@ -17,4 +18,5 @@ int mtk_cldma_txq_free_t800(struct cldma_hw *hw, int vqno= ); struct rxq *mtk_cldma_rxq_alloc_t800(struct cldma_hw *hw, struct sk_buff *= skb); int mtk_cldma_rxq_free_t800(struct cldma_hw *hw, int vqno); int mtk_cldma_start_xfer_t800(struct cldma_hw *hw, int qno); +void mtk_cldma_fsm_state_listener_t800(struct mtk_fsm_param *param, struct= cldma_hw *hw); #endif diff --git a/drivers/net/wwan/mediatek/pcie/mtk_pci.c b/drivers/net/wwan/me= diatek/pcie/mtk_pci.c index d1cf4bf10f6a..fc0f88cf25ce 100644 --- a/drivers/net/wwan/mediatek/pcie/mtk_pci.c +++ b/drivers/net/wwan/mediatek/pcie/mtk_pci.c @@ -13,6 +13,7 @@ #include #include =20 +#include "../mtk_fsm.h" #include "../mtk_port_io.h" #include "mtk_pci.h" #include "mtk_reg.h" @@ -859,9 +860,17 @@ static int mtk_pci_probe(struct pci_dev *pdev, const s= truct pci_device_id *id) =20 mtk_pci_unmask_irq(mdev, priv->mhccif_irq_id); =20 + ret =3D mtk_dev_start(mdev); + if (ret) { + dev_err(mdev->dev, "Failed to start dev.\n"); + goto clear_master_and_device; + } dev_info(mdev->dev, "Probe done hw_ver=3D0x%x\n", mdev->hw_ver); return 0; =20 +clear_master_and_device: + pci_clear_master(pdev); + mtk_dev_exit(mdev); free_irq: mtk_pci_free_irq(mdev); free_mhccif: diff --git a/drivers/net/wwan/mediatek/pcie/mtk_reg.h b/drivers/net/wwan/me= diatek/pcie/mtk_reg.h index 23fa7fd9518e..1159c29685c5 100644 --- a/drivers/net/wwan/mediatek/pcie/mtk_reg.h +++ b/drivers/net/wwan/mediatek/pcie/mtk_reg.h @@ -18,6 +18,17 @@ enum mtk_ext_evt_h2d { EXT_EVT_H2D_DEVICE_RESET =3D 1 << 13, }; =20 +enum mtk_ext_evt_d2h { + EXT_EVT_D2H_PCIE_DS_LOCK_ACK =3D 1 << 0, + EXT_EVT_D2H_EXCEPT_INIT =3D 1 << 1, + EXT_EVT_D2H_EXCEPT_INIT_DONE =3D 1 << 2, + EXT_EVT_D2H_EXCEPT_CLEARQ_DONE =3D 1 << 3, + EXT_EVT_D2H_EXCEPT_ALLQ_RESET =3D 1 << 4, + EXT_EVT_D2H_BOOT_FLOW_SYNC =3D 1 << 5, + EXT_EVT_D2H_ASYNC_HS_NOTIFY_SAP =3D 1 << 15, + EXT_EVT_D2H_ASYNC_HS_NOTIFY_MD =3D 1 << 16, +}; + #define REG_PCIE_SW_TRIG_INT 0x00BC #define REG_PCIE_LTSSM_STATUS 0x0150 #define REG_IMASK_LOCAL 0x0180 --=20 2.32.0 From nobody Thu Nov 14 06:37:28 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 170DEC7618A for ; Fri, 17 Mar 2023 09:04:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231375AbjCQJEU (ORCPT ); Fri, 17 Mar 2023 05:04:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230383AbjCQJDy (ORCPT ); Fri, 17 Mar 2023 05:03:54 -0400 Received: from mailgw02.mediatek.com (unknown [210.61.82.184]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C9EAE7EE4; Fri, 17 Mar 2023 02:02:09 -0700 (PDT) X-UUID: 90b17ca6c49b11edbd2e61cc88cc8f98-20230317 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=S4+SNWjEaDlxcR0HSwTTkOwGpMz9pKwy8zPg8kls7Dc=; b=b8D2Ep8//GuU/5Vk+ZyaKgoT9j1VUEwmu3ZAIzD2iUMnVf8BgjJoMzHLzuu3ceSrg2SLREngn7T42D7Qrn4gk6uIxojAi7OtfwXMpS/RQT3stAYrWI1ekUpRI/kyyTatfCVNwnytABZj5ZwUNR3UZJdXL5Xc1sXd0xPcr8+SGtY=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.21,REQID:a80ac92c-595d-431a-a454-c19360551b42,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:95,FILE:0,BULK:0,RULE:Release_Ham,ACTI ON:release,TS:70 X-CID-INFO: VERSION:1.1.21,REQID:a80ac92c-595d-431a-a454-c19360551b42,IP:0,URL :0,TC:0,Content:-25,EDM:0,RT:0,SF:95,FILE:0,BULK:0,RULE:Spam_GS981B3D,ACTI ON:quarantine,TS:70 X-CID-META: VersionHash:83295aa,CLOUDID:a6278db3-beed-4dfc-bd9c-e1b22fa6ccc4,B ulkID:2303171613160DL5Q7SP,BulkQuantity:0,Recheck:0,SF:38|29|28|17|19|48,T C:nil,Content:0,EDM:-3,IP:nil,URL:11|1,File:nil,Bulk:nil,QS:nil,BEC:nil,CO L:0,OSI:0,OSA:0,AV:0 X-CID-BVR: 0,NGT X-CID-BAS: 0,NGT,0,_ X-UUID: 90b17ca6c49b11edbd2e61cc88cc8f98-20230317 Received: from mtkmbs10n1.mediatek.inc [(172.21.101.34)] by mailgw02.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1406464604; Fri, 17 Mar 2023 16:13:14 +0800 Received: from mtkmbs11n2.mediatek.inc (172.21.101.187) by mtkmbs10n2.mediatek.inc (172.21.101.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Fri, 17 Mar 2023 16:13:14 +0800 Received: from mcddlt001.gcn.mediatek.inc (10.19.240.15) by mtkmbs11n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.25 via Frontend Transport; Fri, 17 Mar 2023 16:13:12 +0800 From: Yanchao Yang To: Loic Poulain , Sergey Ryazanov , Johannes Berg , "David S . Miller" , Eric Dumazet , "Jakub Kicinski" , Paolo Abeni , netdev ML , kernel ML CC: Intel experts , Chetan , MTK ML , Liang Lu , Haijun Liu , Hua Yang , Ting Wang , Felix Chen , Mingliang Xu , Min Dong , Aiden Wang , Guohao Zhang , Chris Feng , "Yanchao Yang" , Lambert Wang , Mingchuang Qiao , Xiayu Zhang , Haozhe Chang Subject: [PATCH net-next v4 06/10] net: wwan: tmi: Add AT & MBIM WWAN ports Date: Fri, 17 Mar 2023 16:09:38 +0800 Message-ID: <20230317080942.183514-7-yanchao.yang@mediatek.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20230317080942.183514-1-yanchao.yang@mediatek.com> References: <20230317080942.183514-1-yanchao.yang@mediatek.com> MIME-Version: 1.0 X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Adds 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. Signed-off-by: Yanchao Yang Signed-off-by: Felix Chen Reported-by: kernel test robot --- drivers/net/wwan/mediatek/mtk_ctrl_plane.c | 3 + drivers/net/wwan/mediatek/mtk_ctrl_plane.h | 2 +- drivers/net/wwan/mediatek/mtk_fsm.c | 9 + drivers/net/wwan/mediatek/mtk_port.c | 79 +++++- drivers/net/wwan/mediatek/mtk_port.h | 56 +++- drivers/net/wwan/mediatek/mtk_port_io.c | 301 ++++++++++++++++++++- drivers/net/wwan/mediatek/mtk_port_io.h | 11 + drivers/net/wwan/mediatek/pcie/mtk_pci.c | 18 +- 8 files changed, 473 insertions(+), 6 deletions(-) diff --git a/drivers/net/wwan/mediatek/mtk_ctrl_plane.c b/drivers/net/wwan/= mediatek/mtk_ctrl_plane.c index f5ac493f0146..bfe41795353d 100644 --- a/drivers/net/wwan/mediatek/mtk_ctrl_plane.c +++ b/drivers/net/wwan/mediatek/mtk_ctrl_plane.c @@ -17,6 +17,9 @@ static const struct virtq vq_tbl[] =3D { {VQ(0), CLDMA0, TXQ(0), RXQ(0), VQ_MTU_3_5K, VQ_MTU_3_5K, TX_REQ_NUM, RX_= REQ_NUM}, {VQ(1), CLDMA1, TXQ(0), RXQ(0), VQ_MTU_3_5K, VQ_MTU_3_5K, TX_REQ_NUM, RX_= REQ_NUM}, + {VQ(2), CLDMA1, TXQ(2), RXQ(2), VQ_MTU_3_5K, VQ_MTU_3_5K, TX_REQ_NUM, RX_= REQ_NUM}, + {VQ(3), CLDMA1, TXQ(5), RXQ(5), VQ_MTU_3_5K, VQ_MTU_3_5K, TX_REQ_NUM, RX_= REQ_NUM}, + {VQ(4), CLDMA1, TXQ(7), RXQ(7), VQ_MTU_3_5K, VQ_MTU_63K, TX_REQ_NUM, RX_R= EQ_NUM}, }; =20 static int mtk_ctrl_get_hif_id(unsigned char peer_id) diff --git a/drivers/net/wwan/mediatek/mtk_ctrl_plane.h b/drivers/net/wwan/= mediatek/mtk_ctrl_plane.h index 0885a434616e..f8216020448f 100644 --- a/drivers/net/wwan/mediatek/mtk_ctrl_plane.h +++ b/drivers/net/wwan/mediatek/mtk_ctrl_plane.h @@ -13,7 +13,7 @@ #include "mtk_fsm.h" =20 #define VQ(N) (N) -#define VQ_NUM (2) +#define VQ_NUM (5) #define TX_REQ_NUM (16) #define RX_REQ_NUM (TX_REQ_NUM) =20 diff --git a/drivers/net/wwan/mediatek/mtk_fsm.c b/drivers/net/wwan/mediate= k/mtk_fsm.c index be63f8adcb15..5d7eef4bb060 100644 --- a/drivers/net/wwan/mediatek/mtk_fsm.c +++ b/drivers/net/wwan/mediatek/mtk_fsm.c @@ -59,6 +59,7 @@ enum ctrl_msg_id { CTRL_MSG_HS1 =3D 0, CTRL_MSG_HS2 =3D 1, CTRL_MSG_HS3 =3D 2, + CTRL_MSG_UNIFIED_PORT_CFG =3D 11, }; =20 struct ctrl_msg_header { @@ -306,6 +307,14 @@ static int mtk_fsm_md_ctrl_msg_handler(void *__fsm, st= ruct sk_buff *skb) mtk_fsm_evt_submit(fsm->mdev, FSM_EVT_STARTUP, hs_info->fsm_flag_hs2, hs_info, sizeof(*hs_info), 0); break; + case CTRL_MSG_UNIFIED_PORT_CFG: + mtk_port_tbl_update(fsm->mdev, skb->data + sizeof(*ctrl_msg_h)); + ret =3D mtk_port_internal_write(hs_info->ctrl_port, skb); + if (ret <=3D 0) + dev_err(fsm->mdev->dev, "Unable to send port config ack message.\n"); + else + need_free_data =3D false; + break; default: dev_err(fsm->mdev->dev, "Invalid control message id\n"); } diff --git a/drivers/net/wwan/mediatek/mtk_port.c b/drivers/net/wwan/mediat= ek/mtk_port.c index 208817225645..68dc4bcca4bf 100644 --- a/drivers/net/wwan/mediatek/mtk_port.c +++ b/drivers/net/wwan/mediatek/mtk_port.c @@ -43,6 +43,8 @@ DEFINE_MUTEX(port_mngr_grp_mtx); static DEFINE_IDA(ccci_dev_ids); =20 static const struct mtk_port_cfg port_cfg[] =3D { + {CCCI_UART2_TX, CCCI_UART2_RX, VQ(3), PORT_TYPE_WWAN, "AT", PORT_F_ALLOW_= DROP}, + {CCCI_MBIM_TX, CCCI_MBIM_RX, VQ(2), PORT_TYPE_WWAN, "MBIM", PORT_F_ALLOW_= DROP}, {CCCI_CONTROL_TX, CCCI_CONTROL_RX, VQ(1), PORT_TYPE_INTERNAL, "MDCTRL", P= ORT_F_ALLOW_DROP}, {CCCI_SAP_CONTROL_TX, CCCI_SAP_CONTROL_RX, VQ(0), PORT_TYPE_INTERNAL, "SA= PCTRL", PORT_F_ALLOW_DROP}, @@ -281,6 +283,81 @@ static void mtk_port_tbl_destroy(struct mtk_port_mngr = *port_mngr, struct mtk_sta } while (tbl_type < PORT_TBL_MAX); } =20 +int mtk_port_tbl_update(struct mtk_md_dev *mdev, void *data) +{ + struct mtk_port_cfg_header *cfg_hdr =3D data; + struct mtk_port_cfg_hif_info *hif_info; + struct mtk_port_cfg_ch_info *ch_info; + struct mtk_port_mngr *port_mngr; + struct mtk_ctrl_blk *ctrl_blk; + int parsed_data_len =3D 0; + struct mtk_port *port; + int ret =3D 0; + + if (unlikely(!mdev || !cfg_hdr)) { + ret =3D -EINVAL; + goto end; + } + + ctrl_blk =3D mdev->ctrl_blk; + port_mngr =3D ctrl_blk->port_mngr; + + if (cfg_hdr->msg_type !=3D PORT_CFG_MSG_REQUEST) { + dev_warn(mdev->dev, "Invalid msg_type: %d\n", cfg_hdr->msg_type); + ret =3D -EPROTO; + goto end; + } + + if (cfg_hdr->is_enable !=3D 1) { + dev_warn(mdev->dev, "Invalid enable flag: %d\n", cfg_hdr->is_enable); + ret =3D -EPROTO; + goto end; + } + switch (cfg_hdr->cfg_type) { + case PORT_CFG_CH_INFO: + while (parsed_data_len < le16_to_cpu(cfg_hdr->port_config_len)) { + ch_info =3D (struct mtk_port_cfg_ch_info *)(cfg_hdr->data + parsed_data= _len); + parsed_data_len +=3D sizeof(*ch_info); + + port =3D mtk_port_search_by_id(port_mngr, le16_to_cpu(ch_info->dl_ch_id= )); + if (port) { + continue; + } else { + dev_warn(mdev->dev, + "It's not supported the extended port(%s),ch: 0x%x\n", + ch_info->port_name, le16_to_cpu(ch_info->dl_ch_id)); + } + } + cfg_hdr->msg_type =3D PORT_CFG_MSG_RESPONSE; + break; + case PORT_CFG_HIF_INFO: + hif_info =3D (struct mtk_port_cfg_hif_info *)cfg_hdr->data; + mtk_ctrl_vq_color_cleanup(port_mngr->ctrl_blk, hif_info->peer_id); + + while (parsed_data_len < le16_to_cpu(cfg_hdr->port_config_len)) { + hif_info =3D (struct mtk_port_cfg_hif_info *) + (cfg_hdr->data + parsed_data_len); + parsed_data_len +=3D sizeof(*hif_info); + mtk_ctrl_vq_color_paint(port_mngr->ctrl_blk, + hif_info->peer_id, + hif_info->ul_hw_queue_id, + hif_info->dl_hw_queue_id, + le32_to_cpu(hif_info->ul_hw_queue_mtu), + le32_to_cpu(hif_info->dl_hw_queue_mtu)); + } + cfg_hdr->msg_type =3D PORT_CFG_MSG_RESPONSE; + break; + default: + dev_warn(mdev->dev, "Unsupported cfg_type: %d\n", cfg_hdr->cfg_type); + cfg_hdr->is_enable =3D 0; + ret =3D -EPROTO; + break; + } + +end: + return ret; +} + static struct mtk_stale_list *mtk_port_stale_list_create(struct mtk_port_m= ngr *port_mngr) { struct mtk_stale_list *s_list; @@ -473,7 +550,7 @@ static int mtk_port_tx_complete(struct sk_buff *skb) return 0; } =20 -static int mtk_port_status_check(struct mtk_port *port) +int mtk_port_status_check(struct mtk_port *port) { if (!test_bit(PORT_S_ENABLE, &port->status)) { pr_err("[TMI]Unable to use port: (%s) disabled. Caller: %ps\n", diff --git a/drivers/net/wwan/mediatek/mtk_port.h b/drivers/net/wwan/mediat= ek/mtk_port.h index 55ab640e1c8b..f6091f3f6e1f 100644 --- a/drivers/net/wwan/mediatek/mtk_port.h +++ b/drivers/net/wwan/mediatek/mtk_port.h @@ -26,6 +26,7 @@ #define MTK_PORT_NAME_HDR "wwanD" #define MTK_DFLT_MAX_DEV_CNT (10) #define MTK_DFLT_PORT_NAME_LEN (20) +#define MTK_DFLT_FULL_NAME_LEN (50) =20 #define MTK_PORT_TBL_TYPE(ch) (MTK_PEER_ID(ch) - 1) =20 @@ -52,6 +53,10 @@ enum mtk_ccci_ch { CCCI_SAP_CONTROL_TX =3D 0x1001, CCCI_CONTROL_RX =3D 0x2000, CCCI_CONTROL_TX =3D 0x2001, + CCCI_UART2_RX =3D 0x200A, + CCCI_UART2_TX =3D 0x200C, + CCCI_MBIM_RX =3D 0x20D0, + CCCI_MBIM_TX =3D 0x20D1, }; =20 enum mtk_port_flag { @@ -69,6 +74,7 @@ enum mtk_port_tbl { =20 enum mtk_port_type { PORT_TYPE_INTERNAL, + PORT_TYPE_WWAN, PORT_TYPE_MAX }; =20 @@ -77,9 +83,16 @@ struct mtk_internal_port { int (*recv_cb)(void *arg, struct sk_buff *skb); }; =20 +struct mtk_wwan_port { + /* w_lock Protect wwan_port when recv data and disable port at the same t= ime */ + struct mutex w_lock; + int w_type; + void *w_port; +}; + union mtk_port_priv { - struct cdev *cdev; struct mtk_internal_port i_priv; + struct mtk_wwan_port w_priv; }; =20 struct mtk_port_cfg { @@ -151,6 +164,45 @@ struct mtk_port_enum_msg { u8 data[]; } __packed; =20 +enum mtk_port_cfg_type { + PORT_CFG_CH_INFO =3D 4, + PORT_CFG_HIF_INFO, +}; + +enum mtk_port_cfg_msg_type { + PORT_CFG_MSG_REQUEST =3D 1, + PORT_CFG_MSG_RESPONSE, +}; + +struct mtk_port_cfg_ch_info { + __le16 dl_ch_id; + u8 dl_hw_queue_id; + u8 ul_hw_queue_id; + u8 reserve[2]; + u8 peer_id; + u8 reserved; + u8 port_name_len; + char port_name[20]; +} __packed; + +struct mtk_port_cfg_hif_info { + u8 dl_hw_queue_id; + u8 ul_hw_queue_id; + u8 peer_id; + u8 reserved; + __le32 dl_hw_queue_mtu; + __le32 ul_hw_queue_mtu; +} __packed; + +struct mtk_port_cfg_header { + __le16 port_config_len; + u8 cfg_type; + u8 msg_type; + u8 is_enable; + u8 reserve[3]; + u8 data[]; +} __packed; + struct mtk_ccci_header { __le32 packet_header; __le32 packet_len; @@ -165,8 +217,10 @@ struct mtk_port *mtk_port_search_by_name(struct mtk_po= rt_mngr *port_mngr, char * void mtk_port_stale_list_grp_cleanup(void); int mtk_port_add_header(struct sk_buff *skb); struct mtk_ccci_header *mtk_port_strip_header(struct sk_buff *skb); +int mtk_port_status_check(struct mtk_port *port); 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_tbl_update(struct mtk_md_dev *mdev, void *data); int mtk_port_vq_enable(struct mtk_port *port); int mtk_port_vq_disable(struct mtk_port *port); void mtk_port_mngr_fsm_state_handler(struct mtk_fsm_param *fsm_param, void= *arg); diff --git a/drivers/net/wwan/mediatek/mtk_port_io.c b/drivers/net/wwan/med= iatek/mtk_port_io.c index 0a59c2049a4a..599033099ad6 100644 --- a/drivers/net/wwan/mediatek/mtk_port_io.c +++ b/drivers/net/wwan/mediatek/mtk_port_io.c @@ -3,10 +3,24 @@ * Copyright (c) 2022, MediaTek Inc. */ =20 +#ifdef CONFIG_COMPAT +#include +#endif +#include +#include +#include +#include +#include +#include +#include + #include "mtk_port_io.h" =20 +#define MTK_CCCI_CLASS_NAME "ccci_node" #define MTK_DFLT_READ_TIMEOUT (1 * HZ) =20 +static void *ccci_class; + static int mtk_port_get_locked(struct mtk_port *port) { int ret =3D 0; @@ -30,6 +44,19 @@ static void mtk_port_put_locked(struct mtk_port *port) mutex_unlock(&port_mngr_grp_mtx); } =20 +int mtk_port_io_init(void) +{ + ccci_class =3D class_create(THIS_MODULE, MTK_CCCI_CLASS_NAME); + if (IS_ERR(ccci_class)) + return PTR_ERR(ccci_class); + return 0; +} + +void mtk_port_io_exit(void) +{ + class_destroy(ccci_class); +} + static void mtk_port_struct_init(struct mtk_port *port) { port->tx_seq =3D 0; @@ -41,6 +68,23 @@ static void mtk_port_struct_init(struct mtk_port *port) init_waitqueue_head(&port->trb_wq); init_waitqueue_head(&port->rx_wq); mutex_init(&port->read_buf_lock); + mutex_init(&port->write_lock); +} + +static int mtk_port_copy_data_from(void *to, union user_buf from, unsigned= int len, + unsigned int offset, bool from_user_space) +{ + int ret =3D 0; + + if (from_user_space) { + ret =3D copy_from_user(to, from.ubuf + offset, len); + if (ret) + ret =3D -EFAULT; + } else { + memcpy(to, from.kbuf + offset, len); + } + + return ret; } =20 static int mtk_port_internal_init(struct mtk_port *port) @@ -73,7 +117,7 @@ static int mtk_port_internal_enable(struct mtk_port *por= t) =20 if (test_bit(PORT_S_ENABLE, &port->status)) { dev_info(port->port_mngr->ctrl_blk->mdev->dev, - "Skip to enable port( %s )\n", port->info.name); + "Skip to enable port(%s)\n", port->info.name); return 0; } =20 @@ -166,6 +210,52 @@ static void mtk_port_common_close(struct mtk_port *por= t) skb_queue_purge(&port->rx_skb_list); } =20 +static int mtk_port_common_write(struct mtk_port *port, union user_buf buf= , unsigned int len, + bool from_user_space) +{ + unsigned int tx_cnt, left_cnt =3D len; + struct sk_buff *skb; + int ret; + +start_write: + ret =3D mtk_port_status_check(port); + if (ret) + goto end_write; + + skb =3D __dev_alloc_skb(port->tx_mtu, GFP_KERNEL); + if (!skb) { + ret =3D -ENOMEM; + goto end_write; + } + + if (!(port->info.flags & PORT_F_RAW_DATA)) + skb_reserve(skb, sizeof(struct mtk_ccci_header)); + + tx_cnt =3D min(left_cnt, port->tx_mtu); + ret =3D mtk_port_copy_data_from(skb_put(skb, tx_cnt), buf, tx_cnt, len - = left_cnt, + from_user_space); + if (ret) { + dev_err(port->port_mngr->ctrl_blk->mdev->dev, + "Failed to copy data for port(%s)\n", port->info.name); + dev_kfree_skb_any(skb); + goto end_write; + } + + ret =3D mtk_port_send_data(port, skb); + if (ret < 0) + goto end_write; + + left_cnt -=3D ret; + if (left_cnt) { + dev_dbg(port->port_mngr->ctrl_blk->mdev->dev, + "Port(%s) send %dBytes, but still left %dBytes to send\n", + port->info.name, ret, left_cnt); + goto start_write; + } +end_write: + return (len > left_cnt) ? (len - left_cnt) : ret; +} + void *mtk_port_internal_open(struct mtk_md_dev *mdev, char *name, int flag) { struct mtk_port_mngr *port_mngr; @@ -187,7 +277,10 @@ void *mtk_port_internal_open(struct mtk_md_dev *mdev, = char *name, int flag) goto end; } =20 - port->info.flags |=3D PORT_F_BLOCKING; + if (flag & O_NONBLOCK) + port->info.flags &=3D ~PORT_F_BLOCKING; + else + port->info.flags |=3D PORT_F_BLOCKING; end: return port; } @@ -236,6 +329,200 @@ void mtk_port_internal_recv_register(void *i_port, priv->recv_cb =3D cb; } =20 +static int mtk_port_wwan_open(struct wwan_port *w_port) +{ + struct mtk_port *port; + int ret; + + port =3D wwan_port_get_drvdata(w_port); + ret =3D mtk_port_get_locked(port); + if (ret) + return ret; + + ret =3D mtk_port_common_open(port); + if (ret) + mtk_port_put_locked(port); + + return ret; +} + +static void mtk_port_wwan_close(struct wwan_port *w_port) +{ + struct mtk_port *port =3D wwan_port_get_drvdata(w_port); + + mtk_port_common_close(port); + mtk_port_put_locked(port); +} + +static int mtk_port_wwan_write(struct wwan_port *w_port, struct sk_buff *s= kb) +{ + struct mtk_port *port =3D wwan_port_get_drvdata(w_port); + union user_buf user_buf; + + port->info.flags &=3D ~PORT_F_BLOCKING; + user_buf.kbuf =3D (void *)skb->data; + return mtk_port_common_write(port, user_buf, skb->len, false); +} + +static int mtk_port_wwan_write_blocking(struct wwan_port *w_port, struct s= k_buff *skb) +{ + struct mtk_port *port =3D wwan_port_get_drvdata(w_port); + union user_buf user_buf; + + port->info.flags |=3D PORT_F_BLOCKING; + user_buf.kbuf =3D (void *)skb->data; + return mtk_port_common_write(port, user_buf, skb->len, false); +} + +static __poll_t mtk_port_wwan_poll(struct wwan_port *w_port, struct file *= file, + struct poll_table_struct *poll) +{ + struct mtk_port *port =3D wwan_port_get_drvdata(w_port); + struct mtk_ctrl_blk *ctrl_blk; + __poll_t mask =3D 0; + + if (mtk_port_status_check(port)) + goto end_poll; + + ctrl_blk =3D port->port_mngr->ctrl_blk; + poll_wait(file, &port->trb_wq, poll); + if (!VQ_LIST_FULL(ctrl_blk->trans, port->info.vq_id)) + mask |=3D EPOLLOUT | EPOLLWRNORM; + else + dev_info(ctrl_blk->mdev->dev, "VQ(%d) skb_list_len is %d\n", + port->info.vq_id, ctrl_blk->trans->skb_list[port->info.vq_id].qlen); + +end_poll: + return mask; +} + +static const struct wwan_port_ops wwan_ops =3D { + .start =3D mtk_port_wwan_open, + .stop =3D mtk_port_wwan_close, + .tx =3D mtk_port_wwan_write, + .tx_blocking =3D mtk_port_wwan_write_blocking, + .tx_poll =3D mtk_port_wwan_poll, +}; + +static int mtk_port_wwan_init(struct mtk_port *port) +{ + mtk_port_struct_init(port); + port->enable =3D false; + + mutex_init(&port->priv.w_priv.w_lock); + + switch (port->info.rx_ch) { + case CCCI_MBIM_RX: + port->priv.w_priv.w_type =3D WWAN_PORT_MBIM; + break; + case CCCI_UART2_RX: + port->priv.w_priv.w_type =3D WWAN_PORT_AT; + break; + default: + port->priv.w_priv.w_type =3D WWAN_PORT_UNKNOWN; + break; + } + + return 0; +} + +static int mtk_port_wwan_exit(struct mtk_port *port) +{ + if (test_bit(PORT_S_ENABLE, &port->status)) + ports_ops[port->info.type]->disable(port); + + pr_err("[TMI] WWAN port(%s) exit is complete\n", port->info.name); + + return 0; +} + +static int mtk_port_wwan_enable(struct mtk_port *port) +{ + struct mtk_port_mngr *port_mngr; + int ret =3D 0; + + port_mngr =3D port->port_mngr; + + if (test_bit(PORT_S_ENABLE, &port->status)) { + dev_err(port_mngr->ctrl_blk->mdev->dev, + "Skip to enable port( %s )\n", port->info.name); + goto end; + } + + ret =3D mtk_port_vq_enable(port); + if (ret && ret !=3D -EBUSY) + goto end; + + port->priv.w_priv.w_port =3D wwan_create_port(port_mngr->ctrl_blk->mdev->= dev, + port->priv.w_priv.w_type, &wwan_ops, port); + if (IS_ERR(port->priv.w_priv.w_port)) { + dev_err(port_mngr->ctrl_blk->mdev->dev, + "Failed to create wwan port for (%s)\n", port->info.name); + ret =3D PTR_ERR(port->priv.w_priv.w_port); + goto end; + } + + set_bit(PORT_S_RDWR, &port->status); + set_bit(PORT_S_ENABLE, &port->status); + dev_info(port_mngr->ctrl_blk->mdev->dev, + "Port(%s) enable is complete\n", port->info.name); + + return 0; +end: + return ret; +} + +static int mtk_port_wwan_disable(struct mtk_port *port) +{ + struct wwan_port *w_port; + + if (!test_and_clear_bit(PORT_S_ENABLE, &port->status)) { + dev_info(port->port_mngr->ctrl_blk->mdev->dev, + "Skip to disable port(%s)\n", port->info.name); + return 0; + } + + clear_bit(PORT_S_RDWR, &port->status); + w_port =3D port->priv.w_priv.w_port; + mutex_lock(&port->priv.w_priv.w_lock); + port->priv.w_priv.w_port =3D NULL; + mutex_unlock(&port->priv.w_priv.w_lock); + + wwan_remove_port(w_port); + + mtk_port_vq_disable(port); + + dev_info(port->port_mngr->ctrl_blk->mdev->dev, + "Port(%s) disable is complete\n", port->info.name); + + return 0; +} + +static int mtk_port_wwan_recv(struct mtk_port *port, struct sk_buff *skb) +{ + if (!test_bit(PORT_S_OPEN, &port->status)) { + dev_warn_ratelimited(port->port_mngr->ctrl_blk->mdev->dev, + "Unabled to recv: (%s) not opened\n", port->info.name); + goto drop_data; + } + + mutex_lock(&port->priv.w_priv.w_lock); + if (!port->priv.w_priv.w_port) { + mutex_unlock(&port->priv.w_priv.w_lock); + dev_warn_ratelimited(port->port_mngr->ctrl_blk->mdev->dev, + "Invalid (%s) wwan_port, drop packet\n", port->info.name); + goto drop_data; + } + + wwan_port_rx(port->priv.w_priv.w_port, skb); + mutex_unlock(&port->priv.w_priv.w_lock); + return 0; + +drop_data: + dev_kfree_skb_any(skb); + return -ENXIO; +} + static const struct port_ops port_internal_ops =3D { .init =3D mtk_port_internal_init, .exit =3D mtk_port_internal_exit, @@ -245,6 +532,16 @@ static const struct port_ops port_internal_ops =3D { .recv =3D mtk_port_internal_recv, }; =20 +static const struct port_ops port_wwan_ops =3D { + .init =3D mtk_port_wwan_init, + .exit =3D mtk_port_wwan_exit, + .reset =3D mtk_port_reset, + .enable =3D mtk_port_wwan_enable, + .disable =3D mtk_port_wwan_disable, + .recv =3D mtk_port_wwan_recv, +}; + const struct port_ops *ports_ops[PORT_TYPE_MAX] =3D { &port_internal_ops, + &port_wwan_ops, }; diff --git a/drivers/net/wwan/mediatek/mtk_port_io.h b/drivers/net/wwan/med= iatek/mtk_port_io.h index 30e1d4149881..034b5a2d8f12 100644 --- a/drivers/net/wwan/mediatek/mtk_port_io.h +++ b/drivers/net/wwan/mediatek/mtk_port_io.h @@ -9,9 +9,12 @@ #include #include =20 +#include "mtk_ctrl_plane.h" +#include "mtk_dev.h" #include "mtk_port.h" =20 #define MTK_RX_BUF_SIZE (1024 * 1024) +#define MTK_RX_BUF_MAX_SIZE (2 * 1024 * 1024) =20 extern struct mutex port_mngr_grp_mtx; =20 @@ -24,6 +27,14 @@ struct port_ops { int (*recv)(struct mtk_port *port, struct sk_buff *skb); }; =20 +union user_buf { + void __user *ubuf; + void *kbuf; +}; + +int mtk_port_io_init(void); +void mtk_port_io_exit(void); + 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); diff --git a/drivers/net/wwan/mediatek/pcie/mtk_pci.c b/drivers/net/wwan/me= diatek/pcie/mtk_pci.c index fc0f88cf25ce..a488f0fa3c2e 100644 --- a/drivers/net/wwan/mediatek/pcie/mtk_pci.c +++ b/drivers/net/wwan/mediatek/pcie/mtk_pci.c @@ -946,13 +946,29 @@ static struct pci_driver mtk_pci_drv =3D { =20 static int __init mtk_drv_init(void) { - return pci_register_driver(&mtk_pci_drv); + int ret; + + ret =3D mtk_port_io_init(); + if (ret) + goto exit; + + ret =3D pci_register_driver(&mtk_pci_drv); + if (ret) + goto free_port_io; + + return 0; +free_port_io: + mtk_port_io_exit(); +exit: + + return ret; } module_init(mtk_drv_init); =20 static void __exit mtk_drv_exit(void) { pci_unregister_driver(&mtk_pci_drv); + mtk_port_io_exit(); mtk_port_stale_list_grp_cleanup(); } module_exit(mtk_drv_exit); --=20 2.32.0 From nobody Thu Nov 14 06:37:28 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6405AC6FD1D for ; Fri, 17 Mar 2023 08:31:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231609AbjCQIby (ORCPT ); Fri, 17 Mar 2023 04:31:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40720 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231518AbjCQIb2 (ORCPT ); Fri, 17 Mar 2023 04:31:28 -0400 Received: from mailgw02.mediatek.com (unknown [210.61.82.184]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E06D43934; Fri, 17 Mar 2023 01:30:11 -0700 (PDT) X-UUID: a69649a2c49b11edbd2e61cc88cc8f98-20230317 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=LBIcKhoPtQjKnJ7FH+rIUihrSDzJMxOLWd6PFxgQVfE=; b=KiCbOfoki57un/zVKw3Ki7AK4K7uvQ4wysJeH2cgaL5ZbbcarTopDrN2tdTA+J0+sm62+SYNAqNEQyYgxJBHJYne/SDN3Fqg00jhj3nYEeHtG3Pb9ue0EFrjmnrA8B/ZIsudmJOIl3cx3zY1tIO+zPCWR8Q2DpkJbTzi0k+mPsM=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.21,REQID:6da436b1-a1fa-4479-939c-002d99022abe,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-25 X-CID-META: VersionHash:83295aa,CLOUDID:c24dac28-564d-42d9-9875-7c868ee415ec,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:0,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:1,OSI:0,OSA:0,AV:0 X-CID-BVR: 2,OSH X-CID-BAS: 2,OSH,0,_ X-UUID: a69649a2c49b11edbd2e61cc88cc8f98-20230317 Received: from mtkmbs13n2.mediatek.inc [(172.21.101.108)] by mailgw02.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 940264783; Fri, 17 Mar 2023 16:13:51 +0800 Received: from mtkmbs11n2.mediatek.inc (172.21.101.187) by mtkmbs11n1.mediatek.inc (172.21.101.185) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Fri, 17 Mar 2023 16:13:44 +0800 Received: from mcddlt001.gcn.mediatek.inc (10.19.240.15) by mtkmbs11n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.25 via Frontend Transport; Fri, 17 Mar 2023 16:13:42 +0800 From: Yanchao Yang To: Loic Poulain , Sergey Ryazanov , Johannes Berg , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , netdev ML , kernel ML CC: Intel experts , Chetan , MTK ML , Liang Lu , Haijun Liu , Hua Yang , Ting Wang , Felix Chen , Mingliang Xu , Min Dong , Aiden Wang , Guohao Zhang , Chris Feng , Yanchao Yang , Lambert Wang , Mingchuang Qiao , Xiayu Zhang , Haozhe Chang Subject: [PATCH net-next v4 07/10] net: wwan: tmi: Introduce data plane hardware interface Date: Fri, 17 Mar 2023 16:09:39 +0800 Message-ID: <20230317080942.183514-8-yanchao.yang@mediatek.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20230317080942.183514-1-yanchao.yang@mediatek.com> References: <20230317080942.183514-1-yanchao.yang@mediatek.com> MIME-Version: 1.0 X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Data Plane Modem AP Interface (DPMAIF) hardware layer provides hardware abstraction for the upper layer (DPMAIF HIF). It implements functions to do= the data plane hardware's configuration, TX/RX control and interrupt handling. Signed-off-by: Yanchao Yang Signed-off-by: Hua Yang --- drivers/net/wwan/mediatek/Makefile | 1 + drivers/net/wwan/mediatek/mtk_dpmaif_drv.h | 202 +++ .../wwan/mediatek/pcie/mtk_dpmaif_drv_t800.c | 1545 +++++++++++++++++ .../wwan/mediatek/pcie/mtk_dpmaif_reg_t800.h | 319 ++++ 4 files changed, 2067 insertions(+) create mode 100644 drivers/net/wwan/mediatek/mtk_dpmaif_drv.h create mode 100644 drivers/net/wwan/mediatek/pcie/mtk_dpmaif_drv_t800.c create mode 100644 drivers/net/wwan/mediatek/pcie/mtk_dpmaif_reg_t800.h diff --git a/drivers/net/wwan/mediatek/Makefile b/drivers/net/wwan/mediatek= /Makefile index c3f13c81b6b0..9de12d8f373a 100644 --- a/drivers/net/wwan/mediatek/Makefile +++ b/drivers/net/wwan/mediatek/Makefile @@ -8,6 +8,7 @@ mtk_tmi-y =3D \ mtk_ctrl_plane.o \ mtk_cldma.o \ pcie/mtk_cldma_drv_t800.o \ + pcie/mtk_dpmaif_drv_t800.o \ mtk_port.o \ mtk_port_io.o \ mtk_fsm.o diff --git a/drivers/net/wwan/mediatek/mtk_dpmaif_drv.h b/drivers/net/wwan/= mediatek/mtk_dpmaif_drv.h new file mode 100644 index 000000000000..1e89fe2ba6e3 --- /dev/null +++ b/drivers/net/wwan/mediatek/mtk_dpmaif_drv.h @@ -0,0 +1,202 @@ +/* SPDX-License-Identifier: BSD-3-Clause-Clear + * + * Copyright (c) 2022, MediaTek Inc. + */ + +#ifndef __MTK_DPMAIF_DRV_H__ +#define __MTK_DPMAIF_DRV_H__ + +enum dpmaif_drv_dir { + DPMAIF_TX, + DPMAIF_RX, +}; + +struct dpmaif_drv_intr { + enum dpmaif_drv_dir dir; + unsigned int q_mask; + unsigned int mode; +}; + +enum mtk_drv_err { + DATA_ERR_STOP_MAX =3D 10, + DATA_HW_REG_TIMEOUT, + DATA_HW_REG_CHK_FAIL, + DATA_FLOW_CHK_ERR, + DATA_DMA_MAP_ERR, + DATA_DL_ONCE_MORE, + DATA_PIT_SEQ_CHK_FAIL, + DATA_LOW_MEM_DRB, + DATA_LOW_MEM_SKB, +}; + +#define DPMAIF_RXQ_CNT_MAX 2 +#define DPMAIF_TXQ_CNT_MAX 5 +#define DPMAIF_IRQ_CNT_MAX 3 + +#define DPMAIF_PIT_SEQ_MAX 251 + +#define DPMAIF_HW_PKT_ALIGN 64 +#define DPMAIF_HW_BAT_RSVLEN 0 + +enum { + DPMAIF_CLEAR_INTR, + DPMAIF_UNMASK_INTR, +}; + +enum dpmaif_drv_dlq_id { + DPMAIF_DLQ0 =3D 0, + DPMAIF_DLQ1, +}; + +struct dpmaif_drv_dlq { + bool q_started; + dma_addr_t pit_base; + u32 pit_size; +}; + +struct dpmaif_drv_ulq { + bool q_started; + dma_addr_t drb_base; + u32 drb_size; +}; + +struct dpmaif_drv_data_ring { + dma_addr_t normal_bat_base; + u32 normal_bat_size; + u32 normal_bat_remain_size; + u32 normal_bat_pkt_bufsz; + u32 normal_bat_rsv_length; + u32 pkt_bid_max_cnt; + u32 pkt_alignment; + u32 mtu; + u32 chk_pit_num; + u32 chk_normal_bat_num; +}; + +struct dpmaif_drv_property { + u32 features; + struct dpmaif_drv_dlq dlq[DPMAIF_RXQ_CNT_MAX]; + struct dpmaif_drv_ulq ulq[DPMAIF_TXQ_CNT_MAX]; + struct dpmaif_drv_data_ring ring; +}; + +enum dpmaif_drv_ring_type { + DPMAIF_PIT, + DPMAIF_BAT, + DPMAIF_DRB, +}; + +enum dpmaif_drv_ring_idx { + DPMAIF_PIT_WIDX, + DPMAIF_PIT_RIDX, + DPMAIF_BAT_WIDX, + DPMAIF_BAT_RIDX, + DPMAIF_DRB_WIDX, + DPMAIF_DRB_RIDX, +}; + +struct dpmaif_drv_irq_en_mask { + u32 ap_ul_l2intr_en_mask; + u32 ap_dl_l2intr_en_mask; + u32 ap_udl_ip_busy_en_mask; +}; + +struct dpmaif_drv_info { + struct mtk_md_dev *mdev; + bool ulq_all_enable, dlq_all_enable; + struct dpmaif_drv_property drv_property; + struct dpmaif_drv_irq_en_mask drv_irq_en_mask; + struct dpmaif_drv_ops *drv_ops; +}; + +struct dpmaif_drv_cfg { + dma_addr_t drb_base[DPMAIF_TXQ_CNT_MAX]; + u32 drb_cnt[DPMAIF_TXQ_CNT_MAX]; + dma_addr_t pit_base[DPMAIF_RXQ_CNT_MAX]; + u32 pit_cnt[DPMAIF_RXQ_CNT_MAX]; + dma_addr_t normal_bat_base; + u32 normal_bat_cnt; + u32 normal_bat_buf_size; + u32 max_mtu; + u32 features; +}; + +enum dpmaif_drv_intr_type { + DPMAIF_INTR_MIN =3D 0, + DPMAIF_INTR_UL_DONE, + DPMAIF_INTR_DL_DONE, + DPMAIF_INTR_MAX +}; + +#define DPMAIF_INTR_COUNT ((DPMAIF_INTR_MAX) - (DPMAIF_INTR_MIN) - 1) + +struct dpmaif_drv_intr_info { + unsigned char intr_cnt; + enum dpmaif_drv_intr_type intr_types[DPMAIF_INTR_COUNT]; + u32 intr_queues[DPMAIF_INTR_COUNT]; +}; + +struct dpmaif_drv_ops { + int (*init)(struct dpmaif_drv_info *drv_info, void *data); + int (*start_queue)(struct dpmaif_drv_info *drv_info, enum dpmaif_drv_dir = dir); + int (*stop_queue)(struct dpmaif_drv_info *drv_info, enum dpmaif_drv_dir d= ir); + int (*intr_handle)(struct dpmaif_drv_info *drv_info, void *data, u8 irq_i= d); + int (*intr_complete)(struct dpmaif_drv_info *drv_info, enum dpmaif_drv_in= tr_type type, + u8 q_id, u64 data); + int (*clear_ip_busy)(struct dpmaif_drv_info *drv_info); + int (*send_doorbell)(struct dpmaif_drv_info *drv_info, enum dpmaif_drv_ri= ng_type type, + u8 q_id, u32 cnt); + int (*get_ring_idx)(struct dpmaif_drv_info *drv_info, enum dpmaif_drv_rin= g_idx index, + u8 q_id); + void (*dump)(struct dpmaif_drv_info *drv_info); +}; + +static inline int mtk_dpmaif_drv_init(struct dpmaif_drv_info *drv_info, vo= id *data) +{ + return drv_info->drv_ops->init(drv_info, data); +} + +static inline int mtk_dpmaif_drv_start_queue(struct dpmaif_drv_info *drv_i= nfo, + enum dpmaif_drv_dir dir) +{ + return drv_info->drv_ops->start_queue(drv_info, dir); +} + +static inline int mtk_dpmaif_drv_stop_queue(struct dpmaif_drv_info *drv_in= fo, + enum dpmaif_drv_dir dir) +{ + return drv_info->drv_ops->stop_queue(drv_info, dir); +} + +static inline int mtk_dpmaif_drv_intr_handle(struct dpmaif_drv_info *drv_i= nfo, + void *data, u8 irq_id) +{ + return drv_info->drv_ops->intr_handle(drv_info, data, irq_id); +} + +static inline int mtk_dpmaif_drv_intr_complete(struct dpmaif_drv_info *drv= _info, + enum dpmaif_drv_intr_type type, u8 q_id, u64 data) +{ + return drv_info->drv_ops->intr_complete(drv_info, type, q_id, data); +} + +static inline int mtk_dpmaif_drv_clear_ip_busy(struct dpmaif_drv_info *drv= _info) +{ + return drv_info->drv_ops->clear_ip_busy(drv_info); +} + +static inline int mtk_dpmaif_drv_send_doorbell(struct dpmaif_drv_info *drv= _info, + enum dpmaif_drv_ring_type type, u8 q_id, u32 cnt) +{ + return drv_info->drv_ops->send_doorbell(drv_info, type, q_id, cnt); +} + +static inline int mtk_dpmaif_drv_get_ring_idx(struct dpmaif_drv_info *drv_= info, + enum dpmaif_drv_ring_idx index, u8 q_id) +{ + return drv_info->drv_ops->get_ring_idx(drv_info, index, q_id); +} + +extern struct dpmaif_drv_ops dpmaif_drv_ops_t800; + +#endif diff --git a/drivers/net/wwan/mediatek/pcie/mtk_dpmaif_drv_t800.c b/drivers= /net/wwan/mediatek/pcie/mtk_dpmaif_drv_t800.c new file mode 100644 index 000000000000..d0c0dfb3205a --- /dev/null +++ b/drivers/net/wwan/mediatek/pcie/mtk_dpmaif_drv_t800.c @@ -0,0 +1,1545 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +/* + * Copyright (c) 2022, MediaTek Inc. + */ + +#include +#include + +#include "../mtk_dev.h" +#include "../mtk_dpmaif_drv.h" +#include "mtk_dpmaif_reg_t800.h" + +#define DRV_TO_MDEV(__drv_info) ((__drv_info)->mdev) + +#define POLL_MAX_TIMES 200 +#define POLL_INTERVAL_US 10 + +static void mtk_dpmaif_drv_reset(struct dpmaif_drv_info *drv_info) +{ + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_AP_AO_RGU_ASSERT, DPMAIF_AP_= AO_RST_BIT); + udelay(2); + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_AP_RGU_ASSERT, DPMAIF_AP_RST= _BIT); + udelay(2); + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_AP_AO_RGU_DEASSERT, DPMAIF_A= P_AO_RST_BIT); + udelay(2); + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_AP_RGU_DEASSERT, DPMAIF_AP_R= ST_BIT); + udelay(2); +} + +static bool mtk_dpmaif_drv_sram_init(struct dpmaif_drv_info *drv_info) +{ + u32 val, cnt =3D 0; + bool ret =3D true; + + val =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_AP_MISC_RSTR_CLR= ); + val |=3D DPMAIF_MEM_CLR_MASK; + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_AP_MISC_RSTR_CLR, val); + + do { + if (!(mtk_hw_read32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_AP_MISC_RSTR_CLR)= & + DPMAIF_MEM_CLR_MASK)) + break; + + cnt++; + udelay(POLL_INTERVAL_US); + } while (cnt < POLL_MAX_TIMES); + + if (cnt >=3D POLL_MAX_TIMES) { + dev_err(DRV_TO_MDEV(drv_info)->dev, "Failed to initialize sram.\n"); + return false; + } + return ret; +} + +static bool mtk_dpmaif_drv_config(struct dpmaif_drv_info *drv_info) +{ + u32 val; + + mtk_dpmaif_drv_reset(drv_info); + + if (!mtk_dpmaif_drv_sram_init(drv_info)) + return false; + + val =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_RDY_CHK_THRES); + val &=3D ~DPMAIF_PORT_MODE_MSK; + val |=3D DPMAIF_PORT_MODE_PCIE; + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_RDY_CHK_THRES, val); + + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_AP_MISC_CG_EN, 0x7f); + return true; +} + +static bool mtk_dpmaif_drv_init_intr(struct dpmaif_drv_info *drv_info) +{ + struct dpmaif_drv_irq_en_mask *irq_en_mask; + u32 cnt =3D 0, cfg; + + irq_en_mask =3D &drv_info->drv_irq_en_mask; + + irq_en_mask->ap_ul_l2intr_en_mask =3D DPMAIF_AP_UL_L2INTR_EN_MASK; + + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_UL_L2TISAR0, 0xFFFFFFF= F); + + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_UL_L2TICR0, + irq_en_mask->ap_ul_l2intr_en_mask); + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_UL_L2TISR0, + ~(irq_en_mask->ap_ul_l2intr_en_mask)); + mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_UL_L2TISR0); + + do { + if (!((mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_UL_L2TIMR0) & + irq_en_mask->ap_ul_l2intr_en_mask) =3D=3D irq_en_mask->ap_ul_l2intr= _en_mask)) + break; + + cnt++; + udelay(POLL_INTERVAL_US); + } while (cnt < POLL_MAX_TIMES); + + if (cnt >=3D POLL_MAX_TIMES) { + dev_err(DRV_TO_MDEV(drv_info)->dev, "Failed to set UL interrupt mask.\n"= ); + return false; + } + + irq_en_mask->ap_dl_l2intr_en_mask =3D DPMAIF_AP_DL_L2INTR_EN_MASK; + + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_DL_L2TISAR0, 0xFFFFFFF= F); + + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_DL_L2TISR0, + ~(irq_en_mask->ap_dl_l2intr_en_mask)); + mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_DL_L2TISR0); + + cnt =3D 0; + do { + if (!((mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_DL_L2TIMR0) & + irq_en_mask->ap_dl_l2intr_en_mask) =3D=3D irq_en_mask->ap_dl_l2intr_= en_mask)) + break; + + cnt++; + udelay(POLL_INTERVAL_US); + } while (cnt < POLL_MAX_TIMES); + + if (cnt >=3D POLL_MAX_TIMES) { + dev_err(DRV_TO_MDEV(drv_info)->dev, "Failed to set DL interrupt mask\n"); + return false; + } + + irq_en_mask->ap_udl_ip_busy_en_mask =3D DPMAIF_AP_UDL_IP_BUSY_EN_MASK; + + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_IP_BUSY, 0xFFFFFFFF); + + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_DLUL_IP_BUSY_MASK, + irq_en_mask->ap_udl_ip_busy_en_mask); + + cfg =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_AO_UL_AP_L1TIMR0= ); + cfg |=3D DPMAIF_DL_INT_Q2APTOP_MSK | DPMAIF_DL_INT_Q2TOQ1_MSK | DPMAIF_UL= _TOP0_INT_MSK; + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_AO_UL_AP_L1TIMR0, cfg); + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_HPC_INTR_MASK, 0xffff); + + dev_info(DRV_TO_MDEV(drv_info)->dev, + "ul_mask=3D0x%08x, dl_mask=3D0x%08x, busy_mask=3D0x%08x\n", + irq_en_mask->ap_ul_l2intr_en_mask, + irq_en_mask->ap_dl_l2intr_en_mask, + irq_en_mask->ap_udl_ip_busy_en_mask); + return true; +} + +static void mtk_dpmaif_drv_set_property(struct dpmaif_drv_info *drv_info, + struct dpmaif_drv_cfg *drv_cfg) +{ + struct dpmaif_drv_property *drv_property =3D &drv_info->drv_property; + struct dpmaif_drv_data_ring *ring; + struct dpmaif_drv_dlq *dlq; + struct dpmaif_drv_ulq *ulq; + u32 i; + + drv_property->features =3D drv_cfg->features; + + for (i =3D 0; i < DPMAIF_DLQ_NUM; i++) { + dlq =3D &drv_property->dlq[i]; + dlq->pit_base =3D drv_cfg->pit_base[i]; + dlq->pit_size =3D drv_cfg->pit_cnt[i]; + dlq->q_started =3D true; + } + + for (i =3D 0; i < DPMAIF_ULQ_NUM; i++) { + ulq =3D &drv_property->ulq[i]; + ulq->drb_base =3D drv_cfg->drb_base[i]; + ulq->drb_size =3D drv_cfg->drb_cnt[i]; + ulq->q_started =3D true; + } + + ring =3D &drv_property->ring; + + ring->normal_bat_base =3D drv_cfg->normal_bat_base; + ring->normal_bat_size =3D drv_cfg->normal_bat_cnt; + ring->normal_bat_pkt_bufsz =3D drv_cfg->normal_bat_buf_size; + ring->normal_bat_remain_size =3D DPMAIF_HW_BAT_REMAIN; + ring->normal_bat_rsv_length =3D DPMAIF_HW_BAT_RSVLEN; + ring->chk_normal_bat_num =3D DPMAIF_HW_CHK_BAT_NUM; + + ring->mtu =3D drv_cfg->max_mtu; + ring->pkt_bid_max_cnt =3D DPMAIF_HW_PKT_BIDCNT; + ring->pkt_alignment =3D DPMAIF_HW_PKT_ALIGN; + ring->chk_pit_num =3D DPMAIF_HW_CHK_PIT_NUM; +} + +static void mtk_dpmaif_drv_init_common_hw(struct dpmaif_drv_info *drv_info) +{ + u32 val; + + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_UL_RESERVE_AO_RW, + DPMAIF_PCIE_MODE_SET_VALUE); + + val =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_DL_BAT_INIT_CON1); + val |=3D DPMAIF_DL_BAT_CACHE_PRI; + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_DL_BAT_INIT_CON1, val); + + val =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_RDY_CHK_THRES); + val |=3D DPMAIF_DL_BURST_PIT_EN; + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_RDY_CHK_THRES, val); +} + +static void mtk_dpmaif_drv_set_hpc_cntl(struct dpmaif_drv_info *drv_info) +{ + u32 cfg =3D 0; + + cfg =3D (DPMAIF_HPC_LRO_PATH_DF & 0x3) << 0; + cfg |=3D (DPMAIF_HPC_ADD_MODE_DF & 0x3) << 2; + cfg |=3D (DPMAIF_HASH_PRIME_DF & 0xf) << 4; + cfg |=3D (DPMAIF_HPC_TOTAL_NUM & 0xff) << 8; + + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_AO_DL_HPC_CNTL, cfg); +} + +static void mtk_dpmaif_drv_set_agg_cfg(struct dpmaif_drv_info *drv_info) +{ + u32 cfg; + + cfg =3D (DPMAIF_AGG_MAX_LEN_DF & 0xffff) << 0; + cfg |=3D (DPMAIF_AGG_TBL_ENT_NUM_DF & 0xffff) << 16; + + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_AO_DL_LRO_AGG_CFG, cfg); + + cfg =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_AO_DL_RDY_CHK_FR= G_THRES); + + mtk_hw_write32(DRV_TO_MDEV(drv_info), + NRL2_DPMAIF_AO_DL_RDY_CHK_FRG_THRES, cfg & 0xf00fffff); +} + +static void mtk_dpmaif_drv_set_hash_bit_choose(struct dpmaif_drv_info *drv= _info) +{ + u32 cfg; + + cfg =3D (DPMAIF_LRO_HASH_BIT_CHOOSE_DF & 0x7) << 0; + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_AO_DL_LROPIT_INIT_CON5,= cfg); +} + +static void mtk_dpmaif_drv_set_mid_pit_timeout_threshold(struct dpmaif_drv= _info *drv_info) +{ + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_AO_DL_LROPIT_TIMEOUT0, + DPMAIF_MID_TIMEOUT_THRES_DF); +} + +static void mtk_dpmaif_drv_set_dlq_timeout_threshold(struct dpmaif_drv_inf= o *drv_info) +{ + u32 val, i; + + for (i =3D 0; i < DPMAIF_HPC_MAX_TOTAL_NUM; i++) { + val =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), + NRL2_DPMAIF_AO_DL_LROPIT_TIMEOUT1 + 4 * (i / 2)); + + if (i % 2) + val =3D (val & 0xFFFF) | (DPMAIF_LRO_TIMEOUT_THRES_DF << 16); + else + val =3D (val & 0xFFFF0000) | (DPMAIF_LRO_TIMEOUT_THRES_DF); + + mtk_hw_write32(DRV_TO_MDEV(drv_info), + NRL2_DPMAIF_AO_DL_LROPIT_TIMEOUT1 + (4 * (i / 2)), val); + } +} + +static void mtk_dpmaif_drv_set_dlq_start_prs_threshold(struct dpmaif_drv_i= nfo *drv_info) +{ + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_AO_DL_LROPIT_TRIG_THRES, + DPMAIF_LRO_PRS_THRES_DF & 0x3FFFF); +} + +static void mtk_dpmaif_drv_init_dl_hpc_hw(struct dpmaif_drv_info *drv_info) +{ + mtk_dpmaif_drv_set_hpc_cntl(drv_info); + mtk_dpmaif_drv_set_agg_cfg(drv_info); + mtk_dpmaif_drv_set_hash_bit_choose(drv_info); + mtk_dpmaif_drv_set_mid_pit_timeout_threshold(drv_info); + mtk_dpmaif_drv_set_dlq_timeout_threshold(drv_info); + mtk_dpmaif_drv_set_dlq_start_prs_threshold(drv_info); +} + +static void mtk_dpmaif_drv_dl_set_ao_remain_minsz(struct dpmaif_drv_info *= drv_info, u32 sz) +{ + u32 val; + + val =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_PKTINFO_CONO); + val &=3D ~DPMAIF_BAT_REMAIN_MINSZ_MSK; + val |=3D ((sz / DPMAIF_BAT_REMAIN_SZ_BASE) << 8) & DPMAIF_BAT_REMAIN_MINS= Z_MSK; + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_PKTINFO_CONO, val); +} + +static void mtk_dpmaif_drv_dl_set_ao_bat_bufsz(struct dpmaif_drv_info *drv= _info, u32 buf_sz) +{ + u32 val; + + val =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_PKTINFO_CON2); + val &=3D ~DPMAIF_BAT_BUF_SZ_MSK; + val |=3D ((buf_sz / DPMAIF_BAT_BUFFER_SZ_BASE) << 8) & DPMAIF_BAT_BUF_SZ_= MSK; + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_PKTINFO_CON2, val); +} + +static void mtk_dpmaif_drv_dl_set_ao_bat_rsv_length(struct dpmaif_drv_info= *drv_info, u32 length) +{ + u32 val; + + val =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_PKTINFO_CON2); + val &=3D ~DPMAIF_BAT_RSV_LEN_MSK; + val |=3D length & DPMAIF_BAT_RSV_LEN_MSK; + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_PKTINFO_CON2, val); +} + +static void mtk_dpmaif_drv_dl_set_ao_bid_maxcnt(struct dpmaif_drv_info *dr= v_info, u32 cnt) +{ + u32 val; + + val =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_PKTINFO_CONO); + val &=3D ~DPMAIF_BAT_BID_MAXCNT_MSK; + val |=3D (cnt << 16) & DPMAIF_BAT_BID_MAXCNT_MSK; + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_PKTINFO_CONO, val); +} + +static void mtk_dpmaif_drv_dl_set_pkt_alignment(struct dpmaif_drv_info *dr= v_info, + bool enable, u32 mode) +{ + u32 val; + + val =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_RDY_CHK_THRES); + val &=3D ~DPMAIF_PKT_ALIGN_MSK; + if (enable) { + val |=3D DPMAIF_PKT_ALIGN_EN; + val |=3D (mode << 22) & DPMAIF_PKT_ALIGN_MSK; + } + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_RDY_CHK_THRES, val); +} + +static void mtk_dpmaif_drv_dl_set_pit_seqnum(struct dpmaif_drv_info *drv_i= nfo, u32 seq) +{ + u32 val; + + val =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_AO_DL_PIT_SEQ_EN= D); + val &=3D ~DPMAIF_DL_PIT_SEQ_MSK; + val |=3D seq & DPMAIF_DL_PIT_SEQ_MSK; + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_AO_DL_PIT_SEQ_END, val); +} + +static void mtk_dpmaif_drv_dl_set_ao_mtu(struct dpmaif_drv_info *drv_info,= u32 mtu_sz) +{ + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_PKTINFO_CON1, mtu_sz); +} + +static void mtk_dpmaif_drv_dl_set_ao_pit_chknum(struct dpmaif_drv_info *dr= v_info, u32 number) +{ + u32 val; + + val =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_PKTINFO_CON2); + val &=3D ~DPMAIF_PIT_CHK_NUM_MSK; + val |=3D (number << 24) & DPMAIF_PIT_CHK_NUM_MSK; + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_PKTINFO_CON2, val); +} + +static void mtk_dpmaif_drv_dl_set_ao_bat_check_threshold(struct dpmaif_drv= _info *drv_info, u32 size) +{ + u32 val; + + val =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_RDY_CHK_THRES); + val &=3D ~DPMAIF_BAT_CHECK_THRES_MSK; + val |=3D (size << 16) & DPMAIF_BAT_CHECK_THRES_MSK; + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_RDY_CHK_THRES, val); +} + +static void mtk_dpmaif_drv_dl_set_bat_base_addr(struct dpmaif_drv_info *dr= v_info, u64 addr) +{ + u32 lb_addr =3D (u32)(addr & 0xFFFFFFFF); + u32 hb_addr =3D (u32)(addr >> 32); + + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_DL_BAT_INIT_CON0, lb_addr= ); + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_DL_BAT_INIT_CON3, hb_addr= ); +} + +static void mtk_dpmaif_drv_dl_set_bat_size(struct dpmaif_drv_info *drv_inf= o, u32 size) +{ + u32 val; + + val =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_DL_BAT_INIT_CON1); + val &=3D ~DPMAIF_BAT_SIZE_MSK; + val |=3D size & DPMAIF_BAT_SIZE_MSK; + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_DL_BAT_INIT_CON1, val); +} + +static void mtk_dpmaif_drv_dl_bat_en(struct dpmaif_drv_info *drv_info, boo= l enable) +{ + u32 val; + + val =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_DL_BAT_INIT_CON1); + if (enable) + val |=3D DPMAIF_BAT_EN_MSK; + else + val &=3D ~DPMAIF_BAT_EN_MSK; + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_DL_BAT_INIT_CON1, val); +} + +static void mtk_dpmaif_drv_dl_bat_init_done(struct dpmaif_drv_info *drv_in= fo, bool frag_en) +{ + u32 cnt =3D 0, dl_bat_init; + + dl_bat_init =3D DPMAIF_DL_BAT_INIT_ALLSET; + dl_bat_init |=3D DPMAIF_DL_BAT_INIT_EN; + + do { + if (!(mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_DL_BAT_INIT) & + DPMAIF_DL_BAT_INIT_NOT_READY)) { + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_DL_BAT_INIT, dl_bat_ini= t); + break; + } + cnt++; + udelay(POLL_INTERVAL_US); + } while (cnt < POLL_MAX_TIMES); + + if (cnt >=3D POLL_MAX_TIMES) { + dev_err(DRV_TO_MDEV(drv_info)->dev, "Failed to initialize bat.\n"); + return; + } + + cnt =3D 0; + do { + if (!((mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_DL_BAT_INIT) & + DPMAIF_DL_BAT_INIT_NOT_READY) =3D=3D DPMAIF_DL_BAT_INIT_NOT_READY)) + break; + + cnt++; + udelay(POLL_INTERVAL_US); + } while (cnt < POLL_MAX_TIMES); + + if (cnt >=3D POLL_MAX_TIMES) { + dev_err(DRV_TO_MDEV(drv_info)->dev, "Initialize bat is not ready.\n"); + return; + } +} + +static void mtk_dpmaif_drv_dl_set_pit_base_addr(struct dpmaif_drv_info *dr= v_info, u64 addr) +{ + u32 lb_addr =3D (u32)(addr & 0xFFFFFFFF); + u32 hb_addr =3D (u32)(addr >> 32); + + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_DL_LROPIT_INIT_CON0, lb= _addr); + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_DL_LROPIT_INIT_CON4, hb= _addr); +} + +static void mtk_dpmaif_drv_dl_set_pit_size(struct dpmaif_drv_info *drv_inf= o, u32 size) +{ + u32 val; + + val =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_DL_LROPIT_INIT_C= ON1); + val &=3D ~DPMAIF_PIT_SIZE_MSK; + val |=3D size & DPMAIF_PIT_SIZE_MSK; + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_DL_LROPIT_INIT_CON1, va= l); + + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_DL_LROPIT_INIT_CON2, 0); + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_DL_LROPIT_INIT_CON3, 0); + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_DL_LROPIT_INIT_CON5, 0); + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_DL_LROPIT_INIT_CON6, 0); +} + +static void mtk_dpmaif_drv_dl_pit_en(struct dpmaif_drv_info *drv_info, boo= l enable) +{ + u32 val; + + val =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_DL_LROPIT_INIT_C= ON3); + if (enable) + val |=3D DPMAIF_LROPIT_EN_MSK; + else + val &=3D ~DPMAIF_LROPIT_EN_MSK; + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_DL_LROPIT_INIT_CON3, va= l); +} + +static void mtk_dpmaif_drv_dl_pit_init_done(struct dpmaif_drv_info *drv_in= fo, u32 pit_idx) +{ + int cnt =3D 0, dl_pit_init; + + dl_pit_init =3D DPMAIF_DL_PIT_INIT_ALLSET; + dl_pit_init |=3D pit_idx << DPMAIF_LROPIT_CHAN_OFS; + dl_pit_init |=3D DPMAIF_DL_PIT_INIT_EN; + + do { + if (!(mtk_hw_read32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_DL_LROPIT_INIT) & + DPMAIF_DL_PIT_INIT_NOT_READY)) { + mtk_hw_write32(DRV_TO_MDEV(drv_info), + NRL2_DPMAIF_DL_LROPIT_INIT, dl_pit_init); + break; + } + udelay(POLL_INTERVAL_US); + cnt++; + } while (cnt < POLL_MAX_TIMES); + + if (cnt >=3D POLL_MAX_TIMES) { + dev_err(DRV_TO_MDEV(drv_info)->dev, "Failed to initialize pit.\n"); + return; + } + + cnt =3D 0; + do { + if (!((mtk_hw_read32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_DL_LROPIT_INIT) & + DPMAIF_DL_PIT_INIT_NOT_READY) =3D=3D DPMAIF_DL_PIT_INIT_NOT_READY)) + break; + + udelay(POLL_INTERVAL_US); + cnt++; + } while (cnt < POLL_MAX_TIMES); + + if (cnt >=3D POLL_MAX_TIMES) { + dev_err(DRV_TO_MDEV(drv_info)->dev, "Initialize pit is not ready.\n"); + return; + } +} + +static void mtk_dpmaif_drv_config_dlq_pit_hw(struct dpmaif_drv_info *drv_i= nfo, u8 q_num, + struct dpmaif_drv_dlq *dlq) +{ + mtk_dpmaif_drv_dl_set_pit_base_addr(drv_info, (u64)dlq->pit_base); + mtk_dpmaif_drv_dl_set_pit_size(drv_info, dlq->pit_size); + mtk_dpmaif_drv_dl_pit_en(drv_info, true); + mtk_dpmaif_drv_dl_pit_init_done(drv_info, q_num); +} + +static int mtk_dpmaif_drv_dlq_all_en(struct dpmaif_drv_info *drv_info, boo= l enable) +{ + u32 val, dl_bat_init, cnt =3D 0; + + val =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_DL_BAT_INIT_CON1); + + if (enable) + val |=3D DPMAIF_BAT_EN_MSK; + else + val &=3D ~DPMAIF_BAT_EN_MSK; + + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_DL_BAT_INIT_CON1, val); + mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_DL_BAT_INIT_CON1); + + dl_bat_init =3D DPMAIF_DL_BAT_INIT_ONLY_ENABLE_BIT; + dl_bat_init |=3D DPMAIF_DL_BAT_INIT_EN; + + do { + if (!(mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_DL_BAT_INIT) & + DPMAIF_DL_BAT_INIT_NOT_READY)) { + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_DL_BAT_INIT, dl_bat_ini= t); + break; + } + udelay(POLL_INTERVAL_US); + cnt++; + } while (cnt < POLL_MAX_TIMES); + + if (cnt >=3D POLL_MAX_TIMES) { + dev_err(DRV_TO_MDEV(drv_info)->dev, "Failed to enable all dl queue.\n"); + return -DATA_HW_REG_TIMEOUT; + } + + cnt =3D 0; + do { + if (!((mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_DL_BAT_INIT) & + DPMAIF_DL_BAT_INIT_NOT_READY) =3D=3D DPMAIF_DL_BAT_INIT_NOT_READY)) + break; + + udelay(POLL_INTERVAL_US); + cnt++; + } while (cnt < POLL_MAX_TIMES); + + if (cnt >=3D POLL_MAX_TIMES) { + dev_err(DRV_TO_MDEV(drv_info)->dev, "Enable all dl queue is not ready.\n= "); + return -DATA_HW_REG_TIMEOUT; + } + + return 0; +} + +static bool mtk_dpmaif_drv_dl_idle_check(struct dpmaif_drv_info *drv_info) +{ + bool is_idle =3D false; + u32 dl_dbg_sta; + + dl_dbg_sta =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_DL_DBG_STA1= ); + + if ((dl_dbg_sta & DPMAIF_DL_IDLE_STS) =3D=3D DPMAIF_DL_IDLE_STS) + is_idle =3D true; + + return is_idle; +} + +static u32 mtk_dpmaif_drv_dl_get_wridx(struct dpmaif_drv_info *drv_info) +{ + return ((mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_PIT_STA3)) & + DPMAIF_DL_PIT_WRIDX_MSK); +} + +static u32 mtk_dpmaif_drv_dl_get_pit_ridx(struct dpmaif_drv_info *drv_info) +{ + return ((mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_PIT_STA2)) & + DPMAIF_DL_PIT_WRIDX_MSK); +} + +static void mtk_dpmaif_drv_dl_set_pkt_checksum(struct dpmaif_drv_info *drv= _info) +{ + u32 val; + + val =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_RDY_CHK_THRES); + val |=3D DPMAIF_DL_PKT_CHECKSUM_EN; + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_RDY_CHK_THRES, val); +} + +static bool mtk_dpmaif_drv_config_dlq_hw(struct dpmaif_drv_info *drv_info) +{ + struct dpmaif_drv_property *drv_property =3D &drv_info->drv_property; + struct dpmaif_drv_data_ring *ring =3D &drv_property->ring; + struct dpmaif_drv_dlq *dlq; + u32 i; + + mtk_dpmaif_drv_init_dl_hpc_hw(drv_info); + mtk_dpmaif_drv_dl_set_ao_remain_minsz(drv_info, ring->normal_bat_remain_s= ize); + mtk_dpmaif_drv_dl_set_ao_bat_bufsz(drv_info, ring->normal_bat_pkt_bufsz); + mtk_dpmaif_drv_dl_set_ao_bat_rsv_length(drv_info, ring->normal_bat_rsv_le= ngth); + mtk_dpmaif_drv_dl_set_ao_bid_maxcnt(drv_info, ring->pkt_bid_max_cnt); + + if (ring->pkt_alignment =3D=3D 64) + mtk_dpmaif_drv_dl_set_pkt_alignment(drv_info, true, DPMAIF_PKT_ALIGN64_M= ODE); + else if (ring->pkt_alignment =3D=3D 128) + mtk_dpmaif_drv_dl_set_pkt_alignment(drv_info, true, DPMAIF_PKT_ALIGN128_= MODE); + else + mtk_dpmaif_drv_dl_set_pkt_alignment(drv_info, false, 0); + + mtk_dpmaif_drv_dl_set_pit_seqnum(drv_info, DPMAIF_PIT_SEQ_MAX); + mtk_dpmaif_drv_dl_set_ao_mtu(drv_info, ring->mtu); + mtk_dpmaif_drv_dl_set_ao_pit_chknum(drv_info, ring->chk_pit_num); + mtk_dpmaif_drv_dl_set_ao_bat_check_threshold(drv_info, ring->chk_normal_b= at_num); + + mtk_dpmaif_drv_dl_set_bat_base_addr(drv_info, (u64)ring->normal_bat_base); + mtk_dpmaif_drv_dl_set_bat_size(drv_info, ring->normal_bat_size); + mtk_dpmaif_drv_dl_bat_en(drv_info, false); + mtk_dpmaif_drv_dl_bat_init_done(drv_info, false); + + for (i =3D 0; i < DPMAIF_DLQ_NUM; i++) { + dlq =3D &drv_property->dlq[i]; + mtk_dpmaif_drv_config_dlq_pit_hw(drv_info, i, dlq); + } + + if (mtk_dpmaif_drv_dlq_all_en(drv_info, true)) + return false; + mtk_dpmaif_drv_dl_set_pkt_checksum(drv_info); + return true; +} + +static void mtk_dpmaif_drv_ul_update_drb_size(struct dpmaif_drv_info *drv_= info, u8 q_num, u32 size) +{ + u32 old_size; + u64 addr; + + addr =3D DPMAIF_UL_DRBSIZE_ADDRH_N(q_num); + + old_size =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), addr); + old_size &=3D ~DPMAIF_DRB_SIZE_MSK; + old_size |=3D size & DPMAIF_DRB_SIZE_MSK; + mtk_hw_write32(DRV_TO_MDEV(drv_info), addr, old_size); +} + +static void mtk_dpmaif_drv_ul_update_drb_base_addr(struct dpmaif_drv_info = *drv_info, + u8 q_num, u64 addr) +{ + u32 lb_addr =3D (u32)(addr & 0xFFFFFFFF); + u32 hb_addr =3D (u32)(addr >> 32); + + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_ULQSAR_N(q_num), lb_addr); + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_UL_DRB_ADDRH_N(q_num), hb_ad= dr); +} + +static void mtk_dpmaif_drv_ul_rdy_en(struct dpmaif_drv_info *drv_info, u8 = q_num, bool ready) +{ + u32 ul_rdy_en; + + ul_rdy_en =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_UL_CHNL_ARB0= ); + if (ready) + ul_rdy_en |=3D (1 << q_num); + else + ul_rdy_en &=3D ~(1 << q_num); + + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_UL_CHNL_ARB0, ul_rdy_en); +} + +static void mtk_dpmaif_drv_ul_arb_en(struct dpmaif_drv_info *drv_info, u8 = q_num, bool enable) +{ + u32 ul_arb_en; + + ul_arb_en =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_UL_CHNL_ARB0= ); + if (enable) + ul_arb_en |=3D (1 << (q_num + 8)); + else + ul_arb_en &=3D ~(1 << (q_num + 8)); + + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_UL_CHNL_ARB0, ul_arb_en); +} + +static void mtk_dpmaif_drv_config_ulq_hw(struct dpmaif_drv_info *drv_info) +{ + struct dpmaif_drv_ulq *ulq; + u32 i; + + for (i =3D 0; i < DPMAIF_ULQ_NUM; i++) { + ulq =3D &drv_info->drv_property.ulq[i]; + mtk_dpmaif_drv_ul_update_drb_size(drv_info, i, + (ulq->drb_size * DPMAIF_UL_DRB_ENTRY_WORD)); + mtk_dpmaif_drv_ul_update_drb_base_addr(drv_info, i, (u64)ulq->drb_base); + mtk_dpmaif_drv_ul_rdy_en(drv_info, i, true); + mtk_dpmaif_drv_ul_arb_en(drv_info, i, true); + } +} + +static bool mtk_dpmaif_drv_init_done(struct dpmaif_drv_info *drv_info) +{ + u32 val, cnt =3D 0; + + val =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_AP_MISC_OVERWRIT= E_CFG); + val |=3D DPMAIF_SRAM_SYNC_MASK; + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_AP_MISC_OVERWRITE_CFG, = val); + do { + if (!(mtk_hw_read32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_AP_MISC_OVERWRITE= _CFG) & + DPMAIF_SRAM_SYNC_MASK)) + break; + + udelay(POLL_INTERVAL_US); + cnt++; + } while (cnt < POLL_MAX_TIMES); + + if (cnt >=3D POLL_MAX_TIMES) { + dev_err(DRV_TO_MDEV(drv_info)->dev, "Failed to sync default value to sra= m\n"); + return false; + } + + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_AO_UL_INIT_SET, DPMAIF_= UL_INIT_DONE_MASK); + + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_AO_DL_INIT_SET, DPMAIF_= DL_INIT_DONE_MASK); + return true; +} + +static bool mtk_dpmaif_drv_cfg_hw(struct dpmaif_drv_info *drv_info) +{ + mtk_dpmaif_drv_init_common_hw(drv_info); + if (!mtk_dpmaif_drv_config_dlq_hw(drv_info)) + return false; + mtk_dpmaif_drv_config_ulq_hw(drv_info); + if (!mtk_dpmaif_drv_init_done(drv_info)) + return false; + + drv_info->ulq_all_enable =3D true; + drv_info->dlq_all_enable =3D true; + + return true; +} + +static void mtk_dpmaif_drv_clr_ul_all_intr(struct dpmaif_drv_info *drv_inf= o) +{ + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_UL_L2TISAR0, 0xFFFFFFF= F); +} + +static void mtk_dpmaif_drv_clr_dl_all_intr(struct dpmaif_drv_info *drv_inf= o) +{ + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_DL_L2TISAR0, 0xFFFFFFF= F); +} + +static int mtk_dpmaif_drv_init_t800(struct dpmaif_drv_info *drv_info, void= *data) +{ + struct dpmaif_drv_cfg *drv_cfg =3D data; + + if (!drv_cfg) { + dev_err(DRV_TO_MDEV(drv_info)->dev, "Invalid parameter\n"); + return -DATA_FLOW_CHK_ERR; + } + + if (!mtk_dpmaif_drv_config(drv_info)) + return DATA_HW_REG_CHK_FAIL; + + if (!mtk_dpmaif_drv_init_intr(drv_info)) + return DATA_HW_REG_CHK_FAIL; + + mtk_dpmaif_drv_set_property(drv_info, drv_cfg); + + if (!mtk_dpmaif_drv_cfg_hw(drv_info)) + return DATA_HW_REG_CHK_FAIL; + + mtk_dpmaif_drv_clr_ul_all_intr(drv_info); + mtk_dpmaif_drv_clr_dl_all_intr(drv_info); + + return 0; +} + +static int mtk_dpmaif_drv_ulq_all_en(struct dpmaif_drv_info *drv_info, boo= l enable) +{ + u32 ul_arb_en; + + ul_arb_en =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_UL_CHNL_ARB0= ); + if (enable) + ul_arb_en |=3D DPMAIF_UL_ALL_QUE_ARB_EN; + else + ul_arb_en &=3D ~DPMAIF_UL_ALL_QUE_ARB_EN; + + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_UL_CHNL_ARB0, ul_arb_en); + mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_UL_CHNL_ARB0); + + return 0; +} + +static bool mtk_dpmaif_drv_ul_all_idle_check(struct dpmaif_drv_info *drv_i= nfo) +{ + bool is_idle =3D false; + u32 ul_dbg_sta; + + ul_dbg_sta =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_UL_DBG_STA2= ); + /* If all the queues are idle, UL idle is true. */ + if ((ul_dbg_sta & DPMAIF_UL_IDLE_STS_MSK) =3D=3D DPMAIF_UL_IDLE_STS) + is_idle =3D true; + + return is_idle; +} + +static int mtk_dpmaif_drv_unmask_ulq_intr(struct dpmaif_drv_info *drv_info= , u32 q_num) +{ + u32 ui_que_done_mask; + + ui_que_done_mask =3D (1 << (q_num + DP_UL_INT_DONE_OFFSET)) & DPMAIF_UL_I= NT_QDONE_MSK; + drv_info->drv_irq_en_mask.ap_ul_l2intr_en_mask |=3D ui_que_done_mask; + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_UL_L2TICR0, ui_que_don= e_mask); + + return 0; +} + +static int mtk_dpmaif_drv_ul_unmask_all_tx_done_intr(struct dpmaif_drv_inf= o *drv_info) +{ + int ret; + u8 i; + + for (i =3D 0; i < DPMAIF_ULQ_NUM; i++) { + ret =3D mtk_dpmaif_drv_unmask_ulq_intr(drv_info, i); + if (ret < 0) + break; + } + + return ret; +} + +static int mtk_dpmaif_drv_dl_unmask_rx_done_intr(struct dpmaif_drv_info *d= rv_info, u8 qno) +{ + u32 di_que_done_mask; + + if (qno =3D=3D DPMAIF_DLQ0) + di_que_done_mask =3D DPMAIF_DL_INT_DLQ0_QDONE_MSK; + else + di_que_done_mask =3D DPMAIF_DL_INT_DLQ1_QDONE_MSK; + + drv_info->drv_irq_en_mask.ap_dl_l2intr_en_mask |=3D di_que_done_mask; + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_DL_L2TICR0, di_que_don= e_mask); + + return 0; +} + +static int mtk_dpmaif_drv_dl_unmask_all_rx_done_intr(struct dpmaif_drv_inf= o *drv_info) +{ + int ret; + u8 i; + + for (i =3D 0; i < DPMAIF_DLQ_NUM; i++) { + ret =3D mtk_dpmaif_drv_dl_unmask_rx_done_intr(drv_info, i); + if (ret < 0) + break; + } + + return ret; +} + +static int mtk_dpmaif_drv_dlq_mask_rx_done_intr(struct dpmaif_drv_info *dr= v_info, u8 qno) +{ + u32 cnt =3D 0, di_que_done_mask; + + if (qno =3D=3D DPMAIF_DLQ0) + di_que_done_mask =3D DPMAIF_DL_INT_DLQ0_QDONE_MSK; + else + di_que_done_mask =3D DPMAIF_DL_INT_DLQ1_QDONE_MSK; + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_DL_L2TISR0, di_que_don= e_mask); + mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_DL_L2TISR0); + + do { + if (!((mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_DL_L2TIMR0) & + di_que_done_mask) !=3D di_que_done_mask)) + break; + + dev_err(DRV_TO_MDEV(drv_info)->dev, "Failed to mask dlq%u interrupt done= -0x%08x\n", + qno, mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_DL_L2TIMR0)); + udelay(POLL_INTERVAL_US); + cnt++; + } while (cnt < POLL_MAX_TIMES); + + if (cnt >=3D POLL_MAX_TIMES) { + dev_err(DRV_TO_MDEV(drv_info)->dev, "Failed to mask dlq0 interrupt done\= n"); + return -DATA_HW_REG_TIMEOUT; + } + + drv_info->drv_irq_en_mask.ap_dl_l2intr_en_mask &=3D ~di_que_done_mask; + + return 0; +} + +static int mtk_dpmaif_drv_dl_mask_all_rx_done_intr(struct dpmaif_drv_info = *drv_info) +{ + int ret; + u8 i; + + for (i =3D 0; i < DPMAIF_DLQ_NUM; i++) { + ret =3D mtk_dpmaif_drv_dlq_mask_rx_done_intr(drv_info, i); + if (ret < 0) + break; + } + + return ret; +} + +static int mtk_dpmaif_drv_start_queue_t800(struct dpmaif_drv_info *drv_inf= o, + enum dpmaif_drv_dir dir) +{ + int ret; + + if (dir =3D=3D DPMAIF_TX) { + if (unlikely(drv_info->ulq_all_enable)) { + dev_info(DRV_TO_MDEV(drv_info)->dev, "ulq all enabled\n"); + return 0; + } + + ret =3D mtk_dpmaif_drv_ulq_all_en(drv_info, true); + if (ret < 0) + return ret; + + ret =3D mtk_dpmaif_drv_ul_unmask_all_tx_done_intr(drv_info); + if (ret < 0) + return ret; + + drv_info->ulq_all_enable =3D true; + } else { + if (unlikely(drv_info->dlq_all_enable)) { + dev_info(DRV_TO_MDEV(drv_info)->dev, "dlq all enabled\n"); + return 0; + } + + ret =3D mtk_dpmaif_drv_dlq_all_en(drv_info, true); + if (ret < 0) + return ret; + + ret =3D mtk_dpmaif_drv_dl_unmask_all_rx_done_intr(drv_info); + if (ret < 0) + return ret; + + drv_info->dlq_all_enable =3D true; + } + + return 0; +} + +static int mtk_dpmaif_drv_stop_ulq(struct dpmaif_drv_info *drv_info) +{ + int cnt =3D 0; + + mtk_dpmaif_drv_ulq_all_en(drv_info, false); + do { + if (++cnt >=3D POLL_MAX_TIMES) { + dev_err(DRV_TO_MDEV(drv_info)->dev, "Failed to stop ul queue, 0x%x\n", + mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_UL_DBG_STA2)); + return -DATA_HW_REG_TIMEOUT; + } + udelay(POLL_INTERVAL_US); + } while (!mtk_dpmaif_drv_ul_all_idle_check(drv_info)); + + return 0; +} + +static int mtk_dpmaif_drv_mask_ulq_intr(struct dpmaif_drv_info *drv_info, = u32 q_num) +{ + u32 cnt =3D 0, ui_que_done_mask; + + ui_que_done_mask =3D (1 << (q_num + DP_UL_INT_DONE_OFFSET)) & DPMAIF_UL_I= NT_QDONE_MSK; + + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_UL_L2TISR0, ui_que_don= e_mask); + mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_UL_L2TISR0); + + do { + if (!((mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_UL_L2TIMR0) & + ui_que_done_mask) !=3D ui_que_done_mask)) + break; + + dev_err(DRV_TO_MDEV(drv_info)->dev, + "Failed to mask ul%u interrupt done-0x%08x\n", q_num, + mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_UL_L2TIMR0)); + udelay(POLL_INTERVAL_US); + cnt++; + } while (cnt < POLL_MAX_TIMES); + + if (cnt >=3D POLL_MAX_TIMES) { + dev_err(DRV_TO_MDEV(drv_info)->dev, "Failed to mask dlq0 interrupt done\= n"); + return -DATA_HW_REG_TIMEOUT; + } + drv_info->drv_irq_en_mask.ap_ul_l2intr_en_mask &=3D ~ui_que_done_mask; + + return 0; +} + +static void mtk_dpmaif_drv_ul_mask_multi_tx_done_intr(struct dpmaif_drv_in= fo *drv_info, u8 q_mask) +{ + u32 i; + + for (i =3D 0; i < DPMAIF_ULQ_NUM; i++) { + if (q_mask & (1 << i)) + mtk_dpmaif_drv_mask_ulq_intr(drv_info, i); + } +} + +static int mtk_dpmaif_drv_ul_mask_all_tx_done_intr(struct dpmaif_drv_info = *drv_info) +{ + int ret; + u8 i; + + for (i =3D 0; i < DPMAIF_ULQ_NUM; i++) { + ret =3D mtk_dpmaif_drv_mask_ulq_intr(drv_info, i); + if (ret < 0) + break; + } + + return ret; +} + +static int mtk_dpmaif_drv_stop_dlq(struct dpmaif_drv_info *drv_info) +{ + u32 cnt =3D 0, wridx, ridx; + + mtk_dpmaif_drv_dlq_all_en(drv_info, false); + do { + if (++cnt >=3D POLL_MAX_TIMES) { + dev_err(DRV_TO_MDEV(drv_info)->dev, "Failed to stop dl queue, 0x%x\n", + mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_DL_DBG_STA1)); + return -DATA_HW_REG_TIMEOUT; + } + udelay(POLL_INTERVAL_US); + } while (!mtk_dpmaif_drv_dl_idle_check(drv_info)); + + cnt =3D 0; + do { + wridx =3D mtk_dpmaif_drv_dl_get_wridx(drv_info); + ridx =3D mtk_dpmaif_drv_dl_get_pit_ridx(drv_info); + if (wridx =3D=3D ridx) + break; + + udelay(POLL_INTERVAL_US); + cnt++; + } while (cnt < POLL_MAX_TIMES); + + if (cnt >=3D POLL_MAX_TIMES) { + dev_err(DRV_TO_MDEV(drv_info)->dev, "Failed to check middle pit sync\n"); + return -DATA_HW_REG_TIMEOUT; + } + + return 0; +} + +static int mtk_dpmaif_drv_stop_queue_t800(struct dpmaif_drv_info *drv_info= , enum dpmaif_drv_dir dir) +{ + int ret; + + if (dir =3D=3D DPMAIF_TX) { + if (unlikely(!drv_info->ulq_all_enable)) { + dev_info(DRV_TO_MDEV(drv_info)->dev, "ulq all disabled\n"); + return 0; + } + + ret =3D mtk_dpmaif_drv_stop_ulq(drv_info); + if (ret < 0) + return ret; + + ret =3D mtk_dpmaif_drv_ul_mask_all_tx_done_intr(drv_info); + if (ret < 0) + return ret; + + drv_info->ulq_all_enable =3D false; + } else { + if (unlikely(!drv_info->dlq_all_enable)) { + dev_info(DRV_TO_MDEV(drv_info)->dev, "dlq all disabled\n"); + return 0; + } + + ret =3D mtk_dpmaif_drv_stop_dlq(drv_info); + if (ret < 0) + return ret; + + ret =3D mtk_dpmaif_drv_dl_mask_all_rx_done_intr(drv_info); + if (ret < 0) + return ret; + + drv_info->dlq_all_enable =3D false; + } + + return 0; +} + +static u32 mtk_dpmaif_drv_get_dl_lv2_sts(struct dpmaif_drv_info *drv_info) +{ + return mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_DL_L2TISAR0); +} + +static u32 mtk_dpmaif_drv_get_ul_lv2_sts(struct dpmaif_drv_info *drv_info) +{ + return mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_UL_L2TISAR0); +} + +static u32 mtk_dpmaif_drv_get_ul_intr_mask(struct dpmaif_drv_info *drv_inf= o) +{ + return mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_UL_L2TIMR0); +} + +static u32 mtk_dpmaif_drv_get_dl_intr_mask(struct dpmaif_drv_info *drv_inf= o) +{ + return mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_DL_L2TIMR0); +} + +static bool mtk_dpmaif_drv_check_clr_ul_done_status(struct dpmaif_drv_info= *drv_info, u8 qno) +{ + u32 val, l2tisar0; + bool ret =3D false; + + l2tisar0 =3D mtk_dpmaif_drv_get_ul_lv2_sts(drv_info); + val =3D l2tisar0 & DPMAIF_UL_INT_QDONE & (1 << (DP_UL_INT_DONE_OFFSET + = qno)); + + if (val) { + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_UL_L2TISAR0, val); + ret =3D true; + } + + return ret; +} + +static u32 mtk_dpmaif_drv_irq_src0_dl_filter(struct dpmaif_drv_info *drv_i= nfo, u32 l2risar0, + u32 l2rimr0) +{ + if (l2rimr0 & DPMAIF_DL_INT_DLQ0_QDONE_MSK) + l2risar0 &=3D ~DPMAIF_DL_INT_DLQ0_QDONE; + + return l2risar0; +} + +static u32 mtk_dpmaif_drv_irq_src1_dl_filter(struct dpmaif_drv_info *drv_i= nfo, u32 l2risar0, + u32 l2rimr0) +{ + if (l2rimr0 & DPMAIF_DL_INT_DLQ1_QDONE_MSK) + l2risar0 &=3D ~DPMAIF_DL_INT_DLQ1_QDONE; + + return l2risar0; +} + +static int mtk_dpmaif_drv_irq_src0(struct dpmaif_drv_info *drv_info, + struct dpmaif_drv_intr_info *intr_info) +{ + u32 val, l2risar0, l2rimr0; + + l2risar0 =3D mtk_dpmaif_drv_get_dl_lv2_sts(drv_info); + l2rimr0 =3D mtk_dpmaif_drv_get_dl_intr_mask(drv_info); + + l2risar0 &=3D DPMAIF_SRC0_DL_STATUS_MASK; + if (l2risar0) { + l2risar0 =3D mtk_dpmaif_drv_irq_src0_dl_filter(drv_info, l2risar0, l2rim= r0); + + val =3D l2risar0 & DPMAIF_DL_INT_DLQ0_QDONE; + if (val) { + if (!mtk_dpmaif_drv_dlq_mask_rx_done_intr(drv_info, DPMAIF_DLQ0)) { + intr_info->intr_types[intr_info->intr_cnt] =3D DPMAIF_INTR_DL_DONE; + intr_info->intr_queues[intr_info->intr_cnt] =3D 0x01 << DPMAIF_DLQ0; + intr_info->intr_cnt++; + } + } + + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_DL_L2TISAR0, l2risar0= ); + } + + return 0; +} + +static int mtk_dpmaif_drv_irq_src1(struct dpmaif_drv_info *drv_info, + struct dpmaif_drv_intr_info *intr_info) +{ + u32 val, l2risar0, l2rimr0; + + l2risar0 =3D mtk_dpmaif_drv_get_dl_lv2_sts(drv_info); + l2rimr0 =3D mtk_dpmaif_drv_get_dl_intr_mask(drv_info); + + l2risar0 &=3D DPMAIF_SRC1_DL_STATUS_MASK; + if (l2risar0) { + l2risar0 =3D mtk_dpmaif_drv_irq_src1_dl_filter(drv_info, l2risar0, l2rim= r0); + + val =3D l2risar0 & DPMAIF_DL_INT_DLQ1_QDONE; + if (val) { + if (!mtk_dpmaif_drv_dlq_mask_rx_done_intr(drv_info, DPMAIF_DLQ1)) { + intr_info->intr_types[intr_info->intr_cnt] =3D DPMAIF_INTR_DL_DONE; + intr_info->intr_queues[intr_info->intr_cnt] =3D 0x01 << DPMAIF_DLQ1; + intr_info->intr_cnt++; + } + } + + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_DL_L2TISAR0, l2risar0= ); + } + + return 0; +} + +static int mtk_dpmaif_drv_irq_src2(struct dpmaif_drv_info *drv_info, + struct dpmaif_drv_intr_info *intr_info) +{ + u32 l2tisar0, l2timr0; + u8 q_mask; + u32 val; + + l2tisar0 =3D mtk_dpmaif_drv_get_ul_lv2_sts(drv_info); + l2timr0 =3D mtk_dpmaif_drv_get_ul_intr_mask(drv_info); + + l2tisar0 &=3D (~l2timr0); + if (l2tisar0) { + val =3D l2tisar0 & DPMAIF_UL_INT_QDONE; + if (val) { + q_mask =3D val >> DP_UL_INT_DONE_OFFSET & DPMAIF_ULQS; + mtk_dpmaif_drv_ul_mask_multi_tx_done_intr(drv_info, q_mask); + intr_info->intr_types[intr_info->intr_cnt] =3D DPMAIF_INTR_UL_DONE; + intr_info->intr_queues[intr_info->intr_cnt] =3D val >> DP_UL_INT_DONE_O= FFSET; + intr_info->intr_cnt++; + } + + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_UL_L2TISAR0, l2tisar0= ); + } + + return 0; +} + +static int mtk_dpmaif_drv_intr_handle_t800(struct dpmaif_drv_info *drv_inf= o, void *data, u8 irq_id) +{ + switch (irq_id) { + case MTK_IRQ_SRC_DPMAIF: + mtk_dpmaif_drv_irq_src0(drv_info, data); + break; + case MTK_IRQ_SRC_DPMAIF2: + mtk_dpmaif_drv_irq_src1(drv_info, data); + break; + case MTK_IRQ_SRC_DPMAIF3: + mtk_dpmaif_drv_irq_src2(drv_info, data); + break; + default: + break; + } + + return 0; +} + +static int mtk_dpmaif_drv_intr_complete_t800(struct dpmaif_drv_info *drv_i= nfo, + enum dpmaif_drv_intr_type type, u8 q_id, u64 data) +{ + int ret =3D 0; + + switch (type) { + case DPMAIF_INTR_UL_DONE: + if (data =3D=3D DPMAIF_CLEAR_INTR) + mtk_dpmaif_drv_check_clr_ul_done_status(drv_info, q_id); + else + ret =3D mtk_dpmaif_drv_unmask_ulq_intr(drv_info, q_id); + break; + case DPMAIF_INTR_DL_DONE: + ret =3D mtk_dpmaif_drv_dl_unmask_rx_done_intr(drv_info, q_id); + break; + default: + break; + } + + return ret; +} + +static int mtk_dpmaif_drv_clr_ip_busy_sts_t800(struct dpmaif_drv_info *drv= _info) +{ + u32 ip_busy_sts; + + ip_busy_sts =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_IP_BUSY= ); + + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_AP_IP_BUSY, ip_busy_sts); + + return 0; +} + +static int mtk_dpmaif_drv_dl_add_pit_cnt(struct dpmaif_drv_info *drv_info, + u32 qno, u32 pit_remain_cnt) +{ + u32 cnt =3D 0, dl_update; + + dl_update =3D pit_remain_cnt & 0x0003ffff; + dl_update |=3D DPMAIF_DL_ADD_UPDATE | (qno << DPMAIF_ADD_LRO_PIT_CHAN_OFS= ); + + do { + if ((mtk_hw_read32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_DL_LROPIT_ADD) & + DPMAIF_DL_ADD_NOT_READY) =3D=3D 0) { + mtk_hw_write32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_DL_LROPIT_ADD, dl_upd= ate); + break; + } + + udelay(POLL_INTERVAL_US); + cnt++; + } while (cnt < POLL_MAX_TIMES); + + if (cnt >=3D POLL_MAX_TIMES) { + dev_err(DRV_TO_MDEV(drv_info)->dev, "Failed to add dlq%u pit-1, cnt=3D%u= \n", + qno, pit_remain_cnt); + return -DATA_HW_REG_TIMEOUT; + } + + cnt =3D 0; + do { + if (!((mtk_hw_read32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_DL_LROPIT_ADD) & + DPMAIF_DL_ADD_NOT_READY) =3D=3D DPMAIF_DL_ADD_NOT_READY)) + break; + + cnt++; + udelay(POLL_INTERVAL_US); + } while (cnt < POLL_MAX_TIMES); + + if (cnt >=3D POLL_MAX_TIMES) { + dev_err(DRV_TO_MDEV(drv_info)->dev, "Failed to add dlq%u pit-2, cnt=3D%u= \n", + qno, pit_remain_cnt); + return false; + } + + return 0; +} + +static int mtk_dpmaif_drv_dl_add_bat_cnt(struct dpmaif_drv_info *drv_info,= u32 bat_entry_cnt) +{ + u32 cnt =3D 0, dl_bat_update; + + dl_bat_update =3D bat_entry_cnt & 0xffff; + dl_bat_update |=3D DPMAIF_DL_ADD_UPDATE; + do { + if ((mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_DL_BAT_ADD) & + DPMAIF_DL_ADD_NOT_READY) =3D=3D 0) { + mtk_hw_write32(DRV_TO_MDEV(drv_info), DPMAIF_PD_DL_BAT_ADD, dl_bat_upda= te); + break; + } + + udelay(POLL_INTERVAL_US); + cnt++; + } while (cnt < POLL_MAX_TIMES); + + if (cnt >=3D POLL_MAX_TIMES) { + dev_err(DRV_TO_MDEV(drv_info)->dev, + "Failed to add bat-1, cnt=3D%u\n", bat_entry_cnt); + return -DATA_HW_REG_TIMEOUT; + } + + cnt =3D 0; + do { + if (!((mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_PD_DL_BAT_ADD) & + DPMAIF_DL_ADD_NOT_READY) =3D=3D DPMAIF_DL_ADD_NOT_READY)) + break; + + cnt++; + udelay(POLL_INTERVAL_US); + } while (cnt < POLL_MAX_TIMES); + + if (cnt >=3D POLL_MAX_TIMES) { + dev_err(DRV_TO_MDEV(drv_info)->dev, "Failed to add bat-2, cnt=3D%u\n", + bat_entry_cnt); + return -DATA_HW_REG_TIMEOUT; + } + return 0; +} + +static int mtk_dpmaif_drv_ul_add_drb(struct dpmaif_drv_info *drv_info, u8 = q_num, u32 drb_cnt) +{ + u32 drb_entry_cnt =3D drb_cnt * DPMAIF_UL_DRB_ENTRY_WORD; + u32 cnt =3D 0, ul_update; + u64 addr; + + ul_update =3D drb_entry_cnt & 0x0000ffff; + ul_update |=3D DPMAIF_UL_ADD_UPDATE; + + if (q_num =3D=3D 4) + addr =3D NRL2_DPMAIF_UL_ADD_DESC_CH4; + else + addr =3D DPMAIF_ULQ_ADD_DESC_CH_N(q_num); + + do { + if (!(mtk_hw_read32(DRV_TO_MDEV(drv_info), addr) & DPMAIF_UL_ADD_NOT_REA= DY)) { + mtk_hw_write32(DRV_TO_MDEV(drv_info), addr, ul_update); + break; + } + + udelay(POLL_INTERVAL_US); + cnt++; + } while (cnt < POLL_MAX_TIMES); + + if (cnt >=3D POLL_MAX_TIMES) { + dev_err(DRV_TO_MDEV(drv_info)->dev, "Failed to add ulq%u drb-1, cnt=3D%u= \n", + q_num, drb_cnt); + return -DATA_HW_REG_TIMEOUT; + } + + cnt =3D 0; + do { + if (!((mtk_hw_read32(DRV_TO_MDEV(drv_info), addr) & + DPMAIF_UL_ADD_NOT_READY) =3D=3D DPMAIF_UL_ADD_NOT_READY)) + break; + + cnt++; + udelay(POLL_INTERVAL_US); + } while (cnt < POLL_MAX_TIMES); + + if (cnt >=3D POLL_MAX_TIMES) { + dev_err(DRV_TO_MDEV(drv_info)->dev, "Failed to add ulq%u drb-2, cnt=3D%u= \n", + q_num, drb_cnt); + return -DATA_HW_REG_TIMEOUT; + } + return 0; +} + +static int mtk_dpmaif_drv_send_doorbell_t800(struct dpmaif_drv_info *drv_i= nfo, + enum dpmaif_drv_ring_type type, + u8 q_id, u32 cnt) +{ + int ret =3D 0; + + switch (type) { + case DPMAIF_PIT: + ret =3D mtk_dpmaif_drv_dl_add_pit_cnt(drv_info, q_id, cnt); + break; + case DPMAIF_BAT: + ret =3D mtk_dpmaif_drv_dl_add_bat_cnt(drv_info, cnt); + break; + case DPMAIF_DRB: + ret =3D mtk_dpmaif_drv_ul_add_drb(drv_info, q_id, cnt); + break; + default: + break; + } + + return ret; +} + +static int mtk_dpmaif_drv_dl_get_pit_wridx(struct dpmaif_drv_info *drv_inf= o, u32 qno) +{ + u32 pit_wridx; + + pit_wridx =3D (mtk_hw_read32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_AO_DL_LRO= _STA5 + qno * 0x20)) + & DPMAIF_DL_PIT_WRIDX_MSK; + if (unlikely(pit_wridx >=3D drv_info->drv_property.dlq[qno].pit_size)) + return -DATA_HW_REG_CHK_FAIL; + + return pit_wridx; +} + +static int mtk_dpmaif_drv_dl_get_pit_rdidx(struct dpmaif_drv_info *drv_inf= o, u32 qno) +{ + u32 pit_rdidx; + + pit_rdidx =3D (mtk_hw_read32(DRV_TO_MDEV(drv_info), NRL2_DPMAIF_AO_DL_LRO= _STA6 + qno * 0x20)) + & DPMAIF_DL_PIT_WRIDX_MSK; + if (unlikely(pit_rdidx >=3D drv_info->drv_property.dlq[qno].pit_size)) + return -DATA_HW_REG_CHK_FAIL; + + return pit_rdidx; +} + +static int mtk_dpmaif_drv_dl_get_bat_ridx(struct dpmaif_drv_info *drv_info) +{ + u32 bat_ridx; + + bat_ridx =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_BAT_STA2) & + DPMAIF_DL_BAT_WRIDX_MSK; + + if (unlikely(bat_ridx >=3D drv_info->drv_property.ring.normal_bat_size)) + return -DATA_HW_REG_CHK_FAIL; + + return bat_ridx; +} + +static int mtk_dpmaif_drv_dl_get_bat_wridx(struct dpmaif_drv_info *drv_inf= o) +{ + u32 bat_wridx; + + bat_wridx =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), DPMAIF_AO_DL_BAT_STA3)= & + DPMAIF_DL_BAT_WRIDX_MSK; + if (unlikely(bat_wridx >=3D drv_info->drv_property.ring.normal_bat_size)) + return -DATA_HW_REG_CHK_FAIL; + + return bat_wridx; +} + +static int mtk_dpmaif_drv_ul_get_drb_ridx(struct dpmaif_drv_info *drv_info= , u8 q_num) +{ + u32 drb_ridx; + u64 addr; + + addr =3D DPMAIF_ULQ_STA0_N(q_num); + + drb_ridx =3D mtk_hw_read32(DRV_TO_MDEV(drv_info), addr) >> 16; + drb_ridx =3D drb_ridx / DPMAIF_UL_DRB_ENTRY_WORD; + + if (unlikely(drb_ridx >=3D drv_info->drv_property.ulq[q_num].drb_size)) + return -DATA_HW_REG_CHK_FAIL; + + return drb_ridx; +} + +static int mtk_dpmaif_drv_get_ring_idx_t800(struct dpmaif_drv_info *drv_in= fo, + enum dpmaif_drv_ring_idx index, u8 q_id) +{ + int ret =3D 0; + + switch (index) { + case DPMAIF_PIT_WIDX: + ret =3D mtk_dpmaif_drv_dl_get_pit_wridx(drv_info, q_id); + break; + case DPMAIF_PIT_RIDX: + ret =3D mtk_dpmaif_drv_dl_get_pit_rdidx(drv_info, q_id); + break; + case DPMAIF_BAT_WIDX: + ret =3D mtk_dpmaif_drv_dl_get_bat_wridx(drv_info); + break; + case DPMAIF_BAT_RIDX: + ret =3D mtk_dpmaif_drv_dl_get_bat_ridx(drv_info); + break; + case DPMAIF_DRB_RIDX: + ret =3D mtk_dpmaif_drv_ul_get_drb_ridx(drv_info, q_id); + break; + default: + break; + } + + return ret; +} + +struct dpmaif_drv_ops dpmaif_drv_ops_t800 =3D { + .init =3D mtk_dpmaif_drv_init_t800, + .start_queue =3D mtk_dpmaif_drv_start_queue_t800, + .stop_queue =3D mtk_dpmaif_drv_stop_queue_t800, + .intr_handle =3D mtk_dpmaif_drv_intr_handle_t800, + .intr_complete =3D mtk_dpmaif_drv_intr_complete_t800, + .clear_ip_busy =3D mtk_dpmaif_drv_clr_ip_busy_sts_t800, + .send_doorbell =3D mtk_dpmaif_drv_send_doorbell_t800, + .get_ring_idx =3D mtk_dpmaif_drv_get_ring_idx_t800, +}; diff --git a/drivers/net/wwan/mediatek/pcie/mtk_dpmaif_reg_t800.h b/drivers= /net/wwan/mediatek/pcie/mtk_dpmaif_reg_t800.h new file mode 100644 index 000000000000..ce45151d9288 --- /dev/null +++ b/drivers/net/wwan/mediatek/pcie/mtk_dpmaif_reg_t800.h @@ -0,0 +1,319 @@ +/* SPDX-License-Identifier: BSD-3-Clause-Clear + * + * Copyright (c) 2022, MediaTek Inc. + */ + +#ifndef __MTK_DPMAIF_DRV_T800_H__ +#define __MTK_DPMAIF_DRV_T800_H__ + +#define DPMAIF_DEV_PD_BASE (0x1022D000) +#define DPMAIF_DEV_AO_BASE (0x10011000) + +#define DPMAIF_PD_BASE DPMAIF_DEV_PD_BASE +#define DPMAIF_AO_BASE DPMAIF_DEV_AO_BASE + +#define BASE_NADDR_NRL2_DPMAIF_UL ((unsigned long)(DPMAIF_PD_BASE)) +#define BASE_NADDR_NRL2_DPMAIF_DL ((unsigned long)(DPMAIF_PD_BASE + 0x100)) +#define BASE_NADDR_NRL2_DPMAIF_AP_MISC ((unsigned long)(DPMAIF_PD_BASE + 0= x400)) +#define BASE_NADDR_NRL2_DPMAIF_PD_SRAM_UL ((unsigned long)(DPMAIF_PD_BASE = + 0xD00)) +#define BASE_NADDR_NRL2_DPMAIF_PD_SRAM_DL ((unsigned long)(DPMAIF_PD_BASE = + 0xC00)) +#define BASE_NADDR_NRL2_DPMAIF_DL_LRO_REMOVEAO_IDX ((unsigned long)(DPMAIF= _PD_BASE + 0x900)) +#define BASE_NADDR_NRL2_DPMAIF_MMW_HPC ((unsigned long)(DPMAIF_PD_BASE + = 0x600)) +#define BASE_NADDR_NRL2_DPMAIF_PD_SRAM_MISC2 ((unsigned long)(DPMAIF_PD_BA= SE + 0xF00)) +#define BASE_NADDR_NRL2_DPMAIF_AO_UL ((unsigned long)(DPMAIF_AO_BASE)) +#define BASE_NADDR_NRL2_DPMAIF_AO_DL ((unsigned long)(DPMAIF_AO_BASE + 0x= 400)) + +/* dpmaif uplink part registers. */ +#define NRL2_DPMAIF_UL_ADD_DESC (BASE_NADDR_NRL2_DPMAIF_UL + 0x00) +#define NRL2_DPMAIF_UL_DBG_STA2 (BASE_NADDR_NRL2_DPMAIF_UL + 0x88) +#define NRL2_DPMAIF_UL_RESERVE_AO_RW (BASE_NADDR_NRL2_DPMAIF_UL + 0xAC) +#define NRL2_DPMAIF_UL_ADD_DESC_CH0 (BASE_NADDR_NRL2_DPMAIF_UL + 0xB0) +#define NRL2_DPMAIF_UL_ADD_DESC_CH4 (BASE_NADDR_NRL2_DPMAIF_UL + 0xE0) + +/* dpmaif downlink part registers. */ +#define NRL2_DPMAIF_DL_BAT_INIT (BASE_NADDR_NRL2_DPMAIF_DL + 0x00) +#define NRL2_DPMAIF_DL_BAT_INIT (BASE_NADDR_NRL2_DPMAIF_DL + 0x00) +#define NRL2_DPMAIF_DL_BAT_ADD (BASE_NADDR_NRL2_DPMAIF_DL + 0x04) +#define NRL2_DPMAIF_DL_BAT_INIT_CON0 (BASE_NADDR_NRL2_DPMAIF_DL + 0x08) +#define NRL2_DPMAIF_DL_BAT_INIT_CON1 (BASE_NADDR_NRL2_DPMAIF_DL + 0x0C) +#define NRL2_DPMAIF_DL_BAT_INIT_CON3 (BASE_NADDR_NRL2_DPMAIF_DL + 0x50) +#define NRL2_DPMAIF_DL_DBG_STA1 (BASE_NADDR_NRL2_DPMAIF_DL + 0xB4) + +/* dpmaif ap misc part registers. */ +#define NRL2_DPMAIF_AP_MISC_AP_L2TISAR0 (BASE_NADDR_NRL2_DPMAIF_AP_MISC += 0x00) +#define NRL2_DPMAIF_AP_MISC_APDL_L2TISAR0 (BASE_NADDR_NRL2_DPMAIF_AP_MISC = + 0x50) +#define NRL2_DPMAIF_AP_MISC_AP_IP_BUSY (BASE_NADDR_NRL2_DPMAIF_AP_MISC + = 0x60) +#define NRL2_DPMAIF_AP_MISC_CG_EN (BASE_NADDR_NRL2_DPMAIF_AP_MISC + 0x68) +#define NRL2_DPMAIF_AP_MISC_OVERWRITE_CFG (BASE_NADDR_NRL2_DPMAIF_AP_MISC = + 0x90) +#define NRL2_DPMAIF_AP_MISC_RSTR_CLR (BASE_NADDR_NRL2_DPMAIF_AP_MISC + 0x= 94) + +/* dpmaif uplink ao part registers. */ +#define NRL2_DPMAIF_AO_UL_INIT_SET (BASE_NADDR_NRL2_DPMAIF_AO_UL + 0x0) +#define NRL2_DPMAIF_AO_UL_CHNL_ARB0 (BASE_NADDR_NRL2_DPMAIF_AO_UL + 0x1C) +#define NRL2_DPMAIF_AO_UL_AP_L2TIMR0 (BASE_NADDR_NRL2_DPMAIF_AO_UL + 0x80) +#define NRL2_DPMAIF_AO_UL_AP_L2TIMCR0 (BASE_NADDR_NRL2_DPMAIF_AO_UL + 0x8= 4) +#define NRL2_DPMAIF_AO_UL_AP_L2TIMSR0 (BASE_NADDR_NRL2_DPMAIF_AO_UL + 0x8= 8) +#define NRL2_DPMAIF_AO_UL_AP_L1TIMR0 (BASE_NADDR_NRL2_DPMAIF_AO_UL + 0x8C) +#define NRL2_DPMAIF_AO_UL_APDL_L2TIMR0 (BASE_NADDR_NRL2_DPMAIF_AO_UL + 0x= 90) +#define NRL2_DPMAIF_AO_UL_APDL_L2TIMCR0 (BASE_NADDR_NRL2_DPMAIF_AO_UL + 0= x94) +#define NRL2_DPMAIF_AO_UL_APDL_L2TIMSR0 (BASE_NADDR_NRL2_DPMAIF_AO_UL + 0= x98) +#define NRL2_DPMAIF_AO_UL_AP_DL_UL_IP_BUSY_MASK (BASE_NADDR_NRL2_DPMAIF_AO= _UL + 0x9C) + +/* dpmaif uplink pd sram part registers. */ +#define NRL2_DPMAIF_AO_UL_CHNL0_CON0 (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_UL += 0x10) +#define NRL2_DPMAIF_AO_UL_CHNL0_CON1 (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_UL += 0x14) +#define NRL2_DPMAIF_AO_UL_CHNL0_CON2 (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_UL += 0x18) +#define NRL2_DPMAIF_DLY_IRQ_TIMER3 (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_UL + 0= x1C) +#define NRL2_DPMAIF_DLY_IRQ_TIMER4 (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_UL + 0= x2C) +#define NRL2_DPMAIF_DLY_IRQ_TIMER5 (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_UL + 0= x3C) +#define NRL2_DPMAIF_DLY_IRQ_TIMER6 (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_UL + 0= x60) +#define NRL2_DPMAIF_DLY_IRQ_TIMER7 (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_UL + 0= x64) +#define NRL2_DPMAIF_AO_UL_CH0_STA (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_UL + 0x= E0) + +/* dpmaif downlink ao part registers. */ +#define NRL2_DPMAIF_AO_DL_INIT_SET (BASE_NADDR_NRL2_DPMAIF_AO_DL + 0x0) +#define NRL2_DPMAIF_AO_DL_LROPIT_INIT_CON5 (BASE_NADDR_NRL2_DPMAIF_AO_DL += 0x28) +#define NRL2_DPMAIF_AO_DL_LROPIT_TRIG_THRES (BASE_NADDR_NRL2_DPMAIF_AO_DL = + 0x34) + +/* dpmaif downlink pd sram part registers. */ +#define NRL2_DPMAIF_AO_DL_PKTINFO_CON0 (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_DL= + 0x0) +#define NRL2_DPMAIF_AO_DL_PKTINFO_CON1 (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_DL= + 0x4) +#define NRL2_DPMAIF_AO_DL_PKTINFO_CON2 (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_DL= + 0x8) +#define NRL2_DPMAIF_AO_DL_RDY_CHK_THRES (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_D= L + 0xC) +#define NRL2_DPMAIF_AO_DL_RDY_CHK_FRG_THRES (BASE_NADDR_NRL2_DPMAIF_PD_SRA= M_DL + 0x10) +#define NRL2_DPMAIF_AO_DL_LRO_AGG_CFG (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_DL = + 0x20) +#define NRL2_DPMAIF_AO_DL_LROPIT_TIMEOUT0 (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_= DL + 0x24) +#define NRL2_DPMAIF_AO_DL_LROPIT_TIMEOUT1 (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_= DL + 0x28) +#define NRL2_DPMAIF_AO_DL_HPC_CNTL (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_DL + 0= x38) +#define NRL2_DPMAIF_AO_DL_PIT_SEQ_END (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_DL = + 0x40) +#define NRL2_DPMAIF_AO_DL_DLY_IRQ_TIMER1 (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_D= L + 0x58) +#define NRL2_DPMAIF_AO_DL_DLY_IRQ_TIMER2 (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_D= L + 0x5C) +#define NRL2_DPMAIF_AO_DL_BAT_STA2 (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_DL + 0= xD8) +#define NRL2_DPMAIF_AO_DL_BAT_STA3 (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_DL + 0= xDC) +#define NRL2_DPMAIF_AO_DL_PIT_STA2 (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_DL + 0= xEC) +#define NRL2_DPMAIF_AO_DL_PIT_STA3 (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_DL + 0= x60) +#define NRL2_DPMAIF_AO_DL_FRGBAT_STA2 (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_DL = + 0x78) +#define NRL2_DPMAIF_AO_DL_LRO_STA5 (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_DL + 0= xA4) +#define NRL2_DPMAIF_AO_DL_LRO_STA6 (BASE_NADDR_NRL2_DPMAIF_PD_SRAM_DL + 0= xA8) + +/* dpmaif hpc part registers. */ +#define NRL2_DPMAIF_HPC_INTR_MASK (BASE_NADDR_NRL2_DPMAIF_MMW_HPC + 0x0F4) + +/* dpmaif LRO part registers. */ +#define NRL2_DPMAIF_DL_LROPIT_INIT (BASE_NADDR_NRL2_DPMAIF_DL_LRO_REMOVEA= O_IDX + 0x0) +#define NRL2_DPMAIF_DL_LROPIT_ADD (BASE_NADDR_NRL2_DPMAIF_DL_LRO_REMOVEAO= _IDX + 0x10) +#define NRL2_DPMAIF_DL_LROPIT_INIT_CON0 (BASE_NADDR_NRL2_DPMAIF_DL_LRO_RE= MOVEAO_IDX + 0x14) +#define NRL2_DPMAIF_DL_LROPIT_INIT_CON1 (BASE_NADDR_NRL2_DPMAIF_DL_LRO_RE= MOVEAO_IDX + 0x18) +#define NRL2_DPMAIF_DL_LROPIT_INIT_CON2 (BASE_NADDR_NRL2_DPMAIF_DL_LRO_RE= MOVEAO_IDX + 0x1C) +#define NRL2_DPMAIF_DL_LROPIT_INIT_CON5 (BASE_NADDR_NRL2_DPMAIF_DL_LRO_RE= MOVEAO_IDX + 0x28) +#define NRL2_DPMAIF_DL_LROPIT_INIT_CON3 (BASE_NADDR_NRL2_DPMAIF_DL_LRO_RE= MOVEAO_IDX + 0x20) +#define NRL2_DPMAIF_DL_LROPIT_INIT_CON4 (BASE_NADDR_NRL2_DPMAIF_DL_LRO_RE= MOVEAO_IDX + 0x24) +#define NRL2_DPMAIF_DL_LROPIT_INIT_CON6 (BASE_NADDR_NRL2_DPMAIF_DL_LRO_RE= MOVEAO_IDX + 0x2C) + +/* dpmaif pd ul, ao ul config. */ +#define DPMAIF_PD_UL_CHNL_ARB0 NRL2_DPMAIF_AO_UL_CHNL_ARB0 +#define DPMAIF_PD_UL_CHNL0_CON0 NRL2_DPMAIF_AO_UL_CHNL0_CON0 +#define DPMAIF_PD_UL_CHNL0_CON1 NRL2_DPMAIF_AO_UL_CHNL0_CON1 +#define DPMAIF_PD_UL_CHNL0_CON2 NRL2_DPMAIF_AO_UL_CHNL0_CON2 +#define DPMAIF_PD_UL_ADD_DESC_CH NRL2_DPMAIF_UL_ADD_DESC_CH0 +#define DPMAIF_PD_UL_DBG_STA2 NRL2_DPMAIF_UL_DBG_STA2 + +/* dpmaif pd dl config. */ +#define DPMAIF_PD_DL_BAT_INIT NRL2_DPMAIF_DL_BAT_INIT +#define DPMAIF_PD_DL_BAT_ADD NRL2_DPMAIF_DL_BAT_ADD +#define DPMAIF_PD_DL_BAT_INIT_CON0 NRL2_DPMAIF_DL_BAT_INIT_CON0 +#define DPMAIF_PD_DL_BAT_INIT_CON1 NRL2_DPMAIF_DL_BAT_INIT_CON1 +#define DPMAIF_PD_DL_BAT_INIT_CON3 NRL2_DPMAIF_DL_BAT_INIT_CON3 +#define DPMAIF_PD_DL_DBG_STA1 NRL2_DPMAIF_DL_DBG_STA1 + +/* dpmaif pd ap misc, ao ul misc config. */ +#define DPMAIF_PD_AP_UL_L2TISAR0 NRL2_DPMAIF_AP_MISC_AP_L2TISAR0 +#define DPMAIF_PD_AP_UL_L2TIMR0 NRL2_DPMAIF_AO_UL_AP_L2TIMR0 +#define DPMAIF_PD_AP_UL_L2TICR0 NRL2_DPMAIF_AO_UL_AP_L2TIMCR0 +#define DPMAIF_PD_AP_UL_L2TISR0 NRL2_DPMAIF_AO_UL_AP_L2TIMSR0 +#define DPMAIF_PD_AP_DL_L2TISAR0 NRL2_DPMAIF_AP_MISC_APDL_L2TISAR0 +#define DPMAIF_PD_AP_DL_L2TIMR0 NRL2_DPMAIF_AO_UL_APDL_L2TIMR0 +#define DPMAIF_PD_AP_DL_L2TICR0 NRL2_DPMAIF_AO_UL_APDL_L2TIMCR0 +#define DPMAIF_PD_AP_DL_L2TISR0 NRL2_DPMAIF_AO_UL_APDL_L2TIMSR0 +#define DPMAIF_PD_AP_IP_BUSY NRL2_DPMAIF_AP_MISC_AP_IP_BUSY +#define DPMAIF_PD_AP_DLUL_IP_BUSY_MASK NRL2_DPMAIF_AO_UL_AP_DL_UL_IP_BUSY_= MASK + +/* dpmaif ao dl config. */ +#define DPMAIF_AO_DL_PKTINFO_CONO NRL2_DPMAIF_AO_DL_PKTINFO_CON0 +#define DPMAIF_AO_DL_PKTINFO_CON1 NRL2_DPMAIF_AO_DL_PKTINFO_CON1 +#define DPMAIF_AO_DL_PKTINFO_CON2 NRL2_DPMAIF_AO_DL_PKTINFO_CON2 +#define DPMAIF_AO_DL_RDY_CHK_THRES NRL2_DPMAIF_AO_DL_RDY_CHK_THRES +#define DPMAIF_AO_DL_BAT_STA2 NRL2_DPMAIF_AO_DL_BAT_STA2 +#define DPMAIF_AO_DL_BAT_STA3 NRL2_DPMAIF_AO_DL_BAT_STA3 +#define DPMAIF_AO_DL_PIT_STA2 NRL2_DPMAIF_AO_DL_PIT_STA2 +#define DPMAIF_AO_DL_PIT_STA3 NRL2_DPMAIF_AO_DL_PIT_STA3 + +/* DPMAIF AO register */ +#define DPMAIF_AP_RGU_ASSERT 0x10001120 +#define DPMAIF_AP_RGU_DEASSERT 0x10001124 +#define DPMAIF_AP_RST_BIT BIT(4) +#define DPMAIF_AP_AO_RGU_ASSERT 0x10001140 +#define DPMAIF_AP_AO_RGU_DEASSERT 0x10001144 +#define DPMAIF_AP_AO_RST_BIT BIT(3) + +/* hw configuration */ +#define DPMAIF_ULQSAR_N(q_num)\ + ((DPMAIF_PD_UL_CHNL0_CON0) + (0x10 * (q_num))) + +#define DPMAIF_UL_DRBSIZE_ADDRH_N(q_num)\ + ((DPMAIF_PD_UL_CHNL0_CON1) + (0x10 * (q_num))) + +#define DPMAIF_UL_DRB_ADDRH_N(q_num)\ + ((DPMAIF_PD_UL_CHNL0_CON2) + (0x10 * (q_num))) + +#define DPMAIF_ULQ_STA0_N(q_num)\ + ((NRL2_DPMAIF_AO_UL_CH0_STA) + (0x04 * (q_num))) + +#define DPMAIF_ULQ_ADD_DESC_CH_N(q_num)\ + ((DPMAIF_PD_UL_ADD_DESC_CH) + (0x04 * (q_num))) + +#define DPMAIF_ULQS 0x1F + +#define DPMAIF_UL_ADD_NOT_READY BIT(31) +#define DPMAIF_UL_ADD_UPDATE BIT(31) +#define DPMAIF_UL_ALL_QUE_ARB_EN (DPMAIF_ULQS << 8) + +#define DPMAIF_DL_ADD_UPDATE BIT(31) +#define DPMAIF_DL_ADD_NOT_READY BIT(31) + +#define DPMAIF_DL_BAT_INIT_ALLSET BIT(0) +#define DPMAIF_DL_BAT_INIT_EN BIT(31) +#define DPMAIF_DL_BAT_INIT_NOT_READY BIT(31) +#define DPMAIF_DL_BAT_INIT_ONLY_ENABLE_BIT 0 + +#define DPMAIF_DL_PIT_INIT_ALLSET BIT(0) +#define DPMAIF_DL_PIT_INIT_EN BIT(31) +#define DPMAIF_DL_PIT_INIT_NOT_READY BIT(31) + +#define DPMAIF_PKT_ALIGN64_MODE 0 +#define DPMAIF_PKT_ALIGN128_MODE 1 + +#define DPMAIF_BAT_REMAIN_SZ_BASE 16 +#define DPMAIF_BAT_BUFFER_SZ_BASE 128 +#define DPMAIF_FRG_BUFFER_SZ_BASE 128 + +#define DPMAIF_PIT_SIZE_MSK 0x3FFFF + +#define DPMAIF_BAT_EN_MSK BIT(16) +#define DPMAIF_BAT_SIZE_MSK 0xFFFF + +#define DPMAIF_BAT_BID_MAXCNT_MSK 0xFFFF0000 +#define DPMAIF_BAT_REMAIN_MINSZ_MSK 0x0000FF00 +#define DPMAIF_PIT_CHK_NUM_MSK 0xFF000000 +#define DPMAIF_BAT_BUF_SZ_MSK 0x0001FF00 +#define DPMAIF_BAT_RSV_LEN_MSK 0x000000FF +#define DPMAIF_PKT_ALIGN_MSK (0x3 << 22) + +#define DPMAIF_BAT_CHECK_THRES_MSK (0x3F << 16) +#define DPMAIF_FRG_CHECK_THRES_MSK 0xFF +#define DPMAIF_PKT_ALIGN_EN BIT(23) +#define DPMAIF_DRB_SIZE_MSK 0x0000FFFF + +#define DPMAIF_DL_PIT_WRIDX_MSK 0x3FFFF +#define DPMAIF_DL_BAT_WRIDX_MSK 0x3FFFF + +/* DPMAIF_PD_UL_DBG_STA2 */ +#define DPMAIF_UL_IDLE_STS_MSK BIT(11) +#define DPMAIF_UL_IDLE_STS BIT(11) + +/* DPMAIF_PD_DL_DBG_STA1 */ +#define DPMAIF_DL_IDLE_STS BIT(23) +#define DPMAIF_DL_PKT_CHECKSUM_EN BIT(31) +#define DPMAIF_PORT_MODE_MSK BIT(30) +#define DPMAIF_PORT_MODE_PCIE BIT(30) + +/* BASE_NADDR_NRL2_DPMAIF_WDMA */ +#define DPMAIF_DL_BAT_CACHE_PRI BIT(22) +#define DPMAIF_DL_BURST_PIT_EN BIT(13) +#define DPMAIF_MEM_CLR_MASK BIT(0) +#define DPMAIF_SRAM_SYNC_MASK BIT(0) +#define DPMAIF_UL_INIT_DONE_MASK BIT(0) +#define DPMAIF_DL_INIT_DONE_MASK BIT(0) + +#define DPMAIF_DL_PIT_SEQ_MSK 0xFF +#define DPMAIF_PCIE_MODE_SET_VALUE 0x55 + +#define DPMAIF_UDL_IP_BUSY_MSK BIT(0) + +#define DP_UL_INT_DONE_OFFSET 0 +#define DP_UL_INT_EMPTY_OFFSET 5 +#define DP_UL_INT_MD_NOTRDY_OFFSET 10 +#define DP_UL_INT_PWR_NOTRDY_OFFSET 15 +#define DP_UL_INT_LEN_ERR_OFFSET 20 + +/* Enable and mask/unmaks UL interrupt */ +#define DPMAIF_UL_INT_QDONE_MSK (DPMAIF_ULQS << DP_UL_INT_DONE_OFFSET) +#define DPMAIF_UL_TOP0_INT_MSK BIT(9) + +/* UL interrupt status */ +#define DPMAIF_UL_INT_QDONE (DPMAIF_ULQS << DP_UL_INT_DONE_OFFSET) + +/* Enable and Mask/unmask DL interrupt */ +#define DPMAIF_DL_INT_DLQ0_QDONE_MSK BIT(8) +#define DPMAIF_DL_INT_DLQ1_QDONE_MSK BIT(9) +#define DPMAIF_DL_INT_Q2TOQ1_MSK BIT(24) +#define DPMAIF_DL_INT_Q2APTOP_MSK BIT(25) + +/* DL interrupt status */ +#define DPMAIF_DL_INT_DUMMY_STATUS BIT(0) +#define DPMAIF_DL_INT_DLQ0_QDONE BIT(13) +#define DPMAIF_DL_INT_DLQ1_QDONE BIT(14) + +/* DPMAIF LRO HW configure */ +#define DPMAIF_HPC_LRO_PATH_DF 3 + +/* 0: HPC rules add by HW; 1: HPC rules add by Host */ +#define DPMAIF_HPC_ADD_MODE_DF 0 +#define DPMAIF_HPC_TOTAL_NUM 8 +#define DPMAIF_HPC_MAX_TOTAL_NUM 8 +#define DPMAIF_AGG_MAX_LEN_DF 65535 +#define DPMAIF_AGG_TBL_ENT_NUM_DF 50 +#define DPMAIF_HASH_PRIME_DF 13 +#define DPMAIF_MID_TIMEOUT_THRES_DF 100 +#define DPMAIF_LRO_TIMEOUT_THRES_DF 100 +#define DPMAIF_LRO_PRS_THRES_DF 10 +#define DPMAIF_LRO_HASH_BIT_CHOOSE_DF 0 + +#define DPMAIF_LROPIT_EN_MSK 0x100000 +#define DPMAIF_LROPIT_CHAN_OFS 16 +#define DPMAIF_ADD_LRO_PIT_CHAN_OFS 20 + +#define DPMAIF_DL_PIT_BYTE_SIZE 16 +#define DPMAIF_DL_BAT_BYTE_SIZE 8 +#define DPMAIF_UL_DRB_BYTE_SIZE 16 + +#define DPMAIF_UL_DRB_ENTRY_WORD (DPMAIF_UL_DRB_BYTE_SIZE >> 2) +#define DPMAIF_DL_PIT_ENTRY_WORD (DPMAIF_DL_PIT_BYTE_SIZE >> 2) +#define DPMAIF_DL_BAT_ENTRY_WORD (DPMAIF_DL_BAT_BYTE_SIZE >> 2) + +#define DPMAIF_HW_BAT_REMAIN 64 +#define DPMAIF_HW_PKT_BIDCNT 1 + +#define DPMAIF_HW_CHK_BAT_NUM 62 +#define DPMAIF_HW_CHK_PIT_NUM (2 * DPMAIF_HW_CHK_BAT_NUM) + +#define DPMAIF_DLQ_NUM 2 +#define DPMAIF_ULQ_NUM 5 +#define DPMAIF_PKT_BIDCNT 1 + +/* dpmaif interrupt configuration */ +#define DPMAIF_AP_UL_L2INTR_EN_MASK DPMAIF_UL_INT_QDONE_MSK + +#define DPMAIF_AP_DL_L2INTR_EN_MASK\ + (DPMAIF_DL_INT_DLQ0_QDONE_MSK | DPMAIF_DL_INT_DLQ1_QDONE_MSK) + +#define DPMAIF_AP_UDL_IP_BUSY_EN_MASK (DPMAIF_UDL_IP_BUSY_MSK) + +/* dpmaif interrupt mask status by interrupt source */ +#define DPMAIF_SRC0_DL_STATUS_MASK\ + (DPMAIF_DL_INT_DLQ0_QDONE | DPMAIF_DL_INT_DUMMY_STATUS) + +#define DPMAIF_SRC1_DL_STATUS_MASK\ + (DPMAIF_DL_INT_DLQ1_QDONE) + +#endif --=20 2.32.0 From nobody Thu Nov 14 06:37:28 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B3795C7618A for ; Fri, 17 Mar 2023 08:16:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229455AbjCQIQe (ORCPT ); Fri, 17 Mar 2023 04:16:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45584 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229554AbjCQIQb (ORCPT ); Fri, 17 Mar 2023 04:16:31 -0400 Received: from mailgw01.mediatek.com (unknown [60.244.123.138]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 63AC42CFD1; Fri, 17 Mar 2023 01:15:47 -0700 (PDT) X-UUID: b5cb72dac49b11ed91027fb02e0f1d65-20230317 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=s8dy7eaukoqNpOay0uW7XB6nJZYK3q4pTzHAAKYJA2w=; b=TkpA4wwcNSgujYWxDmS5iHAgIRFI3glXbyjMZz7S3/21q8hNXu10aTaGAtgtlstY0Z7bEzQdX3/vcG63GOGKygCp3IxbKSc/QHfkWjCxHt/AnNmSOrKwntILk9EX9w+KBmwXG0VUeqawABXQVI9EVt4F4TvHSoC2MB8IvffpWdE=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.21,REQID:300abb6e-8cf0-4f57-8fe8-d89c0aa21816,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-25 X-CID-META: VersionHash:83295aa,CLOUDID:b32f8db3-beed-4dfc-bd9c-e1b22fa6ccc4,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:0,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:1,OSI:0,OSA:0,AV:0 X-CID-BVR: 2,OSH X-CID-BAS: 2,OSH,0,_ X-UUID: b5cb72dac49b11ed91027fb02e0f1d65-20230317 Received: from mtkmbs11n1.mediatek.inc [(172.21.101.185)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1080091763; Fri, 17 Mar 2023 16:14:16 +0800 Received: from mtkmbs11n2.mediatek.inc (172.21.101.187) by mtkmbs13n1.mediatek.inc (172.21.101.193) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Fri, 17 Mar 2023 16:14:15 +0800 Received: from mcddlt001.gcn.mediatek.inc (10.19.240.15) by mtkmbs11n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.25 via Frontend Transport; Fri, 17 Mar 2023 16:14:13 +0800 From: Yanchao Yang To: Loic Poulain , Sergey Ryazanov , Johannes Berg , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , netdev ML , kernel ML CC: Intel experts , Chetan , MTK ML , Liang Lu , Haijun Liu , Hua Yang , Ting Wang , Felix Chen , Mingliang Xu , Min Dong , Aiden Wang , Guohao Zhang , Chris Feng , Yanchao Yang , Lambert Wang , Mingchuang Qiao , Xiayu Zhang , Haozhe Chang Subject: [PATCH net-next v4 08/10] net: wwan: tmi: Add data plane transaction layer Date: Fri, 17 Mar 2023 16:09:40 +0800 Message-ID: <20230317080942.183514-9-yanchao.yang@mediatek.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20230317080942.183514-1-yanchao.yang@mediatek.com> References: <20230317080942.183514-1-yanchao.yang@mediatek.com> MIME-Version: 1.0 X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Data Path Modem AP Interface (DPMAIF) provides methods for initialization, ring buffer management, ISR, control and handling of TX/RX services' flows. DPMAIF TX It exposes the function 'mtk_dpmaif_send' which can be called by the port l= ayer indirectly to transmit packets. The transaction layer manages uplink data w= ith Descriptor Ring Buffer (DRB), which includes one message DRB entry and one = or more normal DRB entries. Message DRB holds the general packet information a= nd each normal DRB entry holds the address of the packet segment. At the same time, DPMAIF provides multiple virtual queues with different priorities. DPMAIF RX The downlink buffer management uses Buffer Address Table (BAT), which inclu= des normal BAT and fragment BAT, and Packet Information Table (PIT) rings. The BAT ring holds the address of the skb data buffer for the hardware to u= se, while the PIT contains metadata about a whole network packet including a reference to the BAT entry holding the data buffer address. The driver reads the PIT and BAT entries written by the modem. When reaching a threshold, the driver reloads the PIT and BAT rings. Signed-off-by: Yanchao Yang --- drivers/net/wwan/mediatek/Makefile | 3 +- drivers/net/wwan/mediatek/mtk_data_plane.h | 90 + drivers/net/wwan/mediatek/mtk_dev.c | 8 + drivers/net/wwan/mediatek/mtk_dev.h | 8 + drivers/net/wwan/mediatek/mtk_dpmaif.c | 2806 ++++++++++++++++++++ drivers/net/wwan/mediatek/mtk_dpmaif_drv.h | 1 - drivers/net/wwan/mediatek/pcie/mtk_pci.c | 6 + 7 files changed, 2920 insertions(+), 2 deletions(-) create mode 100644 drivers/net/wwan/mediatek/mtk_data_plane.h create mode 100644 drivers/net/wwan/mediatek/mtk_dpmaif.c diff --git a/drivers/net/wwan/mediatek/Makefile b/drivers/net/wwan/mediatek= /Makefile index 9de12d8f373a..27ad6628b83d 100644 --- a/drivers/net/wwan/mediatek/Makefile +++ b/drivers/net/wwan/mediatek/Makefile @@ -11,6 +11,7 @@ mtk_tmi-y =3D \ pcie/mtk_dpmaif_drv_t800.o \ mtk_port.o \ mtk_port_io.o \ - mtk_fsm.o + mtk_fsm.o \ + mtk_dpmaif.o =20 obj-$(CONFIG_MTK_TMI) +=3D mtk_tmi.o diff --git a/drivers/net/wwan/mediatek/mtk_data_plane.h b/drivers/net/wwan/= mediatek/mtk_data_plane.h new file mode 100644 index 000000000000..9dbef5911c49 --- /dev/null +++ b/drivers/net/wwan/mediatek/mtk_data_plane.h @@ -0,0 +1,90 @@ +/* SPDX-License-Identifier: BSD-3-Clause-Clear + * + * Copyright (c) 2022, MediaTek Inc. + */ + +#ifndef __MTK_DATA_PLANE_H__ +#define __MTK_DATA_PLANE_H__ + +#include +#include +#include + +#define SKB_TO_CMD(skb) ((struct mtk_data_cmd *)(skb)->data) +#define CMD_TO_DATA(cmd) (*(void **)(cmd)->data) +#define SKB_TO_CMD_DATA(skb) (*(void **)SKB_TO_CMD(skb)->data) + +#define IPV4_VERSION 0x40 +#define IPV6_VERSION 0x60 + +enum mtk_data_feature { + DATA_F_MULTI_NETDEV =3D BIT(16), +}; + +struct mtk_data_blk { + struct mtk_md_dev *mdev; + struct mtk_dpmaif_ctlb *dcb; +}; + +enum mtk_data_type { + DATA_PKT, + DATA_CMD, +}; + +enum mtk_pkt_type { + PURE_IP, +}; + +enum mtk_data_cmd_type { + DATA_CMD_TRANS_CTL, +}; + +struct mtk_data_rxfh { + unsigned int *indir; + u8 *key; +}; + +struct mtk_data_trans_ctl { + bool enable; +}; + +struct mtk_data_cmd { + void (*data_complete)(void *data); + struct completion done; + int ret; + enum mtk_data_cmd_type cmd; + unsigned int len; + char data[]; +}; + +struct mtk_data_trans_ops { + int (*poll)(struct napi_struct *napi, int budget); + int (*select_txq)(struct sk_buff *skb, enum mtk_pkt_type pkt_type); + int (*send)(struct mtk_data_blk *data_blk, enum mtk_data_type type, + struct sk_buff *skb, u64 data); +}; + +enum mtk_data_evt { + DATA_EVT_MIN, + DATA_EVT_TX_START, + DATA_EVT_TX_STOP, + DATA_EVT_RX_STOP, + DATA_EVT_REG_DEV, + DATA_EVT_UNREG_DEV, + DATA_EVT_MAX +}; + +struct mtk_data_trans_info { + u32 cap; + unsigned char rxq_cnt; + unsigned char txq_cnt; + unsigned int max_mtu; + struct napi_struct **napis; +}; + +int mtk_data_init(struct mtk_md_dev *mdev); +int mtk_data_exit(struct mtk_md_dev *mdev); + +extern struct mtk_data_trans_ops data_trans_ops; + +#endif /* __MTK_DATA_PLANE_H__ */ diff --git a/drivers/net/wwan/mediatek/mtk_dev.c b/drivers/net/wwan/mediate= k/mtk_dev.c index db41acc5e733..6dca0594d6d8 100644 --- a/drivers/net/wwan/mediatek/mtk_dev.c +++ b/drivers/net/wwan/mediatek/mtk_dev.c @@ -4,6 +4,7 @@ */ =20 #include "mtk_ctrl_plane.h" +#include "mtk_data_plane.h" #include "mtk_dev.h" #include "mtk_fsm.h" =20 @@ -19,7 +20,13 @@ int mtk_dev_init(struct mtk_md_dev *mdev) if (ret) goto free_fsm; =20 + ret =3D mtk_data_init(mdev); + if (ret) + goto free_ctrl_plane; + return 0; +free_ctrl_plane: + mtk_ctrl_exit(mdev); free_fsm: mtk_fsm_exit(mdev); exit: @@ -30,6 +37,7 @@ void mtk_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_data_exit(mdev); mtk_ctrl_exit(mdev); mtk_fsm_exit(mdev); } diff --git a/drivers/net/wwan/mediatek/mtk_dev.h b/drivers/net/wwan/mediate= k/mtk_dev.h index 23cedb93e51a..ff1858b95324 100644 --- a/drivers/net/wwan/mediatek/mtk_dev.h +++ b/drivers/net/wwan/mediatek/mtk_dev.h @@ -68,6 +68,7 @@ struct mtk_md_dev; * @clear_ext_evt: Callback to clear HW Layer external event status. * @send_ext_evt: Callback to send HW Layer external event. * @get_ext_evt_status:Callback to get HW Layer external event status. + * @mmio_check: Callback to check whether it is available to mmio acce= ss device. * @get_hp_status: Callback to get link hotplug status. */ struct mtk_hw_ops { @@ -91,6 +92,7 @@ struct mtk_hw_ops { void (*clear_ext_evt)(struct mtk_md_dev *mdev, u32 chs); int (*send_ext_evt)(struct mtk_md_dev *mdev, u32 ch); u32 (*get_ext_evt_status)(struct mtk_md_dev *mdev); + bool (*mmio_check)(struct mtk_md_dev *mdev); int (*get_hp_status)(struct mtk_md_dev *mdev); }; =20 @@ -104,6 +106,7 @@ struct mtk_md_dev { =20 struct mtk_md_fsm *fsm; void *ctrl_blk; + void *data_blk; }; =20 int mtk_dev_init(struct mtk_md_dev *mdev); @@ -201,6 +204,11 @@ static inline u32 mtk_hw_get_ext_evt_status(struct mtk= _md_dev *mdev) return mdev->hw_ops->get_ext_evt_status(mdev); } =20 +static inline bool mtk_hw_mmio_check(struct mtk_md_dev *mdev) +{ + return mdev->hw_ops->mmio_check(mdev); +} + static inline int mtk_hw_get_hp_status(struct mtk_md_dev *mdev) { return mdev->hw_ops->get_hp_status(mdev); diff --git a/drivers/net/wwan/mediatek/mtk_dpmaif.c b/drivers/net/wwan/medi= atek/mtk_dpmaif.c new file mode 100644 index 000000000000..10d1a0255ccc --- /dev/null +++ b/drivers/net/wwan/mediatek/mtk_dpmaif.c @@ -0,0 +1,2806 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +/* + * Copyright (c) 2022, MediaTek Inc. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "mtk_data_plane.h" +#include "mtk_dev.h" +#include "mtk_dpmaif_drv.h" +#include "mtk_fsm.h" +#include "pcie/mtk_reg.h" + +#define DPMAIF_PIT_CNT_UPDATE_THRESHOLD 60 +#define DPMAIF_SKB_TX_WEIGHT 5 + +enum dpmaif_rcsum_state { + CS_RESULT_PASS =3D 0, +}; + +struct dpmaif_msg_pit { + __le32 dword1; + __le32 dword2; + __le32 dword3; + __le32 dword4; +}; + +#define PIT_MSG_DP BIT(31) /* Indicates software to drop this packet if s= et. */ +#define PIT_MSG_DW1_RSV1 GENMASK(30, 27) +#define PIT_MSG_NET_TYPE GENMASK(26, 24) +#define PIT_MSG_CHNL_ID GENMASK(23, 16) /* channel index */ +#define PIT_MSG_DW1_RSV2 GENMASK(15, 12) +#define PIT_MSG_HPC_IDX GENMASK(11, 8) +#define PIT_MSG_SRC_QID GENMASK(7, 5) +#define PIT_MSG_ERR BIT(4) +#define PIT_MSG_CHECKSUM GENMASK(3, 2) +#define PIT_MSG_CONT BIT(1) /* 0b: last entry; 1b: more entry */ +#define PIT_MSG_PKT_TYPE BIT(0) /* 0b: normal PIT entry; 1b: message PIT e= ntry */ + +#define PIT_MSG_HP_IDX GENMASK(31, 27) +#define PIT_MSG_CMD GENMASK(26, 24) +#define PIT_MSG_DW2_RSV GENMASK(23, 21) +#define PIT_MSG_FLOW GENMASK(20, 16) +#define PIT_MSG_COUNT_L GENMASK(15, 0) + +#define PIT_MSG_HASH GENMASK(31, 24) /* Hash value calculated by Hardware= using packet */ +#define PIT_MSG_DW3_RSV1 GENMASK(23, 18) +#define PIT_MSG_PRO GENMASK(17, 16) +#define PIT_MSG_VBID GENMASK(15, 3) +#define PIT_MSG_DW3_RSV2 GENMASK(2, 0) + +#define PIT_MSG_DLQ_DONE GENMASK(31, 30) +#define PIT_MSG_ULQ_DONE GENMASK(29, 24) +#define PIT_MSG_IP BIT(23) +#define PIT_MSG_DW4_RSV1 BIT(22) +#define PIT_MSG_MR GENMASK(21, 20) +#define PIT_MSG_DW4_RSV2 GENMASK(19, 17) +#define PIT_MSG_IG BIT(16) +#define PIT_MSG_DW4_RSV3 GENMASK(15, 11) +#define PIT_MSG_H_BID GENMASK(10, 8) +#define PIT_MSG_PIT_SEQ GENMASK(7, 0) + +#define DPMAIF_PIT_LASTONE 0x00 + +enum dpmaif_pit_type { + PD_PIT =3D 0, + MSG_PIT, +}; + +enum dpmaif_bat_type { + NORMAL_BAT =3D 0, +}; + +struct dpmaif_pd_pit { + __le32 pd_header; + __le32 addr_low; + __le32 addr_high; + __le32 pd_footer; +}; + +#define PIT_PD_DATA_LEN GENMASK(31, 16) /* Indicates the data length of c= urrent packet. */ +#define PIT_PD_BUF_ID GENMASK(15, 3) /* The low order of buffer index */ +#define PIT_PD_BUF_TYPE BIT(2) /* 0b: normal BAT entry */ +#define PIT_PD_CONT BIT(1) /* 0b: last entry; 1b: more entry */ +#define PIT_PD_PKT_TYPE BIT(0) /* 0b: normal PIT entry; 1b: message PIT e= ntry */ + +#define PIT_PD_DLQ_DONE GENMASK(31, 30) +#define PIT_PD_ULQ_DONE GENMASK(29, 24) +#define PIT_PD_HD_OFFSET GENMASK(23, 19) +#define PIT_PD_BI_F GENMASK(18, 17) +#define PIT_PD_IG BIT(16) +#define PIT_PD_RSV GENMASK(15, 11) +#define PIT_PD_H_BID GENMASK(10, 8) /* The high order of buffer index */ +#define PIT_PD_SEQ GENMASK(7, 0) /* PIT sequence */ + +struct dpmaif_bat { + __le32 buf_addr_low; + __le32 buf_addr_high; +}; + +enum dpmaif_drb_type { + PD_DRB, + MSG_DRB, +}; + +#define DPMAIF_DRB_LASTONE 0x00 +#define DPMAIF_DRB_MORE 0x01 + +struct dpmaif_msg_drb { + __le32 msg_header1; + __le32 msg_header2; + __le32 msg_rsv1; + __le32 msg_rsv2; +}; + +#define DRB_MSG_PKT_LEN GENMASK(31, 16) /* The length of a whole packet. = */ +#define DRB_MSG_DW1_RSV GENMASK(15, 3) +#define DRB_MSG_CONT BIT(2) /* 0b: last entry; 1b: more entry */ +#define DRB_MSG_DTYP GENMASK(1, 0) /* 00b: normal DRB entry; 01b: message= DRB entry */ + +#define DRB_MSG_DW2_RSV1 GENMASK(31, 30) +#define DRB_MSG_L4_CHK BIT(29) /* 0b: disable layer4 checksum offload; 1b= : enable */ +#define DRB_MSG_IP_CHK BIT(28) /* 0b: disable IP checksum, 1b: enable IP = checksum */ +#define DRB_MSG_DW2_RSV2 BIT(27) +#define DRB_MSG_NET_TYPE GENMASK(26, 24) +#define DRB_MSG_CHNL_ID GENMASK(23, 16) /* channel index */ +#define DRB_MSG_COUNT_L GENMASK(15, 0) + +struct dpmaif_pd_drb { + __le32 pd_header; + __le32 addr_low; + __le32 addr_high; + __le32 pd_rsv; +}; + +#define DRB_PD_DATA_LEN GENMASK(31, 16) /* the length of a payload. */ +#define DRB_PD_RSV GENMASK(15, 3) +#define DRB_PD_CONT BIT(2)/* 0b: last entry; 1b: more entry */ +#define DRB_PD_DTYP GENMASK(1, 0) /* 00b: normal DRB entry; 01b: message = DRB entry. */ + +#define DPMAIF_SRV_CNT_MAX DPMAIF_TXQ_CNT_MAX + +struct dpmaif_res_cfg { + unsigned char tx_srv_cnt; + unsigned char tx_vq_cnt; + unsigned char tx_vq_srv_map[DPMAIF_TXQ_CNT_MAX]; + int srv_prio_tbl[DPMAIF_SRV_CNT_MAX]; + unsigned int txq_doorbell_delay[DPMAIF_TXQ_CNT_MAX]; + unsigned char irq_cnt; + enum mtk_irq_src irq_src[DPMAIF_IRQ_CNT_MAX]; + unsigned char txq_cnt; + unsigned char rxq_cnt; + unsigned int normal_bat_cnt; + unsigned int pit_cnt[DPMAIF_RXQ_CNT_MAX]; + unsigned int drb_cnt[DPMAIF_TXQ_CNT_MAX]; +}; + +static const struct dpmaif_res_cfg res_cfg_t800 =3D { + .tx_srv_cnt =3D 4, + .tx_vq_cnt =3D 5, + .tx_vq_srv_map =3D {3, 1, 2, 0, 3}, + .srv_prio_tbl =3D {-20, -15, -10, -5}, + .txq_doorbell_delay =3D {0}, + .irq_cnt =3D 3, + .irq_src =3D {MTK_IRQ_SRC_DPMAIF, MTK_IRQ_SRC_DPMAIF2, MTK_IRQ_SRC_DPMAIF= 3}, + .txq_cnt =3D 5, + .rxq_cnt =3D 2, + .normal_bat_cnt =3D 16384, + .pit_cnt =3D {16384, 16384}, + .drb_cnt =3D {6144, 6144, 6144, 6144, 6144}, +}; + +enum dpmaif_state { + DPMAIF_STATE_MIN, + DPMAIF_STATE_PWROFF, + DPMAIF_STATE_PWRON, + DPMAIF_STATE_MAX +}; + +struct dpmaif_vq { + unsigned char q_id; + u32 max_len; /* align network tx qdisc 1000 */ + struct sk_buff_head list; +}; + +struct dpmaif_cmd_srv { + struct mtk_dpmaif_ctlb *dcb; + struct work_struct work; + struct dpmaif_vq *vq; +}; + +struct dpmaif_tx_srv { + struct mtk_dpmaif_ctlb *dcb; + unsigned char id; + int prio; + wait_queue_head_t wait; + struct task_struct *srv; + + unsigned long txq_drb_lack_sta; + unsigned char cur_vq_id; + unsigned char vq_cnt; + struct dpmaif_vq *vq[DPMAIF_TXQ_CNT_MAX]; +}; + +struct dpmaif_drb_skb { + struct sk_buff *skb; + dma_addr_t data_dma_addr; + unsigned short data_len; + unsigned short drb_idx:13; + unsigned short is_msg:1; + unsigned short is_last:1; +}; + +struct dpmaif_txq { + struct mtk_dpmaif_ctlb *dcb; + unsigned char id; + atomic_t budget; + atomic_t to_submit_cnt; + struct dpmaif_pd_drb *drb_base; + dma_addr_t drb_dma_addr; + unsigned int drb_cnt; + unsigned short drb_wr_idx; + unsigned short drb_rd_idx; + unsigned short drb_rel_rd_idx; + unsigned short last_ch_id; + struct dpmaif_drb_skb *sw_drb_base; + unsigned int doorbell_delay; + struct delayed_work doorbell_work; + struct delayed_work tx_done_work; +}; + +struct dpmaif_rx_record { + bool msg_pit_recv; + struct sk_buff *cur_skb; + unsigned int cur_ch_id; + unsigned int checksum; + unsigned char pit_dp; + unsigned char err_payload; +}; + +struct dpmaif_rxq { + struct mtk_dpmaif_ctlb *dcb; + unsigned char id; + bool started; + struct dpmaif_pd_pit *pit_base; + dma_addr_t pit_dma_addr; + unsigned int pit_cnt; + unsigned short pit_wr_idx; + unsigned short pit_rd_idx; + unsigned short pit_rel_rd_idx; + unsigned char pit_seq_expect; + unsigned int pit_rel_cnt; + unsigned int pit_burst_rel_cnt; + unsigned int pit_seq_fail_cnt; + struct napi_struct napi; + struct dpmaif_rx_record rx_record; +}; + +struct skb_mapped_t { + struct sk_buff *skb; + dma_addr_t data_dma_addr; + unsigned int data_len; +}; + +union dpmaif_bat_record { + struct skb_mapped_t normal; +}; + +struct dpmaif_bat_ring { + enum dpmaif_bat_type type; + struct dpmaif_bat *bat_base; + dma_addr_t bat_dma_addr; + unsigned int bat_cnt; + unsigned short bat_wr_idx; + unsigned short bat_rd_idx; + unsigned short bat_rel_rd_idx; + union dpmaif_bat_record *sw_record_base; + unsigned int buf_size; + unsigned char *mask_tbl; + struct work_struct reload_work; +}; + +struct dpmaif_bat_info { + struct mtk_dpmaif_ctlb *dcb; + unsigned int max_mtu; + + struct dpmaif_bat_ring normal_bat_ring; + + struct workqueue_struct *reload_wq; +}; + +struct dpmaif_irq_param { + unsigned char idx; + struct mtk_dpmaif_ctlb *dcb; + enum mtk_irq_src dpmaif_irq_src; + int dev_irq_id; +}; + +struct dpmaif_tx_evt { + unsigned long long ul_done; +}; + +struct dpmaif_rx_evt { + unsigned long long dl_done; +}; + +enum dpmaif_dump_flag { + DPMAIF_DUMP_TX_PKT =3D 0, + DPMAIF_DUMP_RX_PKT, + DPMAIF_DUMP_DRB, + DPMAIF_DUMP_PIT +}; + +struct mtk_dpmaif_ctlb { + struct mtk_data_blk *data_blk; + struct dpmaif_drv_info *drv_info; + struct napi_struct *napi[DPMAIF_RXQ_CNT_MAX]; + + enum dpmaif_state dpmaif_state; + bool dpmaif_user_ready; + bool trans_enabled; + struct mutex trans_ctl_lock; /* protect structure fields */ + const struct dpmaif_res_cfg *res_cfg; + + struct dpmaif_cmd_srv cmd_srv; + struct dpmaif_vq cmd_vq; + struct dpmaif_tx_srv *tx_srvs; + struct dpmaif_vq *tx_vqs; + + struct workqueue_struct *tx_done_wq; + struct workqueue_struct *tx_doorbell_wq; + struct dpmaif_txq *txqs; + struct dpmaif_rxq *rxqs; + struct dpmaif_bat_info bat_info; + bool irq_enabled; + struct dpmaif_irq_param *irq_params; + + unsigned long dump_flag; +}; + +struct dpmaif_pkt_info { + unsigned char intf_id; + unsigned char drb_cnt; +}; + +#define DPMAIF_SKB_CB(__skb) ((struct dpmaif_pkt_info *)&((__skb)->cb[0])) + +#define DCB_TO_DEV(dcb) ((dcb)->data_blk->mdev->dev) +#define DCB_TO_MDEV(dcb) ((dcb)->data_blk->mdev) +#define DCB_TO_DEV_STR(dcb) ((dcb)->data_blk->mdev->dev_str) +#define DPMAIF_GET_HW_VER(dcb) ((dcb)->data_blk->mdev->hw_ver) +#define DPMAIF_GET_DRB_CNT(__skb) (skb_shinfo(__skb)->nr_frags + 1 + 1) + +#define DPMAIF_JUMBO_SIZE 9000 +#define DPMAIF_DFLT_MTU 3000 +#define DPMAIF_DL_BUF_MIN_SIZE 128 +#define DPMAIF_BUF_THRESHOLD (DPMAIF_DL_BUF_MIN_SIZE * 28) /* 3.5k, should= be less than page size */ +#define DPMAIF_NORMAL_BUF_SIZE_IN_JUMBO (128 * 13) /* 1664 */ + +static unsigned int mtk_dpmaif_ring_buf_get_next_idx(unsigned int buf_len,= unsigned int buf_idx) +{ + return (++buf_idx) % buf_len; +} + +static unsigned int mtk_dpmaif_ring_buf_readable(unsigned int total_cnt, u= nsigned int rd_idx, + unsigned int wr_idx) +{ + unsigned int pkt_cnt; + + if (wr_idx >=3D rd_idx) + pkt_cnt =3D wr_idx - rd_idx; + else + pkt_cnt =3D total_cnt + wr_idx - rd_idx; + + return pkt_cnt; +} + +static unsigned int mtk_dpmaif_ring_buf_writable(unsigned int total_cnt, u= nsigned int rel_idx, + unsigned int wr_idx) +{ + unsigned int pkt_cnt; + + if (wr_idx < rel_idx) + pkt_cnt =3D rel_idx - wr_idx - 1; + else + pkt_cnt =3D total_cnt + rel_idx - wr_idx - 1; + + return pkt_cnt; +} + +static unsigned int mtk_dpmaif_ring_buf_releasable(unsigned int total_cnt,= unsigned int rel_idx, + unsigned int rd_idx) +{ + unsigned int pkt_cnt; + + if (rel_idx <=3D rd_idx) + pkt_cnt =3D rd_idx - rel_idx; + else + pkt_cnt =3D total_cnt + rd_idx - rel_idx; + + return pkt_cnt; +} + +static void mtk_dpmaif_common_err_handle(struct mtk_dpmaif_ctlb *dcb, bool= is_hw) +{ + if (!is_hw) { + dev_err(DCB_TO_DEV(dcb), "ASSERT file: %s, function: %s, line %d", + __FILE__, __func__, __LINE__); + return; + } + + if (mtk_hw_mmio_check(DCB_TO_MDEV(dcb))) + dev_err(DCB_TO_DEV(dcb), "Failed to access mmio\n"); + else + mtk_hw_send_ext_evt(DCB_TO_MDEV(dcb), EXT_EVT_H2D_RESERVED_FOR_DPMAIF); +} + +static unsigned int mtk_dpmaif_pit_bid(struct dpmaif_pd_pit *pit_info) +{ + unsigned int buf_id =3D FIELD_GET(PIT_PD_H_BID, le32_to_cpu(pit_info->pd_= footer)) << 13; + + return buf_id + FIELD_GET(PIT_PD_BUF_ID, le32_to_cpu(pit_info->pd_header)= ); +} + +static void mtk_dpmaif_disable_irq(struct mtk_dpmaif_ctlb *dcb) +{ + unsigned char irq_cnt =3D dcb->res_cfg->irq_cnt; + struct dpmaif_irq_param *irq_param; + int i; + + if (!dcb->irq_enabled) + return; + + dcb->irq_enabled =3D false; + for (i =3D 0; i < irq_cnt; i++) { + irq_param =3D &dcb->irq_params[i]; + if (mtk_hw_mask_irq(DCB_TO_MDEV(dcb), irq_param->dev_irq_id) !=3D 0) + dev_err(DCB_TO_DEV(dcb), "Failed mask dev irq%d\n", irq_param->dev_irq_= id); + } +} + +static void mtk_dpmaif_enable_irq(struct mtk_dpmaif_ctlb *dcb) +{ + unsigned char irq_cnt =3D dcb->res_cfg->irq_cnt; + struct dpmaif_irq_param *irq_param; + int i; + + if (dcb->irq_enabled) + return; + + dcb->irq_enabled =3D true; + for (i =3D 0; i < irq_cnt; i++) { + irq_param =3D &dcb->irq_params[i]; + if (mtk_hw_unmask_irq(DCB_TO_MDEV(dcb), irq_param->dev_irq_id) !=3D 0) + dev_err(DCB_TO_DEV(dcb), "Fail unmask dev irq%d\n", irq_param->dev_irq_= id); + } +} + +static int mtk_dpmaif_set_rx_bat(struct mtk_dpmaif_ctlb *dcb, struct dpmai= f_bat_ring *bat_ring, + unsigned int bat_cnt) +{ + unsigned short old_sw_rel_rd_idx, new_sw_wr_idx, old_sw_wr_idx; + int ret =3D 0; + + old_sw_rel_rd_idx =3D bat_ring->bat_rel_rd_idx; + old_sw_wr_idx =3D bat_ring->bat_wr_idx; + new_sw_wr_idx =3D old_sw_wr_idx + bat_cnt; + + if (old_sw_rel_rd_idx > old_sw_wr_idx) { + if (new_sw_wr_idx >=3D old_sw_rel_rd_idx) + ret =3D -DATA_FLOW_CHK_ERR; + } else { + if (new_sw_wr_idx >=3D bat_ring->bat_cnt) { + new_sw_wr_idx =3D new_sw_wr_idx - bat_ring->bat_cnt; + if (new_sw_wr_idx >=3D old_sw_rel_rd_idx) + ret =3D -DATA_FLOW_CHK_ERR; + } + } + + if (ret < 0) { + dev_err(DCB_TO_DEV(dcb), "Failed check bat, new_sw_wr_idx=3D%u, old_sw_r= l_idx=3D%u\n", + new_sw_wr_idx, old_sw_rel_rd_idx); + goto out; + } + + bat_ring->bat_wr_idx =3D new_sw_wr_idx; +out: + return ret; +} + +static int mtk_dpmaif_reload_rx_skb(struct mtk_dpmaif_ctlb *dcb, + struct dpmaif_bat_ring *bat_ring, unsigned int buf_cnt) +{ + union dpmaif_bat_record *cur_bat_record; + struct skb_mapped_t *skb_info; + unsigned short cur_bat_idx; + struct dpmaif_bat *cur_bat; + unsigned int i; + int ret; + + cur_bat_idx =3D bat_ring->bat_wr_idx; + for (i =3D 0 ; i < buf_cnt; i++) { + cur_bat_record =3D bat_ring->sw_record_base + cur_bat_idx; + skb_info =3D &cur_bat_record->normal; + if (!skb_info->skb) { + skb_info->skb =3D __dev_alloc_skb(dcb->bat_info.normal_bat_ring.buf_siz= e, + GFP_KERNEL); + if (unlikely(!skb_info->skb)) { + dev_err(DCB_TO_DEV(dcb), "Failed to alloc skb, bat%d buf_cnt:%u/%u\n", + bat_ring->type, buf_cnt, i); + break; + } + + skb_info->data_len =3D bat_ring->buf_size; + skb_info->data_dma_addr =3D dma_map_single(DCB_TO_MDEV(dcb)->dev, + skb_info->skb->data, + skb_info->data_len, + DMA_FROM_DEVICE); + ret =3D dma_mapping_error(DCB_TO_MDEV(dcb)->dev, skb_info->data_dma_add= r); + if (unlikely(ret)) { + dev_err(DCB_TO_MDEV(dcb)->dev, "Failed to map dma!\n"); + dev_kfree_skb_any(skb_info->skb); + skb_info->skb =3D NULL; + break; + } + } + + cur_bat =3D bat_ring->bat_base + cur_bat_idx; + cur_bat->buf_addr_high =3D cpu_to_le32(upper_32_bits(skb_info->data_dma_= addr)); + cur_bat->buf_addr_low =3D cpu_to_le32(lower_32_bits(skb_info->data_dma_a= ddr)); + cur_bat_idx =3D mtk_dpmaif_ring_buf_get_next_idx(bat_ring->bat_cnt, cur_= bat_idx); + } + + ret =3D i; + if (unlikely(ret =3D=3D 0)) + ret =3D -DATA_LOW_MEM_SKB; + + return ret; +} + +static int mtk_dpmaif_reload_rx_buf(struct mtk_dpmaif_ctlb *dcb, struct dp= maif_bat_ring *bat_ring, + unsigned int buf_cnt, bool send_doorbell) +{ + unsigned int reload_cnt; + int ret =3D 0; + + ret =3D mtk_dpmaif_reload_rx_skb(dcb, bat_ring, buf_cnt); + if (ret < 0) + return -DATA_LOW_MEM_SKB; + + reload_cnt =3D ret; + ret =3D mtk_dpmaif_set_rx_bat(dcb, bat_ring, reload_cnt); + if (unlikely(ret < 0)) { + dev_err(DCB_TO_DEV(dcb), "Failed to update bat_wr_idx\n"); + goto out; + } + + dma_wmb(); + + if (send_doorbell) { + ret =3D mtk_dpmaif_drv_send_doorbell(dcb->drv_info, DPMAIF_BAT, 0, reloa= d_cnt); + if (unlikely(ret < 0)) { + dev_err(DCB_TO_DEV(dcb), "Failed to send bat doorbell\n"); + mtk_dpmaif_common_err_handle(dcb, true); + goto out; + } + } + + return 0; +out: + return ret; +} + +static unsigned int mtk_dpmaif_chk_rel_bat_cnt(struct mtk_dpmaif_ctlb *dcb, + struct dpmaif_bat_ring *bat_ring) +{ + unsigned int i, cur_idx; + unsigned int count =3D 0; + unsigned char mask_val; + + cur_idx =3D bat_ring->bat_rel_rd_idx; + for (i =3D 0; i < bat_ring->bat_cnt; i++) { + mask_val =3D bat_ring->mask_tbl[cur_idx]; + if (mask_val =3D=3D 1) + count++; + else + break; + + cur_idx =3D mtk_dpmaif_ring_buf_get_next_idx(bat_ring->bat_cnt, cur_idx); + } + + return count; +} + +static int mtk_dpmaif_recycle_bat(struct mtk_dpmaif_ctlb *dcb, struct dpma= if_bat_ring *bat_ring, + unsigned int rel_bat_cnt) +{ + unsigned short old_sw_rel_idx, new_sw_rel_idx, hw_rd_idx; + unsigned int cur_idx; + unsigned int i; + int ret; + + old_sw_rel_idx =3D bat_ring->bat_rel_rd_idx; + new_sw_rel_idx =3D old_sw_rel_idx + rel_bat_cnt; + + ret =3D mtk_dpmaif_drv_get_ring_idx(dcb->drv_info, DPMAIF_BAT_RIDX, 0); + if (unlikely(ret < 0)) { + mtk_dpmaif_common_err_handle(dcb, true); + return ret; + } + + hw_rd_idx =3D ret; + bat_ring->bat_rd_idx =3D hw_rd_idx; + if (bat_ring->bat_wr_idx =3D=3D old_sw_rel_idx) { + ret =3D -DATA_FLOW_CHK_ERR; + goto out; + } + + if (hw_rd_idx > old_sw_rel_idx) { + if (new_sw_rel_idx > hw_rd_idx) { + ret =3D -DATA_FLOW_CHK_ERR; + goto out; + } + } else if (hw_rd_idx < old_sw_rel_idx) { + if (new_sw_rel_idx >=3D bat_ring->bat_cnt) { + new_sw_rel_idx =3D new_sw_rel_idx - bat_ring->bat_cnt; + if (new_sw_rel_idx > hw_rd_idx) { + ret =3D -DATA_FLOW_CHK_ERR; + goto out; + } + } + } + + cur_idx =3D bat_ring->bat_rel_rd_idx; + for (i =3D 0; i < rel_bat_cnt; i++) { + bat_ring->mask_tbl[cur_idx] =3D 0; + cur_idx =3D mtk_dpmaif_ring_buf_get_next_idx(bat_ring->bat_cnt, cur_idx); + } + + bat_ring->bat_rel_rd_idx =3D new_sw_rel_idx; + + return rel_bat_cnt; + +out: + dev_err(DCB_TO_DEV(dcb), + "Failed to check bat%d rel_rd_idx, bat_rd=3D%u,old_sw_rel=3D%u, new_sw_r= el=3D%u\n", + bat_ring->type, bat_ring->bat_rd_idx, old_sw_rel_idx, new_sw_rel_idx); + + return ret; +} + +static int mtk_dpmaif_reload_bat(struct mtk_dpmaif_ctlb *dcb, struct dpmai= f_bat_ring *bat_ring) +{ + unsigned int rel_bat_cnt; + int ret =3D 0; + + rel_bat_cnt =3D mtk_dpmaif_chk_rel_bat_cnt(dcb, bat_ring); + if (unlikely(rel_bat_cnt =3D=3D 0)) + goto out; + + ret =3D mtk_dpmaif_recycle_bat(dcb, bat_ring, rel_bat_cnt); + if (unlikely(ret < 0)) + goto out; + + ret =3D mtk_dpmaif_reload_rx_buf(dcb, bat_ring, rel_bat_cnt, true); +out: + return ret; +} + +static void mtk_dpmaif_bat_reload_work(struct work_struct *work) +{ + struct dpmaif_bat_ring *bat_ring; + struct dpmaif_bat_info *bat_info; + struct mtk_dpmaif_ctlb *dcb; + int ret; + + bat_ring =3D container_of(work, struct dpmaif_bat_ring, reload_work); + bat_info =3D container_of(bat_ring, struct dpmaif_bat_info, normal_bat_ri= ng); + dcb =3D bat_info->dcb; + + ret =3D mtk_dpmaif_reload_bat(dcb, bat_ring); + if (unlikely(ret < 0)) { + dev_err(DCB_TO_DEV(dcb), "Failed to recycle normal bat and reload rx buf= fer\n"); + return; + } +} + +static void mtk_dpmaif_set_bat_buf_size(struct mtk_dpmaif_ctlb *dcb, unsig= ned int mtu) +{ + struct dpmaif_bat_info *bat_info =3D &dcb->bat_info; + unsigned int buf_size; + + bat_info->max_mtu =3D mtu; + buf_size =3D mtu + DPMAIF_HW_PKT_ALIGN + DPMAIF_HW_BAT_RSVLEN; + bat_info->normal_bat_ring.buf_size =3D ALIGN(buf_size, DPMAIF_DL_BUF_MIN_= SIZE); +} + +static int mtk_dpmaif_bat_init(struct mtk_dpmaif_ctlb *dcb, struct dpmaif_= bat_ring *bat_ring, + enum dpmaif_bat_type type) +{ + int ret; + + bat_ring->type =3D type; + bat_ring->bat_cnt =3D dcb->res_cfg->normal_bat_cnt; + bat_ring->bat_rd_idx =3D 0; + bat_ring->bat_wr_idx =3D 0; + bat_ring->bat_rel_rd_idx =3D 0; + bat_ring->bat_base =3D dma_alloc_coherent(DCB_TO_DEV(dcb), bat_ring->bat_= cnt * + sizeof(*bat_ring->bat_base), + &bat_ring->bat_dma_addr, GFP_KERNEL); + if (!bat_ring->bat_base) { + dev_err(DCB_TO_DEV(dcb), "Failed to allocate bat%d\n", bat_ring->type); + return -ENOMEM; + } + + bat_ring->sw_record_base =3D devm_kcalloc(DCB_TO_DEV(dcb), bat_ring->bat_= cnt, + sizeof(*bat_ring->sw_record_base), GFP_KERNEL); + if (!bat_ring->sw_record_base) { + ret =3D -ENOMEM; + goto free_bat_buf; + } + + bat_ring->mask_tbl =3D devm_kcalloc(DCB_TO_DEV(dcb), bat_ring->bat_cnt, + sizeof(*bat_ring->mask_tbl), GFP_KERNEL); + if (!bat_ring->mask_tbl) { + ret =3D -ENOMEM; + goto free_mask_tbl; + } + + INIT_WORK(&bat_ring->reload_work, mtk_dpmaif_bat_reload_work); + + return 0; + +free_mask_tbl: + devm_kfree(DCB_TO_DEV(dcb), bat_ring->sw_record_base); + bat_ring->sw_record_base =3D NULL; +free_bat_buf: + dma_free_coherent(DCB_TO_DEV(dcb), bat_ring->bat_cnt * sizeof(*bat_ring->= bat_base), + bat_ring->bat_base, bat_ring->bat_dma_addr); + bat_ring->bat_base =3D NULL; + + return ret; +} + +static void mtk_dpmaif_bat_exit(struct mtk_dpmaif_ctlb *dcb, struct dpmaif= _bat_ring *bat_ring, + enum dpmaif_bat_type type) +{ + union dpmaif_bat_record *bat_record; + unsigned int i; + + flush_work(&bat_ring->reload_work); + devm_kfree(DCB_TO_DEV(dcb), bat_ring->mask_tbl); + bat_ring->mask_tbl =3D NULL; + + if (bat_ring->sw_record_base) { + for (i =3D 0; i < bat_ring->bat_cnt; i++) { + bat_record =3D bat_ring->sw_record_base + i; + dma_unmap_single(DCB_TO_DEV(dcb), bat_record->normal.data_dma_addr, + bat_record->normal.data_len, DMA_FROM_DEVICE); + dev_kfree_skb_any(bat_record->normal.skb); + } + + devm_kfree(DCB_TO_DEV(dcb), bat_ring->sw_record_base); + bat_ring->sw_record_base =3D NULL; + } + + if (bat_ring->bat_base) { + dma_free_coherent(DCB_TO_DEV(dcb), bat_ring->bat_cnt * sizeof(*bat_ring-= >bat_base), + bat_ring->bat_base, bat_ring->bat_dma_addr); + bat_ring->bat_base =3D NULL; + } +} + +static void mtk_dpmaif_bat_ring_reset(struct dpmaif_bat_ring *bat_ring) +{ + bat_ring->bat_wr_idx =3D 0; + bat_ring->bat_rd_idx =3D 0; + bat_ring->bat_rel_rd_idx =3D 0; + memset(bat_ring->bat_base, 0x00, (bat_ring->bat_cnt * sizeof(*bat_ring->b= at_base))); + memset(bat_ring->mask_tbl, 0x00, (bat_ring->bat_cnt * sizeof(*bat_ring->m= ask_tbl))); +} + +static int mtk_dpmaif_bat_res_init(struct mtk_dpmaif_ctlb *dcb) +{ + struct dpmaif_bat_info *bat_info =3D &dcb->bat_info; + int ret; + + bat_info->dcb =3D dcb; + ret =3D mtk_dpmaif_bat_init(dcb, &bat_info->normal_bat_ring, NORMAL_BAT); + if (ret < 0) { + dev_err(DCB_TO_DEV(dcb), "Failed to initialize normal bat resource\n"); + goto out; + } + + bat_info->reload_wq =3D alloc_workqueue("dpmaif_bat_reload_wq_%s", WQ_HIG= HPRI | WQ_UNBOUND | + WQ_MEM_RECLAIM, 1, DCB_TO_DEV_STR(dcb)); + if (!bat_info->reload_wq) { + dev_err(DCB_TO_DEV(dcb), "Failed to allocate bat reload workqueue\n"); + ret =3D -ENOMEM; + goto exit_bat; + } + + return 0; + +exit_bat: + mtk_dpmaif_bat_exit(dcb, &bat_info->normal_bat_ring, NORMAL_BAT); +out: + return ret; +} + +static void mtk_dpmaif_bat_res_exit(struct mtk_dpmaif_ctlb *dcb) +{ + struct dpmaif_bat_info *bat_info =3D &dcb->bat_info; + + if (bat_info->reload_wq) { + flush_workqueue(bat_info->reload_wq); + destroy_workqueue(bat_info->reload_wq); + bat_info->reload_wq =3D NULL; + } + + mtk_dpmaif_bat_exit(dcb, &bat_info->normal_bat_ring, NORMAL_BAT); +} + +static int mtk_dpmaif_rxq_init(struct mtk_dpmaif_ctlb *dcb, struct dpmaif_= rxq *rxq) +{ + rxq->started =3D false; + rxq->pit_cnt =3D dcb->res_cfg->pit_cnt[rxq->id]; + rxq->pit_wr_idx =3D 0; + rxq->pit_rd_idx =3D 0; + rxq->pit_rel_rd_idx =3D 0; + rxq->pit_seq_expect =3D 0; + rxq->pit_rel_cnt =3D 0; + rxq->pit_burst_rel_cnt =3D DPMAIF_PIT_CNT_UPDATE_THRESHOLD; + rxq->pit_seq_fail_cnt =3D 0; + memset(&rxq->rx_record, 0x00, sizeof(rxq->rx_record)); + + rxq->pit_base =3D dma_alloc_coherent(DCB_TO_DEV(dcb), rxq->pit_cnt * size= of(*rxq->pit_base), + &rxq->pit_dma_addr, GFP_KERNEL); + if (!rxq->pit_base) + return -ENOMEM; + + return 0; +} + +static void mtk_dpmaif_rxq_exit(struct mtk_dpmaif_ctlb *dcb, struct dpmaif= _rxq *rxq) +{ + if (rxq->pit_base) { + dma_free_coherent(DCB_TO_DEV(dcb), + rxq->pit_cnt * sizeof(*rxq->pit_base), rxq->pit_base, + rxq->pit_dma_addr); + rxq->pit_base =3D NULL; + } +} + +static int mtk_dpmaif_sw_stop_rxq(struct mtk_dpmaif_ctlb *dcb, struct dpma= if_rxq *rxq) +{ + rxq->started =3D false; + + /* Make sure rxq->started value update done. */ + smp_mb(); + + napi_synchronize(&rxq->napi); + + return 0; +} + +static void mtk_dpmaif_sw_stop_rx(struct mtk_dpmaif_ctlb *dcb) +{ + unsigned char rxq_cnt =3D dcb->res_cfg->rxq_cnt; + struct dpmaif_rxq *rxq; + int i; + + for (i =3D 0; i < rxq_cnt; i++) { + rxq =3D &dcb->rxqs[i]; + mtk_dpmaif_sw_stop_rxq(dcb, rxq); + } +} + +static void mtk_dpmaif_sw_start_rx(struct mtk_dpmaif_ctlb *dcb) +{ + unsigned char rxq_cnt =3D dcb->res_cfg->rxq_cnt; + struct dpmaif_rxq *rxq; + int i; + + for (i =3D 0; i < rxq_cnt; i++) { + rxq =3D &dcb->rxqs[i]; + rxq->started =3D true; + } +} + +static void mtk_dpmaif_sw_reset_rxq(struct dpmaif_rxq *rxq) +{ + memset(rxq->pit_base, 0x00, (rxq->pit_cnt * sizeof(*rxq->pit_base))); + memset(&rxq->rx_record, 0x00, sizeof(rxq->rx_record)); + + rxq->started =3D false; + rxq->pit_wr_idx =3D 0; + rxq->pit_rd_idx =3D 0; + rxq->pit_rel_rd_idx =3D 0; + rxq->pit_seq_expect =3D 0; + rxq->pit_rel_cnt =3D 0; + rxq->pit_seq_fail_cnt =3D 0; +} + +static void mtk_dpmaif_rx_res_reset(struct mtk_dpmaif_ctlb *dcb) +{ + unsigned char rxq_cnt =3D dcb->res_cfg->rxq_cnt; + struct dpmaif_rxq *rxq; + int i; + + for (i =3D 0; i < rxq_cnt; i++) { + rxq =3D &dcb->rxqs[i]; + mtk_dpmaif_sw_reset_rxq(rxq); + } +} + +static int mtk_dpmaif_rx_res_init(struct mtk_dpmaif_ctlb *dcb) +{ + unsigned char rxq_cnt =3D dcb->res_cfg->rxq_cnt; + struct dpmaif_rxq *rxq; + int i, j; + int ret; + + dcb->rxqs =3D devm_kcalloc(DCB_TO_DEV(dcb), rxq_cnt, sizeof(*rxq), GFP_KE= RNEL); + if (!dcb->rxqs) + return -ENOMEM; + + for (i =3D 0; i < rxq_cnt; i++) { + rxq =3D &dcb->rxqs[i]; + rxq->id =3D i; + rxq->dcb =3D dcb; + ret =3D mtk_dpmaif_rxq_init(dcb, rxq); + if (ret < 0) { + dev_err(DCB_TO_DEV(dcb), "Failed to init rxq%u resource\n", rxq->id); + goto exit_rxq; + } + } + + return 0; + +exit_rxq: + for (j =3D i - 1; j >=3D 0; j--) + mtk_dpmaif_rxq_exit(dcb, &dcb->rxqs[j]); + + devm_kfree(DCB_TO_DEV(dcb), dcb->rxqs); + dcb->rxqs =3D NULL; + + return ret; +} + +static void mtk_dpmaif_rx_res_exit(struct mtk_dpmaif_ctlb *dcb) +{ + unsigned char rxq_cnt =3D dcb->res_cfg->rxq_cnt; + int i; + + for (i =3D 0; i < rxq_cnt; i++) + mtk_dpmaif_rxq_exit(dcb, &dcb->rxqs[i]); + + devm_kfree(DCB_TO_DEV(dcb), dcb->rxqs); + dcb->rxqs =3D NULL; +} + +static void mtk_dpmaif_tx_doorbell(struct work_struct *work) +{ + struct delayed_work *dwork =3D to_delayed_work(work); + struct mtk_dpmaif_ctlb *dcb; + unsigned int to_submit_cnt; + struct dpmaif_txq *txq; + int ret; + + txq =3D container_of(dwork, struct dpmaif_txq, doorbell_work); + dcb =3D txq->dcb; + to_submit_cnt =3D atomic_read(&txq->to_submit_cnt); + + if (to_submit_cnt > 0) { + ret =3D mtk_dpmaif_drv_send_doorbell(dcb->drv_info, DPMAIF_DRB, + txq->id, to_submit_cnt); + if (unlikely(ret < 0)) { + dev_err(DCB_TO_DEV(dcb), "Failed to send txq%u doorbell\n", txq->id); + mtk_dpmaif_common_err_handle(dcb, true); + } + + atomic_sub(to_submit_cnt, &txq->to_submit_cnt); + } +} + +static unsigned int mtk_dpmaif_poll_tx_drb(struct dpmaif_txq *txq) +{ + unsigned short old_sw_rd_idx, new_hw_rd_idx; + struct mtk_dpmaif_ctlb *dcb =3D txq->dcb; + unsigned int drb_cnt; + int ret; + + old_sw_rd_idx =3D txq->drb_rd_idx; + ret =3D mtk_dpmaif_drv_get_ring_idx(dcb->drv_info, DPMAIF_DRB_RIDX, txq->= id); + if (unlikely(ret < 0)) { + dev_err(DCB_TO_DEV(dcb), "Failed to read txq%u drb_rd_idx, ret=3D%d\n", = txq->id, ret); + mtk_dpmaif_common_err_handle(dcb, true); + return 0; + } + + new_hw_rd_idx =3D ret; + + if (old_sw_rd_idx <=3D new_hw_rd_idx) + drb_cnt =3D new_hw_rd_idx - old_sw_rd_idx; + else + drb_cnt =3D txq->drb_cnt - old_sw_rd_idx + new_hw_rd_idx; + + txq->drb_rd_idx =3D new_hw_rd_idx; + + return drb_cnt; +} + +static int mtk_dpmaif_tx_rel_internal(struct dpmaif_txq *txq, + unsigned int rel_cnt, unsigned int *real_rel_cnt) +{ + struct dpmaif_pd_drb *cur_drb =3D NULL, *drb_base =3D txq->drb_base; + struct mtk_dpmaif_ctlb *dcb =3D txq->dcb; + struct dpmaif_drb_skb *cur_drb_skb; + struct dpmaif_msg_drb *msg_drb; + struct sk_buff *skb_free; + unsigned short cur_idx; + unsigned int i; + + cur_idx =3D txq->drb_rel_rd_idx; + for (i =3D 0 ; i < rel_cnt; i++) { + cur_drb =3D drb_base + cur_idx; + cur_drb_skb =3D txq->sw_drb_base + cur_idx; + if (FIELD_GET(DRB_PD_DTYP, le32_to_cpu(cur_drb->pd_header)) =3D=3D PD_DR= B) { + dma_unmap_single(DCB_TO_MDEV(dcb)->dev, cur_drb_skb->data_dma_addr, + cur_drb_skb->data_len, DMA_TO_DEVICE); + + if (FIELD_GET(DRB_PD_CONT, le32_to_cpu(cur_drb->pd_header)) =3D=3D + DPMAIF_DRB_LASTONE) { + skb_free =3D cur_drb_skb->skb; + if (!skb_free) { + dev_err(DCB_TO_DEV(dcb), "release_cnt=3D%u, cur_id=3D%u\n", + rel_cnt, i); + mtk_dpmaif_common_err_handle(dcb, false); + return -DATA_FLOW_CHK_ERR; + } + + dev_kfree_skb_any(skb_free); + } + } else { + msg_drb =3D (struct dpmaif_msg_drb *)cur_drb; + txq->last_ch_id =3D FIELD_GET(DRB_MSG_CHNL_ID, + le32_to_cpu(msg_drb->msg_header2)); + } + + cur_drb_skb->skb =3D NULL; + txq->drb_rel_rd_idx =3D mtk_dpmaif_ring_buf_get_next_idx(txq->drb_cnt, c= ur_idx); + + atomic_inc(&txq->budget); + } + + *real_rel_cnt =3D i; + + return 0; +} + +static int mtk_dpmaif_tx_rel(struct dpmaif_txq *txq) +{ + unsigned int real_rel_cnt =3D 0; + int ret =3D 0, rel_cnt; + + mtk_dpmaif_poll_tx_drb(txq); + + rel_cnt =3D mtk_dpmaif_ring_buf_releasable(txq->drb_cnt, txq->drb_rel_rd_= idx, + txq->drb_rd_idx); + if (likely(rel_cnt > 0)) + ret =3D mtk_dpmaif_tx_rel_internal(txq, rel_cnt, &real_rel_cnt); + + return ret; +} + +static void mtk_dpmaif_tx_done(struct work_struct *work) +{ + struct delayed_work *dwork =3D to_delayed_work(work); + struct mtk_dpmaif_ctlb *dcb; + struct dpmaif_txq *txq; + + txq =3D container_of(dwork, struct dpmaif_txq, tx_done_work); + dcb =3D txq->dcb; + + mtk_dpmaif_tx_rel(txq); + + if (mtk_dpmaif_poll_tx_drb(txq) > 0) { + mtk_dpmaif_drv_clear_ip_busy(dcb->drv_info); + mtk_dpmaif_drv_intr_complete(dcb->drv_info, DPMAIF_INTR_UL_DONE, + txq->id, DPMAIF_CLEAR_INTR); + queue_delayed_work(dcb->tx_done_wq, &txq->tx_done_work, msecs_to_jiffies= (0)); + } else { + mtk_dpmaif_drv_clear_ip_busy(dcb->drv_info); + mtk_dpmaif_drv_intr_complete(dcb->drv_info, DPMAIF_INTR_UL_DONE, + txq->id, DPMAIF_UNMASK_INTR); + } +} + +static int mtk_dpmaif_txq_init(struct mtk_dpmaif_ctlb *dcb, struct dpmaif_= txq *txq) +{ + unsigned int drb_cnt =3D dcb->res_cfg->drb_cnt[txq->id]; + int ret; + + atomic_set(&txq->budget, drb_cnt); + atomic_set(&txq->to_submit_cnt, 0); + txq->drb_cnt =3D drb_cnt; + txq->drb_wr_idx =3D 0; + txq->drb_rd_idx =3D 0; + txq->drb_rel_rd_idx =3D 0; + txq->last_ch_id =3D 0; + txq->doorbell_delay =3D dcb->res_cfg->txq_doorbell_delay[txq->id]; + + txq->drb_base =3D dma_alloc_coherent(DCB_TO_DEV(dcb), txq->drb_cnt * size= of(*txq->drb_base), + &txq->drb_dma_addr, GFP_KERNEL); + if (!txq->drb_base) + return -ENOMEM; + + txq->sw_drb_base =3D devm_kcalloc(DCB_TO_DEV(dcb), txq->drb_cnt, + sizeof(*txq->sw_drb_base), GFP_KERNEL); + if (!txq->sw_drb_base) { + ret =3D -ENOMEM; + goto free_drb_buf; + } + + INIT_DELAYED_WORK(&txq->tx_done_work, mtk_dpmaif_tx_done); + INIT_DELAYED_WORK(&txq->doorbell_work, mtk_dpmaif_tx_doorbell); + + return 0; + +free_drb_buf: + dma_free_coherent(DCB_TO_DEV(dcb), txq->drb_cnt * sizeof(*txq->drb_base), + txq->drb_base, txq->drb_dma_addr); + txq->drb_base =3D NULL; + + return ret; +} + +static void mtk_dpmaif_txq_exit(struct mtk_dpmaif_ctlb *dcb, struct dpmaif= _txq *txq) +{ + struct dpmaif_drb_skb *drb_skb; + int i; + + if (txq->drb_base) { + dma_free_coherent(DCB_TO_DEV(dcb), txq->drb_cnt * sizeof(*txq->drb_base), + txq->drb_base, txq->drb_dma_addr); + txq->drb_base =3D NULL; + } + + if (txq->sw_drb_base) { + for (i =3D 0; i < txq->drb_cnt; i++) { + drb_skb =3D txq->sw_drb_base + i; + if (drb_skb->skb) { + if (drb_skb->data_dma_addr) + dma_unmap_single(DCB_TO_MDEV(dcb)->dev, + drb_skb->data_dma_addr, drb_skb->data_len, + DMA_TO_DEVICE); + if (drb_skb->is_last) { + dev_kfree_skb_any(drb_skb->skb); + drb_skb->skb =3D NULL; + } + } + } + + devm_kfree(DCB_TO_DEV(dcb), txq->sw_drb_base); + txq->sw_drb_base =3D NULL; + } +} + +static void mtk_dpmaif_sw_wait_tx_stop(struct mtk_dpmaif_ctlb *dcb) +{ + unsigned char txq_cnt =3D dcb->res_cfg->txq_cnt; + int i; + + for (i =3D 0; i < txq_cnt; i++) + flush_delayed_work(&dcb->txqs[i].tx_done_work); +} + +static void mtk_dpmaif_sw_reset_txq(struct dpmaif_txq *txq) +{ + struct dpmaif_drb_skb *drb_skb; + int i; + + for (i =3D 0; i < txq->drb_cnt; i++) { + drb_skb =3D txq->sw_drb_base + i; + if (drb_skb->skb) { + dma_unmap_single(DCB_TO_MDEV(txq->dcb)->dev, + drb_skb->data_dma_addr, drb_skb->data_len, DMA_TO_DEVICE); + if (drb_skb->is_last) { + dev_kfree_skb_any(drb_skb->skb); + drb_skb->skb =3D NULL; + } + } + } + + memset(txq->drb_base, 0x00, (txq->drb_cnt * sizeof(*txq->drb_base))); + memset(txq->sw_drb_base, 0x00, (txq->drb_cnt * sizeof(*txq->sw_drb_base))= ); + + atomic_set(&txq->budget, txq->drb_cnt); + atomic_set(&txq->to_submit_cnt, 0); + txq->drb_rd_idx =3D 0; + txq->drb_wr_idx =3D 0; + txq->drb_rel_rd_idx =3D 0; + txq->last_ch_id =3D 0; +} + +static void mtk_dpmaif_tx_res_reset(struct mtk_dpmaif_ctlb *dcb) +{ + unsigned char txq_cnt =3D dcb->res_cfg->txq_cnt; + struct dpmaif_txq *txq; + int i; + + for (i =3D 0; i < txq_cnt; i++) { + txq =3D &dcb->txqs[i]; + mtk_dpmaif_sw_reset_txq(txq); + } +} + +static int mtk_dpmaif_tx_res_init(struct mtk_dpmaif_ctlb *dcb) +{ + unsigned char txq_cnt =3D dcb->res_cfg->txq_cnt; + struct dpmaif_txq *txq; + int i, j; + int ret; + + dcb->txqs =3D devm_kcalloc(DCB_TO_DEV(dcb), txq_cnt, sizeof(*txq), GFP_KE= RNEL); + if (!dcb->txqs) + return -ENOMEM; + + for (i =3D 0; i < txq_cnt; i++) { + txq =3D &dcb->txqs[i]; + txq->id =3D i; + txq->dcb =3D dcb; + ret =3D mtk_dpmaif_txq_init(dcb, txq); + if (ret < 0) { + dev_err(DCB_TO_DEV(dcb), "Failed to init txq%d resource\n", txq->id); + goto exit_txq; + } + } + + dcb->tx_done_wq =3D alloc_workqueue("dpmaif_tx_done_wq_%s", + WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_HIGHPRI, + txq_cnt, DCB_TO_DEV_STR(dcb)); + if (!dcb->tx_done_wq) { + dev_err(DCB_TO_DEV(dcb), "Failed to allocate tx done workqueue\n"); + ret =3D -ENOMEM; + goto exit_txq; + } + + dcb->tx_doorbell_wq =3D alloc_workqueue("dpmaif_tx_doorbell_wq_%s", + WQ_FREEZABLE | WQ_UNBOUND | + WQ_MEM_RECLAIM | WQ_HIGHPRI, + txq_cnt, DCB_TO_DEV_STR(dcb)); + if (!dcb->tx_doorbell_wq) { + dev_err(DCB_TO_DEV(dcb), "Failed to allocate tx doorbell workqueue\n"); + ret =3D -ENOMEM; + goto flush_tx_doorbell_wq; + } + + return 0; + +flush_tx_doorbell_wq: + flush_workqueue(dcb->tx_done_wq); + destroy_workqueue(dcb->tx_done_wq); + +exit_txq: + for (j =3D i - 1; j >=3D 0; j--) + mtk_dpmaif_txq_exit(dcb, &dcb->txqs[j]); + + devm_kfree(DCB_TO_DEV(dcb), dcb->txqs); + dcb->txqs =3D NULL; + + return ret; +} + +static void mtk_dpmaif_tx_res_exit(struct mtk_dpmaif_ctlb *dcb) +{ + unsigned char txq_cnt =3D dcb->res_cfg->txq_cnt; + struct dpmaif_txq *txq; + int i; + + for (i =3D 0; i < txq_cnt; i++) { + txq =3D &dcb->txqs[i]; + flush_delayed_work(&txq->tx_done_work); + flush_delayed_work(&txq->doorbell_work); + } + + if (dcb->tx_doorbell_wq) { + flush_workqueue(dcb->tx_doorbell_wq); + destroy_workqueue(dcb->tx_doorbell_wq); + dcb->tx_doorbell_wq =3D NULL; + } + + if (dcb->tx_done_wq) { + flush_workqueue(dcb->tx_done_wq); + destroy_workqueue(dcb->tx_done_wq); + dcb->tx_done_wq =3D NULL; + } + + for (i =3D 0; i < txq_cnt; i++) + mtk_dpmaif_txq_exit(dcb, &dcb->txqs[i]); + + devm_kfree(DCB_TO_DEV(dcb), dcb->txqs); + dcb->txqs =3D NULL; +} + +static int mtk_dpmaif_sw_res_init(struct mtk_dpmaif_ctlb *dcb) +{ + int ret; + + ret =3D mtk_dpmaif_bat_res_init(dcb); + if (ret < 0) { + dev_err(DCB_TO_DEV(dcb), "Failed to initialize bat reource, ret=3D%d\n",= ret); + goto out; + } + + ret =3D mtk_dpmaif_rx_res_init(dcb); + if (ret < 0) { + dev_err(DCB_TO_DEV(dcb), "Failed to initialize rx reource, ret=3D%d\n", = ret); + goto exit_rx_res; + } + + ret =3D mtk_dpmaif_tx_res_init(dcb); + if (ret < 0) { + dev_err(DCB_TO_DEV(dcb), "Failed to initialize tx reource, ret=3D%d\n", = ret); + goto exit_tx_res; + } + + return 0; + +exit_tx_res: + mtk_dpmaif_rx_res_exit(dcb); +exit_rx_res: + mtk_dpmaif_bat_res_exit(dcb); +out: + return ret; +} + +static void mtk_dpmaif_sw_res_exit(struct mtk_dpmaif_ctlb *dcb) +{ + mtk_dpmaif_tx_res_exit(dcb); + mtk_dpmaif_rx_res_exit(dcb); + mtk_dpmaif_bat_res_exit(dcb); +} + +static bool mtk_dpmaif_all_vqs_empty(struct dpmaif_tx_srv *tx_srv) +{ + bool is_empty =3D true; + struct dpmaif_vq *vq; + int i; + + for (i =3D 0; i < tx_srv->vq_cnt; i++) { + vq =3D tx_srv->vq[i]; + if (!skb_queue_empty(&vq->list)) { + is_empty =3D false; + break; + } + } + + return is_empty; +} + +static bool mtk_dpmaif_all_txqs_drb_lack(struct dpmaif_tx_srv *tx_srv) +{ + return !!tx_srv->txq_drb_lack_sta; +} + +static void mtk_dpmaif_set_drb_msg(struct mtk_dpmaif_ctlb *dcb, unsigned c= har q_id, + unsigned short cur_idx, unsigned int pkt_len, + unsigned short count_l, unsigned char channel_id, + unsigned short network_type) +{ + struct dpmaif_msg_drb *drb =3D (struct dpmaif_msg_drb *)dcb->txqs[q_id].d= rb_base + cur_idx; + + drb->msg_header1 =3D cpu_to_le32(FIELD_PREP(DRB_MSG_DTYP, MSG_DRB) | + FIELD_PREP(DRB_MSG_CONT, DPMAIF_DRB_MORE) | + FIELD_PREP(DRB_MSG_PKT_LEN, pkt_len)); + drb->msg_header2 =3D cpu_to_le32(FIELD_PREP(DRB_MSG_COUNT_L, count_l) | + FIELD_PREP(DRB_MSG_CHNL_ID, channel_id) | + FIELD_PREP(DRB_MSG_L4_CHK, 1) | + FIELD_PREP(DRB_MSG_NET_TYPE, 0)); +} + +static void mtk_dpmaif_set_drb_payload(struct mtk_dpmaif_ctlb *dcb, unsign= ed char q_id, + unsigned short cur_idx, unsigned long long data_addr, + unsigned int pkt_size, char last_one) +{ + struct dpmaif_pd_drb *drb =3D dcb->txqs[q_id].drb_base + cur_idx; + + drb->pd_header =3D cpu_to_le32(FIELD_PREP(DRB_PD_DTYP, PD_DRB)); + if (last_one) + drb->pd_header |=3D cpu_to_le32(FIELD_PREP(DRB_PD_CONT, DPMAIF_DRB_LASTO= NE)); + else + drb->pd_header |=3D cpu_to_le32(FIELD_PREP(DRB_PD_CONT, DPMAIF_DRB_MORE)= ); + + drb->pd_header |=3D cpu_to_le32(FIELD_PREP(DRB_PD_DATA_LEN, pkt_size)); + drb->addr_low =3D cpu_to_le32(lower_32_bits(data_addr)); + drb->addr_high =3D cpu_to_le32(upper_32_bits(data_addr)); +} + +static void mtk_dpmaif_record_drb_skb(struct mtk_dpmaif_ctlb *dcb, unsigne= d char q_id, + unsigned short cur_idx, struct sk_buff *skb, + unsigned short is_msg, + unsigned short is_last, dma_addr_t data_dma_addr, + unsigned int data_len) +{ + struct dpmaif_drb_skb *drb_skb =3D dcb->txqs[q_id].sw_drb_base + cur_idx; + + drb_skb->skb =3D skb; + drb_skb->data_dma_addr =3D data_dma_addr; + drb_skb->data_len =3D data_len; + drb_skb->drb_idx =3D cur_idx; + drb_skb->is_msg =3D is_msg; + drb_skb->is_last =3D is_last; +} + +static int mtk_dpmaif_tx_fill_drb(struct mtk_dpmaif_ctlb *dcb, + unsigned char q_id, struct sk_buff *skb) +{ + unsigned short cur_idx, cur_backup_idx, is_last; + unsigned int send_drb_cnt, wt_cnt, payload_cnt; + struct dpmaif_txq *txq =3D &dcb->txqs[q_id]; + struct dpmaif_drb_skb *cur_drb_skb; + unsigned int data_len; + dma_addr_t data_dma_addr; + void *data_addr; + int i, ret; + + send_drb_cnt =3D DPMAIF_SKB_CB(skb)->drb_cnt; + payload_cnt =3D send_drb_cnt - 1; + cur_idx =3D txq->drb_wr_idx; + cur_backup_idx =3D cur_idx; + + mtk_dpmaif_set_drb_msg(dcb, txq->id, cur_idx, skb->len, 0, DPMAIF_SKB_CB(= skb)->intf_id, + be16_to_cpu(skb->protocol)); + mtk_dpmaif_record_drb_skb(dcb, txq->id, cur_idx, skb, 1, 0, 0, 0); + + cur_idx =3D mtk_dpmaif_ring_buf_get_next_idx(txq->drb_cnt, cur_idx); + for (wt_cnt =3D 0; wt_cnt < payload_cnt; wt_cnt++) { + if (wt_cnt =3D=3D 0) { + data_len =3D skb_headlen(skb); + data_addr =3D skb->data; + } + + if (wt_cnt =3D=3D payload_cnt - 1) + is_last =3D 1; + else + is_last =3D 0; + + data_dma_addr =3D dma_map_single(DCB_TO_MDEV(dcb)->dev, + data_addr, data_len, DMA_TO_DEVICE); + ret =3D dma_mapping_error(DCB_TO_MDEV(dcb)->dev, data_dma_addr); + if (unlikely(ret)) { + dev_err(DCB_TO_MDEV(dcb)->dev, "Failed to map dma!\n"); + ret =3D -DATA_DMA_MAP_ERR; + goto unmap_dma; + } + + mtk_dpmaif_set_drb_payload(dcb, txq->id, cur_idx, data_dma_addr, data_le= n, is_last); + mtk_dpmaif_record_drb_skb(dcb, txq->id, cur_idx, skb, 0, is_last, + data_dma_addr, data_len); + + cur_idx =3D mtk_dpmaif_ring_buf_get_next_idx(txq->drb_cnt, cur_idx); + } + + txq->drb_wr_idx +=3D send_drb_cnt; + if (txq->drb_wr_idx >=3D txq->drb_cnt) + txq->drb_wr_idx -=3D txq->drb_cnt; + + /* Make sure host write memory done before adding to_submit_cnt */ + smp_mb(); + + atomic_sub(send_drb_cnt, &txq->budget); + atomic_add(send_drb_cnt, &txq->to_submit_cnt); + + return 0; + +unmap_dma: + cur_drb_skb =3D txq->sw_drb_base + cur_backup_idx; + mtk_dpmaif_record_drb_skb(dcb, txq->id, cur_idx, NULL, 0, 0, 0, 0); + cur_backup_idx =3D mtk_dpmaif_ring_buf_get_next_idx(txq->drb_cnt, cur_bac= kup_idx); + for (i =3D 0; i < wt_cnt; i++) { + cur_drb_skb =3D txq->sw_drb_base + cur_backup_idx; + dma_unmap_single(DCB_TO_MDEV(dcb)->dev, + cur_drb_skb->data_dma_addr, cur_drb_skb->data_len, DMA_TO_DEVICE); + + cur_backup_idx =3D mtk_dpmaif_ring_buf_get_next_idx(txq->drb_cnt, cur_ba= ckup_idx); + mtk_dpmaif_record_drb_skb(dcb, txq->id, cur_idx, NULL, 0, 0, 0, 0); + } + + return ret; +} + +static int mtk_dpmaif_tx_update_ring(struct mtk_dpmaif_ctlb *dcb, struct d= pmaif_tx_srv *tx_srv, + struct dpmaif_vq *vq) +{ + struct dpmaif_txq *txq =3D &dcb->txqs[vq->q_id]; + unsigned char q_id =3D vq->q_id; + unsigned char skb_drb_cnt; + int i, drb_available_cnt; + struct sk_buff *skb; + int ret; + + drb_available_cnt =3D mtk_dpmaif_ring_buf_writable(txq->drb_cnt, + txq->drb_rel_rd_idx, txq->drb_wr_idx); + + clear_bit(q_id, &tx_srv->txq_drb_lack_sta); + for (i =3D 0; i < DPMAIF_SKB_TX_WEIGHT; i++) { + skb =3D skb_dequeue(&vq->list); + if (!skb) { + ret =3D 0; + break; + } + + skb_drb_cnt =3D DPMAIF_SKB_CB(skb)->drb_cnt; + if (drb_available_cnt < skb_drb_cnt) { + skb_queue_head(&vq->list, skb); + set_bit(q_id, &tx_srv->txq_drb_lack_sta); + ret =3D -DATA_LOW_MEM_DRB; + break; + } + + ret =3D mtk_dpmaif_tx_fill_drb(dcb, q_id, skb); + if (ret < 0) { + skb_queue_head(&vq->list, skb); + break; + } + drb_available_cnt -=3D skb_drb_cnt; + } + + return ret; +} + +static struct dpmaif_vq *mtk_dpmaif_srv_select_vq(struct dpmaif_tx_srv *tx= _srv) +{ + struct dpmaif_vq *vq; + int i; + + for (i =3D 0; i < tx_srv->vq_cnt; i++) { + tx_srv->cur_vq_id =3D tx_srv->cur_vq_id % tx_srv->vq_cnt; + vq =3D tx_srv->vq[tx_srv->cur_vq_id]; + tx_srv->cur_vq_id++; + if (!skb_queue_empty(&vq->list)) + return vq; + } + + return NULL; +} + +static void mtk_dpmaif_tx(struct dpmaif_tx_srv *tx_srv) +{ + struct mtk_dpmaif_ctlb *dcb =3D tx_srv->dcb; + struct dpmaif_txq *txq; + struct dpmaif_vq *vq; + int ret; + + do { + vq =3D mtk_dpmaif_srv_select_vq(tx_srv); + if (!vq) + break; + + ret =3D mtk_dpmaif_tx_update_ring(dcb, tx_srv, vq); + if (unlikely(ret < 0)) { + if (ret =3D=3D -DATA_LOW_MEM_DRB && mtk_dpmaif_all_txqs_drb_lack(tx_srv= )) + usleep_range(50, 100); + } + + txq =3D &dcb->txqs[vq->q_id]; + if (atomic_read(&txq->to_submit_cnt) > 0) + queue_delayed_work(dcb->tx_doorbell_wq, &txq->doorbell_work, + msecs_to_jiffies(txq->doorbell_delay)); + + if (need_resched()) + cond_resched(); + } while (!kthread_should_stop() && (dcb->dpmaif_state =3D=3D DPMAIF_STATE= _PWRON)); +} + +static int mtk_dpmaif_tx_thread(void *arg) +{ + struct dpmaif_tx_srv *tx_srv =3D arg; + struct mtk_dpmaif_ctlb *dcb; + int ret; + + dcb =3D tx_srv->dcb; + set_user_nice(current, tx_srv->prio); + while (!kthread_should_stop()) { + if (mtk_dpmaif_all_vqs_empty(tx_srv) || + dcb->dpmaif_state !=3D DPMAIF_STATE_PWRON) { + ret =3D wait_event_interruptible(tx_srv->wait, + (!mtk_dpmaif_all_vqs_empty(tx_srv) && + (dcb->dpmaif_state =3D=3D DPMAIF_STATE_PWRON)) || + kthread_should_stop()); + if (ret =3D=3D -ERESTARTSYS) + continue; + } + + if (kthread_should_stop()) + break; + + mtk_dpmaif_tx(tx_srv); + } + + return 0; +} + +static int mtk_dpmaif_tx_srvs_start(struct mtk_dpmaif_ctlb *dcb) +{ + unsigned char srvs_cnt =3D dcb->res_cfg->tx_srv_cnt; + struct dpmaif_tx_srv *tx_srv; + int i, j, ret; + + for (i =3D 0; i < srvs_cnt; i++) { + tx_srv =3D &dcb->tx_srvs[i]; + tx_srv->cur_vq_id =3D 0; + tx_srv->txq_drb_lack_sta =3D 0; + tx_srv->srv =3D kthread_run(mtk_dpmaif_tx_thread, tx_srv, "dpmaif_tx_srv= %u_%s", + tx_srv->id, DCB_TO_DEV_STR(dcb)); + if (IS_ERR(tx_srv->srv)) { + dev_err(DCB_TO_DEV(dcb), "Failed to alloc dpmaif tx_srv%u\n", tx_srv->i= d); + ret =3D PTR_ERR(tx_srv->srv); + goto exit_tx_srvs; + } + } + + return 0; + +exit_tx_srvs: + for (j =3D i - 1; j >=3D 0; j--) { + if (tx_srv->srv) + kthread_stop(tx_srv->srv); + tx_srv->srv =3D NULL; + } + + return ret; +} + +static void mtk_dpmaif_tx_srvs_stop(struct mtk_dpmaif_ctlb *dcb) +{ + unsigned char srvs_cnt =3D dcb->res_cfg->tx_srv_cnt; + struct dpmaif_tx_srv *tx_srv; + int i; + + for (i =3D 0; i < srvs_cnt; i++) { + tx_srv =3D &dcb->tx_srvs[i]; + if (tx_srv->srv) + kthread_stop(tx_srv->srv); + + tx_srv->srv =3D NULL; + } +} + +static int mtk_dpmaif_tx_srvs_init(struct mtk_dpmaif_ctlb *dcb) +{ + unsigned char srvs_cnt =3D dcb->res_cfg->tx_srv_cnt; + unsigned char vqs_cnt =3D dcb->res_cfg->tx_vq_cnt; + struct dpmaif_tx_srv *tx_srv; + struct dpmaif_vq *tx_vq; + int i, j, vq_id; + int ret; + + dcb->tx_vqs =3D devm_kcalloc(DCB_TO_DEV(dcb), vqs_cnt, sizeof(*dcb->tx_vq= s), GFP_KERNEL); + if (!dcb->tx_vqs) + return -ENOMEM; + + for (i =3D 0; i < vqs_cnt; i++) { + tx_vq =3D &dcb->tx_vqs[i]; + tx_vq->q_id =3D i; + tx_vq->max_len =3D DEFAULT_TX_QUEUE_LEN; + skb_queue_head_init(&tx_vq->list); + } + + dcb->tx_srvs =3D devm_kcalloc(DCB_TO_DEV(dcb), srvs_cnt, sizeof(*dcb->tx_= srvs), GFP_KERNEL); + if (!dcb->tx_srvs) { + ret =3D -ENOMEM; + goto free_tx_srvs; + } + + for (i =3D 0; i < srvs_cnt; i++) { + tx_srv =3D &dcb->tx_srvs[i]; + tx_srv->dcb =3D dcb; + tx_srv->id =3D i; + tx_srv->prio =3D dcb->res_cfg->srv_prio_tbl[i]; + tx_srv->cur_vq_id =3D 0; + tx_srv->txq_drb_lack_sta =3D 0; + init_waitqueue_head(&tx_srv->wait); + + vq_id =3D 0; + for (j =3D 0; j < vqs_cnt; j++) { + if (tx_srv->id =3D=3D dcb->res_cfg->tx_vq_srv_map[j]) { + tx_srv->vq[vq_id] =3D &dcb->tx_vqs[j]; + vq_id++; + } + } + + tx_srv->vq_cnt =3D vq_id; + } + + return 0; + +free_tx_srvs: + devm_kfree(DCB_TO_DEV(dcb), dcb->tx_vqs); + dcb->tx_vqs =3D NULL; + + return ret; +} + +static void mtk_dpmaif_tx_vqs_reset(struct mtk_dpmaif_ctlb *dcb) +{ + unsigned char vqs_cnt =3D dcb->res_cfg->tx_vq_cnt; + struct dpmaif_vq *tx_vq; + int i; + + for (i =3D 0; i < vqs_cnt; i++) { + tx_vq =3D &dcb->tx_vqs[i]; + if (tx_vq) + skb_queue_purge(&tx_vq->list); + } +} + +static void mtk_dpmaif_tx_srvs_exit(struct mtk_dpmaif_ctlb *dcb) +{ + mtk_dpmaif_tx_srvs_stop(dcb); + devm_kfree(DCB_TO_DEV(dcb), dcb->tx_srvs); + dcb->tx_srvs =3D NULL; + + mtk_dpmaif_tx_vqs_reset(dcb); + devm_kfree(DCB_TO_DEV(dcb), dcb->tx_vqs); + dcb->tx_vqs =3D NULL; +} + +static void mtk_dpmaif_trans_enable(struct mtk_dpmaif_ctlb *dcb) +{ + mtk_dpmaif_sw_start_rx(dcb); + mtk_dpmaif_enable_irq(dcb); + if (!mtk_hw_mmio_check(DCB_TO_MDEV(dcb))) { + if (mtk_dpmaif_drv_start_queue(dcb->drv_info, DPMAIF_RX) < 0) { + dev_err(DCB_TO_DEV(dcb), "Failed to start dpmaif hw rx\n"); + mtk_dpmaif_common_err_handle(dcb, true); + return; + } + + if (mtk_dpmaif_drv_start_queue(dcb->drv_info, DPMAIF_TX) < 0) { + dev_err(DCB_TO_DEV(dcb), "Failed to start dpmaif hw tx\n"); + mtk_dpmaif_common_err_handle(dcb, true); + return; + } + } +} + +static void mtk_dpmaif_trans_disable(struct mtk_dpmaif_ctlb *dcb) +{ + bool io_err =3D false; + + mtk_dpmaif_sw_wait_tx_stop(dcb); + if (!mtk_hw_mmio_check(DCB_TO_MDEV(dcb))) { + if (mtk_dpmaif_drv_stop_queue(dcb->drv_info, DPMAIF_TX) < 0) { + io_err =3D true; + dev_err(DCB_TO_DEV(dcb), "Failed to stop dpmaif hw tx\n"); + } + + if (mtk_dpmaif_drv_stop_queue(dcb->drv_info, DPMAIF_RX) < 0) { + io_err =3D true; + dev_err(DCB_TO_DEV(dcb), "Failed to stop dpmaif hw rx\n"); + } + + if (io_err) + mtk_dpmaif_common_err_handle(dcb, true); + } + + mtk_dpmaif_disable_irq(dcb); + mtk_dpmaif_sw_stop_rx(dcb); + + flush_workqueue(dcb->bat_info.reload_wq); +} + +static void mtk_dpmaif_trans_ctl(struct mtk_dpmaif_ctlb *dcb, bool enable) +{ + mutex_lock(&dcb->trans_ctl_lock); + + if (enable) { + if (!dcb->trans_enabled) { + if (dcb->dpmaif_state =3D=3D DPMAIF_STATE_PWRON && dcb->dpmaif_user_rea= dy) { + mtk_dpmaif_trans_enable(dcb); + dcb->trans_enabled =3D true; + } + } + } else { + if (dcb->trans_enabled) { + if (!(dcb->dpmaif_state =3D=3D DPMAIF_STATE_PWRON) || !dcb->dpmaif_user= _ready) { + mtk_dpmaif_trans_disable(dcb); + dcb->trans_enabled =3D false; + } + } + } + + mutex_unlock(&dcb->trans_ctl_lock); +} + +static void mtk_dpmaif_cmd_trans_ctl(struct mtk_dpmaif_ctlb *dcb, void *da= ta) +{ + struct mtk_data_trans_ctl *trans_ctl =3D data; + + dcb->dpmaif_user_ready =3D trans_ctl->enable; + if (!trans_ctl->enable) + mtk_dpmaif_tx_vqs_reset(dcb); + + mtk_dpmaif_trans_ctl(dcb, trans_ctl->enable); +} + +static void mtk_dpmaif_cmd_handle(struct dpmaif_cmd_srv *srv) +{ + struct mtk_dpmaif_ctlb *dcb =3D srv->dcb; + struct dpmaif_vq *cmd_vq =3D srv->vq; + struct mtk_data_cmd *cmd_info; + struct sk_buff *skb; + int ret; + + while ((skb =3D skb_dequeue(&cmd_vq->list))) { + ret =3D 0; + cmd_info =3D SKB_TO_CMD(skb); + if (dcb->dpmaif_state =3D=3D DPMAIF_STATE_PWRON) { + switch (cmd_info->cmd) { + case DATA_CMD_TRANS_CTL: + mtk_dpmaif_cmd_trans_ctl(dcb, CMD_TO_DATA(cmd_info)); + break; + default: + ret =3D -EOPNOTSUPP; + break; + } + } + cmd_info->ret =3D ret; + if (cmd_info->data_complete) + cmd_info->data_complete(skb); + } +} + +static void mtk_dpmaif_cmd_srv(struct work_struct *work) +{ + struct dpmaif_cmd_srv *srv =3D container_of(work, struct dpmaif_cmd_srv, = work); + + mtk_dpmaif_cmd_handle(srv); +} + +static int mtk_dpmaif_cmd_srvs_init(struct mtk_dpmaif_ctlb *dcb) +{ + struct dpmaif_cmd_srv *cmd_srv =3D &dcb->cmd_srv; + struct dpmaif_vq *cmd_vq =3D &dcb->cmd_vq; + + cmd_vq->max_len =3D DEFAULT_TX_QUEUE_LEN; + skb_queue_head_init(&cmd_vq->list); + + cmd_srv->dcb =3D dcb; + cmd_srv->vq =3D cmd_vq; + + INIT_WORK(&cmd_srv->work, mtk_dpmaif_cmd_srv); + + return 0; +} + +static void mtk_dpmaif_cmd_srvs_exit(struct mtk_dpmaif_ctlb *dcb) +{ + flush_work(&dcb->cmd_srv.work); + skb_queue_purge(&dcb->cmd_vq.list); +} + +static int mtk_dpmaif_drv_res_init(struct mtk_dpmaif_ctlb *dcb) +{ + int ret =3D 0; + + dcb->drv_info =3D devm_kzalloc(DCB_TO_DEV(dcb), sizeof(*dcb->drv_info), G= FP_KERNEL); + if (!dcb->drv_info) + return -ENOMEM; + + dcb->drv_info->mdev =3D DCB_TO_MDEV(dcb); + if (DPMAIF_GET_HW_VER(dcb) =3D=3D 0x0800) { + dcb->drv_info->drv_ops =3D &dpmaif_drv_ops_t800; + } else { + devm_kfree(DCB_TO_DEV(dcb), dcb->drv_info); + dev_err(DCB_TO_DEV(dcb), "Unsupported mdev, hw_ver=3D0x%x\n", DPMAIF_GET= _HW_VER(dcb)); + ret =3D -EFAULT; + } + + return ret; +} + +static void mtk_dpmaif_drv_res_exit(struct mtk_dpmaif_ctlb *dcb) +{ + devm_kfree(DCB_TO_DEV(dcb), dcb->drv_info); + dcb->drv_info =3D NULL; +} + +static void mtk_dpmaif_irq_tx_done(struct mtk_dpmaif_ctlb *dcb, unsigned i= nt q_mask) +{ + unsigned int ulq_done; + int i; + + for (i =3D 0; i < dcb->res_cfg->txq_cnt; i++) { + ulq_done =3D q_mask & (1 << i); + if (ulq_done) { + queue_delayed_work(dcb->tx_done_wq, + &dcb->txqs[i].tx_done_work, + msecs_to_jiffies(0)); + } + } +} + +static void mtk_dpmaif_irq_rx_done(struct mtk_dpmaif_ctlb *dcb, unsigned i= nt q_mask) +{ + struct dpmaif_rxq *rxq; + unsigned int dlq_done; + int i; + + for (i =3D 0; i < dcb->res_cfg->rxq_cnt; i++) { + dlq_done =3D q_mask & (1 << i); + if (dlq_done) { + rxq =3D &dcb->rxqs[i]; + napi_schedule(&rxq->napi); + break; + } + } +} + +static int mtk_dpmaif_irq_handle(int irq_id, void *data) +{ + struct dpmaif_drv_intr_info intr_info; + struct dpmaif_irq_param *irq_param; + struct mtk_dpmaif_ctlb *dcb; + int ret; + int i; + + irq_param =3D data; + dcb =3D irq_param->dcb; + + if (unlikely(dcb->dpmaif_state !=3D DPMAIF_STATE_PWRON)) + goto out; + + memset(&intr_info, 0x00, sizeof(struct dpmaif_drv_intr_info)); + ret =3D mtk_dpmaif_drv_intr_handle(dcb->drv_info, &intr_info, irq_param->= dpmaif_irq_src); + if (ret < 0) { + dev_err(DCB_TO_DEV(dcb), "Failed to get dpmaif drv irq info\n"); + goto clear_drv_irq_info; + } + + for (i =3D 0; i < intr_info.intr_cnt; i++) { + switch (intr_info.intr_types[i]) { + case DPMAIF_INTR_UL_DONE: + mtk_dpmaif_irq_tx_done(dcb, intr_info.intr_queues[i]); + break; + case DPMAIF_INTR_DL_DONE: + mtk_dpmaif_irq_rx_done(dcb, intr_info.intr_queues[i]); + break; + default: + break; + } + } + +clear_drv_irq_info: + mtk_hw_clear_irq(DCB_TO_MDEV(dcb), irq_param->dev_irq_id); + mtk_hw_unmask_irq(DCB_TO_MDEV(dcb), irq_param->dev_irq_id); +out: + return IRQ_HANDLED; +} + +static int mtk_dpmaif_irq_init(struct mtk_dpmaif_ctlb *dcb) +{ + unsigned char irq_cnt =3D dcb->res_cfg->irq_cnt; + struct dpmaif_irq_param *irq_param; + enum mtk_irq_src irq_src; + int ret =3D 0; + int i, j; + + dcb->irq_params =3D devm_kcalloc(DCB_TO_DEV(dcb), irq_cnt, sizeof(*irq_pa= ram), GFP_KERNEL); + if (!dcb->irq_params) + return -ENOMEM; + + for (i =3D 0; i < irq_cnt; i++) { + irq_param =3D &dcb->irq_params[i]; + irq_param->idx =3D i; + irq_param->dcb =3D dcb; + irq_src =3D dcb->res_cfg->irq_src[i]; + irq_param->dpmaif_irq_src =3D irq_src; + irq_param->dev_irq_id =3D mtk_hw_get_irq_id(DCB_TO_MDEV(dcb), irq_src); + if (irq_param->dev_irq_id < 0) { + dev_err(DCB_TO_DEV(dcb), "Failed to allocate irq id, irq_src=3D%d\n", + irq_src); + ret =3D -EINVAL; + goto clean_reg_irq; + } + + ret =3D mtk_hw_register_irq(DCB_TO_MDEV(dcb), irq_param->dev_irq_id, + mtk_dpmaif_irq_handle, irq_param); + if (ret < 0) { + dev_err(DCB_TO_DEV(dcb), "Failed to register irq, irq_src=3D%d\n", irq_= src); + goto clean_reg_irq; + } + } + + dcb->irq_enabled =3D false; + + return 0; +clean_reg_irq: + for (j =3D i - 1; j >=3D 0; j--) { + irq_param =3D &dcb->irq_params[j]; + mtk_hw_unregister_irq(DCB_TO_MDEV(dcb), irq_param->dev_irq_id); + } + + devm_kfree(DCB_TO_DEV(dcb), dcb->irq_params); + dcb->irq_params =3D NULL; + + return ret; +} + +static int mtk_dpmaif_irq_exit(struct mtk_dpmaif_ctlb *dcb) +{ + unsigned char irq_cnt =3D dcb->res_cfg->irq_cnt; + struct dpmaif_irq_param *irq_param; + int i; + + for (i =3D 0; i < irq_cnt; i++) { + irq_param =3D &dcb->irq_params[i]; + mtk_hw_unregister_irq(DCB_TO_MDEV(dcb), irq_param->dev_irq_id); + } + + devm_kfree(DCB_TO_DEV(dcb), dcb->irq_params); + dcb->irq_params =3D NULL; + + return 0; +} + +static int mtk_dpmaif_hw_init(struct mtk_dpmaif_ctlb *dcb) +{ + struct dpmaif_bat_ring *bat_ring; + struct dpmaif_drv_cfg drv_cfg; + struct dpmaif_rxq *rxq; + struct dpmaif_txq *txq; + int ret; + int i; + + memset(&drv_cfg, 0x00, sizeof(struct dpmaif_drv_cfg)); + + bat_ring =3D &dcb->bat_info.normal_bat_ring; + drv_cfg.normal_bat_base =3D bat_ring->bat_dma_addr; + drv_cfg.normal_bat_cnt =3D bat_ring->bat_cnt; + drv_cfg.normal_bat_buf_size =3D bat_ring->buf_size; + + drv_cfg.max_mtu =3D dcb->bat_info.max_mtu; + + for (i =3D 0; i < dcb->res_cfg->rxq_cnt; i++) { + rxq =3D &dcb->rxqs[i]; + drv_cfg.pit_base[i] =3D rxq->pit_dma_addr; + drv_cfg.pit_cnt[i] =3D rxq->pit_cnt; + } + + for (i =3D 0; i < dcb->res_cfg->txq_cnt; i++) { + txq =3D &dcb->txqs[i]; + drv_cfg.drb_base[i] =3D txq->drb_dma_addr; + drv_cfg.drb_cnt[i] =3D txq->drb_cnt; + } + + ret =3D mtk_dpmaif_drv_init(dcb->drv_info, &drv_cfg); + if (ret < 0) + dev_err(DCB_TO_DEV(dcb), "Failed to initialize dpmaif hw\n"); + + return ret; +} + +static int mtk_dpmaif_start(struct mtk_dpmaif_ctlb *dcb) +{ + struct dpmaif_bat_ring *bat_ring; + unsigned int normal_buf_cnt; + int ret; + + if (dcb->dpmaif_state =3D=3D DPMAIF_STATE_PWRON) { + dev_err(DCB_TO_DEV(dcb), "Invalid parameters, dpmaif_state in PWRON\n"); + ret =3D -EINVAL; + goto out; + } + + bat_ring =3D &dcb->bat_info.normal_bat_ring; + normal_buf_cnt =3D bat_ring->bat_cnt - 1; + ret =3D mtk_dpmaif_reload_rx_buf(dcb, bat_ring, normal_buf_cnt, false); + if (ret < 0) { + dev_err(DCB_TO_DEV(dcb), "Failed to reload normal bat buffer\n"); + goto out; + } + + ret =3D mtk_dpmaif_hw_init(dcb); + if (ret < 0) { + dev_err(DCB_TO_DEV(dcb), "Failed to initialize dpmaif hw\n"); + goto out; + } + + ret =3D mtk_dpmaif_drv_send_doorbell(dcb->drv_info, DPMAIF_BAT, 0, normal= _buf_cnt); + if (unlikely(ret < 0)) { + dev_err(DCB_TO_DEV(dcb), + "Failed to send normal bat buffer count doorbell\n"); + mtk_dpmaif_common_err_handle(dcb, true); + goto out; + } + + ret =3D mtk_dpmaif_tx_srvs_start(dcb); + if (ret < 0) { + dev_err(DCB_TO_DEV(dcb), "Failed to start all tx srvs\n"); + goto out; + } + + dcb->dpmaif_state =3D DPMAIF_STATE_PWRON; + mtk_dpmaif_disable_irq(dcb); + + return 0; +out: + return ret; +} + +static void mtk_dpmaif_sw_reset(struct mtk_dpmaif_ctlb *dcb) +{ + mtk_dpmaif_tx_res_reset(dcb); + mtk_dpmaif_rx_res_reset(dcb); + mtk_dpmaif_bat_ring_reset(&dcb->bat_info.normal_bat_ring); + mtk_dpmaif_tx_vqs_reset(dcb); + skb_queue_purge(&dcb->cmd_vq.list); + dcb->dpmaif_user_ready =3D false; + dcb->trans_enabled =3D false; +} + +static int mtk_dpmaif_stop(struct mtk_dpmaif_ctlb *dcb) +{ + if (dcb->dpmaif_state =3D=3D DPMAIF_STATE_PWROFF) + goto out; + + dcb->dpmaif_state =3D DPMAIF_STATE_PWROFF; + + mtk_dpmaif_tx_srvs_stop(dcb); + mtk_dpmaif_trans_ctl(dcb, false); +out: + return 0; +} + +static void mtk_dpmaif_fsm_callback(struct mtk_fsm_param *fsm_param, void = *data) +{ + struct mtk_dpmaif_ctlb *dcb =3D data; + + switch (fsm_param->to) { + case FSM_STATE_OFF: + mtk_dpmaif_stop(dcb); + + flush_work(&dcb->cmd_srv.work); + + mtk_dpmaif_sw_reset(dcb); + break; + case FSM_STATE_BOOTUP: + if (fsm_param->fsm_flag =3D=3D FSM_F_MD_HS_START) + mtk_dpmaif_start(dcb); + break; + case FSM_STATE_READY: + break; + default: + break; + } +} + +static int mtk_dpmaif_fsm_init(struct mtk_dpmaif_ctlb *dcb) +{ + int ret; + + ret =3D mtk_fsm_notifier_register(DCB_TO_MDEV(dcb), MTK_USER_DPMAIF, + mtk_dpmaif_fsm_callback, dcb, FSM_PRIO_1, false); + if (ret < 0) + dev_err(DCB_TO_DEV(dcb), "Failed to register dpmaif fsm notifier\n"); + + return ret; +} + +static int mtk_dpmaif_fsm_exit(struct mtk_dpmaif_ctlb *dcb) +{ + int ret; + + ret =3D mtk_fsm_notifier_unregister(DCB_TO_MDEV(dcb), MTK_USER_DPMAIF); + if (ret < 0) + dev_err(DCB_TO_DEV(dcb), "Failed to unregister dpmaif fsm notifier\n"); + + return ret; +} + +static int mtk_dpmaif_sw_init(struct mtk_data_blk *data_blk, const struct = dpmaif_res_cfg *res_cfg) +{ + struct mtk_dpmaif_ctlb *dcb; + int ret; + + dcb =3D devm_kzalloc(data_blk->mdev->dev, sizeof(*dcb), GFP_KERNEL); + if (!dcb) + return -ENOMEM; + + data_blk->dcb =3D dcb; + dcb->data_blk =3D data_blk; + dcb->dpmaif_state =3D DPMAIF_STATE_PWROFF; + dcb->dpmaif_user_ready =3D false; + dcb->trans_enabled =3D false; + mutex_init(&dcb->trans_ctl_lock); + dcb->res_cfg =3D res_cfg; + + mtk_dpmaif_set_bat_buf_size(dcb, DPMAIF_DFLT_MTU); + + ret =3D mtk_dpmaif_sw_res_init(dcb); + if (ret < 0) { + dev_err(DCB_TO_DEV(dcb), "Failed to initialize dpmaif sw res, ret=3D%d\n= ", ret); + goto free_sw_res; + } + + ret =3D mtk_dpmaif_tx_srvs_init(dcb); + if (ret < 0) { + dev_err(DCB_TO_DEV(dcb), "Failed to initialize dpmaif tx res, ret=3D%d\n= ", ret); + goto free_tx_res; + } + + ret =3D mtk_dpmaif_cmd_srvs_init(dcb); + if (ret < 0) { + dev_err(DCB_TO_DEV(dcb), "Failed to initialize dpmaif tx cmd res, ret=3D= %d\n", ret); + goto free_ctl_res; + } + + ret =3D mtk_dpmaif_drv_res_init(dcb); + if (ret < 0) { + dev_err(DCB_TO_DEV(dcb), "Failed to initialize dpmaif drv res, ret=3D%d\= n", ret); + goto free_drv_res; + } + + ret =3D mtk_dpmaif_fsm_init(dcb); + if (ret < 0) { + dev_err(DCB_TO_DEV(dcb), "Failed to initialize dpmaif fsm, ret=3D%d\n", = ret); + goto free_fsm; + } + + ret =3D mtk_dpmaif_irq_init(dcb); + if (ret < 0) { + dev_err(DCB_TO_DEV(dcb), "Failed to initialize dpmaif int, ret=3D%d\n", = ret); + goto free_irq; + } + + return 0; + +free_irq: + mtk_dpmaif_fsm_exit(dcb); +free_fsm: + mtk_dpmaif_drv_res_exit(dcb); +free_drv_res: + mtk_dpmaif_cmd_srvs_exit(dcb); +free_ctl_res: + mtk_dpmaif_tx_srvs_exit(dcb); +free_tx_res: + mtk_dpmaif_sw_res_exit(dcb); +free_sw_res: + + devm_kfree(DCB_TO_DEV(dcb), dcb); + data_blk->dcb =3D NULL; + + return ret; +} + +static int mtk_dpmaif_sw_exit(struct mtk_data_blk *data_blk) +{ + struct mtk_dpmaif_ctlb *dcb =3D data_blk->dcb; + int ret =3D 0; + + mtk_dpmaif_irq_exit(dcb); + mtk_dpmaif_fsm_exit(dcb); + mtk_dpmaif_drv_res_exit(dcb); + mtk_dpmaif_cmd_srvs_exit(dcb); + mtk_dpmaif_tx_srvs_exit(dcb); + mtk_dpmaif_sw_res_exit(dcb); + devm_kfree(DCB_TO_DEV(dcb), dcb); + + return ret; +} + +static int mtk_dpmaif_poll_rx_pit(struct dpmaif_rxq *rxq) +{ + struct mtk_dpmaif_ctlb *dcb =3D rxq->dcb; + unsigned int sw_rd_idx, hw_wr_idx; + unsigned int pit_cnt; + int ret; + + sw_rd_idx =3D rxq->pit_rd_idx; + ret =3D mtk_dpmaif_drv_get_ring_idx(dcb->drv_info, DPMAIF_PIT_WIDX, rxq->= id); + if (unlikely(ret < 0)) { + dev_err(DCB_TO_DEV(dcb), + "Failed to read rxq%u hw pit_wr_idx, ret=3D%d\n", rxq->id, ret); + mtk_dpmaif_common_err_handle(dcb, true); + goto out; + } + + hw_wr_idx =3D ret; + pit_cnt =3D mtk_dpmaif_ring_buf_readable(rxq->pit_cnt, sw_rd_idx, hw_wr_i= dx); + rxq->pit_wr_idx =3D hw_wr_idx; + + return pit_cnt; + +out: + return ret; +} + +#define DPMAIF_POLL_STEP 20 +#define DPMAIF_POLL_PIT_CNT_MAX 100 +#define DPMAIF_PIT_SEQ_CHECK_FAIL_CNT 2500 + +static int mtk_dpmaif_check_pit_seq(struct dpmaif_rxq *rxq, struct dpmaif_= pd_pit *pit) +{ + unsigned int expect_pit_seq, cur_pit_seq; + unsigned int count =3D 0; + int ret; + + expect_pit_seq =3D rxq->pit_seq_expect; + do { + cur_pit_seq =3D FIELD_GET(PIT_PD_SEQ, le32_to_cpu(pit->pd_footer)); + if (cur_pit_seq > DPMAIF_PIT_SEQ_MAX) { + dev_err(DCB_TO_DEV(rxq->dcb), + "Invalid rxq%u pit sequence number, cur_seq(%u) > max_seq(%u)\n", + rxq->id, cur_pit_seq, DPMAIF_PIT_SEQ_MAX); + break; + } + + if (cur_pit_seq =3D=3D expect_pit_seq) { + rxq->pit_seq_expect++; + if (rxq->pit_seq_expect >=3D DPMAIF_PIT_SEQ_MAX) + rxq->pit_seq_expect =3D 0; + + rxq->pit_seq_fail_cnt =3D 0; + ret =3D 0; + + goto out; + } else { + count++; + } + + udelay(DPMAIF_POLL_STEP); + } while (count <=3D DPMAIF_POLL_PIT_CNT_MAX); + + ret =3D -DATA_PIT_SEQ_CHK_FAIL; + rxq->pit_seq_fail_cnt++; + if (rxq->pit_seq_fail_cnt >=3D DPMAIF_PIT_SEQ_CHECK_FAIL_CNT) { + mtk_dpmaif_common_err_handle(rxq->dcb, true); + rxq->pit_seq_fail_cnt =3D 0; + } + +out: + return ret; +} + +static void mtk_dpmaif_rx_msg_pit(struct dpmaif_rxq *rxq, struct dpmaif_ms= g_pit *msg_pit, + struct dpmaif_rx_record *rx_record) +{ + rx_record->cur_ch_id =3D FIELD_GET(PIT_MSG_CHNL_ID, le32_to_cpu(msg_pit->= dword1)); + rx_record->checksum =3D FIELD_GET(PIT_MSG_CHECKSUM, le32_to_cpu(msg_pit->= dword1)); + rx_record->pit_dp =3D FIELD_GET(PIT_MSG_DP, le32_to_cpu(msg_pit->dword1)); +} + +static int mtk_dpmaif_pit_bid_check(struct dpmaif_rxq *rxq, unsigned int c= ur_bid) +{ + union dpmaif_bat_record *cur_bat_record; + struct mtk_dpmaif_ctlb *dcb =3D rxq->dcb; + struct dpmaif_bat_ring *bat_ring; + int ret =3D 0; + + bat_ring =3D &rxq->dcb->bat_info.normal_bat_ring; + cur_bat_record =3D bat_ring->sw_record_base + cur_bid; + + if (unlikely(!cur_bat_record->normal.skb || cur_bid >=3D bat_ring->bat_cn= t)) { + dev_err(DCB_TO_DEV(dcb), "Invalid parameter rxq%u bat%d, bid=3D%u, bat_c= nt=3D%u\n", + rxq->id, bat_ring->type, cur_bid, bat_ring->bat_cnt); + ret =3D -DATA_FLOW_CHK_ERR; + } + + return ret; +} + +static int mtk_dpmaif_rx_set_data_to_skb(struct dpmaif_rxq *rxq, struct dp= maif_pd_pit *pit_info, + struct dpmaif_rx_record *rx_record) +{ + struct dpmaif_bat_ring *bat_ring =3D &rxq->dcb->bat_info.normal_bat_ring; + unsigned long long data_dma_addr, data_dma_base_addr; + struct mtk_dpmaif_ctlb *dcb =3D rxq->dcb; + union dpmaif_bat_record *bat_record; + struct sk_buff *new_skb; + unsigned int data_len; + int data_offset; + + bat_record =3D bat_ring->sw_record_base + mtk_dpmaif_pit_bid(pit_info); + new_skb =3D bat_record->normal.skb; + data_dma_base_addr =3D (unsigned long long)bat_record->normal.data_dma_ad= dr; + + dma_unmap_single(dcb->data_blk->mdev->dev, bat_record->normal.data_dma_ad= dr, + bat_record->normal.data_len, DMA_FROM_DEVICE); + + data_dma_addr =3D le32_to_cpu(pit_info->addr_high); + data_dma_addr =3D (data_dma_addr << 32) + le32_to_cpu(pit_info->addr_low); + data_offset =3D (int)(data_dma_addr - data_dma_base_addr); + data_len =3D FIELD_GET(PIT_PD_DATA_LEN, le32_to_cpu(pit_info->pd_header)); + + if (FIELD_GET(PIT_PD_HD_OFFSET, le32_to_cpu(pit_info->pd_footer)) !=3D 0) + data_len +=3D (FIELD_GET(PIT_PD_HD_OFFSET, le32_to_cpu(pit_info->pd_foot= er)) * 4); + + new_skb->len =3D 0; + skb_reset_tail_pointer(new_skb); + skb_reserve(new_skb, data_offset); + if (unlikely((new_skb->tail + data_len) > new_skb->end)) { + dev_err(DCB_TO_DEV(dcb), "pkt(%u/%u):len=3D%u, offset=3D0x%llx-0x%llx\n", + rxq->pit_rd_idx, mtk_dpmaif_pit_bid(pit_info), data_len, + data_dma_addr, data_dma_base_addr); + + return -DATA_FLOW_CHK_ERR; + } + + skb_put(new_skb, data_len); + + if (FIELD_GET(PIT_PD_HD_OFFSET, le32_to_cpu(pit_info->pd_footer)) !=3D 0) + skb_pull(new_skb, + FIELD_GET(PIT_PD_HD_OFFSET, le32_to_cpu(pit_info->pd_footer)) * 4); + + rx_record->cur_skb =3D new_skb; + bat_record->normal.skb =3D NULL; + + return 0; +} + +static int mtk_dpmaif_bat_ring_set_mask(struct mtk_dpmaif_ctlb *dcb, enum = dpmaif_bat_type type, + unsigned int bat_idx) +{ + struct dpmaif_bat_ring *bat_ring; + int ret =3D 0; + + bat_ring =3D &dcb->bat_info.normal_bat_ring; + + if (likely(bat_ring->mask_tbl[bat_idx] =3D=3D 0)) { + bat_ring->mask_tbl[bat_idx] =3D 1; + } else { + dev_err(DCB_TO_DEV(dcb), "Invalid bat%u mask_table[%u] value\n", type, b= at_idx); + ret =3D -DATA_FLOW_CHK_ERR; + } + + return ret; +} + +static int mtk_dpmaif_get_rx_pkt(struct dpmaif_rxq *rxq, struct dpmaif_pd_= pit *pit_info, + struct dpmaif_rx_record *rx_record) +{ + struct mtk_dpmaif_ctlb *dcb =3D rxq->dcb; + unsigned int cur_bid; + int ret; + + cur_bid =3D mtk_dpmaif_pit_bid(pit_info); + + ret =3D mtk_dpmaif_pit_bid_check(rxq, cur_bid); + if (unlikely(ret < 0)) { + dev_err(DCB_TO_DEV(dcb), "Failed to check rxq%u pit normal bid\n", rxq->= id); + goto out; + } + + ret =3D mtk_dpmaif_rx_set_data_to_skb(rxq, pit_info, rx_record); + if (unlikely(ret < 0)) { + dev_err(DCB_TO_DEV(dcb), "Failed to set rxq%u data to skb\n", rxq->id); + goto out; + } + + ret =3D mtk_dpmaif_bat_ring_set_mask(dcb, NORMAL_BAT, cur_bid); + if (unlikely(ret < 0)) { + dev_err(DCB_TO_DEV(dcb), "Failed to rxq%u set bat mask\n", rxq->id); + goto out; + } + + return 0; + +out: + return ret; +} + +static void mtk_dpmaif_set_rcsum(struct sk_buff *skb, unsigned int hw_chec= ksum_state) +{ + if (hw_checksum_state =3D=3D CS_RESULT_PASS) + skb->ip_summed =3D CHECKSUM_UNNECESSARY; + else + skb->ip_summed =3D CHECKSUM_NONE; +} + +static int mtk_dpmaif_rx_skb(struct dpmaif_rxq *rxq, struct dpmaif_rx_reco= rd *rx_record) +{ + struct sk_buff *new_skb =3D rx_record->cur_skb; + int ret =3D 0; + + if (unlikely(rx_record->pit_dp)) { + dev_kfree_skb_any(new_skb); + goto out; + } + + mtk_dpmaif_set_rcsum(new_skb, rx_record->checksum); + skb_record_rx_queue(new_skb, rxq->id); + +out: + return ret; +} + +static int mtk_dpmaif_recycle_pit_internal(struct dpmaif_rxq *rxq, unsigne= d short pit_rel_cnt) +{ + unsigned short old_sw_rel_idx, new_sw_rel_idx, old_hw_wr_idx; + struct mtk_dpmaif_ctlb *dcb =3D rxq->dcb; + int ret =3D 0; + + old_sw_rel_idx =3D rxq->pit_rel_rd_idx; + new_sw_rel_idx =3D old_sw_rel_idx + pit_rel_cnt; + old_hw_wr_idx =3D rxq->pit_wr_idx; + + if (old_hw_wr_idx =3D=3D old_sw_rel_idx) + dev_err(DCB_TO_DEV(dcb), "old_hw_wr_idx =3D=3D old_sw_rel_idx\n"); + + if (old_hw_wr_idx > old_sw_rel_idx) { + if (new_sw_rel_idx > old_hw_wr_idx) + dev_err(DCB_TO_DEV(dcb), "new_rel_idx > old_hw_wr_idx\n"); + } else if (old_hw_wr_idx < old_sw_rel_idx) { + if (new_sw_rel_idx >=3D rxq->pit_cnt) { + new_sw_rel_idx =3D new_sw_rel_idx - rxq->pit_cnt; + if (new_sw_rel_idx > old_hw_wr_idx) + dev_err(DCB_TO_DEV(dcb), "new_rel_idx > old_wr_idx\n"); + } + } + + ret =3D mtk_dpmaif_drv_send_doorbell(dcb->drv_info, DPMAIF_PIT, rxq->id, = pit_rel_cnt); + if (unlikely(ret < 0)) { + dev_err(DCB_TO_DEV(dcb), + "Failed to send pit doorbell,pit-r/w/rel-%u,%u,%u, rel_pit_cnt=3D%u, re= t=3D%d\n", + rxq->pit_rd_idx, rxq->pit_wr_idx, rxq->pit_rel_rd_idx, pit_rel_cnt, ret= ); + mtk_dpmaif_common_err_handle(dcb, true); + } + + rxq->pit_rel_rd_idx =3D new_sw_rel_idx; + + return ret; +} + +static int mtk_dpmaif_recycle_rx_ring(struct dpmaif_rxq *rxq) +{ + int ret =3D 0; + + if (rxq->pit_rel_cnt < rxq->pit_burst_rel_cnt) + return 0; + + if (unlikely(rxq->pit_rel_cnt > rxq->pit_cnt)) { + dev_err(DCB_TO_DEV(rxq->dcb), "Invalid rxq%u pit release count, %u>%u\n", + rxq->id, rxq->pit_rel_cnt, rxq->pit_cnt); + ret =3D -DATA_FLOW_CHK_ERR; + goto out; + } + + ret =3D mtk_dpmaif_recycle_pit_internal(rxq, rxq->pit_rel_cnt); + if (unlikely(ret < 0)) { + dev_err(DCB_TO_DEV(rxq->dcb), "Failed to rxq%u recycle pit, ret=3D%d\n", + rxq->id, ret); + } + + rxq->pit_rel_cnt =3D 0; + queue_work(rxq->dcb->bat_info.reload_wq, &rxq->dcb->bat_info.normal_bat_r= ing.reload_work); + +out: + return ret; +} + +static int mtk_dpmaif_rx_data_collect_internal(struct dpmaif_rxq *rxq, int= pit_cnt, int budget, + unsigned int *pkt_cnt) +{ + struct dpmaif_rx_record *rx_record =3D &rxq->rx_record; + struct mtk_dpmaif_ctlb *dcb =3D rxq->dcb; + struct dpmaif_pd_pit *pit_info; + unsigned int recv_pkt_cnt =3D 0; + unsigned int rx_cnt, cur_pit; + int ret; + + cur_pit =3D rxq->pit_rd_idx; + for (rx_cnt =3D 0; rx_cnt < pit_cnt; rx_cnt++) { + if (!rx_record->msg_pit_recv) { + if (recv_pkt_cnt >=3D budget) + break; + } + + pit_info =3D rxq->pit_base + cur_pit; + ret =3D mtk_dpmaif_check_pit_seq(rxq, pit_info); + if (unlikely(ret < 0)) + break; + + if (FIELD_GET(PIT_PD_PKT_TYPE, le32_to_cpu(pit_info->pd_header)) =3D=3D = MSG_PIT) { + if (unlikely(rx_record->msg_pit_recv)) + memset(&rxq->rx_record, 0x00, sizeof(rxq->rx_record)); + + rx_record->msg_pit_recv =3D true; + mtk_dpmaif_rx_msg_pit(rxq, (struct dpmaif_msg_pit *)pit_info, rx_record= ); + } else { + ret =3D mtk_dpmaif_get_rx_pkt(rxq, pit_info, rx_record); + if (unlikely(ret < 0)) { + rx_record->err_payload =3D 1; + mtk_dpmaif_common_err_handle(dcb, true); + } + + if (FIELD_GET(PIT_PD_CONT, le32_to_cpu(pit_info->pd_header)) =3D=3D + DPMAIF_PIT_LASTONE) { + if (likely(rx_record->err_payload =3D=3D 0)) + mtk_dpmaif_rx_skb(rxq, rx_record); + memset(&rxq->rx_record, 0x00, sizeof(rxq->rx_record)); + recv_pkt_cnt++; + } + } + + cur_pit =3D mtk_dpmaif_ring_buf_get_next_idx(rxq->pit_cnt, cur_pit); + rxq->pit_rd_idx =3D cur_pit; + rxq->pit_rel_cnt++; + } + + *pkt_cnt =3D recv_pkt_cnt; + + ret =3D mtk_dpmaif_recycle_rx_ring(rxq); + if (unlikely(ret < 0)) + dev_err(DCB_TO_DEV(dcb), "Failed to recycle rxq%u pit\n", rxq->id); + + return ret; +} + +static int mtk_dpmaif_rx_data_collect(struct dpmaif_rxq *rxq, int budget, = unsigned int *pkt_cnt) +{ + struct mtk_dpmaif_ctlb *dcb =3D rxq->dcb; + unsigned int pit_cnt; + int ret; + + ret =3D mtk_dpmaif_poll_rx_pit(rxq); + if (unlikely(ret < 0)) + goto out; + + pit_cnt =3D ret; + if (likely(pit_cnt > 0)) { + ret =3D mtk_dpmaif_rx_data_collect_internal(rxq, pit_cnt, budget, pkt_cn= t); + if (ret <=3D -DATA_DL_ONCE_MORE) { + ret =3D -DATA_DL_ONCE_MORE; + } else if (ret <=3D -DATA_ERR_STOP_MAX) { + ret =3D -DATA_ERR_STOP_MAX; + mtk_dpmaif_common_err_handle(dcb, true); + } else { + ret =3D 0; + } + } + +out: + return ret; +} + +static int mtk_dpmaif_rx_data_collect_more(struct dpmaif_rxq *rxq, int bud= get, int *work_done) +{ + unsigned int total_pkt_cnt =3D 0, pkt_cnt; + int each_budget; + int ret =3D 0; + + do { + each_budget =3D budget - total_pkt_cnt; + pkt_cnt =3D 0; + ret =3D mtk_dpmaif_rx_data_collect(rxq, each_budget, &pkt_cnt); + total_pkt_cnt +=3D pkt_cnt; + if (ret < 0) + break; + } while (total_pkt_cnt < budget && pkt_cnt > 0 && rxq->started); + + *work_done =3D total_pkt_cnt; + + return ret; +} + +static int mtk_dpmaif_rx_napi_poll(struct napi_struct *napi, int budget) +{ + struct dpmaif_rxq *rxq =3D container_of(napi, struct dpmaif_rxq, napi); + struct mtk_dpmaif_ctlb *dcb =3D rxq->dcb; + int work_done =3D 0; + int ret; + + if (likely(rxq->started)) { + ret =3D mtk_dpmaif_rx_data_collect_more(rxq, budget, &work_done); + if (ret =3D=3D -DATA_DL_ONCE_MORE) { + napi_gro_flush(napi, false); + work_done =3D budget; + } + } + + if (work_done < budget) { + napi_complete_done(napi, work_done); + mtk_dpmaif_drv_clear_ip_busy(dcb->drv_info); + mtk_dpmaif_drv_intr_complete(dcb->drv_info, DPMAIF_INTR_DL_DONE, rxq->id= , 0); + } + + return work_done; +} + +static int mtk_dpmaif_send_pkt(struct mtk_dpmaif_ctlb *dcb, struct sk_buff= *skb, + unsigned char intf_id) +{ + unsigned char vq_id =3D skb_get_queue_mapping(skb); + struct dpmaif_pkt_info *pkt_info; + unsigned char srv_id; + struct dpmaif_vq *vq; + int ret =3D 0; + + pkt_info =3D DPMAIF_SKB_CB(skb); + pkt_info->intf_id =3D intf_id; + pkt_info->drb_cnt =3D DPMAIF_GET_DRB_CNT(skb); + + vq =3D &dcb->tx_vqs[vq_id]; + srv_id =3D dcb->res_cfg->tx_vq_srv_map[vq_id]; + if (likely(skb_queue_len(&vq->list) < vq->max_len)) + skb_queue_tail(&vq->list, skb); + else + ret =3D -EBUSY; + + wake_up(&dcb->tx_srvs[srv_id].wait); + + return ret; +} + +static int mtk_dpmaif_send_cmd(struct mtk_dpmaif_ctlb *dcb, struct sk_buff= *skb) +{ + struct dpmaif_vq *vq =3D &dcb->cmd_vq; + int ret =3D 0; + + if (likely(skb_queue_len(&vq->list) < vq->max_len)) + skb_queue_tail(&vq->list, skb); + else + ret =3D -EBUSY; + + schedule_work(&dcb->cmd_srv.work); + + return ret; +} + +static int mtk_dpmaif_send(struct mtk_data_blk *data_blk, enum mtk_data_ty= pe type, + struct sk_buff *skb, u64 data) +{ + int ret; + + if (likely(type =3D=3D DATA_PKT)) + ret =3D mtk_dpmaif_send_pkt(data_blk->dcb, skb, data); + else + ret =3D mtk_dpmaif_send_cmd(data_blk->dcb, skb); + + return ret; +} + +struct mtk_data_trans_ops data_trans_ops =3D { + .poll =3D mtk_dpmaif_rx_napi_poll, + .send =3D mtk_dpmaif_send, +}; + +int mtk_data_init(struct mtk_md_dev *mdev) +{ + const struct dpmaif_res_cfg *res_cfg; + struct mtk_data_blk *data_blk; + int ret; + + data_blk =3D devm_kzalloc(mdev->dev, sizeof(*data_blk), GFP_KERNEL); + if (!data_blk) + return -ENOMEM; + + data_blk->mdev =3D mdev; + mdev->data_blk =3D data_blk; + + if (mdev->hw_ver =3D=3D 0x0800) { + res_cfg =3D &res_cfg_t800; + } else { + dev_err(mdev->dev, "Unsupported mdev, hw_ver=3D0x%x\n", mdev->hw_ver); + ret =3D -ENODEV; + goto out; + } + + ret =3D mtk_dpmaif_sw_init(data_blk, res_cfg); + if (ret < 0) { + dev_err(mdev->dev, "Failed to initialize data trans, ret=3D%d\n", ret); + goto out; + } + + return 0; + +out: + devm_kfree(mdev->dev, data_blk); + mdev->data_blk =3D NULL; + + return ret; +} + +int mtk_data_exit(struct mtk_md_dev *mdev) +{ + int ret; + + ret =3D mtk_dpmaif_sw_exit(mdev->data_blk); + if (ret < 0) + dev_err(mdev->dev, "Failed to exit data trans, ret=3D%d\n", ret); + + devm_kfree(mdev->dev, mdev->data_blk); + mdev->data_blk =3D NULL; + + return ret; +} diff --git a/drivers/net/wwan/mediatek/mtk_dpmaif_drv.h b/drivers/net/wwan/= mediatek/mtk_dpmaif_drv.h index 1e89fe2ba6e3..9195bdc068ae 100644 --- a/drivers/net/wwan/mediatek/mtk_dpmaif_drv.h +++ b/drivers/net/wwan/mediatek/mtk_dpmaif_drv.h @@ -14,7 +14,6 @@ enum dpmaif_drv_dir { struct dpmaif_drv_intr { enum dpmaif_drv_dir dir; unsigned int q_mask; - unsigned int mode; }; =20 enum mtk_drv_err { diff --git a/drivers/net/wwan/mediatek/pcie/mtk_pci.c b/drivers/net/wwan/me= diatek/pcie/mtk_pci.c index a488f0fa3c2e..780df366ab0b 100644 --- a/drivers/net/wwan/mediatek/pcie/mtk_pci.c +++ b/drivers/net/wwan/mediatek/pcie/mtk_pci.c @@ -501,6 +501,11 @@ static bool mtk_pci_link_check(struct mtk_md_dev *mdev) return !pci_device_is_present(to_pci_dev(mdev->dev)); } =20 +static bool mtk_pci_mmio_check(struct mtk_md_dev *mdev) +{ + return mtk_pci_mac_read32(mdev->hw_priv, REG_ATR_PCIE_WIN0_T0_SRC_ADDR_LS= B) =3D=3D (u32)-1; +} + static int mtk_pci_get_hp_status(struct mtk_md_dev *mdev) { struct mtk_pci_priv *priv =3D mdev->hw_priv; @@ -527,6 +532,7 @@ static const struct mtk_hw_ops mtk_pci_ops =3D { .clear_ext_evt =3D mtk_mhccif_clear_evt, .send_ext_evt =3D mtk_mhccif_send_evt, .get_ext_evt_status =3D mtk_mhccif_get_evt_status, + .mmio_check =3D mtk_pci_mmio_check, .get_hp_status =3D mtk_pci_get_hp_status, }; =20 --=20 2.32.0 From nobody Thu Nov 14 06:37:28 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 88EB0C7618A for ; Fri, 17 Mar 2023 08:46:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231757AbjCQIqe (ORCPT ); Fri, 17 Mar 2023 04:46:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230004AbjCQIqN (ORCPT ); Fri, 17 Mar 2023 04:46:13 -0400 Received: from mailgw01.mediatek.com (unknown [60.244.123.138]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 412C620A18; Fri, 17 Mar 2023 01:45:08 -0700 (PDT) X-UUID: c6ff11ecc49b11ed91027fb02e0f1d65-20230317 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=e5lmoZClcTEuA7WXoUuFqbFX5Tn8C6idhdBcfso/xmI=; b=pH8I1gydyz2ZZGdSXPeJzqwv1XgYSCmCcw9Snzq/0/ewLZRSTwe0riJ+EpWno9x6zgGcMEhcj5ltvAYG8Wr4JEYnYyYFOzC67wCqF/Yb497wdKD/iRcRB9IpTqzbInIybhb446sUgkqUc3l27Ym8UWyKtYHTIyGP3PB8Q9Tgdg0=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.21,REQID:c17511cb-6d17-4a27-a510-2ecf3d045ae1,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-25 X-CID-META: VersionHash:83295aa,CLOUDID:04348db3-beed-4dfc-bd9c-e1b22fa6ccc4,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:11|1,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-UUID: c6ff11ecc49b11ed91027fb02e0f1d65-20230317 Received: from mtkmbs13n2.mediatek.inc [(172.21.101.108)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1103174578; Fri, 17 Mar 2023 16:14:45 +0800 Received: from mtkmbs11n2.mediatek.inc (172.21.101.187) by mtkmbs10n2.mediatek.inc (172.21.101.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Fri, 17 Mar 2023 16:14:44 +0800 Received: from mcddlt001.gcn.mediatek.inc (10.19.240.15) by mtkmbs11n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.25 via Frontend Transport; Fri, 17 Mar 2023 16:14:42 +0800 From: Yanchao Yang To: Loic Poulain , Sergey Ryazanov , Johannes Berg , "David S . Miller" , Eric Dumazet , "Jakub Kicinski" , Paolo Abeni , netdev ML , kernel ML CC: Intel experts , Chetan , MTK ML , Liang Lu , Haijun Liu , Hua Yang , Ting Wang , Felix Chen , Mingliang Xu , Min Dong , Aiden Wang , Guohao Zhang , Chris Feng , "Yanchao Yang" , Lambert Wang , Mingchuang Qiao , Xiayu Zhang , Haozhe Chang Subject: [PATCH net-next v4 09/10] net: wwan: tmi: Introduce WWAN interface Date: Fri, 17 Mar 2023 16:09:41 +0800 Message-ID: <20230317080942.183514-10-yanchao.yang@mediatek.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20230317080942.183514-1-yanchao.yang@mediatek.com> References: <20230317080942.183514-1-yanchao.yang@mediatek.com> MIME-Version: 1.0 X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Creates the WWAN interface which implements the wwan_ops for registration w= ith the WWAN framework. WWAN interface also implements the net_device_ops funct= ions used by the network devices. Network device operations include open, stop, start transmission and get states. Signed-off-by: Yanchao Yang Signed-off-by: Hua Yang --- drivers/net/wwan/mediatek/Makefile | 3 +- drivers/net/wwan/mediatek/mtk_data_plane.h | 11 + drivers/net/wwan/mediatek/mtk_dpmaif.c | 68 ++- drivers/net/wwan/mediatek/mtk_wwan.c | 511 +++++++++++++++++++++ 4 files changed, 587 insertions(+), 6 deletions(-) create mode 100644 drivers/net/wwan/mediatek/mtk_wwan.c diff --git a/drivers/net/wwan/mediatek/Makefile b/drivers/net/wwan/mediatek= /Makefile index 27ad6628b83d..86ed172532ac 100644 --- a/drivers/net/wwan/mediatek/Makefile +++ b/drivers/net/wwan/mediatek/Makefile @@ -12,6 +12,7 @@ mtk_tmi-y =3D \ mtk_port.o \ mtk_port_io.o \ mtk_fsm.o \ - mtk_dpmaif.o + mtk_dpmaif.o \ + mtk_wwan.o =20 obj-$(CONFIG_MTK_TMI) +=3D mtk_tmi.o diff --git a/drivers/net/wwan/mediatek/mtk_data_plane.h b/drivers/net/wwan/= mediatek/mtk_data_plane.h index 9dbef5911c49..672f582aca61 100644 --- a/drivers/net/wwan/mediatek/mtk_data_plane.h +++ b/drivers/net/wwan/mediatek/mtk_data_plane.h @@ -23,6 +23,7 @@ enum mtk_data_feature { =20 struct mtk_data_blk { struct mtk_md_dev *mdev; + struct mtk_wwan_ctlb *wcb; struct mtk_dpmaif_ctlb *dcb; }; =20 @@ -82,9 +83,19 @@ struct mtk_data_trans_info { struct napi_struct **napis; }; =20 +struct mtk_data_port_ops { + int (*init)(struct mtk_data_blk *data_blk, struct mtk_data_trans_info *tr= ans_info); + void (*exit)(struct mtk_data_blk *data_blk); + int (*recv)(struct mtk_data_blk *data_blk, struct sk_buff *skb, + unsigned char q_id, unsigned char if_id); + void (*notify)(struct mtk_data_blk *data_blk, enum mtk_data_evt evt, u64 = data); +}; + +int mtk_wwan_cmd_execute(struct net_device *dev, enum mtk_data_cmd_type cm= d, void *data); int mtk_data_init(struct mtk_md_dev *mdev); int mtk_data_exit(struct mtk_md_dev *mdev); =20 +extern struct mtk_data_port_ops data_port_ops; extern struct mtk_data_trans_ops data_trans_ops; =20 #endif /* __MTK_DATA_PLANE_H__ */ diff --git a/drivers/net/wwan/mediatek/mtk_dpmaif.c b/drivers/net/wwan/medi= atek/mtk_dpmaif.c index 10d1a0255ccc..bf09486630de 100644 --- a/drivers/net/wwan/mediatek/mtk_dpmaif.c +++ b/drivers/net/wwan/mediatek/mtk_dpmaif.c @@ -319,6 +319,7 @@ enum dpmaif_dump_flag { =20 struct mtk_dpmaif_ctlb { struct mtk_data_blk *data_blk; + struct mtk_data_port_ops *port_ops; struct dpmaif_drv_info *drv_info; struct napi_struct *napi[DPMAIF_RXQ_CNT_MAX]; =20 @@ -761,9 +762,13 @@ static void mtk_dpmaif_bat_exit(struct mtk_dpmaif_ctlb= *dcb, struct dpmaif_bat_r if (bat_ring->sw_record_base) { for (i =3D 0; i < bat_ring->bat_cnt; i++) { bat_record =3D bat_ring->sw_record_base + i; - dma_unmap_single(DCB_TO_DEV(dcb), bat_record->normal.data_dma_addr, - bat_record->normal.data_len, DMA_FROM_DEVICE); - dev_kfree_skb_any(bat_record->normal.skb); + if (bat_record->normal.skb) { + dma_unmap_single(DCB_TO_DEV(dcb), + bat_record->normal.data_dma_addr, + bat_record->normal.data_len, + DMA_FROM_DEVICE); + dev_kfree_skb_any(bat_record->normal.skb); + } } =20 devm_kfree(DCB_TO_DEV(dcb), bat_ring->sw_record_base); @@ -1058,6 +1063,8 @@ static int mtk_dpmaif_tx_rel_internal(struct dpmaif_t= xq *txq, txq->drb_rel_rd_idx =3D mtk_dpmaif_ring_buf_get_next_idx(txq->drb_cnt, c= ur_idx); =20 atomic_inc(&txq->budget); + if (atomic_read(&txq->budget) > txq->drb_cnt / 8) + dcb->port_ops->notify(dcb->data_blk, DATA_EVT_TX_START, (u64)1 << txq->= id); } =20 *real_rel_cnt =3D i; @@ -2033,6 +2040,38 @@ static int mtk_dpmaif_irq_exit(struct mtk_dpmaif_ctl= b *dcb) return 0; } =20 +static int mtk_dpmaif_port_init(struct mtk_dpmaif_ctlb *dcb) +{ + struct mtk_data_trans_info trans_info; + struct dpmaif_rxq *rxq; + int ret; + int i; + + memset(&trans_info, 0x00, sizeof(struct mtk_data_trans_info)); + trans_info.max_mtu =3D dcb->bat_info.max_mtu; + trans_info.txq_cnt =3D dcb->res_cfg->txq_cnt; + trans_info.rxq_cnt =3D dcb->res_cfg->rxq_cnt; + + for (i =3D 0; i < trans_info.rxq_cnt; i++) { + rxq =3D &dcb->rxqs[i]; + dcb->napi[i] =3D &rxq->napi; + } + trans_info.napis =3D dcb->napi; + + dcb->port_ops =3D &data_port_ops; + ret =3D dcb->port_ops->init(dcb->data_blk, &trans_info); + if (ret < 0) + dev_err(DCB_TO_DEV(dcb), + "Failed to initialize data port layer, ret=3D%d\n", ret); + + return ret; +} + +static void mtk_dpmaif_port_exit(struct mtk_dpmaif_ctlb *dcb) +{ + dcb->port_ops->exit(dcb->data_blk); +} + static int mtk_dpmaif_hw_init(struct mtk_dpmaif_ctlb *dcb) { struct dpmaif_bat_ring *bat_ring; @@ -2135,9 +2174,13 @@ static int mtk_dpmaif_stop(struct mtk_dpmaif_ctlb *d= cb) goto out; =20 dcb->dpmaif_state =3D DPMAIF_STATE_PWROFF; + dcb->port_ops->notify(dcb->data_blk, DATA_EVT_TX_STOP, 0xff); =20 mtk_dpmaif_tx_srvs_stop(dcb); mtk_dpmaif_trans_ctl(dcb, false); + + dcb->port_ops->notify(dcb->data_blk, DATA_EVT_RX_STOP, 0xff); + out: return 0; } @@ -2150,6 +2193,7 @@ static void mtk_dpmaif_fsm_callback(struct mtk_fsm_pa= ram *fsm_param, void *data) case FSM_STATE_OFF: mtk_dpmaif_stop(dcb); =20 + dcb->port_ops->notify(dcb->data_blk, DATA_EVT_UNREG_DEV, 0); flush_work(&dcb->cmd_srv.work); =20 mtk_dpmaif_sw_reset(dcb); @@ -2159,6 +2203,7 @@ static void mtk_dpmaif_fsm_callback(struct mtk_fsm_pa= ram *fsm_param, void *data) mtk_dpmaif_start(dcb); break; case FSM_STATE_READY: + dcb->port_ops->notify(dcb->data_blk, DATA_EVT_REG_DEV, 0); break; default: break; @@ -2231,6 +2276,12 @@ static int mtk_dpmaif_sw_init(struct mtk_data_blk *d= ata_blk, const struct dpmaif goto free_drv_res; } =20 + ret =3D mtk_dpmaif_port_init(dcb); + if (ret < 0) { + dev_err(DCB_TO_DEV(dcb), "Failed to initialize data port, ret=3D%d\n", r= et); + goto free_port; + } + ret =3D mtk_dpmaif_fsm_init(dcb); if (ret < 0) { dev_err(DCB_TO_DEV(dcb), "Failed to initialize dpmaif fsm, ret=3D%d\n", = ret); @@ -2248,6 +2299,8 @@ static int mtk_dpmaif_sw_init(struct mtk_data_blk *da= ta_blk, const struct dpmaif free_irq: mtk_dpmaif_fsm_exit(dcb); free_fsm: + mtk_dpmaif_port_exit(dcb); +free_port: mtk_dpmaif_drv_res_exit(dcb); free_drv_res: mtk_dpmaif_cmd_srvs_exit(dcb); @@ -2270,6 +2323,7 @@ static int mtk_dpmaif_sw_exit(struct mtk_data_blk *da= ta_blk) =20 mtk_dpmaif_irq_exit(dcb); mtk_dpmaif_fsm_exit(dcb); + mtk_dpmaif_port_exit(dcb); mtk_dpmaif_drv_res_exit(dcb); mtk_dpmaif_cmd_srvs_exit(dcb); mtk_dpmaif_tx_srvs_exit(dcb); @@ -2490,6 +2544,7 @@ static void mtk_dpmaif_set_rcsum(struct sk_buff *skb,= unsigned int hw_checksum_s static int mtk_dpmaif_rx_skb(struct dpmaif_rxq *rxq, struct dpmaif_rx_reco= rd *rx_record) { struct sk_buff *new_skb =3D rx_record->cur_skb; + struct mtk_dpmaif_ctlb *dcb =3D rxq->dcb; int ret =3D 0; =20 if (unlikely(rx_record->pit_dp)) { @@ -2500,6 +2555,7 @@ static int mtk_dpmaif_rx_skb(struct dpmaif_rxq *rxq, = struct dpmaif_rx_record *rx mtk_dpmaif_set_rcsum(new_skb, rx_record->checksum); skb_record_rx_queue(new_skb, rxq->id); =20 + ret =3D dcb->port_ops->recv(dcb->data_blk, new_skb, rxq->id, rx_record->c= ur_ch_id); out: return ret; } @@ -2712,10 +2768,12 @@ static int mtk_dpmaif_send_pkt(struct mtk_dpmaif_ct= lb *dcb, struct sk_buff *skb, =20 vq =3D &dcb->tx_vqs[vq_id]; srv_id =3D dcb->res_cfg->tx_vq_srv_map[vq_id]; - if (likely(skb_queue_len(&vq->list) < vq->max_len)) + if (likely(skb_queue_len(&vq->list) < vq->max_len)) { skb_queue_tail(&vq->list, skb); - else + } else { + dcb->port_ops->notify(dcb->data_blk, DATA_EVT_TX_STOP, (u64)1 << vq_id); ret =3D -EBUSY; + } =20 wake_up(&dcb->tx_srvs[srv_id].wait); =20 diff --git a/drivers/net/wwan/mediatek/mtk_wwan.c b/drivers/net/wwan/mediat= ek/mtk_wwan.c new file mode 100644 index 000000000000..a0f31cb33271 --- /dev/null +++ b/drivers/net/wwan/mediatek/mtk_wwan.c @@ -0,0 +1,511 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +/* + * Copyright (c) 2022, MediaTek Inc. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mtk_data_plane.h" +#include "mtk_dev.h" + +#define MTK_NETDEV_MAX 20 +#define MTK_DFLT_INTF_ID 0 +#define MTK_NETDEV_WDT (HZ) +#define MTK_CMD_WDT (HZ) +#define MTK_MAX_INTF_ID (MTK_NETDEV_MAX - 1) + +static unsigned int napi_budget =3D 128; + +struct mtk_wwan_instance { + struct mtk_wwan_ctlb *wcb; + struct net_device *netdev; + unsigned int intf_id; +}; + +struct mtk_wwan_ctlb { + struct mtk_data_blk *data_blk; + struct mtk_md_dev *mdev; + struct mtk_data_trans_ops *trans_ops; + struct mtk_wwan_instance __rcu *wwan_inst[MTK_NETDEV_MAX]; + struct napi_struct **napis; + struct net_device dummy_dev; + + u32 cap; + atomic_t napi_enabled; + unsigned int max_mtu; + unsigned int active_cnt; + unsigned char txq_num; + unsigned char rxq_num; + bool reg_done; +}; + +static void mtk_wwan_set_skb(struct sk_buff *skb, struct net_device *netde= v) +{ + unsigned int pkt_type; + + pkt_type =3D skb->data[0] & 0xF0; + + if (pkt_type =3D=3D IPV4_VERSION) + skb->protocol =3D htons(ETH_P_IP); + else + skb->protocol =3D htons(ETH_P_IPV6); + + skb->dev =3D netdev; +} + +static int mtk_wwan_data_recv(struct mtk_data_blk *data_blk, struct sk_buf= f *skb, + unsigned char q_id, unsigned char intf_id) +{ + struct mtk_wwan_instance *wwan_inst; + struct net_device *netdev; + struct napi_struct *napi; + + rcu_read_lock(); + wwan_inst =3D rcu_dereference(data_blk->wcb->wwan_inst[intf_id]); + + napi =3D data_blk->wcb->napis[q_id]; + netdev =3D wwan_inst->netdev; + + mtk_wwan_set_skb(skb, netdev); + + napi_gro_receive(napi, skb); + + rcu_read_unlock(); + return 0; +} + +static void mtk_wwan_napi_enable(struct mtk_wwan_ctlb *wcb) +{ + int i; + + if (atomic_cmpxchg(&wcb->napi_enabled, 0, 1) =3D=3D 0) { + for (i =3D 0; i < wcb->rxq_num; i++) + napi_enable(wcb->napis[i]); + } +} + +static void mtk_wwan_napi_disable(struct mtk_wwan_ctlb *wcb) +{ + int i; + + if (atomic_cmpxchg(&wcb->napi_enabled, 1, 0) =3D=3D 1) { + for (i =3D 0; i < wcb->rxq_num; i++) { + napi_synchronize(wcb->napis[i]); + napi_disable(wcb->napis[i]); + } + } +} + +static int mtk_wwan_open(struct net_device *dev) +{ + struct mtk_wwan_instance *wwan_inst =3D wwan_netdev_drvpriv(dev); + struct mtk_wwan_ctlb *wcb =3D wwan_inst->wcb; + struct mtk_data_trans_ctl trans_ctl; + int ret; + + if (wcb->active_cnt =3D=3D 0) { + mtk_wwan_napi_enable(wcb); + trans_ctl.enable =3D true; + ret =3D mtk_wwan_cmd_execute(dev, DATA_CMD_TRANS_CTL, &trans_ctl); + if (ret < 0) { + dev_err(wcb->mdev->dev, "Failed to enable trans\n"); + goto out; + } + } + + wcb->active_cnt++; + + netif_tx_start_all_queues(dev); + netif_carrier_on(dev); + + return 0; + +out: + mtk_wwan_napi_disable(wcb); + return ret; +} + +static int mtk_wwan_stop(struct net_device *dev) +{ + struct mtk_wwan_instance *wwan_inst =3D wwan_netdev_drvpriv(dev); + struct mtk_wwan_ctlb *wcb =3D wwan_inst->wcb; + struct mtk_data_trans_ctl trans_ctl; + int ret; + + netif_carrier_off(dev); + netif_tx_disable(dev); + + if (wcb->active_cnt =3D=3D 1) { + trans_ctl.enable =3D false; + ret =3D mtk_wwan_cmd_execute(dev, DATA_CMD_TRANS_CTL, &trans_ctl); + if (ret < 0) + dev_err(wcb->mdev->dev, "Failed to disable trans\n"); + mtk_wwan_napi_disable(wcb); + } + wcb->active_cnt--; + + return 0; +} + +static netdev_tx_t mtk_wwan_start_xmit(struct sk_buff *skb, struct net_dev= ice *dev) +{ + struct mtk_wwan_instance *wwan_inst =3D wwan_netdev_drvpriv(dev); + unsigned int intf_id =3D wwan_inst->intf_id; + int ret; + + skb_set_queue_mapping(skb, 0); + + ret =3D wwan_inst->wcb->trans_ops->send(wwan_inst->wcb->data_blk, DATA_PK= T, skb, intf_id); + if (ret =3D=3D -EBUSY) + return NETDEV_TX_BUSY; + + return NETDEV_TX_OK; +} + +static const struct net_device_ops mtk_netdev_ops =3D { + .ndo_open =3D mtk_wwan_open, + .ndo_stop =3D mtk_wwan_stop, + .ndo_start_xmit =3D mtk_wwan_start_xmit, +}; + +static void mtk_wwan_cmd_complete(void *data) +{ + struct mtk_data_cmd *event; + struct sk_buff *skb =3D data; + + event =3D (struct mtk_data_cmd *)skb->data; + complete(&event->done); +} + +static int mtk_wwan_cmd_check(struct net_device *dev, enum mtk_data_cmd_ty= pe cmd) +{ + int ret =3D 0; + + switch (cmd) { + case DATA_CMD_TRANS_CTL: + break; + default: + ret =3D -EOPNOTSUPP; + break; + } + + return ret; +} + +static struct sk_buff *mtk_wwan_cmd_alloc(enum mtk_data_cmd_type cmd, unsi= gned int len) + +{ + struct mtk_data_cmd *event; + struct sk_buff *skb; + + skb =3D dev_alloc_skb(sizeof(*event) + len); + if (unlikely(!skb)) + return NULL; + + skb_put(skb, len + sizeof(*event)); + event =3D (struct mtk_data_cmd *)skb->data; + event->cmd =3D cmd; + event->len =3D len; + + init_completion(&event->done); + event->data_complete =3D mtk_wwan_cmd_complete; + + return skb; +} + +static int mtk_wwan_cmd_send(struct net_device *dev, struct sk_buff *skb) +{ + struct mtk_wwan_instance *wwan_inst =3D wwan_netdev_drvpriv(dev); + struct mtk_data_cmd *event =3D (struct mtk_data_cmd *)skb->data; + int ret; + + ret =3D wwan_inst->wcb->trans_ops->send(wwan_inst->wcb->data_blk, DATA_CM= D, skb, 0); + if (ret < 0) + return ret; + + if (!wait_for_completion_timeout(&event->done, MTK_CMD_WDT)) + return -ETIMEDOUT; + + if (event->ret < 0) + return event->ret; + + return 0; +} + +int mtk_wwan_cmd_execute(struct net_device *dev, + enum mtk_data_cmd_type cmd, void *data) +{ + struct mtk_wwan_instance *wwan_inst; + struct sk_buff *skb; + int ret; + + if (mtk_wwan_cmd_check(dev, cmd)) + return -EOPNOTSUPP; + + skb =3D mtk_wwan_cmd_alloc(cmd, sizeof(void *)); + if (unlikely(!skb)) + return -ENOMEM; + + SKB_TO_CMD_DATA(skb) =3D data; + + ret =3D mtk_wwan_cmd_send(dev, skb); + if (ret < 0) { + wwan_inst =3D wwan_netdev_drvpriv(dev); + dev_err(wwan_inst->wcb->mdev->dev, + "Failed to excute command:ret=3D%d,cmd=3D%d\n", ret, cmd); + } + + if (likely(skb)) + dev_kfree_skb_any(skb); + + return ret; +} + +static int mtk_wwan_start_txq(struct mtk_wwan_ctlb *wcb, u32 qmask) +{ + struct mtk_wwan_instance *wwan_inst; + struct net_device *dev; + int i; + + rcu_read_lock(); + for (i =3D 0; i < MTK_NETDEV_MAX; i++) { + wwan_inst =3D rcu_dereference(wcb->wwan_inst[i]); + if (!wwan_inst) + continue; + + dev =3D wwan_inst->netdev; + + if (!(dev->flags & IFF_UP)) + continue; + + netif_tx_wake_all_queues(dev); + netif_carrier_on(dev); + } + rcu_read_unlock(); + + return 0; +} + +static int mtk_wwan_stop_txq(struct mtk_wwan_ctlb *wcb, u32 qmask) +{ + struct mtk_wwan_instance *wwan_inst; + struct net_device *dev; + int i; + + rcu_read_lock(); + for (i =3D 0; i < MTK_NETDEV_MAX; i++) { + wwan_inst =3D rcu_dereference(wcb->wwan_inst[i]); + if (!wwan_inst) + continue; + + dev =3D wwan_inst->netdev; + + if (!(dev->flags & IFF_UP)) + continue; + + netif_carrier_off(dev); + netif_tx_stop_all_queues(dev); + } + rcu_read_unlock(); + + return 0; +} + +static void mtk_wwan_napi_exit(struct mtk_wwan_ctlb *wcb) +{ + int i; + + for (i =3D 0; i < wcb->rxq_num; i++) { + if (!wcb->napis[i]) + continue; + netif_napi_del(wcb->napis[i]); + } +} + +static int mtk_wwan_napi_init(struct mtk_wwan_ctlb *wcb, struct net_device= *dev) +{ + int i; + + for (i =3D 0; i < wcb->rxq_num; i++) { + if (!wcb->napis[i]) { + dev_err(wcb->mdev->dev, "Invalid napi pointer, napi=3D%d", i); + goto out; + } + netif_napi_add_weight(dev, wcb->napis[i], wcb->trans_ops->poll, napi_bud= get); + } + + return 0; + +out: + for (--i; i >=3D 0; i--) + netif_napi_del(wcb->napis[i]); + return -EINVAL; +} + +static void mtk_wwan_setup(struct net_device *dev) +{ + dev->watchdog_timeo =3D MTK_NETDEV_WDT; + dev->mtu =3D ETH_DATA_LEN; + dev->min_mtu =3D ETH_MIN_MTU; + + dev->features |=3D NETIF_F_HW_CSUM; + dev->hw_features |=3D NETIF_F_HW_CSUM; + + dev->features |=3D NETIF_F_RXCSUM; + dev->hw_features |=3D NETIF_F_RXCSUM; + + dev->addr_len =3D ETH_ALEN; + dev->tx_queue_len =3D DEFAULT_TX_QUEUE_LEN; + + dev->flags =3D IFF_NOARP; + dev->type =3D ARPHRD_NONE; + + dev->needs_free_netdev =3D true; + + dev->netdev_ops =3D &mtk_netdev_ops; +} + +static int mtk_wwan_newlink(void *ctxt, struct net_device *dev, u32 intf_i= d, + struct netlink_ext_ack *extack) +{ + struct mtk_wwan_instance *wwan_inst =3D wwan_netdev_drvpriv(dev); + struct mtk_wwan_ctlb *wcb =3D ctxt; + int ret; + + dev->max_mtu =3D wcb->max_mtu; + + wwan_inst->wcb =3D wcb; + wwan_inst->netdev =3D dev; + wwan_inst->intf_id =3D intf_id; + + if (rcu_access_pointer(wcb->wwan_inst[intf_id])) { + ret =3D -EBUSY; + goto out; + } + + ret =3D register_netdevice(dev); + if (ret) + goto out; + + rcu_assign_pointer(wcb->wwan_inst[intf_id], wwan_inst); + + netif_device_attach(dev); + + return 0; +out: + return ret; +} + +static void mtk_wwan_dellink(void *ctxt, struct net_device *dev, + struct list_head *head) +{ + struct mtk_wwan_instance *wwan_inst =3D wwan_netdev_drvpriv(dev); + int intf_id =3D wwan_inst->intf_id; + struct mtk_wwan_ctlb *wcb =3D ctxt; + + if (WARN_ON(rcu_access_pointer(wcb->wwan_inst[intf_id]) !=3D wwan_inst)) + return; + + RCU_INIT_POINTER(wcb->wwan_inst[intf_id], NULL); + unregister_netdevice_queue(dev, head); +} + +static const struct wwan_ops mtk_wwan_ops =3D { + .priv_size =3D sizeof(struct mtk_wwan_instance), + .setup =3D mtk_wwan_setup, + .newlink =3D mtk_wwan_newlink, + .dellink =3D mtk_wwan_dellink, +}; + +static void mtk_wwan_notify(struct mtk_data_blk *data_blk, enum mtk_data_e= vt evt, u64 data) +{ + struct mtk_wwan_ctlb *wcb; + + wcb =3D data_blk->wcb; + + switch (evt) { + case DATA_EVT_TX_START: + mtk_wwan_start_txq(wcb, data); + break; + case DATA_EVT_TX_STOP: + mtk_wwan_stop_txq(wcb, data); + break; + case DATA_EVT_RX_STOP: + mtk_wwan_napi_disable(wcb); + break; + case DATA_EVT_REG_DEV: + if (!wcb->reg_done) { + wwan_register_ops(wcb->mdev->dev, &mtk_wwan_ops, wcb, MTK_DFLT_INTF_ID); + wcb->reg_done =3D true; + } + break; + case DATA_EVT_UNREG_DEV: + if (wcb->reg_done) { + wwan_unregister_ops(wcb->mdev->dev); + wcb->reg_done =3D false; + } + break; + default: + break; + } +} + +static int mtk_wwan_init(struct mtk_data_blk *data_blk, struct mtk_data_tr= ans_info *trans_info) +{ + struct mtk_wwan_ctlb *wcb; + int ret; + + wcb =3D devm_kzalloc(data_blk->mdev->dev, sizeof(*wcb), GFP_KERNEL); + if (unlikely(!wcb)) + return -ENOMEM; + + wcb->trans_ops =3D &data_trans_ops; + wcb->mdev =3D data_blk->mdev; + wcb->data_blk =3D data_blk; + wcb->napis =3D trans_info->napis; + wcb->max_mtu =3D trans_info->max_mtu; + wcb->cap =3D trans_info->cap; + wcb->rxq_num =3D trans_info->rxq_cnt; + wcb->txq_num =3D trans_info->txq_cnt; + atomic_set(&wcb->napi_enabled, 0); + init_dummy_netdev(&wcb->dummy_dev); + + data_blk->wcb =3D wcb; + + ret =3D mtk_wwan_napi_init(wcb, &wcb->dummy_dev); + if (ret < 0) + goto out; + + return 0; +out: + devm_kfree(data_blk->mdev->dev, wcb); + data_blk->wcb =3D NULL; + + return ret; +} + +static void mtk_wwan_exit(struct mtk_data_blk *data_blk) +{ + struct mtk_wwan_ctlb *wcb; + + wcb =3D data_blk->wcb; + mtk_wwan_napi_exit(wcb); + devm_kfree(data_blk->mdev->dev, wcb); + data_blk->wcb =3D NULL; +} + +struct mtk_data_port_ops data_port_ops =3D { + .init =3D mtk_wwan_init, + .exit =3D mtk_wwan_exit, + .recv =3D mtk_wwan_data_recv, + .notify =3D mtk_wwan_notify, +}; --=20 2.32.0 From nobody Thu Nov 14 06:37:28 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6A69AC7618A for ; Fri, 17 Mar 2023 09:07:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231338AbjCQJH4 (ORCPT ); Fri, 17 Mar 2023 05:07:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229962AbjCQJHf (ORCPT ); Fri, 17 Mar 2023 05:07:35 -0400 Received: from mailgw02.mediatek.com (unknown [210.61.82.184]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F120B4ED0; Fri, 17 Mar 2023 02:06:33 -0700 (PDT) X-UUID: d938815ec49b11edbd2e61cc88cc8f98-20230317 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=FGfHOBVymC0DHuKRYmnC5Uxx4OWvdn5zdJ7U9DYtQ5U=; b=ikcEbLkIxAEIvahriLsuh87t+6pHsv62MC2euRW7W9W4qEOlBJ6pq1fuLCxlffXOOWcIw3Nul//yFdEu+x7OqO1j1SkiVGlFbGd9tr17LgJXEBn00x6Yg/FDGWEzfkWUqdjNezUVC38sZdappQlZYPcKaLgfK1dj7yDJyyXGVe8=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.21,REQID:80b96f3b-72fc-4b21-b459-4786f0e423e3,IP:0,U RL:0,TC:0,Content:0,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION: release,TS:0 X-CID-META: VersionHash:83295aa,CLOUDID:8859ac28-564d-42d9-9875-7c868ee415ec,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:11|1,File:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0 X-CID-BVR: 0,NGT X-CID-BAS: 0,NGT,0,_ X-UUID: d938815ec49b11edbd2e61cc88cc8f98-20230317 Received: from mtkmbs11n2.mediatek.inc [(172.21.101.187)] by mailgw02.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1577523023; Fri, 17 Mar 2023 16:15:16 +0800 Received: from mtkmbs11n2.mediatek.inc (172.21.101.187) by mtkmbs13n2.mediatek.inc (172.21.101.108) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.25; Fri, 17 Mar 2023 16:15:14 +0800 Received: from mcddlt001.gcn.mediatek.inc (10.19.240.15) by mtkmbs11n2.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.25 via Frontend Transport; Fri, 17 Mar 2023 16:15:13 +0800 From: Yanchao Yang To: Loic Poulain , Sergey Ryazanov , Johannes Berg , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , netdev ML , kernel ML CC: Intel experts , Chetan , MTK ML , Liang Lu , Haijun Liu , Hua Yang , Ting Wang , Felix Chen , Mingliang Xu , Min Dong , Aiden Wang , Guohao Zhang , Chris Feng , Yanchao Yang , Lambert Wang , Mingchuang Qiao , Xiayu Zhang , Haozhe Chang Subject: [PATCH net-next v4 10/10] net: wwan: tmi: Add maintainers and documentation Date: Fri, 17 Mar 2023 16:09:42 +0800 Message-ID: <20230317080942.183514-11-yanchao.yang@mediatek.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20230317080942.183514-1-yanchao.yang@mediatek.com> References: <20230317080942.183514-1-yanchao.yang@mediatek.com> MIME-Version: 1.0 X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Adds maintainers and documentation for MediaTek TMI 5G WWAN modem device driver. Signed-off-by: Yanchao Yang Signed-off-by: Felix Chen --- .../networking/device_drivers/wwan/index.rst | 1 + .../networking/device_drivers/wwan/tmi.rst | 48 +++++++++++++++++++ MAINTAINERS | 11 +++++ 3 files changed, 60 insertions(+) create mode 100644 Documentation/networking/device_drivers/wwan/tmi.rst diff --git a/Documentation/networking/device_drivers/wwan/index.rst b/Docum= entation/networking/device_drivers/wwan/index.rst index 370d8264d5dc..8298629b4d55 100644 --- a/Documentation/networking/device_drivers/wwan/index.rst +++ b/Documentation/networking/device_drivers/wwan/index.rst @@ -10,6 +10,7 @@ Contents: =20 iosm t7xx + tmi =20 .. only:: subproject and html =20 diff --git a/Documentation/networking/device_drivers/wwan/tmi.rst b/Documen= tation/networking/device_drivers/wwan/tmi.rst new file mode 100644 index 000000000000..3655779bf692 --- /dev/null +++ b/Documentation/networking/device_drivers/wwan/tmi.rst @@ -0,0 +1,48 @@ +.. SPDX-License-Identifier: BSD-3-Clause-Clear + +.. Copyright (c) 2022, MediaTek Inc. + +.. _tmi_driver_doc: + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D +TMI driver for MTK PCIe based T-series 5G Modem +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D +The TMI(T-series Modem Interface) driver is a WWAN PCIe host driver develo= ped +for data exchange over PCIe interface between Host platform and MediaTek's +T-series 5G modem. The driver exposes control plane and data plane interfa= ces +to applications. The control plane provides device node interfaces for con= trol +data transactions. The data plane provides network link interfaces for IP = data +transactions. + +Control channel userspace ABI +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D +/dev/wwan0at0 character device +------------------------------ +The driver exposes an AT port by implementing AT WWAN Port. +The userspace end of the control channel pipe is a /dev/wwan0at0 character +device. Application shall use this interface to issue AT commands. + +/dev/wwan0mbim0 character device +-------------------------------- +The driver exposes an MBIM interface to the MBIM function by implementing +MBIM WWAN Port. The userspace end of the control channel pipe is a +/dev/wwan0mbim0 character device. Applications shall use this interface +for MBIM protocol communication. + +Data channel userspace ABI +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D +wwan0-X network device +---------------------- +The TMI driver exposes IP link interfaces "wwan0-X" of type "wwan" for IP +traffic. Iproute network utility is used for creating "wwan0-X" network +interfaces and for associating it with the MBIM IP session. + +The userspace management application is responsible for creating a new IP = link +prior to establishing an MBIM IP session where the SessionId is greater th= an 0. + +For example, creating a new IP link for an MBIM IP session with SessionId = 1: + + ip link add dev wwan0-1 parentdev wwan0 type wwan linkid 1 + +The driver will automatically map the "wwan0-1" network device to MBIM IP +session 1. diff --git a/MAINTAINERS b/MAINTAINERS index edd3d562beee..5224a42be5ff 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -13187,6 +13187,17 @@ L: netdev@vger.kernel.org S: Supported F: drivers/net/wwan/t7xx/ =20 +MEDIATEK TMI 5G WWAN MODEM DRIVER +M: Yanchao Yang +M: Min Dong +M: MediaTek Corporation +R: Liang Lu +R: Haijun Liu +R: Lambert Wang +L: netdev@vger.kernel.org +S: Supported +F: drivers/net/wwan/tmi/ + MEDIATEK USB3 DRD IP DRIVER M: Chunfeng Yun L: linux-usb@vger.kernel.org --=20 2.32.0