xref: /linux/drivers/net/wireless/realtek/rtw89/mac.c (revision 8d540f9d29162e273797bef36686a010724f0f8a)
1e3ec7017SPing-Ke Shih // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2e3ec7017SPing-Ke Shih /* Copyright(c) 2019-2020  Realtek Corporation
3e3ec7017SPing-Ke Shih  */
4e3ec7017SPing-Ke Shih 
5e3ec7017SPing-Ke Shih #include "cam.h"
6967439c7SZong-Zhe Yang #include "chan.h"
7e3ec7017SPing-Ke Shih #include "debug.h"
8e3ec7017SPing-Ke Shih #include "fw.h"
9e3ec7017SPing-Ke Shih #include "mac.h"
10e3ec7017SPing-Ke Shih #include "ps.h"
11e3ec7017SPing-Ke Shih #include "reg.h"
12e3ec7017SPing-Ke Shih #include "util.h"
13e3ec7017SPing-Ke Shih 
14ec356ffbSChia-Yuan Li const u32 rtw89_mac_mem_base_addrs[RTW89_MAC_MEM_NUM] = {
15e1400b11SZong-Zhe Yang 	[RTW89_MAC_MEM_AXIDMA]	        = AXIDMA_BASE_ADDR,
16e1400b11SZong-Zhe Yang 	[RTW89_MAC_MEM_SHARED_BUF]	= SHARED_BUF_BASE_ADDR,
17e1400b11SZong-Zhe Yang 	[RTW89_MAC_MEM_DMAC_TBL]	= DMAC_TBL_BASE_ADDR,
18e1400b11SZong-Zhe Yang 	[RTW89_MAC_MEM_SHCUT_MACHDR]	= SHCUT_MACHDR_BASE_ADDR,
19e1400b11SZong-Zhe Yang 	[RTW89_MAC_MEM_STA_SCHED]	= STA_SCHED_BASE_ADDR,
20e1400b11SZong-Zhe Yang 	[RTW89_MAC_MEM_RXPLD_FLTR_CAM]	= RXPLD_FLTR_CAM_BASE_ADDR,
21e1400b11SZong-Zhe Yang 	[RTW89_MAC_MEM_SECURITY_CAM]	= SECURITY_CAM_BASE_ADDR,
22e1400b11SZong-Zhe Yang 	[RTW89_MAC_MEM_WOW_CAM]		= WOW_CAM_BASE_ADDR,
23e1400b11SZong-Zhe Yang 	[RTW89_MAC_MEM_CMAC_TBL]	= CMAC_TBL_BASE_ADDR,
24e1400b11SZong-Zhe Yang 	[RTW89_MAC_MEM_ADDR_CAM]	= ADDR_CAM_BASE_ADDR,
25e1400b11SZong-Zhe Yang 	[RTW89_MAC_MEM_BA_CAM]		= BA_CAM_BASE_ADDR,
26e1400b11SZong-Zhe Yang 	[RTW89_MAC_MEM_BCN_IE_CAM0]	= BCN_IE_CAM0_BASE_ADDR,
27e1400b11SZong-Zhe Yang 	[RTW89_MAC_MEM_BCN_IE_CAM1]	= BCN_IE_CAM1_BASE_ADDR,
28e1400b11SZong-Zhe Yang 	[RTW89_MAC_MEM_TXD_FIFO_0]	= TXD_FIFO_0_BASE_ADDR,
29e1400b11SZong-Zhe Yang 	[RTW89_MAC_MEM_TXD_FIFO_1]	= TXD_FIFO_1_BASE_ADDR,
30e1400b11SZong-Zhe Yang 	[RTW89_MAC_MEM_TXDATA_FIFO_0]	= TXDATA_FIFO_0_BASE_ADDR,
31e1400b11SZong-Zhe Yang 	[RTW89_MAC_MEM_TXDATA_FIFO_1]	= TXDATA_FIFO_1_BASE_ADDR,
32ec356ffbSChia-Yuan Li 	[RTW89_MAC_MEM_CPU_LOCAL]	= CPU_LOCAL_BASE_ADDR,
33dadb2086SPing-Ke Shih 	[RTW89_MAC_MEM_BSSID_CAM]	= BSSID_CAM_BASE_ADDR,
34e1400b11SZong-Zhe Yang };
35e1400b11SZong-Zhe Yang 
36ec356ffbSChia-Yuan Li static void rtw89_mac_mem_write(struct rtw89_dev *rtwdev, u32 offset,
37ec356ffbSChia-Yuan Li 				u32 val, enum rtw89_mac_mem_sel sel)
38ec356ffbSChia-Yuan Li {
39ec356ffbSChia-Yuan Li 	u32 addr = rtw89_mac_mem_base_addrs[sel] + offset;
40ec356ffbSChia-Yuan Li 
41ec356ffbSChia-Yuan Li 	rtw89_write32(rtwdev, R_AX_FILTER_MODEL_ADDR, addr);
42ec356ffbSChia-Yuan Li 	rtw89_write32(rtwdev, R_AX_INDIR_ACCESS_ENTRY, val);
43ec356ffbSChia-Yuan Li }
44ec356ffbSChia-Yuan Li 
45ec356ffbSChia-Yuan Li static u32 rtw89_mac_mem_read(struct rtw89_dev *rtwdev, u32 offset,
46ec356ffbSChia-Yuan Li 			      enum rtw89_mac_mem_sel sel)
47ec356ffbSChia-Yuan Li {
48ec356ffbSChia-Yuan Li 	u32 addr = rtw89_mac_mem_base_addrs[sel] + offset;
49ec356ffbSChia-Yuan Li 
50ec356ffbSChia-Yuan Li 	rtw89_write32(rtwdev, R_AX_FILTER_MODEL_ADDR, addr);
51ec356ffbSChia-Yuan Li 	return rtw89_read32(rtwdev, R_AX_INDIR_ACCESS_ENTRY);
52ec356ffbSChia-Yuan Li }
53ec356ffbSChia-Yuan Li 
54e3ec7017SPing-Ke Shih int rtw89_mac_check_mac_en(struct rtw89_dev *rtwdev, u8 mac_idx,
55e3ec7017SPing-Ke Shih 			   enum rtw89_mac_hwmod_sel sel)
56e3ec7017SPing-Ke Shih {
57e3ec7017SPing-Ke Shih 	u32 val, r_val;
58e3ec7017SPing-Ke Shih 
59e3ec7017SPing-Ke Shih 	if (sel == RTW89_DMAC_SEL) {
60e3ec7017SPing-Ke Shih 		r_val = rtw89_read32(rtwdev, R_AX_DMAC_FUNC_EN);
61e3ec7017SPing-Ke Shih 		val = (B_AX_MAC_FUNC_EN | B_AX_DMAC_FUNC_EN);
62e3ec7017SPing-Ke Shih 	} else if (sel == RTW89_CMAC_SEL && mac_idx == 0) {
63e3ec7017SPing-Ke Shih 		r_val = rtw89_read32(rtwdev, R_AX_CMAC_FUNC_EN);
64e3ec7017SPing-Ke Shih 		val = B_AX_CMAC_EN;
65e3ec7017SPing-Ke Shih 	} else if (sel == RTW89_CMAC_SEL && mac_idx == 1) {
66e3ec7017SPing-Ke Shih 		r_val = rtw89_read32(rtwdev, R_AX_SYS_ISO_CTRL_EXTEND);
67e3ec7017SPing-Ke Shih 		val = B_AX_CMAC1_FEN;
68e3ec7017SPing-Ke Shih 	} else {
69e3ec7017SPing-Ke Shih 		return -EINVAL;
70e3ec7017SPing-Ke Shih 	}
71e3ec7017SPing-Ke Shih 	if (r_val == RTW89_R32_EA || r_val == RTW89_R32_DEAD ||
72e3ec7017SPing-Ke Shih 	    (val & r_val) != val)
73e3ec7017SPing-Ke Shih 		return -EFAULT;
74e3ec7017SPing-Ke Shih 
75e3ec7017SPing-Ke Shih 	return 0;
76e3ec7017SPing-Ke Shih }
77e3ec7017SPing-Ke Shih 
78e3ec7017SPing-Ke Shih int rtw89_mac_write_lte(struct rtw89_dev *rtwdev, const u32 offset, u32 val)
79e3ec7017SPing-Ke Shih {
80e3ec7017SPing-Ke Shih 	u8 lte_ctrl;
81e3ec7017SPing-Ke Shih 	int ret;
82e3ec7017SPing-Ke Shih 
83e3ec7017SPing-Ke Shih 	ret = read_poll_timeout(rtw89_read8, lte_ctrl, (lte_ctrl & BIT(5)) != 0,
84e3ec7017SPing-Ke Shih 				50, 50000, false, rtwdev, R_AX_LTE_CTRL + 3);
85e3ec7017SPing-Ke Shih 	if (ret)
86e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]lte not ready(W)\n");
87e3ec7017SPing-Ke Shih 
88e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_LTE_WDATA, val);
89e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_LTE_CTRL, 0xC00F0000 | offset);
90e3ec7017SPing-Ke Shih 
91e3ec7017SPing-Ke Shih 	return ret;
92e3ec7017SPing-Ke Shih }
93e3ec7017SPing-Ke Shih 
94e3ec7017SPing-Ke Shih int rtw89_mac_read_lte(struct rtw89_dev *rtwdev, const u32 offset, u32 *val)
95e3ec7017SPing-Ke Shih {
96e3ec7017SPing-Ke Shih 	u8 lte_ctrl;
97e3ec7017SPing-Ke Shih 	int ret;
98e3ec7017SPing-Ke Shih 
99e3ec7017SPing-Ke Shih 	ret = read_poll_timeout(rtw89_read8, lte_ctrl, (lte_ctrl & BIT(5)) != 0,
100e3ec7017SPing-Ke Shih 				50, 50000, false, rtwdev, R_AX_LTE_CTRL + 3);
101e3ec7017SPing-Ke Shih 	if (ret)
102e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]lte not ready(W)\n");
103e3ec7017SPing-Ke Shih 
104e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_LTE_CTRL, 0x800F0000 | offset);
105e3ec7017SPing-Ke Shih 	*val = rtw89_read32(rtwdev, R_AX_LTE_RDATA);
106e3ec7017SPing-Ke Shih 
107e3ec7017SPing-Ke Shih 	return ret;
108e3ec7017SPing-Ke Shih }
109e3ec7017SPing-Ke Shih 
110e3ec7017SPing-Ke Shih static
111e3ec7017SPing-Ke Shih int dle_dfi_ctrl(struct rtw89_dev *rtwdev, struct rtw89_mac_dle_dfi_ctrl *ctrl)
112e3ec7017SPing-Ke Shih {
113e3ec7017SPing-Ke Shih 	u32 ctrl_reg, data_reg, ctrl_data;
114e3ec7017SPing-Ke Shih 	u32 val;
115e3ec7017SPing-Ke Shih 	int ret;
116e3ec7017SPing-Ke Shih 
117e3ec7017SPing-Ke Shih 	switch (ctrl->type) {
118e3ec7017SPing-Ke Shih 	case DLE_CTRL_TYPE_WDE:
119e3ec7017SPing-Ke Shih 		ctrl_reg = R_AX_WDE_DBG_FUN_INTF_CTL;
120e3ec7017SPing-Ke Shih 		data_reg = R_AX_WDE_DBG_FUN_INTF_DATA;
121e3ec7017SPing-Ke Shih 		ctrl_data = FIELD_PREP(B_AX_WDE_DFI_TRGSEL_MASK, ctrl->target) |
122e3ec7017SPing-Ke Shih 			    FIELD_PREP(B_AX_WDE_DFI_ADDR_MASK, ctrl->addr) |
123e3ec7017SPing-Ke Shih 			    B_AX_WDE_DFI_ACTIVE;
124e3ec7017SPing-Ke Shih 		break;
125e3ec7017SPing-Ke Shih 	case DLE_CTRL_TYPE_PLE:
126e3ec7017SPing-Ke Shih 		ctrl_reg = R_AX_PLE_DBG_FUN_INTF_CTL;
127e3ec7017SPing-Ke Shih 		data_reg = R_AX_PLE_DBG_FUN_INTF_DATA;
128e3ec7017SPing-Ke Shih 		ctrl_data = FIELD_PREP(B_AX_PLE_DFI_TRGSEL_MASK, ctrl->target) |
129e3ec7017SPing-Ke Shih 			    FIELD_PREP(B_AX_PLE_DFI_ADDR_MASK, ctrl->addr) |
130e3ec7017SPing-Ke Shih 			    B_AX_PLE_DFI_ACTIVE;
131e3ec7017SPing-Ke Shih 		break;
132e3ec7017SPing-Ke Shih 	default:
133e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "[ERR] dfi ctrl type %d\n", ctrl->type);
134e3ec7017SPing-Ke Shih 		return -EINVAL;
135e3ec7017SPing-Ke Shih 	}
136e3ec7017SPing-Ke Shih 
137e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, ctrl_reg, ctrl_data);
138e3ec7017SPing-Ke Shih 
139e3ec7017SPing-Ke Shih 	ret = read_poll_timeout_atomic(rtw89_read32, val, !(val & B_AX_WDE_DFI_ACTIVE),
140e3ec7017SPing-Ke Shih 				       1, 1000, false, rtwdev, ctrl_reg);
141e3ec7017SPing-Ke Shih 	if (ret) {
142e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "[ERR] dle dfi ctrl 0x%X set 0x%X timeout\n",
143e3ec7017SPing-Ke Shih 			   ctrl_reg, ctrl_data);
144e3ec7017SPing-Ke Shih 		return ret;
145e3ec7017SPing-Ke Shih 	}
146e3ec7017SPing-Ke Shih 
147e3ec7017SPing-Ke Shih 	ctrl->out_data = rtw89_read32(rtwdev, data_reg);
148e3ec7017SPing-Ke Shih 	return 0;
149e3ec7017SPing-Ke Shih }
150e3ec7017SPing-Ke Shih 
151e3ec7017SPing-Ke Shih static int dle_dfi_quota(struct rtw89_dev *rtwdev,
152e3ec7017SPing-Ke Shih 			 struct rtw89_mac_dle_dfi_quota *quota)
153e3ec7017SPing-Ke Shih {
154e3ec7017SPing-Ke Shih 	struct rtw89_mac_dle_dfi_ctrl ctrl;
155e3ec7017SPing-Ke Shih 	int ret;
156e3ec7017SPing-Ke Shih 
157e3ec7017SPing-Ke Shih 	ctrl.type = quota->dle_type;
158e3ec7017SPing-Ke Shih 	ctrl.target = DLE_DFI_TYPE_QUOTA;
159e3ec7017SPing-Ke Shih 	ctrl.addr = quota->qtaid;
160e3ec7017SPing-Ke Shih 	ret = dle_dfi_ctrl(rtwdev, &ctrl);
161e3ec7017SPing-Ke Shih 	if (ret) {
162e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "[ERR]dle_dfi_ctrl %d\n", ret);
163e3ec7017SPing-Ke Shih 		return ret;
164e3ec7017SPing-Ke Shih 	}
165e3ec7017SPing-Ke Shih 
166e3ec7017SPing-Ke Shih 	quota->rsv_pgnum = FIELD_GET(B_AX_DLE_RSV_PGNUM, ctrl.out_data);
167e3ec7017SPing-Ke Shih 	quota->use_pgnum = FIELD_GET(B_AX_DLE_USE_PGNUM, ctrl.out_data);
168e3ec7017SPing-Ke Shih 	return 0;
169e3ec7017SPing-Ke Shih }
170e3ec7017SPing-Ke Shih 
171e3ec7017SPing-Ke Shih static int dle_dfi_qempty(struct rtw89_dev *rtwdev,
172e3ec7017SPing-Ke Shih 			  struct rtw89_mac_dle_dfi_qempty *qempty)
173e3ec7017SPing-Ke Shih {
174e3ec7017SPing-Ke Shih 	struct rtw89_mac_dle_dfi_ctrl ctrl;
175e3ec7017SPing-Ke Shih 	u32 ret;
176e3ec7017SPing-Ke Shih 
177e3ec7017SPing-Ke Shih 	ctrl.type = qempty->dle_type;
178e3ec7017SPing-Ke Shih 	ctrl.target = DLE_DFI_TYPE_QEMPTY;
179e3ec7017SPing-Ke Shih 	ctrl.addr = qempty->grpsel;
180e3ec7017SPing-Ke Shih 	ret = dle_dfi_ctrl(rtwdev, &ctrl);
181e3ec7017SPing-Ke Shih 	if (ret) {
182e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "[ERR]dle_dfi_ctrl %d\n", ret);
183e3ec7017SPing-Ke Shih 		return ret;
184e3ec7017SPing-Ke Shih 	}
185e3ec7017SPing-Ke Shih 
186e3ec7017SPing-Ke Shih 	qempty->qempty = FIELD_GET(B_AX_DLE_QEMPTY_GRP, ctrl.out_data);
187e3ec7017SPing-Ke Shih 	return 0;
188e3ec7017SPing-Ke Shih }
189e3ec7017SPing-Ke Shih 
190e3ec7017SPing-Ke Shih static void dump_err_status_dispatcher(struct rtw89_dev *rtwdev)
191e3ec7017SPing-Ke Shih {
192e3ec7017SPing-Ke Shih 	rtw89_info(rtwdev, "R_AX_HOST_DISPATCHER_ALWAYS_IMR=0x%08x ",
193e3ec7017SPing-Ke Shih 		   rtw89_read32(rtwdev, R_AX_HOST_DISPATCHER_ERR_IMR));
194e3ec7017SPing-Ke Shih 	rtw89_info(rtwdev, "R_AX_HOST_DISPATCHER_ALWAYS_ISR=0x%08x\n",
195e3ec7017SPing-Ke Shih 		   rtw89_read32(rtwdev, R_AX_HOST_DISPATCHER_ERR_ISR));
196e3ec7017SPing-Ke Shih 	rtw89_info(rtwdev, "R_AX_CPU_DISPATCHER_ALWAYS_IMR=0x%08x ",
197e3ec7017SPing-Ke Shih 		   rtw89_read32(rtwdev, R_AX_CPU_DISPATCHER_ERR_IMR));
198e3ec7017SPing-Ke Shih 	rtw89_info(rtwdev, "R_AX_CPU_DISPATCHER_ALWAYS_ISR=0x%08x\n",
199e3ec7017SPing-Ke Shih 		   rtw89_read32(rtwdev, R_AX_CPU_DISPATCHER_ERR_ISR));
200e3ec7017SPing-Ke Shih 	rtw89_info(rtwdev, "R_AX_OTHER_DISPATCHER_ALWAYS_IMR=0x%08x ",
201e3ec7017SPing-Ke Shih 		   rtw89_read32(rtwdev, R_AX_OTHER_DISPATCHER_ERR_IMR));
202e3ec7017SPing-Ke Shih 	rtw89_info(rtwdev, "R_AX_OTHER_DISPATCHER_ALWAYS_ISR=0x%08x\n",
203e3ec7017SPing-Ke Shih 		   rtw89_read32(rtwdev, R_AX_OTHER_DISPATCHER_ERR_ISR));
204e3ec7017SPing-Ke Shih }
205e3ec7017SPing-Ke Shih 
206e3ec7017SPing-Ke Shih static void rtw89_mac_dump_qta_lost(struct rtw89_dev *rtwdev)
207e3ec7017SPing-Ke Shih {
208e3ec7017SPing-Ke Shih 	struct rtw89_mac_dle_dfi_qempty qempty;
209e3ec7017SPing-Ke Shih 	struct rtw89_mac_dle_dfi_quota quota;
210e3ec7017SPing-Ke Shih 	struct rtw89_mac_dle_dfi_ctrl ctrl;
211e3ec7017SPing-Ke Shih 	u32 val, not_empty, i;
212e3ec7017SPing-Ke Shih 	int ret;
213e3ec7017SPing-Ke Shih 
214e3ec7017SPing-Ke Shih 	qempty.dle_type = DLE_CTRL_TYPE_PLE;
215e3ec7017SPing-Ke Shih 	qempty.grpsel = 0;
21689e4a00fSÍñigo Huguet 	qempty.qempty = ~(u32)0;
217e3ec7017SPing-Ke Shih 	ret = dle_dfi_qempty(rtwdev, &qempty);
218e3ec7017SPing-Ke Shih 	if (ret)
219e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "%s: query DLE fail\n", __func__);
220e3ec7017SPing-Ke Shih 	else
221e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "DLE group0 empty: 0x%x\n", qempty.qempty);
222e3ec7017SPing-Ke Shih 
223e3ec7017SPing-Ke Shih 	for (not_empty = ~qempty.qempty, i = 0; not_empty != 0; not_empty >>= 1, i++) {
224e3ec7017SPing-Ke Shih 		if (!(not_empty & BIT(0)))
225e3ec7017SPing-Ke Shih 			continue;
226e3ec7017SPing-Ke Shih 		ctrl.type = DLE_CTRL_TYPE_PLE;
227e3ec7017SPing-Ke Shih 		ctrl.target = DLE_DFI_TYPE_QLNKTBL;
228e3ec7017SPing-Ke Shih 		ctrl.addr = (QLNKTBL_ADDR_INFO_SEL_0 ? QLNKTBL_ADDR_INFO_SEL : 0) |
229e3ec7017SPing-Ke Shih 			    FIELD_PREP(QLNKTBL_ADDR_TBL_IDX_MASK, i);
230e3ec7017SPing-Ke Shih 		ret = dle_dfi_ctrl(rtwdev, &ctrl);
231e3ec7017SPing-Ke Shih 		if (ret)
232e3ec7017SPing-Ke Shih 			rtw89_warn(rtwdev, "%s: query DLE fail\n", __func__);
233e3ec7017SPing-Ke Shih 		else
234e3ec7017SPing-Ke Shih 			rtw89_info(rtwdev, "qidx%d pktcnt = %ld\n", i,
235e3ec7017SPing-Ke Shih 				   FIELD_GET(QLNKTBL_DATA_SEL1_PKT_CNT_MASK,
236e3ec7017SPing-Ke Shih 					     ctrl.out_data));
237e3ec7017SPing-Ke Shih 	}
238e3ec7017SPing-Ke Shih 
239e3ec7017SPing-Ke Shih 	quota.dle_type = DLE_CTRL_TYPE_PLE;
240e3ec7017SPing-Ke Shih 	quota.qtaid = 6;
241e3ec7017SPing-Ke Shih 	ret = dle_dfi_quota(rtwdev, &quota);
242e3ec7017SPing-Ke Shih 	if (ret)
243e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "%s: query DLE fail\n", __func__);
244e3ec7017SPing-Ke Shih 	else
245e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "quota6 rsv/use: 0x%x/0x%x\n",
246e3ec7017SPing-Ke Shih 			   quota.rsv_pgnum, quota.use_pgnum);
247e3ec7017SPing-Ke Shih 
248e3ec7017SPing-Ke Shih 	val = rtw89_read32(rtwdev, R_AX_PLE_QTA6_CFG);
249e3ec7017SPing-Ke Shih 	rtw89_info(rtwdev, "[PLE][CMAC0_RX]min_pgnum=0x%lx\n",
250e3ec7017SPing-Ke Shih 		   FIELD_GET(B_AX_PLE_Q6_MIN_SIZE_MASK, val));
251e3ec7017SPing-Ke Shih 	rtw89_info(rtwdev, "[PLE][CMAC0_RX]max_pgnum=0x%lx\n",
252e3ec7017SPing-Ke Shih 		   FIELD_GET(B_AX_PLE_Q6_MAX_SIZE_MASK, val));
253e3ec7017SPing-Ke Shih 
254e3ec7017SPing-Ke Shih 	dump_err_status_dispatcher(rtwdev);
255e3ec7017SPing-Ke Shih }
256e3ec7017SPing-Ke Shih 
257e3ec7017SPing-Ke Shih static void rtw89_mac_dump_l0_to_l1(struct rtw89_dev *rtwdev,
258e3ec7017SPing-Ke Shih 				    enum mac_ax_err_info err)
259e3ec7017SPing-Ke Shih {
260e3ec7017SPing-Ke Shih 	u32 dbg, event;
261e3ec7017SPing-Ke Shih 
262e3ec7017SPing-Ke Shih 	dbg = rtw89_read32(rtwdev, R_AX_SER_DBG_INFO);
263e3ec7017SPing-Ke Shih 	event = FIELD_GET(B_AX_L0_TO_L1_EVENT_MASK, dbg);
264e3ec7017SPing-Ke Shih 
265e3ec7017SPing-Ke Shih 	switch (event) {
266e3ec7017SPing-Ke Shih 	case MAC_AX_L0_TO_L1_RX_QTA_LOST:
267e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "quota lost!\n");
268e3ec7017SPing-Ke Shih 		rtw89_mac_dump_qta_lost(rtwdev);
269e3ec7017SPing-Ke Shih 		break;
270e3ec7017SPing-Ke Shih 	default:
271e3ec7017SPing-Ke Shih 		break;
272e3ec7017SPing-Ke Shih 	}
273e3ec7017SPing-Ke Shih }
274e3ec7017SPing-Ke Shih 
275e3ec7017SPing-Ke Shih static void rtw89_mac_dump_err_status(struct rtw89_dev *rtwdev,
276e3ec7017SPing-Ke Shih 				      enum mac_ax_err_info err)
277e3ec7017SPing-Ke Shih {
278e3ec7017SPing-Ke Shih 	u32 dmac_err, cmac_err;
279e3ec7017SPing-Ke Shih 
280e3ec7017SPing-Ke Shih 	if (err != MAC_AX_ERR_L1_ERR_DMAC &&
281198b6cf7SZong-Zhe Yang 	    err != MAC_AX_ERR_L0_PROMOTE_TO_L1 &&
282198b6cf7SZong-Zhe Yang 	    err != MAC_AX_ERR_L0_ERR_CMAC0 &&
283198b6cf7SZong-Zhe Yang 	    err != MAC_AX_ERR_L0_ERR_CMAC1)
284e3ec7017SPing-Ke Shih 		return;
285e3ec7017SPing-Ke Shih 
286e3ec7017SPing-Ke Shih 	rtw89_info(rtwdev, "--->\nerr=0x%x\n", err);
287e3ec7017SPing-Ke Shih 	rtw89_info(rtwdev, "R_AX_SER_DBG_INFO =0x%08x\n",
288e3ec7017SPing-Ke Shih 		   rtw89_read32(rtwdev, R_AX_SER_DBG_INFO));
289e3ec7017SPing-Ke Shih 
290e3ec7017SPing-Ke Shih 	cmac_err = rtw89_read32(rtwdev, R_AX_CMAC_ERR_ISR);
291e3ec7017SPing-Ke Shih 	rtw89_info(rtwdev, "R_AX_CMAC_ERR_ISR =0x%08x\n", cmac_err);
292e3ec7017SPing-Ke Shih 	dmac_err = rtw89_read32(rtwdev, R_AX_DMAC_ERR_ISR);
293e3ec7017SPing-Ke Shih 	rtw89_info(rtwdev, "R_AX_DMAC_ERR_ISR =0x%08x\n", dmac_err);
294e3ec7017SPing-Ke Shih 
295e3ec7017SPing-Ke Shih 	if (dmac_err) {
296e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_WDE_ERR_FLAG_CFG =0x%08x ",
297e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_WDE_ERR_FLAG_CFG));
298e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_PLE_ERR_FLAG_CFG =0x%08x\n",
299e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_PLE_ERR_FLAG_CFG));
300e3ec7017SPing-Ke Shih 	}
301e3ec7017SPing-Ke Shih 
302e3ec7017SPing-Ke Shih 	if (dmac_err & B_AX_WDRLS_ERR_FLAG) {
303e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_WDRLS_ERR_IMR =0x%08x ",
304e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_WDRLS_ERR_IMR));
305e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_WDRLS_ERR_ISR =0x%08x\n",
306e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_WDRLS_ERR_ISR));
307e3ec7017SPing-Ke Shih 	}
308e3ec7017SPing-Ke Shih 
309e3ec7017SPing-Ke Shih 	if (dmac_err & B_AX_WSEC_ERR_FLAG) {
310e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_SEC_ERR_IMR_ISR =0x%08x\n",
311e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_SEC_DEBUG));
312e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "SEC_local_Register 0x9D00 =0x%08x\n",
313e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_SEC_ENG_CTRL));
314e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "SEC_local_Register 0x9D04 =0x%08x\n",
315e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_SEC_MPDU_PROC));
316e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "SEC_local_Register 0x9D10 =0x%08x\n",
317e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_SEC_CAM_ACCESS));
318e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "SEC_local_Register 0x9D14 =0x%08x\n",
319e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_SEC_CAM_RDATA));
320e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "SEC_local_Register 0x9D18 =0x%08x\n",
321e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_SEC_CAM_WDATA));
322e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "SEC_local_Register 0x9D20 =0x%08x\n",
323e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_SEC_TX_DEBUG));
324e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "SEC_local_Register 0x9D24 =0x%08x\n",
325e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_SEC_RX_DEBUG));
326e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "SEC_local_Register 0x9D28 =0x%08x\n",
327e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_SEC_TRX_PKT_CNT));
328e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "SEC_local_Register 0x9D2C =0x%08x\n",
329e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_SEC_TRX_BLK_CNT));
330e3ec7017SPing-Ke Shih 	}
331e3ec7017SPing-Ke Shih 
332e3ec7017SPing-Ke Shih 	if (dmac_err & B_AX_MPDU_ERR_FLAG) {
333e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_MPDU_TX_ERR_IMR =0x%08x ",
334e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_MPDU_TX_ERR_IMR));
335e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_MPDU_TX_ERR_ISR =0x%08x\n",
336e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_MPDU_TX_ERR_ISR));
337e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_MPDU_RX_ERR_IMR =0x%08x ",
338e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_MPDU_RX_ERR_IMR));
339e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_MPDU_RX_ERR_ISR =0x%08x\n",
340e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_MPDU_RX_ERR_ISR));
341e3ec7017SPing-Ke Shih 	}
342e3ec7017SPing-Ke Shih 
343e3ec7017SPing-Ke Shih 	if (dmac_err & B_AX_STA_SCHEDULER_ERR_FLAG) {
344e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_STA_SCHEDULER_ERR_IMR =0x%08x ",
345e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_STA_SCHEDULER_ERR_IMR));
346e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_STA_SCHEDULER_ERR_ISR= 0x%08x\n",
347e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_STA_SCHEDULER_ERR_ISR));
348e3ec7017SPing-Ke Shih 	}
349e3ec7017SPing-Ke Shih 
350e3ec7017SPing-Ke Shih 	if (dmac_err & B_AX_WDE_DLE_ERR_FLAG) {
351e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_WDE_ERR_IMR=0x%08x ",
352e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_WDE_ERR_IMR));
353e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_WDE_ERR_ISR=0x%08x\n",
354e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_WDE_ERR_ISR));
355e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_PLE_ERR_IMR=0x%08x ",
356e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_PLE_ERR_IMR));
357e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_PLE_ERR_FLAG_ISR=0x%08x\n",
358e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_PLE_ERR_FLAG_ISR));
359e3ec7017SPing-Ke Shih 		dump_err_status_dispatcher(rtwdev);
360e3ec7017SPing-Ke Shih 	}
361e3ec7017SPing-Ke Shih 
362e3ec7017SPing-Ke Shih 	if (dmac_err & B_AX_TXPKTCTRL_ERR_FLAG) {
363e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_TXPKTCTL_ERR_IMR_ISR=0x%08x\n",
364e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_TXPKTCTL_ERR_IMR_ISR));
365e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_TXPKTCTL_ERR_IMR_ISR_B1=0x%08x\n",
366e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_TXPKTCTL_ERR_IMR_ISR_B1));
367e3ec7017SPing-Ke Shih 	}
368e3ec7017SPing-Ke Shih 
369e3ec7017SPing-Ke Shih 	if (dmac_err & B_AX_PLE_DLE_ERR_FLAG) {
370e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_WDE_ERR_IMR=0x%08x ",
371e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_WDE_ERR_IMR));
372e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_WDE_ERR_ISR=0x%08x\n",
373e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_WDE_ERR_ISR));
374e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_PLE_ERR_IMR=0x%08x ",
375e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_PLE_ERR_IMR));
376e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_PLE_ERR_FLAG_ISR=0x%08x\n",
377e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_PLE_ERR_FLAG_ISR));
378e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_WD_CPUQ_OP_0=0x%08x\n",
379e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_WD_CPUQ_OP_0));
380e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_WD_CPUQ_OP_1=0x%08x\n",
381e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_WD_CPUQ_OP_1));
382e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_WD_CPUQ_OP_2=0x%08x\n",
383e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_WD_CPUQ_OP_2));
384e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_WD_CPUQ_OP_STATUS=0x%08x\n",
385e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_WD_CPUQ_OP_STATUS));
386e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_PL_CPUQ_OP_0=0x%08x\n",
387e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_PL_CPUQ_OP_0));
388e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_PL_CPUQ_OP_1=0x%08x\n",
389e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_PL_CPUQ_OP_1));
390e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_PL_CPUQ_OP_2=0x%08x\n",
391e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_PL_CPUQ_OP_2));
392e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_PL_CPUQ_OP_STATUS=0x%08x\n",
393e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_PL_CPUQ_OP_STATUS));
394e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_RXDMA_PKT_INFO_0=0x%08x\n",
395e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_RXDMA_PKT_INFO_0));
396e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_RXDMA_PKT_INFO_1=0x%08x\n",
397e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_RXDMA_PKT_INFO_1));
398e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_RXDMA_PKT_INFO_2=0x%08x\n",
399e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_RXDMA_PKT_INFO_2));
400e3ec7017SPing-Ke Shih 		dump_err_status_dispatcher(rtwdev);
401e3ec7017SPing-Ke Shih 	}
402e3ec7017SPing-Ke Shih 
403e3ec7017SPing-Ke Shih 	if (dmac_err & B_AX_PKTIN_ERR_FLAG) {
404e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_PKTIN_ERR_IMR =0x%08x ",
405e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_PKTIN_ERR_IMR));
406e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_PKTIN_ERR_ISR =0x%08x\n",
407e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_PKTIN_ERR_ISR));
408e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_PKTIN_ERR_IMR =0x%08x ",
409e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_PKTIN_ERR_IMR));
410e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_PKTIN_ERR_ISR =0x%08x\n",
411e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_PKTIN_ERR_ISR));
412e3ec7017SPing-Ke Shih 	}
413e3ec7017SPing-Ke Shih 
414e3ec7017SPing-Ke Shih 	if (dmac_err & B_AX_DISPATCH_ERR_FLAG)
415e3ec7017SPing-Ke Shih 		dump_err_status_dispatcher(rtwdev);
416e3ec7017SPing-Ke Shih 
417e3ec7017SPing-Ke Shih 	if (dmac_err & B_AX_DLE_CPUIO_ERR_FLAG) {
418e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_CPUIO_ERR_IMR=0x%08x ",
419e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_CPUIO_ERR_IMR));
420e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_CPUIO_ERR_ISR=0x%08x\n",
421e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_CPUIO_ERR_ISR));
422e3ec7017SPing-Ke Shih 	}
423e3ec7017SPing-Ke Shih 
424e3ec7017SPing-Ke Shih 	if (dmac_err & BIT(11)) {
425e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_BBRPT_COM_ERR_IMR_ISR=0x%08x\n",
426e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_BBRPT_COM_ERR_IMR_ISR));
427e3ec7017SPing-Ke Shih 	}
428e3ec7017SPing-Ke Shih 
429e3ec7017SPing-Ke Shih 	if (cmac_err & B_AX_SCHEDULE_TOP_ERR_IND) {
430e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_SCHEDULE_ERR_IMR=0x%08x ",
431e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_SCHEDULE_ERR_IMR));
432e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_SCHEDULE_ERR_ISR=0x%04x\n",
433e3ec7017SPing-Ke Shih 			   rtw89_read16(rtwdev, R_AX_SCHEDULE_ERR_ISR));
434e3ec7017SPing-Ke Shih 	}
435e3ec7017SPing-Ke Shih 
436e3ec7017SPing-Ke Shih 	if (cmac_err & B_AX_PTCL_TOP_ERR_IND) {
437e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_PTCL_IMR0=0x%08x ",
438e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_PTCL_IMR0));
439e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_PTCL_ISR0=0x%08x\n",
440e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_PTCL_ISR0));
441e3ec7017SPing-Ke Shih 	}
442e3ec7017SPing-Ke Shih 
443e3ec7017SPing-Ke Shih 	if (cmac_err & B_AX_DMA_TOP_ERR_IND) {
444e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_DLE_CTRL=0x%08x\n",
445e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_DLE_CTRL));
446e3ec7017SPing-Ke Shih 	}
447e3ec7017SPing-Ke Shih 
448e3ec7017SPing-Ke Shih 	if (cmac_err & B_AX_PHYINTF_ERR_IND) {
449e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_PHYINFO_ERR_IMR=0x%08x\n",
450e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_PHYINFO_ERR_IMR));
451e3ec7017SPing-Ke Shih 	}
452e3ec7017SPing-Ke Shih 
453e3ec7017SPing-Ke Shih 	if (cmac_err & B_AX_TXPWR_CTRL_ERR_IND) {
454e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_TXPWR_IMR=0x%08x ",
455e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_TXPWR_IMR));
456e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_TXPWR_ISR=0x%08x\n",
457e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_TXPWR_ISR));
458e3ec7017SPing-Ke Shih 	}
459e3ec7017SPing-Ke Shih 
460e3ec7017SPing-Ke Shih 	if (cmac_err & B_AX_WMAC_RX_ERR_IND) {
461e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_DBGSEL_TRXPTCL=0x%08x ",
462e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_DBGSEL_TRXPTCL));
463e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_PHYINFO_ERR_ISR=0x%08x\n",
464e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_PHYINFO_ERR_ISR));
465e3ec7017SPing-Ke Shih 	}
466e3ec7017SPing-Ke Shih 
467e3ec7017SPing-Ke Shih 	if (cmac_err & B_AX_WMAC_TX_ERR_IND) {
468e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_TMAC_ERR_IMR_ISR=0x%08x ",
469e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_TMAC_ERR_IMR_ISR));
470e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "R_AX_DBGSEL_TRXPTCL=0x%08x\n",
471e3ec7017SPing-Ke Shih 			   rtw89_read32(rtwdev, R_AX_DBGSEL_TRXPTCL));
472e3ec7017SPing-Ke Shih 	}
473e3ec7017SPing-Ke Shih 
474e3ec7017SPing-Ke Shih 	rtwdev->hci.ops->dump_err_status(rtwdev);
475e3ec7017SPing-Ke Shih 
476e3ec7017SPing-Ke Shih 	if (err == MAC_AX_ERR_L0_PROMOTE_TO_L1)
477e3ec7017SPing-Ke Shih 		rtw89_mac_dump_l0_to_l1(rtwdev, err);
478e3ec7017SPing-Ke Shih 
479e3ec7017SPing-Ke Shih 	rtw89_info(rtwdev, "<---\n");
480e3ec7017SPing-Ke Shih }
481e3ec7017SPing-Ke Shih 
482e3ec7017SPing-Ke Shih u32 rtw89_mac_get_err_status(struct rtw89_dev *rtwdev)
483e3ec7017SPing-Ke Shih {
484198b6cf7SZong-Zhe Yang 	u32 err, err_scnr;
485e3ec7017SPing-Ke Shih 	int ret;
486e3ec7017SPing-Ke Shih 
487e3ec7017SPing-Ke Shih 	ret = read_poll_timeout(rtw89_read32, err, (err != 0), 1000, 100000,
488e3ec7017SPing-Ke Shih 				false, rtwdev, R_AX_HALT_C2H_CTRL);
489e3ec7017SPing-Ke Shih 	if (ret) {
490e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "Polling FW err status fail\n");
491e3ec7017SPing-Ke Shih 		return ret;
492e3ec7017SPing-Ke Shih 	}
493e3ec7017SPing-Ke Shih 
494e3ec7017SPing-Ke Shih 	err = rtw89_read32(rtwdev, R_AX_HALT_C2H);
495e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_HALT_C2H_CTRL, 0);
496e3ec7017SPing-Ke Shih 
497198b6cf7SZong-Zhe Yang 	err_scnr = RTW89_ERROR_SCENARIO(err);
498198b6cf7SZong-Zhe Yang 	if (err_scnr == RTW89_WCPU_CPU_EXCEPTION)
499198b6cf7SZong-Zhe Yang 		err = MAC_AX_ERR_CPU_EXCEPTION;
500198b6cf7SZong-Zhe Yang 	else if (err_scnr == RTW89_WCPU_ASSERTION)
501198b6cf7SZong-Zhe Yang 		err = MAC_AX_ERR_ASSERTION;
502198b6cf7SZong-Zhe Yang 
503e3ec7017SPing-Ke Shih 	rtw89_fw_st_dbg_dump(rtwdev);
504e3ec7017SPing-Ke Shih 	rtw89_mac_dump_err_status(rtwdev, err);
505e3ec7017SPing-Ke Shih 
506e3ec7017SPing-Ke Shih 	return err;
507e3ec7017SPing-Ke Shih }
508e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_mac_get_err_status);
509e3ec7017SPing-Ke Shih 
510e3ec7017SPing-Ke Shih int rtw89_mac_set_err_status(struct rtw89_dev *rtwdev, u32 err)
511e3ec7017SPing-Ke Shih {
512e3ec7017SPing-Ke Shih 	u32 halt;
513e3ec7017SPing-Ke Shih 	int ret = 0;
514e3ec7017SPing-Ke Shih 
515e3ec7017SPing-Ke Shih 	if (err > MAC_AX_SET_ERR_MAX) {
516e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "Bad set-err-status value 0x%08x\n", err);
517e3ec7017SPing-Ke Shih 		return -EINVAL;
518e3ec7017SPing-Ke Shih 	}
519e3ec7017SPing-Ke Shih 
520e3ec7017SPing-Ke Shih 	ret = read_poll_timeout(rtw89_read32, halt, (halt == 0x0), 1000,
521e3ec7017SPing-Ke Shih 				100000, false, rtwdev, R_AX_HALT_H2C_CTRL);
522e3ec7017SPing-Ke Shih 	if (ret) {
523e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "FW doesn't receive previous msg\n");
524e3ec7017SPing-Ke Shih 		return -EFAULT;
525e3ec7017SPing-Ke Shih 	}
526e3ec7017SPing-Ke Shih 
527e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_HALT_H2C, err);
528e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_HALT_H2C_CTRL, B_AX_HALT_H2C_TRIGGER);
529e3ec7017SPing-Ke Shih 
530e3ec7017SPing-Ke Shih 	return 0;
531e3ec7017SPing-Ke Shih }
532e3ec7017SPing-Ke Shih EXPORT_SYMBOL(rtw89_mac_set_err_status);
533e3ec7017SPing-Ke Shih 
534e3ec7017SPing-Ke Shih static int hfc_reset_param(struct rtw89_dev *rtwdev)
535e3ec7017SPing-Ke Shih {
536e3ec7017SPing-Ke Shih 	struct rtw89_hfc_param *param = &rtwdev->mac.hfc_param;
537e3ec7017SPing-Ke Shih 	struct rtw89_hfc_param_ini param_ini = {NULL};
538e3ec7017SPing-Ke Shih 	u8 qta_mode = rtwdev->mac.dle_info.qta_mode;
539e3ec7017SPing-Ke Shih 
540e3ec7017SPing-Ke Shih 	switch (rtwdev->hci.type) {
541e3ec7017SPing-Ke Shih 	case RTW89_HCI_TYPE_PCIE:
542e3ec7017SPing-Ke Shih 		param_ini = rtwdev->chip->hfc_param_ini[qta_mode];
543e3ec7017SPing-Ke Shih 		param->en = 0;
544e3ec7017SPing-Ke Shih 		break;
545e3ec7017SPing-Ke Shih 	default:
546e3ec7017SPing-Ke Shih 		return -EINVAL;
547e3ec7017SPing-Ke Shih 	}
548e3ec7017SPing-Ke Shih 
549e3ec7017SPing-Ke Shih 	if (param_ini.pub_cfg)
550e3ec7017SPing-Ke Shih 		param->pub_cfg = *param_ini.pub_cfg;
551e3ec7017SPing-Ke Shih 
552e3ec7017SPing-Ke Shih 	if (param_ini.prec_cfg) {
553e3ec7017SPing-Ke Shih 		param->prec_cfg = *param_ini.prec_cfg;
554e3ec7017SPing-Ke Shih 		rtwdev->hal.sw_amsdu_max_size =
555e3ec7017SPing-Ke Shih 				param->prec_cfg.wp_ch07_prec * HFC_PAGE_UNIT;
556e3ec7017SPing-Ke Shih 	}
557e3ec7017SPing-Ke Shih 
558e3ec7017SPing-Ke Shih 	if (param_ini.ch_cfg)
559e3ec7017SPing-Ke Shih 		param->ch_cfg = param_ini.ch_cfg;
560e3ec7017SPing-Ke Shih 
561e3ec7017SPing-Ke Shih 	memset(&param->ch_info, 0, sizeof(param->ch_info));
562e3ec7017SPing-Ke Shih 	memset(&param->pub_info, 0, sizeof(param->pub_info));
563e3ec7017SPing-Ke Shih 	param->mode = param_ini.mode;
564e3ec7017SPing-Ke Shih 
565e3ec7017SPing-Ke Shih 	return 0;
566e3ec7017SPing-Ke Shih }
567e3ec7017SPing-Ke Shih 
568e3ec7017SPing-Ke Shih static int hfc_ch_cfg_chk(struct rtw89_dev *rtwdev, u8 ch)
569e3ec7017SPing-Ke Shih {
570e3ec7017SPing-Ke Shih 	struct rtw89_hfc_param *param = &rtwdev->mac.hfc_param;
571e3ec7017SPing-Ke Shih 	const struct rtw89_hfc_ch_cfg *ch_cfg = param->ch_cfg;
572e3ec7017SPing-Ke Shih 	const struct rtw89_hfc_pub_cfg *pub_cfg = &param->pub_cfg;
573e3ec7017SPing-Ke Shih 	const struct rtw89_hfc_prec_cfg *prec_cfg = &param->prec_cfg;
574e3ec7017SPing-Ke Shih 
575e3ec7017SPing-Ke Shih 	if (ch >= RTW89_DMA_CH_NUM)
576e3ec7017SPing-Ke Shih 		return -EINVAL;
577e3ec7017SPing-Ke Shih 
578e3ec7017SPing-Ke Shih 	if ((ch_cfg[ch].min && ch_cfg[ch].min < prec_cfg->ch011_prec) ||
579e3ec7017SPing-Ke Shih 	    ch_cfg[ch].max > pub_cfg->pub_max)
580e3ec7017SPing-Ke Shih 		return -EINVAL;
581e3ec7017SPing-Ke Shih 	if (ch_cfg[ch].grp >= grp_num)
582e3ec7017SPing-Ke Shih 		return -EINVAL;
583e3ec7017SPing-Ke Shih 
584e3ec7017SPing-Ke Shih 	return 0;
585e3ec7017SPing-Ke Shih }
586e3ec7017SPing-Ke Shih 
587e3ec7017SPing-Ke Shih static int hfc_pub_info_chk(struct rtw89_dev *rtwdev)
588e3ec7017SPing-Ke Shih {
589e3ec7017SPing-Ke Shih 	struct rtw89_hfc_param *param = &rtwdev->mac.hfc_param;
590e3ec7017SPing-Ke Shih 	const struct rtw89_hfc_pub_cfg *cfg = &param->pub_cfg;
591e3ec7017SPing-Ke Shih 	struct rtw89_hfc_pub_info *info = &param->pub_info;
592e3ec7017SPing-Ke Shih 
593e3ec7017SPing-Ke Shih 	if (info->g0_used + info->g1_used + info->pub_aval != cfg->pub_max) {
594e3ec7017SPing-Ke Shih 		if (rtwdev->chip->chip_id == RTL8852A)
595e3ec7017SPing-Ke Shih 			return 0;
596e3ec7017SPing-Ke Shih 		else
597e3ec7017SPing-Ke Shih 			return -EFAULT;
598e3ec7017SPing-Ke Shih 	}
599e3ec7017SPing-Ke Shih 
600e3ec7017SPing-Ke Shih 	return 0;
601e3ec7017SPing-Ke Shih }
602e3ec7017SPing-Ke Shih 
603e3ec7017SPing-Ke Shih static int hfc_pub_cfg_chk(struct rtw89_dev *rtwdev)
604e3ec7017SPing-Ke Shih {
605e3ec7017SPing-Ke Shih 	struct rtw89_hfc_param *param = &rtwdev->mac.hfc_param;
606e3ec7017SPing-Ke Shih 	const struct rtw89_hfc_pub_cfg *pub_cfg = &param->pub_cfg;
607e3ec7017SPing-Ke Shih 
608e3ec7017SPing-Ke Shih 	if (pub_cfg->grp0 + pub_cfg->grp1 != pub_cfg->pub_max)
609c6477cb2SKevin Lo 		return -EFAULT;
610e3ec7017SPing-Ke Shih 
611e3ec7017SPing-Ke Shih 	return 0;
612e3ec7017SPing-Ke Shih }
613e3ec7017SPing-Ke Shih 
614e3ec7017SPing-Ke Shih static int hfc_ch_ctrl(struct rtw89_dev *rtwdev, u8 ch)
615e3ec7017SPing-Ke Shih {
616ab8a5671SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
617ab8a5671SPing-Ke Shih 	const struct rtw89_page_regs *regs = chip->page_regs;
618e3ec7017SPing-Ke Shih 	struct rtw89_hfc_param *param = &rtwdev->mac.hfc_param;
619e3ec7017SPing-Ke Shih 	const struct rtw89_hfc_ch_cfg *cfg = param->ch_cfg;
620e3ec7017SPing-Ke Shih 	int ret = 0;
621e3ec7017SPing-Ke Shih 	u32 val = 0;
622e3ec7017SPing-Ke Shih 
623e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, RTW89_MAC_0, RTW89_DMAC_SEL);
624e3ec7017SPing-Ke Shih 	if (ret)
625e3ec7017SPing-Ke Shih 		return ret;
626e3ec7017SPing-Ke Shih 
627e3ec7017SPing-Ke Shih 	ret = hfc_ch_cfg_chk(rtwdev, ch);
628e3ec7017SPing-Ke Shih 	if (ret)
629e3ec7017SPing-Ke Shih 		return ret;
630e3ec7017SPing-Ke Shih 
631e3ec7017SPing-Ke Shih 	if (ch > RTW89_DMA_B1HI)
632e3ec7017SPing-Ke Shih 		return -EINVAL;
633e3ec7017SPing-Ke Shih 
634e3ec7017SPing-Ke Shih 	val = u32_encode_bits(cfg[ch].min, B_AX_MIN_PG_MASK) |
635e3ec7017SPing-Ke Shih 	      u32_encode_bits(cfg[ch].max, B_AX_MAX_PG_MASK) |
636e3ec7017SPing-Ke Shih 	      (cfg[ch].grp ? B_AX_GRP : 0);
637ab8a5671SPing-Ke Shih 	rtw89_write32(rtwdev, regs->ach_page_ctrl + ch * 4, val);
638e3ec7017SPing-Ke Shih 
639e3ec7017SPing-Ke Shih 	return 0;
640e3ec7017SPing-Ke Shih }
641e3ec7017SPing-Ke Shih 
642e3ec7017SPing-Ke Shih static int hfc_upd_ch_info(struct rtw89_dev *rtwdev, u8 ch)
643e3ec7017SPing-Ke Shih {
644ab8a5671SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
645ab8a5671SPing-Ke Shih 	const struct rtw89_page_regs *regs = chip->page_regs;
646e3ec7017SPing-Ke Shih 	struct rtw89_hfc_param *param = &rtwdev->mac.hfc_param;
647e3ec7017SPing-Ke Shih 	struct rtw89_hfc_ch_info *info = param->ch_info;
648e3ec7017SPing-Ke Shih 	const struct rtw89_hfc_ch_cfg *cfg = param->ch_cfg;
649e3ec7017SPing-Ke Shih 	u32 val;
650e3ec7017SPing-Ke Shih 	u32 ret;
651e3ec7017SPing-Ke Shih 
652e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, RTW89_MAC_0, RTW89_DMAC_SEL);
653e3ec7017SPing-Ke Shih 	if (ret)
654e3ec7017SPing-Ke Shih 		return ret;
655e3ec7017SPing-Ke Shih 
656e3ec7017SPing-Ke Shih 	if (ch > RTW89_DMA_H2C)
657e3ec7017SPing-Ke Shih 		return -EINVAL;
658e3ec7017SPing-Ke Shih 
659ab8a5671SPing-Ke Shih 	val = rtw89_read32(rtwdev, regs->ach_page_info + ch * 4);
660e3ec7017SPing-Ke Shih 	info[ch].aval = u32_get_bits(val, B_AX_AVAL_PG_MASK);
661e3ec7017SPing-Ke Shih 	if (ch < RTW89_DMA_H2C)
662e3ec7017SPing-Ke Shih 		info[ch].used = u32_get_bits(val, B_AX_USE_PG_MASK);
663e3ec7017SPing-Ke Shih 	else
664e3ec7017SPing-Ke Shih 		info[ch].used = cfg[ch].min - info[ch].aval;
665e3ec7017SPing-Ke Shih 
666e3ec7017SPing-Ke Shih 	return 0;
667e3ec7017SPing-Ke Shih }
668e3ec7017SPing-Ke Shih 
669e3ec7017SPing-Ke Shih static int hfc_pub_ctrl(struct rtw89_dev *rtwdev)
670e3ec7017SPing-Ke Shih {
671ab8a5671SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
672ab8a5671SPing-Ke Shih 	const struct rtw89_page_regs *regs = chip->page_regs;
673e3ec7017SPing-Ke Shih 	const struct rtw89_hfc_pub_cfg *cfg = &rtwdev->mac.hfc_param.pub_cfg;
674e3ec7017SPing-Ke Shih 	u32 val;
675e3ec7017SPing-Ke Shih 	int ret;
676e3ec7017SPing-Ke Shih 
677e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, RTW89_MAC_0, RTW89_DMAC_SEL);
678e3ec7017SPing-Ke Shih 	if (ret)
679e3ec7017SPing-Ke Shih 		return ret;
680e3ec7017SPing-Ke Shih 
681e3ec7017SPing-Ke Shih 	ret = hfc_pub_cfg_chk(rtwdev);
682e3ec7017SPing-Ke Shih 	if (ret)
683e3ec7017SPing-Ke Shih 		return ret;
684e3ec7017SPing-Ke Shih 
685e3ec7017SPing-Ke Shih 	val = u32_encode_bits(cfg->grp0, B_AX_PUBPG_G0_MASK) |
686e3ec7017SPing-Ke Shih 	      u32_encode_bits(cfg->grp1, B_AX_PUBPG_G1_MASK);
687ab8a5671SPing-Ke Shih 	rtw89_write32(rtwdev, regs->pub_page_ctrl1, val);
688e3ec7017SPing-Ke Shih 
689e3ec7017SPing-Ke Shih 	val = u32_encode_bits(cfg->wp_thrd, B_AX_WP_THRD_MASK);
690ab8a5671SPing-Ke Shih 	rtw89_write32(rtwdev, regs->wp_page_ctrl2, val);
691e3ec7017SPing-Ke Shih 
692e3ec7017SPing-Ke Shih 	return 0;
693e3ec7017SPing-Ke Shih }
694e3ec7017SPing-Ke Shih 
695e3ec7017SPing-Ke Shih static int hfc_upd_mix_info(struct rtw89_dev *rtwdev)
696e3ec7017SPing-Ke Shih {
697ab8a5671SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
698ab8a5671SPing-Ke Shih 	const struct rtw89_page_regs *regs = chip->page_regs;
699e3ec7017SPing-Ke Shih 	struct rtw89_hfc_param *param = &rtwdev->mac.hfc_param;
700e3ec7017SPing-Ke Shih 	struct rtw89_hfc_pub_cfg *pub_cfg = &param->pub_cfg;
701e3ec7017SPing-Ke Shih 	struct rtw89_hfc_prec_cfg *prec_cfg = &param->prec_cfg;
702e3ec7017SPing-Ke Shih 	struct rtw89_hfc_pub_info *info = &param->pub_info;
703e3ec7017SPing-Ke Shih 	u32 val;
704e3ec7017SPing-Ke Shih 	int ret;
705e3ec7017SPing-Ke Shih 
706e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, RTW89_MAC_0, RTW89_DMAC_SEL);
707e3ec7017SPing-Ke Shih 	if (ret)
708e3ec7017SPing-Ke Shih 		return ret;
709e3ec7017SPing-Ke Shih 
710ab8a5671SPing-Ke Shih 	val = rtw89_read32(rtwdev, regs->pub_page_info1);
711e3ec7017SPing-Ke Shih 	info->g0_used = u32_get_bits(val, B_AX_G0_USE_PG_MASK);
712e3ec7017SPing-Ke Shih 	info->g1_used = u32_get_bits(val, B_AX_G1_USE_PG_MASK);
713ab8a5671SPing-Ke Shih 	val = rtw89_read32(rtwdev, regs->pub_page_info3);
714e3ec7017SPing-Ke Shih 	info->g0_aval = u32_get_bits(val, B_AX_G0_AVAL_PG_MASK);
715e3ec7017SPing-Ke Shih 	info->g1_aval = u32_get_bits(val, B_AX_G1_AVAL_PG_MASK);
716e3ec7017SPing-Ke Shih 	info->pub_aval =
717ab8a5671SPing-Ke Shih 		u32_get_bits(rtw89_read32(rtwdev, regs->pub_page_info2),
718e3ec7017SPing-Ke Shih 			     B_AX_PUB_AVAL_PG_MASK);
719e3ec7017SPing-Ke Shih 	info->wp_aval =
720ab8a5671SPing-Ke Shih 		u32_get_bits(rtw89_read32(rtwdev, regs->wp_page_info1),
721e3ec7017SPing-Ke Shih 			     B_AX_WP_AVAL_PG_MASK);
722e3ec7017SPing-Ke Shih 
723ab8a5671SPing-Ke Shih 	val = rtw89_read32(rtwdev, regs->hci_fc_ctrl);
724e3ec7017SPing-Ke Shih 	param->en = val & B_AX_HCI_FC_EN ? 1 : 0;
725e3ec7017SPing-Ke Shih 	param->h2c_en = val & B_AX_HCI_FC_CH12_EN ? 1 : 0;
726e3ec7017SPing-Ke Shih 	param->mode = u32_get_bits(val, B_AX_HCI_FC_MODE_MASK);
727e3ec7017SPing-Ke Shih 	prec_cfg->ch011_full_cond =
728e3ec7017SPing-Ke Shih 		u32_get_bits(val, B_AX_HCI_FC_WD_FULL_COND_MASK);
729e3ec7017SPing-Ke Shih 	prec_cfg->h2c_full_cond =
730e3ec7017SPing-Ke Shih 		u32_get_bits(val, B_AX_HCI_FC_CH12_FULL_COND_MASK);
731e3ec7017SPing-Ke Shih 	prec_cfg->wp_ch07_full_cond =
732e3ec7017SPing-Ke Shih 		u32_get_bits(val, B_AX_HCI_FC_WP_CH07_FULL_COND_MASK);
733e3ec7017SPing-Ke Shih 	prec_cfg->wp_ch811_full_cond =
734e3ec7017SPing-Ke Shih 		u32_get_bits(val, B_AX_HCI_FC_WP_CH811_FULL_COND_MASK);
735e3ec7017SPing-Ke Shih 
736ab8a5671SPing-Ke Shih 	val = rtw89_read32(rtwdev, regs->ch_page_ctrl);
737e3ec7017SPing-Ke Shih 	prec_cfg->ch011_prec = u32_get_bits(val, B_AX_PREC_PAGE_CH011_MASK);
738e3ec7017SPing-Ke Shih 	prec_cfg->h2c_prec = u32_get_bits(val, B_AX_PREC_PAGE_CH12_MASK);
739e3ec7017SPing-Ke Shih 
740ab8a5671SPing-Ke Shih 	val = rtw89_read32(rtwdev, regs->pub_page_ctrl2);
741e3ec7017SPing-Ke Shih 	pub_cfg->pub_max = u32_get_bits(val, B_AX_PUBPG_ALL_MASK);
742e3ec7017SPing-Ke Shih 
743ab8a5671SPing-Ke Shih 	val = rtw89_read32(rtwdev, regs->wp_page_ctrl1);
744e3ec7017SPing-Ke Shih 	prec_cfg->wp_ch07_prec = u32_get_bits(val, B_AX_PREC_PAGE_WP_CH07_MASK);
745e3ec7017SPing-Ke Shih 	prec_cfg->wp_ch811_prec = u32_get_bits(val, B_AX_PREC_PAGE_WP_CH811_MASK);
746e3ec7017SPing-Ke Shih 
747ab8a5671SPing-Ke Shih 	val = rtw89_read32(rtwdev, regs->wp_page_ctrl2);
748e3ec7017SPing-Ke Shih 	pub_cfg->wp_thrd = u32_get_bits(val, B_AX_WP_THRD_MASK);
749e3ec7017SPing-Ke Shih 
750ab8a5671SPing-Ke Shih 	val = rtw89_read32(rtwdev, regs->pub_page_ctrl1);
751e3ec7017SPing-Ke Shih 	pub_cfg->grp0 = u32_get_bits(val, B_AX_PUBPG_G0_MASK);
752e3ec7017SPing-Ke Shih 	pub_cfg->grp1 = u32_get_bits(val, B_AX_PUBPG_G1_MASK);
753e3ec7017SPing-Ke Shih 
754e3ec7017SPing-Ke Shih 	ret = hfc_pub_info_chk(rtwdev);
755e3ec7017SPing-Ke Shih 	if (param->en && ret)
756e3ec7017SPing-Ke Shih 		return ret;
757e3ec7017SPing-Ke Shih 
758e3ec7017SPing-Ke Shih 	return 0;
759e3ec7017SPing-Ke Shih }
760e3ec7017SPing-Ke Shih 
761e3ec7017SPing-Ke Shih static void hfc_h2c_cfg(struct rtw89_dev *rtwdev)
762e3ec7017SPing-Ke Shih {
763ab8a5671SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
764ab8a5671SPing-Ke Shih 	const struct rtw89_page_regs *regs = chip->page_regs;
765e3ec7017SPing-Ke Shih 	struct rtw89_hfc_param *param = &rtwdev->mac.hfc_param;
766e3ec7017SPing-Ke Shih 	const struct rtw89_hfc_prec_cfg *prec_cfg = &param->prec_cfg;
767e3ec7017SPing-Ke Shih 	u32 val;
768e3ec7017SPing-Ke Shih 
769e3ec7017SPing-Ke Shih 	val = u32_encode_bits(prec_cfg->h2c_prec, B_AX_PREC_PAGE_CH12_MASK);
770ab8a5671SPing-Ke Shih 	rtw89_write32(rtwdev, regs->ch_page_ctrl, val);
771e3ec7017SPing-Ke Shih 
772ab8a5671SPing-Ke Shih 	rtw89_write32_mask(rtwdev, regs->hci_fc_ctrl,
773e3ec7017SPing-Ke Shih 			   B_AX_HCI_FC_CH12_FULL_COND_MASK,
774e3ec7017SPing-Ke Shih 			   prec_cfg->h2c_full_cond);
775e3ec7017SPing-Ke Shih }
776e3ec7017SPing-Ke Shih 
777e3ec7017SPing-Ke Shih static void hfc_mix_cfg(struct rtw89_dev *rtwdev)
778e3ec7017SPing-Ke Shih {
779ab8a5671SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
780ab8a5671SPing-Ke Shih 	const struct rtw89_page_regs *regs = chip->page_regs;
781e3ec7017SPing-Ke Shih 	struct rtw89_hfc_param *param = &rtwdev->mac.hfc_param;
782e3ec7017SPing-Ke Shih 	const struct rtw89_hfc_pub_cfg *pub_cfg = &param->pub_cfg;
783e3ec7017SPing-Ke Shih 	const struct rtw89_hfc_prec_cfg *prec_cfg = &param->prec_cfg;
784e3ec7017SPing-Ke Shih 	u32 val;
785e3ec7017SPing-Ke Shih 
786e3ec7017SPing-Ke Shih 	val = u32_encode_bits(prec_cfg->ch011_prec, B_AX_PREC_PAGE_CH011_MASK) |
787e3ec7017SPing-Ke Shih 	      u32_encode_bits(prec_cfg->h2c_prec, B_AX_PREC_PAGE_CH12_MASK);
788ab8a5671SPing-Ke Shih 	rtw89_write32(rtwdev, regs->ch_page_ctrl, val);
789e3ec7017SPing-Ke Shih 
790e3ec7017SPing-Ke Shih 	val = u32_encode_bits(pub_cfg->pub_max, B_AX_PUBPG_ALL_MASK);
791ab8a5671SPing-Ke Shih 	rtw89_write32(rtwdev, regs->pub_page_ctrl2, val);
792e3ec7017SPing-Ke Shih 
793e3ec7017SPing-Ke Shih 	val = u32_encode_bits(prec_cfg->wp_ch07_prec,
794e3ec7017SPing-Ke Shih 			      B_AX_PREC_PAGE_WP_CH07_MASK) |
795e3ec7017SPing-Ke Shih 	      u32_encode_bits(prec_cfg->wp_ch811_prec,
796e3ec7017SPing-Ke Shih 			      B_AX_PREC_PAGE_WP_CH811_MASK);
797ab8a5671SPing-Ke Shih 	rtw89_write32(rtwdev, regs->wp_page_ctrl1, val);
798e3ec7017SPing-Ke Shih 
799ab8a5671SPing-Ke Shih 	val = u32_replace_bits(rtw89_read32(rtwdev, regs->hci_fc_ctrl),
800e3ec7017SPing-Ke Shih 			       param->mode, B_AX_HCI_FC_MODE_MASK);
801e3ec7017SPing-Ke Shih 	val = u32_replace_bits(val, prec_cfg->ch011_full_cond,
802e3ec7017SPing-Ke Shih 			       B_AX_HCI_FC_WD_FULL_COND_MASK);
803e3ec7017SPing-Ke Shih 	val = u32_replace_bits(val, prec_cfg->h2c_full_cond,
804e3ec7017SPing-Ke Shih 			       B_AX_HCI_FC_CH12_FULL_COND_MASK);
805e3ec7017SPing-Ke Shih 	val = u32_replace_bits(val, prec_cfg->wp_ch07_full_cond,
806e3ec7017SPing-Ke Shih 			       B_AX_HCI_FC_WP_CH07_FULL_COND_MASK);
807e3ec7017SPing-Ke Shih 	val = u32_replace_bits(val, prec_cfg->wp_ch811_full_cond,
808e3ec7017SPing-Ke Shih 			       B_AX_HCI_FC_WP_CH811_FULL_COND_MASK);
809ab8a5671SPing-Ke Shih 	rtw89_write32(rtwdev, regs->hci_fc_ctrl, val);
810e3ec7017SPing-Ke Shih }
811e3ec7017SPing-Ke Shih 
812e3ec7017SPing-Ke Shih static void hfc_func_en(struct rtw89_dev *rtwdev, bool en, bool h2c_en)
813e3ec7017SPing-Ke Shih {
814ab8a5671SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
815ab8a5671SPing-Ke Shih 	const struct rtw89_page_regs *regs = chip->page_regs;
816e3ec7017SPing-Ke Shih 	struct rtw89_hfc_param *param = &rtwdev->mac.hfc_param;
817e3ec7017SPing-Ke Shih 	u32 val;
818e3ec7017SPing-Ke Shih 
819ab8a5671SPing-Ke Shih 	val = rtw89_read32(rtwdev, regs->hci_fc_ctrl);
820e3ec7017SPing-Ke Shih 	param->en = en;
821e3ec7017SPing-Ke Shih 	param->h2c_en = h2c_en;
822e3ec7017SPing-Ke Shih 	val = en ? (val | B_AX_HCI_FC_EN) : (val & ~B_AX_HCI_FC_EN);
823e3ec7017SPing-Ke Shih 	val = h2c_en ? (val | B_AX_HCI_FC_CH12_EN) :
824e3ec7017SPing-Ke Shih 			 (val & ~B_AX_HCI_FC_CH12_EN);
825ab8a5671SPing-Ke Shih 	rtw89_write32(rtwdev, regs->hci_fc_ctrl, val);
826e3ec7017SPing-Ke Shih }
827e3ec7017SPing-Ke Shih 
828e3ec7017SPing-Ke Shih static int hfc_init(struct rtw89_dev *rtwdev, bool reset, bool en, bool h2c_en)
829e3ec7017SPing-Ke Shih {
830e3ec7017SPing-Ke Shih 	u8 ch;
831e3ec7017SPing-Ke Shih 	u32 ret = 0;
832e3ec7017SPing-Ke Shih 
833e3ec7017SPing-Ke Shih 	if (reset)
834e3ec7017SPing-Ke Shih 		ret = hfc_reset_param(rtwdev);
835e3ec7017SPing-Ke Shih 	if (ret)
836e3ec7017SPing-Ke Shih 		return ret;
837e3ec7017SPing-Ke Shih 
838e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, RTW89_MAC_0, RTW89_DMAC_SEL);
839e3ec7017SPing-Ke Shih 	if (ret)
840e3ec7017SPing-Ke Shih 		return ret;
841e3ec7017SPing-Ke Shih 
842e3ec7017SPing-Ke Shih 	hfc_func_en(rtwdev, false, false);
843e3ec7017SPing-Ke Shih 
844e3ec7017SPing-Ke Shih 	if (!en && h2c_en) {
845e3ec7017SPing-Ke Shih 		hfc_h2c_cfg(rtwdev);
846e3ec7017SPing-Ke Shih 		hfc_func_en(rtwdev, en, h2c_en);
847e3ec7017SPing-Ke Shih 		return ret;
848e3ec7017SPing-Ke Shih 	}
849e3ec7017SPing-Ke Shih 
850e3ec7017SPing-Ke Shih 	for (ch = RTW89_DMA_ACH0; ch < RTW89_DMA_H2C; ch++) {
851e3ec7017SPing-Ke Shih 		ret = hfc_ch_ctrl(rtwdev, ch);
852e3ec7017SPing-Ke Shih 		if (ret)
853e3ec7017SPing-Ke Shih 			return ret;
854e3ec7017SPing-Ke Shih 	}
855e3ec7017SPing-Ke Shih 
856e3ec7017SPing-Ke Shih 	ret = hfc_pub_ctrl(rtwdev);
857e3ec7017SPing-Ke Shih 	if (ret)
858e3ec7017SPing-Ke Shih 		return ret;
859e3ec7017SPing-Ke Shih 
860e3ec7017SPing-Ke Shih 	hfc_mix_cfg(rtwdev);
861e3ec7017SPing-Ke Shih 	if (en || h2c_en) {
862e3ec7017SPing-Ke Shih 		hfc_func_en(rtwdev, en, h2c_en);
863e3ec7017SPing-Ke Shih 		udelay(10);
864e3ec7017SPing-Ke Shih 	}
865e3ec7017SPing-Ke Shih 	for (ch = RTW89_DMA_ACH0; ch < RTW89_DMA_H2C; ch++) {
866e3ec7017SPing-Ke Shih 		ret = hfc_upd_ch_info(rtwdev, ch);
867e3ec7017SPing-Ke Shih 		if (ret)
868e3ec7017SPing-Ke Shih 			return ret;
869e3ec7017SPing-Ke Shih 	}
870e3ec7017SPing-Ke Shih 	ret = hfc_upd_mix_info(rtwdev);
871e3ec7017SPing-Ke Shih 
872e3ec7017SPing-Ke Shih 	return ret;
873e3ec7017SPing-Ke Shih }
874e3ec7017SPing-Ke Shih 
875e3ec7017SPing-Ke Shih #define PWR_POLL_CNT	2000
876e3ec7017SPing-Ke Shih static int pwr_cmd_poll(struct rtw89_dev *rtwdev,
877e3ec7017SPing-Ke Shih 			const struct rtw89_pwr_cfg *cfg)
878e3ec7017SPing-Ke Shih {
879e3ec7017SPing-Ke Shih 	u8 val = 0;
880e3ec7017SPing-Ke Shih 	int ret;
881e3ec7017SPing-Ke Shih 	u32 addr = cfg->base == PWR_INTF_MSK_SDIO ?
882e3ec7017SPing-Ke Shih 		   cfg->addr | SDIO_LOCAL_BASE_ADDR : cfg->addr;
883e3ec7017SPing-Ke Shih 
884e3ec7017SPing-Ke Shih 	ret = read_poll_timeout(rtw89_read8, val, !((val ^ cfg->val) & cfg->msk),
885e3ec7017SPing-Ke Shih 				1000, 1000 * PWR_POLL_CNT, false, rtwdev, addr);
886e3ec7017SPing-Ke Shih 
887e3ec7017SPing-Ke Shih 	if (!ret)
888e3ec7017SPing-Ke Shih 		return 0;
889e3ec7017SPing-Ke Shih 
890e3ec7017SPing-Ke Shih 	rtw89_warn(rtwdev, "[ERR] Polling timeout\n");
891e3ec7017SPing-Ke Shih 	rtw89_warn(rtwdev, "[ERR] addr: %X, %X\n", addr, cfg->addr);
892e3ec7017SPing-Ke Shih 	rtw89_warn(rtwdev, "[ERR] val: %X, %X\n", val, cfg->val);
893e3ec7017SPing-Ke Shih 
894e3ec7017SPing-Ke Shih 	return -EBUSY;
895e3ec7017SPing-Ke Shih }
896e3ec7017SPing-Ke Shih 
897e3ec7017SPing-Ke Shih static int rtw89_mac_sub_pwr_seq(struct rtw89_dev *rtwdev, u8 cv_msk,
898e3ec7017SPing-Ke Shih 				 u8 intf_msk, const struct rtw89_pwr_cfg *cfg)
899e3ec7017SPing-Ke Shih {
900e3ec7017SPing-Ke Shih 	const struct rtw89_pwr_cfg *cur_cfg;
901e3ec7017SPing-Ke Shih 	u32 addr;
902e3ec7017SPing-Ke Shih 	u8 val;
903e3ec7017SPing-Ke Shih 
904e3ec7017SPing-Ke Shih 	for (cur_cfg = cfg; cur_cfg->cmd != PWR_CMD_END; cur_cfg++) {
905e3ec7017SPing-Ke Shih 		if (!(cur_cfg->intf_msk & intf_msk) ||
906e3ec7017SPing-Ke Shih 		    !(cur_cfg->cv_msk & cv_msk))
907e3ec7017SPing-Ke Shih 			continue;
908e3ec7017SPing-Ke Shih 
909e3ec7017SPing-Ke Shih 		switch (cur_cfg->cmd) {
910e3ec7017SPing-Ke Shih 		case PWR_CMD_WRITE:
911e3ec7017SPing-Ke Shih 			addr = cur_cfg->addr;
912e3ec7017SPing-Ke Shih 
913e3ec7017SPing-Ke Shih 			if (cur_cfg->base == PWR_BASE_SDIO)
914e3ec7017SPing-Ke Shih 				addr |= SDIO_LOCAL_BASE_ADDR;
915e3ec7017SPing-Ke Shih 
916e3ec7017SPing-Ke Shih 			val = rtw89_read8(rtwdev, addr);
917e3ec7017SPing-Ke Shih 			val &= ~(cur_cfg->msk);
918e3ec7017SPing-Ke Shih 			val |= (cur_cfg->val & cur_cfg->msk);
919e3ec7017SPing-Ke Shih 
920e3ec7017SPing-Ke Shih 			rtw89_write8(rtwdev, addr, val);
921e3ec7017SPing-Ke Shih 			break;
922e3ec7017SPing-Ke Shih 		case PWR_CMD_POLL:
923e3ec7017SPing-Ke Shih 			if (pwr_cmd_poll(rtwdev, cur_cfg))
924e3ec7017SPing-Ke Shih 				return -EBUSY;
925e3ec7017SPing-Ke Shih 			break;
926e3ec7017SPing-Ke Shih 		case PWR_CMD_DELAY:
927e3ec7017SPing-Ke Shih 			if (cur_cfg->val == PWR_DELAY_US)
928e3ec7017SPing-Ke Shih 				udelay(cur_cfg->addr);
929e3ec7017SPing-Ke Shih 			else
930e3ec7017SPing-Ke Shih 				fsleep(cur_cfg->addr * 1000);
931e3ec7017SPing-Ke Shih 			break;
932e3ec7017SPing-Ke Shih 		default:
933e3ec7017SPing-Ke Shih 			return -EINVAL;
934e3ec7017SPing-Ke Shih 		}
935e3ec7017SPing-Ke Shih 	}
936e3ec7017SPing-Ke Shih 
937e3ec7017SPing-Ke Shih 	return 0;
938e3ec7017SPing-Ke Shih }
939e3ec7017SPing-Ke Shih 
940e3ec7017SPing-Ke Shih static int rtw89_mac_pwr_seq(struct rtw89_dev *rtwdev,
941e3ec7017SPing-Ke Shih 			     const struct rtw89_pwr_cfg * const *cfg_seq)
942e3ec7017SPing-Ke Shih {
943e3ec7017SPing-Ke Shih 	int ret;
944e3ec7017SPing-Ke Shih 
945e3ec7017SPing-Ke Shih 	for (; *cfg_seq; cfg_seq++) {
946e3ec7017SPing-Ke Shih 		ret = rtw89_mac_sub_pwr_seq(rtwdev, BIT(rtwdev->hal.cv),
947e3ec7017SPing-Ke Shih 					    PWR_INTF_MSK_PCIE, *cfg_seq);
948e3ec7017SPing-Ke Shih 		if (ret)
949e3ec7017SPing-Ke Shih 			return -EBUSY;
950e3ec7017SPing-Ke Shih 	}
951e3ec7017SPing-Ke Shih 
952e3ec7017SPing-Ke Shih 	return 0;
953e3ec7017SPing-Ke Shih }
954e3ec7017SPing-Ke Shih 
955e3ec7017SPing-Ke Shih static enum rtw89_rpwm_req_pwr_state
956e3ec7017SPing-Ke Shih rtw89_mac_get_req_pwr_state(struct rtw89_dev *rtwdev)
957e3ec7017SPing-Ke Shih {
958e3ec7017SPing-Ke Shih 	enum rtw89_rpwm_req_pwr_state state;
959e3ec7017SPing-Ke Shih 
960e3ec7017SPing-Ke Shih 	switch (rtwdev->ps_mode) {
961e3ec7017SPing-Ke Shih 	case RTW89_PS_MODE_RFOFF:
962e3ec7017SPing-Ke Shih 		state = RTW89_MAC_RPWM_REQ_PWR_STATE_BAND0_RFOFF;
963e3ec7017SPing-Ke Shih 		break;
964e3ec7017SPing-Ke Shih 	case RTW89_PS_MODE_CLK_GATED:
965e3ec7017SPing-Ke Shih 		state = RTW89_MAC_RPWM_REQ_PWR_STATE_CLK_GATED;
966e3ec7017SPing-Ke Shih 		break;
967e3ec7017SPing-Ke Shih 	case RTW89_PS_MODE_PWR_GATED:
968e3ec7017SPing-Ke Shih 		state = RTW89_MAC_RPWM_REQ_PWR_STATE_PWR_GATED;
969e3ec7017SPing-Ke Shih 		break;
970e3ec7017SPing-Ke Shih 	default:
971e3ec7017SPing-Ke Shih 		state = RTW89_MAC_RPWM_REQ_PWR_STATE_ACTIVE;
972e3ec7017SPing-Ke Shih 		break;
973e3ec7017SPing-Ke Shih 	}
974e3ec7017SPing-Ke Shih 	return state;
975e3ec7017SPing-Ke Shih }
976e3ec7017SPing-Ke Shih 
977e3ec7017SPing-Ke Shih static void rtw89_mac_send_rpwm(struct rtw89_dev *rtwdev,
9787bfd05ffSChin-Yen Lee 				enum rtw89_rpwm_req_pwr_state req_pwr_state,
9797bfd05ffSChin-Yen Lee 				bool notify_wake)
980e3ec7017SPing-Ke Shih {
981e3ec7017SPing-Ke Shih 	u16 request;
982e3ec7017SPing-Ke Shih 
9837bfd05ffSChin-Yen Lee 	spin_lock_bh(&rtwdev->rpwm_lock);
9847bfd05ffSChin-Yen Lee 
985e3ec7017SPing-Ke Shih 	request = rtw89_read16(rtwdev, R_AX_RPWM);
986e3ec7017SPing-Ke Shih 	request ^= request | PS_RPWM_TOGGLE;
9877bfd05ffSChin-Yen Lee 	request |= req_pwr_state;
988e3ec7017SPing-Ke Shih 
9897bfd05ffSChin-Yen Lee 	if (notify_wake) {
9907bfd05ffSChin-Yen Lee 		request |= PS_RPWM_NOTIFY_WAKE;
9917bfd05ffSChin-Yen Lee 	} else {
992e3ec7017SPing-Ke Shih 		rtwdev->mac.rpwm_seq_num = (rtwdev->mac.rpwm_seq_num + 1) &
993e3ec7017SPing-Ke Shih 					    RPWM_SEQ_NUM_MAX;
9947bfd05ffSChin-Yen Lee 		request |= FIELD_PREP(PS_RPWM_SEQ_NUM,
9957bfd05ffSChin-Yen Lee 				      rtwdev->mac.rpwm_seq_num);
996e3ec7017SPing-Ke Shih 
997e3ec7017SPing-Ke Shih 		if (req_pwr_state < RTW89_MAC_RPWM_REQ_PWR_STATE_CLK_GATED)
998e3ec7017SPing-Ke Shih 			request |= PS_RPWM_ACK;
9997bfd05ffSChin-Yen Lee 	}
1000e3ec7017SPing-Ke Shih 	rtw89_write16(rtwdev, rtwdev->hci.rpwm_addr, request);
10017bfd05ffSChin-Yen Lee 
10027bfd05ffSChin-Yen Lee 	spin_unlock_bh(&rtwdev->rpwm_lock);
1003e3ec7017SPing-Ke Shih }
1004e3ec7017SPing-Ke Shih 
1005e3ec7017SPing-Ke Shih static int rtw89_mac_check_cpwm_state(struct rtw89_dev *rtwdev,
1006e3ec7017SPing-Ke Shih 				      enum rtw89_rpwm_req_pwr_state req_pwr_state)
1007e3ec7017SPing-Ke Shih {
1008e3ec7017SPing-Ke Shih 	bool request_deep_mode;
1009e3ec7017SPing-Ke Shih 	bool in_deep_mode;
1010e3ec7017SPing-Ke Shih 	u8 rpwm_req_num;
1011e3ec7017SPing-Ke Shih 	u8 cpwm_rsp_seq;
1012e3ec7017SPing-Ke Shih 	u8 cpwm_seq;
1013e3ec7017SPing-Ke Shih 	u8 cpwm_status;
1014e3ec7017SPing-Ke Shih 
1015e3ec7017SPing-Ke Shih 	if (req_pwr_state >= RTW89_MAC_RPWM_REQ_PWR_STATE_CLK_GATED)
1016e3ec7017SPing-Ke Shih 		request_deep_mode = true;
1017e3ec7017SPing-Ke Shih 	else
1018e3ec7017SPing-Ke Shih 		request_deep_mode = false;
1019e3ec7017SPing-Ke Shih 
1020e3ec7017SPing-Ke Shih 	if (rtw89_read32_mask(rtwdev, R_AX_LDM, B_AX_EN_32K))
1021e3ec7017SPing-Ke Shih 		in_deep_mode = true;
1022e3ec7017SPing-Ke Shih 	else
1023e3ec7017SPing-Ke Shih 		in_deep_mode = false;
1024e3ec7017SPing-Ke Shih 
1025e3ec7017SPing-Ke Shih 	if (request_deep_mode != in_deep_mode)
1026e3ec7017SPing-Ke Shih 		return -EPERM;
1027e3ec7017SPing-Ke Shih 
1028e3ec7017SPing-Ke Shih 	if (request_deep_mode)
1029e3ec7017SPing-Ke Shih 		return 0;
1030e3ec7017SPing-Ke Shih 
1031e3ec7017SPing-Ke Shih 	rpwm_req_num = rtwdev->mac.rpwm_seq_num;
1032e1757e80SPing-Ke Shih 	cpwm_rsp_seq = rtw89_read16_mask(rtwdev, rtwdev->hci.cpwm_addr,
1033e3ec7017SPing-Ke Shih 					 PS_CPWM_RSP_SEQ_NUM);
1034e3ec7017SPing-Ke Shih 
1035e3ec7017SPing-Ke Shih 	if (rpwm_req_num != cpwm_rsp_seq)
1036e3ec7017SPing-Ke Shih 		return -EPERM;
1037e3ec7017SPing-Ke Shih 
1038e3ec7017SPing-Ke Shih 	rtwdev->mac.cpwm_seq_num = (rtwdev->mac.cpwm_seq_num + 1) &
1039e3ec7017SPing-Ke Shih 				    CPWM_SEQ_NUM_MAX;
1040e3ec7017SPing-Ke Shih 
1041e1757e80SPing-Ke Shih 	cpwm_seq = rtw89_read16_mask(rtwdev, rtwdev->hci.cpwm_addr, PS_CPWM_SEQ_NUM);
1042e3ec7017SPing-Ke Shih 	if (cpwm_seq != rtwdev->mac.cpwm_seq_num)
1043e3ec7017SPing-Ke Shih 		return -EPERM;
1044e3ec7017SPing-Ke Shih 
1045e1757e80SPing-Ke Shih 	cpwm_status = rtw89_read16_mask(rtwdev, rtwdev->hci.cpwm_addr, PS_CPWM_STATE);
1046e3ec7017SPing-Ke Shih 	if (cpwm_status != req_pwr_state)
1047e3ec7017SPing-Ke Shih 		return -EPERM;
1048e3ec7017SPing-Ke Shih 
1049e3ec7017SPing-Ke Shih 	return 0;
1050e3ec7017SPing-Ke Shih }
1051e3ec7017SPing-Ke Shih 
1052e3ec7017SPing-Ke Shih void rtw89_mac_power_mode_change(struct rtw89_dev *rtwdev, bool enter)
1053e3ec7017SPing-Ke Shih {
1054e3ec7017SPing-Ke Shih 	enum rtw89_rpwm_req_pwr_state state;
105539a76521SPing-Ke Shih 	unsigned long delay = enter ? 10 : 150;
1056e3ec7017SPing-Ke Shih 	int ret;
105748c0e347SChin-Yen Lee 	int i;
1058e3ec7017SPing-Ke Shih 
1059e3ec7017SPing-Ke Shih 	if (enter)
1060e3ec7017SPing-Ke Shih 		state = rtw89_mac_get_req_pwr_state(rtwdev);
1061e3ec7017SPing-Ke Shih 	else
1062e3ec7017SPing-Ke Shih 		state = RTW89_MAC_RPWM_REQ_PWR_STATE_ACTIVE;
1063e3ec7017SPing-Ke Shih 
106448c0e347SChin-Yen Lee 	for (i = 0; i < RPWM_TRY_CNT; i++) {
10657bfd05ffSChin-Yen Lee 		rtw89_mac_send_rpwm(rtwdev, state, false);
106648c0e347SChin-Yen Lee 		ret = read_poll_timeout_atomic(rtw89_mac_check_cpwm_state, ret,
106748c0e347SChin-Yen Lee 					       !ret, delay, 15000, false,
106848c0e347SChin-Yen Lee 					       rtwdev, state);
106948c0e347SChin-Yen Lee 		if (!ret)
107048c0e347SChin-Yen Lee 			break;
107148c0e347SChin-Yen Lee 
107248c0e347SChin-Yen Lee 		if (i == RPWM_TRY_CNT - 1)
1073e3ec7017SPing-Ke Shih 			rtw89_err(rtwdev, "firmware failed to ack for %s ps mode\n",
1074e3ec7017SPing-Ke Shih 				  enter ? "entering" : "leaving");
107548c0e347SChin-Yen Lee 		else
107648c0e347SChin-Yen Lee 			rtw89_debug(rtwdev, RTW89_DBG_UNEXP,
107748c0e347SChin-Yen Lee 				    "%d time firmware failed to ack for %s ps mode\n",
107848c0e347SChin-Yen Lee 				    i + 1, enter ? "entering" : "leaving");
107948c0e347SChin-Yen Lee 	}
1080e3ec7017SPing-Ke Shih }
1081e3ec7017SPing-Ke Shih 
10827bfd05ffSChin-Yen Lee void rtw89_mac_notify_wake(struct rtw89_dev *rtwdev)
10837bfd05ffSChin-Yen Lee {
10847bfd05ffSChin-Yen Lee 	enum rtw89_rpwm_req_pwr_state state;
10857bfd05ffSChin-Yen Lee 
10867bfd05ffSChin-Yen Lee 	state = rtw89_mac_get_req_pwr_state(rtwdev);
10877bfd05ffSChin-Yen Lee 	rtw89_mac_send_rpwm(rtwdev, state, true);
10887bfd05ffSChin-Yen Lee }
10897bfd05ffSChin-Yen Lee 
1090e3ec7017SPing-Ke Shih static int rtw89_mac_power_switch(struct rtw89_dev *rtwdev, bool on)
1091e3ec7017SPing-Ke Shih {
1092e3ec7017SPing-Ke Shih #define PWR_ACT 1
1093e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
1094e3ec7017SPing-Ke Shih 	const struct rtw89_pwr_cfg * const *cfg_seq;
10952a7e54dbSPing-Ke Shih 	int (*cfg_func)(struct rtw89_dev *rtwdev);
1096e3ec7017SPing-Ke Shih 	int ret;
1097e3ec7017SPing-Ke Shih 	u8 val;
1098e3ec7017SPing-Ke Shih 
10992a7e54dbSPing-Ke Shih 	if (on) {
1100e3ec7017SPing-Ke Shih 		cfg_seq = chip->pwr_on_seq;
11012a7e54dbSPing-Ke Shih 		cfg_func = chip->ops->pwr_on_func;
11022a7e54dbSPing-Ke Shih 	} else {
1103e3ec7017SPing-Ke Shih 		cfg_seq = chip->pwr_off_seq;
11042a7e54dbSPing-Ke Shih 		cfg_func = chip->ops->pwr_off_func;
11052a7e54dbSPing-Ke Shih 	}
1106e3ec7017SPing-Ke Shih 
1107e3ec7017SPing-Ke Shih 	if (test_bit(RTW89_FLAG_FW_RDY, rtwdev->flags))
1108e3ec7017SPing-Ke Shih 		__rtw89_leave_ps_mode(rtwdev);
1109e3ec7017SPing-Ke Shih 
1110e3ec7017SPing-Ke Shih 	val = rtw89_read32_mask(rtwdev, R_AX_IC_PWR_STATE, B_AX_WLMAC_PWR_STE_MASK);
1111e3ec7017SPing-Ke Shih 	if (on && val == PWR_ACT) {
1112e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "MAC has already powered on\n");
1113e3ec7017SPing-Ke Shih 		return -EBUSY;
1114e3ec7017SPing-Ke Shih 	}
1115e3ec7017SPing-Ke Shih 
11162a7e54dbSPing-Ke Shih 	ret = cfg_func ? cfg_func(rtwdev) : rtw89_mac_pwr_seq(rtwdev, cfg_seq);
1117e3ec7017SPing-Ke Shih 	if (ret)
1118e3ec7017SPing-Ke Shih 		return ret;
1119e3ec7017SPing-Ke Shih 
1120e3ec7017SPing-Ke Shih 	if (on) {
1121e3ec7017SPing-Ke Shih 		set_bit(RTW89_FLAG_POWERON, rtwdev->flags);
1122e3ec7017SPing-Ke Shih 		rtw89_write8(rtwdev, R_AX_SCOREBOARD + 3, MAC_AX_NOTIFY_TP_MAJOR);
1123e3ec7017SPing-Ke Shih 	} else {
1124e3ec7017SPing-Ke Shih 		clear_bit(RTW89_FLAG_POWERON, rtwdev->flags);
1125e3ec7017SPing-Ke Shih 		clear_bit(RTW89_FLAG_FW_RDY, rtwdev->flags);
1126e3ec7017SPing-Ke Shih 		rtw89_write8(rtwdev, R_AX_SCOREBOARD + 3, MAC_AX_NOTIFY_PWR_MAJOR);
1127967439c7SZong-Zhe Yang 		rtw89_set_entity_state(rtwdev, false);
1128e3ec7017SPing-Ke Shih 	}
1129e3ec7017SPing-Ke Shih 
1130e3ec7017SPing-Ke Shih 	return 0;
1131e3ec7017SPing-Ke Shih #undef PWR_ACT
1132e3ec7017SPing-Ke Shih }
1133e3ec7017SPing-Ke Shih 
1134e3ec7017SPing-Ke Shih void rtw89_mac_pwr_off(struct rtw89_dev *rtwdev)
1135e3ec7017SPing-Ke Shih {
1136e3ec7017SPing-Ke Shih 	rtw89_mac_power_switch(rtwdev, false);
1137e3ec7017SPing-Ke Shih }
1138e3ec7017SPing-Ke Shih 
1139e3ec7017SPing-Ke Shih static int cmac_func_en(struct rtw89_dev *rtwdev, u8 mac_idx, bool en)
1140e3ec7017SPing-Ke Shih {
1141e3ec7017SPing-Ke Shih 	u32 func_en = 0;
1142e3ec7017SPing-Ke Shih 	u32 ck_en = 0;
1143e3ec7017SPing-Ke Shih 	u32 c1pc_en = 0;
1144e3ec7017SPing-Ke Shih 	u32 addrl_func_en[] = {R_AX_CMAC_FUNC_EN, R_AX_CMAC_FUNC_EN_C1};
1145e3ec7017SPing-Ke Shih 	u32 addrl_ck_en[] = {R_AX_CK_EN, R_AX_CK_EN_C1};
1146e3ec7017SPing-Ke Shih 
1147e3ec7017SPing-Ke Shih 	func_en = B_AX_CMAC_EN | B_AX_CMAC_TXEN | B_AX_CMAC_RXEN |
1148e3ec7017SPing-Ke Shih 			B_AX_PHYINTF_EN | B_AX_CMAC_DMA_EN | B_AX_PTCLTOP_EN |
11495cb5562dSPing-Ke Shih 			B_AX_SCHEDULER_EN | B_AX_TMAC_EN | B_AX_RMAC_EN |
11505cb5562dSPing-Ke Shih 			B_AX_CMAC_CRPRT;
1151e3ec7017SPing-Ke Shih 	ck_en = B_AX_CMAC_CKEN | B_AX_PHYINTF_CKEN | B_AX_CMAC_DMA_CKEN |
1152e3ec7017SPing-Ke Shih 		      B_AX_PTCLTOP_CKEN | B_AX_SCHEDULER_CKEN | B_AX_TMAC_CKEN |
1153e3ec7017SPing-Ke Shih 		      B_AX_RMAC_CKEN;
1154e3ec7017SPing-Ke Shih 	c1pc_en = B_AX_R_SYM_WLCMAC1_PC_EN |
1155e3ec7017SPing-Ke Shih 			B_AX_R_SYM_WLCMAC1_P1_PC_EN |
1156e3ec7017SPing-Ke Shih 			B_AX_R_SYM_WLCMAC1_P2_PC_EN |
1157e3ec7017SPing-Ke Shih 			B_AX_R_SYM_WLCMAC1_P3_PC_EN |
1158e3ec7017SPing-Ke Shih 			B_AX_R_SYM_WLCMAC1_P4_PC_EN;
1159e3ec7017SPing-Ke Shih 
1160e3ec7017SPing-Ke Shih 	if (en) {
1161e3ec7017SPing-Ke Shih 		if (mac_idx == RTW89_MAC_1) {
1162e3ec7017SPing-Ke Shih 			rtw89_write32_set(rtwdev, R_AX_AFE_CTRL1, c1pc_en);
1163e3ec7017SPing-Ke Shih 			rtw89_write32_clr(rtwdev, R_AX_SYS_ISO_CTRL_EXTEND,
1164e3ec7017SPing-Ke Shih 					  B_AX_R_SYM_ISO_CMAC12PP);
1165e3ec7017SPing-Ke Shih 			rtw89_write32_set(rtwdev, R_AX_SYS_ISO_CTRL_EXTEND,
1166e3ec7017SPing-Ke Shih 					  B_AX_CMAC1_FEN);
1167e3ec7017SPing-Ke Shih 		}
1168e3ec7017SPing-Ke Shih 		rtw89_write32_set(rtwdev, addrl_ck_en[mac_idx], ck_en);
1169e3ec7017SPing-Ke Shih 		rtw89_write32_set(rtwdev, addrl_func_en[mac_idx], func_en);
1170e3ec7017SPing-Ke Shih 	} else {
1171e3ec7017SPing-Ke Shih 		rtw89_write32_clr(rtwdev, addrl_func_en[mac_idx], func_en);
1172e3ec7017SPing-Ke Shih 		rtw89_write32_clr(rtwdev, addrl_ck_en[mac_idx], ck_en);
1173e3ec7017SPing-Ke Shih 		if (mac_idx == RTW89_MAC_1) {
1174e3ec7017SPing-Ke Shih 			rtw89_write32_clr(rtwdev, R_AX_SYS_ISO_CTRL_EXTEND,
1175e3ec7017SPing-Ke Shih 					  B_AX_CMAC1_FEN);
1176e3ec7017SPing-Ke Shih 			rtw89_write32_set(rtwdev, R_AX_SYS_ISO_CTRL_EXTEND,
1177e3ec7017SPing-Ke Shih 					  B_AX_R_SYM_ISO_CMAC12PP);
1178e3ec7017SPing-Ke Shih 			rtw89_write32_clr(rtwdev, R_AX_AFE_CTRL1, c1pc_en);
1179e3ec7017SPing-Ke Shih 		}
1180e3ec7017SPing-Ke Shih 	}
1181e3ec7017SPing-Ke Shih 
1182e3ec7017SPing-Ke Shih 	return 0;
1183e3ec7017SPing-Ke Shih }
1184e3ec7017SPing-Ke Shih 
1185e3ec7017SPing-Ke Shih static int dmac_func_en(struct rtw89_dev *rtwdev)
1186e3ec7017SPing-Ke Shih {
1187828a4396SChia-Yuan Li 	enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
1188e3ec7017SPing-Ke Shih 	u32 val32;
1189e3ec7017SPing-Ke Shih 
1190828a4396SChia-Yuan Li 	if (chip_id == RTL8852C)
1191828a4396SChia-Yuan Li 		val32 = (B_AX_MAC_FUNC_EN | B_AX_DMAC_FUNC_EN |
1192828a4396SChia-Yuan Li 			 B_AX_MAC_SEC_EN | B_AX_DISPATCHER_EN |
1193828a4396SChia-Yuan Li 			 B_AX_DLE_CPUIO_EN | B_AX_PKT_IN_EN |
1194828a4396SChia-Yuan Li 			 B_AX_DMAC_TBL_EN | B_AX_PKT_BUF_EN |
1195828a4396SChia-Yuan Li 			 B_AX_STA_SCH_EN | B_AX_TXPKT_CTRL_EN |
1196828a4396SChia-Yuan Li 			 B_AX_WD_RLS_EN | B_AX_MPDU_PROC_EN |
1197828a4396SChia-Yuan Li 			 B_AX_DMAC_CRPRT | B_AX_H_AXIDMA_EN);
1198828a4396SChia-Yuan Li 	else
1199828a4396SChia-Yuan Li 		val32 = (B_AX_MAC_FUNC_EN | B_AX_DMAC_FUNC_EN |
1200828a4396SChia-Yuan Li 			 B_AX_MAC_SEC_EN | B_AX_DISPATCHER_EN |
1201828a4396SChia-Yuan Li 			 B_AX_DLE_CPUIO_EN | B_AX_PKT_IN_EN |
1202828a4396SChia-Yuan Li 			 B_AX_DMAC_TBL_EN | B_AX_PKT_BUF_EN |
1203828a4396SChia-Yuan Li 			 B_AX_STA_SCH_EN | B_AX_TXPKT_CTRL_EN |
1204828a4396SChia-Yuan Li 			 B_AX_WD_RLS_EN | B_AX_MPDU_PROC_EN |
1205828a4396SChia-Yuan Li 			 B_AX_DMAC_CRPRT);
1206e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_DMAC_FUNC_EN, val32);
1207e3ec7017SPing-Ke Shih 
1208e3ec7017SPing-Ke Shih 	val32 = (B_AX_MAC_SEC_CLK_EN | B_AX_DISPATCHER_CLK_EN |
1209e3ec7017SPing-Ke Shih 		 B_AX_DLE_CPUIO_CLK_EN | B_AX_PKT_IN_CLK_EN |
1210e3ec7017SPing-Ke Shih 		 B_AX_STA_SCH_CLK_EN | B_AX_TXPKT_CTRL_CLK_EN |
1211828a4396SChia-Yuan Li 		 B_AX_WD_RLS_CLK_EN | B_AX_BBRPT_CLK_EN);
1212e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_DMAC_CLK_EN, val32);
1213e3ec7017SPing-Ke Shih 
121443863efeSChangcheng Deng 	return 0;
1215e3ec7017SPing-Ke Shih }
1216e3ec7017SPing-Ke Shih 
1217e3ec7017SPing-Ke Shih static int chip_func_en(struct rtw89_dev *rtwdev)
1218e3ec7017SPing-Ke Shih {
1219828a4396SChia-Yuan Li 	enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
1220828a4396SChia-Yuan Li 
1221828a4396SChia-Yuan Li 	if (chip_id == RTL8852A)
1222828a4396SChia-Yuan Li 		rtw89_write32_set(rtwdev, R_AX_SPSLDO_ON_CTRL0,
1223828a4396SChia-Yuan Li 				  B_AX_OCP_L1_MASK);
1224e3ec7017SPing-Ke Shih 
1225e3ec7017SPing-Ke Shih 	return 0;
1226e3ec7017SPing-Ke Shih }
1227e3ec7017SPing-Ke Shih 
1228e3ec7017SPing-Ke Shih static int rtw89_mac_sys_init(struct rtw89_dev *rtwdev)
1229e3ec7017SPing-Ke Shih {
1230e3ec7017SPing-Ke Shih 	int ret;
1231e3ec7017SPing-Ke Shih 
1232e3ec7017SPing-Ke Shih 	ret = dmac_func_en(rtwdev);
1233e3ec7017SPing-Ke Shih 	if (ret)
1234e3ec7017SPing-Ke Shih 		return ret;
1235e3ec7017SPing-Ke Shih 
1236e3ec7017SPing-Ke Shih 	ret = cmac_func_en(rtwdev, 0, true);
1237e3ec7017SPing-Ke Shih 	if (ret)
1238e3ec7017SPing-Ke Shih 		return ret;
1239e3ec7017SPing-Ke Shih 
1240e3ec7017SPing-Ke Shih 	ret = chip_func_en(rtwdev);
1241e3ec7017SPing-Ke Shih 	if (ret)
1242e3ec7017SPing-Ke Shih 		return ret;
1243e3ec7017SPing-Ke Shih 
1244e3ec7017SPing-Ke Shih 	return ret;
1245e3ec7017SPing-Ke Shih }
1246e3ec7017SPing-Ke Shih 
124730645118SPing-Ke Shih const struct rtw89_mac_size_set rtw89_mac_size = {
124830645118SPing-Ke Shih 	.hfc_preccfg_pcie = {2, 40, 0, 0, 1, 0, 0, 0},
1249e3ec7017SPing-Ke Shih 	/* PCIE 64 */
125030645118SPing-Ke Shih 	.wde_size0 = {RTW89_WDE_PG_64, 4095, 1,},
1251e3ec7017SPing-Ke Shih 	/* DLFW */
125230645118SPing-Ke Shih 	.wde_size4 = {RTW89_WDE_PG_64, 0, 4096,},
125379d099e0SPing-Ke Shih 	/* 8852C DLFW */
125430645118SPing-Ke Shih 	.wde_size18 = {RTW89_WDE_PG_64, 0, 2048,},
125579d099e0SPing-Ke Shih 	/* 8852C PCIE SCC */
125630645118SPing-Ke Shih 	.wde_size19 = {RTW89_WDE_PG_64, 3328, 0,},
1257e3ec7017SPing-Ke Shih 	/* PCIE */
125830645118SPing-Ke Shih 	.ple_size0 = {RTW89_PLE_PG_128, 1520, 16,},
1259e3ec7017SPing-Ke Shih 	/* DLFW */
126030645118SPing-Ke Shih 	.ple_size4 = {RTW89_PLE_PG_128, 64, 1472,},
126179d099e0SPing-Ke Shih 	/* 8852C DLFW */
126230645118SPing-Ke Shih 	.ple_size18 = {RTW89_PLE_PG_128, 2544, 16,},
126379d099e0SPing-Ke Shih 	/* 8852C PCIE SCC */
126430645118SPing-Ke Shih 	.ple_size19 = {RTW89_PLE_PG_128, 1904, 16,},
1265e3ec7017SPing-Ke Shih 	/* PCIE 64 */
126630645118SPing-Ke Shih 	.wde_qt0 = {3792, 196, 0, 107,},
1267e3ec7017SPing-Ke Shih 	/* DLFW */
126830645118SPing-Ke Shih 	.wde_qt4 = {0, 0, 0, 0,},
126979d099e0SPing-Ke Shih 	/* 8852C DLFW */
127030645118SPing-Ke Shih 	.wde_qt17 = {0, 0, 0,  0,},
127179d099e0SPing-Ke Shih 	/* 8852C PCIE SCC */
127230645118SPing-Ke Shih 	.wde_qt18 = {3228, 60, 0, 40,},
1273e3ec7017SPing-Ke Shih 	/* PCIE SCC */
127430645118SPing-Ke Shih 	.ple_qt4 = {264, 0, 16, 20, 26, 13, 356, 0, 32, 40, 8,},
1275e3ec7017SPing-Ke Shih 	/* PCIE SCC */
127630645118SPing-Ke Shih 	.ple_qt5 = {264, 0, 32, 20, 64, 13, 1101, 0, 64, 128, 120,},
1277e3ec7017SPing-Ke Shih 	/* DLFW */
127830645118SPing-Ke Shih 	.ple_qt13 = {0, 0, 16, 48, 0, 0, 0, 0, 0, 0, 0,},
127979d099e0SPing-Ke Shih 	/* DLFW 52C */
128030645118SPing-Ke Shih 	.ple_qt44 = {0, 0, 16, 256, 0, 0, 0, 0, 0, 0, 0, 0,},
128179d099e0SPing-Ke Shih 	/* DLFW 52C */
128230645118SPing-Ke Shih 	.ple_qt45 = {0, 0, 32, 256, 0, 0, 0, 0, 0, 0, 0, 0,},
128379d099e0SPing-Ke Shih 	/* 8852C PCIE SCC */
128430645118SPing-Ke Shih 	.ple_qt46 = {525, 0, 16, 20, 13, 13, 178, 0, 32, 62, 8, 16,},
128579d099e0SPing-Ke Shih 	/* 8852C PCIE SCC */
128630645118SPing-Ke Shih 	.ple_qt47 = {525, 0, 32, 20, 1034, 13, 1199, 0, 1053, 62, 160, 1037,},
128779d099e0SPing-Ke Shih };
128830645118SPing-Ke Shih EXPORT_SYMBOL(rtw89_mac_size);
128979d099e0SPing-Ke Shih 
1290e3ec7017SPing-Ke Shih static const struct rtw89_dle_mem *get_dle_mem_cfg(struct rtw89_dev *rtwdev,
1291e3ec7017SPing-Ke Shih 						   enum rtw89_qta_mode mode)
1292e3ec7017SPing-Ke Shih {
1293e3ec7017SPing-Ke Shih 	struct rtw89_mac_info *mac = &rtwdev->mac;
1294e3ec7017SPing-Ke Shih 	const struct rtw89_dle_mem *cfg;
1295e3ec7017SPing-Ke Shih 
1296e3ec7017SPing-Ke Shih 	cfg = &rtwdev->chip->dle_mem[mode];
1297e3ec7017SPing-Ke Shih 	if (!cfg)
1298e3ec7017SPing-Ke Shih 		return NULL;
1299e3ec7017SPing-Ke Shih 
1300e3ec7017SPing-Ke Shih 	if (cfg->mode != mode) {
1301e3ec7017SPing-Ke Shih 		rtw89_warn(rtwdev, "qta mode unmatch!\n");
1302e3ec7017SPing-Ke Shih 		return NULL;
1303e3ec7017SPing-Ke Shih 	}
1304e3ec7017SPing-Ke Shih 
1305e3ec7017SPing-Ke Shih 	mac->dle_info.wde_pg_size = cfg->wde_size->pge_size;
1306e3ec7017SPing-Ke Shih 	mac->dle_info.ple_pg_size = cfg->ple_size->pge_size;
1307e3ec7017SPing-Ke Shih 	mac->dle_info.qta_mode = mode;
1308e3ec7017SPing-Ke Shih 	mac->dle_info.c0_rx_qta = cfg->ple_min_qt->cma0_dma;
1309e3ec7017SPing-Ke Shih 	mac->dle_info.c1_rx_qta = cfg->ple_min_qt->cma1_dma;
1310e3ec7017SPing-Ke Shih 
1311e3ec7017SPing-Ke Shih 	return cfg;
1312e3ec7017SPing-Ke Shih }
1313e3ec7017SPing-Ke Shih 
1314e3ec7017SPing-Ke Shih static inline u32 dle_used_size(const struct rtw89_dle_size *wde,
1315e3ec7017SPing-Ke Shih 				const struct rtw89_dle_size *ple)
1316e3ec7017SPing-Ke Shih {
1317e3ec7017SPing-Ke Shih 	return wde->pge_size * (wde->lnk_pge_num + wde->unlnk_pge_num) +
1318e3ec7017SPing-Ke Shih 	       ple->pge_size * (ple->lnk_pge_num + ple->unlnk_pge_num);
1319e3ec7017SPing-Ke Shih }
1320e3ec7017SPing-Ke Shih 
1321e3ec7017SPing-Ke Shih static void dle_func_en(struct rtw89_dev *rtwdev, bool enable)
1322e3ec7017SPing-Ke Shih {
1323e3ec7017SPing-Ke Shih 	if (enable)
1324e3ec7017SPing-Ke Shih 		rtw89_write32_set(rtwdev, R_AX_DMAC_FUNC_EN,
1325e3ec7017SPing-Ke Shih 				  B_AX_DLE_WDE_EN | B_AX_DLE_PLE_EN);
1326e3ec7017SPing-Ke Shih 	else
1327e3ec7017SPing-Ke Shih 		rtw89_write32_clr(rtwdev, R_AX_DMAC_FUNC_EN,
1328e3ec7017SPing-Ke Shih 				  B_AX_DLE_WDE_EN | B_AX_DLE_PLE_EN);
1329e3ec7017SPing-Ke Shih }
1330e3ec7017SPing-Ke Shih 
1331e3ec7017SPing-Ke Shih static void dle_clk_en(struct rtw89_dev *rtwdev, bool enable)
1332e3ec7017SPing-Ke Shih {
1333e3ec7017SPing-Ke Shih 	if (enable)
1334e3ec7017SPing-Ke Shih 		rtw89_write32_set(rtwdev, R_AX_DMAC_CLK_EN,
1335e3ec7017SPing-Ke Shih 				  B_AX_DLE_WDE_CLK_EN | B_AX_DLE_PLE_CLK_EN);
1336e3ec7017SPing-Ke Shih 	else
1337e3ec7017SPing-Ke Shih 		rtw89_write32_clr(rtwdev, R_AX_DMAC_CLK_EN,
1338e3ec7017SPing-Ke Shih 				  B_AX_DLE_WDE_CLK_EN | B_AX_DLE_PLE_CLK_EN);
1339e3ec7017SPing-Ke Shih }
1340e3ec7017SPing-Ke Shih 
1341e3ec7017SPing-Ke Shih static int dle_mix_cfg(struct rtw89_dev *rtwdev, const struct rtw89_dle_mem *cfg)
1342e3ec7017SPing-Ke Shih {
1343e3ec7017SPing-Ke Shih 	const struct rtw89_dle_size *size_cfg;
1344e3ec7017SPing-Ke Shih 	u32 val;
1345e3ec7017SPing-Ke Shih 	u8 bound = 0;
1346e3ec7017SPing-Ke Shih 
1347e3ec7017SPing-Ke Shih 	val = rtw89_read32(rtwdev, R_AX_WDE_PKTBUF_CFG);
1348e3ec7017SPing-Ke Shih 	size_cfg = cfg->wde_size;
1349e3ec7017SPing-Ke Shih 
1350e3ec7017SPing-Ke Shih 	switch (size_cfg->pge_size) {
1351e3ec7017SPing-Ke Shih 	default:
1352e3ec7017SPing-Ke Shih 	case RTW89_WDE_PG_64:
1353e3ec7017SPing-Ke Shih 		val = u32_replace_bits(val, S_AX_WDE_PAGE_SEL_64,
1354e3ec7017SPing-Ke Shih 				       B_AX_WDE_PAGE_SEL_MASK);
1355e3ec7017SPing-Ke Shih 		break;
1356e3ec7017SPing-Ke Shih 	case RTW89_WDE_PG_128:
1357e3ec7017SPing-Ke Shih 		val = u32_replace_bits(val, S_AX_WDE_PAGE_SEL_128,
1358e3ec7017SPing-Ke Shih 				       B_AX_WDE_PAGE_SEL_MASK);
1359e3ec7017SPing-Ke Shih 		break;
1360e3ec7017SPing-Ke Shih 	case RTW89_WDE_PG_256:
1361e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]WDE DLE doesn't support 256 byte!\n");
1362e3ec7017SPing-Ke Shih 		return -EINVAL;
1363e3ec7017SPing-Ke Shih 	}
1364e3ec7017SPing-Ke Shih 
1365e3ec7017SPing-Ke Shih 	val = u32_replace_bits(val, bound, B_AX_WDE_START_BOUND_MASK);
1366e3ec7017SPing-Ke Shih 	val = u32_replace_bits(val, size_cfg->lnk_pge_num,
1367e3ec7017SPing-Ke Shih 			       B_AX_WDE_FREE_PAGE_NUM_MASK);
1368e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_WDE_PKTBUF_CFG, val);
1369e3ec7017SPing-Ke Shih 
1370e3ec7017SPing-Ke Shih 	val = rtw89_read32(rtwdev, R_AX_PLE_PKTBUF_CFG);
1371e3ec7017SPing-Ke Shih 	bound = (size_cfg->lnk_pge_num + size_cfg->unlnk_pge_num)
1372e3ec7017SPing-Ke Shih 				* size_cfg->pge_size / DLE_BOUND_UNIT;
1373e3ec7017SPing-Ke Shih 	size_cfg = cfg->ple_size;
1374e3ec7017SPing-Ke Shih 
1375e3ec7017SPing-Ke Shih 	switch (size_cfg->pge_size) {
1376e3ec7017SPing-Ke Shih 	default:
1377e3ec7017SPing-Ke Shih 	case RTW89_PLE_PG_64:
1378e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]PLE DLE doesn't support 64 byte!\n");
1379e3ec7017SPing-Ke Shih 		return -EINVAL;
1380e3ec7017SPing-Ke Shih 	case RTW89_PLE_PG_128:
1381e3ec7017SPing-Ke Shih 		val = u32_replace_bits(val, S_AX_PLE_PAGE_SEL_128,
1382e3ec7017SPing-Ke Shih 				       B_AX_PLE_PAGE_SEL_MASK);
1383e3ec7017SPing-Ke Shih 		break;
1384e3ec7017SPing-Ke Shih 	case RTW89_PLE_PG_256:
1385e3ec7017SPing-Ke Shih 		val = u32_replace_bits(val, S_AX_PLE_PAGE_SEL_256,
1386e3ec7017SPing-Ke Shih 				       B_AX_PLE_PAGE_SEL_MASK);
1387e3ec7017SPing-Ke Shih 		break;
1388e3ec7017SPing-Ke Shih 	}
1389e3ec7017SPing-Ke Shih 
1390e3ec7017SPing-Ke Shih 	val = u32_replace_bits(val, bound, B_AX_PLE_START_BOUND_MASK);
1391e3ec7017SPing-Ke Shih 	val = u32_replace_bits(val, size_cfg->lnk_pge_num,
1392e3ec7017SPing-Ke Shih 			       B_AX_PLE_FREE_PAGE_NUM_MASK);
1393e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_PLE_PKTBUF_CFG, val);
1394e3ec7017SPing-Ke Shih 
1395e3ec7017SPing-Ke Shih 	return 0;
1396e3ec7017SPing-Ke Shih }
1397e3ec7017SPing-Ke Shih 
1398e3ec7017SPing-Ke Shih #define INVALID_QT_WCPU U16_MAX
1399e3ec7017SPing-Ke Shih #define SET_QUOTA_VAL(_min_x, _max_x, _module, _idx)			\
1400e3ec7017SPing-Ke Shih 	do {								\
1401e3ec7017SPing-Ke Shih 		val = ((_min_x) &					\
1402e3ec7017SPing-Ke Shih 		       B_AX_ ## _module ## _MIN_SIZE_MASK) |		\
1403e3ec7017SPing-Ke Shih 		      (((_max_x) << 16) &				\
1404e3ec7017SPing-Ke Shih 		       B_AX_ ## _module ## _MAX_SIZE_MASK);		\
1405e3ec7017SPing-Ke Shih 		rtw89_write32(rtwdev,					\
1406e3ec7017SPing-Ke Shih 			      R_AX_ ## _module ## _QTA ## _idx ## _CFG,	\
1407e3ec7017SPing-Ke Shih 			      val);					\
1408e3ec7017SPing-Ke Shih 	} while (0)
1409e3ec7017SPing-Ke Shih #define SET_QUOTA(_x, _module, _idx)					\
1410e3ec7017SPing-Ke Shih 	SET_QUOTA_VAL(min_cfg->_x, max_cfg->_x, _module, _idx)
1411e3ec7017SPing-Ke Shih 
1412e3ec7017SPing-Ke Shih static void wde_quota_cfg(struct rtw89_dev *rtwdev,
1413e3ec7017SPing-Ke Shih 			  const struct rtw89_wde_quota *min_cfg,
1414e3ec7017SPing-Ke Shih 			  const struct rtw89_wde_quota *max_cfg,
1415e3ec7017SPing-Ke Shih 			  u16 ext_wde_min_qt_wcpu)
1416e3ec7017SPing-Ke Shih {
1417e3ec7017SPing-Ke Shih 	u16 min_qt_wcpu = ext_wde_min_qt_wcpu != INVALID_QT_WCPU ?
1418e3ec7017SPing-Ke Shih 			  ext_wde_min_qt_wcpu : min_cfg->wcpu;
1419e3ec7017SPing-Ke Shih 	u32 val;
1420e3ec7017SPing-Ke Shih 
1421e3ec7017SPing-Ke Shih 	SET_QUOTA(hif, WDE, 0);
1422e3ec7017SPing-Ke Shih 	SET_QUOTA_VAL(min_qt_wcpu, max_cfg->wcpu, WDE, 1);
1423e3ec7017SPing-Ke Shih 	SET_QUOTA(pkt_in, WDE, 3);
1424e3ec7017SPing-Ke Shih 	SET_QUOTA(cpu_io, WDE, 4);
1425e3ec7017SPing-Ke Shih }
1426e3ec7017SPing-Ke Shih 
1427e3ec7017SPing-Ke Shih static void ple_quota_cfg(struct rtw89_dev *rtwdev,
1428e3ec7017SPing-Ke Shih 			  const struct rtw89_ple_quota *min_cfg,
1429e3ec7017SPing-Ke Shih 			  const struct rtw89_ple_quota *max_cfg)
1430e3ec7017SPing-Ke Shih {
1431e3ec7017SPing-Ke Shih 	u32 val;
1432e3ec7017SPing-Ke Shih 
1433e3ec7017SPing-Ke Shih 	SET_QUOTA(cma0_tx, PLE, 0);
1434e3ec7017SPing-Ke Shih 	SET_QUOTA(cma1_tx, PLE, 1);
1435e3ec7017SPing-Ke Shih 	SET_QUOTA(c2h, PLE, 2);
1436e3ec7017SPing-Ke Shih 	SET_QUOTA(h2c, PLE, 3);
1437e3ec7017SPing-Ke Shih 	SET_QUOTA(wcpu, PLE, 4);
1438e3ec7017SPing-Ke Shih 	SET_QUOTA(mpdu_proc, PLE, 5);
1439e3ec7017SPing-Ke Shih 	SET_QUOTA(cma0_dma, PLE, 6);
1440e3ec7017SPing-Ke Shih 	SET_QUOTA(cma1_dma, PLE, 7);
1441e3ec7017SPing-Ke Shih 	SET_QUOTA(bb_rpt, PLE, 8);
1442e3ec7017SPing-Ke Shih 	SET_QUOTA(wd_rel, PLE, 9);
1443e3ec7017SPing-Ke Shih 	SET_QUOTA(cpu_io, PLE, 10);
144479d099e0SPing-Ke Shih 	if (rtwdev->chip->chip_id == RTL8852C)
144579d099e0SPing-Ke Shih 		SET_QUOTA(tx_rpt, PLE, 11);
1446e3ec7017SPing-Ke Shih }
1447e3ec7017SPing-Ke Shih 
1448e3ec7017SPing-Ke Shih #undef SET_QUOTA
1449e3ec7017SPing-Ke Shih 
1450e3ec7017SPing-Ke Shih static void dle_quota_cfg(struct rtw89_dev *rtwdev,
1451e3ec7017SPing-Ke Shih 			  const struct rtw89_dle_mem *cfg,
1452e3ec7017SPing-Ke Shih 			  u16 ext_wde_min_qt_wcpu)
1453e3ec7017SPing-Ke Shih {
1454e3ec7017SPing-Ke Shih 	wde_quota_cfg(rtwdev, cfg->wde_min_qt, cfg->wde_max_qt, ext_wde_min_qt_wcpu);
1455e3ec7017SPing-Ke Shih 	ple_quota_cfg(rtwdev, cfg->ple_min_qt, cfg->ple_max_qt);
1456e3ec7017SPing-Ke Shih }
1457e3ec7017SPing-Ke Shih 
1458e3ec7017SPing-Ke Shih static int dle_init(struct rtw89_dev *rtwdev, enum rtw89_qta_mode mode,
1459e3ec7017SPing-Ke Shih 		    enum rtw89_qta_mode ext_mode)
1460e3ec7017SPing-Ke Shih {
1461e3ec7017SPing-Ke Shih 	const struct rtw89_dle_mem *cfg, *ext_cfg;
1462e3ec7017SPing-Ke Shih 	u16 ext_wde_min_qt_wcpu = INVALID_QT_WCPU;
1463e3ec7017SPing-Ke Shih 	int ret = 0;
1464e3ec7017SPing-Ke Shih 	u32 ini;
1465e3ec7017SPing-Ke Shih 
1466e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, RTW89_MAC_0, RTW89_DMAC_SEL);
1467e3ec7017SPing-Ke Shih 	if (ret)
1468e3ec7017SPing-Ke Shih 		return ret;
1469e3ec7017SPing-Ke Shih 
1470e3ec7017SPing-Ke Shih 	cfg = get_dle_mem_cfg(rtwdev, mode);
1471e3ec7017SPing-Ke Shih 	if (!cfg) {
1472e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]get_dle_mem_cfg\n");
1473e3ec7017SPing-Ke Shih 		ret = -EINVAL;
1474e3ec7017SPing-Ke Shih 		goto error;
1475e3ec7017SPing-Ke Shih 	}
1476e3ec7017SPing-Ke Shih 
1477e3ec7017SPing-Ke Shih 	if (mode == RTW89_QTA_DLFW) {
1478e3ec7017SPing-Ke Shih 		ext_cfg = get_dle_mem_cfg(rtwdev, ext_mode);
1479e3ec7017SPing-Ke Shih 		if (!ext_cfg) {
1480e3ec7017SPing-Ke Shih 			rtw89_err(rtwdev, "[ERR]get_dle_ext_mem_cfg %d\n",
1481e3ec7017SPing-Ke Shih 				  ext_mode);
1482e3ec7017SPing-Ke Shih 			ret = -EINVAL;
1483e3ec7017SPing-Ke Shih 			goto error;
1484e3ec7017SPing-Ke Shih 		}
1485e3ec7017SPing-Ke Shih 		ext_wde_min_qt_wcpu = ext_cfg->wde_min_qt->wcpu;
1486e3ec7017SPing-Ke Shih 	}
1487e3ec7017SPing-Ke Shih 
1488e3ec7017SPing-Ke Shih 	if (dle_used_size(cfg->wde_size, cfg->ple_size) != rtwdev->chip->fifo_size) {
1489e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]wd/dle mem cfg\n");
1490e3ec7017SPing-Ke Shih 		ret = -EINVAL;
1491e3ec7017SPing-Ke Shih 		goto error;
1492e3ec7017SPing-Ke Shih 	}
1493e3ec7017SPing-Ke Shih 
1494e3ec7017SPing-Ke Shih 	dle_func_en(rtwdev, false);
1495e3ec7017SPing-Ke Shih 	dle_clk_en(rtwdev, true);
1496e3ec7017SPing-Ke Shih 
1497e3ec7017SPing-Ke Shih 	ret = dle_mix_cfg(rtwdev, cfg);
1498e3ec7017SPing-Ke Shih 	if (ret) {
1499e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR] dle mix cfg\n");
1500e3ec7017SPing-Ke Shih 		goto error;
1501e3ec7017SPing-Ke Shih 	}
1502e3ec7017SPing-Ke Shih 	dle_quota_cfg(rtwdev, cfg, ext_wde_min_qt_wcpu);
1503e3ec7017SPing-Ke Shih 
1504e3ec7017SPing-Ke Shih 	dle_func_en(rtwdev, true);
1505e3ec7017SPing-Ke Shih 
1506e3ec7017SPing-Ke Shih 	ret = read_poll_timeout(rtw89_read32, ini,
1507e3ec7017SPing-Ke Shih 				(ini & WDE_MGN_INI_RDY) == WDE_MGN_INI_RDY, 1,
1508e3ec7017SPing-Ke Shih 				2000, false, rtwdev, R_AX_WDE_INI_STATUS);
1509e3ec7017SPing-Ke Shih 	if (ret) {
1510e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]WDE cfg ready\n");
1511e3ec7017SPing-Ke Shih 		return ret;
1512e3ec7017SPing-Ke Shih 	}
1513e3ec7017SPing-Ke Shih 
1514e3ec7017SPing-Ke Shih 	ret = read_poll_timeout(rtw89_read32, ini,
1515e3ec7017SPing-Ke Shih 				(ini & WDE_MGN_INI_RDY) == WDE_MGN_INI_RDY, 1,
1516e3ec7017SPing-Ke Shih 				2000, false, rtwdev, R_AX_PLE_INI_STATUS);
1517e3ec7017SPing-Ke Shih 	if (ret) {
1518e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]PLE cfg ready\n");
1519e3ec7017SPing-Ke Shih 		return ret;
1520e3ec7017SPing-Ke Shih 	}
1521e3ec7017SPing-Ke Shih 
1522e3ec7017SPing-Ke Shih 	return 0;
1523e3ec7017SPing-Ke Shih error:
1524e3ec7017SPing-Ke Shih 	dle_func_en(rtwdev, false);
1525e3ec7017SPing-Ke Shih 	rtw89_err(rtwdev, "[ERR]trxcfg wde 0x8900 = %x\n",
1526e3ec7017SPing-Ke Shih 		  rtw89_read32(rtwdev, R_AX_WDE_INI_STATUS));
1527e3ec7017SPing-Ke Shih 	rtw89_err(rtwdev, "[ERR]trxcfg ple 0x8D00 = %x\n",
1528e3ec7017SPing-Ke Shih 		  rtw89_read32(rtwdev, R_AX_PLE_INI_STATUS));
1529e3ec7017SPing-Ke Shih 
1530e3ec7017SPing-Ke Shih 	return ret;
1531e3ec7017SPing-Ke Shih }
1532e3ec7017SPing-Ke Shih 
1533e07a9968SPing-Ke Shih static int preload_init_set(struct rtw89_dev *rtwdev, enum rtw89_mac_idx mac_idx,
1534e07a9968SPing-Ke Shih 			    enum rtw89_qta_mode mode)
1535e07a9968SPing-Ke Shih {
1536e07a9968SPing-Ke Shih 	u32 reg, max_preld_size, min_rsvd_size;
1537e07a9968SPing-Ke Shih 
1538e07a9968SPing-Ke Shih 	max_preld_size = (mac_idx == RTW89_MAC_0 ?
1539e07a9968SPing-Ke Shih 			  PRELD_B0_ENT_NUM : PRELD_B1_ENT_NUM) * PRELD_AMSDU_SIZE;
1540e07a9968SPing-Ke Shih 	reg = mac_idx == RTW89_MAC_0 ?
1541e07a9968SPing-Ke Shih 	      R_AX_TXPKTCTL_B0_PRELD_CFG0 : R_AX_TXPKTCTL_B1_PRELD_CFG0;
1542e07a9968SPing-Ke Shih 	rtw89_write32_mask(rtwdev, reg, B_AX_B0_PRELD_USEMAXSZ_MASK, max_preld_size);
1543e07a9968SPing-Ke Shih 	rtw89_write32_set(rtwdev, reg, B_AX_B0_PRELD_FEN);
1544e07a9968SPing-Ke Shih 
1545e07a9968SPing-Ke Shih 	min_rsvd_size = PRELD_AMSDU_SIZE;
1546e07a9968SPing-Ke Shih 	reg = mac_idx == RTW89_MAC_0 ?
1547e07a9968SPing-Ke Shih 	      R_AX_TXPKTCTL_B0_PRELD_CFG1 : R_AX_TXPKTCTL_B1_PRELD_CFG1;
1548e07a9968SPing-Ke Shih 	rtw89_write32_mask(rtwdev, reg, B_AX_B0_PRELD_NXT_TXENDWIN_MASK, PRELD_NEXT_WND);
1549e07a9968SPing-Ke Shih 	rtw89_write32_mask(rtwdev, reg, B_AX_B0_PRELD_NXT_RSVMINSZ_MASK, min_rsvd_size);
1550e07a9968SPing-Ke Shih 
1551e07a9968SPing-Ke Shih 	return 0;
1552e07a9968SPing-Ke Shih }
1553e07a9968SPing-Ke Shih 
1554e07a9968SPing-Ke Shih static bool is_qta_poh(struct rtw89_dev *rtwdev)
1555e07a9968SPing-Ke Shih {
1556e07a9968SPing-Ke Shih 	return rtwdev->hci.type == RTW89_HCI_TYPE_PCIE;
1557e07a9968SPing-Ke Shih }
1558e07a9968SPing-Ke Shih 
1559e07a9968SPing-Ke Shih static int preload_init(struct rtw89_dev *rtwdev, enum rtw89_mac_idx mac_idx,
1560e07a9968SPing-Ke Shih 			enum rtw89_qta_mode mode)
1561e07a9968SPing-Ke Shih {
1562e07a9968SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
1563e07a9968SPing-Ke Shih 
1564e07a9968SPing-Ke Shih 	if (chip->chip_id == RTL8852A || chip->chip_id == RTL8852B || !is_qta_poh(rtwdev))
1565e07a9968SPing-Ke Shih 		return 0;
1566e07a9968SPing-Ke Shih 
1567e07a9968SPing-Ke Shih 	return preload_init_set(rtwdev, mac_idx, mode);
1568e07a9968SPing-Ke Shih }
1569e07a9968SPing-Ke Shih 
1570e3ec7017SPing-Ke Shih static bool dle_is_txq_empty(struct rtw89_dev *rtwdev)
1571e3ec7017SPing-Ke Shih {
1572e3ec7017SPing-Ke Shih 	u32 msk32;
1573e3ec7017SPing-Ke Shih 	u32 val32;
1574e3ec7017SPing-Ke Shih 
1575e3ec7017SPing-Ke Shih 	msk32 = B_AX_WDE_EMPTY_QUE_CMAC0_ALL_AC | B_AX_WDE_EMPTY_QUE_CMAC0_MBH |
1576e3ec7017SPing-Ke Shih 		B_AX_WDE_EMPTY_QUE_CMAC1_MBH | B_AX_WDE_EMPTY_QUE_CMAC0_WMM0 |
1577e3ec7017SPing-Ke Shih 		B_AX_WDE_EMPTY_QUE_CMAC0_WMM1 | B_AX_WDE_EMPTY_QUE_OTHERS |
1578e3ec7017SPing-Ke Shih 		B_AX_PLE_EMPTY_QUE_DMAC_MPDU_TX | B_AX_PLE_EMPTY_QTA_DMAC_H2C |
1579e3ec7017SPing-Ke Shih 		B_AX_PLE_EMPTY_QUE_DMAC_SEC_TX | B_AX_WDE_EMPTY_QUE_DMAC_PKTIN |
1580e3ec7017SPing-Ke Shih 		B_AX_WDE_EMPTY_QTA_DMAC_HIF | B_AX_WDE_EMPTY_QTA_DMAC_WLAN_CPU |
1581e3ec7017SPing-Ke Shih 		B_AX_WDE_EMPTY_QTA_DMAC_PKTIN | B_AX_WDE_EMPTY_QTA_DMAC_CPUIO |
1582e3ec7017SPing-Ke Shih 		B_AX_PLE_EMPTY_QTA_DMAC_B0_TXPL |
1583e3ec7017SPing-Ke Shih 		B_AX_PLE_EMPTY_QTA_DMAC_B1_TXPL |
1584e3ec7017SPing-Ke Shih 		B_AX_PLE_EMPTY_QTA_DMAC_MPDU_TX |
1585e3ec7017SPing-Ke Shih 		B_AX_PLE_EMPTY_QTA_DMAC_CPUIO |
1586e3ec7017SPing-Ke Shih 		B_AX_WDE_EMPTY_QTA_DMAC_DATA_CPU |
1587e3ec7017SPing-Ke Shih 		B_AX_PLE_EMPTY_QTA_DMAC_WLAN_CPU;
1588e3ec7017SPing-Ke Shih 	val32 = rtw89_read32(rtwdev, R_AX_DLE_EMPTY0);
1589e3ec7017SPing-Ke Shih 
1590e3ec7017SPing-Ke Shih 	if ((val32 & msk32) == msk32)
1591e3ec7017SPing-Ke Shih 		return true;
1592e3ec7017SPing-Ke Shih 
1593e3ec7017SPing-Ke Shih 	return false;
1594e3ec7017SPing-Ke Shih }
1595e3ec7017SPing-Ke Shih 
1596cf7b8b80SPing-Ke Shih static void _patch_ss2f_path(struct rtw89_dev *rtwdev)
1597cf7b8b80SPing-Ke Shih {
1598cf7b8b80SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
1599cf7b8b80SPing-Ke Shih 
1600cf7b8b80SPing-Ke Shih 	if (chip->chip_id == RTL8852A || chip->chip_id == RTL8852B)
1601cf7b8b80SPing-Ke Shih 		return;
1602cf7b8b80SPing-Ke Shih 
1603cf7b8b80SPing-Ke Shih 	rtw89_write32_mask(rtwdev, R_AX_SS2FINFO_PATH, B_AX_SS_DEST_QUEUE_MASK,
1604cf7b8b80SPing-Ke Shih 			   SS2F_PATH_WLCPU);
1605cf7b8b80SPing-Ke Shih }
1606cf7b8b80SPing-Ke Shih 
1607e3ec7017SPing-Ke Shih static int sta_sch_init(struct rtw89_dev *rtwdev)
1608e3ec7017SPing-Ke Shih {
1609e3ec7017SPing-Ke Shih 	u32 p_val;
1610e3ec7017SPing-Ke Shih 	u8 val;
1611e3ec7017SPing-Ke Shih 	int ret;
1612e3ec7017SPing-Ke Shih 
1613e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, RTW89_MAC_0, RTW89_DMAC_SEL);
1614e3ec7017SPing-Ke Shih 	if (ret)
1615e3ec7017SPing-Ke Shih 		return ret;
1616e3ec7017SPing-Ke Shih 
1617e3ec7017SPing-Ke Shih 	val = rtw89_read8(rtwdev, R_AX_SS_CTRL);
1618e3ec7017SPing-Ke Shih 	val |= B_AX_SS_EN;
1619e3ec7017SPing-Ke Shih 	rtw89_write8(rtwdev, R_AX_SS_CTRL, val);
1620e3ec7017SPing-Ke Shih 
1621e3ec7017SPing-Ke Shih 	ret = read_poll_timeout(rtw89_read32, p_val, p_val & B_AX_SS_INIT_DONE_1,
1622e3ec7017SPing-Ke Shih 				1, TRXCFG_WAIT_CNT, false, rtwdev, R_AX_SS_CTRL);
1623e3ec7017SPing-Ke Shih 	if (ret) {
1624e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]STA scheduler init\n");
1625e3ec7017SPing-Ke Shih 		return ret;
1626e3ec7017SPing-Ke Shih 	}
1627e3ec7017SPing-Ke Shih 
16289a1ab283SPing-Ke Shih 	rtw89_write32_set(rtwdev, R_AX_SS_CTRL, B_AX_SS_WARM_INIT_FLG);
16299a1ab283SPing-Ke Shih 	rtw89_write32_clr(rtwdev, R_AX_SS_CTRL, B_AX_SS_NONEMPTY_SS2FINFO_EN);
1630cf7b8b80SPing-Ke Shih 
1631cf7b8b80SPing-Ke Shih 	_patch_ss2f_path(rtwdev);
1632e3ec7017SPing-Ke Shih 
1633e3ec7017SPing-Ke Shih 	return 0;
1634e3ec7017SPing-Ke Shih }
1635e3ec7017SPing-Ke Shih 
1636e3ec7017SPing-Ke Shih static int mpdu_proc_init(struct rtw89_dev *rtwdev)
1637e3ec7017SPing-Ke Shih {
1638e3ec7017SPing-Ke Shih 	int ret;
1639e3ec7017SPing-Ke Shih 
1640e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, RTW89_MAC_0, RTW89_DMAC_SEL);
1641e3ec7017SPing-Ke Shih 	if (ret)
1642e3ec7017SPing-Ke Shih 		return ret;
1643e3ec7017SPing-Ke Shih 
1644e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_ACTION_FWD0, TRXCFG_MPDU_PROC_ACT_FRWD);
1645e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_TF_FWD, TRXCFG_MPDU_PROC_TF_FRWD);
1646e3ec7017SPing-Ke Shih 	rtw89_write32_set(rtwdev, R_AX_MPDU_PROC,
1647e3ec7017SPing-Ke Shih 			  B_AX_APPEND_FCS | B_AX_A_ICV_ERR);
1648e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_CUT_AMSDU_CTRL, TRXCFG_MPDU_PROC_CUT_CTRL);
1649e3ec7017SPing-Ke Shih 
1650e3ec7017SPing-Ke Shih 	return 0;
1651e3ec7017SPing-Ke Shih }
1652e3ec7017SPing-Ke Shih 
1653e3ec7017SPing-Ke Shih static int sec_eng_init(struct rtw89_dev *rtwdev)
1654e3ec7017SPing-Ke Shih {
1655b61adeedSPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
1656e3ec7017SPing-Ke Shih 	u32 val = 0;
1657e3ec7017SPing-Ke Shih 	int ret;
1658e3ec7017SPing-Ke Shih 
1659e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, RTW89_MAC_0, RTW89_DMAC_SEL);
1660e3ec7017SPing-Ke Shih 	if (ret)
1661e3ec7017SPing-Ke Shih 		return ret;
1662e3ec7017SPing-Ke Shih 
1663e3ec7017SPing-Ke Shih 	val = rtw89_read32(rtwdev, R_AX_SEC_ENG_CTRL);
1664e3ec7017SPing-Ke Shih 	/* init clock */
1665e3ec7017SPing-Ke Shih 	val |= (B_AX_CLK_EN_CGCMP | B_AX_CLK_EN_WAPI | B_AX_CLK_EN_WEP_TKIP);
1666e3ec7017SPing-Ke Shih 	/* init TX encryption */
1667e3ec7017SPing-Ke Shih 	val |= (B_AX_SEC_TX_ENC | B_AX_SEC_RX_DEC);
1668e3ec7017SPing-Ke Shih 	val |= (B_AX_MC_DEC | B_AX_BC_DEC);
1669b61adeedSPing-Ke Shih 	if (chip->chip_id == RTL8852A || chip->chip_id == RTL8852B)
1670e3ec7017SPing-Ke Shih 		val &= ~B_AX_TX_PARTIAL_MODE;
1671e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_SEC_ENG_CTRL, val);
1672e3ec7017SPing-Ke Shih 
1673e3ec7017SPing-Ke Shih 	/* init MIC ICV append */
1674e3ec7017SPing-Ke Shih 	val = rtw89_read32(rtwdev, R_AX_SEC_MPDU_PROC);
1675e3ec7017SPing-Ke Shih 	val |= (B_AX_APPEND_ICV | B_AX_APPEND_MIC);
1676e3ec7017SPing-Ke Shih 
1677e3ec7017SPing-Ke Shih 	/* option init */
1678e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_SEC_MPDU_PROC, val);
1679e3ec7017SPing-Ke Shih 
1680b61adeedSPing-Ke Shih 	if (chip->chip_id == RTL8852C)
1681b61adeedSPing-Ke Shih 		rtw89_write32_mask(rtwdev, R_AX_SEC_DEBUG1,
1682b61adeedSPing-Ke Shih 				   B_AX_TX_TIMEOUT_SEL_MASK, AX_TX_TO_VAL);
1683b61adeedSPing-Ke Shih 
1684e3ec7017SPing-Ke Shih 	return 0;
1685e3ec7017SPing-Ke Shih }
1686e3ec7017SPing-Ke Shih 
1687e3ec7017SPing-Ke Shih static int dmac_init(struct rtw89_dev *rtwdev, u8 mac_idx)
1688e3ec7017SPing-Ke Shih {
1689e3ec7017SPing-Ke Shih 	int ret;
1690e3ec7017SPing-Ke Shih 
1691e3ec7017SPing-Ke Shih 	ret = dle_init(rtwdev, rtwdev->mac.qta_mode, RTW89_QTA_INVALID);
1692e3ec7017SPing-Ke Shih 	if (ret) {
1693e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]DLE init %d\n", ret);
1694e3ec7017SPing-Ke Shih 		return ret;
1695e3ec7017SPing-Ke Shih 	}
1696e3ec7017SPing-Ke Shih 
1697e07a9968SPing-Ke Shih 	ret = preload_init(rtwdev, RTW89_MAC_0, rtwdev->mac.qta_mode);
1698e07a9968SPing-Ke Shih 	if (ret) {
1699e07a9968SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]preload init %d\n", ret);
1700e07a9968SPing-Ke Shih 		return ret;
1701e07a9968SPing-Ke Shih 	}
1702e07a9968SPing-Ke Shih 
1703e3ec7017SPing-Ke Shih 	ret = hfc_init(rtwdev, true, true, true);
1704e3ec7017SPing-Ke Shih 	if (ret) {
1705e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]HCI FC init %d\n", ret);
1706e3ec7017SPing-Ke Shih 		return ret;
1707e3ec7017SPing-Ke Shih 	}
1708e3ec7017SPing-Ke Shih 
1709e3ec7017SPing-Ke Shih 	ret = sta_sch_init(rtwdev);
1710e3ec7017SPing-Ke Shih 	if (ret) {
1711e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]STA SCH init %d\n", ret);
1712e3ec7017SPing-Ke Shih 		return ret;
1713e3ec7017SPing-Ke Shih 	}
1714e3ec7017SPing-Ke Shih 
1715e3ec7017SPing-Ke Shih 	ret = mpdu_proc_init(rtwdev);
1716e3ec7017SPing-Ke Shih 	if (ret) {
1717e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]MPDU Proc init %d\n", ret);
1718e3ec7017SPing-Ke Shih 		return ret;
1719e3ec7017SPing-Ke Shih 	}
1720e3ec7017SPing-Ke Shih 
1721e3ec7017SPing-Ke Shih 	ret = sec_eng_init(rtwdev);
1722e3ec7017SPing-Ke Shih 	if (ret) {
1723e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]Security Engine init %d\n", ret);
1724e3ec7017SPing-Ke Shih 		return ret;
1725e3ec7017SPing-Ke Shih 	}
1726e3ec7017SPing-Ke Shih 
1727e3ec7017SPing-Ke Shih 	return ret;
1728e3ec7017SPing-Ke Shih }
1729e3ec7017SPing-Ke Shih 
1730e3ec7017SPing-Ke Shih static int addr_cam_init(struct rtw89_dev *rtwdev, u8 mac_idx)
1731e3ec7017SPing-Ke Shih {
1732e3ec7017SPing-Ke Shih 	u32 val, reg;
1733e3ec7017SPing-Ke Shih 	u16 p_val;
1734e3ec7017SPing-Ke Shih 	int ret;
1735e3ec7017SPing-Ke Shih 
1736e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL);
1737e3ec7017SPing-Ke Shih 	if (ret)
1738e3ec7017SPing-Ke Shih 		return ret;
1739e3ec7017SPing-Ke Shih 
1740e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_ADDR_CAM_CTRL, mac_idx);
1741e3ec7017SPing-Ke Shih 
1742e3ec7017SPing-Ke Shih 	val = rtw89_read32(rtwdev, reg);
1743e3ec7017SPing-Ke Shih 	val |= u32_encode_bits(0x7f, B_AX_ADDR_CAM_RANGE_MASK) |
1744e3ec7017SPing-Ke Shih 	       B_AX_ADDR_CAM_CLR | B_AX_ADDR_CAM_EN;
1745e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, reg, val);
1746e3ec7017SPing-Ke Shih 
1747e3ec7017SPing-Ke Shih 	ret = read_poll_timeout(rtw89_read16, p_val, !(p_val & B_AX_ADDR_CAM_CLR),
1748ad275d0aSPing-Ke Shih 				1, TRXCFG_WAIT_CNT, false, rtwdev, reg);
1749e3ec7017SPing-Ke Shih 	if (ret) {
1750e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]ADDR_CAM reset\n");
1751e3ec7017SPing-Ke Shih 		return ret;
1752e3ec7017SPing-Ke Shih 	}
1753e3ec7017SPing-Ke Shih 
1754e3ec7017SPing-Ke Shih 	return 0;
1755e3ec7017SPing-Ke Shih }
1756e3ec7017SPing-Ke Shih 
1757e3ec7017SPing-Ke Shih static int scheduler_init(struct rtw89_dev *rtwdev, u8 mac_idx)
1758e3ec7017SPing-Ke Shih {
1759e3ec7017SPing-Ke Shih 	u32 ret;
1760e3ec7017SPing-Ke Shih 	u32 reg;
176160b2ede9SChia-Yuan Li 	u32 val;
1762e3ec7017SPing-Ke Shih 
1763e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL);
1764e3ec7017SPing-Ke Shih 	if (ret)
1765e3ec7017SPing-Ke Shih 		return ret;
1766e3ec7017SPing-Ke Shih 
1767c49154ffSPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_PREBKF_CFG_1, mac_idx);
1768ee546904SChia-Yuan Li 	if (rtwdev->chip->chip_id == RTL8852C)
1769ee546904SChia-Yuan Li 		rtw89_write32_mask(rtwdev, reg, B_AX_SIFS_MACTXEN_T1_MASK,
1770ee546904SChia-Yuan Li 				   SIFS_MACTXEN_T1_V1);
1771ee546904SChia-Yuan Li 	else
1772ee546904SChia-Yuan Li 		rtw89_write32_mask(rtwdev, reg, B_AX_SIFS_MACTXEN_T1_MASK,
1773ee546904SChia-Yuan Li 				   SIFS_MACTXEN_T1);
1774c49154ffSPing-Ke Shih 
1775c49154ffSPing-Ke Shih 	if (rtwdev->chip->chip_id == RTL8852B) {
1776c49154ffSPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(R_AX_SCH_EXT_CTRL, mac_idx);
1777c49154ffSPing-Ke Shih 		rtw89_write32_set(rtwdev, reg, B_AX_PORT_RST_TSF_ADV);
1778c49154ffSPing-Ke Shih 	}
1779c49154ffSPing-Ke Shih 
1780c49154ffSPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_CCA_CFG_0, mac_idx);
1781c49154ffSPing-Ke Shih 	rtw89_write32_clr(rtwdev, reg, B_AX_BTCCA_EN);
1782c49154ffSPing-Ke Shih 
1783e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_PREBKF_CFG_0, mac_idx);
178460b2ede9SChia-Yuan Li 	if (rtwdev->chip->chip_id == RTL8852C) {
178560b2ede9SChia-Yuan Li 		val = rtw89_read32_mask(rtwdev, R_AX_SEC_ENG_CTRL,
178660b2ede9SChia-Yuan Li 					B_AX_TX_PARTIAL_MODE);
178760b2ede9SChia-Yuan Li 		if (!val)
178860b2ede9SChia-Yuan Li 			rtw89_write32_mask(rtwdev, reg, B_AX_PREBKF_TIME_MASK,
178960b2ede9SChia-Yuan Li 					   SCH_PREBKF_24US);
179060b2ede9SChia-Yuan Li 	} else {
179160b2ede9SChia-Yuan Li 		rtw89_write32_mask(rtwdev, reg, B_AX_PREBKF_TIME_MASK,
179260b2ede9SChia-Yuan Li 				   SCH_PREBKF_24US);
179360b2ede9SChia-Yuan Li 	}
1794e3ec7017SPing-Ke Shih 
1795e3ec7017SPing-Ke Shih 	return 0;
1796e3ec7017SPing-Ke Shih }
1797e3ec7017SPing-Ke Shih 
1798e3ec7017SPing-Ke Shih static int rtw89_mac_typ_fltr_opt(struct rtw89_dev *rtwdev,
1799e3ec7017SPing-Ke Shih 				  enum rtw89_machdr_frame_type type,
1800e3ec7017SPing-Ke Shih 				  enum rtw89_mac_fwd_target fwd_target,
1801e3ec7017SPing-Ke Shih 				  u8 mac_idx)
1802e3ec7017SPing-Ke Shih {
1803e3ec7017SPing-Ke Shih 	u32 reg;
1804e3ec7017SPing-Ke Shih 	u32 val;
1805e3ec7017SPing-Ke Shih 
1806e3ec7017SPing-Ke Shih 	switch (fwd_target) {
1807e3ec7017SPing-Ke Shih 	case RTW89_FWD_DONT_CARE:
1808e3ec7017SPing-Ke Shih 		val = RX_FLTR_FRAME_DROP;
1809e3ec7017SPing-Ke Shih 		break;
1810e3ec7017SPing-Ke Shih 	case RTW89_FWD_TO_HOST:
1811e3ec7017SPing-Ke Shih 		val = RX_FLTR_FRAME_TO_HOST;
1812e3ec7017SPing-Ke Shih 		break;
1813e3ec7017SPing-Ke Shih 	case RTW89_FWD_TO_WLAN_CPU:
1814e3ec7017SPing-Ke Shih 		val = RX_FLTR_FRAME_TO_WLCPU;
1815e3ec7017SPing-Ke Shih 		break;
1816e3ec7017SPing-Ke Shih 	default:
1817e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]set rx filter fwd target err\n");
1818e3ec7017SPing-Ke Shih 		return -EINVAL;
1819e3ec7017SPing-Ke Shih 	}
1820e3ec7017SPing-Ke Shih 
1821e3ec7017SPing-Ke Shih 	switch (type) {
1822e3ec7017SPing-Ke Shih 	case RTW89_MGNT:
1823e3ec7017SPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(R_AX_MGNT_FLTR, mac_idx);
1824e3ec7017SPing-Ke Shih 		break;
1825e3ec7017SPing-Ke Shih 	case RTW89_CTRL:
1826e3ec7017SPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(R_AX_CTRL_FLTR, mac_idx);
1827e3ec7017SPing-Ke Shih 		break;
1828e3ec7017SPing-Ke Shih 	case RTW89_DATA:
1829e3ec7017SPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(R_AX_DATA_FLTR, mac_idx);
1830e3ec7017SPing-Ke Shih 		break;
1831e3ec7017SPing-Ke Shih 	default:
1832e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]set rx filter type err\n");
1833e3ec7017SPing-Ke Shih 		return -EINVAL;
1834e3ec7017SPing-Ke Shih 	}
1835e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, reg, val);
1836e3ec7017SPing-Ke Shih 
1837e3ec7017SPing-Ke Shih 	return 0;
1838e3ec7017SPing-Ke Shih }
1839e3ec7017SPing-Ke Shih 
1840e3ec7017SPing-Ke Shih static int rx_fltr_init(struct rtw89_dev *rtwdev, u8 mac_idx)
1841e3ec7017SPing-Ke Shih {
1842e3ec7017SPing-Ke Shih 	int ret, i;
1843e3ec7017SPing-Ke Shih 	u32 mac_ftlr, plcp_ftlr;
1844e3ec7017SPing-Ke Shih 
1845e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL);
1846e3ec7017SPing-Ke Shih 	if (ret)
1847e3ec7017SPing-Ke Shih 		return ret;
1848e3ec7017SPing-Ke Shih 
1849e3ec7017SPing-Ke Shih 	for (i = RTW89_MGNT; i <= RTW89_DATA; i++) {
1850e3ec7017SPing-Ke Shih 		ret = rtw89_mac_typ_fltr_opt(rtwdev, i, RTW89_FWD_TO_HOST,
1851e3ec7017SPing-Ke Shih 					     mac_idx);
1852e3ec7017SPing-Ke Shih 		if (ret)
1853e3ec7017SPing-Ke Shih 			return ret;
1854e3ec7017SPing-Ke Shih 	}
1855e3ec7017SPing-Ke Shih 	mac_ftlr = rtwdev->hal.rx_fltr;
1856e3ec7017SPing-Ke Shih 	plcp_ftlr = B_AX_CCK_CRC_CHK | B_AX_CCK_SIG_CHK |
1857e3ec7017SPing-Ke Shih 		    B_AX_LSIG_PARITY_CHK_EN | B_AX_SIGA_CRC_CHK |
1858e3ec7017SPing-Ke Shih 		    B_AX_VHT_SU_SIGB_CRC_CHK | B_AX_VHT_MU_SIGB_CRC_CHK |
1859e3ec7017SPing-Ke Shih 		    B_AX_HE_SIGB_CRC_CHK;
1860e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, rtw89_mac_reg_by_idx(R_AX_RX_FLTR_OPT, mac_idx),
1861e3ec7017SPing-Ke Shih 		      mac_ftlr);
1862e3ec7017SPing-Ke Shih 	rtw89_write16(rtwdev, rtw89_mac_reg_by_idx(R_AX_PLCP_HDR_FLTR, mac_idx),
1863e3ec7017SPing-Ke Shih 		      plcp_ftlr);
1864e3ec7017SPing-Ke Shih 
1865e3ec7017SPing-Ke Shih 	return 0;
1866e3ec7017SPing-Ke Shih }
1867e3ec7017SPing-Ke Shih 
1868e3ec7017SPing-Ke Shih static void _patch_dis_resp_chk(struct rtw89_dev *rtwdev, u8 mac_idx)
1869e3ec7017SPing-Ke Shih {
1870e3ec7017SPing-Ke Shih 	u32 reg, val32;
1871e3ec7017SPing-Ke Shih 	u32 b_rsp_chk_nav, b_rsp_chk_cca;
1872e3ec7017SPing-Ke Shih 
1873e3ec7017SPing-Ke Shih 	b_rsp_chk_nav = B_AX_RSP_CHK_TXNAV | B_AX_RSP_CHK_INTRA_NAV |
1874e3ec7017SPing-Ke Shih 			B_AX_RSP_CHK_BASIC_NAV;
1875e3ec7017SPing-Ke Shih 	b_rsp_chk_cca = B_AX_RSP_CHK_SEC_CCA_80 | B_AX_RSP_CHK_SEC_CCA_40 |
1876e3ec7017SPing-Ke Shih 			B_AX_RSP_CHK_SEC_CCA_20 | B_AX_RSP_CHK_BTCCA |
1877e3ec7017SPing-Ke Shih 			B_AX_RSP_CHK_EDCCA | B_AX_RSP_CHK_CCA;
1878e3ec7017SPing-Ke Shih 
1879e3ec7017SPing-Ke Shih 	switch (rtwdev->chip->chip_id) {
1880e3ec7017SPing-Ke Shih 	case RTL8852A:
1881e3ec7017SPing-Ke Shih 	case RTL8852B:
1882e3ec7017SPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(R_AX_RSP_CHK_SIG, mac_idx);
1883e3ec7017SPing-Ke Shih 		val32 = rtw89_read32(rtwdev, reg) & ~b_rsp_chk_nav;
1884e3ec7017SPing-Ke Shih 		rtw89_write32(rtwdev, reg, val32);
1885e3ec7017SPing-Ke Shih 
1886e3ec7017SPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(R_AX_TRXPTCL_RESP_0, mac_idx);
1887e3ec7017SPing-Ke Shih 		val32 = rtw89_read32(rtwdev, reg) & ~b_rsp_chk_cca;
1888e3ec7017SPing-Ke Shih 		rtw89_write32(rtwdev, reg, val32);
1889e3ec7017SPing-Ke Shih 		break;
1890e3ec7017SPing-Ke Shih 	default:
1891e3ec7017SPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(R_AX_RSP_CHK_SIG, mac_idx);
1892e3ec7017SPing-Ke Shih 		val32 = rtw89_read32(rtwdev, reg) | b_rsp_chk_nav;
1893e3ec7017SPing-Ke Shih 		rtw89_write32(rtwdev, reg, val32);
1894e3ec7017SPing-Ke Shih 
1895e3ec7017SPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(R_AX_TRXPTCL_RESP_0, mac_idx);
1896e3ec7017SPing-Ke Shih 		val32 = rtw89_read32(rtwdev, reg) | b_rsp_chk_cca;
1897e3ec7017SPing-Ke Shih 		rtw89_write32(rtwdev, reg, val32);
1898e3ec7017SPing-Ke Shih 		break;
1899e3ec7017SPing-Ke Shih 	}
1900e3ec7017SPing-Ke Shih }
1901e3ec7017SPing-Ke Shih 
1902e3ec7017SPing-Ke Shih static int cca_ctrl_init(struct rtw89_dev *rtwdev, u8 mac_idx)
1903e3ec7017SPing-Ke Shih {
1904e3ec7017SPing-Ke Shih 	u32 val, reg;
1905e3ec7017SPing-Ke Shih 	int ret;
1906e3ec7017SPing-Ke Shih 
1907e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL);
1908e3ec7017SPing-Ke Shih 	if (ret)
1909e3ec7017SPing-Ke Shih 		return ret;
1910e3ec7017SPing-Ke Shih 
1911e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_CCA_CONTROL, mac_idx);
1912e3ec7017SPing-Ke Shih 	val = rtw89_read32(rtwdev, reg);
1913e3ec7017SPing-Ke Shih 	val |= (B_AX_TB_CHK_BASIC_NAV | B_AX_TB_CHK_BTCCA |
1914e3ec7017SPing-Ke Shih 		B_AX_TB_CHK_EDCCA | B_AX_TB_CHK_CCA_P20 |
1915e3ec7017SPing-Ke Shih 		B_AX_SIFS_CHK_BTCCA | B_AX_SIFS_CHK_CCA_P20 |
1916e3ec7017SPing-Ke Shih 		B_AX_CTN_CHK_INTRA_NAV |
1917e3ec7017SPing-Ke Shih 		B_AX_CTN_CHK_BASIC_NAV | B_AX_CTN_CHK_BTCCA |
1918e3ec7017SPing-Ke Shih 		B_AX_CTN_CHK_EDCCA | B_AX_CTN_CHK_CCA_S80 |
1919e3ec7017SPing-Ke Shih 		B_AX_CTN_CHK_CCA_S40 | B_AX_CTN_CHK_CCA_S20 |
192098ed6159SPing-Ke Shih 		B_AX_CTN_CHK_CCA_P20);
1921e3ec7017SPing-Ke Shih 	val &= ~(B_AX_TB_CHK_TX_NAV | B_AX_TB_CHK_CCA_S80 |
1922e3ec7017SPing-Ke Shih 		 B_AX_TB_CHK_CCA_S40 | B_AX_TB_CHK_CCA_S20 |
1923e3ec7017SPing-Ke Shih 		 B_AX_SIFS_CHK_CCA_S80 | B_AX_SIFS_CHK_CCA_S40 |
192498ed6159SPing-Ke Shih 		 B_AX_SIFS_CHK_CCA_S20 | B_AX_CTN_CHK_TXNAV |
192598ed6159SPing-Ke Shih 		 B_AX_SIFS_CHK_EDCCA);
1926e3ec7017SPing-Ke Shih 
1927e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, reg, val);
1928e3ec7017SPing-Ke Shih 
1929e3ec7017SPing-Ke Shih 	_patch_dis_resp_chk(rtwdev, mac_idx);
1930e3ec7017SPing-Ke Shih 
1931e3ec7017SPing-Ke Shih 	return 0;
1932e3ec7017SPing-Ke Shih }
1933e3ec7017SPing-Ke Shih 
193419cb9427SPing-Ke Shih static int nav_ctrl_init(struct rtw89_dev *rtwdev)
193519cb9427SPing-Ke Shih {
193619cb9427SPing-Ke Shih 	rtw89_write32_set(rtwdev, R_AX_WMAC_NAV_CTL, B_AX_WMAC_PLCP_UP_NAV_EN |
193719cb9427SPing-Ke Shih 						     B_AX_WMAC_TF_UP_NAV_EN |
193819cb9427SPing-Ke Shih 						     B_AX_WMAC_NAV_UPPER_EN);
1939c060dc51SPing-Ke Shih 	rtw89_write32_mask(rtwdev, R_AX_WMAC_NAV_CTL, B_AX_WMAC_NAV_UPPER_MASK, NAV_25MS);
194019cb9427SPing-Ke Shih 
194119cb9427SPing-Ke Shih 	return 0;
194219cb9427SPing-Ke Shih }
194319cb9427SPing-Ke Shih 
1944e3ec7017SPing-Ke Shih static int spatial_reuse_init(struct rtw89_dev *rtwdev, u8 mac_idx)
1945e3ec7017SPing-Ke Shih {
1946e3ec7017SPing-Ke Shih 	u32 reg;
1947e3ec7017SPing-Ke Shih 	int ret;
1948e3ec7017SPing-Ke Shih 
1949e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL);
1950e3ec7017SPing-Ke Shih 	if (ret)
1951e3ec7017SPing-Ke Shih 		return ret;
1952e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_RX_SR_CTRL, mac_idx);
1953e3ec7017SPing-Ke Shih 	rtw89_write8_clr(rtwdev, reg, B_AX_SR_EN);
1954e3ec7017SPing-Ke Shih 
1955e3ec7017SPing-Ke Shih 	return 0;
1956e3ec7017SPing-Ke Shih }
1957e3ec7017SPing-Ke Shih 
1958e3ec7017SPing-Ke Shih static int tmac_init(struct rtw89_dev *rtwdev, u8 mac_idx)
1959e3ec7017SPing-Ke Shih {
1960e3ec7017SPing-Ke Shih 	u32 reg;
1961e3ec7017SPing-Ke Shih 	int ret;
1962e3ec7017SPing-Ke Shih 
1963e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL);
1964e3ec7017SPing-Ke Shih 	if (ret)
1965e3ec7017SPing-Ke Shih 		return ret;
1966e3ec7017SPing-Ke Shih 
1967e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_MAC_LOOPBACK, mac_idx);
1968e3ec7017SPing-Ke Shih 	rtw89_write32_clr(rtwdev, reg, B_AX_MACLBK_EN);
1969e3ec7017SPing-Ke Shih 
197075fd91aaSPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_TCR0, mac_idx);
197175fd91aaSPing-Ke Shih 	rtw89_write32_mask(rtwdev, reg, B_AX_TCR_UDF_THSD_MASK, TCR_UDF_THSD);
197275fd91aaSPing-Ke Shih 
197375fd91aaSPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_TXD_FIFO_CTRL, mac_idx);
197475fd91aaSPing-Ke Shih 	rtw89_write32_mask(rtwdev, reg, B_AX_TXDFIFO_HIGH_MCS_THRE_MASK, TXDFIFO_HIGH_MCS_THRE);
197575fd91aaSPing-Ke Shih 	rtw89_write32_mask(rtwdev, reg, B_AX_TXDFIFO_LOW_MCS_THRE_MASK, TXDFIFO_LOW_MCS_THRE);
197675fd91aaSPing-Ke Shih 
1977e3ec7017SPing-Ke Shih 	return 0;
1978e3ec7017SPing-Ke Shih }
1979e3ec7017SPing-Ke Shih 
1980e3ec7017SPing-Ke Shih static int trxptcl_init(struct rtw89_dev *rtwdev, u8 mac_idx)
1981e3ec7017SPing-Ke Shih {
19829ef9edb9SChia-Yuan Li 	const struct rtw89_chip_info *chip = rtwdev->chip;
19839ef9edb9SChia-Yuan Li 	const struct rtw89_rrsr_cfgs *rrsr = chip->rrsr_cfgs;
1984e3ec7017SPing-Ke Shih 	u32 reg, val, sifs;
1985e3ec7017SPing-Ke Shih 	int ret;
1986e3ec7017SPing-Ke Shih 
1987e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL);
1988e3ec7017SPing-Ke Shih 	if (ret)
1989e3ec7017SPing-Ke Shih 		return ret;
1990e3ec7017SPing-Ke Shih 
1991e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_TRXPTCL_RESP_0, mac_idx);
1992e3ec7017SPing-Ke Shih 	val = rtw89_read32(rtwdev, reg);
1993e3ec7017SPing-Ke Shih 	val &= ~B_AX_WMAC_SPEC_SIFS_CCK_MASK;
1994e3ec7017SPing-Ke Shih 	val |= FIELD_PREP(B_AX_WMAC_SPEC_SIFS_CCK_MASK, WMAC_SPEC_SIFS_CCK);
1995e3ec7017SPing-Ke Shih 
1996e3ec7017SPing-Ke Shih 	switch (rtwdev->chip->chip_id) {
1997e3ec7017SPing-Ke Shih 	case RTL8852A:
1998e3ec7017SPing-Ke Shih 		sifs = WMAC_SPEC_SIFS_OFDM_52A;
1999e3ec7017SPing-Ke Shih 		break;
2000e3ec7017SPing-Ke Shih 	case RTL8852B:
2001e3ec7017SPing-Ke Shih 		sifs = WMAC_SPEC_SIFS_OFDM_52B;
2002e3ec7017SPing-Ke Shih 		break;
2003e3ec7017SPing-Ke Shih 	default:
2004e3ec7017SPing-Ke Shih 		sifs = WMAC_SPEC_SIFS_OFDM_52C;
2005e3ec7017SPing-Ke Shih 		break;
2006e3ec7017SPing-Ke Shih 	}
2007e3ec7017SPing-Ke Shih 	val &= ~B_AX_WMAC_SPEC_SIFS_OFDM_MASK;
2008e3ec7017SPing-Ke Shih 	val |= FIELD_PREP(B_AX_WMAC_SPEC_SIFS_OFDM_MASK, sifs);
2009e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, reg, val);
2010e3ec7017SPing-Ke Shih 
2011e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_RXTRIG_TEST_USER_2, mac_idx);
2012e3ec7017SPing-Ke Shih 	rtw89_write32_set(rtwdev, reg, B_AX_RXTRIG_FCSCHK_EN);
2013e3ec7017SPing-Ke Shih 
20149ef9edb9SChia-Yuan Li 	reg = rtw89_mac_reg_by_idx(rrsr->ref_rate.addr, mac_idx);
20159ef9edb9SChia-Yuan Li 	rtw89_write32_mask(rtwdev, reg, rrsr->ref_rate.mask, rrsr->ref_rate.data);
20169ef9edb9SChia-Yuan Li 	reg = rtw89_mac_reg_by_idx(rrsr->rsc.addr, mac_idx);
20179ef9edb9SChia-Yuan Li 	rtw89_write32_mask(rtwdev, reg, rrsr->rsc.mask, rrsr->rsc.data);
20189ef9edb9SChia-Yuan Li 
2019e3ec7017SPing-Ke Shih 	return 0;
2020e3ec7017SPing-Ke Shih }
2021e3ec7017SPing-Ke Shih 
202218175197SPing-Ke Shih static void rst_bacam(struct rtw89_dev *rtwdev)
202318175197SPing-Ke Shih {
202418175197SPing-Ke Shih 	u32 val32;
202518175197SPing-Ke Shih 	int ret;
202618175197SPing-Ke Shih 
202718175197SPing-Ke Shih 	rtw89_write32_mask(rtwdev, R_AX_RESPBA_CAM_CTRL, B_AX_BACAM_RST_MASK,
202818175197SPing-Ke Shih 			   S_AX_BACAM_RST_ALL);
202918175197SPing-Ke Shih 
203018175197SPing-Ke Shih 	ret = read_poll_timeout_atomic(rtw89_read32_mask, val32, val32 == 0,
203118175197SPing-Ke Shih 				       1, 1000, false,
203218175197SPing-Ke Shih 				       rtwdev, R_AX_RESPBA_CAM_CTRL, B_AX_BACAM_RST_MASK);
203318175197SPing-Ke Shih 	if (ret)
203418175197SPing-Ke Shih 		rtw89_warn(rtwdev, "failed to reset BA CAM\n");
203518175197SPing-Ke Shih }
203618175197SPing-Ke Shih 
2037e3ec7017SPing-Ke Shih static int rmac_init(struct rtw89_dev *rtwdev, u8 mac_idx)
2038e3ec7017SPing-Ke Shih {
2039e3ec7017SPing-Ke Shih #define TRXCFG_RMAC_CCA_TO	32
2040e3ec7017SPing-Ke Shih #define TRXCFG_RMAC_DATA_TO	15
2041e3ec7017SPing-Ke Shih #define RX_MAX_LEN_UNIT 512
2042e3ec7017SPing-Ke Shih #define PLD_RLS_MAX_PG 127
20434b0d341bSPing-Ke Shih #define RX_SPEC_MAX_LEN (11454 + RX_MAX_LEN_UNIT)
2044e3ec7017SPing-Ke Shih 	int ret;
2045e3ec7017SPing-Ke Shih 	u32 reg, rx_max_len, rx_qta;
2046e3ec7017SPing-Ke Shih 	u16 val;
2047e3ec7017SPing-Ke Shih 
2048e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL);
2049e3ec7017SPing-Ke Shih 	if (ret)
2050e3ec7017SPing-Ke Shih 		return ret;
2051e3ec7017SPing-Ke Shih 
205218175197SPing-Ke Shih 	if (mac_idx == RTW89_MAC_0)
205318175197SPing-Ke Shih 		rst_bacam(rtwdev);
205418175197SPing-Ke Shih 
2055e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_RESPBA_CAM_CTRL, mac_idx);
2056e3ec7017SPing-Ke Shih 	rtw89_write8_set(rtwdev, reg, B_AX_SSN_SEL);
2057e3ec7017SPing-Ke Shih 
2058e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_DLK_PROTECT_CTL, mac_idx);
2059e3ec7017SPing-Ke Shih 	val = rtw89_read16(rtwdev, reg);
2060e3ec7017SPing-Ke Shih 	val = u16_replace_bits(val, TRXCFG_RMAC_DATA_TO,
2061e3ec7017SPing-Ke Shih 			       B_AX_RX_DLK_DATA_TIME_MASK);
2062e3ec7017SPing-Ke Shih 	val = u16_replace_bits(val, TRXCFG_RMAC_CCA_TO,
2063e3ec7017SPing-Ke Shih 			       B_AX_RX_DLK_CCA_TIME_MASK);
2064e3ec7017SPing-Ke Shih 	rtw89_write16(rtwdev, reg, val);
2065e3ec7017SPing-Ke Shih 
2066e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_RCR, mac_idx);
2067e3ec7017SPing-Ke Shih 	rtw89_write8_mask(rtwdev, reg, B_AX_CH_EN_MASK, 0x1);
2068e3ec7017SPing-Ke Shih 
2069e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_RX_FLTR_OPT, mac_idx);
2070e3ec7017SPing-Ke Shih 	if (mac_idx == RTW89_MAC_0)
2071e3ec7017SPing-Ke Shih 		rx_qta = rtwdev->mac.dle_info.c0_rx_qta;
2072e3ec7017SPing-Ke Shih 	else
2073e3ec7017SPing-Ke Shih 		rx_qta = rtwdev->mac.dle_info.c1_rx_qta;
20744b0d341bSPing-Ke Shih 	rx_qta = min_t(u32, rx_qta, PLD_RLS_MAX_PG);
20754b0d341bSPing-Ke Shih 	rx_max_len = rx_qta * rtwdev->mac.dle_info.ple_pg_size;
20764b0d341bSPing-Ke Shih 	rx_max_len = min_t(u32, rx_max_len, RX_SPEC_MAX_LEN);
20774b0d341bSPing-Ke Shih 	rx_max_len /= RX_MAX_LEN_UNIT;
2078e3ec7017SPing-Ke Shih 	rtw89_write32_mask(rtwdev, reg, B_AX_RX_MPDU_MAX_LEN_MASK, rx_max_len);
2079e3ec7017SPing-Ke Shih 
2080e3ec7017SPing-Ke Shih 	if (rtwdev->chip->chip_id == RTL8852A &&
2081e3ec7017SPing-Ke Shih 	    rtwdev->hal.cv == CHIP_CBV) {
2082e3ec7017SPing-Ke Shih 		rtw89_write16_mask(rtwdev,
2083e3ec7017SPing-Ke Shih 				   rtw89_mac_reg_by_idx(R_AX_DLK_PROTECT_CTL, mac_idx),
2084e3ec7017SPing-Ke Shih 				   B_AX_RX_DLK_CCA_TIME_MASK, 0);
2085e3ec7017SPing-Ke Shih 		rtw89_write16_set(rtwdev, rtw89_mac_reg_by_idx(R_AX_RCR, mac_idx),
2086e3ec7017SPing-Ke Shih 				  BIT(12));
2087e3ec7017SPing-Ke Shih 	}
2088e3ec7017SPing-Ke Shih 
2089e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_PLCP_HDR_FLTR, mac_idx);
2090e3ec7017SPing-Ke Shih 	rtw89_write8_clr(rtwdev, reg, B_AX_VHT_SU_SIGB_CRC_CHK);
2091e3ec7017SPing-Ke Shih 
2092e3ec7017SPing-Ke Shih 	return ret;
2093e3ec7017SPing-Ke Shih }
2094e3ec7017SPing-Ke Shih 
2095e3ec7017SPing-Ke Shih static int cmac_com_init(struct rtw89_dev *rtwdev, u8 mac_idx)
2096e3ec7017SPing-Ke Shih {
20979ef9edb9SChia-Yuan Li 	enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
2098e3ec7017SPing-Ke Shih 	u32 val, reg;
2099e3ec7017SPing-Ke Shih 	int ret;
2100e3ec7017SPing-Ke Shih 
2101e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL);
2102e3ec7017SPing-Ke Shih 	if (ret)
2103e3ec7017SPing-Ke Shih 		return ret;
2104e3ec7017SPing-Ke Shih 
2105e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_TX_SUB_CARRIER_VALUE, mac_idx);
2106e3ec7017SPing-Ke Shih 	val = rtw89_read32(rtwdev, reg);
2107e3ec7017SPing-Ke Shih 	val = u32_replace_bits(val, 0, B_AX_TXSC_20M_MASK);
2108e3ec7017SPing-Ke Shih 	val = u32_replace_bits(val, 0, B_AX_TXSC_40M_MASK);
2109e3ec7017SPing-Ke Shih 	val = u32_replace_bits(val, 0, B_AX_TXSC_80M_MASK);
2110e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, reg, val);
2111e3ec7017SPing-Ke Shih 
21129ef9edb9SChia-Yuan Li 	if (chip_id == RTL8852A || chip_id == RTL8852B) {
21139ef9edb9SChia-Yuan Li 		reg = rtw89_mac_reg_by_idx(R_AX_PTCL_RRSR1, mac_idx);
21149ef9edb9SChia-Yuan Li 		rtw89_write32_mask(rtwdev, reg, B_AX_RRSR_RATE_EN_MASK, RRSR_OFDM_CCK_EN);
21159ef9edb9SChia-Yuan Li 	}
21169ef9edb9SChia-Yuan Li 
2117e3ec7017SPing-Ke Shih 	return 0;
2118e3ec7017SPing-Ke Shih }
2119e3ec7017SPing-Ke Shih 
2120e3ec7017SPing-Ke Shih static bool is_qta_dbcc(struct rtw89_dev *rtwdev, enum rtw89_qta_mode mode)
2121e3ec7017SPing-Ke Shih {
2122e3ec7017SPing-Ke Shih 	const struct rtw89_dle_mem *cfg;
2123e3ec7017SPing-Ke Shih 
2124e3ec7017SPing-Ke Shih 	cfg = get_dle_mem_cfg(rtwdev, mode);
2125e3ec7017SPing-Ke Shih 	if (!cfg) {
2126e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]get_dle_mem_cfg\n");
2127e3ec7017SPing-Ke Shih 		return false;
2128e3ec7017SPing-Ke Shih 	}
2129e3ec7017SPing-Ke Shih 
2130e3ec7017SPing-Ke Shih 	return (cfg->ple_min_qt->cma1_dma && cfg->ple_max_qt->cma1_dma);
2131e3ec7017SPing-Ke Shih }
2132e3ec7017SPing-Ke Shih 
2133e3ec7017SPing-Ke Shih static int ptcl_init(struct rtw89_dev *rtwdev, u8 mac_idx)
2134e3ec7017SPing-Ke Shih {
2135e3ec7017SPing-Ke Shih 	u32 val, reg;
2136e3ec7017SPing-Ke Shih 	int ret;
2137e3ec7017SPing-Ke Shih 
2138e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL);
2139e3ec7017SPing-Ke Shih 	if (ret)
2140e3ec7017SPing-Ke Shih 		return ret;
2141e3ec7017SPing-Ke Shih 
2142e3ec7017SPing-Ke Shih 	if (rtwdev->hci.type == RTW89_HCI_TYPE_PCIE) {
2143e3ec7017SPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(R_AX_SIFS_SETTING, mac_idx);
2144e3ec7017SPing-Ke Shih 		val = rtw89_read32(rtwdev, reg);
2145e3ec7017SPing-Ke Shih 		val = u32_replace_bits(val, S_AX_CTS2S_TH_1K,
2146e3ec7017SPing-Ke Shih 				       B_AX_HW_CTS2SELF_PKT_LEN_TH_MASK);
21479fb4862eSPing-Ke Shih 		val = u32_replace_bits(val, S_AX_CTS2S_TH_SEC_256B,
21489fb4862eSPing-Ke Shih 				       B_AX_HW_CTS2SELF_PKT_LEN_TH_TWW_MASK);
2149e3ec7017SPing-Ke Shih 		val |= B_AX_HW_CTS2SELF_EN;
2150e3ec7017SPing-Ke Shih 		rtw89_write32(rtwdev, reg, val);
2151e3ec7017SPing-Ke Shih 
2152e3ec7017SPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(R_AX_PTCL_FSM_MON, mac_idx);
2153e3ec7017SPing-Ke Shih 		val = rtw89_read32(rtwdev, reg);
2154e3ec7017SPing-Ke Shih 		val = u32_replace_bits(val, S_AX_PTCL_TO_2MS, B_AX_PTCL_TX_ARB_TO_THR_MASK);
2155e3ec7017SPing-Ke Shih 		val &= ~B_AX_PTCL_TX_ARB_TO_MODE;
2156e3ec7017SPing-Ke Shih 		rtw89_write32(rtwdev, reg, val);
2157e3ec7017SPing-Ke Shih 	}
2158e3ec7017SPing-Ke Shih 
21599fb4862eSPing-Ke Shih 	if (mac_idx == RTW89_MAC_0) {
21609fb4862eSPing-Ke Shih 		rtw89_write8_set(rtwdev, R_AX_PTCL_COMMON_SETTING_0,
21619fb4862eSPing-Ke Shih 				 B_AX_CMAC_TX_MODE_0 | B_AX_CMAC_TX_MODE_1);
21629fb4862eSPing-Ke Shih 		rtw89_write8_clr(rtwdev, R_AX_PTCL_COMMON_SETTING_0,
21639fb4862eSPing-Ke Shih 				 B_AX_PTCL_TRIGGER_SS_EN_0 |
21649fb4862eSPing-Ke Shih 				 B_AX_PTCL_TRIGGER_SS_EN_1 |
21659fb4862eSPing-Ke Shih 				 B_AX_PTCL_TRIGGER_SS_EN_UL);
21669fb4862eSPing-Ke Shih 		rtw89_write8_mask(rtwdev, R_AX_PTCLRPT_FULL_HDL,
21679fb4862eSPing-Ke Shih 				  B_AX_SPE_RPT_PATH_MASK, FWD_TO_WLCPU);
21689fb4862eSPing-Ke Shih 	} else if (mac_idx == RTW89_MAC_1) {
21699fb4862eSPing-Ke Shih 		rtw89_write8_mask(rtwdev, R_AX_PTCLRPT_FULL_HDL_C1,
21709fb4862eSPing-Ke Shih 				  B_AX_SPE_RPT_PATH_MASK, FWD_TO_WLCPU);
21719fb4862eSPing-Ke Shih 	}
2172e3ec7017SPing-Ke Shih 
2173e3ec7017SPing-Ke Shih 	return 0;
2174e3ec7017SPing-Ke Shih }
2175e3ec7017SPing-Ke Shih 
2176a0d99ebbSPing-Ke Shih static int cmac_dma_init(struct rtw89_dev *rtwdev, u8 mac_idx)
2177a0d99ebbSPing-Ke Shih {
2178a0d99ebbSPing-Ke Shih 	enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
2179a0d99ebbSPing-Ke Shih 	u32 reg;
2180a0d99ebbSPing-Ke Shih 	int ret;
2181a0d99ebbSPing-Ke Shih 
2182a0d99ebbSPing-Ke Shih 	if (chip_id != RTL8852A && chip_id != RTL8852B)
2183a0d99ebbSPing-Ke Shih 		return 0;
2184a0d99ebbSPing-Ke Shih 
2185a0d99ebbSPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL);
2186a0d99ebbSPing-Ke Shih 	if (ret)
2187a0d99ebbSPing-Ke Shih 		return ret;
2188a0d99ebbSPing-Ke Shih 
2189a0d99ebbSPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_RXDMA_CTRL_0, mac_idx);
2190a0d99ebbSPing-Ke Shih 	rtw89_write8_clr(rtwdev, reg, RX_FULL_MODE);
2191a0d99ebbSPing-Ke Shih 
2192a0d99ebbSPing-Ke Shih 	return 0;
2193a0d99ebbSPing-Ke Shih }
2194a0d99ebbSPing-Ke Shih 
2195e3ec7017SPing-Ke Shih static int cmac_init(struct rtw89_dev *rtwdev, u8 mac_idx)
2196e3ec7017SPing-Ke Shih {
2197e3ec7017SPing-Ke Shih 	int ret;
2198e3ec7017SPing-Ke Shih 
2199e3ec7017SPing-Ke Shih 	ret = scheduler_init(rtwdev, mac_idx);
2200e3ec7017SPing-Ke Shih 	if (ret) {
2201e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]CMAC%d SCH init %d\n", mac_idx, ret);
2202e3ec7017SPing-Ke Shih 		return ret;
2203e3ec7017SPing-Ke Shih 	}
2204e3ec7017SPing-Ke Shih 
2205e3ec7017SPing-Ke Shih 	ret = addr_cam_init(rtwdev, mac_idx);
2206e3ec7017SPing-Ke Shih 	if (ret) {
2207e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]CMAC%d ADDR_CAM reset %d\n", mac_idx,
2208e3ec7017SPing-Ke Shih 			  ret);
2209e3ec7017SPing-Ke Shih 		return ret;
2210e3ec7017SPing-Ke Shih 	}
2211e3ec7017SPing-Ke Shih 
2212e3ec7017SPing-Ke Shih 	ret = rx_fltr_init(rtwdev, mac_idx);
2213e3ec7017SPing-Ke Shih 	if (ret) {
2214e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]CMAC%d RX filter init %d\n", mac_idx,
2215e3ec7017SPing-Ke Shih 			  ret);
2216e3ec7017SPing-Ke Shih 		return ret;
2217e3ec7017SPing-Ke Shih 	}
2218e3ec7017SPing-Ke Shih 
2219e3ec7017SPing-Ke Shih 	ret = cca_ctrl_init(rtwdev, mac_idx);
2220e3ec7017SPing-Ke Shih 	if (ret) {
2221e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]CMAC%d CCA CTRL init %d\n", mac_idx,
2222e3ec7017SPing-Ke Shih 			  ret);
2223e3ec7017SPing-Ke Shih 		return ret;
2224e3ec7017SPing-Ke Shih 	}
2225e3ec7017SPing-Ke Shih 
222619cb9427SPing-Ke Shih 	ret = nav_ctrl_init(rtwdev);
222719cb9427SPing-Ke Shih 	if (ret) {
222819cb9427SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]CMAC%d NAV CTRL init %d\n", mac_idx,
222919cb9427SPing-Ke Shih 			  ret);
223019cb9427SPing-Ke Shih 		return ret;
223119cb9427SPing-Ke Shih 	}
223219cb9427SPing-Ke Shih 
2233e3ec7017SPing-Ke Shih 	ret = spatial_reuse_init(rtwdev, mac_idx);
2234e3ec7017SPing-Ke Shih 	if (ret) {
2235e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]CMAC%d Spatial Reuse init %d\n",
2236e3ec7017SPing-Ke Shih 			  mac_idx, ret);
2237e3ec7017SPing-Ke Shih 		return ret;
2238e3ec7017SPing-Ke Shih 	}
2239e3ec7017SPing-Ke Shih 
2240e3ec7017SPing-Ke Shih 	ret = tmac_init(rtwdev, mac_idx);
2241e3ec7017SPing-Ke Shih 	if (ret) {
2242e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]CMAC%d TMAC init %d\n", mac_idx, ret);
2243e3ec7017SPing-Ke Shih 		return ret;
2244e3ec7017SPing-Ke Shih 	}
2245e3ec7017SPing-Ke Shih 
2246e3ec7017SPing-Ke Shih 	ret = trxptcl_init(rtwdev, mac_idx);
2247e3ec7017SPing-Ke Shih 	if (ret) {
2248e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]CMAC%d TRXPTCL init %d\n", mac_idx, ret);
2249e3ec7017SPing-Ke Shih 		return ret;
2250e3ec7017SPing-Ke Shih 	}
2251e3ec7017SPing-Ke Shih 
2252e3ec7017SPing-Ke Shih 	ret = rmac_init(rtwdev, mac_idx);
2253e3ec7017SPing-Ke Shih 	if (ret) {
2254e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]CMAC%d RMAC init %d\n", mac_idx, ret);
2255e3ec7017SPing-Ke Shih 		return ret;
2256e3ec7017SPing-Ke Shih 	}
2257e3ec7017SPing-Ke Shih 
2258e3ec7017SPing-Ke Shih 	ret = cmac_com_init(rtwdev, mac_idx);
2259e3ec7017SPing-Ke Shih 	if (ret) {
2260e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]CMAC%d Com init %d\n", mac_idx, ret);
2261e3ec7017SPing-Ke Shih 		return ret;
2262e3ec7017SPing-Ke Shih 	}
2263e3ec7017SPing-Ke Shih 
2264e3ec7017SPing-Ke Shih 	ret = ptcl_init(rtwdev, mac_idx);
2265e3ec7017SPing-Ke Shih 	if (ret) {
2266e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]CMAC%d PTCL init %d\n", mac_idx, ret);
2267e3ec7017SPing-Ke Shih 		return ret;
2268e3ec7017SPing-Ke Shih 	}
2269e3ec7017SPing-Ke Shih 
2270a0d99ebbSPing-Ke Shih 	ret = cmac_dma_init(rtwdev, mac_idx);
2271a0d99ebbSPing-Ke Shih 	if (ret) {
2272a0d99ebbSPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]CMAC%d DMA init %d\n", mac_idx, ret);
2273a0d99ebbSPing-Ke Shih 		return ret;
2274a0d99ebbSPing-Ke Shih 	}
2275a0d99ebbSPing-Ke Shih 
2276e3ec7017SPing-Ke Shih 	return ret;
2277e3ec7017SPing-Ke Shih }
2278e3ec7017SPing-Ke Shih 
2279e3ec7017SPing-Ke Shih static int rtw89_mac_read_phycap(struct rtw89_dev *rtwdev,
2280e3ec7017SPing-Ke Shih 				 struct rtw89_mac_c2h_info *c2h_info)
2281e3ec7017SPing-Ke Shih {
2282e3ec7017SPing-Ke Shih 	struct rtw89_mac_h2c_info h2c_info = {0};
2283e3ec7017SPing-Ke Shih 	u32 ret;
2284e3ec7017SPing-Ke Shih 
2285e3ec7017SPing-Ke Shih 	h2c_info.id = RTW89_FWCMD_H2CREG_FUNC_GET_FEATURE;
2286e3ec7017SPing-Ke Shih 	h2c_info.content_len = 0;
2287e3ec7017SPing-Ke Shih 
2288e3ec7017SPing-Ke Shih 	ret = rtw89_fw_msg_reg(rtwdev, &h2c_info, c2h_info);
2289e3ec7017SPing-Ke Shih 	if (ret)
2290e3ec7017SPing-Ke Shih 		return ret;
2291e3ec7017SPing-Ke Shih 
2292e3ec7017SPing-Ke Shih 	if (c2h_info->id != RTW89_FWCMD_C2HREG_FUNC_PHY_CAP)
2293e3ec7017SPing-Ke Shih 		return -EINVAL;
2294e3ec7017SPing-Ke Shih 
2295e3ec7017SPing-Ke Shih 	return 0;
2296e3ec7017SPing-Ke Shih }
2297e3ec7017SPing-Ke Shih 
2298e3ec7017SPing-Ke Shih int rtw89_mac_setup_phycap(struct rtw89_dev *rtwdev)
2299e3ec7017SPing-Ke Shih {
2300e3ec7017SPing-Ke Shih 	struct rtw89_hal *hal = &rtwdev->hal;
2301e3ec7017SPing-Ke Shih 	const struct rtw89_chip_info *chip = rtwdev->chip;
2302e3ec7017SPing-Ke Shih 	struct rtw89_mac_c2h_info c2h_info = {0};
2303c7ad08c6SPing-Ke Shih 	u8 tx_nss;
2304c7ad08c6SPing-Ke Shih 	u8 rx_nss;
2305dc229d94SPing-Ke Shih 	u8 tx_ant;
2306dc229d94SPing-Ke Shih 	u8 rx_ant;
2307e3ec7017SPing-Ke Shih 	u32 ret;
2308e3ec7017SPing-Ke Shih 
2309e3ec7017SPing-Ke Shih 	ret = rtw89_mac_read_phycap(rtwdev, &c2h_info);
2310e3ec7017SPing-Ke Shih 	if (ret)
2311e3ec7017SPing-Ke Shih 		return ret;
2312e3ec7017SPing-Ke Shih 
2313c7ad08c6SPing-Ke Shih 	tx_nss = RTW89_GET_C2H_PHYCAP_TX_NSS(c2h_info.c2hreg);
2314c7ad08c6SPing-Ke Shih 	rx_nss = RTW89_GET_C2H_PHYCAP_RX_NSS(c2h_info.c2hreg);
2315dc229d94SPing-Ke Shih 	tx_ant = RTW89_GET_C2H_PHYCAP_ANT_TX_NUM(c2h_info.c2hreg);
2316dc229d94SPing-Ke Shih 	rx_ant = RTW89_GET_C2H_PHYCAP_ANT_RX_NUM(c2h_info.c2hreg);
2317c7ad08c6SPing-Ke Shih 
2318c7ad08c6SPing-Ke Shih 	hal->tx_nss = tx_nss ? min_t(u8, tx_nss, chip->tx_nss) : chip->tx_nss;
2319c7ad08c6SPing-Ke Shih 	hal->rx_nss = rx_nss ? min_t(u8, rx_nss, chip->rx_nss) : chip->rx_nss;
2320e3ec7017SPing-Ke Shih 
2321dc229d94SPing-Ke Shih 	if (tx_ant == 1)
2322dc229d94SPing-Ke Shih 		hal->antenna_tx = RF_B;
2323dc229d94SPing-Ke Shih 	if (rx_ant == 1)
2324dc229d94SPing-Ke Shih 		hal->antenna_rx = RF_B;
2325dc229d94SPing-Ke Shih 
2326dc229d94SPing-Ke Shih 	if (tx_nss == 1 && tx_ant == 2 && rx_ant == 2) {
2327dc229d94SPing-Ke Shih 		hal->antenna_tx = RF_B;
2328dc229d94SPing-Ke Shih 		hal->tx_path_diversity = true;
2329dc229d94SPing-Ke Shih 	}
2330dc229d94SPing-Ke Shih 
2331e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_FW,
2332e3ec7017SPing-Ke Shih 		    "phycap hal/phy/chip: tx_nss=0x%x/0x%x/0x%x rx_nss=0x%x/0x%x/0x%x\n",
2333c7ad08c6SPing-Ke Shih 		    hal->tx_nss, tx_nss, chip->tx_nss,
2334c7ad08c6SPing-Ke Shih 		    hal->rx_nss, rx_nss, chip->rx_nss);
2335dc229d94SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_FW,
2336dc229d94SPing-Ke Shih 		    "ant num/bitmap: tx=%d/0x%x rx=%d/0x%x\n",
2337dc229d94SPing-Ke Shih 		    tx_ant, hal->antenna_tx, rx_ant, hal->antenna_rx);
2338dc229d94SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_FW, "TX path diversity=%d\n", hal->tx_path_diversity);
2339e3ec7017SPing-Ke Shih 
2340e3ec7017SPing-Ke Shih 	return 0;
2341e3ec7017SPing-Ke Shih }
2342e3ec7017SPing-Ke Shih 
2343e3ec7017SPing-Ke Shih static int rtw89_hw_sch_tx_en_h2c(struct rtw89_dev *rtwdev, u8 band,
2344e3ec7017SPing-Ke Shih 				  u16 tx_en_u16, u16 mask_u16)
2345e3ec7017SPing-Ke Shih {
2346e3ec7017SPing-Ke Shih 	u32 ret;
2347e3ec7017SPing-Ke Shih 	struct rtw89_mac_c2h_info c2h_info = {0};
2348e3ec7017SPing-Ke Shih 	struct rtw89_mac_h2c_info h2c_info = {0};
2349e3ec7017SPing-Ke Shih 	struct rtw89_h2creg_sch_tx_en *h2creg =
2350e3ec7017SPing-Ke Shih 		(struct rtw89_h2creg_sch_tx_en *)h2c_info.h2creg;
2351e3ec7017SPing-Ke Shih 
2352e3ec7017SPing-Ke Shih 	h2c_info.id = RTW89_FWCMD_H2CREG_FUNC_SCH_TX_EN;
2353e3ec7017SPing-Ke Shih 	h2c_info.content_len = sizeof(*h2creg) - RTW89_H2CREG_HDR_LEN;
2354e3ec7017SPing-Ke Shih 	h2creg->tx_en = tx_en_u16;
2355e3ec7017SPing-Ke Shih 	h2creg->mask = mask_u16;
2356e3ec7017SPing-Ke Shih 	h2creg->band = band;
2357e3ec7017SPing-Ke Shih 
2358e3ec7017SPing-Ke Shih 	ret = rtw89_fw_msg_reg(rtwdev, &h2c_info, &c2h_info);
2359e3ec7017SPing-Ke Shih 	if (ret)
2360e3ec7017SPing-Ke Shih 		return ret;
2361e3ec7017SPing-Ke Shih 
2362e3ec7017SPing-Ke Shih 	if (c2h_info.id != RTW89_FWCMD_C2HREG_FUNC_TX_PAUSE_RPT)
2363e3ec7017SPing-Ke Shih 		return -EINVAL;
2364e3ec7017SPing-Ke Shih 
2365e3ec7017SPing-Ke Shih 	return 0;
2366e3ec7017SPing-Ke Shih }
2367e3ec7017SPing-Ke Shih 
2368e3ec7017SPing-Ke Shih static int rtw89_set_hw_sch_tx_en(struct rtw89_dev *rtwdev, u8 mac_idx,
2369e3ec7017SPing-Ke Shih 				  u16 tx_en, u16 tx_en_mask)
2370e3ec7017SPing-Ke Shih {
2371e3ec7017SPing-Ke Shih 	u32 reg = rtw89_mac_reg_by_idx(R_AX_CTN_TXEN, mac_idx);
2372e3ec7017SPing-Ke Shih 	u16 val;
2373e3ec7017SPing-Ke Shih 	int ret;
2374e3ec7017SPing-Ke Shih 
2375e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL);
2376e3ec7017SPing-Ke Shih 	if (ret)
2377e3ec7017SPing-Ke Shih 		return ret;
2378e3ec7017SPing-Ke Shih 
2379e3ec7017SPing-Ke Shih 	if (test_bit(RTW89_FLAG_FW_RDY, rtwdev->flags))
2380e3ec7017SPing-Ke Shih 		return rtw89_hw_sch_tx_en_h2c(rtwdev, mac_idx,
2381e3ec7017SPing-Ke Shih 					      tx_en, tx_en_mask);
2382e3ec7017SPing-Ke Shih 
2383e3ec7017SPing-Ke Shih 	val = rtw89_read16(rtwdev, reg);
2384e3ec7017SPing-Ke Shih 	val = (val & ~tx_en_mask) | (tx_en & tx_en_mask);
2385e3ec7017SPing-Ke Shih 	rtw89_write16(rtwdev, reg, val);
2386e3ec7017SPing-Ke Shih 
2387e3ec7017SPing-Ke Shih 	return 0;
2388e3ec7017SPing-Ke Shih }
2389e3ec7017SPing-Ke Shih 
2390de7ba639SPing-Ke Shih static int rtw89_set_hw_sch_tx_en_v1(struct rtw89_dev *rtwdev, u8 mac_idx,
2391de7ba639SPing-Ke Shih 				     u32 tx_en, u32 tx_en_mask)
2392de7ba639SPing-Ke Shih {
2393de7ba639SPing-Ke Shih 	u32 reg = rtw89_mac_reg_by_idx(R_AX_CTN_DRV_TXEN, mac_idx);
2394de7ba639SPing-Ke Shih 	u32 val;
2395de7ba639SPing-Ke Shih 	int ret;
2396de7ba639SPing-Ke Shih 
2397de7ba639SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL);
2398de7ba639SPing-Ke Shih 	if (ret)
2399de7ba639SPing-Ke Shih 		return ret;
2400de7ba639SPing-Ke Shih 
2401de7ba639SPing-Ke Shih 	val = rtw89_read32(rtwdev, reg);
2402de7ba639SPing-Ke Shih 	val = (val & ~tx_en_mask) | (tx_en & tx_en_mask);
2403de7ba639SPing-Ke Shih 	rtw89_write32(rtwdev, reg, val);
2404de7ba639SPing-Ke Shih 
2405de7ba639SPing-Ke Shih 	return 0;
2406de7ba639SPing-Ke Shih }
2407de7ba639SPing-Ke Shih 
2408e3ec7017SPing-Ke Shih int rtw89_mac_stop_sch_tx(struct rtw89_dev *rtwdev, u8 mac_idx,
2409d780f926SPing-Ke Shih 			  u32 *tx_en, enum rtw89_sch_tx_sel sel)
2410e3ec7017SPing-Ke Shih {
2411e3ec7017SPing-Ke Shih 	int ret;
2412e3ec7017SPing-Ke Shih 
2413e3ec7017SPing-Ke Shih 	*tx_en = rtw89_read16(rtwdev,
2414e3ec7017SPing-Ke Shih 			      rtw89_mac_reg_by_idx(R_AX_CTN_TXEN, mac_idx));
2415e3ec7017SPing-Ke Shih 
2416e3ec7017SPing-Ke Shih 	switch (sel) {
2417e3ec7017SPing-Ke Shih 	case RTW89_SCH_TX_SEL_ALL:
2418de7ba639SPing-Ke Shih 		ret = rtw89_set_hw_sch_tx_en(rtwdev, mac_idx, 0,
2419de7ba639SPing-Ke Shih 					     B_AX_CTN_TXEN_ALL_MASK);
2420e3ec7017SPing-Ke Shih 		if (ret)
2421e3ec7017SPing-Ke Shih 			return ret;
2422e3ec7017SPing-Ke Shih 		break;
2423e3ec7017SPing-Ke Shih 	case RTW89_SCH_TX_SEL_HIQ:
2424e3ec7017SPing-Ke Shih 		ret = rtw89_set_hw_sch_tx_en(rtwdev, mac_idx,
2425e3ec7017SPing-Ke Shih 					     0, B_AX_CTN_TXEN_HGQ);
2426e3ec7017SPing-Ke Shih 		if (ret)
2427e3ec7017SPing-Ke Shih 			return ret;
2428e3ec7017SPing-Ke Shih 		break;
2429e3ec7017SPing-Ke Shih 	case RTW89_SCH_TX_SEL_MG0:
2430e3ec7017SPing-Ke Shih 		ret = rtw89_set_hw_sch_tx_en(rtwdev, mac_idx,
2431e3ec7017SPing-Ke Shih 					     0, B_AX_CTN_TXEN_MGQ);
2432e3ec7017SPing-Ke Shih 		if (ret)
2433e3ec7017SPing-Ke Shih 			return ret;
2434e3ec7017SPing-Ke Shih 		break;
2435e3ec7017SPing-Ke Shih 	case RTW89_SCH_TX_SEL_MACID:
2436de7ba639SPing-Ke Shih 		ret = rtw89_set_hw_sch_tx_en(rtwdev, mac_idx, 0,
2437de7ba639SPing-Ke Shih 					     B_AX_CTN_TXEN_ALL_MASK);
2438e3ec7017SPing-Ke Shih 		if (ret)
2439e3ec7017SPing-Ke Shih 			return ret;
2440e3ec7017SPing-Ke Shih 		break;
2441e3ec7017SPing-Ke Shih 	default:
2442e3ec7017SPing-Ke Shih 		return 0;
2443e3ec7017SPing-Ke Shih 	}
2444e3ec7017SPing-Ke Shih 
2445e3ec7017SPing-Ke Shih 	return 0;
2446e3ec7017SPing-Ke Shih }
2447861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_mac_stop_sch_tx);
2448e3ec7017SPing-Ke Shih 
2449de7ba639SPing-Ke Shih int rtw89_mac_stop_sch_tx_v1(struct rtw89_dev *rtwdev, u8 mac_idx,
2450de7ba639SPing-Ke Shih 			     u32 *tx_en, enum rtw89_sch_tx_sel sel)
2451de7ba639SPing-Ke Shih {
2452de7ba639SPing-Ke Shih 	int ret;
2453de7ba639SPing-Ke Shih 
2454de7ba639SPing-Ke Shih 	*tx_en = rtw89_read32(rtwdev,
2455de7ba639SPing-Ke Shih 			      rtw89_mac_reg_by_idx(R_AX_CTN_DRV_TXEN, mac_idx));
2456de7ba639SPing-Ke Shih 
2457de7ba639SPing-Ke Shih 	switch (sel) {
2458de7ba639SPing-Ke Shih 	case RTW89_SCH_TX_SEL_ALL:
2459de7ba639SPing-Ke Shih 		ret = rtw89_set_hw_sch_tx_en_v1(rtwdev, mac_idx, 0,
2460de7ba639SPing-Ke Shih 						B_AX_CTN_TXEN_ALL_MASK_V1);
2461de7ba639SPing-Ke Shih 		if (ret)
2462de7ba639SPing-Ke Shih 			return ret;
2463de7ba639SPing-Ke Shih 		break;
2464de7ba639SPing-Ke Shih 	case RTW89_SCH_TX_SEL_HIQ:
2465de7ba639SPing-Ke Shih 		ret = rtw89_set_hw_sch_tx_en_v1(rtwdev, mac_idx,
2466de7ba639SPing-Ke Shih 						0, B_AX_CTN_TXEN_HGQ);
2467de7ba639SPing-Ke Shih 		if (ret)
2468de7ba639SPing-Ke Shih 			return ret;
2469de7ba639SPing-Ke Shih 		break;
2470de7ba639SPing-Ke Shih 	case RTW89_SCH_TX_SEL_MG0:
2471de7ba639SPing-Ke Shih 		ret = rtw89_set_hw_sch_tx_en_v1(rtwdev, mac_idx,
2472de7ba639SPing-Ke Shih 						0, B_AX_CTN_TXEN_MGQ);
2473de7ba639SPing-Ke Shih 		if (ret)
2474de7ba639SPing-Ke Shih 			return ret;
2475de7ba639SPing-Ke Shih 		break;
2476de7ba639SPing-Ke Shih 	case RTW89_SCH_TX_SEL_MACID:
2477de7ba639SPing-Ke Shih 		ret = rtw89_set_hw_sch_tx_en_v1(rtwdev, mac_idx, 0,
2478de7ba639SPing-Ke Shih 						B_AX_CTN_TXEN_ALL_MASK_V1);
2479de7ba639SPing-Ke Shih 		if (ret)
2480de7ba639SPing-Ke Shih 			return ret;
2481de7ba639SPing-Ke Shih 		break;
2482de7ba639SPing-Ke Shih 	default:
2483de7ba639SPing-Ke Shih 		return 0;
2484de7ba639SPing-Ke Shih 	}
2485de7ba639SPing-Ke Shih 
2486de7ba639SPing-Ke Shih 	return 0;
2487de7ba639SPing-Ke Shih }
2488de7ba639SPing-Ke Shih EXPORT_SYMBOL(rtw89_mac_stop_sch_tx_v1);
2489de7ba639SPing-Ke Shih 
2490d780f926SPing-Ke Shih int rtw89_mac_resume_sch_tx(struct rtw89_dev *rtwdev, u8 mac_idx, u32 tx_en)
2491e3ec7017SPing-Ke Shih {
2492e3ec7017SPing-Ke Shih 	int ret;
2493e3ec7017SPing-Ke Shih 
2494de7ba639SPing-Ke Shih 	ret = rtw89_set_hw_sch_tx_en(rtwdev, mac_idx, tx_en, B_AX_CTN_TXEN_ALL_MASK);
2495e3ec7017SPing-Ke Shih 	if (ret)
2496e3ec7017SPing-Ke Shih 		return ret;
2497e3ec7017SPing-Ke Shih 
2498e3ec7017SPing-Ke Shih 	return 0;
2499e3ec7017SPing-Ke Shih }
2500861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_mac_resume_sch_tx);
2501e3ec7017SPing-Ke Shih 
2502de7ba639SPing-Ke Shih int rtw89_mac_resume_sch_tx_v1(struct rtw89_dev *rtwdev, u8 mac_idx, u32 tx_en)
2503de7ba639SPing-Ke Shih {
2504de7ba639SPing-Ke Shih 	int ret;
2505de7ba639SPing-Ke Shih 
2506de7ba639SPing-Ke Shih 	ret = rtw89_set_hw_sch_tx_en_v1(rtwdev, mac_idx, tx_en,
2507de7ba639SPing-Ke Shih 					B_AX_CTN_TXEN_ALL_MASK_V1);
2508de7ba639SPing-Ke Shih 	if (ret)
2509de7ba639SPing-Ke Shih 		return ret;
2510de7ba639SPing-Ke Shih 
2511de7ba639SPing-Ke Shih 	return 0;
2512de7ba639SPing-Ke Shih }
2513de7ba639SPing-Ke Shih EXPORT_SYMBOL(rtw89_mac_resume_sch_tx_v1);
2514de7ba639SPing-Ke Shih 
25158a1f6c88SZong-Zhe Yang u16 rtw89_mac_dle_buf_req(struct rtw89_dev *rtwdev, u16 buf_len, bool wd)
2516e3ec7017SPing-Ke Shih {
2517e3ec7017SPing-Ke Shih 	u32 val, reg;
2518e3ec7017SPing-Ke Shih 	int ret;
2519e3ec7017SPing-Ke Shih 
2520e3ec7017SPing-Ke Shih 	reg = wd ? R_AX_WD_BUF_REQ : R_AX_PL_BUF_REQ;
2521e3ec7017SPing-Ke Shih 	val = buf_len;
2522e3ec7017SPing-Ke Shih 	val |= B_AX_WD_BUF_REQ_EXEC;
2523e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, reg, val);
2524e3ec7017SPing-Ke Shih 
2525e3ec7017SPing-Ke Shih 	reg = wd ? R_AX_WD_BUF_STATUS : R_AX_PL_BUF_STATUS;
2526e3ec7017SPing-Ke Shih 
2527e3ec7017SPing-Ke Shih 	ret = read_poll_timeout(rtw89_read32, val, val & B_AX_WD_BUF_STAT_DONE,
2528e3ec7017SPing-Ke Shih 				1, 2000, false, rtwdev, reg);
2529e3ec7017SPing-Ke Shih 	if (ret)
2530e3ec7017SPing-Ke Shih 		return 0xffff;
2531e3ec7017SPing-Ke Shih 
2532e3ec7017SPing-Ke Shih 	return FIELD_GET(B_AX_WD_BUF_STAT_PKTID_MASK, val);
2533e3ec7017SPing-Ke Shih }
2534e3ec7017SPing-Ke Shih 
25358a1f6c88SZong-Zhe Yang int rtw89_mac_set_cpuio(struct rtw89_dev *rtwdev,
25368a1f6c88SZong-Zhe Yang 			struct rtw89_cpuio_ctrl *ctrl_para, bool wd)
2537e3ec7017SPing-Ke Shih {
2538e3ec7017SPing-Ke Shih 	u32 val, cmd_type, reg;
2539e3ec7017SPing-Ke Shih 	int ret;
2540e3ec7017SPing-Ke Shih 
2541e3ec7017SPing-Ke Shih 	cmd_type = ctrl_para->cmd_type;
2542e3ec7017SPing-Ke Shih 
2543e3ec7017SPing-Ke Shih 	reg = wd ? R_AX_WD_CPUQ_OP_2 : R_AX_PL_CPUQ_OP_2;
2544e3ec7017SPing-Ke Shih 	val = 0;
2545e3ec7017SPing-Ke Shih 	val = u32_replace_bits(val, ctrl_para->start_pktid,
2546e3ec7017SPing-Ke Shih 			       B_AX_WD_CPUQ_OP_STRT_PKTID_MASK);
2547e3ec7017SPing-Ke Shih 	val = u32_replace_bits(val, ctrl_para->end_pktid,
2548e3ec7017SPing-Ke Shih 			       B_AX_WD_CPUQ_OP_END_PKTID_MASK);
2549e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, reg, val);
2550e3ec7017SPing-Ke Shih 
2551e3ec7017SPing-Ke Shih 	reg = wd ? R_AX_WD_CPUQ_OP_1 : R_AX_PL_CPUQ_OP_1;
2552e3ec7017SPing-Ke Shih 	val = 0;
2553e3ec7017SPing-Ke Shih 	val = u32_replace_bits(val, ctrl_para->src_pid,
2554e3ec7017SPing-Ke Shih 			       B_AX_CPUQ_OP_SRC_PID_MASK);
2555e3ec7017SPing-Ke Shih 	val = u32_replace_bits(val, ctrl_para->src_qid,
2556e3ec7017SPing-Ke Shih 			       B_AX_CPUQ_OP_SRC_QID_MASK);
2557e3ec7017SPing-Ke Shih 	val = u32_replace_bits(val, ctrl_para->dst_pid,
2558e3ec7017SPing-Ke Shih 			       B_AX_CPUQ_OP_DST_PID_MASK);
2559e3ec7017SPing-Ke Shih 	val = u32_replace_bits(val, ctrl_para->dst_qid,
2560e3ec7017SPing-Ke Shih 			       B_AX_CPUQ_OP_DST_QID_MASK);
2561e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, reg, val);
2562e3ec7017SPing-Ke Shih 
2563e3ec7017SPing-Ke Shih 	reg = wd ? R_AX_WD_CPUQ_OP_0 : R_AX_PL_CPUQ_OP_0;
2564e3ec7017SPing-Ke Shih 	val = 0;
2565e3ec7017SPing-Ke Shih 	val = u32_replace_bits(val, cmd_type,
2566e3ec7017SPing-Ke Shih 			       B_AX_CPUQ_OP_CMD_TYPE_MASK);
2567e3ec7017SPing-Ke Shih 	val = u32_replace_bits(val, ctrl_para->macid,
2568e3ec7017SPing-Ke Shih 			       B_AX_CPUQ_OP_MACID_MASK);
2569e3ec7017SPing-Ke Shih 	val = u32_replace_bits(val, ctrl_para->pkt_num,
2570e3ec7017SPing-Ke Shih 			       B_AX_CPUQ_OP_PKTNUM_MASK);
2571e3ec7017SPing-Ke Shih 	val |= B_AX_WD_CPUQ_OP_EXEC;
2572e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, reg, val);
2573e3ec7017SPing-Ke Shih 
2574e3ec7017SPing-Ke Shih 	reg = wd ? R_AX_WD_CPUQ_OP_STATUS : R_AX_PL_CPUQ_OP_STATUS;
2575e3ec7017SPing-Ke Shih 
2576e3ec7017SPing-Ke Shih 	ret = read_poll_timeout(rtw89_read32, val, val & B_AX_WD_CPUQ_OP_STAT_DONE,
2577e3ec7017SPing-Ke Shih 				1, 2000, false, rtwdev, reg);
2578e3ec7017SPing-Ke Shih 	if (ret)
2579e3ec7017SPing-Ke Shih 		return ret;
2580e3ec7017SPing-Ke Shih 
2581e3ec7017SPing-Ke Shih 	if (cmd_type == CPUIO_OP_CMD_GET_1ST_PID ||
2582e3ec7017SPing-Ke Shih 	    cmd_type == CPUIO_OP_CMD_GET_NEXT_PID)
2583e3ec7017SPing-Ke Shih 		ctrl_para->pktid = FIELD_GET(B_AX_WD_CPUQ_OP_PKTID_MASK, val);
2584e3ec7017SPing-Ke Shih 
2585e3ec7017SPing-Ke Shih 	return 0;
2586e3ec7017SPing-Ke Shih }
2587e3ec7017SPing-Ke Shih 
2588e3ec7017SPing-Ke Shih static int dle_quota_change(struct rtw89_dev *rtwdev, enum rtw89_qta_mode mode)
2589e3ec7017SPing-Ke Shih {
2590e3ec7017SPing-Ke Shih 	const struct rtw89_dle_mem *cfg;
2591e3ec7017SPing-Ke Shih 	struct rtw89_cpuio_ctrl ctrl_para = {0};
2592e3ec7017SPing-Ke Shih 	u16 pkt_id;
2593e3ec7017SPing-Ke Shih 	int ret;
2594e3ec7017SPing-Ke Shih 
2595e3ec7017SPing-Ke Shih 	cfg = get_dle_mem_cfg(rtwdev, mode);
2596e3ec7017SPing-Ke Shih 	if (!cfg) {
2597e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]wd/dle mem cfg\n");
2598e3ec7017SPing-Ke Shih 		return -EINVAL;
2599e3ec7017SPing-Ke Shih 	}
2600e3ec7017SPing-Ke Shih 
2601e3ec7017SPing-Ke Shih 	if (dle_used_size(cfg->wde_size, cfg->ple_size) != rtwdev->chip->fifo_size) {
2602e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]wd/dle mem cfg\n");
2603e3ec7017SPing-Ke Shih 		return -EINVAL;
2604e3ec7017SPing-Ke Shih 	}
2605e3ec7017SPing-Ke Shih 
2606e3ec7017SPing-Ke Shih 	dle_quota_cfg(rtwdev, cfg, INVALID_QT_WCPU);
2607e3ec7017SPing-Ke Shih 
2608e3ec7017SPing-Ke Shih 	pkt_id = rtw89_mac_dle_buf_req(rtwdev, 0x20, true);
2609e3ec7017SPing-Ke Shih 	if (pkt_id == 0xffff) {
2610e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]WDE DLE buf req\n");
2611e3ec7017SPing-Ke Shih 		return -ENOMEM;
2612e3ec7017SPing-Ke Shih 	}
2613e3ec7017SPing-Ke Shih 
2614e3ec7017SPing-Ke Shih 	ctrl_para.cmd_type = CPUIO_OP_CMD_ENQ_TO_HEAD;
2615e3ec7017SPing-Ke Shih 	ctrl_para.start_pktid = pkt_id;
2616e3ec7017SPing-Ke Shih 	ctrl_para.end_pktid = pkt_id;
2617e3ec7017SPing-Ke Shih 	ctrl_para.pkt_num = 0;
2618e3ec7017SPing-Ke Shih 	ctrl_para.dst_pid = WDE_DLE_PORT_ID_WDRLS;
2619e3ec7017SPing-Ke Shih 	ctrl_para.dst_qid = WDE_DLE_QUEID_NO_REPORT;
2620e3ec7017SPing-Ke Shih 	ret = rtw89_mac_set_cpuio(rtwdev, &ctrl_para, true);
2621e3ec7017SPing-Ke Shih 	if (ret) {
2622e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]WDE DLE enqueue to head\n");
2623e3ec7017SPing-Ke Shih 		return -EFAULT;
2624e3ec7017SPing-Ke Shih 	}
2625e3ec7017SPing-Ke Shih 
2626e3ec7017SPing-Ke Shih 	pkt_id = rtw89_mac_dle_buf_req(rtwdev, 0x20, false);
2627e3ec7017SPing-Ke Shih 	if (pkt_id == 0xffff) {
2628e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]PLE DLE buf req\n");
2629e3ec7017SPing-Ke Shih 		return -ENOMEM;
2630e3ec7017SPing-Ke Shih 	}
2631e3ec7017SPing-Ke Shih 
2632e3ec7017SPing-Ke Shih 	ctrl_para.cmd_type = CPUIO_OP_CMD_ENQ_TO_HEAD;
2633e3ec7017SPing-Ke Shih 	ctrl_para.start_pktid = pkt_id;
2634e3ec7017SPing-Ke Shih 	ctrl_para.end_pktid = pkt_id;
2635e3ec7017SPing-Ke Shih 	ctrl_para.pkt_num = 0;
2636e3ec7017SPing-Ke Shih 	ctrl_para.dst_pid = PLE_DLE_PORT_ID_PLRLS;
2637e3ec7017SPing-Ke Shih 	ctrl_para.dst_qid = PLE_DLE_QUEID_NO_REPORT;
2638e3ec7017SPing-Ke Shih 	ret = rtw89_mac_set_cpuio(rtwdev, &ctrl_para, false);
2639e3ec7017SPing-Ke Shih 	if (ret) {
2640e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]PLE DLE enqueue to head\n");
2641e3ec7017SPing-Ke Shih 		return -EFAULT;
2642e3ec7017SPing-Ke Shih 	}
2643e3ec7017SPing-Ke Shih 
2644e3ec7017SPing-Ke Shih 	return 0;
2645e3ec7017SPing-Ke Shih }
2646e3ec7017SPing-Ke Shih 
2647e3ec7017SPing-Ke Shih static int band_idle_ck_b(struct rtw89_dev *rtwdev, u8 mac_idx)
2648e3ec7017SPing-Ke Shih {
2649e3ec7017SPing-Ke Shih 	int ret;
2650e3ec7017SPing-Ke Shih 	u32 reg;
2651e3ec7017SPing-Ke Shih 	u8 val;
2652e3ec7017SPing-Ke Shih 
2653e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL);
2654e3ec7017SPing-Ke Shih 	if (ret)
2655e3ec7017SPing-Ke Shih 		return ret;
2656e3ec7017SPing-Ke Shih 
2657e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_PTCL_TX_CTN_SEL, mac_idx);
2658e3ec7017SPing-Ke Shih 
2659e3ec7017SPing-Ke Shih 	ret = read_poll_timeout(rtw89_read8, val,
2660e3ec7017SPing-Ke Shih 				(val & B_AX_PTCL_TX_ON_STAT) == 0,
2661e3ec7017SPing-Ke Shih 				SW_CVR_DUR_US,
2662e3ec7017SPing-Ke Shih 				SW_CVR_DUR_US * PTCL_IDLE_POLL_CNT,
2663e3ec7017SPing-Ke Shih 				false, rtwdev, reg);
2664e3ec7017SPing-Ke Shih 	if (ret)
2665e3ec7017SPing-Ke Shih 		return ret;
2666e3ec7017SPing-Ke Shih 
2667e3ec7017SPing-Ke Shih 	return 0;
2668e3ec7017SPing-Ke Shih }
2669e3ec7017SPing-Ke Shih 
2670e3ec7017SPing-Ke Shih static int band1_enable(struct rtw89_dev *rtwdev)
2671e3ec7017SPing-Ke Shih {
2672e3ec7017SPing-Ke Shih 	int ret, i;
2673e3ec7017SPing-Ke Shih 	u32 sleep_bak[4] = {0};
2674e3ec7017SPing-Ke Shih 	u32 pause_bak[4] = {0};
2675d780f926SPing-Ke Shih 	u32 tx_en;
2676e3ec7017SPing-Ke Shih 
2677de7ba639SPing-Ke Shih 	ret = rtw89_chip_stop_sch_tx(rtwdev, 0, &tx_en, RTW89_SCH_TX_SEL_ALL);
2678e3ec7017SPing-Ke Shih 	if (ret) {
2679e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]stop sch tx %d\n", ret);
2680e3ec7017SPing-Ke Shih 		return ret;
2681e3ec7017SPing-Ke Shih 	}
2682e3ec7017SPing-Ke Shih 
2683e3ec7017SPing-Ke Shih 	for (i = 0; i < 4; i++) {
2684e3ec7017SPing-Ke Shih 		sleep_bak[i] = rtw89_read32(rtwdev, R_AX_MACID_SLEEP_0 + i * 4);
2685e3ec7017SPing-Ke Shih 		pause_bak[i] = rtw89_read32(rtwdev, R_AX_SS_MACID_PAUSE_0 + i * 4);
2686e3ec7017SPing-Ke Shih 		rtw89_write32(rtwdev, R_AX_MACID_SLEEP_0 + i * 4, U32_MAX);
2687e3ec7017SPing-Ke Shih 		rtw89_write32(rtwdev, R_AX_SS_MACID_PAUSE_0 + i * 4, U32_MAX);
2688e3ec7017SPing-Ke Shih 	}
2689e3ec7017SPing-Ke Shih 
2690e3ec7017SPing-Ke Shih 	ret = band_idle_ck_b(rtwdev, 0);
2691e3ec7017SPing-Ke Shih 	if (ret) {
2692e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]tx idle poll %d\n", ret);
2693e3ec7017SPing-Ke Shih 		return ret;
2694e3ec7017SPing-Ke Shih 	}
2695e3ec7017SPing-Ke Shih 
2696e3ec7017SPing-Ke Shih 	ret = dle_quota_change(rtwdev, rtwdev->mac.qta_mode);
2697e3ec7017SPing-Ke Shih 	if (ret) {
2698e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]DLE quota change %d\n", ret);
2699e3ec7017SPing-Ke Shih 		return ret;
2700e3ec7017SPing-Ke Shih 	}
2701e3ec7017SPing-Ke Shih 
2702e3ec7017SPing-Ke Shih 	for (i = 0; i < 4; i++) {
2703e3ec7017SPing-Ke Shih 		rtw89_write32(rtwdev, R_AX_MACID_SLEEP_0 + i * 4, sleep_bak[i]);
2704e3ec7017SPing-Ke Shih 		rtw89_write32(rtwdev, R_AX_SS_MACID_PAUSE_0 + i * 4, pause_bak[i]);
2705e3ec7017SPing-Ke Shih 	}
2706e3ec7017SPing-Ke Shih 
2707de7ba639SPing-Ke Shih 	ret = rtw89_chip_resume_sch_tx(rtwdev, 0, tx_en);
2708e3ec7017SPing-Ke Shih 	if (ret) {
2709e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]CMAC1 resume sch tx %d\n", ret);
2710e3ec7017SPing-Ke Shih 		return ret;
2711e3ec7017SPing-Ke Shih 	}
2712e3ec7017SPing-Ke Shih 
2713e3ec7017SPing-Ke Shih 	ret = cmac_func_en(rtwdev, 1, true);
2714e3ec7017SPing-Ke Shih 	if (ret) {
2715e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]CMAC1 func en %d\n", ret);
2716e3ec7017SPing-Ke Shih 		return ret;
2717e3ec7017SPing-Ke Shih 	}
2718e3ec7017SPing-Ke Shih 
2719e3ec7017SPing-Ke Shih 	ret = cmac_init(rtwdev, 1);
2720e3ec7017SPing-Ke Shih 	if (ret) {
2721e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]CMAC1 init %d\n", ret);
2722e3ec7017SPing-Ke Shih 		return ret;
2723e3ec7017SPing-Ke Shih 	}
2724e3ec7017SPing-Ke Shih 
2725e3ec7017SPing-Ke Shih 	rtw89_write32_set(rtwdev, R_AX_SYS_ISO_CTRL_EXTEND,
2726e3ec7017SPing-Ke Shih 			  B_AX_R_SYM_FEN_WLBBFUN_1 | B_AX_R_SYM_FEN_WLBBGLB_1);
2727e3ec7017SPing-Ke Shih 
2728e3ec7017SPing-Ke Shih 	return 0;
2729e3ec7017SPing-Ke Shih }
2730e3ec7017SPing-Ke Shih 
2731eeadcd2aSChia-Yuan Li static void rtw89_wdrls_imr_enable(struct rtw89_dev *rtwdev)
2732eeadcd2aSChia-Yuan Li {
2733eeadcd2aSChia-Yuan Li 	const struct rtw89_imr_info *imr = rtwdev->chip->imr_info;
2734eeadcd2aSChia-Yuan Li 
2735eeadcd2aSChia-Yuan Li 	rtw89_write32_clr(rtwdev, R_AX_WDRLS_ERR_IMR, B_AX_WDRLS_IMR_EN_CLR);
2736eeadcd2aSChia-Yuan Li 	rtw89_write32_set(rtwdev, R_AX_WDRLS_ERR_IMR, imr->wdrls_imr_set);
2737eeadcd2aSChia-Yuan Li }
2738eeadcd2aSChia-Yuan Li 
2739eeadcd2aSChia-Yuan Li static void rtw89_wsec_imr_enable(struct rtw89_dev *rtwdev)
2740eeadcd2aSChia-Yuan Li {
2741eeadcd2aSChia-Yuan Li 	const struct rtw89_imr_info *imr = rtwdev->chip->imr_info;
2742eeadcd2aSChia-Yuan Li 
2743eeadcd2aSChia-Yuan Li 	rtw89_write32_set(rtwdev, imr->wsec_imr_reg, imr->wsec_imr_set);
2744eeadcd2aSChia-Yuan Li }
2745eeadcd2aSChia-Yuan Li 
2746eeadcd2aSChia-Yuan Li static void rtw89_mpdu_trx_imr_enable(struct rtw89_dev *rtwdev)
2747eeadcd2aSChia-Yuan Li {
2748eeadcd2aSChia-Yuan Li 	enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
2749eeadcd2aSChia-Yuan Li 	const struct rtw89_imr_info *imr = rtwdev->chip->imr_info;
2750eeadcd2aSChia-Yuan Li 
2751eeadcd2aSChia-Yuan Li 	rtw89_write32_clr(rtwdev, R_AX_MPDU_TX_ERR_IMR,
2752eeadcd2aSChia-Yuan Li 			  B_AX_TX_GET_ERRPKTID_INT_EN |
2753eeadcd2aSChia-Yuan Li 			  B_AX_TX_NXT_ERRPKTID_INT_EN |
2754eeadcd2aSChia-Yuan Li 			  B_AX_TX_MPDU_SIZE_ZERO_INT_EN |
2755eeadcd2aSChia-Yuan Li 			  B_AX_TX_OFFSET_ERR_INT_EN |
2756eeadcd2aSChia-Yuan Li 			  B_AX_TX_HDR3_SIZE_ERR_INT_EN);
2757eeadcd2aSChia-Yuan Li 	if (chip_id == RTL8852C)
2758eeadcd2aSChia-Yuan Li 		rtw89_write32_clr(rtwdev, R_AX_MPDU_TX_ERR_IMR,
2759eeadcd2aSChia-Yuan Li 				  B_AX_TX_ETH_TYPE_ERR_EN |
2760eeadcd2aSChia-Yuan Li 				  B_AX_TX_LLC_PRE_ERR_EN |
2761eeadcd2aSChia-Yuan Li 				  B_AX_TX_NW_TYPE_ERR_EN |
2762eeadcd2aSChia-Yuan Li 				  B_AX_TX_KSRCH_ERR_EN);
2763eeadcd2aSChia-Yuan Li 	rtw89_write32_set(rtwdev, R_AX_MPDU_TX_ERR_IMR,
2764eeadcd2aSChia-Yuan Li 			  imr->mpdu_tx_imr_set);
2765eeadcd2aSChia-Yuan Li 
2766eeadcd2aSChia-Yuan Li 	rtw89_write32_clr(rtwdev, R_AX_MPDU_RX_ERR_IMR,
2767eeadcd2aSChia-Yuan Li 			  B_AX_GETPKTID_ERR_INT_EN |
2768eeadcd2aSChia-Yuan Li 			  B_AX_MHDRLEN_ERR_INT_EN |
2769eeadcd2aSChia-Yuan Li 			  B_AX_RPT_ERR_INT_EN);
2770eeadcd2aSChia-Yuan Li 	rtw89_write32_set(rtwdev, R_AX_MPDU_RX_ERR_IMR,
2771eeadcd2aSChia-Yuan Li 			  imr->mpdu_rx_imr_set);
2772eeadcd2aSChia-Yuan Li }
2773eeadcd2aSChia-Yuan Li 
2774eeadcd2aSChia-Yuan Li static void rtw89_sta_sch_imr_enable(struct rtw89_dev *rtwdev)
2775eeadcd2aSChia-Yuan Li {
2776eeadcd2aSChia-Yuan Li 	const struct rtw89_imr_info *imr = rtwdev->chip->imr_info;
2777eeadcd2aSChia-Yuan Li 
2778eeadcd2aSChia-Yuan Li 	rtw89_write32_clr(rtwdev, R_AX_STA_SCHEDULER_ERR_IMR,
2779eeadcd2aSChia-Yuan Li 			  B_AX_SEARCH_HANG_TIMEOUT_INT_EN |
2780eeadcd2aSChia-Yuan Li 			  B_AX_RPT_HANG_TIMEOUT_INT_EN |
2781eeadcd2aSChia-Yuan Li 			  B_AX_PLE_B_PKTID_ERR_INT_EN);
2782eeadcd2aSChia-Yuan Li 	rtw89_write32_set(rtwdev, R_AX_STA_SCHEDULER_ERR_IMR,
2783eeadcd2aSChia-Yuan Li 			  imr->sta_sch_imr_set);
2784eeadcd2aSChia-Yuan Li }
2785eeadcd2aSChia-Yuan Li 
2786eeadcd2aSChia-Yuan Li static void rtw89_txpktctl_imr_enable(struct rtw89_dev *rtwdev)
2787eeadcd2aSChia-Yuan Li {
2788eeadcd2aSChia-Yuan Li 	const struct rtw89_imr_info *imr = rtwdev->chip->imr_info;
2789eeadcd2aSChia-Yuan Li 
2790eeadcd2aSChia-Yuan Li 	rtw89_write32_clr(rtwdev, imr->txpktctl_imr_b0_reg,
2791eeadcd2aSChia-Yuan Li 			  imr->txpktctl_imr_b0_clr);
2792eeadcd2aSChia-Yuan Li 	rtw89_write32_set(rtwdev, imr->txpktctl_imr_b0_reg,
2793eeadcd2aSChia-Yuan Li 			  imr->txpktctl_imr_b0_set);
2794eeadcd2aSChia-Yuan Li 	rtw89_write32_clr(rtwdev, imr->txpktctl_imr_b1_reg,
2795eeadcd2aSChia-Yuan Li 			  imr->txpktctl_imr_b1_clr);
2796eeadcd2aSChia-Yuan Li 	rtw89_write32_set(rtwdev, imr->txpktctl_imr_b1_reg,
2797eeadcd2aSChia-Yuan Li 			  imr->txpktctl_imr_b1_set);
2798eeadcd2aSChia-Yuan Li }
2799eeadcd2aSChia-Yuan Li 
2800eeadcd2aSChia-Yuan Li static void rtw89_wde_imr_enable(struct rtw89_dev *rtwdev)
2801eeadcd2aSChia-Yuan Li {
2802eeadcd2aSChia-Yuan Li 	const struct rtw89_imr_info *imr = rtwdev->chip->imr_info;
2803eeadcd2aSChia-Yuan Li 
2804eeadcd2aSChia-Yuan Li 	rtw89_write32_clr(rtwdev, R_AX_WDE_ERR_IMR, imr->wde_imr_clr);
2805eeadcd2aSChia-Yuan Li 	rtw89_write32_set(rtwdev, R_AX_WDE_ERR_IMR, imr->wde_imr_set);
2806eeadcd2aSChia-Yuan Li }
2807eeadcd2aSChia-Yuan Li 
2808eeadcd2aSChia-Yuan Li static void rtw89_ple_imr_enable(struct rtw89_dev *rtwdev)
2809eeadcd2aSChia-Yuan Li {
2810eeadcd2aSChia-Yuan Li 	const struct rtw89_imr_info *imr = rtwdev->chip->imr_info;
2811eeadcd2aSChia-Yuan Li 
2812eeadcd2aSChia-Yuan Li 	rtw89_write32_clr(rtwdev, R_AX_PLE_ERR_IMR, imr->ple_imr_clr);
2813eeadcd2aSChia-Yuan Li 	rtw89_write32_set(rtwdev, R_AX_PLE_ERR_IMR, imr->ple_imr_set);
2814eeadcd2aSChia-Yuan Li }
2815eeadcd2aSChia-Yuan Li 
2816eeadcd2aSChia-Yuan Li static void rtw89_pktin_imr_enable(struct rtw89_dev *rtwdev)
2817eeadcd2aSChia-Yuan Li {
2818eeadcd2aSChia-Yuan Li 	rtw89_write32_set(rtwdev, R_AX_PKTIN_ERR_IMR,
2819eeadcd2aSChia-Yuan Li 			  B_AX_PKTIN_GETPKTID_ERR_INT_EN);
2820eeadcd2aSChia-Yuan Li }
2821eeadcd2aSChia-Yuan Li 
2822eeadcd2aSChia-Yuan Li static void rtw89_dispatcher_imr_enable(struct rtw89_dev *rtwdev)
2823eeadcd2aSChia-Yuan Li {
2824eeadcd2aSChia-Yuan Li 	const struct rtw89_imr_info *imr = rtwdev->chip->imr_info;
2825eeadcd2aSChia-Yuan Li 
2826eeadcd2aSChia-Yuan Li 	rtw89_write32_clr(rtwdev, R_AX_HOST_DISPATCHER_ERR_IMR,
2827eeadcd2aSChia-Yuan Li 			  imr->host_disp_imr_clr);
2828eeadcd2aSChia-Yuan Li 	rtw89_write32_set(rtwdev, R_AX_HOST_DISPATCHER_ERR_IMR,
2829eeadcd2aSChia-Yuan Li 			  imr->host_disp_imr_set);
2830eeadcd2aSChia-Yuan Li 	rtw89_write32_clr(rtwdev, R_AX_CPU_DISPATCHER_ERR_IMR,
2831eeadcd2aSChia-Yuan Li 			  imr->cpu_disp_imr_clr);
2832eeadcd2aSChia-Yuan Li 	rtw89_write32_set(rtwdev, R_AX_CPU_DISPATCHER_ERR_IMR,
2833eeadcd2aSChia-Yuan Li 			  imr->cpu_disp_imr_set);
2834eeadcd2aSChia-Yuan Li 	rtw89_write32_clr(rtwdev, R_AX_OTHER_DISPATCHER_ERR_IMR,
2835eeadcd2aSChia-Yuan Li 			  imr->other_disp_imr_clr);
2836eeadcd2aSChia-Yuan Li 	rtw89_write32_set(rtwdev, R_AX_OTHER_DISPATCHER_ERR_IMR,
2837eeadcd2aSChia-Yuan Li 			  imr->other_disp_imr_set);
2838eeadcd2aSChia-Yuan Li }
2839eeadcd2aSChia-Yuan Li 
2840eeadcd2aSChia-Yuan Li static void rtw89_cpuio_imr_enable(struct rtw89_dev *rtwdev)
2841eeadcd2aSChia-Yuan Li {
2842eeadcd2aSChia-Yuan Li 	rtw89_write32_clr(rtwdev, R_AX_CPUIO_ERR_IMR, B_AX_CPUIO_IMR_CLR);
2843eeadcd2aSChia-Yuan Li 	rtw89_write32_set(rtwdev, R_AX_CPUIO_ERR_IMR, B_AX_CPUIO_IMR_SET);
2844eeadcd2aSChia-Yuan Li }
2845eeadcd2aSChia-Yuan Li 
2846eeadcd2aSChia-Yuan Li static void rtw89_bbrpt_imr_enable(struct rtw89_dev *rtwdev)
2847eeadcd2aSChia-Yuan Li {
2848eeadcd2aSChia-Yuan Li 	const struct rtw89_imr_info *imr = rtwdev->chip->imr_info;
2849eeadcd2aSChia-Yuan Li 
2850eeadcd2aSChia-Yuan Li 	rtw89_write32_set(rtwdev, R_AX_BBRPT_COM_ERR_IMR,
2851eeadcd2aSChia-Yuan Li 			  B_AX_BBRPT_COM_NULL_PLPKTID_ERR_INT_EN);
2852eeadcd2aSChia-Yuan Li 	rtw89_write32_clr(rtwdev, imr->bbrpt_chinfo_err_imr_reg,
2853eeadcd2aSChia-Yuan Li 			  B_AX_BBRPT_CHINFO_IMR_CLR);
2854eeadcd2aSChia-Yuan Li 	rtw89_write32_set(rtwdev, imr->bbrpt_chinfo_err_imr_reg,
2855eeadcd2aSChia-Yuan Li 			  imr->bbrpt_err_imr_set);
2856eeadcd2aSChia-Yuan Li 	rtw89_write32_set(rtwdev, imr->bbrpt_dfs_err_imr_reg,
2857eeadcd2aSChia-Yuan Li 			  B_AX_BBRPT_DFS_TO_ERR_INT_EN);
2858eeadcd2aSChia-Yuan Li 	rtw89_write32_set(rtwdev, R_AX_LA_ERRFLAG, B_AX_LA_IMR_DATA_LOSS_ERR);
2859eeadcd2aSChia-Yuan Li }
2860eeadcd2aSChia-Yuan Li 
2861d86369e9SChia-Yuan Li static void rtw89_scheduler_imr_enable(struct rtw89_dev *rtwdev, u8 mac_idx)
2862d86369e9SChia-Yuan Li {
2863d86369e9SChia-Yuan Li 	u32 reg;
2864d86369e9SChia-Yuan Li 
2865d86369e9SChia-Yuan Li 	reg = rtw89_mac_reg_by_idx(R_AX_SCHEDULE_ERR_IMR, mac_idx);
2866d86369e9SChia-Yuan Li 	rtw89_write32_clr(rtwdev, reg, B_AX_SORT_NON_IDLE_ERR_INT_EN |
2867d86369e9SChia-Yuan Li 				       B_AX_FSM_TIMEOUT_ERR_INT_EN);
2868d86369e9SChia-Yuan Li 	rtw89_write32_set(rtwdev, reg, B_AX_FSM_TIMEOUT_ERR_INT_EN);
2869d86369e9SChia-Yuan Li }
2870d86369e9SChia-Yuan Li 
2871d86369e9SChia-Yuan Li static void rtw89_ptcl_imr_enable(struct rtw89_dev *rtwdev, u8 mac_idx)
2872d86369e9SChia-Yuan Li {
2873d86369e9SChia-Yuan Li 	const struct rtw89_imr_info *imr = rtwdev->chip->imr_info;
2874d86369e9SChia-Yuan Li 	u32 reg;
2875d86369e9SChia-Yuan Li 
2876d86369e9SChia-Yuan Li 	reg = rtw89_mac_reg_by_idx(R_AX_PTCL_IMR0, mac_idx);
2877d86369e9SChia-Yuan Li 	rtw89_write32_clr(rtwdev, reg, imr->ptcl_imr_clr);
2878d86369e9SChia-Yuan Li 	rtw89_write32_set(rtwdev, reg, imr->ptcl_imr_set);
2879d86369e9SChia-Yuan Li }
2880d86369e9SChia-Yuan Li 
2881d86369e9SChia-Yuan Li static void rtw89_cdma_imr_enable(struct rtw89_dev *rtwdev, u8 mac_idx)
2882d86369e9SChia-Yuan Li {
2883d86369e9SChia-Yuan Li 	const struct rtw89_imr_info *imr = rtwdev->chip->imr_info;
2884d86369e9SChia-Yuan Li 	enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
2885d86369e9SChia-Yuan Li 	u32 reg;
2886d86369e9SChia-Yuan Li 
2887d86369e9SChia-Yuan Li 	reg = rtw89_mac_reg_by_idx(imr->cdma_imr_0_reg, mac_idx);
2888d86369e9SChia-Yuan Li 	rtw89_write32_clr(rtwdev, reg, imr->cdma_imr_0_clr);
2889d86369e9SChia-Yuan Li 	rtw89_write32_set(rtwdev, reg, imr->cdma_imr_0_set);
2890d86369e9SChia-Yuan Li 
2891d86369e9SChia-Yuan Li 	if (chip_id == RTL8852C) {
2892d86369e9SChia-Yuan Li 		reg = rtw89_mac_reg_by_idx(imr->cdma_imr_1_reg, mac_idx);
2893d86369e9SChia-Yuan Li 		rtw89_write32_clr(rtwdev, reg, imr->cdma_imr_1_clr);
2894d86369e9SChia-Yuan Li 		rtw89_write32_set(rtwdev, reg, imr->cdma_imr_1_set);
2895d86369e9SChia-Yuan Li 	}
2896d86369e9SChia-Yuan Li }
2897d86369e9SChia-Yuan Li 
2898d86369e9SChia-Yuan Li static void rtw89_phy_intf_imr_enable(struct rtw89_dev *rtwdev, u8 mac_idx)
2899d86369e9SChia-Yuan Li {
2900d86369e9SChia-Yuan Li 	const struct rtw89_imr_info *imr = rtwdev->chip->imr_info;
2901d86369e9SChia-Yuan Li 	u32 reg;
2902d86369e9SChia-Yuan Li 
2903d86369e9SChia-Yuan Li 	reg = rtw89_mac_reg_by_idx(imr->phy_intf_imr_reg, mac_idx);
2904d86369e9SChia-Yuan Li 	rtw89_write32_clr(rtwdev, reg, imr->phy_intf_imr_clr);
2905d86369e9SChia-Yuan Li 	rtw89_write32_set(rtwdev, reg, imr->phy_intf_imr_set);
2906d86369e9SChia-Yuan Li }
2907d86369e9SChia-Yuan Li 
2908d86369e9SChia-Yuan Li static void rtw89_rmac_imr_enable(struct rtw89_dev *rtwdev, u8 mac_idx)
2909d86369e9SChia-Yuan Li {
2910d86369e9SChia-Yuan Li 	const struct rtw89_imr_info *imr = rtwdev->chip->imr_info;
2911d86369e9SChia-Yuan Li 	u32 reg;
2912d86369e9SChia-Yuan Li 
2913d86369e9SChia-Yuan Li 	reg = rtw89_mac_reg_by_idx(imr->rmac_imr_reg, mac_idx);
2914d86369e9SChia-Yuan Li 	rtw89_write32_clr(rtwdev, reg, imr->rmac_imr_clr);
2915d86369e9SChia-Yuan Li 	rtw89_write32_set(rtwdev, reg, imr->rmac_imr_set);
2916d86369e9SChia-Yuan Li }
2917d86369e9SChia-Yuan Li 
2918d86369e9SChia-Yuan Li static void rtw89_tmac_imr_enable(struct rtw89_dev *rtwdev, u8 mac_idx)
2919d86369e9SChia-Yuan Li {
2920d86369e9SChia-Yuan Li 	const struct rtw89_imr_info *imr = rtwdev->chip->imr_info;
2921d86369e9SChia-Yuan Li 	u32 reg;
2922d86369e9SChia-Yuan Li 
2923d86369e9SChia-Yuan Li 	reg = rtw89_mac_reg_by_idx(imr->tmac_imr_reg, mac_idx);
2924d86369e9SChia-Yuan Li 	rtw89_write32_clr(rtwdev, reg, imr->tmac_imr_clr);
2925d86369e9SChia-Yuan Li 	rtw89_write32_set(rtwdev, reg, imr->tmac_imr_set);
2926d86369e9SChia-Yuan Li }
2927d86369e9SChia-Yuan Li 
2928e3ec7017SPing-Ke Shih static int rtw89_mac_enable_imr(struct rtw89_dev *rtwdev, u8 mac_idx,
2929e3ec7017SPing-Ke Shih 				enum rtw89_mac_hwmod_sel sel)
2930e3ec7017SPing-Ke Shih {
2931e3ec7017SPing-Ke Shih 	int ret;
2932e3ec7017SPing-Ke Shih 
2933e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, sel);
2934e3ec7017SPing-Ke Shih 	if (ret) {
2935e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "MAC%d mac_idx%d is not ready\n",
2936e3ec7017SPing-Ke Shih 			  sel, mac_idx);
2937e3ec7017SPing-Ke Shih 		return ret;
2938e3ec7017SPing-Ke Shih 	}
2939e3ec7017SPing-Ke Shih 
2940e3ec7017SPing-Ke Shih 	if (sel == RTW89_DMAC_SEL) {
2941eeadcd2aSChia-Yuan Li 		rtw89_wdrls_imr_enable(rtwdev);
2942eeadcd2aSChia-Yuan Li 		rtw89_wsec_imr_enable(rtwdev);
2943eeadcd2aSChia-Yuan Li 		rtw89_mpdu_trx_imr_enable(rtwdev);
2944eeadcd2aSChia-Yuan Li 		rtw89_sta_sch_imr_enable(rtwdev);
2945eeadcd2aSChia-Yuan Li 		rtw89_txpktctl_imr_enable(rtwdev);
2946eeadcd2aSChia-Yuan Li 		rtw89_wde_imr_enable(rtwdev);
2947eeadcd2aSChia-Yuan Li 		rtw89_ple_imr_enable(rtwdev);
2948eeadcd2aSChia-Yuan Li 		rtw89_pktin_imr_enable(rtwdev);
2949eeadcd2aSChia-Yuan Li 		rtw89_dispatcher_imr_enable(rtwdev);
2950eeadcd2aSChia-Yuan Li 		rtw89_cpuio_imr_enable(rtwdev);
2951eeadcd2aSChia-Yuan Li 		rtw89_bbrpt_imr_enable(rtwdev);
2952e3ec7017SPing-Ke Shih 	} else if (sel == RTW89_CMAC_SEL) {
2953d86369e9SChia-Yuan Li 		rtw89_scheduler_imr_enable(rtwdev, mac_idx);
2954d86369e9SChia-Yuan Li 		rtw89_ptcl_imr_enable(rtwdev, mac_idx);
2955d86369e9SChia-Yuan Li 		rtw89_cdma_imr_enable(rtwdev, mac_idx);
2956d86369e9SChia-Yuan Li 		rtw89_phy_intf_imr_enable(rtwdev, mac_idx);
2957d86369e9SChia-Yuan Li 		rtw89_rmac_imr_enable(rtwdev, mac_idx);
2958d86369e9SChia-Yuan Li 		rtw89_tmac_imr_enable(rtwdev, mac_idx);
2959e3ec7017SPing-Ke Shih 	} else {
2960e3ec7017SPing-Ke Shih 		return -EINVAL;
2961e3ec7017SPing-Ke Shih 	}
2962e3ec7017SPing-Ke Shih 
2963e3ec7017SPing-Ke Shih 	return 0;
2964e3ec7017SPing-Ke Shih }
2965e3ec7017SPing-Ke Shih 
29669f405b01SPing-Ke Shih static void rtw89_mac_err_imr_ctrl(struct rtw89_dev *rtwdev, bool en)
29679f405b01SPing-Ke Shih {
29689f405b01SPing-Ke Shih 	enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
29699f405b01SPing-Ke Shih 
29709f405b01SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_DMAC_ERR_IMR,
29719f405b01SPing-Ke Shih 		      en ? DMAC_ERR_IMR_EN : DMAC_ERR_IMR_DIS);
29729f405b01SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_CMAC_ERR_IMR,
29739f405b01SPing-Ke Shih 		      en ? CMAC0_ERR_IMR_EN : CMAC0_ERR_IMR_DIS);
29749f405b01SPing-Ke Shih 	if (chip_id != RTL8852B && rtwdev->mac.dle_info.c1_rx_qta)
29759f405b01SPing-Ke Shih 		rtw89_write32(rtwdev, R_AX_CMAC_ERR_IMR_C1,
29769f405b01SPing-Ke Shih 			      en ? CMAC1_ERR_IMR_EN : CMAC1_ERR_IMR_DIS);
29779f405b01SPing-Ke Shih }
29789f405b01SPing-Ke Shih 
2979e3ec7017SPing-Ke Shih static int rtw89_mac_dbcc_enable(struct rtw89_dev *rtwdev, bool enable)
2980e3ec7017SPing-Ke Shih {
2981e3ec7017SPing-Ke Shih 	int ret = 0;
2982e3ec7017SPing-Ke Shih 
2983e3ec7017SPing-Ke Shih 	if (enable) {
2984e3ec7017SPing-Ke Shih 		ret = band1_enable(rtwdev);
2985e3ec7017SPing-Ke Shih 		if (ret) {
2986e3ec7017SPing-Ke Shih 			rtw89_err(rtwdev, "[ERR] band1_enable %d\n", ret);
2987e3ec7017SPing-Ke Shih 			return ret;
2988e3ec7017SPing-Ke Shih 		}
2989e3ec7017SPing-Ke Shih 
2990e3ec7017SPing-Ke Shih 		ret = rtw89_mac_enable_imr(rtwdev, RTW89_MAC_1, RTW89_CMAC_SEL);
2991e3ec7017SPing-Ke Shih 		if (ret) {
2992e3ec7017SPing-Ke Shih 			rtw89_err(rtwdev, "[ERR] enable CMAC1 IMR %d\n", ret);
2993e3ec7017SPing-Ke Shih 			return ret;
2994e3ec7017SPing-Ke Shih 		}
2995e3ec7017SPing-Ke Shih 	} else {
2996e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR] disable dbcc is not implemented not\n");
2997e3ec7017SPing-Ke Shih 		return -EINVAL;
2998e3ec7017SPing-Ke Shih 	}
2999e3ec7017SPing-Ke Shih 
3000e3ec7017SPing-Ke Shih 	return 0;
3001e3ec7017SPing-Ke Shih }
3002e3ec7017SPing-Ke Shih 
3003e3ec7017SPing-Ke Shih static int set_host_rpr(struct rtw89_dev *rtwdev)
3004e3ec7017SPing-Ke Shih {
3005e3ec7017SPing-Ke Shih 	if (rtwdev->hci.type == RTW89_HCI_TYPE_PCIE) {
3006e3ec7017SPing-Ke Shih 		rtw89_write32_mask(rtwdev, R_AX_WDRLS_CFG,
3007e3ec7017SPing-Ke Shih 				   B_AX_WDRLS_MODE_MASK, RTW89_RPR_MODE_POH);
3008e3ec7017SPing-Ke Shih 		rtw89_write32_set(rtwdev, R_AX_RLSRPT0_CFG0,
3009e3ec7017SPing-Ke Shih 				  B_AX_RLSRPT0_FLTR_MAP_MASK);
3010e3ec7017SPing-Ke Shih 	} else {
3011e3ec7017SPing-Ke Shih 		rtw89_write32_mask(rtwdev, R_AX_WDRLS_CFG,
3012e3ec7017SPing-Ke Shih 				   B_AX_WDRLS_MODE_MASK, RTW89_RPR_MODE_STF);
3013e3ec7017SPing-Ke Shih 		rtw89_write32_clr(rtwdev, R_AX_RLSRPT0_CFG0,
3014e3ec7017SPing-Ke Shih 				  B_AX_RLSRPT0_FLTR_MAP_MASK);
3015e3ec7017SPing-Ke Shih 	}
3016e3ec7017SPing-Ke Shih 
3017e3ec7017SPing-Ke Shih 	rtw89_write32_mask(rtwdev, R_AX_RLSRPT0_CFG1, B_AX_RLSRPT0_AGGNUM_MASK, 30);
3018e3ec7017SPing-Ke Shih 	rtw89_write32_mask(rtwdev, R_AX_RLSRPT0_CFG1, B_AX_RLSRPT0_TO_MASK, 255);
3019e3ec7017SPing-Ke Shih 
3020e3ec7017SPing-Ke Shih 	return 0;
3021e3ec7017SPing-Ke Shih }
3022e3ec7017SPing-Ke Shih 
3023e3ec7017SPing-Ke Shih static int rtw89_mac_trx_init(struct rtw89_dev *rtwdev)
3024e3ec7017SPing-Ke Shih {
3025e3ec7017SPing-Ke Shih 	enum rtw89_qta_mode qta_mode = rtwdev->mac.qta_mode;
3026e3ec7017SPing-Ke Shih 	int ret;
3027e3ec7017SPing-Ke Shih 
3028e3ec7017SPing-Ke Shih 	ret = dmac_init(rtwdev, 0);
3029e3ec7017SPing-Ke Shih 	if (ret) {
3030e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]DMAC init %d\n", ret);
3031e3ec7017SPing-Ke Shih 		return ret;
3032e3ec7017SPing-Ke Shih 	}
3033e3ec7017SPing-Ke Shih 
3034e3ec7017SPing-Ke Shih 	ret = cmac_init(rtwdev, 0);
3035e3ec7017SPing-Ke Shih 	if (ret) {
3036e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]CMAC%d init %d\n", 0, ret);
3037e3ec7017SPing-Ke Shih 		return ret;
3038e3ec7017SPing-Ke Shih 	}
3039e3ec7017SPing-Ke Shih 
3040e3ec7017SPing-Ke Shih 	if (is_qta_dbcc(rtwdev, qta_mode)) {
3041e3ec7017SPing-Ke Shih 		ret = rtw89_mac_dbcc_enable(rtwdev, true);
3042e3ec7017SPing-Ke Shih 		if (ret) {
3043e3ec7017SPing-Ke Shih 			rtw89_err(rtwdev, "[ERR]dbcc_enable init %d\n", ret);
3044e3ec7017SPing-Ke Shih 			return ret;
3045e3ec7017SPing-Ke Shih 		}
3046e3ec7017SPing-Ke Shih 	}
3047e3ec7017SPing-Ke Shih 
3048e3ec7017SPing-Ke Shih 	ret = rtw89_mac_enable_imr(rtwdev, RTW89_MAC_0, RTW89_DMAC_SEL);
3049e3ec7017SPing-Ke Shih 	if (ret) {
3050e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR] enable DMAC IMR %d\n", ret);
3051e3ec7017SPing-Ke Shih 		return ret;
3052e3ec7017SPing-Ke Shih 	}
3053e3ec7017SPing-Ke Shih 
3054e3ec7017SPing-Ke Shih 	ret = rtw89_mac_enable_imr(rtwdev, RTW89_MAC_0, RTW89_CMAC_SEL);
3055e3ec7017SPing-Ke Shih 	if (ret) {
3056e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR] to enable CMAC0 IMR %d\n", ret);
3057e3ec7017SPing-Ke Shih 		return ret;
3058e3ec7017SPing-Ke Shih 	}
3059e3ec7017SPing-Ke Shih 
30609f405b01SPing-Ke Shih 	rtw89_mac_err_imr_ctrl(rtwdev, true);
30619f405b01SPing-Ke Shih 
3062e3ec7017SPing-Ke Shih 	ret = set_host_rpr(rtwdev);
3063e3ec7017SPing-Ke Shih 	if (ret) {
3064e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR] set host rpr %d\n", ret);
3065e3ec7017SPing-Ke Shih 		return ret;
3066e3ec7017SPing-Ke Shih 	}
3067e3ec7017SPing-Ke Shih 
3068e3ec7017SPing-Ke Shih 	return 0;
3069e3ec7017SPing-Ke Shih }
3070e3ec7017SPing-Ke Shih 
3071ec356ffbSChia-Yuan Li static void rtw89_disable_fw_watchdog(struct rtw89_dev *rtwdev)
3072ec356ffbSChia-Yuan Li {
3073ec356ffbSChia-Yuan Li 	u32 val32;
3074ec356ffbSChia-Yuan Li 
3075ec356ffbSChia-Yuan Li 	rtw89_mac_mem_write(rtwdev, R_AX_WDT_CTRL,
3076ec356ffbSChia-Yuan Li 			    WDT_CTRL_ALL_DIS, RTW89_MAC_MEM_CPU_LOCAL);
3077ec356ffbSChia-Yuan Li 
3078ec356ffbSChia-Yuan Li 	val32 = rtw89_mac_mem_read(rtwdev, R_AX_WDT_STATUS, RTW89_MAC_MEM_CPU_LOCAL);
3079ec356ffbSChia-Yuan Li 	val32 |= B_AX_FS_WDT_INT;
3080ec356ffbSChia-Yuan Li 	val32 &= ~B_AX_FS_WDT_INT_MSK;
3081ec356ffbSChia-Yuan Li 	rtw89_mac_mem_write(rtwdev, R_AX_WDT_STATUS, val32, RTW89_MAC_MEM_CPU_LOCAL);
3082ec356ffbSChia-Yuan Li }
3083ec356ffbSChia-Yuan Li 
3084e3ec7017SPing-Ke Shih static void rtw89_mac_disable_cpu(struct rtw89_dev *rtwdev)
3085e3ec7017SPing-Ke Shih {
3086e3ec7017SPing-Ke Shih 	clear_bit(RTW89_FLAG_FW_RDY, rtwdev->flags);
3087e3ec7017SPing-Ke Shih 
3088e3ec7017SPing-Ke Shih 	rtw89_write32_clr(rtwdev, R_AX_PLATFORM_ENABLE, B_AX_WCPU_EN);
3089de78869dSChia-Yuan Li 	rtw89_write32_clr(rtwdev, R_AX_WCPU_FW_CTRL, B_AX_WCPU_FWDL_EN |
3090de78869dSChia-Yuan Li 			  B_AX_H2C_PATH_RDY | B_AX_FWDL_PATH_RDY);
3091e3ec7017SPing-Ke Shih 	rtw89_write32_clr(rtwdev, R_AX_SYS_CLK_CTRL, B_AX_CPU_CLK_EN);
3092ec356ffbSChia-Yuan Li 
3093ec356ffbSChia-Yuan Li 	rtw89_disable_fw_watchdog(rtwdev);
3094ec356ffbSChia-Yuan Li 
3095de78869dSChia-Yuan Li 	rtw89_write32_clr(rtwdev, R_AX_PLATFORM_ENABLE, B_AX_PLATFORM_EN);
3096de78869dSChia-Yuan Li 	rtw89_write32_set(rtwdev, R_AX_PLATFORM_ENABLE, B_AX_PLATFORM_EN);
3097e3ec7017SPing-Ke Shih }
3098e3ec7017SPing-Ke Shih 
3099e3ec7017SPing-Ke Shih static int rtw89_mac_enable_cpu(struct rtw89_dev *rtwdev, u8 boot_reason,
3100e3ec7017SPing-Ke Shih 				bool dlfw)
3101e3ec7017SPing-Ke Shih {
3102e3ec7017SPing-Ke Shih 	u32 val;
3103e3ec7017SPing-Ke Shih 	int ret;
3104e3ec7017SPing-Ke Shih 
3105e3ec7017SPing-Ke Shih 	if (rtw89_read32(rtwdev, R_AX_PLATFORM_ENABLE) & B_AX_WCPU_EN)
3106e3ec7017SPing-Ke Shih 		return -EFAULT;
3107e3ec7017SPing-Ke Shih 
3108e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_HALT_H2C_CTRL, 0);
3109e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_HALT_C2H_CTRL, 0);
3110a63ae673SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_HALT_H2C, 0);
3111a63ae673SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_HALT_C2H, 0);
3112e3ec7017SPing-Ke Shih 
3113e3ec7017SPing-Ke Shih 	rtw89_write32_set(rtwdev, R_AX_SYS_CLK_CTRL, B_AX_CPU_CLK_EN);
3114e3ec7017SPing-Ke Shih 
3115e3ec7017SPing-Ke Shih 	val = rtw89_read32(rtwdev, R_AX_WCPU_FW_CTRL);
3116e3ec7017SPing-Ke Shih 	val &= ~(B_AX_WCPU_FWDL_EN | B_AX_H2C_PATH_RDY | B_AX_FWDL_PATH_RDY);
3117e3ec7017SPing-Ke Shih 	val = u32_replace_bits(val, RTW89_FWDL_INITIAL_STATE,
3118e3ec7017SPing-Ke Shih 			       B_AX_WCPU_FWDL_STS_MASK);
3119e3ec7017SPing-Ke Shih 
3120e3ec7017SPing-Ke Shih 	if (dlfw)
3121e3ec7017SPing-Ke Shih 		val |= B_AX_WCPU_FWDL_EN;
3122e3ec7017SPing-Ke Shih 
3123e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_WCPU_FW_CTRL, val);
3124e3ec7017SPing-Ke Shih 	rtw89_write16_mask(rtwdev, R_AX_BOOT_REASON, B_AX_BOOT_REASON_MASK,
3125e3ec7017SPing-Ke Shih 			   boot_reason);
3126e3ec7017SPing-Ke Shih 	rtw89_write32_set(rtwdev, R_AX_PLATFORM_ENABLE, B_AX_WCPU_EN);
3127e3ec7017SPing-Ke Shih 
3128e3ec7017SPing-Ke Shih 	if (!dlfw) {
3129e3ec7017SPing-Ke Shih 		mdelay(5);
3130e3ec7017SPing-Ke Shih 
3131e3ec7017SPing-Ke Shih 		ret = rtw89_fw_check_rdy(rtwdev);
3132e3ec7017SPing-Ke Shih 		if (ret)
3133e3ec7017SPing-Ke Shih 			return ret;
3134e3ec7017SPing-Ke Shih 	}
3135e3ec7017SPing-Ke Shih 
3136e3ec7017SPing-Ke Shih 	return 0;
3137e3ec7017SPing-Ke Shih }
3138e3ec7017SPing-Ke Shih 
3139a7d82a7aSPing-Ke Shih static int rtw89_mac_dmac_pre_init(struct rtw89_dev *rtwdev)
3140e3ec7017SPing-Ke Shih {
3141a7d82a7aSPing-Ke Shih 	enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
3142e3ec7017SPing-Ke Shih 	u32 val;
3143e3ec7017SPing-Ke Shih 	int ret;
3144e3ec7017SPing-Ke Shih 
3145a7d82a7aSPing-Ke Shih 	if (chip_id == RTL8852C)
3146a7d82a7aSPing-Ke Shih 		val = B_AX_MAC_FUNC_EN | B_AX_DMAC_FUNC_EN | B_AX_DISPATCHER_EN |
3147a7d82a7aSPing-Ke Shih 		      B_AX_PKT_BUF_EN | B_AX_H_AXIDMA_EN;
3148a7d82a7aSPing-Ke Shih 	else
3149e3ec7017SPing-Ke Shih 		val = B_AX_MAC_FUNC_EN | B_AX_DMAC_FUNC_EN | B_AX_DISPATCHER_EN |
3150e3ec7017SPing-Ke Shih 		      B_AX_PKT_BUF_EN;
3151e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_DMAC_FUNC_EN, val);
3152e3ec7017SPing-Ke Shih 
3153e3ec7017SPing-Ke Shih 	val = B_AX_DISPATCHER_CLK_EN;
3154e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_DMAC_CLK_EN, val);
3155e3ec7017SPing-Ke Shih 
3156a7d82a7aSPing-Ke Shih 	if (chip_id != RTL8852C)
3157a7d82a7aSPing-Ke Shih 		goto dle;
3158a7d82a7aSPing-Ke Shih 
3159a7d82a7aSPing-Ke Shih 	val = rtw89_read32(rtwdev, R_AX_HAXI_INIT_CFG1);
3160a7d82a7aSPing-Ke Shih 	val &= ~(B_AX_DMA_MODE_MASK | B_AX_STOP_AXI_MST);
3161a7d82a7aSPing-Ke Shih 	val |= FIELD_PREP(B_AX_DMA_MODE_MASK, DMA_MOD_PCIE_1B) |
3162a7d82a7aSPing-Ke Shih 	       B_AX_TXHCI_EN_V1 | B_AX_RXHCI_EN_V1;
3163a7d82a7aSPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_HAXI_INIT_CFG1, val);
3164a7d82a7aSPing-Ke Shih 
3165a7d82a7aSPing-Ke Shih 	rtw89_write32_clr(rtwdev, R_AX_HAXI_DMA_STOP1,
3166a7d82a7aSPing-Ke Shih 			  B_AX_STOP_ACH0 | B_AX_STOP_ACH1 | B_AX_STOP_ACH3 |
3167a7d82a7aSPing-Ke Shih 			  B_AX_STOP_ACH4 | B_AX_STOP_ACH5 | B_AX_STOP_ACH6 |
3168a7d82a7aSPing-Ke Shih 			  B_AX_STOP_ACH7 | B_AX_STOP_CH8 | B_AX_STOP_CH9 |
3169a7d82a7aSPing-Ke Shih 			  B_AX_STOP_CH12 | B_AX_STOP_ACH2);
3170a7d82a7aSPing-Ke Shih 	rtw89_write32_clr(rtwdev, R_AX_HAXI_DMA_STOP2, B_AX_STOP_CH10 | B_AX_STOP_CH11);
3171a7d82a7aSPing-Ke Shih 	rtw89_write32_set(rtwdev, R_AX_PLATFORM_ENABLE, B_AX_AXIDMA_EN);
3172a7d82a7aSPing-Ke Shih 
3173a7d82a7aSPing-Ke Shih dle:
3174e3ec7017SPing-Ke Shih 	ret = dle_init(rtwdev, RTW89_QTA_DLFW, rtwdev->mac.qta_mode);
3175e3ec7017SPing-Ke Shih 	if (ret) {
3176e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]DLE pre init %d\n", ret);
3177e3ec7017SPing-Ke Shih 		return ret;
3178e3ec7017SPing-Ke Shih 	}
3179e3ec7017SPing-Ke Shih 
3180e3ec7017SPing-Ke Shih 	ret = hfc_init(rtwdev, true, false, true);
3181e3ec7017SPing-Ke Shih 	if (ret) {
3182e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[ERR]HCI FC pre init %d\n", ret);
3183e3ec7017SPing-Ke Shih 		return ret;
3184e3ec7017SPing-Ke Shih 	}
3185e3ec7017SPing-Ke Shih 
3186e3ec7017SPing-Ke Shih 	return ret;
3187e3ec7017SPing-Ke Shih }
3188e3ec7017SPing-Ke Shih 
318961ebeecbSPing-Ke Shih int rtw89_mac_enable_bb_rf(struct rtw89_dev *rtwdev)
3190e3ec7017SPing-Ke Shih {
3191e3ec7017SPing-Ke Shih 	rtw89_write8_set(rtwdev, R_AX_SYS_FUNC_EN,
3192e3ec7017SPing-Ke Shih 			 B_AX_FEN_BBRSTB | B_AX_FEN_BB_GLB_RSTN);
3193e3ec7017SPing-Ke Shih 	rtw89_write32_set(rtwdev, R_AX_WLRF_CTRL,
3194e3ec7017SPing-Ke Shih 			  B_AX_WLRF1_CTRL_7 | B_AX_WLRF1_CTRL_1 |
3195e3ec7017SPing-Ke Shih 			  B_AX_WLRF_CTRL_7 | B_AX_WLRF_CTRL_1);
3196e3ec7017SPing-Ke Shih 	rtw89_write8_set(rtwdev, R_AX_PHYREG_SET, PHYREG_SET_ALL_CYCLE);
319761ebeecbSPing-Ke Shih 
319861ebeecbSPing-Ke Shih 	return 0;
3199e3ec7017SPing-Ke Shih }
320061ebeecbSPing-Ke Shih EXPORT_SYMBOL(rtw89_mac_enable_bb_rf);
3201e3ec7017SPing-Ke Shih 
3202e3ec7017SPing-Ke Shih void rtw89_mac_disable_bb_rf(struct rtw89_dev *rtwdev)
3203e3ec7017SPing-Ke Shih {
3204e3ec7017SPing-Ke Shih 	rtw89_write8_clr(rtwdev, R_AX_SYS_FUNC_EN,
3205e3ec7017SPing-Ke Shih 			 B_AX_FEN_BBRSTB | B_AX_FEN_BB_GLB_RSTN);
3206e3ec7017SPing-Ke Shih 	rtw89_write32_clr(rtwdev, R_AX_WLRF_CTRL,
3207e3ec7017SPing-Ke Shih 			  B_AX_WLRF1_CTRL_7 | B_AX_WLRF1_CTRL_1 |
3208e3ec7017SPing-Ke Shih 			  B_AX_WLRF_CTRL_7 | B_AX_WLRF_CTRL_1);
3209e3ec7017SPing-Ke Shih 	rtw89_write8_clr(rtwdev, R_AX_PHYREG_SET, PHYREG_SET_ALL_CYCLE);
3210e3ec7017SPing-Ke Shih }
321161ebeecbSPing-Ke Shih EXPORT_SYMBOL(rtw89_mac_disable_bb_rf);
3212e3ec7017SPing-Ke Shih 
3213e3ec7017SPing-Ke Shih int rtw89_mac_partial_init(struct rtw89_dev *rtwdev)
3214e3ec7017SPing-Ke Shih {
3215e3ec7017SPing-Ke Shih 	int ret;
3216e3ec7017SPing-Ke Shih 
3217e3ec7017SPing-Ke Shih 	ret = rtw89_mac_power_switch(rtwdev, true);
3218e3ec7017SPing-Ke Shih 	if (ret) {
3219e3ec7017SPing-Ke Shih 		rtw89_mac_power_switch(rtwdev, false);
3220e3ec7017SPing-Ke Shih 		ret = rtw89_mac_power_switch(rtwdev, true);
3221e3ec7017SPing-Ke Shih 		if (ret)
3222e3ec7017SPing-Ke Shih 			return ret;
3223e3ec7017SPing-Ke Shih 	}
3224e3ec7017SPing-Ke Shih 
322552f12705SChin-Yen Lee 	rtw89_mac_ctrl_hci_dma_trx(rtwdev, true);
3226e3ec7017SPing-Ke Shih 
3227a7d82a7aSPing-Ke Shih 	ret = rtw89_mac_dmac_pre_init(rtwdev);
3228a7d82a7aSPing-Ke Shih 	if (ret)
3229a7d82a7aSPing-Ke Shih 		return ret;
3230a7d82a7aSPing-Ke Shih 
3231e3ec7017SPing-Ke Shih 	if (rtwdev->hci.ops->mac_pre_init) {
3232e3ec7017SPing-Ke Shih 		ret = rtwdev->hci.ops->mac_pre_init(rtwdev);
3233e3ec7017SPing-Ke Shih 		if (ret)
3234e3ec7017SPing-Ke Shih 			return ret;
3235e3ec7017SPing-Ke Shih 	}
3236e3ec7017SPing-Ke Shih 
3237e3ec7017SPing-Ke Shih 	rtw89_mac_disable_cpu(rtwdev);
3238e3ec7017SPing-Ke Shih 	ret = rtw89_mac_enable_cpu(rtwdev, 0, true);
3239e3ec7017SPing-Ke Shih 	if (ret)
3240e3ec7017SPing-Ke Shih 		return ret;
3241e3ec7017SPing-Ke Shih 
3242e3ec7017SPing-Ke Shih 	ret = rtw89_fw_download(rtwdev, RTW89_FW_NORMAL);
3243e3ec7017SPing-Ke Shih 	if (ret)
3244e3ec7017SPing-Ke Shih 		return ret;
3245e3ec7017SPing-Ke Shih 
3246e3ec7017SPing-Ke Shih 	return 0;
3247e3ec7017SPing-Ke Shih }
3248e3ec7017SPing-Ke Shih 
3249e3ec7017SPing-Ke Shih int rtw89_mac_init(struct rtw89_dev *rtwdev)
3250e3ec7017SPing-Ke Shih {
3251e3ec7017SPing-Ke Shih 	int ret;
3252e3ec7017SPing-Ke Shih 
3253e3ec7017SPing-Ke Shih 	ret = rtw89_mac_partial_init(rtwdev);
3254e3ec7017SPing-Ke Shih 	if (ret)
3255e3ec7017SPing-Ke Shih 		goto fail;
3256e3ec7017SPing-Ke Shih 
325761ebeecbSPing-Ke Shih 	ret = rtw89_chip_enable_bb_rf(rtwdev);
325861ebeecbSPing-Ke Shih 	if (ret)
325961ebeecbSPing-Ke Shih 		goto fail;
3260e3ec7017SPing-Ke Shih 
3261e3ec7017SPing-Ke Shih 	ret = rtw89_mac_sys_init(rtwdev);
3262e3ec7017SPing-Ke Shih 	if (ret)
3263e3ec7017SPing-Ke Shih 		goto fail;
3264e3ec7017SPing-Ke Shih 
3265e3ec7017SPing-Ke Shih 	ret = rtw89_mac_trx_init(rtwdev);
3266e3ec7017SPing-Ke Shih 	if (ret)
3267e3ec7017SPing-Ke Shih 		goto fail;
3268e3ec7017SPing-Ke Shih 
3269e3ec7017SPing-Ke Shih 	if (rtwdev->hci.ops->mac_post_init) {
3270e3ec7017SPing-Ke Shih 		ret = rtwdev->hci.ops->mac_post_init(rtwdev);
3271e3ec7017SPing-Ke Shih 		if (ret)
3272e3ec7017SPing-Ke Shih 			goto fail;
3273e3ec7017SPing-Ke Shih 	}
3274e3ec7017SPing-Ke Shih 
3275e3ec7017SPing-Ke Shih 	rtw89_fw_send_all_early_h2c(rtwdev);
3276e3ec7017SPing-Ke Shih 	rtw89_fw_h2c_set_ofld_cfg(rtwdev);
3277e3ec7017SPing-Ke Shih 
3278e3ec7017SPing-Ke Shih 	return ret;
3279e3ec7017SPing-Ke Shih fail:
3280e3ec7017SPing-Ke Shih 	rtw89_mac_power_switch(rtwdev, false);
3281e3ec7017SPing-Ke Shih 
3282e3ec7017SPing-Ke Shih 	return ret;
3283e3ec7017SPing-Ke Shih }
3284e3ec7017SPing-Ke Shih 
3285e3ec7017SPing-Ke Shih static void rtw89_mac_dmac_tbl_init(struct rtw89_dev *rtwdev, u8 macid)
3286e3ec7017SPing-Ke Shih {
3287e3ec7017SPing-Ke Shih 	u8 i;
3288e3ec7017SPing-Ke Shih 
3289e3ec7017SPing-Ke Shih 	for (i = 0; i < 4; i++) {
3290e3ec7017SPing-Ke Shih 		rtw89_write32(rtwdev, R_AX_FILTER_MODEL_ADDR,
3291e3ec7017SPing-Ke Shih 			      DMAC_TBL_BASE_ADDR + (macid << 4) + (i << 2));
3292e3ec7017SPing-Ke Shih 		rtw89_write32(rtwdev, R_AX_INDIR_ACCESS_ENTRY, 0);
3293e3ec7017SPing-Ke Shih 	}
3294e3ec7017SPing-Ke Shih }
3295e3ec7017SPing-Ke Shih 
3296e3ec7017SPing-Ke Shih static void rtw89_mac_cmac_tbl_init(struct rtw89_dev *rtwdev, u8 macid)
3297e3ec7017SPing-Ke Shih {
3298e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_FILTER_MODEL_ADDR,
3299e3ec7017SPing-Ke Shih 		      CMAC_TBL_BASE_ADDR + macid * CCTL_INFO_SIZE);
3300e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_INDIR_ACCESS_ENTRY, 0x4);
3301e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_INDIR_ACCESS_ENTRY + 4, 0x400A0004);
3302e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_INDIR_ACCESS_ENTRY + 8, 0);
3303e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_INDIR_ACCESS_ENTRY + 12, 0);
3304e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_INDIR_ACCESS_ENTRY + 16, 0);
3305e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_INDIR_ACCESS_ENTRY + 20, 0xE43000B);
3306e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_INDIR_ACCESS_ENTRY + 24, 0);
3307e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_INDIR_ACCESS_ENTRY + 28, 0xB8109);
3308e3ec7017SPing-Ke Shih }
3309e3ec7017SPing-Ke Shih 
33101b73e77dSPing-Ke Shih int rtw89_mac_set_macid_pause(struct rtw89_dev *rtwdev, u8 macid, bool pause)
3311e3ec7017SPing-Ke Shih {
3312e3ec7017SPing-Ke Shih 	u8 sh =  FIELD_GET(GENMASK(4, 0), macid);
3313e3ec7017SPing-Ke Shih 	u8 grp = macid >> 5;
3314e3ec7017SPing-Ke Shih 	int ret;
3315e3ec7017SPing-Ke Shih 
3316e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, RTW89_MAC_0, RTW89_CMAC_SEL);
3317e3ec7017SPing-Ke Shih 	if (ret)
3318e3ec7017SPing-Ke Shih 		return ret;
3319e3ec7017SPing-Ke Shih 
3320e3ec7017SPing-Ke Shih 	rtw89_fw_h2c_macid_pause(rtwdev, sh, grp, pause);
3321e3ec7017SPing-Ke Shih 
3322e3ec7017SPing-Ke Shih 	return 0;
3323e3ec7017SPing-Ke Shih }
3324e3ec7017SPing-Ke Shih 
3325e3ec7017SPing-Ke Shih static const struct rtw89_port_reg rtw_port_base = {
3326e3ec7017SPing-Ke Shih 	.port_cfg = R_AX_PORT_CFG_P0,
3327e3ec7017SPing-Ke Shih 	.tbtt_prohib = R_AX_TBTT_PROHIB_P0,
3328e3ec7017SPing-Ke Shih 	.bcn_area = R_AX_BCN_AREA_P0,
3329e3ec7017SPing-Ke Shih 	.bcn_early = R_AX_BCNERLYINT_CFG_P0,
3330e3ec7017SPing-Ke Shih 	.tbtt_early = R_AX_TBTTERLYINT_CFG_P0,
3331e3ec7017SPing-Ke Shih 	.tbtt_agg = R_AX_TBTT_AGG_P0,
3332e3ec7017SPing-Ke Shih 	.bcn_space = R_AX_BCN_SPACE_CFG_P0,
3333e3ec7017SPing-Ke Shih 	.bcn_forcetx = R_AX_BCN_FORCETX_P0,
3334e3ec7017SPing-Ke Shih 	.bcn_err_cnt = R_AX_BCN_ERR_CNT_P0,
3335e3ec7017SPing-Ke Shih 	.bcn_err_flag = R_AX_BCN_ERR_FLAG_P0,
3336e3ec7017SPing-Ke Shih 	.dtim_ctrl = R_AX_DTIM_CTRL_P0,
3337e3ec7017SPing-Ke Shih 	.tbtt_shift = R_AX_TBTT_SHIFT_P0,
3338e3ec7017SPing-Ke Shih 	.bcn_cnt_tmr = R_AX_BCN_CNT_TMR_P0,
3339e3ec7017SPing-Ke Shih 	.tsftr_l = R_AX_TSFTR_LOW_P0,
3340e3ec7017SPing-Ke Shih 	.tsftr_h = R_AX_TSFTR_HIGH_P0
3341e3ec7017SPing-Ke Shih };
3342e3ec7017SPing-Ke Shih 
3343e3ec7017SPing-Ke Shih #define BCN_INTERVAL 100
3344e3ec7017SPing-Ke Shih #define BCN_ERLY_DEF 160
3345e3ec7017SPing-Ke Shih #define BCN_SETUP_DEF 2
3346e3ec7017SPing-Ke Shih #define BCN_HOLD_DEF 200
3347e3ec7017SPing-Ke Shih #define BCN_MASK_DEF 0
3348e3ec7017SPing-Ke Shih #define TBTT_ERLY_DEF 5
3349e3ec7017SPing-Ke Shih #define BCN_SET_UNIT 32
3350e3ec7017SPing-Ke Shih #define BCN_ERLY_SET_DLY (10 * 2)
3351e3ec7017SPing-Ke Shih 
3352e3ec7017SPing-Ke Shih static void rtw89_mac_port_cfg_func_sw(struct rtw89_dev *rtwdev,
3353e3ec7017SPing-Ke Shih 				       struct rtw89_vif *rtwvif)
3354e3ec7017SPing-Ke Shih {
3355e3ec7017SPing-Ke Shih 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
3356e3ec7017SPing-Ke Shih 	const struct rtw89_port_reg *p = &rtw_port_base;
3357e3ec7017SPing-Ke Shih 
3358e3ec7017SPing-Ke Shih 	if (!rtw89_read32_port_mask(rtwdev, rtwvif, p->port_cfg, B_AX_PORT_FUNC_EN))
3359e3ec7017SPing-Ke Shih 		return;
3360e3ec7017SPing-Ke Shih 
3361e3ec7017SPing-Ke Shih 	rtw89_write32_port_clr(rtwdev, rtwvif, p->tbtt_prohib, B_AX_TBTT_SETUP_MASK);
3362e3ec7017SPing-Ke Shih 	rtw89_write32_port_mask(rtwdev, rtwvif, p->tbtt_prohib, B_AX_TBTT_HOLD_MASK, 1);
3363e3ec7017SPing-Ke Shih 	rtw89_write16_port_clr(rtwdev, rtwvif, p->tbtt_early, B_AX_TBTTERLY_MASK);
3364e3ec7017SPing-Ke Shih 	rtw89_write16_port_clr(rtwdev, rtwvif, p->bcn_early, B_AX_BCNERLY_MASK);
3365e3ec7017SPing-Ke Shih 
3366e3ec7017SPing-Ke Shih 	msleep(vif->bss_conf.beacon_int + 1);
3367e3ec7017SPing-Ke Shih 
3368e3ec7017SPing-Ke Shih 	rtw89_write32_port_clr(rtwdev, rtwvif, p->port_cfg, B_AX_PORT_FUNC_EN |
3369e3ec7017SPing-Ke Shih 							    B_AX_BRK_SETUP);
3370e3ec7017SPing-Ke Shih 	rtw89_write32_port_set(rtwdev, rtwvif, p->port_cfg, B_AX_TSFTR_RST);
3371e3ec7017SPing-Ke Shih 	rtw89_write32_port(rtwdev, rtwvif, p->bcn_cnt_tmr, 0);
3372e3ec7017SPing-Ke Shih }
3373e3ec7017SPing-Ke Shih 
3374e3ec7017SPing-Ke Shih static void rtw89_mac_port_cfg_tx_rpt(struct rtw89_dev *rtwdev,
3375e3ec7017SPing-Ke Shih 				      struct rtw89_vif *rtwvif, bool en)
3376e3ec7017SPing-Ke Shih {
3377e3ec7017SPing-Ke Shih 	const struct rtw89_port_reg *p = &rtw_port_base;
3378e3ec7017SPing-Ke Shih 
3379e3ec7017SPing-Ke Shih 	if (en)
3380e3ec7017SPing-Ke Shih 		rtw89_write32_port_set(rtwdev, rtwvif, p->port_cfg, B_AX_TXBCN_RPT_EN);
3381e3ec7017SPing-Ke Shih 	else
3382e3ec7017SPing-Ke Shih 		rtw89_write32_port_clr(rtwdev, rtwvif, p->port_cfg, B_AX_TXBCN_RPT_EN);
3383e3ec7017SPing-Ke Shih }
3384e3ec7017SPing-Ke Shih 
3385e3ec7017SPing-Ke Shih static void rtw89_mac_port_cfg_rx_rpt(struct rtw89_dev *rtwdev,
3386e3ec7017SPing-Ke Shih 				      struct rtw89_vif *rtwvif, bool en)
3387e3ec7017SPing-Ke Shih {
3388e3ec7017SPing-Ke Shih 	const struct rtw89_port_reg *p = &rtw_port_base;
3389e3ec7017SPing-Ke Shih 
3390e3ec7017SPing-Ke Shih 	if (en)
3391e3ec7017SPing-Ke Shih 		rtw89_write32_port_set(rtwdev, rtwvif, p->port_cfg, B_AX_RXBCN_RPT_EN);
3392e3ec7017SPing-Ke Shih 	else
3393e3ec7017SPing-Ke Shih 		rtw89_write32_port_clr(rtwdev, rtwvif, p->port_cfg, B_AX_RXBCN_RPT_EN);
3394e3ec7017SPing-Ke Shih }
3395e3ec7017SPing-Ke Shih 
3396e3ec7017SPing-Ke Shih static void rtw89_mac_port_cfg_net_type(struct rtw89_dev *rtwdev,
3397e3ec7017SPing-Ke Shih 					struct rtw89_vif *rtwvif)
3398e3ec7017SPing-Ke Shih {
3399e3ec7017SPing-Ke Shih 	const struct rtw89_port_reg *p = &rtw_port_base;
3400e3ec7017SPing-Ke Shih 
3401e3ec7017SPing-Ke Shih 	rtw89_write32_port_mask(rtwdev, rtwvif, p->port_cfg, B_AX_NET_TYPE_MASK,
3402e3ec7017SPing-Ke Shih 				rtwvif->net_type);
3403e3ec7017SPing-Ke Shih }
3404e3ec7017SPing-Ke Shih 
3405e3ec7017SPing-Ke Shih static void rtw89_mac_port_cfg_bcn_prct(struct rtw89_dev *rtwdev,
3406e3ec7017SPing-Ke Shih 					struct rtw89_vif *rtwvif)
3407e3ec7017SPing-Ke Shih {
3408e3ec7017SPing-Ke Shih 	const struct rtw89_port_reg *p = &rtw_port_base;
3409e3ec7017SPing-Ke Shih 	bool en = rtwvif->net_type != RTW89_NET_TYPE_NO_LINK;
3410e3ec7017SPing-Ke Shih 	u32 bits = B_AX_TBTT_PROHIB_EN | B_AX_BRK_SETUP;
3411e3ec7017SPing-Ke Shih 
3412e3ec7017SPing-Ke Shih 	if (en)
3413e3ec7017SPing-Ke Shih 		rtw89_write32_port_set(rtwdev, rtwvif, p->port_cfg, bits);
3414e3ec7017SPing-Ke Shih 	else
3415e3ec7017SPing-Ke Shih 		rtw89_write32_port_clr(rtwdev, rtwvif, p->port_cfg, bits);
3416e3ec7017SPing-Ke Shih }
3417e3ec7017SPing-Ke Shih 
3418e3ec7017SPing-Ke Shih static void rtw89_mac_port_cfg_rx_sw(struct rtw89_dev *rtwdev,
3419e3ec7017SPing-Ke Shih 				     struct rtw89_vif *rtwvif)
3420e3ec7017SPing-Ke Shih {
3421e3ec7017SPing-Ke Shih 	const struct rtw89_port_reg *p = &rtw_port_base;
3422e3ec7017SPing-Ke Shih 	bool en = rtwvif->net_type == RTW89_NET_TYPE_INFRA ||
3423e3ec7017SPing-Ke Shih 		  rtwvif->net_type == RTW89_NET_TYPE_AD_HOC;
3424e3ec7017SPing-Ke Shih 	u32 bit = B_AX_RX_BSSID_FIT_EN;
3425e3ec7017SPing-Ke Shih 
3426e3ec7017SPing-Ke Shih 	if (en)
3427e3ec7017SPing-Ke Shih 		rtw89_write32_port_set(rtwdev, rtwvif, p->port_cfg, bit);
3428e3ec7017SPing-Ke Shih 	else
3429e3ec7017SPing-Ke Shih 		rtw89_write32_port_clr(rtwdev, rtwvif, p->port_cfg, bit);
3430e3ec7017SPing-Ke Shih }
3431e3ec7017SPing-Ke Shih 
3432e3ec7017SPing-Ke Shih static void rtw89_mac_port_cfg_rx_sync(struct rtw89_dev *rtwdev,
3433e3ec7017SPing-Ke Shih 				       struct rtw89_vif *rtwvif)
3434e3ec7017SPing-Ke Shih {
3435e3ec7017SPing-Ke Shih 	const struct rtw89_port_reg *p = &rtw_port_base;
3436e3ec7017SPing-Ke Shih 	bool en = rtwvif->net_type == RTW89_NET_TYPE_INFRA ||
3437e3ec7017SPing-Ke Shih 		  rtwvif->net_type == RTW89_NET_TYPE_AD_HOC;
3438e3ec7017SPing-Ke Shih 
3439e3ec7017SPing-Ke Shih 	if (en)
3440e3ec7017SPing-Ke Shih 		rtw89_write32_port_set(rtwdev, rtwvif, p->port_cfg, B_AX_TSF_UDT_EN);
3441e3ec7017SPing-Ke Shih 	else
3442e3ec7017SPing-Ke Shih 		rtw89_write32_port_clr(rtwdev, rtwvif, p->port_cfg, B_AX_TSF_UDT_EN);
3443e3ec7017SPing-Ke Shih }
3444e3ec7017SPing-Ke Shih 
3445e3ec7017SPing-Ke Shih static void rtw89_mac_port_cfg_tx_sw(struct rtw89_dev *rtwdev,
3446e3ec7017SPing-Ke Shih 				     struct rtw89_vif *rtwvif)
3447e3ec7017SPing-Ke Shih {
3448e3ec7017SPing-Ke Shih 	const struct rtw89_port_reg *p = &rtw_port_base;
3449e3ec7017SPing-Ke Shih 	bool en = rtwvif->net_type == RTW89_NET_TYPE_AP_MODE ||
3450e3ec7017SPing-Ke Shih 		  rtwvif->net_type == RTW89_NET_TYPE_AD_HOC;
3451e3ec7017SPing-Ke Shih 
3452e3ec7017SPing-Ke Shih 	if (en)
3453e3ec7017SPing-Ke Shih 		rtw89_write32_port_set(rtwdev, rtwvif, p->port_cfg, B_AX_BCNTX_EN);
3454e3ec7017SPing-Ke Shih 	else
3455e3ec7017SPing-Ke Shih 		rtw89_write32_port_clr(rtwdev, rtwvif, p->port_cfg, B_AX_BCNTX_EN);
3456e3ec7017SPing-Ke Shih }
3457e3ec7017SPing-Ke Shih 
3458e3ec7017SPing-Ke Shih static void rtw89_mac_port_cfg_bcn_intv(struct rtw89_dev *rtwdev,
3459e3ec7017SPing-Ke Shih 					struct rtw89_vif *rtwvif)
3460e3ec7017SPing-Ke Shih {
3461e3ec7017SPing-Ke Shih 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
3462e3ec7017SPing-Ke Shih 	const struct rtw89_port_reg *p = &rtw_port_base;
3463e3ec7017SPing-Ke Shih 	u16 bcn_int = vif->bss_conf.beacon_int ? vif->bss_conf.beacon_int : BCN_INTERVAL;
3464e3ec7017SPing-Ke Shih 
3465e3ec7017SPing-Ke Shih 	rtw89_write32_port_mask(rtwdev, rtwvif, p->bcn_space, B_AX_BCN_SPACE_MASK,
3466e3ec7017SPing-Ke Shih 				bcn_int);
3467e3ec7017SPing-Ke Shih }
3468e3ec7017SPing-Ke Shih 
3469283c3d88SPing-Ke Shih static void rtw89_mac_port_cfg_hiq_win(struct rtw89_dev *rtwdev,
3470283c3d88SPing-Ke Shih 				       struct rtw89_vif *rtwvif)
3471283c3d88SPing-Ke Shih {
3472283c3d88SPing-Ke Shih 	static const u32 hiq_win_addr[RTW89_PORT_NUM] = {
3473283c3d88SPing-Ke Shih 		R_AX_P0MB_HGQ_WINDOW_CFG_0, R_AX_PORT_HGQ_WINDOW_CFG,
3474283c3d88SPing-Ke Shih 		R_AX_PORT_HGQ_WINDOW_CFG + 1, R_AX_PORT_HGQ_WINDOW_CFG + 2,
3475283c3d88SPing-Ke Shih 		R_AX_PORT_HGQ_WINDOW_CFG + 3,
3476283c3d88SPing-Ke Shih 	};
3477283c3d88SPing-Ke Shih 	u8 win = rtwvif->net_type == RTW89_NET_TYPE_AP_MODE ? 16 : 0;
3478283c3d88SPing-Ke Shih 	u8 port = rtwvif->port;
3479283c3d88SPing-Ke Shih 	u32 reg;
3480283c3d88SPing-Ke Shih 
3481283c3d88SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(hiq_win_addr[port], rtwvif->mac_idx);
3482283c3d88SPing-Ke Shih 	rtw89_write8(rtwdev, reg, win);
3483283c3d88SPing-Ke Shih }
3484283c3d88SPing-Ke Shih 
3485283c3d88SPing-Ke Shih static void rtw89_mac_port_cfg_hiq_dtim(struct rtw89_dev *rtwdev,
3486283c3d88SPing-Ke Shih 					struct rtw89_vif *rtwvif)
3487283c3d88SPing-Ke Shih {
3488283c3d88SPing-Ke Shih 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
3489283c3d88SPing-Ke Shih 	const struct rtw89_port_reg *p = &rtw_port_base;
3490283c3d88SPing-Ke Shih 	u32 addr;
3491283c3d88SPing-Ke Shih 
3492283c3d88SPing-Ke Shih 	addr = rtw89_mac_reg_by_idx(R_AX_MD_TSFT_STMP_CTL, rtwvif->mac_idx);
3493283c3d88SPing-Ke Shih 	rtw89_write8_set(rtwdev, addr, B_AX_UPD_HGQMD | B_AX_UPD_TIMIE);
3494283c3d88SPing-Ke Shih 
3495283c3d88SPing-Ke Shih 	rtw89_write16_port_mask(rtwdev, rtwvif, p->dtim_ctrl, B_AX_DTIM_NUM_MASK,
3496283c3d88SPing-Ke Shih 				vif->bss_conf.dtim_period);
3497283c3d88SPing-Ke Shih }
3498283c3d88SPing-Ke Shih 
3499e3ec7017SPing-Ke Shih static void rtw89_mac_port_cfg_bcn_setup_time(struct rtw89_dev *rtwdev,
3500e3ec7017SPing-Ke Shih 					      struct rtw89_vif *rtwvif)
3501e3ec7017SPing-Ke Shih {
3502e3ec7017SPing-Ke Shih 	const struct rtw89_port_reg *p = &rtw_port_base;
3503e3ec7017SPing-Ke Shih 
3504e3ec7017SPing-Ke Shih 	rtw89_write32_port_mask(rtwdev, rtwvif, p->tbtt_prohib,
3505e3ec7017SPing-Ke Shih 				B_AX_TBTT_SETUP_MASK, BCN_SETUP_DEF);
3506e3ec7017SPing-Ke Shih }
3507e3ec7017SPing-Ke Shih 
3508e3ec7017SPing-Ke Shih static void rtw89_mac_port_cfg_bcn_hold_time(struct rtw89_dev *rtwdev,
3509e3ec7017SPing-Ke Shih 					     struct rtw89_vif *rtwvif)
3510e3ec7017SPing-Ke Shih {
3511e3ec7017SPing-Ke Shih 	const struct rtw89_port_reg *p = &rtw_port_base;
3512e3ec7017SPing-Ke Shih 
3513e3ec7017SPing-Ke Shih 	rtw89_write32_port_mask(rtwdev, rtwvif, p->tbtt_prohib,
3514e3ec7017SPing-Ke Shih 				B_AX_TBTT_HOLD_MASK, BCN_HOLD_DEF);
3515e3ec7017SPing-Ke Shih }
3516e3ec7017SPing-Ke Shih 
3517e3ec7017SPing-Ke Shih static void rtw89_mac_port_cfg_bcn_mask_area(struct rtw89_dev *rtwdev,
3518e3ec7017SPing-Ke Shih 					     struct rtw89_vif *rtwvif)
3519e3ec7017SPing-Ke Shih {
3520e3ec7017SPing-Ke Shih 	const struct rtw89_port_reg *p = &rtw_port_base;
3521e3ec7017SPing-Ke Shih 
3522e3ec7017SPing-Ke Shih 	rtw89_write32_port_mask(rtwdev, rtwvif, p->bcn_area,
3523e3ec7017SPing-Ke Shih 				B_AX_BCN_MSK_AREA_MASK, BCN_MASK_DEF);
3524e3ec7017SPing-Ke Shih }
3525e3ec7017SPing-Ke Shih 
3526e3ec7017SPing-Ke Shih static void rtw89_mac_port_cfg_tbtt_early(struct rtw89_dev *rtwdev,
3527e3ec7017SPing-Ke Shih 					  struct rtw89_vif *rtwvif)
3528e3ec7017SPing-Ke Shih {
3529e3ec7017SPing-Ke Shih 	const struct rtw89_port_reg *p = &rtw_port_base;
3530e3ec7017SPing-Ke Shih 
3531e3ec7017SPing-Ke Shih 	rtw89_write16_port_mask(rtwdev, rtwvif, p->tbtt_early,
3532e3ec7017SPing-Ke Shih 				B_AX_TBTTERLY_MASK, TBTT_ERLY_DEF);
3533e3ec7017SPing-Ke Shih }
3534e3ec7017SPing-Ke Shih 
3535e3ec7017SPing-Ke Shih static void rtw89_mac_port_cfg_bss_color(struct rtw89_dev *rtwdev,
3536e3ec7017SPing-Ke Shih 					 struct rtw89_vif *rtwvif)
3537e3ec7017SPing-Ke Shih {
3538e3ec7017SPing-Ke Shih 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
3539e3ec7017SPing-Ke Shih 	static const u32 masks[RTW89_PORT_NUM] = {
3540e3ec7017SPing-Ke Shih 		B_AX_BSS_COLOB_AX_PORT_0_MASK, B_AX_BSS_COLOB_AX_PORT_1_MASK,
3541e3ec7017SPing-Ke Shih 		B_AX_BSS_COLOB_AX_PORT_2_MASK, B_AX_BSS_COLOB_AX_PORT_3_MASK,
3542e3ec7017SPing-Ke Shih 		B_AX_BSS_COLOB_AX_PORT_4_MASK,
3543e3ec7017SPing-Ke Shih 	};
3544e3ec7017SPing-Ke Shih 	u8 port = rtwvif->port;
3545e3ec7017SPing-Ke Shih 	u32 reg_base;
3546e3ec7017SPing-Ke Shih 	u32 reg;
3547e3ec7017SPing-Ke Shih 	u8 bss_color;
3548e3ec7017SPing-Ke Shih 
3549e3ec7017SPing-Ke Shih 	bss_color = vif->bss_conf.he_bss_color.color;
3550e3ec7017SPing-Ke Shih 	reg_base = port >= 4 ? R_AX_PTCL_BSS_COLOR_1 : R_AX_PTCL_BSS_COLOR_0;
3551e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(reg_base, rtwvif->mac_idx);
3552e3ec7017SPing-Ke Shih 	rtw89_write32_mask(rtwdev, reg, masks[port], bss_color);
3553e3ec7017SPing-Ke Shih }
3554e3ec7017SPing-Ke Shih 
3555e3ec7017SPing-Ke Shih static void rtw89_mac_port_cfg_mbssid(struct rtw89_dev *rtwdev,
3556e3ec7017SPing-Ke Shih 				      struct rtw89_vif *rtwvif)
3557e3ec7017SPing-Ke Shih {
3558e3ec7017SPing-Ke Shih 	u8 port = rtwvif->port;
3559e3ec7017SPing-Ke Shih 	u32 reg;
3560e3ec7017SPing-Ke Shih 
3561e3ec7017SPing-Ke Shih 	if (rtwvif->net_type == RTW89_NET_TYPE_AP_MODE)
3562e3ec7017SPing-Ke Shih 		return;
3563e3ec7017SPing-Ke Shih 
3564e3ec7017SPing-Ke Shih 	if (port == 0) {
3565e3ec7017SPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(R_AX_MBSSID_CTRL, rtwvif->mac_idx);
3566e3ec7017SPing-Ke Shih 		rtw89_write32_clr(rtwdev, reg, B_AX_P0MB_ALL_MASK);
3567e3ec7017SPing-Ke Shih 	}
3568e3ec7017SPing-Ke Shih }
3569e3ec7017SPing-Ke Shih 
3570e3ec7017SPing-Ke Shih static void rtw89_mac_port_cfg_hiq_drop(struct rtw89_dev *rtwdev,
3571e3ec7017SPing-Ke Shih 					struct rtw89_vif *rtwvif)
3572e3ec7017SPing-Ke Shih {
3573e3ec7017SPing-Ke Shih 	u8 port = rtwvif->port;
3574e3ec7017SPing-Ke Shih 	u32 reg;
3575e3ec7017SPing-Ke Shih 	u32 val;
3576e3ec7017SPing-Ke Shih 
3577e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_MBSSID_DROP_0, rtwvif->mac_idx);
3578e3ec7017SPing-Ke Shih 	val = rtw89_read32(rtwdev, reg);
3579e3ec7017SPing-Ke Shih 	val &= ~FIELD_PREP(B_AX_PORT_DROP_4_0_MASK, BIT(port));
3580e3ec7017SPing-Ke Shih 	if (port == 0)
3581e3ec7017SPing-Ke Shih 		val &= ~BIT(0);
3582e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, reg, val);
3583e3ec7017SPing-Ke Shih }
3584e3ec7017SPing-Ke Shih 
3585e3ec7017SPing-Ke Shih static void rtw89_mac_port_cfg_func_en(struct rtw89_dev *rtwdev,
3586e3ec7017SPing-Ke Shih 				       struct rtw89_vif *rtwvif)
3587e3ec7017SPing-Ke Shih {
3588e3ec7017SPing-Ke Shih 	const struct rtw89_port_reg *p = &rtw_port_base;
3589e3ec7017SPing-Ke Shih 
3590e3ec7017SPing-Ke Shih 	rtw89_write32_port_set(rtwdev, rtwvif, p->port_cfg, B_AX_PORT_FUNC_EN);
3591e3ec7017SPing-Ke Shih }
3592e3ec7017SPing-Ke Shih 
3593e3ec7017SPing-Ke Shih static void rtw89_mac_port_cfg_bcn_early(struct rtw89_dev *rtwdev,
3594e3ec7017SPing-Ke Shih 					 struct rtw89_vif *rtwvif)
3595e3ec7017SPing-Ke Shih {
3596e3ec7017SPing-Ke Shih 	const struct rtw89_port_reg *p = &rtw_port_base;
3597e3ec7017SPing-Ke Shih 
3598e3ec7017SPing-Ke Shih 	rtw89_write32_port_mask(rtwdev, rtwvif, p->bcn_early, B_AX_BCNERLY_MASK,
3599e3ec7017SPing-Ke Shih 				BCN_ERLY_DEF);
3600e3ec7017SPing-Ke Shih }
3601e3ec7017SPing-Ke Shih 
3602704052f5SChia-Yuan Li static void rtw89_mac_port_cfg_tbtt_shift(struct rtw89_dev *rtwdev,
3603704052f5SChia-Yuan Li 					  struct rtw89_vif *rtwvif)
3604704052f5SChia-Yuan Li {
3605704052f5SChia-Yuan Li 	const struct rtw89_port_reg *p = &rtw_port_base;
3606704052f5SChia-Yuan Li 	u16 val;
3607704052f5SChia-Yuan Li 
3608704052f5SChia-Yuan Li 	if (rtwdev->chip->chip_id != RTL8852C)
3609704052f5SChia-Yuan Li 		return;
3610704052f5SChia-Yuan Li 
3611704052f5SChia-Yuan Li 	if (rtwvif->wifi_role != RTW89_WIFI_ROLE_P2P_CLIENT &&
3612704052f5SChia-Yuan Li 	    rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION)
3613704052f5SChia-Yuan Li 		return;
3614704052f5SChia-Yuan Li 
3615704052f5SChia-Yuan Li 	val = FIELD_PREP(B_AX_TBTT_SHIFT_OFST_MAG, 1) |
3616704052f5SChia-Yuan Li 			 B_AX_TBTT_SHIFT_OFST_SIGN;
3617704052f5SChia-Yuan Li 
3618704052f5SChia-Yuan Li 	rtw89_write16_port_mask(rtwdev, rtwvif, p->tbtt_shift,
3619704052f5SChia-Yuan Li 				B_AX_TBTT_SHIFT_OFST_MASK, val);
3620704052f5SChia-Yuan Li }
3621704052f5SChia-Yuan Li 
3622e3ec7017SPing-Ke Shih int rtw89_mac_vif_init(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
3623e3ec7017SPing-Ke Shih {
3624e3ec7017SPing-Ke Shih 	int ret;
3625e3ec7017SPing-Ke Shih 
3626e3ec7017SPing-Ke Shih 	ret = rtw89_mac_port_update(rtwdev, rtwvif);
3627e3ec7017SPing-Ke Shih 	if (ret)
3628e3ec7017SPing-Ke Shih 		return ret;
3629e3ec7017SPing-Ke Shih 
3630e3ec7017SPing-Ke Shih 	rtw89_mac_dmac_tbl_init(rtwdev, rtwvif->mac_id);
3631e3ec7017SPing-Ke Shih 	rtw89_mac_cmac_tbl_init(rtwdev, rtwvif->mac_id);
3632e3ec7017SPing-Ke Shih 
36331b73e77dSPing-Ke Shih 	ret = rtw89_mac_set_macid_pause(rtwdev, rtwvif->mac_id, false);
3634e3ec7017SPing-Ke Shih 	if (ret)
3635e3ec7017SPing-Ke Shih 		return ret;
3636e3ec7017SPing-Ke Shih 
3637ff66964aSPing-Ke Shih 	ret = rtw89_fw_h2c_role_maintain(rtwdev, rtwvif, NULL, RTW89_ROLE_CREATE);
3638e3ec7017SPing-Ke Shih 	if (ret)
3639e3ec7017SPing-Ke Shih 		return ret;
3640e3ec7017SPing-Ke Shih 
3641e3ec7017SPing-Ke Shih 	ret = rtw89_cam_init(rtwdev, rtwvif);
3642e3ec7017SPing-Ke Shih 	if (ret)
3643e3ec7017SPing-Ke Shih 		return ret;
3644e3ec7017SPing-Ke Shih 
364540822e07SPing-Ke Shih 	ret = rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, NULL);
3646e3ec7017SPing-Ke Shih 	if (ret)
3647e3ec7017SPing-Ke Shih 		return ret;
3648e3ec7017SPing-Ke Shih 
3649742c470bSPing-Ke Shih 	ret = rtw89_fw_h2c_default_cmac_tbl(rtwdev, rtwvif);
3650e3ec7017SPing-Ke Shih 	if (ret)
3651e3ec7017SPing-Ke Shih 		return ret;
3652e3ec7017SPing-Ke Shih 
3653e3ec7017SPing-Ke Shih 	return 0;
3654e3ec7017SPing-Ke Shih }
3655e3ec7017SPing-Ke Shih 
3656e3ec7017SPing-Ke Shih int rtw89_mac_vif_deinit(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
3657e3ec7017SPing-Ke Shih {
3658e3ec7017SPing-Ke Shih 	int ret;
3659e3ec7017SPing-Ke Shih 
3660ff66964aSPing-Ke Shih 	ret = rtw89_fw_h2c_role_maintain(rtwdev, rtwvif, NULL, RTW89_ROLE_REMOVE);
3661e3ec7017SPing-Ke Shih 	if (ret)
3662e3ec7017SPing-Ke Shih 		return ret;
3663e3ec7017SPing-Ke Shih 
3664e3ec7017SPing-Ke Shih 	rtw89_cam_deinit(rtwdev, rtwvif);
3665e3ec7017SPing-Ke Shih 
366640822e07SPing-Ke Shih 	ret = rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, NULL);
3667e3ec7017SPing-Ke Shih 	if (ret)
3668e3ec7017SPing-Ke Shih 		return ret;
3669e3ec7017SPing-Ke Shih 
3670e3ec7017SPing-Ke Shih 	return 0;
3671e3ec7017SPing-Ke Shih }
3672e3ec7017SPing-Ke Shih 
3673e3ec7017SPing-Ke Shih int rtw89_mac_port_update(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
3674e3ec7017SPing-Ke Shih {
3675e3ec7017SPing-Ke Shih 	u8 port = rtwvif->port;
3676e3ec7017SPing-Ke Shih 
3677e3ec7017SPing-Ke Shih 	if (port >= RTW89_PORT_NUM)
3678e3ec7017SPing-Ke Shih 		return -EINVAL;
3679e3ec7017SPing-Ke Shih 
3680e3ec7017SPing-Ke Shih 	rtw89_mac_port_cfg_func_sw(rtwdev, rtwvif);
3681e3ec7017SPing-Ke Shih 	rtw89_mac_port_cfg_tx_rpt(rtwdev, rtwvif, false);
3682e3ec7017SPing-Ke Shih 	rtw89_mac_port_cfg_rx_rpt(rtwdev, rtwvif, false);
3683e3ec7017SPing-Ke Shih 	rtw89_mac_port_cfg_net_type(rtwdev, rtwvif);
3684e3ec7017SPing-Ke Shih 	rtw89_mac_port_cfg_bcn_prct(rtwdev, rtwvif);
3685e3ec7017SPing-Ke Shih 	rtw89_mac_port_cfg_rx_sw(rtwdev, rtwvif);
3686e3ec7017SPing-Ke Shih 	rtw89_mac_port_cfg_rx_sync(rtwdev, rtwvif);
3687e3ec7017SPing-Ke Shih 	rtw89_mac_port_cfg_tx_sw(rtwdev, rtwvif);
3688e3ec7017SPing-Ke Shih 	rtw89_mac_port_cfg_bcn_intv(rtwdev, rtwvif);
3689283c3d88SPing-Ke Shih 	rtw89_mac_port_cfg_hiq_win(rtwdev, rtwvif);
3690283c3d88SPing-Ke Shih 	rtw89_mac_port_cfg_hiq_dtim(rtwdev, rtwvif);
3691283c3d88SPing-Ke Shih 	rtw89_mac_port_cfg_hiq_drop(rtwdev, rtwvif);
3692e3ec7017SPing-Ke Shih 	rtw89_mac_port_cfg_bcn_setup_time(rtwdev, rtwvif);
3693e3ec7017SPing-Ke Shih 	rtw89_mac_port_cfg_bcn_hold_time(rtwdev, rtwvif);
3694e3ec7017SPing-Ke Shih 	rtw89_mac_port_cfg_bcn_mask_area(rtwdev, rtwvif);
3695e3ec7017SPing-Ke Shih 	rtw89_mac_port_cfg_tbtt_early(rtwdev, rtwvif);
3696704052f5SChia-Yuan Li 	rtw89_mac_port_cfg_tbtt_shift(rtwdev, rtwvif);
3697e3ec7017SPing-Ke Shih 	rtw89_mac_port_cfg_bss_color(rtwdev, rtwvif);
3698e3ec7017SPing-Ke Shih 	rtw89_mac_port_cfg_mbssid(rtwdev, rtwvif);
3699e3ec7017SPing-Ke Shih 	rtw89_mac_port_cfg_func_en(rtwdev, rtwvif);
3700e3ec7017SPing-Ke Shih 	fsleep(BCN_ERLY_SET_DLY);
3701e3ec7017SPing-Ke Shih 	rtw89_mac_port_cfg_bcn_early(rtwdev, rtwvif);
3702e3ec7017SPing-Ke Shih 
3703e3ec7017SPing-Ke Shih 	return 0;
3704e3ec7017SPing-Ke Shih }
3705e3ec7017SPing-Ke Shih 
3706*8d540f9dSKuan-Chung Chen static void rtw89_mac_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
3707*8d540f9dSKuan-Chung Chen 						      struct cfg80211_bss *bss,
3708*8d540f9dSKuan-Chung Chen 						      void *data)
3709*8d540f9dSKuan-Chung Chen {
3710*8d540f9dSKuan-Chung Chen 	const struct cfg80211_bss_ies *ies;
3711*8d540f9dSKuan-Chung Chen 	const struct element *elem;
3712*8d540f9dSKuan-Chung Chen 	bool *tolerated = data;
3713*8d540f9dSKuan-Chung Chen 
3714*8d540f9dSKuan-Chung Chen 	rcu_read_lock();
3715*8d540f9dSKuan-Chung Chen 	ies = rcu_dereference(bss->ies);
3716*8d540f9dSKuan-Chung Chen 	elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, ies->data,
3717*8d540f9dSKuan-Chung Chen 				  ies->len);
3718*8d540f9dSKuan-Chung Chen 
3719*8d540f9dSKuan-Chung Chen 	if (!elem || elem->datalen < 10 ||
3720*8d540f9dSKuan-Chung Chen 	    !(elem->data[10] & WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT))
3721*8d540f9dSKuan-Chung Chen 		*tolerated = false;
3722*8d540f9dSKuan-Chung Chen 	rcu_read_unlock();
3723*8d540f9dSKuan-Chung Chen }
3724*8d540f9dSKuan-Chung Chen 
3725*8d540f9dSKuan-Chung Chen void rtw89_mac_set_he_obss_narrow_bw_ru(struct rtw89_dev *rtwdev,
3726*8d540f9dSKuan-Chung Chen 					struct ieee80211_vif *vif)
3727*8d540f9dSKuan-Chung Chen {
3728*8d540f9dSKuan-Chung Chen 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
3729*8d540f9dSKuan-Chung Chen 	struct ieee80211_hw *hw = rtwdev->hw;
3730*8d540f9dSKuan-Chung Chen 	bool tolerated = true;
3731*8d540f9dSKuan-Chung Chen 	u32 reg;
3732*8d540f9dSKuan-Chung Chen 
3733*8d540f9dSKuan-Chung Chen 	if (!vif->bss_conf.he_support || vif->type != NL80211_IFTYPE_STATION)
3734*8d540f9dSKuan-Chung Chen 		return;
3735*8d540f9dSKuan-Chung Chen 
3736*8d540f9dSKuan-Chung Chen 	if (!(vif->bss_conf.chandef.chan->flags & IEEE80211_CHAN_RADAR))
3737*8d540f9dSKuan-Chung Chen 		return;
3738*8d540f9dSKuan-Chung Chen 
3739*8d540f9dSKuan-Chung Chen 	cfg80211_bss_iter(hw->wiphy, &vif->bss_conf.chandef,
3740*8d540f9dSKuan-Chung Chen 			  rtw89_mac_check_he_obss_narrow_bw_ru_iter,
3741*8d540f9dSKuan-Chung Chen 			  &tolerated);
3742*8d540f9dSKuan-Chung Chen 
3743*8d540f9dSKuan-Chung Chen 	reg = rtw89_mac_reg_by_idx(R_AX_RXTRIG_TEST_USER_2, rtwvif->mac_idx);
3744*8d540f9dSKuan-Chung Chen 	if (tolerated)
3745*8d540f9dSKuan-Chung Chen 		rtw89_write32_clr(rtwdev, reg, B_AX_RXTRIG_RU26_DIS);
3746*8d540f9dSKuan-Chung Chen 	else
3747*8d540f9dSKuan-Chung Chen 		rtw89_write32_set(rtwdev, reg, B_AX_RXTRIG_RU26_DIS);
3748*8d540f9dSKuan-Chung Chen }
3749*8d540f9dSKuan-Chung Chen 
3750e3ec7017SPing-Ke Shih int rtw89_mac_add_vif(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
3751e3ec7017SPing-Ke Shih {
3752e3ec7017SPing-Ke Shih 	int ret;
3753e3ec7017SPing-Ke Shih 
3754e3ec7017SPing-Ke Shih 	rtwvif->mac_id = rtw89_core_acquire_bit_map(rtwdev->mac_id_map,
3755e3ec7017SPing-Ke Shih 						    RTW89_MAX_MAC_ID_NUM);
3756e3ec7017SPing-Ke Shih 	if (rtwvif->mac_id == RTW89_MAX_MAC_ID_NUM)
3757e3ec7017SPing-Ke Shih 		return -ENOSPC;
3758e3ec7017SPing-Ke Shih 
3759e3ec7017SPing-Ke Shih 	ret = rtw89_mac_vif_init(rtwdev, rtwvif);
3760e3ec7017SPing-Ke Shih 	if (ret)
3761e3ec7017SPing-Ke Shih 		goto release_mac_id;
3762e3ec7017SPing-Ke Shih 
3763e3ec7017SPing-Ke Shih 	return 0;
3764e3ec7017SPing-Ke Shih 
3765e3ec7017SPing-Ke Shih release_mac_id:
3766e3ec7017SPing-Ke Shih 	rtw89_core_release_bit_map(rtwdev->mac_id_map, rtwvif->mac_id);
3767e3ec7017SPing-Ke Shih 
3768e3ec7017SPing-Ke Shih 	return ret;
3769e3ec7017SPing-Ke Shih }
3770e3ec7017SPing-Ke Shih 
3771e3ec7017SPing-Ke Shih int rtw89_mac_remove_vif(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
3772e3ec7017SPing-Ke Shih {
3773e3ec7017SPing-Ke Shih 	int ret;
3774e3ec7017SPing-Ke Shih 
3775e3ec7017SPing-Ke Shih 	ret = rtw89_mac_vif_deinit(rtwdev, rtwvif);
3776e3ec7017SPing-Ke Shih 	rtw89_core_release_bit_map(rtwdev->mac_id_map, rtwvif->mac_id);
3777e3ec7017SPing-Ke Shih 
3778e3ec7017SPing-Ke Shih 	return ret;
3779e3ec7017SPing-Ke Shih }
3780e3ec7017SPing-Ke Shih 
3781e3ec7017SPing-Ke Shih static void
3782e3ec7017SPing-Ke Shih rtw89_mac_c2h_macid_pause(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len)
3783e3ec7017SPing-Ke Shih {
3784e3ec7017SPing-Ke Shih }
3785e3ec7017SPing-Ke Shih 
378689590777SPo Hao Huang static bool rtw89_is_op_chan(struct rtw89_dev *rtwdev, u8 band, u8 channel)
378789590777SPo Hao Huang {
378889590777SPo Hao Huang 	struct rtw89_hw_scan_info *scan_info = &rtwdev->scan_info;
378989590777SPo Hao Huang 
379089590777SPo Hao Huang 	return band == scan_info->op_band && channel == scan_info->op_pri_ch;
379189590777SPo Hao Huang }
379289590777SPo Hao Huang 
379389590777SPo Hao Huang static void
379489590777SPo Hao Huang rtw89_mac_c2h_scanofld_rsp(struct rtw89_dev *rtwdev, struct sk_buff *c2h,
379589590777SPo Hao Huang 			   u32 len)
379689590777SPo Hao Huang {
379789590777SPo Hao Huang 	struct ieee80211_vif *vif = rtwdev->scan_info.scanning_vif;
3798bd1056d4SPo-Hao Huang 	struct rtw89_vif *rtwvif = vif_to_rtwvif_safe(vif);
3799cbb145b9SZong-Zhe Yang 	struct rtw89_chan new;
38003a1e7cb1SPo-Hao Huang 	u8 reason, status, tx_fail, band, actual_period;
3801bd1056d4SPo-Hao Huang 	u32 last_chan = rtwdev->scan_info.last_chan_idx;
380289590777SPo Hao Huang 	u16 chan;
3803bd1056d4SPo-Hao Huang 	int ret;
380489590777SPo Hao Huang 
380589590777SPo Hao Huang 	tx_fail = RTW89_GET_MAC_C2H_SCANOFLD_TX_FAIL(c2h->data);
380689590777SPo Hao Huang 	status = RTW89_GET_MAC_C2H_SCANOFLD_STATUS(c2h->data);
380789590777SPo Hao Huang 	chan = RTW89_GET_MAC_C2H_SCANOFLD_PRI_CH(c2h->data);
380889590777SPo Hao Huang 	reason = RTW89_GET_MAC_C2H_SCANOFLD_RSP(c2h->data);
380989590777SPo Hao Huang 	band = RTW89_GET_MAC_C2H_SCANOFLD_BAND(c2h->data);
38103a1e7cb1SPo-Hao Huang 	actual_period = RTW89_GET_MAC_C2H_ACTUAL_PERIOD(c2h->data);
381189590777SPo Hao Huang 
381289590777SPo Hao Huang 	if (!(rtwdev->chip->support_bands & BIT(NL80211_BAND_6GHZ)))
381389590777SPo Hao Huang 		band = chan > 14 ? RTW89_BAND_5G : RTW89_BAND_2G;
381489590777SPo Hao Huang 
381589590777SPo Hao Huang 	rtw89_debug(rtwdev, RTW89_DBG_HW_SCAN,
38163a1e7cb1SPo-Hao Huang 		    "band: %d, chan: %d, reason: %d, status: %d, tx_fail: %d, actual: %d\n",
38173a1e7cb1SPo-Hao Huang 		    band, chan, reason, status, tx_fail, actual_period);
381889590777SPo Hao Huang 
381989590777SPo Hao Huang 	switch (reason) {
382089590777SPo Hao Huang 	case RTW89_SCAN_LEAVE_CH_NOTIFY:
382189590777SPo Hao Huang 		if (rtw89_is_op_chan(rtwdev, band, chan))
382289590777SPo Hao Huang 			ieee80211_stop_queues(rtwdev->hw);
382389590777SPo Hao Huang 		return;
382489590777SPo Hao Huang 	case RTW89_SCAN_END_SCAN_NOTIFY:
3825bd1056d4SPo-Hao Huang 		if (rtwvif && rtwvif->scan_req &&
3826bd1056d4SPo-Hao Huang 		    last_chan < rtwvif->scan_req->n_channels) {
3827bd1056d4SPo-Hao Huang 			ret = rtw89_hw_scan_offload(rtwdev, vif, true);
3828bd1056d4SPo-Hao Huang 			if (ret) {
3829bd1056d4SPo-Hao Huang 				rtw89_hw_scan_abort(rtwdev, vif);
3830bd1056d4SPo-Hao Huang 				rtw89_warn(rtwdev, "HW scan failed: %d\n", ret);
3831bd1056d4SPo-Hao Huang 			}
3832bd1056d4SPo-Hao Huang 		} else {
383389590777SPo Hao Huang 			rtw89_hw_scan_complete(rtwdev, vif, false);
3834bd1056d4SPo-Hao Huang 		}
383589590777SPo Hao Huang 		break;
383689590777SPo Hao Huang 	case RTW89_SCAN_ENTER_CH_NOTIFY:
3837bb8152b3SZong-Zhe Yang 		rtw89_chan_create(&new, chan, chan, band, RTW89_CHANNEL_WIDTH_20);
3838cbb145b9SZong-Zhe Yang 		rtw89_assign_entity_chan(rtwdev, RTW89_SUB_ENTITY_0, &new);
383928000f7bSPo Hao Huang 		if (rtw89_is_op_chan(rtwdev, band, chan)) {
384028000f7bSPo Hao Huang 			rtw89_store_op_chan(rtwdev, false);
384189590777SPo Hao Huang 			ieee80211_wake_queues(rtwdev->hw);
384228000f7bSPo Hao Huang 		}
384389590777SPo Hao Huang 		break;
384489590777SPo Hao Huang 	default:
384589590777SPo Hao Huang 		return;
384689590777SPo Hao Huang 	}
384789590777SPo Hao Huang }
384889590777SPo Hao Huang 
3849e3ec7017SPing-Ke Shih static void
3850e3ec7017SPing-Ke Shih rtw89_mac_c2h_rec_ack(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len)
3851e3ec7017SPing-Ke Shih {
3852e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_FW,
3853e3ec7017SPing-Ke Shih 		    "C2H rev ack recv, cat: %d, class: %d, func: %d, seq : %d\n",
3854e3ec7017SPing-Ke Shih 		    RTW89_GET_MAC_C2H_REV_ACK_CAT(c2h->data),
3855e3ec7017SPing-Ke Shih 		    RTW89_GET_MAC_C2H_REV_ACK_CLASS(c2h->data),
3856e3ec7017SPing-Ke Shih 		    RTW89_GET_MAC_C2H_REV_ACK_FUNC(c2h->data),
3857e3ec7017SPing-Ke Shih 		    RTW89_GET_MAC_C2H_REV_ACK_H2C_SEQ(c2h->data));
3858e3ec7017SPing-Ke Shih }
3859e3ec7017SPing-Ke Shih 
3860e3ec7017SPing-Ke Shih static void
3861e3ec7017SPing-Ke Shih rtw89_mac_c2h_done_ack(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len)
3862e3ec7017SPing-Ke Shih {
3863e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_FW,
3864e3ec7017SPing-Ke Shih 		    "C2H done ack recv, cat: %d, class: %d, func: %d, ret: %d, seq : %d\n",
3865e3ec7017SPing-Ke Shih 		    RTW89_GET_MAC_C2H_DONE_ACK_CAT(c2h->data),
3866e3ec7017SPing-Ke Shih 		    RTW89_GET_MAC_C2H_DONE_ACK_CLASS(c2h->data),
3867e3ec7017SPing-Ke Shih 		    RTW89_GET_MAC_C2H_DONE_ACK_FUNC(c2h->data),
3868e3ec7017SPing-Ke Shih 		    RTW89_GET_MAC_C2H_DONE_ACK_H2C_RETURN(c2h->data),
3869e3ec7017SPing-Ke Shih 		    RTW89_GET_MAC_C2H_DONE_ACK_H2C_SEQ(c2h->data));
3870e3ec7017SPing-Ke Shih }
3871e3ec7017SPing-Ke Shih 
3872e3ec7017SPing-Ke Shih static void
3873e3ec7017SPing-Ke Shih rtw89_mac_c2h_log(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len)
3874e3ec7017SPing-Ke Shih {
3875e3ec7017SPing-Ke Shih 	rtw89_info(rtwdev, "%*s", RTW89_GET_C2H_LOG_LEN(len),
3876e3ec7017SPing-Ke Shih 		   RTW89_GET_C2H_LOG_SRT_PRT(c2h->data));
3877e3ec7017SPing-Ke Shih }
3878e3ec7017SPing-Ke Shih 
3879fccca934SPing-Ke Shih static void
3880fccca934SPing-Ke Shih rtw89_mac_c2h_bcn_cnt(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len)
3881fccca934SPing-Ke Shih {
3882fccca934SPing-Ke Shih }
3883fccca934SPing-Ke Shih 
38842b8219e9SPo Hao Huang static void
38852b8219e9SPo Hao Huang rtw89_mac_c2h_pkt_ofld_rsp(struct rtw89_dev *rtwdev, struct sk_buff *c2h,
38862b8219e9SPo Hao Huang 			   u32 len)
38872b8219e9SPo Hao Huang {
38882b8219e9SPo Hao Huang }
38892b8219e9SPo Hao Huang 
3890f4a43c3bSDian-Syuan Yang static void
3891f4a43c3bSDian-Syuan Yang rtw89_mac_c2h_tsf32_toggle_rpt(struct rtw89_dev *rtwdev, struct sk_buff *c2h,
3892f4a43c3bSDian-Syuan Yang 			       u32 len)
3893f4a43c3bSDian-Syuan Yang {
3894f4a43c3bSDian-Syuan Yang }
3895f4a43c3bSDian-Syuan Yang 
3896e3ec7017SPing-Ke Shih static
3897e3ec7017SPing-Ke Shih void (* const rtw89_mac_c2h_ofld_handler[])(struct rtw89_dev *rtwdev,
3898e3ec7017SPing-Ke Shih 					    struct sk_buff *c2h, u32 len) = {
3899e3ec7017SPing-Ke Shih 	[RTW89_MAC_C2H_FUNC_EFUSE_DUMP] = NULL,
3900e3ec7017SPing-Ke Shih 	[RTW89_MAC_C2H_FUNC_READ_RSP] = NULL,
39012b8219e9SPo Hao Huang 	[RTW89_MAC_C2H_FUNC_PKT_OFLD_RSP] = rtw89_mac_c2h_pkt_ofld_rsp,
3902e3ec7017SPing-Ke Shih 	[RTW89_MAC_C2H_FUNC_BCN_RESEND] = NULL,
3903e3ec7017SPing-Ke Shih 	[RTW89_MAC_C2H_FUNC_MACID_PAUSE] = rtw89_mac_c2h_macid_pause,
390489590777SPo Hao Huang 	[RTW89_MAC_C2H_FUNC_SCANOFLD_RSP] = rtw89_mac_c2h_scanofld_rsp,
3905f4a43c3bSDian-Syuan Yang 	[RTW89_MAC_C2H_FUNC_TSF32_TOGL_RPT] = rtw89_mac_c2h_tsf32_toggle_rpt,
3906e3ec7017SPing-Ke Shih };
3907e3ec7017SPing-Ke Shih 
3908e3ec7017SPing-Ke Shih static
3909e3ec7017SPing-Ke Shih void (* const rtw89_mac_c2h_info_handler[])(struct rtw89_dev *rtwdev,
3910e3ec7017SPing-Ke Shih 					    struct sk_buff *c2h, u32 len) = {
3911e3ec7017SPing-Ke Shih 	[RTW89_MAC_C2H_FUNC_REC_ACK] = rtw89_mac_c2h_rec_ack,
3912e3ec7017SPing-Ke Shih 	[RTW89_MAC_C2H_FUNC_DONE_ACK] = rtw89_mac_c2h_done_ack,
3913e3ec7017SPing-Ke Shih 	[RTW89_MAC_C2H_FUNC_C2H_LOG] = rtw89_mac_c2h_log,
3914fccca934SPing-Ke Shih 	[RTW89_MAC_C2H_FUNC_BCN_CNT] = rtw89_mac_c2h_bcn_cnt,
3915e3ec7017SPing-Ke Shih };
3916e3ec7017SPing-Ke Shih 
3917e3ec7017SPing-Ke Shih void rtw89_mac_c2h_handle(struct rtw89_dev *rtwdev, struct sk_buff *skb,
3918e3ec7017SPing-Ke Shih 			  u32 len, u8 class, u8 func)
3919e3ec7017SPing-Ke Shih {
3920e3ec7017SPing-Ke Shih 	void (*handler)(struct rtw89_dev *rtwdev,
3921e3ec7017SPing-Ke Shih 			struct sk_buff *c2h, u32 len) = NULL;
3922e3ec7017SPing-Ke Shih 
3923e3ec7017SPing-Ke Shih 	switch (class) {
3924e3ec7017SPing-Ke Shih 	case RTW89_MAC_C2H_CLASS_INFO:
3925e3ec7017SPing-Ke Shih 		if (func < RTW89_MAC_C2H_FUNC_INFO_MAX)
3926e3ec7017SPing-Ke Shih 			handler = rtw89_mac_c2h_info_handler[func];
3927e3ec7017SPing-Ke Shih 		break;
3928e3ec7017SPing-Ke Shih 	case RTW89_MAC_C2H_CLASS_OFLD:
3929e3ec7017SPing-Ke Shih 		if (func < RTW89_MAC_C2H_FUNC_OFLD_MAX)
3930e3ec7017SPing-Ke Shih 			handler = rtw89_mac_c2h_ofld_handler[func];
3931e3ec7017SPing-Ke Shih 		break;
3932e3ec7017SPing-Ke Shih 	case RTW89_MAC_C2H_CLASS_FWDBG:
3933e3ec7017SPing-Ke Shih 		return;
3934e3ec7017SPing-Ke Shih 	default:
3935e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "c2h class %d not support\n", class);
3936e3ec7017SPing-Ke Shih 		return;
3937e3ec7017SPing-Ke Shih 	}
3938e3ec7017SPing-Ke Shih 	if (!handler) {
3939e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "c2h class %d func %d not support\n", class,
3940e3ec7017SPing-Ke Shih 			   func);
3941e3ec7017SPing-Ke Shih 		return;
3942e3ec7017SPing-Ke Shih 	}
3943e3ec7017SPing-Ke Shih 	handler(rtwdev, skb, len);
3944e3ec7017SPing-Ke Shih }
3945e3ec7017SPing-Ke Shih 
3946e3ec7017SPing-Ke Shih bool rtw89_mac_get_txpwr_cr(struct rtw89_dev *rtwdev,
3947e3ec7017SPing-Ke Shih 			    enum rtw89_phy_idx phy_idx,
3948e3ec7017SPing-Ke Shih 			    u32 reg_base, u32 *cr)
3949e3ec7017SPing-Ke Shih {
3950e3ec7017SPing-Ke Shih 	const struct rtw89_dle_mem *dle_mem = rtwdev->chip->dle_mem;
3951e3ec7017SPing-Ke Shih 	enum rtw89_qta_mode mode = dle_mem->mode;
3952e3ec7017SPing-Ke Shih 	u32 addr = rtw89_mac_reg_by_idx(reg_base, phy_idx);
3953e3ec7017SPing-Ke Shih 
3954e3ec7017SPing-Ke Shih 	if (addr < R_AX_PWR_RATE_CTRL || addr > CMAC1_END_ADDR) {
3955e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "[TXPWR] addr=0x%x exceed txpwr cr\n",
3956e3ec7017SPing-Ke Shih 			  addr);
3957e3ec7017SPing-Ke Shih 		goto error;
3958e3ec7017SPing-Ke Shih 	}
3959e3ec7017SPing-Ke Shih 
3960e3ec7017SPing-Ke Shih 	if (addr >= CMAC1_START_ADDR && addr <= CMAC1_END_ADDR)
3961e3ec7017SPing-Ke Shih 		if (mode == RTW89_QTA_SCC) {
3962e3ec7017SPing-Ke Shih 			rtw89_err(rtwdev,
3963e3ec7017SPing-Ke Shih 				  "[TXPWR] addr=0x%x but hw not enable\n",
3964e3ec7017SPing-Ke Shih 				  addr);
3965e3ec7017SPing-Ke Shih 			goto error;
3966e3ec7017SPing-Ke Shih 		}
3967e3ec7017SPing-Ke Shih 
3968e3ec7017SPing-Ke Shih 	*cr = addr;
3969e3ec7017SPing-Ke Shih 	return true;
3970e3ec7017SPing-Ke Shih 
3971e3ec7017SPing-Ke Shih error:
3972e3ec7017SPing-Ke Shih 	rtw89_err(rtwdev, "[TXPWR] check txpwr cr 0x%x(phy%d) fail\n",
3973e3ec7017SPing-Ke Shih 		  addr, phy_idx);
3974e3ec7017SPing-Ke Shih 
3975e3ec7017SPing-Ke Shih 	return false;
3976e3ec7017SPing-Ke Shih }
3977861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_mac_get_txpwr_cr);
3978e3ec7017SPing-Ke Shih 
3979e3ec7017SPing-Ke Shih int rtw89_mac_cfg_ppdu_status(struct rtw89_dev *rtwdev, u8 mac_idx, bool enable)
3980e3ec7017SPing-Ke Shih {
3981e3ec7017SPing-Ke Shih 	u32 reg = rtw89_mac_reg_by_idx(R_AX_PPDU_STAT, mac_idx);
3982e3ec7017SPing-Ke Shih 	int ret = 0;
3983e3ec7017SPing-Ke Shih 
3984e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL);
3985e3ec7017SPing-Ke Shih 	if (ret)
3986e3ec7017SPing-Ke Shih 		return ret;
3987e3ec7017SPing-Ke Shih 
3988e3ec7017SPing-Ke Shih 	if (!enable) {
3989e3ec7017SPing-Ke Shih 		rtw89_write32_clr(rtwdev, reg, B_AX_PPDU_STAT_RPT_EN);
3990e3ec7017SPing-Ke Shih 		return ret;
3991e3ec7017SPing-Ke Shih 	}
3992e3ec7017SPing-Ke Shih 
3993e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, reg, B_AX_PPDU_STAT_RPT_EN |
3994e3ec7017SPing-Ke Shih 				   B_AX_APP_MAC_INFO_RPT |
3995e3ec7017SPing-Ke Shih 				   B_AX_APP_RX_CNT_RPT | B_AX_APP_PLCP_HDR_RPT |
3996e3ec7017SPing-Ke Shih 				   B_AX_PPDU_STAT_RPT_CRC32);
3997e3ec7017SPing-Ke Shih 	rtw89_write32_mask(rtwdev, R_AX_HW_RPT_FWD, B_AX_FWD_PPDU_STAT_MASK,
3998e3ec7017SPing-Ke Shih 			   RTW89_PRPT_DEST_HOST);
3999e3ec7017SPing-Ke Shih 
4000e3ec7017SPing-Ke Shih 	return ret;
4001e3ec7017SPing-Ke Shih }
4002861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_mac_cfg_ppdu_status);
4003e3ec7017SPing-Ke Shih 
4004e3ec7017SPing-Ke Shih void rtw89_mac_update_rts_threshold(struct rtw89_dev *rtwdev, u8 mac_idx)
4005e3ec7017SPing-Ke Shih {
4006e3ec7017SPing-Ke Shih #define MAC_AX_TIME_TH_SH  5
4007e3ec7017SPing-Ke Shih #define MAC_AX_LEN_TH_SH   4
4008e3ec7017SPing-Ke Shih #define MAC_AX_TIME_TH_MAX 255
4009e3ec7017SPing-Ke Shih #define MAC_AX_LEN_TH_MAX  255
4010e3ec7017SPing-Ke Shih #define MAC_AX_TIME_TH_DEF 88
4011e3ec7017SPing-Ke Shih #define MAC_AX_LEN_TH_DEF  4080
4012e3ec7017SPing-Ke Shih 	struct ieee80211_hw *hw = rtwdev->hw;
4013e3ec7017SPing-Ke Shih 	u32 rts_threshold = hw->wiphy->rts_threshold;
4014e3ec7017SPing-Ke Shih 	u32 time_th, len_th;
4015e3ec7017SPing-Ke Shih 	u32 reg;
4016e3ec7017SPing-Ke Shih 
4017e3ec7017SPing-Ke Shih 	if (rts_threshold == (u32)-1) {
4018e3ec7017SPing-Ke Shih 		time_th = MAC_AX_TIME_TH_DEF;
4019e3ec7017SPing-Ke Shih 		len_th = MAC_AX_LEN_TH_DEF;
4020e3ec7017SPing-Ke Shih 	} else {
4021e3ec7017SPing-Ke Shih 		time_th = MAC_AX_TIME_TH_MAX << MAC_AX_TIME_TH_SH;
4022e3ec7017SPing-Ke Shih 		len_th = rts_threshold;
4023e3ec7017SPing-Ke Shih 	}
4024e3ec7017SPing-Ke Shih 
4025e3ec7017SPing-Ke Shih 	time_th = min_t(u32, time_th >> MAC_AX_TIME_TH_SH, MAC_AX_TIME_TH_MAX);
4026e3ec7017SPing-Ke Shih 	len_th = min_t(u32, len_th >> MAC_AX_LEN_TH_SH, MAC_AX_LEN_TH_MAX);
4027e3ec7017SPing-Ke Shih 
4028e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_AGG_LEN_HT_0, mac_idx);
4029e3ec7017SPing-Ke Shih 	rtw89_write16_mask(rtwdev, reg, B_AX_RTS_TXTIME_TH_MASK, time_th);
4030e3ec7017SPing-Ke Shih 	rtw89_write16_mask(rtwdev, reg, B_AX_RTS_LEN_TH_MASK, len_th);
4031e3ec7017SPing-Ke Shih }
4032e3ec7017SPing-Ke Shih 
4033e3ec7017SPing-Ke Shih void rtw89_mac_flush_txq(struct rtw89_dev *rtwdev, u32 queues, bool drop)
4034e3ec7017SPing-Ke Shih {
4035e3ec7017SPing-Ke Shih 	bool empty;
4036e3ec7017SPing-Ke Shih 	int ret;
4037e3ec7017SPing-Ke Shih 
4038e3ec7017SPing-Ke Shih 	if (!test_bit(RTW89_FLAG_POWERON, rtwdev->flags))
4039e3ec7017SPing-Ke Shih 		return;
4040e3ec7017SPing-Ke Shih 
4041e3ec7017SPing-Ke Shih 	ret = read_poll_timeout(dle_is_txq_empty, empty, empty,
4042e3ec7017SPing-Ke Shih 				10000, 200000, false, rtwdev);
4043e3ec7017SPing-Ke Shih 	if (ret && !drop && (rtwdev->total_sta_assoc || rtwdev->scanning))
4044e3ec7017SPing-Ke Shih 		rtw89_info(rtwdev, "timed out to flush queues\n");
4045e3ec7017SPing-Ke Shih }
4046e3ec7017SPing-Ke Shih 
4047e3ec7017SPing-Ke Shih int rtw89_mac_coex_init(struct rtw89_dev *rtwdev, const struct rtw89_mac_ax_coex *coex)
4048e3ec7017SPing-Ke Shih {
4049e3ec7017SPing-Ke Shih 	u8 val;
4050e3ec7017SPing-Ke Shih 	u16 val16;
4051e3ec7017SPing-Ke Shih 	u32 val32;
4052e3ec7017SPing-Ke Shih 	int ret;
4053e3ec7017SPing-Ke Shih 
4054e3ec7017SPing-Ke Shih 	rtw89_write8_set(rtwdev, R_AX_GPIO_MUXCFG, B_AX_ENBT);
4055e3ec7017SPing-Ke Shih 	rtw89_write8_set(rtwdev, R_AX_BTC_FUNC_EN, B_AX_PTA_WL_TX_EN);
4056e3ec7017SPing-Ke Shih 	rtw89_write8_set(rtwdev, R_AX_BT_COEX_CFG_2 + 1, B_AX_GNT_BT_POLARITY >> 8);
4057e3ec7017SPing-Ke Shih 	rtw89_write8_set(rtwdev, R_AX_CSR_MODE, B_AX_STATIS_BT_EN | B_AX_WL_ACT_MSK);
4058e3ec7017SPing-Ke Shih 	rtw89_write8_set(rtwdev, R_AX_CSR_MODE + 2, B_AX_BT_CNT_RST >> 16);
4059e3ec7017SPing-Ke Shih 	rtw89_write8_clr(rtwdev, R_AX_TRXPTCL_RESP_0 + 3, B_AX_RSP_CHK_BTCCA >> 24);
4060e3ec7017SPing-Ke Shih 
4061e3ec7017SPing-Ke Shih 	val16 = rtw89_read16(rtwdev, R_AX_CCA_CFG_0);
4062e3ec7017SPing-Ke Shih 	val16 = (val16 | B_AX_BTCCA_EN) & ~B_AX_BTCCA_BRK_TXOP_EN;
4063e3ec7017SPing-Ke Shih 	rtw89_write16(rtwdev, R_AX_CCA_CFG_0, val16);
4064e3ec7017SPing-Ke Shih 
4065e3ec7017SPing-Ke Shih 	ret = rtw89_mac_read_lte(rtwdev, R_AX_LTE_SW_CFG_2, &val32);
4066e3ec7017SPing-Ke Shih 	if (ret) {
4067e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "Read R_AX_LTE_SW_CFG_2 fail!\n");
4068e3ec7017SPing-Ke Shih 		return ret;
4069e3ec7017SPing-Ke Shih 	}
4070e3ec7017SPing-Ke Shih 	val32 = val32 & B_AX_WL_RX_CTRL;
4071e3ec7017SPing-Ke Shih 	ret = rtw89_mac_write_lte(rtwdev, R_AX_LTE_SW_CFG_2, val32);
4072e3ec7017SPing-Ke Shih 	if (ret) {
4073e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "Write R_AX_LTE_SW_CFG_2 fail!\n");
4074e3ec7017SPing-Ke Shih 		return ret;
4075e3ec7017SPing-Ke Shih 	}
4076e3ec7017SPing-Ke Shih 
4077e3ec7017SPing-Ke Shih 	switch (coex->pta_mode) {
4078e3ec7017SPing-Ke Shih 	case RTW89_MAC_AX_COEX_RTK_MODE:
4079e3ec7017SPing-Ke Shih 		val = rtw89_read8(rtwdev, R_AX_GPIO_MUXCFG);
4080e3ec7017SPing-Ke Shih 		val &= ~B_AX_BTMODE_MASK;
4081e3ec7017SPing-Ke Shih 		val |= FIELD_PREP(B_AX_BTMODE_MASK, MAC_AX_BT_MODE_0_3);
4082e3ec7017SPing-Ke Shih 		rtw89_write8(rtwdev, R_AX_GPIO_MUXCFG, val);
4083e3ec7017SPing-Ke Shih 
4084e3ec7017SPing-Ke Shih 		val = rtw89_read8(rtwdev, R_AX_TDMA_MODE);
4085e3ec7017SPing-Ke Shih 		rtw89_write8(rtwdev, R_AX_TDMA_MODE, val | B_AX_RTK_BT_ENABLE);
4086e3ec7017SPing-Ke Shih 
4087e3ec7017SPing-Ke Shih 		val = rtw89_read8(rtwdev, R_AX_BT_COEX_CFG_5);
4088e3ec7017SPing-Ke Shih 		val &= ~B_AX_BT_RPT_SAMPLE_RATE_MASK;
4089e3ec7017SPing-Ke Shih 		val |= FIELD_PREP(B_AX_BT_RPT_SAMPLE_RATE_MASK, MAC_AX_RTK_RATE);
4090e3ec7017SPing-Ke Shih 		rtw89_write8(rtwdev, R_AX_BT_COEX_CFG_5, val);
4091e3ec7017SPing-Ke Shih 		break;
4092e3ec7017SPing-Ke Shih 	case RTW89_MAC_AX_COEX_CSR_MODE:
4093e3ec7017SPing-Ke Shih 		val = rtw89_read8(rtwdev, R_AX_GPIO_MUXCFG);
4094e3ec7017SPing-Ke Shih 		val &= ~B_AX_BTMODE_MASK;
4095e3ec7017SPing-Ke Shih 		val |= FIELD_PREP(B_AX_BTMODE_MASK, MAC_AX_BT_MODE_2);
4096e3ec7017SPing-Ke Shih 		rtw89_write8(rtwdev, R_AX_GPIO_MUXCFG, val);
4097e3ec7017SPing-Ke Shih 
4098e3ec7017SPing-Ke Shih 		val16 = rtw89_read16(rtwdev, R_AX_CSR_MODE);
4099e3ec7017SPing-Ke Shih 		val16 &= ~B_AX_BT_PRI_DETECT_TO_MASK;
4100e3ec7017SPing-Ke Shih 		val16 |= FIELD_PREP(B_AX_BT_PRI_DETECT_TO_MASK, MAC_AX_CSR_PRI_TO);
4101e3ec7017SPing-Ke Shih 		val16 &= ~B_AX_BT_TRX_INIT_DETECT_MASK;
4102e3ec7017SPing-Ke Shih 		val16 |= FIELD_PREP(B_AX_BT_TRX_INIT_DETECT_MASK, MAC_AX_CSR_TRX_TO);
4103e3ec7017SPing-Ke Shih 		val16 &= ~B_AX_BT_STAT_DELAY_MASK;
4104e3ec7017SPing-Ke Shih 		val16 |= FIELD_PREP(B_AX_BT_STAT_DELAY_MASK, MAC_AX_CSR_DELAY);
4105e3ec7017SPing-Ke Shih 		val16 |= B_AX_ENHANCED_BT;
4106e3ec7017SPing-Ke Shih 		rtw89_write16(rtwdev, R_AX_CSR_MODE, val16);
4107e3ec7017SPing-Ke Shih 
4108e3ec7017SPing-Ke Shih 		rtw89_write8(rtwdev, R_AX_BT_COEX_CFG_2, MAC_AX_CSR_RATE);
4109e3ec7017SPing-Ke Shih 		break;
4110e3ec7017SPing-Ke Shih 	default:
4111e3ec7017SPing-Ke Shih 		return -EINVAL;
4112e3ec7017SPing-Ke Shih 	}
4113e3ec7017SPing-Ke Shih 
4114e3ec7017SPing-Ke Shih 	switch (coex->direction) {
4115e3ec7017SPing-Ke Shih 	case RTW89_MAC_AX_COEX_INNER:
4116e3ec7017SPing-Ke Shih 		val = rtw89_read8(rtwdev, R_AX_GPIO_MUXCFG + 1);
4117e3ec7017SPing-Ke Shih 		val = (val & ~BIT(2)) | BIT(1);
4118e3ec7017SPing-Ke Shih 		rtw89_write8(rtwdev, R_AX_GPIO_MUXCFG + 1, val);
4119e3ec7017SPing-Ke Shih 		break;
4120e3ec7017SPing-Ke Shih 	case RTW89_MAC_AX_COEX_OUTPUT:
4121e3ec7017SPing-Ke Shih 		val = rtw89_read8(rtwdev, R_AX_GPIO_MUXCFG + 1);
4122e3ec7017SPing-Ke Shih 		val = val | BIT(1) | BIT(0);
4123e3ec7017SPing-Ke Shih 		rtw89_write8(rtwdev, R_AX_GPIO_MUXCFG + 1, val);
4124e3ec7017SPing-Ke Shih 		break;
4125e3ec7017SPing-Ke Shih 	case RTW89_MAC_AX_COEX_INPUT:
4126e3ec7017SPing-Ke Shih 		val = rtw89_read8(rtwdev, R_AX_GPIO_MUXCFG + 1);
4127e3ec7017SPing-Ke Shih 		val = val & ~(BIT(2) | BIT(1));
4128e3ec7017SPing-Ke Shih 		rtw89_write8(rtwdev, R_AX_GPIO_MUXCFG + 1, val);
4129e3ec7017SPing-Ke Shih 		break;
4130e3ec7017SPing-Ke Shih 	default:
4131e3ec7017SPing-Ke Shih 		return -EINVAL;
4132e3ec7017SPing-Ke Shih 	}
4133e3ec7017SPing-Ke Shih 
4134e3ec7017SPing-Ke Shih 	return 0;
4135e3ec7017SPing-Ke Shih }
4136861e58c8SZong-Zhe Yang EXPORT_SYMBOL(rtw89_mac_coex_init);
4137e3ec7017SPing-Ke Shih 
4138065cf8f9SChia-Yuan Li int rtw89_mac_coex_init_v1(struct rtw89_dev *rtwdev,
4139065cf8f9SChia-Yuan Li 			   const struct rtw89_mac_ax_coex *coex)
4140065cf8f9SChia-Yuan Li {
4141065cf8f9SChia-Yuan Li 	rtw89_write32_set(rtwdev, R_AX_BTC_CFG,
4142065cf8f9SChia-Yuan Li 			  B_AX_BTC_EN | B_AX_BTG_LNA1_GAIN_SEL);
4143065cf8f9SChia-Yuan Li 	rtw89_write32_set(rtwdev, R_AX_BT_CNT_CFG, B_AX_BT_CNT_EN);
4144065cf8f9SChia-Yuan Li 	rtw89_write16_set(rtwdev, R_AX_CCA_CFG_0, B_AX_BTCCA_EN);
4145065cf8f9SChia-Yuan Li 	rtw89_write16_clr(rtwdev, R_AX_CCA_CFG_0, B_AX_BTCCA_BRK_TXOP_EN);
4146065cf8f9SChia-Yuan Li 
4147065cf8f9SChia-Yuan Li 	switch (coex->pta_mode) {
4148065cf8f9SChia-Yuan Li 	case RTW89_MAC_AX_COEX_RTK_MODE:
4149065cf8f9SChia-Yuan Li 		rtw89_write32_mask(rtwdev, R_AX_BTC_CFG, B_AX_BTC_MODE_MASK,
4150065cf8f9SChia-Yuan Li 				   MAC_AX_RTK_MODE);
4151065cf8f9SChia-Yuan Li 		rtw89_write32_mask(rtwdev, R_AX_RTK_MODE_CFG_V1,
4152065cf8f9SChia-Yuan Li 				   B_AX_SAMPLE_CLK_MASK, MAC_AX_RTK_RATE);
4153065cf8f9SChia-Yuan Li 		break;
4154065cf8f9SChia-Yuan Li 	case RTW89_MAC_AX_COEX_CSR_MODE:
4155065cf8f9SChia-Yuan Li 		rtw89_write32_mask(rtwdev, R_AX_BTC_CFG, B_AX_BTC_MODE_MASK,
4156065cf8f9SChia-Yuan Li 				   MAC_AX_CSR_MODE);
4157065cf8f9SChia-Yuan Li 		break;
4158065cf8f9SChia-Yuan Li 	default:
4159065cf8f9SChia-Yuan Li 		return -EINVAL;
4160065cf8f9SChia-Yuan Li 	}
4161065cf8f9SChia-Yuan Li 
4162065cf8f9SChia-Yuan Li 	return 0;
4163065cf8f9SChia-Yuan Li }
4164065cf8f9SChia-Yuan Li EXPORT_SYMBOL(rtw89_mac_coex_init_v1);
4165065cf8f9SChia-Yuan Li 
4166e3ec7017SPing-Ke Shih int rtw89_mac_cfg_gnt(struct rtw89_dev *rtwdev,
4167e3ec7017SPing-Ke Shih 		      const struct rtw89_mac_ax_coex_gnt *gnt_cfg)
4168e3ec7017SPing-Ke Shih {
41698001c741SPing-Ke Shih 	u32 val = 0, ret;
4170e3ec7017SPing-Ke Shih 
41718001c741SPing-Ke Shih 	if (gnt_cfg->band[0].gnt_bt)
41728001c741SPing-Ke Shih 		val |= B_AX_GNT_BT_RFC_S0_SW_VAL | B_AX_GNT_BT_BB_S0_SW_VAL;
41738001c741SPing-Ke Shih 
41748001c741SPing-Ke Shih 	if (gnt_cfg->band[0].gnt_bt_sw_en)
41758001c741SPing-Ke Shih 		val |= B_AX_GNT_BT_RFC_S0_SW_CTRL | B_AX_GNT_BT_BB_S0_SW_CTRL;
41768001c741SPing-Ke Shih 
41778001c741SPing-Ke Shih 	if (gnt_cfg->band[0].gnt_wl)
41788001c741SPing-Ke Shih 		val |= B_AX_GNT_WL_RFC_S0_SW_VAL | B_AX_GNT_WL_BB_S0_SW_VAL;
41798001c741SPing-Ke Shih 
41808001c741SPing-Ke Shih 	if (gnt_cfg->band[0].gnt_wl_sw_en)
41818001c741SPing-Ke Shih 		val |= B_AX_GNT_WL_RFC_S0_SW_CTRL | B_AX_GNT_WL_BB_S0_SW_CTRL;
41828001c741SPing-Ke Shih 
41838001c741SPing-Ke Shih 	if (gnt_cfg->band[1].gnt_bt)
41848001c741SPing-Ke Shih 		val |= B_AX_GNT_BT_RFC_S1_SW_VAL | B_AX_GNT_BT_BB_S1_SW_VAL;
41858001c741SPing-Ke Shih 
41868001c741SPing-Ke Shih 	if (gnt_cfg->band[1].gnt_bt_sw_en)
41878001c741SPing-Ke Shih 		val |= B_AX_GNT_BT_RFC_S1_SW_CTRL | B_AX_GNT_BT_BB_S1_SW_CTRL;
41888001c741SPing-Ke Shih 
41898001c741SPing-Ke Shih 	if (gnt_cfg->band[1].gnt_wl)
41908001c741SPing-Ke Shih 		val |= B_AX_GNT_WL_RFC_S1_SW_VAL | B_AX_GNT_WL_BB_S1_SW_VAL;
41918001c741SPing-Ke Shih 
41928001c741SPing-Ke Shih 	if (gnt_cfg->band[1].gnt_wl_sw_en)
41938001c741SPing-Ke Shih 		val |= B_AX_GNT_WL_RFC_S1_SW_CTRL | B_AX_GNT_WL_BB_S1_SW_CTRL;
41948001c741SPing-Ke Shih 
4195e3ec7017SPing-Ke Shih 	ret = rtw89_mac_write_lte(rtwdev, R_AX_LTE_SW_CFG_1, val);
4196e3ec7017SPing-Ke Shih 	if (ret) {
4197e3ec7017SPing-Ke Shih 		rtw89_err(rtwdev, "Write LTE fail!\n");
4198e3ec7017SPing-Ke Shih 		return ret;
4199e3ec7017SPing-Ke Shih 	}
4200e3ec7017SPing-Ke Shih 
4201e3ec7017SPing-Ke Shih 	return 0;
4202e3ec7017SPing-Ke Shih }
4203feed6541SChia-Yuan Li EXPORT_SYMBOL(rtw89_mac_cfg_gnt);
4204feed6541SChia-Yuan Li 
4205feed6541SChia-Yuan Li int rtw89_mac_cfg_gnt_v1(struct rtw89_dev *rtwdev,
4206feed6541SChia-Yuan Li 			 const struct rtw89_mac_ax_coex_gnt *gnt_cfg)
4207feed6541SChia-Yuan Li {
4208feed6541SChia-Yuan Li 	u32 val = 0;
4209feed6541SChia-Yuan Li 
4210feed6541SChia-Yuan Li 	if (gnt_cfg->band[0].gnt_bt)
4211feed6541SChia-Yuan Li 		val |= B_AX_GNT_BT_RFC_S0_VAL | B_AX_GNT_BT_RX_VAL |
4212feed6541SChia-Yuan Li 		       B_AX_GNT_BT_TX_VAL;
4213feed6541SChia-Yuan Li 	else
4214feed6541SChia-Yuan Li 		val |= B_AX_WL_ACT_VAL;
4215feed6541SChia-Yuan Li 
4216feed6541SChia-Yuan Li 	if (gnt_cfg->band[0].gnt_bt_sw_en)
4217feed6541SChia-Yuan Li 		val |= B_AX_GNT_BT_RFC_S0_SWCTRL | B_AX_GNT_BT_RX_SWCTRL |
4218feed6541SChia-Yuan Li 		       B_AX_GNT_BT_TX_SWCTRL | B_AX_WL_ACT_SWCTRL;
4219feed6541SChia-Yuan Li 
4220feed6541SChia-Yuan Li 	if (gnt_cfg->band[0].gnt_wl)
4221feed6541SChia-Yuan Li 		val |= B_AX_GNT_WL_RFC_S0_VAL | B_AX_GNT_WL_RX_VAL |
4222feed6541SChia-Yuan Li 		       B_AX_GNT_WL_TX_VAL | B_AX_GNT_WL_BB_VAL;
4223feed6541SChia-Yuan Li 
4224feed6541SChia-Yuan Li 	if (gnt_cfg->band[0].gnt_wl_sw_en)
4225feed6541SChia-Yuan Li 		val |= B_AX_GNT_WL_RFC_S0_SWCTRL | B_AX_GNT_WL_RX_SWCTRL |
4226feed6541SChia-Yuan Li 		       B_AX_GNT_WL_TX_SWCTRL | B_AX_GNT_WL_BB_SWCTRL;
4227feed6541SChia-Yuan Li 
4228feed6541SChia-Yuan Li 	if (gnt_cfg->band[1].gnt_bt)
4229feed6541SChia-Yuan Li 		val |= B_AX_GNT_BT_RFC_S1_VAL | B_AX_GNT_BT_RX_VAL |
4230feed6541SChia-Yuan Li 		       B_AX_GNT_BT_TX_VAL;
4231feed6541SChia-Yuan Li 	else
4232feed6541SChia-Yuan Li 		val |= B_AX_WL_ACT_VAL;
4233feed6541SChia-Yuan Li 
4234feed6541SChia-Yuan Li 	if (gnt_cfg->band[1].gnt_bt_sw_en)
4235feed6541SChia-Yuan Li 		val |= B_AX_GNT_BT_RFC_S1_SWCTRL | B_AX_GNT_BT_RX_SWCTRL |
4236feed6541SChia-Yuan Li 		       B_AX_GNT_BT_TX_SWCTRL | B_AX_WL_ACT_SWCTRL;
4237feed6541SChia-Yuan Li 
4238feed6541SChia-Yuan Li 	if (gnt_cfg->band[1].gnt_wl)
4239feed6541SChia-Yuan Li 		val |= B_AX_GNT_WL_RFC_S1_VAL | B_AX_GNT_WL_RX_VAL |
4240feed6541SChia-Yuan Li 		       B_AX_GNT_WL_TX_VAL | B_AX_GNT_WL_BB_VAL;
4241feed6541SChia-Yuan Li 
4242feed6541SChia-Yuan Li 	if (gnt_cfg->band[1].gnt_wl_sw_en)
4243feed6541SChia-Yuan Li 		val |= B_AX_GNT_WL_RFC_S1_SWCTRL | B_AX_GNT_WL_RX_SWCTRL |
4244feed6541SChia-Yuan Li 		       B_AX_GNT_WL_TX_SWCTRL | B_AX_GNT_WL_BB_SWCTRL;
4245feed6541SChia-Yuan Li 
4246feed6541SChia-Yuan Li 	rtw89_write32(rtwdev, R_AX_GNT_SW_CTRL, val);
4247feed6541SChia-Yuan Li 
4248feed6541SChia-Yuan Li 	return 0;
4249feed6541SChia-Yuan Li }
4250feed6541SChia-Yuan Li EXPORT_SYMBOL(rtw89_mac_cfg_gnt_v1);
4251e3ec7017SPing-Ke Shih 
4252e3ec7017SPing-Ke Shih int rtw89_mac_cfg_plt(struct rtw89_dev *rtwdev, struct rtw89_mac_ax_plt *plt)
4253e3ec7017SPing-Ke Shih {
4254e3ec7017SPing-Ke Shih 	u32 reg;
425528e7ea8aSPing-Ke Shih 	u16 val;
4256e3ec7017SPing-Ke Shih 	int ret;
4257e3ec7017SPing-Ke Shih 
4258e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, plt->band, RTW89_CMAC_SEL);
4259e3ec7017SPing-Ke Shih 	if (ret)
4260e3ec7017SPing-Ke Shih 		return ret;
4261e3ec7017SPing-Ke Shih 
4262e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_BT_PLT, plt->band);
4263e3ec7017SPing-Ke Shih 	val = (plt->tx & RTW89_MAC_AX_PLT_LTE_RX ? B_AX_TX_PLT_GNT_LTE_RX : 0) |
4264e3ec7017SPing-Ke Shih 	      (plt->tx & RTW89_MAC_AX_PLT_GNT_BT_TX ? B_AX_TX_PLT_GNT_BT_TX : 0) |
4265e3ec7017SPing-Ke Shih 	      (plt->tx & RTW89_MAC_AX_PLT_GNT_BT_RX ? B_AX_TX_PLT_GNT_BT_RX : 0) |
4266e3ec7017SPing-Ke Shih 	      (plt->tx & RTW89_MAC_AX_PLT_GNT_WL ? B_AX_TX_PLT_GNT_WL : 0) |
4267e3ec7017SPing-Ke Shih 	      (plt->rx & RTW89_MAC_AX_PLT_LTE_RX ? B_AX_RX_PLT_GNT_LTE_RX : 0) |
4268e3ec7017SPing-Ke Shih 	      (plt->rx & RTW89_MAC_AX_PLT_GNT_BT_TX ? B_AX_RX_PLT_GNT_BT_TX : 0) |
4269e3ec7017SPing-Ke Shih 	      (plt->rx & RTW89_MAC_AX_PLT_GNT_BT_RX ? B_AX_RX_PLT_GNT_BT_RX : 0) |
427028e7ea8aSPing-Ke Shih 	      (plt->rx & RTW89_MAC_AX_PLT_GNT_WL ? B_AX_RX_PLT_GNT_WL : 0) |
427128e7ea8aSPing-Ke Shih 	      B_AX_PLT_EN;
427228e7ea8aSPing-Ke Shih 	rtw89_write16(rtwdev, reg, val);
4273e3ec7017SPing-Ke Shih 
4274e3ec7017SPing-Ke Shih 	return 0;
4275e3ec7017SPing-Ke Shih }
4276e3ec7017SPing-Ke Shih 
4277e3ec7017SPing-Ke Shih void rtw89_mac_cfg_sb(struct rtw89_dev *rtwdev, u32 val)
4278e3ec7017SPing-Ke Shih {
4279e3ec7017SPing-Ke Shih 	u32 fw_sb;
4280e3ec7017SPing-Ke Shih 
4281e3ec7017SPing-Ke Shih 	fw_sb = rtw89_read32(rtwdev, R_AX_SCOREBOARD);
4282e3ec7017SPing-Ke Shih 	fw_sb = FIELD_GET(B_MAC_AX_SB_FW_MASK, fw_sb);
4283e3ec7017SPing-Ke Shih 	fw_sb = fw_sb & ~B_MAC_AX_BTGS1_NOTIFY;
4284e3ec7017SPing-Ke Shih 	if (!test_bit(RTW89_FLAG_POWERON, rtwdev->flags))
4285e3ec7017SPing-Ke Shih 		fw_sb = fw_sb | MAC_AX_NOTIFY_PWR_MAJOR;
4286e3ec7017SPing-Ke Shih 	else
4287e3ec7017SPing-Ke Shih 		fw_sb = fw_sb | MAC_AX_NOTIFY_TP_MAJOR;
4288e3ec7017SPing-Ke Shih 	val = FIELD_GET(B_MAC_AX_SB_DRV_MASK, val);
4289e3ec7017SPing-Ke Shih 	val = B_AX_TOGGLE |
4290e3ec7017SPing-Ke Shih 	      FIELD_PREP(B_MAC_AX_SB_DRV_MASK, val) |
4291e3ec7017SPing-Ke Shih 	      FIELD_PREP(B_MAC_AX_SB_FW_MASK, fw_sb);
4292e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_SCOREBOARD, val);
4293e3ec7017SPing-Ke Shih 	fsleep(1000); /* avoid BT FW loss information */
4294e3ec7017SPing-Ke Shih }
4295e3ec7017SPing-Ke Shih 
4296e3ec7017SPing-Ke Shih u32 rtw89_mac_get_sb(struct rtw89_dev *rtwdev)
4297e3ec7017SPing-Ke Shih {
4298e3ec7017SPing-Ke Shih 	return rtw89_read32(rtwdev, R_AX_SCOREBOARD);
4299e3ec7017SPing-Ke Shih }
4300e3ec7017SPing-Ke Shih 
4301e3ec7017SPing-Ke Shih int rtw89_mac_cfg_ctrl_path(struct rtw89_dev *rtwdev, bool wl)
4302e3ec7017SPing-Ke Shih {
4303e3ec7017SPing-Ke Shih 	u8 val = rtw89_read8(rtwdev, R_AX_SYS_SDIO_CTRL + 3);
4304e3ec7017SPing-Ke Shih 
4305e3ec7017SPing-Ke Shih 	val = wl ? val | BIT(2) : val & ~BIT(2);
4306e3ec7017SPing-Ke Shih 	rtw89_write8(rtwdev, R_AX_SYS_SDIO_CTRL + 3, val);
4307e3ec7017SPing-Ke Shih 
4308e3ec7017SPing-Ke Shih 	return 0;
4309e3ec7017SPing-Ke Shih }
4310feed6541SChia-Yuan Li EXPORT_SYMBOL(rtw89_mac_cfg_ctrl_path);
4311feed6541SChia-Yuan Li 
4312feed6541SChia-Yuan Li int rtw89_mac_cfg_ctrl_path_v1(struct rtw89_dev *rtwdev, bool wl)
4313feed6541SChia-Yuan Li {
4314feed6541SChia-Yuan Li 	struct rtw89_btc *btc = &rtwdev->btc;
4315feed6541SChia-Yuan Li 	struct rtw89_btc_dm *dm = &btc->dm;
4316feed6541SChia-Yuan Li 	struct rtw89_mac_ax_gnt *g = dm->gnt.band;
4317feed6541SChia-Yuan Li 	int i;
4318feed6541SChia-Yuan Li 
4319feed6541SChia-Yuan Li 	if (wl)
4320feed6541SChia-Yuan Li 		return 0;
4321feed6541SChia-Yuan Li 
4322feed6541SChia-Yuan Li 	for (i = 0; i < RTW89_PHY_MAX; i++) {
4323feed6541SChia-Yuan Li 		g[i].gnt_bt_sw_en = 1;
4324feed6541SChia-Yuan Li 		g[i].gnt_bt = 1;
4325feed6541SChia-Yuan Li 		g[i].gnt_wl_sw_en = 1;
4326feed6541SChia-Yuan Li 		g[i].gnt_wl = 0;
4327feed6541SChia-Yuan Li 	}
4328feed6541SChia-Yuan Li 
4329feed6541SChia-Yuan Li 	return rtw89_mac_cfg_gnt_v1(rtwdev, &dm->gnt);
4330feed6541SChia-Yuan Li }
4331feed6541SChia-Yuan Li EXPORT_SYMBOL(rtw89_mac_cfg_ctrl_path_v1);
4332e3ec7017SPing-Ke Shih 
4333e3ec7017SPing-Ke Shih bool rtw89_mac_get_ctrl_path(struct rtw89_dev *rtwdev)
4334e3ec7017SPing-Ke Shih {
4335e3ec7017SPing-Ke Shih 	u8 val = rtw89_read8(rtwdev, R_AX_SYS_SDIO_CTRL + 3);
4336e3ec7017SPing-Ke Shih 
4337e3ec7017SPing-Ke Shih 	return FIELD_GET(B_AX_LTE_MUX_CTRL_PATH >> 24, val);
4338e3ec7017SPing-Ke Shih }
4339e3ec7017SPing-Ke Shih 
43408c7e9cebSChing-Te Ku u16 rtw89_mac_get_plt_cnt(struct rtw89_dev *rtwdev, u8 band)
43418c7e9cebSChing-Te Ku {
43428c7e9cebSChing-Te Ku 	u32 reg;
43438c7e9cebSChing-Te Ku 	u16 cnt;
43448c7e9cebSChing-Te Ku 
43458c7e9cebSChing-Te Ku 	reg = rtw89_mac_reg_by_idx(R_AX_BT_PLT, band);
43468c7e9cebSChing-Te Ku 	cnt = rtw89_read32_mask(rtwdev, reg, B_AX_BT_PLT_PKT_CNT_MASK);
43478c7e9cebSChing-Te Ku 	rtw89_write16_set(rtwdev, reg, B_AX_BT_PLT_RST);
43488c7e9cebSChing-Te Ku 
43498c7e9cebSChing-Te Ku 	return cnt;
43508c7e9cebSChing-Te Ku }
43518c7e9cebSChing-Te Ku 
4352e3ec7017SPing-Ke Shih static void rtw89_mac_bfee_ctrl(struct rtw89_dev *rtwdev, u8 mac_idx, bool en)
4353e3ec7017SPing-Ke Shih {
4354e3ec7017SPing-Ke Shih 	u32 reg;
4355e3ec7017SPing-Ke Shih 	u32 mask = B_AX_BFMEE_HT_NDPA_EN | B_AX_BFMEE_VHT_NDPA_EN |
4356e3ec7017SPing-Ke Shih 		   B_AX_BFMEE_HE_NDPA_EN;
4357e3ec7017SPing-Ke Shih 
4358e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_BF, "set bfee ndpa_en to %d\n", en);
4359e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_BFMEE_RESP_OPTION, mac_idx);
4360e3ec7017SPing-Ke Shih 	if (en) {
4361e3ec7017SPing-Ke Shih 		set_bit(RTW89_FLAG_BFEE_EN, rtwdev->flags);
4362e3ec7017SPing-Ke Shih 		rtw89_write32_set(rtwdev, reg, mask);
4363e3ec7017SPing-Ke Shih 	} else {
4364e3ec7017SPing-Ke Shih 		clear_bit(RTW89_FLAG_BFEE_EN, rtwdev->flags);
4365e3ec7017SPing-Ke Shih 		rtw89_write32_clr(rtwdev, reg, mask);
4366e3ec7017SPing-Ke Shih 	}
4367e3ec7017SPing-Ke Shih }
4368e3ec7017SPing-Ke Shih 
4369e3ec7017SPing-Ke Shih static int rtw89_mac_init_bfee(struct rtw89_dev *rtwdev, u8 mac_idx)
4370e3ec7017SPing-Ke Shih {
4371e3ec7017SPing-Ke Shih 	u32 reg;
4372e3ec7017SPing-Ke Shih 	u32 val32;
4373e3ec7017SPing-Ke Shih 	int ret;
4374e3ec7017SPing-Ke Shih 
4375e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL);
4376e3ec7017SPing-Ke Shih 	if (ret)
4377e3ec7017SPing-Ke Shih 		return ret;
4378e3ec7017SPing-Ke Shih 
4379e3ec7017SPing-Ke Shih 	/* AP mode set tx gid to 63 */
4380e3ec7017SPing-Ke Shih 	/* STA mode set tx gid to 0(default) */
4381e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_BFMER_CTRL_0, mac_idx);
4382e3ec7017SPing-Ke Shih 	rtw89_write32_set(rtwdev, reg, B_AX_BFMER_NDP_BFEN);
4383e3ec7017SPing-Ke Shih 
4384e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_TRXPTCL_RESP_CSI_RRSC, mac_idx);
4385e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, reg, CSI_RRSC_BMAP);
4386e3ec7017SPing-Ke Shih 
4387e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_BFMEE_RESP_OPTION, mac_idx);
4388e3ec7017SPing-Ke Shih 	val32 = FIELD_PREP(B_AX_BFMEE_BFRP_RX_STANDBY_TIMER_MASK, BFRP_RX_STANDBY_TIMER);
4389e3ec7017SPing-Ke Shih 	val32 |= FIELD_PREP(B_AX_BFMEE_NDP_RX_STANDBY_TIMER_MASK, NDP_RX_STANDBY_TIMER);
4390e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, reg, val32);
4391e3ec7017SPing-Ke Shih 	rtw89_mac_bfee_ctrl(rtwdev, mac_idx, true);
4392e3ec7017SPing-Ke Shih 
4393e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_TRXPTCL_RESP_CSI_CTRL_0, mac_idx);
4394e3ec7017SPing-Ke Shih 	rtw89_write32_set(rtwdev, reg, B_AX_BFMEE_BFPARAM_SEL |
4395e3ec7017SPing-Ke Shih 				       B_AX_BFMEE_USE_NSTS |
4396e3ec7017SPing-Ke Shih 				       B_AX_BFMEE_CSI_GID_SEL |
4397e3ec7017SPing-Ke Shih 				       B_AX_BFMEE_CSI_FORCE_RETE_EN);
4398e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_TRXPTCL_RESP_CSI_RATE, mac_idx);
4399e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, reg,
4400e3ec7017SPing-Ke Shih 		      u32_encode_bits(CSI_INIT_RATE_HT, B_AX_BFMEE_HT_CSI_RATE_MASK) |
4401e3ec7017SPing-Ke Shih 		      u32_encode_bits(CSI_INIT_RATE_VHT, B_AX_BFMEE_VHT_CSI_RATE_MASK) |
4402e3ec7017SPing-Ke Shih 		      u32_encode_bits(CSI_INIT_RATE_HE, B_AX_BFMEE_HE_CSI_RATE_MASK));
4403e3ec7017SPing-Ke Shih 
440462440fbeSPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_CSIRPT_OPTION, mac_idx);
440562440fbeSPing-Ke Shih 	rtw89_write32_set(rtwdev, reg,
440662440fbeSPing-Ke Shih 			  B_AX_CSIPRT_VHTSU_AID_EN | B_AX_CSIPRT_HESU_AID_EN);
440762440fbeSPing-Ke Shih 
4408e3ec7017SPing-Ke Shih 	return 0;
4409e3ec7017SPing-Ke Shih }
4410e3ec7017SPing-Ke Shih 
4411e3ec7017SPing-Ke Shih static int rtw89_mac_set_csi_para_reg(struct rtw89_dev *rtwdev,
4412e3ec7017SPing-Ke Shih 				      struct ieee80211_vif *vif,
4413e3ec7017SPing-Ke Shih 				      struct ieee80211_sta *sta)
4414e3ec7017SPing-Ke Shih {
4415e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
4416e3ec7017SPing-Ke Shih 	u8 mac_idx = rtwvif->mac_idx;
4417e3ec7017SPing-Ke Shih 	u8 nc = 1, nr = 3, ng = 0, cb = 1, cs = 1, ldpc_en = 1, stbc_en = 1;
4418e3ec7017SPing-Ke Shih 	u8 port_sel = rtwvif->port;
4419e3ec7017SPing-Ke Shih 	u8 sound_dim = 3, t;
4420046d2e7cSSriram R 	u8 *phy_cap = sta->deflink.he_cap.he_cap_elem.phy_cap_info;
4421e3ec7017SPing-Ke Shih 	u32 reg;
4422e3ec7017SPing-Ke Shih 	u16 val;
4423e3ec7017SPing-Ke Shih 	int ret;
4424e3ec7017SPing-Ke Shih 
4425e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL);
4426e3ec7017SPing-Ke Shih 	if (ret)
4427e3ec7017SPing-Ke Shih 		return ret;
4428e3ec7017SPing-Ke Shih 
4429e3ec7017SPing-Ke Shih 	if ((phy_cap[3] & IEEE80211_HE_PHY_CAP3_SU_BEAMFORMER) ||
4430e3ec7017SPing-Ke Shih 	    (phy_cap[4] & IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER)) {
4431e3ec7017SPing-Ke Shih 		ldpc_en &= !!(phy_cap[1] & IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD);
4432e3ec7017SPing-Ke Shih 		stbc_en &= !!(phy_cap[2] & IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ);
4433e3ec7017SPing-Ke Shih 		t = FIELD_GET(IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK,
4434e3ec7017SPing-Ke Shih 			      phy_cap[5]);
4435e3ec7017SPing-Ke Shih 		sound_dim = min(sound_dim, t);
4436e3ec7017SPing-Ke Shih 	}
4437046d2e7cSSriram R 	if ((sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) ||
4438046d2e7cSSriram R 	    (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
4439046d2e7cSSriram R 		ldpc_en &= !!(sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC);
4440046d2e7cSSriram R 		stbc_en &= !!(sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXSTBC_MASK);
4441e3ec7017SPing-Ke Shih 		t = FIELD_GET(IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK,
4442046d2e7cSSriram R 			      sta->deflink.vht_cap.cap);
4443e3ec7017SPing-Ke Shih 		sound_dim = min(sound_dim, t);
4444e3ec7017SPing-Ke Shih 	}
4445e3ec7017SPing-Ke Shih 	nc = min(nc, sound_dim);
4446e3ec7017SPing-Ke Shih 	nr = min(nr, sound_dim);
4447e3ec7017SPing-Ke Shih 
4448e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_TRXPTCL_RESP_CSI_CTRL_0, mac_idx);
4449e3ec7017SPing-Ke Shih 	rtw89_write32_set(rtwdev, reg, B_AX_BFMEE_BFPARAM_SEL);
4450e3ec7017SPing-Ke Shih 
4451e3ec7017SPing-Ke Shih 	val = FIELD_PREP(B_AX_BFMEE_CSIINFO0_NC_MASK, nc) |
4452e3ec7017SPing-Ke Shih 	      FIELD_PREP(B_AX_BFMEE_CSIINFO0_NR_MASK, nr) |
4453e3ec7017SPing-Ke Shih 	      FIELD_PREP(B_AX_BFMEE_CSIINFO0_NG_MASK, ng) |
4454e3ec7017SPing-Ke Shih 	      FIELD_PREP(B_AX_BFMEE_CSIINFO0_CB_MASK, cb) |
4455e3ec7017SPing-Ke Shih 	      FIELD_PREP(B_AX_BFMEE_CSIINFO0_CS_MASK, cs) |
4456e3ec7017SPing-Ke Shih 	      FIELD_PREP(B_AX_BFMEE_CSIINFO0_LDPC_EN, ldpc_en) |
4457e3ec7017SPing-Ke Shih 	      FIELD_PREP(B_AX_BFMEE_CSIINFO0_STBC_EN, stbc_en);
4458e3ec7017SPing-Ke Shih 
4459e3ec7017SPing-Ke Shih 	if (port_sel == 0)
4460e3ec7017SPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(R_AX_TRXPTCL_RESP_CSI_CTRL_0, mac_idx);
4461e3ec7017SPing-Ke Shih 	else
4462e3ec7017SPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(R_AX_TRXPTCL_RESP_CSI_CTRL_1, mac_idx);
4463e3ec7017SPing-Ke Shih 
4464e3ec7017SPing-Ke Shih 	rtw89_write16(rtwdev, reg, val);
4465e3ec7017SPing-Ke Shih 
4466e3ec7017SPing-Ke Shih 	return 0;
4467e3ec7017SPing-Ke Shih }
4468e3ec7017SPing-Ke Shih 
4469e3ec7017SPing-Ke Shih static int rtw89_mac_csi_rrsc(struct rtw89_dev *rtwdev,
4470e3ec7017SPing-Ke Shih 			      struct ieee80211_vif *vif,
4471e3ec7017SPing-Ke Shih 			      struct ieee80211_sta *sta)
4472e3ec7017SPing-Ke Shih {
4473e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
4474e3ec7017SPing-Ke Shih 	u32 rrsc = BIT(RTW89_MAC_BF_RRSC_6M) | BIT(RTW89_MAC_BF_RRSC_24M);
4475e3ec7017SPing-Ke Shih 	u32 reg;
4476e3ec7017SPing-Ke Shih 	u8 mac_idx = rtwvif->mac_idx;
4477e3ec7017SPing-Ke Shih 	int ret;
4478e3ec7017SPing-Ke Shih 
4479e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL);
4480e3ec7017SPing-Ke Shih 	if (ret)
4481e3ec7017SPing-Ke Shih 		return ret;
4482e3ec7017SPing-Ke Shih 
4483046d2e7cSSriram R 	if (sta->deflink.he_cap.has_he) {
4484e3ec7017SPing-Ke Shih 		rrsc |= (BIT(RTW89_MAC_BF_RRSC_HE_MSC0) |
4485e3ec7017SPing-Ke Shih 			 BIT(RTW89_MAC_BF_RRSC_HE_MSC3) |
4486e3ec7017SPing-Ke Shih 			 BIT(RTW89_MAC_BF_RRSC_HE_MSC5));
4487e3ec7017SPing-Ke Shih 	}
4488046d2e7cSSriram R 	if (sta->deflink.vht_cap.vht_supported) {
4489e3ec7017SPing-Ke Shih 		rrsc |= (BIT(RTW89_MAC_BF_RRSC_VHT_MSC0) |
4490e3ec7017SPing-Ke Shih 			 BIT(RTW89_MAC_BF_RRSC_VHT_MSC3) |
4491e3ec7017SPing-Ke Shih 			 BIT(RTW89_MAC_BF_RRSC_VHT_MSC5));
4492e3ec7017SPing-Ke Shih 	}
4493046d2e7cSSriram R 	if (sta->deflink.ht_cap.ht_supported) {
4494e3ec7017SPing-Ke Shih 		rrsc |= (BIT(RTW89_MAC_BF_RRSC_HT_MSC0) |
4495e3ec7017SPing-Ke Shih 			 BIT(RTW89_MAC_BF_RRSC_HT_MSC3) |
4496e3ec7017SPing-Ke Shih 			 BIT(RTW89_MAC_BF_RRSC_HT_MSC5));
4497e3ec7017SPing-Ke Shih 	}
4498e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_TRXPTCL_RESP_CSI_CTRL_0, mac_idx);
4499e3ec7017SPing-Ke Shih 	rtw89_write32_set(rtwdev, reg, B_AX_BFMEE_BFPARAM_SEL);
4500e3ec7017SPing-Ke Shih 	rtw89_write32_clr(rtwdev, reg, B_AX_BFMEE_CSI_FORCE_RETE_EN);
4501e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev,
4502e3ec7017SPing-Ke Shih 		      rtw89_mac_reg_by_idx(R_AX_TRXPTCL_RESP_CSI_RRSC, mac_idx),
4503e3ec7017SPing-Ke Shih 		      rrsc);
4504e3ec7017SPing-Ke Shih 
4505e3ec7017SPing-Ke Shih 	return 0;
4506e3ec7017SPing-Ke Shih }
4507e3ec7017SPing-Ke Shih 
4508e3ec7017SPing-Ke Shih void rtw89_mac_bf_assoc(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif,
4509e3ec7017SPing-Ke Shih 			struct ieee80211_sta *sta)
4510e3ec7017SPing-Ke Shih {
4511e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
4512e3ec7017SPing-Ke Shih 
4513e3ec7017SPing-Ke Shih 	if (rtw89_sta_has_beamformer_cap(sta)) {
4514e3ec7017SPing-Ke Shih 		rtw89_debug(rtwdev, RTW89_DBG_BF,
4515e3ec7017SPing-Ke Shih 			    "initialize bfee for new association\n");
4516e3ec7017SPing-Ke Shih 		rtw89_mac_init_bfee(rtwdev, rtwvif->mac_idx);
4517e3ec7017SPing-Ke Shih 		rtw89_mac_set_csi_para_reg(rtwdev, vif, sta);
4518e3ec7017SPing-Ke Shih 		rtw89_mac_csi_rrsc(rtwdev, vif, sta);
4519e3ec7017SPing-Ke Shih 	}
4520e3ec7017SPing-Ke Shih }
4521e3ec7017SPing-Ke Shih 
4522e3ec7017SPing-Ke Shih void rtw89_mac_bf_disassoc(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif,
4523e3ec7017SPing-Ke Shih 			   struct ieee80211_sta *sta)
4524e3ec7017SPing-Ke Shih {
4525e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
4526e3ec7017SPing-Ke Shih 
4527e3ec7017SPing-Ke Shih 	rtw89_mac_bfee_ctrl(rtwdev, rtwvif->mac_idx, false);
4528e3ec7017SPing-Ke Shih }
4529e3ec7017SPing-Ke Shih 
4530e3ec7017SPing-Ke Shih void rtw89_mac_bf_set_gid_table(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif,
4531e3ec7017SPing-Ke Shih 				struct ieee80211_bss_conf *conf)
4532e3ec7017SPing-Ke Shih {
4533e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv;
4534e3ec7017SPing-Ke Shih 	u8 mac_idx = rtwvif->mac_idx;
4535e3ec7017SPing-Ke Shih 	__le32 *p;
4536e3ec7017SPing-Ke Shih 
4537e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_BF, "update bf GID table\n");
4538e3ec7017SPing-Ke Shih 
4539e3ec7017SPing-Ke Shih 	p = (__le32 *)conf->mu_group.membership;
4540e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, rtw89_mac_reg_by_idx(R_AX_GID_POSITION_EN0, mac_idx),
4541e3ec7017SPing-Ke Shih 		      le32_to_cpu(p[0]));
4542e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, rtw89_mac_reg_by_idx(R_AX_GID_POSITION_EN1, mac_idx),
4543e3ec7017SPing-Ke Shih 		      le32_to_cpu(p[1]));
4544e3ec7017SPing-Ke Shih 
4545e3ec7017SPing-Ke Shih 	p = (__le32 *)conf->mu_group.position;
4546e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, rtw89_mac_reg_by_idx(R_AX_GID_POSITION0, mac_idx),
4547e3ec7017SPing-Ke Shih 		      le32_to_cpu(p[0]));
4548e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, rtw89_mac_reg_by_idx(R_AX_GID_POSITION1, mac_idx),
4549e3ec7017SPing-Ke Shih 		      le32_to_cpu(p[1]));
4550e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, rtw89_mac_reg_by_idx(R_AX_GID_POSITION2, mac_idx),
4551e3ec7017SPing-Ke Shih 		      le32_to_cpu(p[2]));
4552e3ec7017SPing-Ke Shih 	rtw89_write32(rtwdev, rtw89_mac_reg_by_idx(R_AX_GID_POSITION3, mac_idx),
4553e3ec7017SPing-Ke Shih 		      le32_to_cpu(p[3]));
4554e3ec7017SPing-Ke Shih }
4555e3ec7017SPing-Ke Shih 
4556e3ec7017SPing-Ke Shih struct rtw89_mac_bf_monitor_iter_data {
4557e3ec7017SPing-Ke Shih 	struct rtw89_dev *rtwdev;
4558e3ec7017SPing-Ke Shih 	struct ieee80211_sta *down_sta;
4559e3ec7017SPing-Ke Shih 	int count;
4560e3ec7017SPing-Ke Shih };
4561e3ec7017SPing-Ke Shih 
4562e3ec7017SPing-Ke Shih static
4563e3ec7017SPing-Ke Shih void rtw89_mac_bf_monitor_calc_iter(void *data, struct ieee80211_sta *sta)
4564e3ec7017SPing-Ke Shih {
4565e3ec7017SPing-Ke Shih 	struct rtw89_mac_bf_monitor_iter_data *iter_data =
4566e3ec7017SPing-Ke Shih 				(struct rtw89_mac_bf_monitor_iter_data *)data;
4567e3ec7017SPing-Ke Shih 	struct ieee80211_sta *down_sta = iter_data->down_sta;
4568e3ec7017SPing-Ke Shih 	int *count = &iter_data->count;
4569e3ec7017SPing-Ke Shih 
4570e3ec7017SPing-Ke Shih 	if (down_sta == sta)
4571e3ec7017SPing-Ke Shih 		return;
4572e3ec7017SPing-Ke Shih 
4573e3ec7017SPing-Ke Shih 	if (rtw89_sta_has_beamformer_cap(sta))
4574e3ec7017SPing-Ke Shih 		(*count)++;
4575e3ec7017SPing-Ke Shih }
4576e3ec7017SPing-Ke Shih 
4577e3ec7017SPing-Ke Shih void rtw89_mac_bf_monitor_calc(struct rtw89_dev *rtwdev,
4578e3ec7017SPing-Ke Shih 			       struct ieee80211_sta *sta, bool disconnect)
4579e3ec7017SPing-Ke Shih {
4580e3ec7017SPing-Ke Shih 	struct rtw89_mac_bf_monitor_iter_data data;
4581e3ec7017SPing-Ke Shih 
4582e3ec7017SPing-Ke Shih 	data.rtwdev = rtwdev;
4583e3ec7017SPing-Ke Shih 	data.down_sta = disconnect ? sta : NULL;
4584e3ec7017SPing-Ke Shih 	data.count = 0;
4585e3ec7017SPing-Ke Shih 	ieee80211_iterate_stations_atomic(rtwdev->hw,
4586e3ec7017SPing-Ke Shih 					  rtw89_mac_bf_monitor_calc_iter,
4587e3ec7017SPing-Ke Shih 					  &data);
4588e3ec7017SPing-Ke Shih 
4589e3ec7017SPing-Ke Shih 	rtw89_debug(rtwdev, RTW89_DBG_BF, "bfee STA count=%d\n", data.count);
4590e3ec7017SPing-Ke Shih 	if (data.count)
4591e3ec7017SPing-Ke Shih 		set_bit(RTW89_FLAG_BFEE_MON, rtwdev->flags);
4592e3ec7017SPing-Ke Shih 	else
4593e3ec7017SPing-Ke Shih 		clear_bit(RTW89_FLAG_BFEE_MON, rtwdev->flags);
4594e3ec7017SPing-Ke Shih }
4595e3ec7017SPing-Ke Shih 
4596e3ec7017SPing-Ke Shih void _rtw89_mac_bf_monitor_track(struct rtw89_dev *rtwdev)
4597e3ec7017SPing-Ke Shih {
4598e3ec7017SPing-Ke Shih 	struct rtw89_traffic_stats *stats = &rtwdev->stats;
4599e3ec7017SPing-Ke Shih 	struct rtw89_vif *rtwvif;
46001646ce8fSYe Guojin 	bool en = stats->tx_tfc_lv <= stats->rx_tfc_lv;
4601e3ec7017SPing-Ke Shih 	bool old = test_bit(RTW89_FLAG_BFEE_EN, rtwdev->flags);
4602e3ec7017SPing-Ke Shih 
4603e3ec7017SPing-Ke Shih 	if (en == old)
4604e3ec7017SPing-Ke Shih 		return;
4605e3ec7017SPing-Ke Shih 
4606e3ec7017SPing-Ke Shih 	rtw89_for_each_rtwvif(rtwdev, rtwvif)
4607e3ec7017SPing-Ke Shih 		rtw89_mac_bfee_ctrl(rtwdev, rtwvif->mac_idx, en);
4608e3ec7017SPing-Ke Shih }
4609e3ec7017SPing-Ke Shih 
4610e3ec7017SPing-Ke Shih static int
4611e3ec7017SPing-Ke Shih __rtw89_mac_set_tx_time(struct rtw89_dev *rtwdev, struct rtw89_sta *rtwsta,
4612e3ec7017SPing-Ke Shih 			u32 tx_time)
4613e3ec7017SPing-Ke Shih {
4614e3ec7017SPing-Ke Shih #define MAC_AX_DFLT_TX_TIME 5280
4615e3ec7017SPing-Ke Shih 	u8 mac_idx = rtwsta->rtwvif->mac_idx;
4616e3ec7017SPing-Ke Shih 	u32 max_tx_time = tx_time == 0 ? MAC_AX_DFLT_TX_TIME : tx_time;
4617e3ec7017SPing-Ke Shih 	u32 reg;
4618e3ec7017SPing-Ke Shih 	int ret = 0;
4619e3ec7017SPing-Ke Shih 
4620e3ec7017SPing-Ke Shih 	if (rtwsta->cctl_tx_time) {
4621e3ec7017SPing-Ke Shih 		rtwsta->ampdu_max_time = (max_tx_time - 512) >> 9;
4622e3ec7017SPing-Ke Shih 		ret = rtw89_fw_h2c_txtime_cmac_tbl(rtwdev, rtwsta);
4623e3ec7017SPing-Ke Shih 	} else {
4624e3ec7017SPing-Ke Shih 		ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL);
4625e3ec7017SPing-Ke Shih 		if (ret) {
4626e3ec7017SPing-Ke Shih 			rtw89_warn(rtwdev, "failed to check cmac in set txtime\n");
4627e3ec7017SPing-Ke Shih 			return ret;
4628e3ec7017SPing-Ke Shih 		}
4629e3ec7017SPing-Ke Shih 
4630e3ec7017SPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(R_AX_AMPDU_AGG_LIMIT, mac_idx);
4631e3ec7017SPing-Ke Shih 		rtw89_write32_mask(rtwdev, reg, B_AX_AMPDU_MAX_TIME_MASK,
4632e3ec7017SPing-Ke Shih 				   max_tx_time >> 5);
4633e3ec7017SPing-Ke Shih 	}
4634e3ec7017SPing-Ke Shih 
4635e3ec7017SPing-Ke Shih 	return ret;
4636e3ec7017SPing-Ke Shih }
4637e3ec7017SPing-Ke Shih 
4638e3ec7017SPing-Ke Shih int rtw89_mac_set_tx_time(struct rtw89_dev *rtwdev, struct rtw89_sta *rtwsta,
4639e3ec7017SPing-Ke Shih 			  bool resume, u32 tx_time)
4640e3ec7017SPing-Ke Shih {
4641e3ec7017SPing-Ke Shih 	int ret = 0;
4642e3ec7017SPing-Ke Shih 
4643e3ec7017SPing-Ke Shih 	if (!resume) {
4644e3ec7017SPing-Ke Shih 		rtwsta->cctl_tx_time = true;
4645e3ec7017SPing-Ke Shih 		ret = __rtw89_mac_set_tx_time(rtwdev, rtwsta, tx_time);
4646e3ec7017SPing-Ke Shih 	} else {
4647e3ec7017SPing-Ke Shih 		ret = __rtw89_mac_set_tx_time(rtwdev, rtwsta, tx_time);
4648e3ec7017SPing-Ke Shih 		rtwsta->cctl_tx_time = false;
4649e3ec7017SPing-Ke Shih 	}
4650e3ec7017SPing-Ke Shih 
4651e3ec7017SPing-Ke Shih 	return ret;
4652e3ec7017SPing-Ke Shih }
4653e3ec7017SPing-Ke Shih 
4654e3ec7017SPing-Ke Shih int rtw89_mac_get_tx_time(struct rtw89_dev *rtwdev, struct rtw89_sta *rtwsta,
4655e3ec7017SPing-Ke Shih 			  u32 *tx_time)
4656e3ec7017SPing-Ke Shih {
4657e3ec7017SPing-Ke Shih 	u8 mac_idx = rtwsta->rtwvif->mac_idx;
4658e3ec7017SPing-Ke Shih 	u32 reg;
4659e3ec7017SPing-Ke Shih 	int ret = 0;
4660e3ec7017SPing-Ke Shih 
4661e3ec7017SPing-Ke Shih 	if (rtwsta->cctl_tx_time) {
4662e3ec7017SPing-Ke Shih 		*tx_time = (rtwsta->ampdu_max_time + 1) << 9;
4663e3ec7017SPing-Ke Shih 	} else {
4664e3ec7017SPing-Ke Shih 		ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL);
4665e3ec7017SPing-Ke Shih 		if (ret) {
4666e3ec7017SPing-Ke Shih 			rtw89_warn(rtwdev, "failed to check cmac in tx_time\n");
4667e3ec7017SPing-Ke Shih 			return ret;
4668e3ec7017SPing-Ke Shih 		}
4669e3ec7017SPing-Ke Shih 
4670e3ec7017SPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(R_AX_AMPDU_AGG_LIMIT, mac_idx);
4671e3ec7017SPing-Ke Shih 		*tx_time = rtw89_read32_mask(rtwdev, reg, B_AX_AMPDU_MAX_TIME_MASK) << 5;
4672e3ec7017SPing-Ke Shih 	}
4673e3ec7017SPing-Ke Shih 
4674e3ec7017SPing-Ke Shih 	return ret;
4675e3ec7017SPing-Ke Shih }
4676e3ec7017SPing-Ke Shih 
4677e3ec7017SPing-Ke Shih int rtw89_mac_set_tx_retry_limit(struct rtw89_dev *rtwdev,
4678e3ec7017SPing-Ke Shih 				 struct rtw89_sta *rtwsta,
4679e3ec7017SPing-Ke Shih 				 bool resume, u8 tx_retry)
4680e3ec7017SPing-Ke Shih {
4681e3ec7017SPing-Ke Shih 	int ret = 0;
4682e3ec7017SPing-Ke Shih 
4683e3ec7017SPing-Ke Shih 	rtwsta->data_tx_cnt_lmt = tx_retry;
4684e3ec7017SPing-Ke Shih 
4685e3ec7017SPing-Ke Shih 	if (!resume) {
4686e3ec7017SPing-Ke Shih 		rtwsta->cctl_tx_retry_limit = true;
4687e3ec7017SPing-Ke Shih 		ret = rtw89_fw_h2c_txtime_cmac_tbl(rtwdev, rtwsta);
4688e3ec7017SPing-Ke Shih 	} else {
4689e3ec7017SPing-Ke Shih 		ret = rtw89_fw_h2c_txtime_cmac_tbl(rtwdev, rtwsta);
4690e3ec7017SPing-Ke Shih 		rtwsta->cctl_tx_retry_limit = false;
4691e3ec7017SPing-Ke Shih 	}
4692e3ec7017SPing-Ke Shih 
4693e3ec7017SPing-Ke Shih 	return ret;
4694e3ec7017SPing-Ke Shih }
4695e3ec7017SPing-Ke Shih 
4696e3ec7017SPing-Ke Shih int rtw89_mac_get_tx_retry_limit(struct rtw89_dev *rtwdev,
4697e3ec7017SPing-Ke Shih 				 struct rtw89_sta *rtwsta, u8 *tx_retry)
4698e3ec7017SPing-Ke Shih {
4699e3ec7017SPing-Ke Shih 	u8 mac_idx = rtwsta->rtwvif->mac_idx;
4700e3ec7017SPing-Ke Shih 	u32 reg;
4701e3ec7017SPing-Ke Shih 	int ret = 0;
4702e3ec7017SPing-Ke Shih 
4703e3ec7017SPing-Ke Shih 	if (rtwsta->cctl_tx_retry_limit) {
4704e3ec7017SPing-Ke Shih 		*tx_retry = rtwsta->data_tx_cnt_lmt;
4705e3ec7017SPing-Ke Shih 	} else {
4706e3ec7017SPing-Ke Shih 		ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL);
4707e3ec7017SPing-Ke Shih 		if (ret) {
4708e3ec7017SPing-Ke Shih 			rtw89_warn(rtwdev, "failed to check cmac in rty_lmt\n");
4709e3ec7017SPing-Ke Shih 			return ret;
4710e3ec7017SPing-Ke Shih 		}
4711e3ec7017SPing-Ke Shih 
4712e3ec7017SPing-Ke Shih 		reg = rtw89_mac_reg_by_idx(R_AX_TXCNT, mac_idx);
4713e3ec7017SPing-Ke Shih 		*tx_retry = rtw89_read32_mask(rtwdev, reg, B_AX_L_TXCNT_LMT_MASK);
4714e3ec7017SPing-Ke Shih 	}
4715e3ec7017SPing-Ke Shih 
4716e3ec7017SPing-Ke Shih 	return ret;
4717e3ec7017SPing-Ke Shih }
4718e3ec7017SPing-Ke Shih 
4719e3ec7017SPing-Ke Shih int rtw89_mac_set_hw_muedca_ctrl(struct rtw89_dev *rtwdev,
4720e3ec7017SPing-Ke Shih 				 struct rtw89_vif *rtwvif, bool en)
4721e3ec7017SPing-Ke Shih {
4722e3ec7017SPing-Ke Shih 	u8 mac_idx = rtwvif->mac_idx;
4723e3ec7017SPing-Ke Shih 	u16 set = B_AX_MUEDCA_EN_0 | B_AX_SET_MUEDCATIMER_TF_0;
4724e3ec7017SPing-Ke Shih 	u32 reg;
4725e3ec7017SPing-Ke Shih 	u32 ret;
4726e3ec7017SPing-Ke Shih 
4727e3ec7017SPing-Ke Shih 	ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL);
4728e3ec7017SPing-Ke Shih 	if (ret)
4729e3ec7017SPing-Ke Shih 		return ret;
4730e3ec7017SPing-Ke Shih 
4731e3ec7017SPing-Ke Shih 	reg = rtw89_mac_reg_by_idx(R_AX_MUEDCA_EN, mac_idx);
4732e3ec7017SPing-Ke Shih 	if (en)
4733e3ec7017SPing-Ke Shih 		rtw89_write16_set(rtwdev, reg, set);
4734e3ec7017SPing-Ke Shih 	else
4735e3ec7017SPing-Ke Shih 		rtw89_write16_clr(rtwdev, reg, set);
4736e3ec7017SPing-Ke Shih 
4737e3ec7017SPing-Ke Shih 	return 0;
4738e3ec7017SPing-Ke Shih }
47392a7e54dbSPing-Ke Shih 
47402a7e54dbSPing-Ke Shih int rtw89_mac_write_xtal_si(struct rtw89_dev *rtwdev, u8 offset, u8 val, u8 mask)
47412a7e54dbSPing-Ke Shih {
47422a7e54dbSPing-Ke Shih 	u32 val32;
47432a7e54dbSPing-Ke Shih 	int ret;
47442a7e54dbSPing-Ke Shih 
47452a7e54dbSPing-Ke Shih 	val32 = FIELD_PREP(B_AX_WL_XTAL_SI_ADDR_MASK, offset) |
47462a7e54dbSPing-Ke Shih 		FIELD_PREP(B_AX_WL_XTAL_SI_DATA_MASK, val) |
47472a7e54dbSPing-Ke Shih 		FIELD_PREP(B_AX_WL_XTAL_SI_BITMASK_MASK, mask) |
47482a7e54dbSPing-Ke Shih 		FIELD_PREP(B_AX_WL_XTAL_SI_MODE_MASK, XTAL_SI_NORMAL_WRITE) |
47492a7e54dbSPing-Ke Shih 		FIELD_PREP(B_AX_WL_XTAL_SI_CMD_POLL, 1);
47502a7e54dbSPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_WLAN_XTAL_SI_CTRL, val32);
47512a7e54dbSPing-Ke Shih 
47522a7e54dbSPing-Ke Shih 	ret = read_poll_timeout(rtw89_read32, val32, !(val32 & B_AX_WL_XTAL_SI_CMD_POLL),
47532a7e54dbSPing-Ke Shih 				50, 50000, false, rtwdev, R_AX_WLAN_XTAL_SI_CTRL);
47542a7e54dbSPing-Ke Shih 	if (ret) {
47552a7e54dbSPing-Ke Shih 		rtw89_warn(rtwdev, "xtal si not ready(W): offset=%x val=%x mask=%x\n",
47562a7e54dbSPing-Ke Shih 			   offset, val, mask);
47572a7e54dbSPing-Ke Shih 		return ret;
47582a7e54dbSPing-Ke Shih 	}
47592a7e54dbSPing-Ke Shih 
47602a7e54dbSPing-Ke Shih 	return 0;
47612a7e54dbSPing-Ke Shih }
47622a7e54dbSPing-Ke Shih EXPORT_SYMBOL(rtw89_mac_write_xtal_si);
4763bdfbf06cSPing-Ke Shih 
4764bdfbf06cSPing-Ke Shih int rtw89_mac_read_xtal_si(struct rtw89_dev *rtwdev, u8 offset, u8 *val)
4765bdfbf06cSPing-Ke Shih {
4766bdfbf06cSPing-Ke Shih 	u32 val32;
4767bdfbf06cSPing-Ke Shih 	int ret;
4768bdfbf06cSPing-Ke Shih 
4769bdfbf06cSPing-Ke Shih 	val32 = FIELD_PREP(B_AX_WL_XTAL_SI_ADDR_MASK, offset) |
4770bdfbf06cSPing-Ke Shih 		FIELD_PREP(B_AX_WL_XTAL_SI_DATA_MASK, 0x00) |
4771bdfbf06cSPing-Ke Shih 		FIELD_PREP(B_AX_WL_XTAL_SI_BITMASK_MASK, 0x00) |
4772bdfbf06cSPing-Ke Shih 		FIELD_PREP(B_AX_WL_XTAL_SI_MODE_MASK, XTAL_SI_NORMAL_READ) |
4773bdfbf06cSPing-Ke Shih 		FIELD_PREP(B_AX_WL_XTAL_SI_CMD_POLL, 1);
4774bdfbf06cSPing-Ke Shih 	rtw89_write32(rtwdev, R_AX_WLAN_XTAL_SI_CTRL, val32);
4775bdfbf06cSPing-Ke Shih 
4776bdfbf06cSPing-Ke Shih 	ret = read_poll_timeout(rtw89_read32, val32, !(val32 & B_AX_WL_XTAL_SI_CMD_POLL),
4777bdfbf06cSPing-Ke Shih 				50, 50000, false, rtwdev, R_AX_WLAN_XTAL_SI_CTRL);
4778bdfbf06cSPing-Ke Shih 	if (ret) {
4779bdfbf06cSPing-Ke Shih 		rtw89_warn(rtwdev, "xtal si not ready(R): offset=%x\n", offset);
4780bdfbf06cSPing-Ke Shih 		return ret;
4781bdfbf06cSPing-Ke Shih 	}
4782bdfbf06cSPing-Ke Shih 
4783bdfbf06cSPing-Ke Shih 	*val = rtw89_read8(rtwdev, R_AX_WLAN_XTAL_SI_CTRL + 1);
4784bdfbf06cSPing-Ke Shih 
4785bdfbf06cSPing-Ke Shih 	return 0;
4786bdfbf06cSPing-Ke Shih }
47879a785583SZong-Zhe Yang 
47889a785583SZong-Zhe Yang static
47899a785583SZong-Zhe Yang void rtw89_mac_pkt_drop_sta(struct rtw89_dev *rtwdev, struct rtw89_sta *rtwsta)
47909a785583SZong-Zhe Yang {
47919a785583SZong-Zhe Yang 	static const enum rtw89_pkt_drop_sel sels[] = {
47929a785583SZong-Zhe Yang 		RTW89_PKT_DROP_SEL_MACID_BE_ONCE,
47939a785583SZong-Zhe Yang 		RTW89_PKT_DROP_SEL_MACID_BK_ONCE,
47949a785583SZong-Zhe Yang 		RTW89_PKT_DROP_SEL_MACID_VI_ONCE,
47959a785583SZong-Zhe Yang 		RTW89_PKT_DROP_SEL_MACID_VO_ONCE,
47969a785583SZong-Zhe Yang 	};
47979a785583SZong-Zhe Yang 	struct rtw89_vif *rtwvif = rtwsta->rtwvif;
47989a785583SZong-Zhe Yang 	struct rtw89_pkt_drop_params params = {0};
47999a785583SZong-Zhe Yang 	int i;
48009a785583SZong-Zhe Yang 
48019a785583SZong-Zhe Yang 	params.mac_band = RTW89_MAC_0;
48029a785583SZong-Zhe Yang 	params.macid = rtwsta->mac_id;
48039a785583SZong-Zhe Yang 	params.port = rtwvif->port;
48049a785583SZong-Zhe Yang 	params.mbssid = 0;
48059a785583SZong-Zhe Yang 	params.tf_trs = rtwvif->trigger;
48069a785583SZong-Zhe Yang 
48079a785583SZong-Zhe Yang 	for (i = 0; i < ARRAY_SIZE(sels); i++) {
48089a785583SZong-Zhe Yang 		params.sel = sels[i];
48099a785583SZong-Zhe Yang 		rtw89_fw_h2c_pkt_drop(rtwdev, &params);
48109a785583SZong-Zhe Yang 	}
48119a785583SZong-Zhe Yang }
48129a785583SZong-Zhe Yang 
48139a785583SZong-Zhe Yang static void rtw89_mac_pkt_drop_vif_iter(void *data, struct ieee80211_sta *sta)
48149a785583SZong-Zhe Yang {
48159a785583SZong-Zhe Yang 	struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv;
48169a785583SZong-Zhe Yang 	struct rtw89_vif *rtwvif = rtwsta->rtwvif;
48179a785583SZong-Zhe Yang 	struct rtw89_dev *rtwdev = rtwvif->rtwdev;
48189a785583SZong-Zhe Yang 	struct rtw89_vif *target = data;
48199a785583SZong-Zhe Yang 
48209a785583SZong-Zhe Yang 	if (rtwvif != target)
48219a785583SZong-Zhe Yang 		return;
48229a785583SZong-Zhe Yang 
48239a785583SZong-Zhe Yang 	rtw89_mac_pkt_drop_sta(rtwdev, rtwsta);
48249a785583SZong-Zhe Yang }
48259a785583SZong-Zhe Yang 
48269a785583SZong-Zhe Yang void rtw89_mac_pkt_drop_vif(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
48279a785583SZong-Zhe Yang {
48289a785583SZong-Zhe Yang 	ieee80211_iterate_stations_atomic(rtwdev->hw,
48299a785583SZong-Zhe Yang 					  rtw89_mac_pkt_drop_vif_iter,
48309a785583SZong-Zhe Yang 					  rtwvif);
48319a785583SZong-Zhe Yang }
4832