1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* Copyright(c) 2019-2020 Realtek Corporation 3 */ 4 5 #include "cam.h" 6 #include "chan.h" 7 #include "debug.h" 8 #include "fw.h" 9 #include "mac.h" 10 #include "pci.h" 11 #include "ps.h" 12 #include "reg.h" 13 #include "util.h" 14 15 const u32 rtw89_mac_mem_base_addrs[RTW89_MAC_MEM_NUM] = { 16 [RTW89_MAC_MEM_AXIDMA] = AXIDMA_BASE_ADDR, 17 [RTW89_MAC_MEM_SHARED_BUF] = SHARED_BUF_BASE_ADDR, 18 [RTW89_MAC_MEM_DMAC_TBL] = DMAC_TBL_BASE_ADDR, 19 [RTW89_MAC_MEM_SHCUT_MACHDR] = SHCUT_MACHDR_BASE_ADDR, 20 [RTW89_MAC_MEM_STA_SCHED] = STA_SCHED_BASE_ADDR, 21 [RTW89_MAC_MEM_RXPLD_FLTR_CAM] = RXPLD_FLTR_CAM_BASE_ADDR, 22 [RTW89_MAC_MEM_SECURITY_CAM] = SECURITY_CAM_BASE_ADDR, 23 [RTW89_MAC_MEM_WOW_CAM] = WOW_CAM_BASE_ADDR, 24 [RTW89_MAC_MEM_CMAC_TBL] = CMAC_TBL_BASE_ADDR, 25 [RTW89_MAC_MEM_ADDR_CAM] = ADDR_CAM_BASE_ADDR, 26 [RTW89_MAC_MEM_BA_CAM] = BA_CAM_BASE_ADDR, 27 [RTW89_MAC_MEM_BCN_IE_CAM0] = BCN_IE_CAM0_BASE_ADDR, 28 [RTW89_MAC_MEM_BCN_IE_CAM1] = BCN_IE_CAM1_BASE_ADDR, 29 [RTW89_MAC_MEM_TXD_FIFO_0] = TXD_FIFO_0_BASE_ADDR, 30 [RTW89_MAC_MEM_TXD_FIFO_1] = TXD_FIFO_1_BASE_ADDR, 31 [RTW89_MAC_MEM_TXDATA_FIFO_0] = TXDATA_FIFO_0_BASE_ADDR, 32 [RTW89_MAC_MEM_TXDATA_FIFO_1] = TXDATA_FIFO_1_BASE_ADDR, 33 [RTW89_MAC_MEM_CPU_LOCAL] = CPU_LOCAL_BASE_ADDR, 34 [RTW89_MAC_MEM_BSSID_CAM] = BSSID_CAM_BASE_ADDR, 35 [RTW89_MAC_MEM_TXD_FIFO_0_V1] = TXD_FIFO_0_BASE_ADDR_V1, 36 [RTW89_MAC_MEM_TXD_FIFO_1_V1] = TXD_FIFO_1_BASE_ADDR_V1, 37 }; 38 39 static void rtw89_mac_mem_write(struct rtw89_dev *rtwdev, u32 offset, 40 u32 val, enum rtw89_mac_mem_sel sel) 41 { 42 u32 addr = rtw89_mac_mem_base_addrs[sel] + offset; 43 44 rtw89_write32(rtwdev, R_AX_FILTER_MODEL_ADDR, addr); 45 rtw89_write32(rtwdev, R_AX_INDIR_ACCESS_ENTRY, val); 46 } 47 48 static u32 rtw89_mac_mem_read(struct rtw89_dev *rtwdev, u32 offset, 49 enum rtw89_mac_mem_sel sel) 50 { 51 u32 addr = rtw89_mac_mem_base_addrs[sel] + offset; 52 53 rtw89_write32(rtwdev, R_AX_FILTER_MODEL_ADDR, addr); 54 return rtw89_read32(rtwdev, R_AX_INDIR_ACCESS_ENTRY); 55 } 56 57 int rtw89_mac_check_mac_en(struct rtw89_dev *rtwdev, u8 mac_idx, 58 enum rtw89_mac_hwmod_sel sel) 59 { 60 u32 val, r_val; 61 62 if (sel == RTW89_DMAC_SEL) { 63 r_val = rtw89_read32(rtwdev, R_AX_DMAC_FUNC_EN); 64 val = (B_AX_MAC_FUNC_EN | B_AX_DMAC_FUNC_EN); 65 } else if (sel == RTW89_CMAC_SEL && mac_idx == 0) { 66 r_val = rtw89_read32(rtwdev, R_AX_CMAC_FUNC_EN); 67 val = B_AX_CMAC_EN; 68 } else if (sel == RTW89_CMAC_SEL && mac_idx == 1) { 69 r_val = rtw89_read32(rtwdev, R_AX_SYS_ISO_CTRL_EXTEND); 70 val = B_AX_CMAC1_FEN; 71 } else { 72 return -EINVAL; 73 } 74 if (r_val == RTW89_R32_EA || r_val == RTW89_R32_DEAD || 75 (val & r_val) != val) 76 return -EFAULT; 77 78 return 0; 79 } 80 81 int rtw89_mac_write_lte(struct rtw89_dev *rtwdev, const u32 offset, u32 val) 82 { 83 u8 lte_ctrl; 84 int ret; 85 86 ret = read_poll_timeout(rtw89_read8, lte_ctrl, (lte_ctrl & BIT(5)) != 0, 87 50, 50000, false, rtwdev, R_AX_LTE_CTRL + 3); 88 if (ret) 89 rtw89_err(rtwdev, "[ERR]lte not ready(W)\n"); 90 91 rtw89_write32(rtwdev, R_AX_LTE_WDATA, val); 92 rtw89_write32(rtwdev, R_AX_LTE_CTRL, 0xC00F0000 | offset); 93 94 return ret; 95 } 96 97 int rtw89_mac_read_lte(struct rtw89_dev *rtwdev, const u32 offset, u32 *val) 98 { 99 u8 lte_ctrl; 100 int ret; 101 102 ret = read_poll_timeout(rtw89_read8, lte_ctrl, (lte_ctrl & BIT(5)) != 0, 103 50, 50000, false, rtwdev, R_AX_LTE_CTRL + 3); 104 if (ret) 105 rtw89_err(rtwdev, "[ERR]lte not ready(W)\n"); 106 107 rtw89_write32(rtwdev, R_AX_LTE_CTRL, 0x800F0000 | offset); 108 *val = rtw89_read32(rtwdev, R_AX_LTE_RDATA); 109 110 return ret; 111 } 112 113 static 114 int dle_dfi_ctrl(struct rtw89_dev *rtwdev, struct rtw89_mac_dle_dfi_ctrl *ctrl) 115 { 116 u32 ctrl_reg, data_reg, ctrl_data; 117 u32 val; 118 int ret; 119 120 switch (ctrl->type) { 121 case DLE_CTRL_TYPE_WDE: 122 ctrl_reg = R_AX_WDE_DBG_FUN_INTF_CTL; 123 data_reg = R_AX_WDE_DBG_FUN_INTF_DATA; 124 ctrl_data = FIELD_PREP(B_AX_WDE_DFI_TRGSEL_MASK, ctrl->target) | 125 FIELD_PREP(B_AX_WDE_DFI_ADDR_MASK, ctrl->addr) | 126 B_AX_WDE_DFI_ACTIVE; 127 break; 128 case DLE_CTRL_TYPE_PLE: 129 ctrl_reg = R_AX_PLE_DBG_FUN_INTF_CTL; 130 data_reg = R_AX_PLE_DBG_FUN_INTF_DATA; 131 ctrl_data = FIELD_PREP(B_AX_PLE_DFI_TRGSEL_MASK, ctrl->target) | 132 FIELD_PREP(B_AX_PLE_DFI_ADDR_MASK, ctrl->addr) | 133 B_AX_PLE_DFI_ACTIVE; 134 break; 135 default: 136 rtw89_warn(rtwdev, "[ERR] dfi ctrl type %d\n", ctrl->type); 137 return -EINVAL; 138 } 139 140 rtw89_write32(rtwdev, ctrl_reg, ctrl_data); 141 142 ret = read_poll_timeout_atomic(rtw89_read32, val, !(val & B_AX_WDE_DFI_ACTIVE), 143 1, 1000, false, rtwdev, ctrl_reg); 144 if (ret) { 145 rtw89_warn(rtwdev, "[ERR] dle dfi ctrl 0x%X set 0x%X timeout\n", 146 ctrl_reg, ctrl_data); 147 return ret; 148 } 149 150 ctrl->out_data = rtw89_read32(rtwdev, data_reg); 151 return 0; 152 } 153 154 static int dle_dfi_quota(struct rtw89_dev *rtwdev, 155 struct rtw89_mac_dle_dfi_quota *quota) 156 { 157 struct rtw89_mac_dle_dfi_ctrl ctrl; 158 int ret; 159 160 ctrl.type = quota->dle_type; 161 ctrl.target = DLE_DFI_TYPE_QUOTA; 162 ctrl.addr = quota->qtaid; 163 ret = dle_dfi_ctrl(rtwdev, &ctrl); 164 if (ret) { 165 rtw89_warn(rtwdev, "[ERR]dle_dfi_ctrl %d\n", ret); 166 return ret; 167 } 168 169 quota->rsv_pgnum = FIELD_GET(B_AX_DLE_RSV_PGNUM, ctrl.out_data); 170 quota->use_pgnum = FIELD_GET(B_AX_DLE_USE_PGNUM, ctrl.out_data); 171 return 0; 172 } 173 174 static int dle_dfi_qempty(struct rtw89_dev *rtwdev, 175 struct rtw89_mac_dle_dfi_qempty *qempty) 176 { 177 struct rtw89_mac_dle_dfi_ctrl ctrl; 178 u32 ret; 179 180 ctrl.type = qempty->dle_type; 181 ctrl.target = DLE_DFI_TYPE_QEMPTY; 182 ctrl.addr = qempty->grpsel; 183 ret = dle_dfi_ctrl(rtwdev, &ctrl); 184 if (ret) { 185 rtw89_warn(rtwdev, "[ERR]dle_dfi_ctrl %d\n", ret); 186 return ret; 187 } 188 189 qempty->qempty = FIELD_GET(B_AX_DLE_QEMPTY_GRP, ctrl.out_data); 190 return 0; 191 } 192 193 static void dump_err_status_dispatcher(struct rtw89_dev *rtwdev) 194 { 195 rtw89_info(rtwdev, "R_AX_HOST_DISPATCHER_ALWAYS_IMR=0x%08x ", 196 rtw89_read32(rtwdev, R_AX_HOST_DISPATCHER_ERR_IMR)); 197 rtw89_info(rtwdev, "R_AX_HOST_DISPATCHER_ALWAYS_ISR=0x%08x\n", 198 rtw89_read32(rtwdev, R_AX_HOST_DISPATCHER_ERR_ISR)); 199 rtw89_info(rtwdev, "R_AX_CPU_DISPATCHER_ALWAYS_IMR=0x%08x ", 200 rtw89_read32(rtwdev, R_AX_CPU_DISPATCHER_ERR_IMR)); 201 rtw89_info(rtwdev, "R_AX_CPU_DISPATCHER_ALWAYS_ISR=0x%08x\n", 202 rtw89_read32(rtwdev, R_AX_CPU_DISPATCHER_ERR_ISR)); 203 rtw89_info(rtwdev, "R_AX_OTHER_DISPATCHER_ALWAYS_IMR=0x%08x ", 204 rtw89_read32(rtwdev, R_AX_OTHER_DISPATCHER_ERR_IMR)); 205 rtw89_info(rtwdev, "R_AX_OTHER_DISPATCHER_ALWAYS_ISR=0x%08x\n", 206 rtw89_read32(rtwdev, R_AX_OTHER_DISPATCHER_ERR_ISR)); 207 } 208 209 static void rtw89_mac_dump_qta_lost(struct rtw89_dev *rtwdev) 210 { 211 struct rtw89_mac_dle_dfi_qempty qempty; 212 struct rtw89_mac_dle_dfi_quota quota; 213 struct rtw89_mac_dle_dfi_ctrl ctrl; 214 u32 val, not_empty, i; 215 int ret; 216 217 qempty.dle_type = DLE_CTRL_TYPE_PLE; 218 qempty.grpsel = 0; 219 qempty.qempty = ~(u32)0; 220 ret = dle_dfi_qempty(rtwdev, &qempty); 221 if (ret) 222 rtw89_warn(rtwdev, "%s: query DLE fail\n", __func__); 223 else 224 rtw89_info(rtwdev, "DLE group0 empty: 0x%x\n", qempty.qempty); 225 226 for (not_empty = ~qempty.qempty, i = 0; not_empty != 0; not_empty >>= 1, i++) { 227 if (!(not_empty & BIT(0))) 228 continue; 229 ctrl.type = DLE_CTRL_TYPE_PLE; 230 ctrl.target = DLE_DFI_TYPE_QLNKTBL; 231 ctrl.addr = (QLNKTBL_ADDR_INFO_SEL_0 ? QLNKTBL_ADDR_INFO_SEL : 0) | 232 FIELD_PREP(QLNKTBL_ADDR_TBL_IDX_MASK, i); 233 ret = dle_dfi_ctrl(rtwdev, &ctrl); 234 if (ret) 235 rtw89_warn(rtwdev, "%s: query DLE fail\n", __func__); 236 else 237 rtw89_info(rtwdev, "qidx%d pktcnt = %ld\n", i, 238 FIELD_GET(QLNKTBL_DATA_SEL1_PKT_CNT_MASK, 239 ctrl.out_data)); 240 } 241 242 quota.dle_type = DLE_CTRL_TYPE_PLE; 243 quota.qtaid = 6; 244 ret = dle_dfi_quota(rtwdev, "a); 245 if (ret) 246 rtw89_warn(rtwdev, "%s: query DLE fail\n", __func__); 247 else 248 rtw89_info(rtwdev, "quota6 rsv/use: 0x%x/0x%x\n", 249 quota.rsv_pgnum, quota.use_pgnum); 250 251 val = rtw89_read32(rtwdev, R_AX_PLE_QTA6_CFG); 252 rtw89_info(rtwdev, "[PLE][CMAC0_RX]min_pgnum=0x%lx\n", 253 FIELD_GET(B_AX_PLE_Q6_MIN_SIZE_MASK, val)); 254 rtw89_info(rtwdev, "[PLE][CMAC0_RX]max_pgnum=0x%lx\n", 255 FIELD_GET(B_AX_PLE_Q6_MAX_SIZE_MASK, val)); 256 257 dump_err_status_dispatcher(rtwdev); 258 } 259 260 static void rtw89_mac_dump_l0_to_l1(struct rtw89_dev *rtwdev, 261 enum mac_ax_err_info err) 262 { 263 u32 dbg, event; 264 265 dbg = rtw89_read32(rtwdev, R_AX_SER_DBG_INFO); 266 event = FIELD_GET(B_AX_L0_TO_L1_EVENT_MASK, dbg); 267 268 switch (event) { 269 case MAC_AX_L0_TO_L1_RX_QTA_LOST: 270 rtw89_info(rtwdev, "quota lost!\n"); 271 rtw89_mac_dump_qta_lost(rtwdev); 272 break; 273 default: 274 break; 275 } 276 } 277 278 static void rtw89_mac_dump_dmac_err_status(struct rtw89_dev *rtwdev) 279 { 280 const struct rtw89_chip_info *chip = rtwdev->chip; 281 u32 dmac_err; 282 int i, ret; 283 284 ret = rtw89_mac_check_mac_en(rtwdev, 0, RTW89_DMAC_SEL); 285 if (ret) { 286 rtw89_warn(rtwdev, "[DMAC] : DMAC not enabled\n"); 287 return; 288 } 289 290 dmac_err = rtw89_read32(rtwdev, R_AX_DMAC_ERR_ISR); 291 rtw89_info(rtwdev, "R_AX_DMAC_ERR_ISR=0x%08x\n", dmac_err); 292 rtw89_info(rtwdev, "R_AX_DMAC_ERR_IMR=0x%08x\n", 293 rtw89_read32(rtwdev, R_AX_DMAC_ERR_IMR)); 294 295 if (dmac_err) { 296 rtw89_info(rtwdev, "R_AX_WDE_ERR_FLAG_CFG=0x%08x\n", 297 rtw89_read32(rtwdev, R_AX_WDE_ERR_FLAG_CFG_NUM1)); 298 rtw89_info(rtwdev, "R_AX_PLE_ERR_FLAG_CFG=0x%08x\n", 299 rtw89_read32(rtwdev, R_AX_PLE_ERR_FLAG_CFG_NUM1)); 300 if (chip->chip_id == RTL8852C) { 301 rtw89_info(rtwdev, "R_AX_PLE_ERRFLAG_MSG=0x%08x\n", 302 rtw89_read32(rtwdev, R_AX_PLE_ERRFLAG_MSG)); 303 rtw89_info(rtwdev, "R_AX_WDE_ERRFLAG_MSG=0x%08x\n", 304 rtw89_read32(rtwdev, R_AX_WDE_ERRFLAG_MSG)); 305 rtw89_info(rtwdev, "R_AX_PLE_DBGERR_LOCKEN=0x%08x\n", 306 rtw89_read32(rtwdev, R_AX_PLE_DBGERR_LOCKEN)); 307 rtw89_info(rtwdev, "R_AX_PLE_DBGERR_STS=0x%08x\n", 308 rtw89_read32(rtwdev, R_AX_PLE_DBGERR_STS)); 309 } 310 } 311 312 if (dmac_err & B_AX_WDRLS_ERR_FLAG) { 313 rtw89_info(rtwdev, "R_AX_WDRLS_ERR_IMR=0x%08x\n", 314 rtw89_read32(rtwdev, R_AX_WDRLS_ERR_IMR)); 315 rtw89_info(rtwdev, "R_AX_WDRLS_ERR_ISR=0x%08x\n", 316 rtw89_read32(rtwdev, R_AX_WDRLS_ERR_ISR)); 317 if (chip->chip_id == RTL8852C) 318 rtw89_info(rtwdev, "R_AX_RPQ_RXBD_IDX=0x%08x\n", 319 rtw89_read32(rtwdev, R_AX_RPQ_RXBD_IDX_V1)); 320 else 321 rtw89_info(rtwdev, "R_AX_RPQ_RXBD_IDX=0x%08x\n", 322 rtw89_read32(rtwdev, R_AX_RPQ_RXBD_IDX)); 323 } 324 325 if (dmac_err & B_AX_WSEC_ERR_FLAG) { 326 if (chip->chip_id == RTL8852C) { 327 rtw89_info(rtwdev, "R_AX_SEC_ERR_IMR=0x%08x\n", 328 rtw89_read32(rtwdev, R_AX_SEC_ERROR_FLAG_IMR)); 329 rtw89_info(rtwdev, "R_AX_SEC_ERR_ISR=0x%08x\n", 330 rtw89_read32(rtwdev, R_AX_SEC_ERROR_FLAG)); 331 rtw89_info(rtwdev, "R_AX_SEC_ENG_CTRL=0x%08x\n", 332 rtw89_read32(rtwdev, R_AX_SEC_ENG_CTRL)); 333 rtw89_info(rtwdev, "R_AX_SEC_MPDU_PROC=0x%08x\n", 334 rtw89_read32(rtwdev, R_AX_SEC_MPDU_PROC)); 335 rtw89_info(rtwdev, "R_AX_SEC_CAM_ACCESS=0x%08x\n", 336 rtw89_read32(rtwdev, R_AX_SEC_CAM_ACCESS)); 337 rtw89_info(rtwdev, "R_AX_SEC_CAM_RDATA=0x%08x\n", 338 rtw89_read32(rtwdev, R_AX_SEC_CAM_RDATA)); 339 rtw89_info(rtwdev, "R_AX_SEC_DEBUG1=0x%08x\n", 340 rtw89_read32(rtwdev, R_AX_SEC_DEBUG1)); 341 rtw89_info(rtwdev, "R_AX_SEC_TX_DEBUG=0x%08x\n", 342 rtw89_read32(rtwdev, R_AX_SEC_TX_DEBUG)); 343 rtw89_info(rtwdev, "R_AX_SEC_RX_DEBUG=0x%08x\n", 344 rtw89_read32(rtwdev, R_AX_SEC_RX_DEBUG)); 345 346 rtw89_write32_mask(rtwdev, R_AX_DBG_CTRL, 347 B_AX_DBG_SEL0, 0x8B); 348 rtw89_write32_mask(rtwdev, R_AX_DBG_CTRL, 349 B_AX_DBG_SEL1, 0x8B); 350 rtw89_write32_mask(rtwdev, R_AX_SYS_STATUS1, 351 B_AX_SEL_0XC0_MASK, 1); 352 for (i = 0; i < 0x10; i++) { 353 rtw89_write32_mask(rtwdev, R_AX_SEC_ENG_CTRL, 354 B_AX_SEC_DBG_PORT_FIELD_MASK, i); 355 rtw89_info(rtwdev, "sel=%x,R_AX_SEC_DEBUG2=0x%08x\n", 356 i, rtw89_read32(rtwdev, R_AX_SEC_DEBUG2)); 357 } 358 } else { 359 rtw89_info(rtwdev, "R_AX_SEC_ERR_IMR_ISR=0x%08x\n", 360 rtw89_read32(rtwdev, R_AX_SEC_DEBUG)); 361 rtw89_info(rtwdev, "R_AX_SEC_ENG_CTRL=0x%08x\n", 362 rtw89_read32(rtwdev, R_AX_SEC_ENG_CTRL)); 363 rtw89_info(rtwdev, "R_AX_SEC_MPDU_PROC=0x%08x\n", 364 rtw89_read32(rtwdev, R_AX_SEC_MPDU_PROC)); 365 rtw89_info(rtwdev, "R_AX_SEC_CAM_ACCESS=0x%08x\n", 366 rtw89_read32(rtwdev, R_AX_SEC_CAM_ACCESS)); 367 rtw89_info(rtwdev, "R_AX_SEC_CAM_RDATA=0x%08x\n", 368 rtw89_read32(rtwdev, R_AX_SEC_CAM_RDATA)); 369 rtw89_info(rtwdev, "R_AX_SEC_CAM_WDATA=0x%08x\n", 370 rtw89_read32(rtwdev, R_AX_SEC_CAM_WDATA)); 371 rtw89_info(rtwdev, "R_AX_SEC_TX_DEBUG=0x%08x\n", 372 rtw89_read32(rtwdev, R_AX_SEC_TX_DEBUG)); 373 rtw89_info(rtwdev, "R_AX_SEC_RX_DEBUG=0x%08x\n", 374 rtw89_read32(rtwdev, R_AX_SEC_RX_DEBUG)); 375 rtw89_info(rtwdev, "R_AX_SEC_TRX_PKT_CNT=0x%08x\n", 376 rtw89_read32(rtwdev, R_AX_SEC_TRX_PKT_CNT)); 377 rtw89_info(rtwdev, "R_AX_SEC_TRX_BLK_CNT=0x%08x\n", 378 rtw89_read32(rtwdev, R_AX_SEC_TRX_BLK_CNT)); 379 } 380 } 381 382 if (dmac_err & B_AX_MPDU_ERR_FLAG) { 383 rtw89_info(rtwdev, "R_AX_MPDU_TX_ERR_IMR=0x%08x\n", 384 rtw89_read32(rtwdev, R_AX_MPDU_TX_ERR_IMR)); 385 rtw89_info(rtwdev, "R_AX_MPDU_TX_ERR_ISR=0x%08x\n", 386 rtw89_read32(rtwdev, R_AX_MPDU_TX_ERR_ISR)); 387 rtw89_info(rtwdev, "R_AX_MPDU_RX_ERR_IMR=0x%08x\n", 388 rtw89_read32(rtwdev, R_AX_MPDU_RX_ERR_IMR)); 389 rtw89_info(rtwdev, "R_AX_MPDU_RX_ERR_ISR=0x%08x\n", 390 rtw89_read32(rtwdev, R_AX_MPDU_RX_ERR_ISR)); 391 } 392 393 if (dmac_err & B_AX_STA_SCHEDULER_ERR_FLAG) { 394 rtw89_info(rtwdev, "R_AX_STA_SCHEDULER_ERR_IMR=0x%08x\n", 395 rtw89_read32(rtwdev, R_AX_STA_SCHEDULER_ERR_IMR)); 396 rtw89_info(rtwdev, "R_AX_STA_SCHEDULER_ERR_ISR=0x%08x\n", 397 rtw89_read32(rtwdev, R_AX_STA_SCHEDULER_ERR_ISR)); 398 } 399 400 if (dmac_err & B_AX_WDE_DLE_ERR_FLAG) { 401 rtw89_info(rtwdev, "R_AX_WDE_ERR_IMR=0x%08x\n", 402 rtw89_read32(rtwdev, R_AX_WDE_ERR_IMR)); 403 rtw89_info(rtwdev, "R_AX_WDE_ERR_ISR=0x%08x\n", 404 rtw89_read32(rtwdev, R_AX_WDE_ERR_ISR)); 405 rtw89_info(rtwdev, "R_AX_PLE_ERR_IMR=0x%08x\n", 406 rtw89_read32(rtwdev, R_AX_PLE_ERR_IMR)); 407 rtw89_info(rtwdev, "R_AX_PLE_ERR_FLAG_ISR=0x%08x\n", 408 rtw89_read32(rtwdev, R_AX_PLE_ERR_FLAG_ISR)); 409 } 410 411 if (dmac_err & B_AX_TXPKTCTRL_ERR_FLAG) { 412 if (chip->chip_id == RTL8852C) { 413 rtw89_info(rtwdev, "R_AX_TXPKTCTL_B0_ERRFLAG_IMR=0x%08x\n", 414 rtw89_read32(rtwdev, R_AX_TXPKTCTL_B0_ERRFLAG_IMR)); 415 rtw89_info(rtwdev, "R_AX_TXPKTCTL_B0_ERRFLAG_ISR=0x%08x\n", 416 rtw89_read32(rtwdev, R_AX_TXPKTCTL_B0_ERRFLAG_ISR)); 417 rtw89_info(rtwdev, "R_AX_TXPKTCTL_B1_ERRFLAG_IMR=0x%08x\n", 418 rtw89_read32(rtwdev, R_AX_TXPKTCTL_B1_ERRFLAG_IMR)); 419 rtw89_info(rtwdev, "R_AX_TXPKTCTL_B1_ERRFLAG_ISR=0x%08x\n", 420 rtw89_read32(rtwdev, R_AX_TXPKTCTL_B1_ERRFLAG_ISR)); 421 } else { 422 rtw89_info(rtwdev, "R_AX_TXPKTCTL_ERR_IMR_ISR=0x%08x\n", 423 rtw89_read32(rtwdev, R_AX_TXPKTCTL_ERR_IMR_ISR)); 424 rtw89_info(rtwdev, "R_AX_TXPKTCTL_ERR_IMR_ISR_B1=0x%08x\n", 425 rtw89_read32(rtwdev, R_AX_TXPKTCTL_ERR_IMR_ISR_B1)); 426 } 427 } 428 429 if (dmac_err & B_AX_PLE_DLE_ERR_FLAG) { 430 rtw89_info(rtwdev, "R_AX_WDE_ERR_IMR=0x%08x\n", 431 rtw89_read32(rtwdev, R_AX_WDE_ERR_IMR)); 432 rtw89_info(rtwdev, "R_AX_WDE_ERR_ISR=0x%08x\n", 433 rtw89_read32(rtwdev, R_AX_WDE_ERR_ISR)); 434 rtw89_info(rtwdev, "R_AX_PLE_ERR_IMR=0x%08x\n", 435 rtw89_read32(rtwdev, R_AX_PLE_ERR_IMR)); 436 rtw89_info(rtwdev, "R_AX_PLE_ERR_FLAG_ISR=0x%08x\n", 437 rtw89_read32(rtwdev, R_AX_PLE_ERR_FLAG_ISR)); 438 rtw89_info(rtwdev, "R_AX_WD_CPUQ_OP_0=0x%08x\n", 439 rtw89_read32(rtwdev, R_AX_WD_CPUQ_OP_0)); 440 rtw89_info(rtwdev, "R_AX_WD_CPUQ_OP_1=0x%08x\n", 441 rtw89_read32(rtwdev, R_AX_WD_CPUQ_OP_1)); 442 rtw89_info(rtwdev, "R_AX_WD_CPUQ_OP_2=0x%08x\n", 443 rtw89_read32(rtwdev, R_AX_WD_CPUQ_OP_2)); 444 rtw89_info(rtwdev, "R_AX_WD_CPUQ_OP_STATUS=0x%08x\n", 445 rtw89_read32(rtwdev, R_AX_WD_CPUQ_OP_STATUS)); 446 rtw89_info(rtwdev, "R_AX_PL_CPUQ_OP_0=0x%08x\n", 447 rtw89_read32(rtwdev, R_AX_PL_CPUQ_OP_0)); 448 rtw89_info(rtwdev, "R_AX_PL_CPUQ_OP_1=0x%08x\n", 449 rtw89_read32(rtwdev, R_AX_PL_CPUQ_OP_1)); 450 rtw89_info(rtwdev, "R_AX_PL_CPUQ_OP_2=0x%08x\n", 451 rtw89_read32(rtwdev, R_AX_PL_CPUQ_OP_2)); 452 rtw89_info(rtwdev, "R_AX_PL_CPUQ_OP_STATUS=0x%08x\n", 453 rtw89_read32(rtwdev, R_AX_PL_CPUQ_OP_STATUS)); 454 if (chip->chip_id == RTL8852C) { 455 rtw89_info(rtwdev, "R_AX_RX_CTRL0=0x%08x\n", 456 rtw89_read32(rtwdev, R_AX_RX_CTRL0)); 457 rtw89_info(rtwdev, "R_AX_RX_CTRL1=0x%08x\n", 458 rtw89_read32(rtwdev, R_AX_RX_CTRL1)); 459 rtw89_info(rtwdev, "R_AX_RX_CTRL2=0x%08x\n", 460 rtw89_read32(rtwdev, R_AX_RX_CTRL2)); 461 } else { 462 rtw89_info(rtwdev, "R_AX_RXDMA_PKT_INFO_0=0x%08x\n", 463 rtw89_read32(rtwdev, R_AX_RXDMA_PKT_INFO_0)); 464 rtw89_info(rtwdev, "R_AX_RXDMA_PKT_INFO_1=0x%08x\n", 465 rtw89_read32(rtwdev, R_AX_RXDMA_PKT_INFO_1)); 466 rtw89_info(rtwdev, "R_AX_RXDMA_PKT_INFO_2=0x%08x\n", 467 rtw89_read32(rtwdev, R_AX_RXDMA_PKT_INFO_2)); 468 } 469 } 470 471 if (dmac_err & B_AX_PKTIN_ERR_FLAG) { 472 rtw89_info(rtwdev, "R_AX_PKTIN_ERR_IMR=0x%08x\n", 473 rtw89_read32(rtwdev, R_AX_PKTIN_ERR_IMR)); 474 rtw89_info(rtwdev, "R_AX_PKTIN_ERR_ISR=0x%08x\n", 475 rtw89_read32(rtwdev, R_AX_PKTIN_ERR_ISR)); 476 } 477 478 if (dmac_err & B_AX_DISPATCH_ERR_FLAG) { 479 rtw89_info(rtwdev, "R_AX_HOST_DISPATCHER_ERR_IMR=0x%08x\n", 480 rtw89_read32(rtwdev, R_AX_HOST_DISPATCHER_ERR_IMR)); 481 rtw89_info(rtwdev, "R_AX_HOST_DISPATCHER_ERR_ISR=0x%08x\n", 482 rtw89_read32(rtwdev, R_AX_HOST_DISPATCHER_ERR_ISR)); 483 rtw89_info(rtwdev, "R_AX_CPU_DISPATCHER_ERR_IMR=0x%08x\n", 484 rtw89_read32(rtwdev, R_AX_CPU_DISPATCHER_ERR_IMR)); 485 rtw89_info(rtwdev, "R_AX_CPU_DISPATCHER_ERR_ISR=0x%08x\n", 486 rtw89_read32(rtwdev, R_AX_CPU_DISPATCHER_ERR_ISR)); 487 rtw89_info(rtwdev, "R_AX_OTHER_DISPATCHER_ERR_IMR=0x%08x\n", 488 rtw89_read32(rtwdev, R_AX_OTHER_DISPATCHER_ERR_IMR)); 489 rtw89_info(rtwdev, "R_AX_OTHER_DISPATCHER_ERR_ISR=0x%08x\n", 490 rtw89_read32(rtwdev, R_AX_OTHER_DISPATCHER_ERR_ISR)); 491 } 492 493 if (dmac_err & B_AX_BBRPT_ERR_FLAG) { 494 if (chip->chip_id == RTL8852C) { 495 rtw89_info(rtwdev, "R_AX_BBRPT_COM_ERR_IMR=0x%08x\n", 496 rtw89_read32(rtwdev, R_AX_BBRPT_COM_ERR_IMR)); 497 rtw89_info(rtwdev, "R_AX_BBRPT_COM_ERR_ISR=0x%08x\n", 498 rtw89_read32(rtwdev, R_AX_BBRPT_COM_ERR_ISR)); 499 rtw89_info(rtwdev, "R_AX_BBRPT_CHINFO_ERR_ISR=0x%08x\n", 500 rtw89_read32(rtwdev, R_AX_BBRPT_CHINFO_ERR_ISR)); 501 rtw89_info(rtwdev, "R_AX_BBRPT_CHINFO_ERR_IMR=0x%08x\n", 502 rtw89_read32(rtwdev, R_AX_BBRPT_CHINFO_ERR_IMR)); 503 rtw89_info(rtwdev, "R_AX_BBRPT_DFS_ERR_IMR=0x%08x\n", 504 rtw89_read32(rtwdev, R_AX_BBRPT_DFS_ERR_IMR)); 505 rtw89_info(rtwdev, "R_AX_BBRPT_DFS_ERR_ISR=0x%08x\n", 506 rtw89_read32(rtwdev, R_AX_BBRPT_DFS_ERR_ISR)); 507 } else { 508 rtw89_info(rtwdev, "R_AX_BBRPT_COM_ERR_IMR_ISR=0x%08x\n", 509 rtw89_read32(rtwdev, R_AX_BBRPT_COM_ERR_IMR_ISR)); 510 rtw89_info(rtwdev, "R_AX_BBRPT_CHINFO_ERR_ISR=0x%08x\n", 511 rtw89_read32(rtwdev, R_AX_BBRPT_CHINFO_ERR_ISR)); 512 rtw89_info(rtwdev, "R_AX_BBRPT_CHINFO_ERR_IMR=0x%08x\n", 513 rtw89_read32(rtwdev, R_AX_BBRPT_CHINFO_ERR_IMR)); 514 rtw89_info(rtwdev, "R_AX_BBRPT_DFS_ERR_IMR=0x%08x\n", 515 rtw89_read32(rtwdev, R_AX_BBRPT_DFS_ERR_IMR)); 516 rtw89_info(rtwdev, "R_AX_BBRPT_DFS_ERR_ISR=0x%08x\n", 517 rtw89_read32(rtwdev, R_AX_BBRPT_DFS_ERR_ISR)); 518 } 519 } 520 521 if (dmac_err & B_AX_HAXIDMA_ERR_FLAG && chip->chip_id == RTL8852C) { 522 rtw89_info(rtwdev, "R_AX_HAXIDMA_ERR_IMR=0x%08x\n", 523 rtw89_read32(rtwdev, R_AX_HAXI_IDCT_MSK)); 524 rtw89_info(rtwdev, "R_AX_HAXIDMA_ERR_ISR=0x%08x\n", 525 rtw89_read32(rtwdev, R_AX_HAXI_IDCT)); 526 } 527 } 528 529 static void rtw89_mac_dump_cmac_err_status(struct rtw89_dev *rtwdev, 530 u8 band) 531 { 532 const struct rtw89_chip_info *chip = rtwdev->chip; 533 u32 offset = 0; 534 u32 cmac_err; 535 int ret; 536 537 ret = rtw89_mac_check_mac_en(rtwdev, band, RTW89_CMAC_SEL); 538 if (ret) { 539 if (band) 540 rtw89_warn(rtwdev, "[CMAC] : CMAC1 not enabled\n"); 541 else 542 rtw89_warn(rtwdev, "[CMAC] : CMAC0 not enabled\n"); 543 return; 544 } 545 546 if (band) 547 offset = RTW89_MAC_AX_BAND_REG_OFFSET; 548 549 cmac_err = rtw89_read32(rtwdev, R_AX_CMAC_ERR_ISR + offset); 550 rtw89_info(rtwdev, "R_AX_CMAC_ERR_ISR [%d]=0x%08x\n", band, 551 rtw89_read32(rtwdev, R_AX_CMAC_ERR_ISR + offset)); 552 rtw89_info(rtwdev, "R_AX_CMAC_FUNC_EN [%d]=0x%08x\n", band, 553 rtw89_read32(rtwdev, R_AX_CMAC_FUNC_EN + offset)); 554 rtw89_info(rtwdev, "R_AX_CK_EN [%d]=0x%08x\n", band, 555 rtw89_read32(rtwdev, R_AX_CK_EN + offset)); 556 557 if (cmac_err & B_AX_SCHEDULE_TOP_ERR_IND) { 558 rtw89_info(rtwdev, "R_AX_SCHEDULE_ERR_IMR [%d]=0x%08x\n", band, 559 rtw89_read32(rtwdev, R_AX_SCHEDULE_ERR_IMR + offset)); 560 rtw89_info(rtwdev, "R_AX_SCHEDULE_ERR_ISR [%d]=0x%08x\n", band, 561 rtw89_read32(rtwdev, R_AX_SCHEDULE_ERR_ISR + offset)); 562 } 563 564 if (cmac_err & B_AX_PTCL_TOP_ERR_IND) { 565 rtw89_info(rtwdev, "R_AX_PTCL_IMR0 [%d]=0x%08x\n", band, 566 rtw89_read32(rtwdev, R_AX_PTCL_IMR0 + offset)); 567 rtw89_info(rtwdev, "R_AX_PTCL_ISR0 [%d]=0x%08x\n", band, 568 rtw89_read32(rtwdev, R_AX_PTCL_ISR0 + offset)); 569 } 570 571 if (cmac_err & B_AX_DMA_TOP_ERR_IND) { 572 if (chip->chip_id == RTL8852C) { 573 rtw89_info(rtwdev, "R_AX_RX_ERR_FLAG [%d]=0x%08x\n", band, 574 rtw89_read32(rtwdev, R_AX_RX_ERR_FLAG + offset)); 575 rtw89_info(rtwdev, "R_AX_RX_ERR_FLAG_IMR [%d]=0x%08x\n", band, 576 rtw89_read32(rtwdev, R_AX_RX_ERR_FLAG_IMR + offset)); 577 } else { 578 rtw89_info(rtwdev, "R_AX_DLE_CTRL [%d]=0x%08x\n", band, 579 rtw89_read32(rtwdev, R_AX_DLE_CTRL + offset)); 580 } 581 } 582 583 if (cmac_err & B_AX_DMA_TOP_ERR_IND || cmac_err & B_AX_WMAC_RX_ERR_IND) { 584 if (chip->chip_id == RTL8852C) { 585 rtw89_info(rtwdev, "R_AX_PHYINFO_ERR_ISR [%d]=0x%08x\n", band, 586 rtw89_read32(rtwdev, R_AX_PHYINFO_ERR_ISR + offset)); 587 rtw89_info(rtwdev, "R_AX_PHYINFO_ERR_IMR [%d]=0x%08x\n", band, 588 rtw89_read32(rtwdev, R_AX_PHYINFO_ERR_IMR + offset)); 589 } else { 590 rtw89_info(rtwdev, "R_AX_PHYINFO_ERR_IMR [%d]=0x%08x\n", band, 591 rtw89_read32(rtwdev, R_AX_PHYINFO_ERR_IMR + offset)); 592 } 593 } 594 595 if (cmac_err & B_AX_TXPWR_CTRL_ERR_IND) { 596 rtw89_info(rtwdev, "R_AX_TXPWR_IMR [%d]=0x%08x\n", band, 597 rtw89_read32(rtwdev, R_AX_TXPWR_IMR + offset)); 598 rtw89_info(rtwdev, "R_AX_TXPWR_ISR [%d]=0x%08x\n", band, 599 rtw89_read32(rtwdev, R_AX_TXPWR_ISR + offset)); 600 } 601 602 if (cmac_err & B_AX_WMAC_TX_ERR_IND) { 603 if (chip->chip_id == RTL8852C) { 604 rtw89_info(rtwdev, "R_AX_TRXPTCL_ERROR_INDICA [%d]=0x%08x\n", band, 605 rtw89_read32(rtwdev, R_AX_TRXPTCL_ERROR_INDICA + offset)); 606 rtw89_info(rtwdev, "R_AX_TRXPTCL_ERROR_INDICA_MASK [%d]=0x%08x\n", band, 607 rtw89_read32(rtwdev, R_AX_TRXPTCL_ERROR_INDICA_MASK + offset)); 608 } else { 609 rtw89_info(rtwdev, "R_AX_TMAC_ERR_IMR_ISR [%d]=0x%08x\n", band, 610 rtw89_read32(rtwdev, R_AX_TMAC_ERR_IMR_ISR + offset)); 611 } 612 rtw89_info(rtwdev, "R_AX_DBGSEL_TRXPTCL [%d]=0x%08x\n", band, 613 rtw89_read32(rtwdev, R_AX_DBGSEL_TRXPTCL + offset)); 614 } 615 616 rtw89_info(rtwdev, "R_AX_CMAC_ERR_IMR [%d]=0x%08x\n", band, 617 rtw89_read32(rtwdev, R_AX_CMAC_ERR_IMR + offset)); 618 } 619 620 static void rtw89_mac_dump_err_status(struct rtw89_dev *rtwdev, 621 enum mac_ax_err_info err) 622 { 623 if (err != MAC_AX_ERR_L1_ERR_DMAC && 624 err != MAC_AX_ERR_L0_PROMOTE_TO_L1 && 625 err != MAC_AX_ERR_L0_ERR_CMAC0 && 626 err != MAC_AX_ERR_L0_ERR_CMAC1 && 627 err != MAC_AX_ERR_RXI300) 628 return; 629 630 rtw89_info(rtwdev, "--->\nerr=0x%x\n", err); 631 rtw89_info(rtwdev, "R_AX_SER_DBG_INFO =0x%08x\n", 632 rtw89_read32(rtwdev, R_AX_SER_DBG_INFO)); 633 634 rtw89_mac_dump_dmac_err_status(rtwdev); 635 rtw89_mac_dump_cmac_err_status(rtwdev, RTW89_MAC_0); 636 if (rtwdev->dbcc_en) 637 rtw89_mac_dump_cmac_err_status(rtwdev, RTW89_MAC_1); 638 639 rtwdev->hci.ops->dump_err_status(rtwdev); 640 641 if (err == MAC_AX_ERR_L0_PROMOTE_TO_L1) 642 rtw89_mac_dump_l0_to_l1(rtwdev, err); 643 644 rtw89_info(rtwdev, "<---\n"); 645 } 646 647 u32 rtw89_mac_get_err_status(struct rtw89_dev *rtwdev) 648 { 649 u32 err, err_scnr; 650 int ret; 651 652 ret = read_poll_timeout(rtw89_read32, err, (err != 0), 1000, 100000, 653 false, rtwdev, R_AX_HALT_C2H_CTRL); 654 if (ret) { 655 rtw89_warn(rtwdev, "Polling FW err status fail\n"); 656 return ret; 657 } 658 659 err = rtw89_read32(rtwdev, R_AX_HALT_C2H); 660 rtw89_write32(rtwdev, R_AX_HALT_C2H_CTRL, 0); 661 662 err_scnr = RTW89_ERROR_SCENARIO(err); 663 if (err_scnr == RTW89_WCPU_CPU_EXCEPTION) 664 err = MAC_AX_ERR_CPU_EXCEPTION; 665 else if (err_scnr == RTW89_WCPU_ASSERTION) 666 err = MAC_AX_ERR_ASSERTION; 667 else if (err_scnr == RTW89_RXI300_ERROR) 668 err = MAC_AX_ERR_RXI300; 669 670 rtw89_fw_st_dbg_dump(rtwdev); 671 rtw89_mac_dump_err_status(rtwdev, err); 672 673 return err; 674 } 675 EXPORT_SYMBOL(rtw89_mac_get_err_status); 676 677 int rtw89_mac_set_err_status(struct rtw89_dev *rtwdev, u32 err) 678 { 679 u32 halt; 680 int ret = 0; 681 682 if (err > MAC_AX_SET_ERR_MAX) { 683 rtw89_err(rtwdev, "Bad set-err-status value 0x%08x\n", err); 684 return -EINVAL; 685 } 686 687 ret = read_poll_timeout(rtw89_read32, halt, (halt == 0x0), 1000, 688 100000, false, rtwdev, R_AX_HALT_H2C_CTRL); 689 if (ret) { 690 rtw89_err(rtwdev, "FW doesn't receive previous msg\n"); 691 return -EFAULT; 692 } 693 694 rtw89_write32(rtwdev, R_AX_HALT_H2C, err); 695 rtw89_write32(rtwdev, R_AX_HALT_H2C_CTRL, B_AX_HALT_H2C_TRIGGER); 696 697 return 0; 698 } 699 EXPORT_SYMBOL(rtw89_mac_set_err_status); 700 701 static int hfc_reset_param(struct rtw89_dev *rtwdev) 702 { 703 struct rtw89_hfc_param *param = &rtwdev->mac.hfc_param; 704 struct rtw89_hfc_param_ini param_ini = {NULL}; 705 u8 qta_mode = rtwdev->mac.dle_info.qta_mode; 706 707 switch (rtwdev->hci.type) { 708 case RTW89_HCI_TYPE_PCIE: 709 param_ini = rtwdev->chip->hfc_param_ini[qta_mode]; 710 param->en = 0; 711 break; 712 default: 713 return -EINVAL; 714 } 715 716 if (param_ini.pub_cfg) 717 param->pub_cfg = *param_ini.pub_cfg; 718 719 if (param_ini.prec_cfg) { 720 param->prec_cfg = *param_ini.prec_cfg; 721 rtwdev->hal.sw_amsdu_max_size = 722 param->prec_cfg.wp_ch07_prec * HFC_PAGE_UNIT; 723 } 724 725 if (param_ini.ch_cfg) 726 param->ch_cfg = param_ini.ch_cfg; 727 728 memset(¶m->ch_info, 0, sizeof(param->ch_info)); 729 memset(¶m->pub_info, 0, sizeof(param->pub_info)); 730 param->mode = param_ini.mode; 731 732 return 0; 733 } 734 735 static int hfc_ch_cfg_chk(struct rtw89_dev *rtwdev, u8 ch) 736 { 737 struct rtw89_hfc_param *param = &rtwdev->mac.hfc_param; 738 const struct rtw89_hfc_ch_cfg *ch_cfg = param->ch_cfg; 739 const struct rtw89_hfc_pub_cfg *pub_cfg = ¶m->pub_cfg; 740 const struct rtw89_hfc_prec_cfg *prec_cfg = ¶m->prec_cfg; 741 742 if (ch >= RTW89_DMA_CH_NUM) 743 return -EINVAL; 744 745 if ((ch_cfg[ch].min && ch_cfg[ch].min < prec_cfg->ch011_prec) || 746 ch_cfg[ch].max > pub_cfg->pub_max) 747 return -EINVAL; 748 if (ch_cfg[ch].grp >= grp_num) 749 return -EINVAL; 750 751 return 0; 752 } 753 754 static int hfc_pub_info_chk(struct rtw89_dev *rtwdev) 755 { 756 struct rtw89_hfc_param *param = &rtwdev->mac.hfc_param; 757 const struct rtw89_hfc_pub_cfg *cfg = ¶m->pub_cfg; 758 struct rtw89_hfc_pub_info *info = ¶m->pub_info; 759 760 if (info->g0_used + info->g1_used + info->pub_aval != cfg->pub_max) { 761 if (rtwdev->chip->chip_id == RTL8852A) 762 return 0; 763 else 764 return -EFAULT; 765 } 766 767 return 0; 768 } 769 770 static int hfc_pub_cfg_chk(struct rtw89_dev *rtwdev) 771 { 772 struct rtw89_hfc_param *param = &rtwdev->mac.hfc_param; 773 const struct rtw89_hfc_pub_cfg *pub_cfg = ¶m->pub_cfg; 774 775 if (pub_cfg->grp0 + pub_cfg->grp1 != pub_cfg->pub_max) 776 return -EFAULT; 777 778 return 0; 779 } 780 781 static int hfc_ch_ctrl(struct rtw89_dev *rtwdev, u8 ch) 782 { 783 const struct rtw89_chip_info *chip = rtwdev->chip; 784 const struct rtw89_page_regs *regs = chip->page_regs; 785 struct rtw89_hfc_param *param = &rtwdev->mac.hfc_param; 786 const struct rtw89_hfc_ch_cfg *cfg = param->ch_cfg; 787 int ret = 0; 788 u32 val = 0; 789 790 ret = rtw89_mac_check_mac_en(rtwdev, RTW89_MAC_0, RTW89_DMAC_SEL); 791 if (ret) 792 return ret; 793 794 ret = hfc_ch_cfg_chk(rtwdev, ch); 795 if (ret) 796 return ret; 797 798 if (ch > RTW89_DMA_B1HI) 799 return -EINVAL; 800 801 val = u32_encode_bits(cfg[ch].min, B_AX_MIN_PG_MASK) | 802 u32_encode_bits(cfg[ch].max, B_AX_MAX_PG_MASK) | 803 (cfg[ch].grp ? B_AX_GRP : 0); 804 rtw89_write32(rtwdev, regs->ach_page_ctrl + ch * 4, val); 805 806 return 0; 807 } 808 809 static int hfc_upd_ch_info(struct rtw89_dev *rtwdev, u8 ch) 810 { 811 const struct rtw89_chip_info *chip = rtwdev->chip; 812 const struct rtw89_page_regs *regs = chip->page_regs; 813 struct rtw89_hfc_param *param = &rtwdev->mac.hfc_param; 814 struct rtw89_hfc_ch_info *info = param->ch_info; 815 const struct rtw89_hfc_ch_cfg *cfg = param->ch_cfg; 816 u32 val; 817 u32 ret; 818 819 ret = rtw89_mac_check_mac_en(rtwdev, RTW89_MAC_0, RTW89_DMAC_SEL); 820 if (ret) 821 return ret; 822 823 if (ch > RTW89_DMA_H2C) 824 return -EINVAL; 825 826 val = rtw89_read32(rtwdev, regs->ach_page_info + ch * 4); 827 info[ch].aval = u32_get_bits(val, B_AX_AVAL_PG_MASK); 828 if (ch < RTW89_DMA_H2C) 829 info[ch].used = u32_get_bits(val, B_AX_USE_PG_MASK); 830 else 831 info[ch].used = cfg[ch].min - info[ch].aval; 832 833 return 0; 834 } 835 836 static int hfc_pub_ctrl(struct rtw89_dev *rtwdev) 837 { 838 const struct rtw89_chip_info *chip = rtwdev->chip; 839 const struct rtw89_page_regs *regs = chip->page_regs; 840 const struct rtw89_hfc_pub_cfg *cfg = &rtwdev->mac.hfc_param.pub_cfg; 841 u32 val; 842 int ret; 843 844 ret = rtw89_mac_check_mac_en(rtwdev, RTW89_MAC_0, RTW89_DMAC_SEL); 845 if (ret) 846 return ret; 847 848 ret = hfc_pub_cfg_chk(rtwdev); 849 if (ret) 850 return ret; 851 852 val = u32_encode_bits(cfg->grp0, B_AX_PUBPG_G0_MASK) | 853 u32_encode_bits(cfg->grp1, B_AX_PUBPG_G1_MASK); 854 rtw89_write32(rtwdev, regs->pub_page_ctrl1, val); 855 856 val = u32_encode_bits(cfg->wp_thrd, B_AX_WP_THRD_MASK); 857 rtw89_write32(rtwdev, regs->wp_page_ctrl2, val); 858 859 return 0; 860 } 861 862 static int hfc_upd_mix_info(struct rtw89_dev *rtwdev) 863 { 864 const struct rtw89_chip_info *chip = rtwdev->chip; 865 const struct rtw89_page_regs *regs = chip->page_regs; 866 struct rtw89_hfc_param *param = &rtwdev->mac.hfc_param; 867 struct rtw89_hfc_pub_cfg *pub_cfg = ¶m->pub_cfg; 868 struct rtw89_hfc_prec_cfg *prec_cfg = ¶m->prec_cfg; 869 struct rtw89_hfc_pub_info *info = ¶m->pub_info; 870 u32 val; 871 int ret; 872 873 ret = rtw89_mac_check_mac_en(rtwdev, RTW89_MAC_0, RTW89_DMAC_SEL); 874 if (ret) 875 return ret; 876 877 val = rtw89_read32(rtwdev, regs->pub_page_info1); 878 info->g0_used = u32_get_bits(val, B_AX_G0_USE_PG_MASK); 879 info->g1_used = u32_get_bits(val, B_AX_G1_USE_PG_MASK); 880 val = rtw89_read32(rtwdev, regs->pub_page_info3); 881 info->g0_aval = u32_get_bits(val, B_AX_G0_AVAL_PG_MASK); 882 info->g1_aval = u32_get_bits(val, B_AX_G1_AVAL_PG_MASK); 883 info->pub_aval = 884 u32_get_bits(rtw89_read32(rtwdev, regs->pub_page_info2), 885 B_AX_PUB_AVAL_PG_MASK); 886 info->wp_aval = 887 u32_get_bits(rtw89_read32(rtwdev, regs->wp_page_info1), 888 B_AX_WP_AVAL_PG_MASK); 889 890 val = rtw89_read32(rtwdev, regs->hci_fc_ctrl); 891 param->en = val & B_AX_HCI_FC_EN ? 1 : 0; 892 param->h2c_en = val & B_AX_HCI_FC_CH12_EN ? 1 : 0; 893 param->mode = u32_get_bits(val, B_AX_HCI_FC_MODE_MASK); 894 prec_cfg->ch011_full_cond = 895 u32_get_bits(val, B_AX_HCI_FC_WD_FULL_COND_MASK); 896 prec_cfg->h2c_full_cond = 897 u32_get_bits(val, B_AX_HCI_FC_CH12_FULL_COND_MASK); 898 prec_cfg->wp_ch07_full_cond = 899 u32_get_bits(val, B_AX_HCI_FC_WP_CH07_FULL_COND_MASK); 900 prec_cfg->wp_ch811_full_cond = 901 u32_get_bits(val, B_AX_HCI_FC_WP_CH811_FULL_COND_MASK); 902 903 val = rtw89_read32(rtwdev, regs->ch_page_ctrl); 904 prec_cfg->ch011_prec = u32_get_bits(val, B_AX_PREC_PAGE_CH011_MASK); 905 prec_cfg->h2c_prec = u32_get_bits(val, B_AX_PREC_PAGE_CH12_MASK); 906 907 val = rtw89_read32(rtwdev, regs->pub_page_ctrl2); 908 pub_cfg->pub_max = u32_get_bits(val, B_AX_PUBPG_ALL_MASK); 909 910 val = rtw89_read32(rtwdev, regs->wp_page_ctrl1); 911 prec_cfg->wp_ch07_prec = u32_get_bits(val, B_AX_PREC_PAGE_WP_CH07_MASK); 912 prec_cfg->wp_ch811_prec = u32_get_bits(val, B_AX_PREC_PAGE_WP_CH811_MASK); 913 914 val = rtw89_read32(rtwdev, regs->wp_page_ctrl2); 915 pub_cfg->wp_thrd = u32_get_bits(val, B_AX_WP_THRD_MASK); 916 917 val = rtw89_read32(rtwdev, regs->pub_page_ctrl1); 918 pub_cfg->grp0 = u32_get_bits(val, B_AX_PUBPG_G0_MASK); 919 pub_cfg->grp1 = u32_get_bits(val, B_AX_PUBPG_G1_MASK); 920 921 ret = hfc_pub_info_chk(rtwdev); 922 if (param->en && ret) 923 return ret; 924 925 return 0; 926 } 927 928 static void hfc_h2c_cfg(struct rtw89_dev *rtwdev) 929 { 930 const struct rtw89_chip_info *chip = rtwdev->chip; 931 const struct rtw89_page_regs *regs = chip->page_regs; 932 struct rtw89_hfc_param *param = &rtwdev->mac.hfc_param; 933 const struct rtw89_hfc_prec_cfg *prec_cfg = ¶m->prec_cfg; 934 u32 val; 935 936 val = u32_encode_bits(prec_cfg->h2c_prec, B_AX_PREC_PAGE_CH12_MASK); 937 rtw89_write32(rtwdev, regs->ch_page_ctrl, val); 938 939 rtw89_write32_mask(rtwdev, regs->hci_fc_ctrl, 940 B_AX_HCI_FC_CH12_FULL_COND_MASK, 941 prec_cfg->h2c_full_cond); 942 } 943 944 static void hfc_mix_cfg(struct rtw89_dev *rtwdev) 945 { 946 const struct rtw89_chip_info *chip = rtwdev->chip; 947 const struct rtw89_page_regs *regs = chip->page_regs; 948 struct rtw89_hfc_param *param = &rtwdev->mac.hfc_param; 949 const struct rtw89_hfc_pub_cfg *pub_cfg = ¶m->pub_cfg; 950 const struct rtw89_hfc_prec_cfg *prec_cfg = ¶m->prec_cfg; 951 u32 val; 952 953 val = u32_encode_bits(prec_cfg->ch011_prec, B_AX_PREC_PAGE_CH011_MASK) | 954 u32_encode_bits(prec_cfg->h2c_prec, B_AX_PREC_PAGE_CH12_MASK); 955 rtw89_write32(rtwdev, regs->ch_page_ctrl, val); 956 957 val = u32_encode_bits(pub_cfg->pub_max, B_AX_PUBPG_ALL_MASK); 958 rtw89_write32(rtwdev, regs->pub_page_ctrl2, val); 959 960 val = u32_encode_bits(prec_cfg->wp_ch07_prec, 961 B_AX_PREC_PAGE_WP_CH07_MASK) | 962 u32_encode_bits(prec_cfg->wp_ch811_prec, 963 B_AX_PREC_PAGE_WP_CH811_MASK); 964 rtw89_write32(rtwdev, regs->wp_page_ctrl1, val); 965 966 val = u32_replace_bits(rtw89_read32(rtwdev, regs->hci_fc_ctrl), 967 param->mode, B_AX_HCI_FC_MODE_MASK); 968 val = u32_replace_bits(val, prec_cfg->ch011_full_cond, 969 B_AX_HCI_FC_WD_FULL_COND_MASK); 970 val = u32_replace_bits(val, prec_cfg->h2c_full_cond, 971 B_AX_HCI_FC_CH12_FULL_COND_MASK); 972 val = u32_replace_bits(val, prec_cfg->wp_ch07_full_cond, 973 B_AX_HCI_FC_WP_CH07_FULL_COND_MASK); 974 val = u32_replace_bits(val, prec_cfg->wp_ch811_full_cond, 975 B_AX_HCI_FC_WP_CH811_FULL_COND_MASK); 976 rtw89_write32(rtwdev, regs->hci_fc_ctrl, val); 977 } 978 979 static void hfc_func_en(struct rtw89_dev *rtwdev, bool en, bool h2c_en) 980 { 981 const struct rtw89_chip_info *chip = rtwdev->chip; 982 const struct rtw89_page_regs *regs = chip->page_regs; 983 struct rtw89_hfc_param *param = &rtwdev->mac.hfc_param; 984 u32 val; 985 986 val = rtw89_read32(rtwdev, regs->hci_fc_ctrl); 987 param->en = en; 988 param->h2c_en = h2c_en; 989 val = en ? (val | B_AX_HCI_FC_EN) : (val & ~B_AX_HCI_FC_EN); 990 val = h2c_en ? (val | B_AX_HCI_FC_CH12_EN) : 991 (val & ~B_AX_HCI_FC_CH12_EN); 992 rtw89_write32(rtwdev, regs->hci_fc_ctrl, val); 993 } 994 995 static int hfc_init(struct rtw89_dev *rtwdev, bool reset, bool en, bool h2c_en) 996 { 997 const struct rtw89_chip_info *chip = rtwdev->chip; 998 u32 dma_ch_mask = chip->dma_ch_mask; 999 u8 ch; 1000 u32 ret = 0; 1001 1002 if (reset) 1003 ret = hfc_reset_param(rtwdev); 1004 if (ret) 1005 return ret; 1006 1007 ret = rtw89_mac_check_mac_en(rtwdev, RTW89_MAC_0, RTW89_DMAC_SEL); 1008 if (ret) 1009 return ret; 1010 1011 hfc_func_en(rtwdev, false, false); 1012 1013 if (!en && h2c_en) { 1014 hfc_h2c_cfg(rtwdev); 1015 hfc_func_en(rtwdev, en, h2c_en); 1016 return ret; 1017 } 1018 1019 for (ch = RTW89_DMA_ACH0; ch < RTW89_DMA_H2C; ch++) { 1020 if (dma_ch_mask & BIT(ch)) 1021 continue; 1022 ret = hfc_ch_ctrl(rtwdev, ch); 1023 if (ret) 1024 return ret; 1025 } 1026 1027 ret = hfc_pub_ctrl(rtwdev); 1028 if (ret) 1029 return ret; 1030 1031 hfc_mix_cfg(rtwdev); 1032 if (en || h2c_en) { 1033 hfc_func_en(rtwdev, en, h2c_en); 1034 udelay(10); 1035 } 1036 for (ch = RTW89_DMA_ACH0; ch < RTW89_DMA_H2C; ch++) { 1037 if (dma_ch_mask & BIT(ch)) 1038 continue; 1039 ret = hfc_upd_ch_info(rtwdev, ch); 1040 if (ret) 1041 return ret; 1042 } 1043 ret = hfc_upd_mix_info(rtwdev); 1044 1045 return ret; 1046 } 1047 1048 #define PWR_POLL_CNT 2000 1049 static int pwr_cmd_poll(struct rtw89_dev *rtwdev, 1050 const struct rtw89_pwr_cfg *cfg) 1051 { 1052 u8 val = 0; 1053 int ret; 1054 u32 addr = cfg->base == PWR_INTF_MSK_SDIO ? 1055 cfg->addr | SDIO_LOCAL_BASE_ADDR : cfg->addr; 1056 1057 ret = read_poll_timeout(rtw89_read8, val, !((val ^ cfg->val) & cfg->msk), 1058 1000, 1000 * PWR_POLL_CNT, false, rtwdev, addr); 1059 1060 if (!ret) 1061 return 0; 1062 1063 rtw89_warn(rtwdev, "[ERR] Polling timeout\n"); 1064 rtw89_warn(rtwdev, "[ERR] addr: %X, %X\n", addr, cfg->addr); 1065 rtw89_warn(rtwdev, "[ERR] val: %X, %X\n", val, cfg->val); 1066 1067 return -EBUSY; 1068 } 1069 1070 static int rtw89_mac_sub_pwr_seq(struct rtw89_dev *rtwdev, u8 cv_msk, 1071 u8 intf_msk, const struct rtw89_pwr_cfg *cfg) 1072 { 1073 const struct rtw89_pwr_cfg *cur_cfg; 1074 u32 addr; 1075 u8 val; 1076 1077 for (cur_cfg = cfg; cur_cfg->cmd != PWR_CMD_END; cur_cfg++) { 1078 if (!(cur_cfg->intf_msk & intf_msk) || 1079 !(cur_cfg->cv_msk & cv_msk)) 1080 continue; 1081 1082 switch (cur_cfg->cmd) { 1083 case PWR_CMD_WRITE: 1084 addr = cur_cfg->addr; 1085 1086 if (cur_cfg->base == PWR_BASE_SDIO) 1087 addr |= SDIO_LOCAL_BASE_ADDR; 1088 1089 val = rtw89_read8(rtwdev, addr); 1090 val &= ~(cur_cfg->msk); 1091 val |= (cur_cfg->val & cur_cfg->msk); 1092 1093 rtw89_write8(rtwdev, addr, val); 1094 break; 1095 case PWR_CMD_POLL: 1096 if (pwr_cmd_poll(rtwdev, cur_cfg)) 1097 return -EBUSY; 1098 break; 1099 case PWR_CMD_DELAY: 1100 if (cur_cfg->val == PWR_DELAY_US) 1101 udelay(cur_cfg->addr); 1102 else 1103 fsleep(cur_cfg->addr * 1000); 1104 break; 1105 default: 1106 return -EINVAL; 1107 } 1108 } 1109 1110 return 0; 1111 } 1112 1113 static int rtw89_mac_pwr_seq(struct rtw89_dev *rtwdev, 1114 const struct rtw89_pwr_cfg * const *cfg_seq) 1115 { 1116 int ret; 1117 1118 for (; *cfg_seq; cfg_seq++) { 1119 ret = rtw89_mac_sub_pwr_seq(rtwdev, BIT(rtwdev->hal.cv), 1120 PWR_INTF_MSK_PCIE, *cfg_seq); 1121 if (ret) 1122 return -EBUSY; 1123 } 1124 1125 return 0; 1126 } 1127 1128 static enum rtw89_rpwm_req_pwr_state 1129 rtw89_mac_get_req_pwr_state(struct rtw89_dev *rtwdev) 1130 { 1131 enum rtw89_rpwm_req_pwr_state state; 1132 1133 switch (rtwdev->ps_mode) { 1134 case RTW89_PS_MODE_RFOFF: 1135 state = RTW89_MAC_RPWM_REQ_PWR_STATE_BAND0_RFOFF; 1136 break; 1137 case RTW89_PS_MODE_CLK_GATED: 1138 state = RTW89_MAC_RPWM_REQ_PWR_STATE_CLK_GATED; 1139 break; 1140 case RTW89_PS_MODE_PWR_GATED: 1141 state = RTW89_MAC_RPWM_REQ_PWR_STATE_PWR_GATED; 1142 break; 1143 default: 1144 state = RTW89_MAC_RPWM_REQ_PWR_STATE_ACTIVE; 1145 break; 1146 } 1147 return state; 1148 } 1149 1150 static void rtw89_mac_send_rpwm(struct rtw89_dev *rtwdev, 1151 enum rtw89_rpwm_req_pwr_state req_pwr_state, 1152 bool notify_wake) 1153 { 1154 u16 request; 1155 1156 spin_lock_bh(&rtwdev->rpwm_lock); 1157 1158 request = rtw89_read16(rtwdev, R_AX_RPWM); 1159 request ^= request | PS_RPWM_TOGGLE; 1160 request |= req_pwr_state; 1161 1162 if (notify_wake) { 1163 request |= PS_RPWM_NOTIFY_WAKE; 1164 } else { 1165 rtwdev->mac.rpwm_seq_num = (rtwdev->mac.rpwm_seq_num + 1) & 1166 RPWM_SEQ_NUM_MAX; 1167 request |= FIELD_PREP(PS_RPWM_SEQ_NUM, 1168 rtwdev->mac.rpwm_seq_num); 1169 1170 if (req_pwr_state < RTW89_MAC_RPWM_REQ_PWR_STATE_CLK_GATED) 1171 request |= PS_RPWM_ACK; 1172 } 1173 rtw89_write16(rtwdev, rtwdev->hci.rpwm_addr, request); 1174 1175 spin_unlock_bh(&rtwdev->rpwm_lock); 1176 } 1177 1178 static int rtw89_mac_check_cpwm_state(struct rtw89_dev *rtwdev, 1179 enum rtw89_rpwm_req_pwr_state req_pwr_state) 1180 { 1181 bool request_deep_mode; 1182 bool in_deep_mode; 1183 u8 rpwm_req_num; 1184 u8 cpwm_rsp_seq; 1185 u8 cpwm_seq; 1186 u8 cpwm_status; 1187 1188 if (req_pwr_state >= RTW89_MAC_RPWM_REQ_PWR_STATE_CLK_GATED) 1189 request_deep_mode = true; 1190 else 1191 request_deep_mode = false; 1192 1193 if (rtw89_read32_mask(rtwdev, R_AX_LDM, B_AX_EN_32K)) 1194 in_deep_mode = true; 1195 else 1196 in_deep_mode = false; 1197 1198 if (request_deep_mode != in_deep_mode) 1199 return -EPERM; 1200 1201 if (request_deep_mode) 1202 return 0; 1203 1204 rpwm_req_num = rtwdev->mac.rpwm_seq_num; 1205 cpwm_rsp_seq = rtw89_read16_mask(rtwdev, rtwdev->hci.cpwm_addr, 1206 PS_CPWM_RSP_SEQ_NUM); 1207 1208 if (rpwm_req_num != cpwm_rsp_seq) 1209 return -EPERM; 1210 1211 rtwdev->mac.cpwm_seq_num = (rtwdev->mac.cpwm_seq_num + 1) & 1212 CPWM_SEQ_NUM_MAX; 1213 1214 cpwm_seq = rtw89_read16_mask(rtwdev, rtwdev->hci.cpwm_addr, PS_CPWM_SEQ_NUM); 1215 if (cpwm_seq != rtwdev->mac.cpwm_seq_num) 1216 return -EPERM; 1217 1218 cpwm_status = rtw89_read16_mask(rtwdev, rtwdev->hci.cpwm_addr, PS_CPWM_STATE); 1219 if (cpwm_status != req_pwr_state) 1220 return -EPERM; 1221 1222 return 0; 1223 } 1224 1225 void rtw89_mac_power_mode_change(struct rtw89_dev *rtwdev, bool enter) 1226 { 1227 enum rtw89_rpwm_req_pwr_state state; 1228 unsigned long delay = enter ? 10 : 150; 1229 int ret; 1230 int i; 1231 1232 if (enter) 1233 state = rtw89_mac_get_req_pwr_state(rtwdev); 1234 else 1235 state = RTW89_MAC_RPWM_REQ_PWR_STATE_ACTIVE; 1236 1237 for (i = 0; i < RPWM_TRY_CNT; i++) { 1238 rtw89_mac_send_rpwm(rtwdev, state, false); 1239 ret = read_poll_timeout_atomic(rtw89_mac_check_cpwm_state, ret, 1240 !ret, delay, 15000, false, 1241 rtwdev, state); 1242 if (!ret) 1243 break; 1244 1245 if (i == RPWM_TRY_CNT - 1) 1246 rtw89_err(rtwdev, "firmware failed to ack for %s ps mode\n", 1247 enter ? "entering" : "leaving"); 1248 else 1249 rtw89_debug(rtwdev, RTW89_DBG_UNEXP, 1250 "%d time firmware failed to ack for %s ps mode\n", 1251 i + 1, enter ? "entering" : "leaving"); 1252 } 1253 } 1254 1255 void rtw89_mac_notify_wake(struct rtw89_dev *rtwdev) 1256 { 1257 enum rtw89_rpwm_req_pwr_state state; 1258 1259 state = rtw89_mac_get_req_pwr_state(rtwdev); 1260 rtw89_mac_send_rpwm(rtwdev, state, true); 1261 } 1262 1263 static int rtw89_mac_power_switch(struct rtw89_dev *rtwdev, bool on) 1264 { 1265 #define PWR_ACT 1 1266 const struct rtw89_chip_info *chip = rtwdev->chip; 1267 const struct rtw89_pwr_cfg * const *cfg_seq; 1268 int (*cfg_func)(struct rtw89_dev *rtwdev); 1269 int ret; 1270 u8 val; 1271 1272 if (on) { 1273 cfg_seq = chip->pwr_on_seq; 1274 cfg_func = chip->ops->pwr_on_func; 1275 } else { 1276 cfg_seq = chip->pwr_off_seq; 1277 cfg_func = chip->ops->pwr_off_func; 1278 } 1279 1280 if (test_bit(RTW89_FLAG_FW_RDY, rtwdev->flags)) 1281 __rtw89_leave_ps_mode(rtwdev); 1282 1283 val = rtw89_read32_mask(rtwdev, R_AX_IC_PWR_STATE, B_AX_WLMAC_PWR_STE_MASK); 1284 if (on && val == PWR_ACT) { 1285 rtw89_err(rtwdev, "MAC has already powered on\n"); 1286 return -EBUSY; 1287 } 1288 1289 ret = cfg_func ? cfg_func(rtwdev) : rtw89_mac_pwr_seq(rtwdev, cfg_seq); 1290 if (ret) 1291 return ret; 1292 1293 if (on) { 1294 set_bit(RTW89_FLAG_POWERON, rtwdev->flags); 1295 rtw89_write8(rtwdev, R_AX_SCOREBOARD + 3, MAC_AX_NOTIFY_TP_MAJOR); 1296 } else { 1297 clear_bit(RTW89_FLAG_POWERON, rtwdev->flags); 1298 clear_bit(RTW89_FLAG_FW_RDY, rtwdev->flags); 1299 rtw89_write8(rtwdev, R_AX_SCOREBOARD + 3, MAC_AX_NOTIFY_PWR_MAJOR); 1300 rtw89_set_entity_state(rtwdev, false); 1301 } 1302 1303 return 0; 1304 #undef PWR_ACT 1305 } 1306 1307 void rtw89_mac_pwr_off(struct rtw89_dev *rtwdev) 1308 { 1309 rtw89_mac_power_switch(rtwdev, false); 1310 } 1311 1312 static int cmac_func_en(struct rtw89_dev *rtwdev, u8 mac_idx, bool en) 1313 { 1314 u32 func_en = 0; 1315 u32 ck_en = 0; 1316 u32 c1pc_en = 0; 1317 u32 addrl_func_en[] = {R_AX_CMAC_FUNC_EN, R_AX_CMAC_FUNC_EN_C1}; 1318 u32 addrl_ck_en[] = {R_AX_CK_EN, R_AX_CK_EN_C1}; 1319 1320 func_en = B_AX_CMAC_EN | B_AX_CMAC_TXEN | B_AX_CMAC_RXEN | 1321 B_AX_PHYINTF_EN | B_AX_CMAC_DMA_EN | B_AX_PTCLTOP_EN | 1322 B_AX_SCHEDULER_EN | B_AX_TMAC_EN | B_AX_RMAC_EN | 1323 B_AX_CMAC_CRPRT; 1324 ck_en = B_AX_CMAC_CKEN | B_AX_PHYINTF_CKEN | B_AX_CMAC_DMA_CKEN | 1325 B_AX_PTCLTOP_CKEN | B_AX_SCHEDULER_CKEN | B_AX_TMAC_CKEN | 1326 B_AX_RMAC_CKEN; 1327 c1pc_en = B_AX_R_SYM_WLCMAC1_PC_EN | 1328 B_AX_R_SYM_WLCMAC1_P1_PC_EN | 1329 B_AX_R_SYM_WLCMAC1_P2_PC_EN | 1330 B_AX_R_SYM_WLCMAC1_P3_PC_EN | 1331 B_AX_R_SYM_WLCMAC1_P4_PC_EN; 1332 1333 if (en) { 1334 if (mac_idx == RTW89_MAC_1) { 1335 rtw89_write32_set(rtwdev, R_AX_AFE_CTRL1, c1pc_en); 1336 rtw89_write32_clr(rtwdev, R_AX_SYS_ISO_CTRL_EXTEND, 1337 B_AX_R_SYM_ISO_CMAC12PP); 1338 rtw89_write32_set(rtwdev, R_AX_SYS_ISO_CTRL_EXTEND, 1339 B_AX_CMAC1_FEN); 1340 } 1341 rtw89_write32_set(rtwdev, addrl_ck_en[mac_idx], ck_en); 1342 rtw89_write32_set(rtwdev, addrl_func_en[mac_idx], func_en); 1343 } else { 1344 rtw89_write32_clr(rtwdev, addrl_func_en[mac_idx], func_en); 1345 rtw89_write32_clr(rtwdev, addrl_ck_en[mac_idx], ck_en); 1346 if (mac_idx == RTW89_MAC_1) { 1347 rtw89_write32_clr(rtwdev, R_AX_SYS_ISO_CTRL_EXTEND, 1348 B_AX_CMAC1_FEN); 1349 rtw89_write32_set(rtwdev, R_AX_SYS_ISO_CTRL_EXTEND, 1350 B_AX_R_SYM_ISO_CMAC12PP); 1351 rtw89_write32_clr(rtwdev, R_AX_AFE_CTRL1, c1pc_en); 1352 } 1353 } 1354 1355 return 0; 1356 } 1357 1358 static int dmac_func_en(struct rtw89_dev *rtwdev) 1359 { 1360 enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id; 1361 u32 val32; 1362 1363 if (chip_id == RTL8852C) 1364 val32 = (B_AX_MAC_FUNC_EN | B_AX_DMAC_FUNC_EN | 1365 B_AX_MAC_SEC_EN | B_AX_DISPATCHER_EN | 1366 B_AX_DLE_CPUIO_EN | B_AX_PKT_IN_EN | 1367 B_AX_DMAC_TBL_EN | B_AX_PKT_BUF_EN | 1368 B_AX_STA_SCH_EN | B_AX_TXPKT_CTRL_EN | 1369 B_AX_WD_RLS_EN | B_AX_MPDU_PROC_EN | 1370 B_AX_DMAC_CRPRT | B_AX_H_AXIDMA_EN); 1371 else 1372 val32 = (B_AX_MAC_FUNC_EN | B_AX_DMAC_FUNC_EN | 1373 B_AX_MAC_SEC_EN | B_AX_DISPATCHER_EN | 1374 B_AX_DLE_CPUIO_EN | B_AX_PKT_IN_EN | 1375 B_AX_DMAC_TBL_EN | B_AX_PKT_BUF_EN | 1376 B_AX_STA_SCH_EN | B_AX_TXPKT_CTRL_EN | 1377 B_AX_WD_RLS_EN | B_AX_MPDU_PROC_EN | 1378 B_AX_DMAC_CRPRT); 1379 rtw89_write32(rtwdev, R_AX_DMAC_FUNC_EN, val32); 1380 1381 val32 = (B_AX_MAC_SEC_CLK_EN | B_AX_DISPATCHER_CLK_EN | 1382 B_AX_DLE_CPUIO_CLK_EN | B_AX_PKT_IN_CLK_EN | 1383 B_AX_STA_SCH_CLK_EN | B_AX_TXPKT_CTRL_CLK_EN | 1384 B_AX_WD_RLS_CLK_EN | B_AX_BBRPT_CLK_EN); 1385 rtw89_write32(rtwdev, R_AX_DMAC_CLK_EN, val32); 1386 1387 return 0; 1388 } 1389 1390 static int chip_func_en(struct rtw89_dev *rtwdev) 1391 { 1392 enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id; 1393 1394 if (chip_id == RTL8852A || chip_id == RTL8852B) 1395 rtw89_write32_set(rtwdev, R_AX_SPS_DIG_ON_CTRL0, 1396 B_AX_OCP_L1_MASK); 1397 1398 return 0; 1399 } 1400 1401 static int rtw89_mac_sys_init(struct rtw89_dev *rtwdev) 1402 { 1403 int ret; 1404 1405 ret = dmac_func_en(rtwdev); 1406 if (ret) 1407 return ret; 1408 1409 ret = cmac_func_en(rtwdev, 0, true); 1410 if (ret) 1411 return ret; 1412 1413 ret = chip_func_en(rtwdev); 1414 if (ret) 1415 return ret; 1416 1417 return ret; 1418 } 1419 1420 const struct rtw89_mac_size_set rtw89_mac_size = { 1421 .hfc_preccfg_pcie = {2, 40, 0, 0, 1, 0, 0, 0}, 1422 /* PCIE 64 */ 1423 .wde_size0 = {RTW89_WDE_PG_64, 4095, 1,}, 1424 /* DLFW */ 1425 .wde_size4 = {RTW89_WDE_PG_64, 0, 4096,}, 1426 /* PCIE 64 */ 1427 .wde_size6 = {RTW89_WDE_PG_64, 512, 0,}, 1428 /* DLFW */ 1429 .wde_size9 = {RTW89_WDE_PG_64, 0, 1024,}, 1430 /* 8852C DLFW */ 1431 .wde_size18 = {RTW89_WDE_PG_64, 0, 2048,}, 1432 /* 8852C PCIE SCC */ 1433 .wde_size19 = {RTW89_WDE_PG_64, 3328, 0,}, 1434 /* PCIE */ 1435 .ple_size0 = {RTW89_PLE_PG_128, 1520, 16,}, 1436 /* DLFW */ 1437 .ple_size4 = {RTW89_PLE_PG_128, 64, 1472,}, 1438 /* PCIE 64 */ 1439 .ple_size6 = {RTW89_PLE_PG_128, 496, 16,}, 1440 /* DLFW */ 1441 .ple_size8 = {RTW89_PLE_PG_128, 64, 960,}, 1442 /* 8852C DLFW */ 1443 .ple_size18 = {RTW89_PLE_PG_128, 2544, 16,}, 1444 /* 8852C PCIE SCC */ 1445 .ple_size19 = {RTW89_PLE_PG_128, 1904, 16,}, 1446 /* PCIE 64 */ 1447 .wde_qt0 = {3792, 196, 0, 107,}, 1448 /* DLFW */ 1449 .wde_qt4 = {0, 0, 0, 0,}, 1450 /* PCIE 64 */ 1451 .wde_qt6 = {448, 48, 0, 16,}, 1452 /* 8852C DLFW */ 1453 .wde_qt17 = {0, 0, 0, 0,}, 1454 /* 8852C PCIE SCC */ 1455 .wde_qt18 = {3228, 60, 0, 40,}, 1456 /* PCIE SCC */ 1457 .ple_qt4 = {264, 0, 16, 20, 26, 13, 356, 0, 32, 40, 8,}, 1458 /* PCIE SCC */ 1459 .ple_qt5 = {264, 0, 32, 20, 64, 13, 1101, 0, 64, 128, 120,}, 1460 /* DLFW */ 1461 .ple_qt13 = {0, 0, 16, 48, 0, 0, 0, 0, 0, 0, 0,}, 1462 /* PCIE 64 */ 1463 .ple_qt18 = {147, 0, 16, 20, 17, 13, 89, 0, 32, 14, 8, 0,}, 1464 /* DLFW 52C */ 1465 .ple_qt44 = {0, 0, 16, 256, 0, 0, 0, 0, 0, 0, 0, 0,}, 1466 /* DLFW 52C */ 1467 .ple_qt45 = {0, 0, 32, 256, 0, 0, 0, 0, 0, 0, 0, 0,}, 1468 /* 8852C PCIE SCC */ 1469 .ple_qt46 = {525, 0, 16, 20, 13, 13, 178, 0, 32, 62, 8, 16,}, 1470 /* 8852C PCIE SCC */ 1471 .ple_qt47 = {525, 0, 32, 20, 1034, 13, 1199, 0, 1053, 62, 160, 1037,}, 1472 /* PCIE 64 */ 1473 .ple_qt58 = {147, 0, 16, 20, 157, 13, 229, 0, 172, 14, 24, 0,}, 1474 /* 8852A PCIE WOW */ 1475 .ple_qt_52a_wow = {264, 0, 32, 20, 64, 13, 1005, 0, 64, 128, 120,}, 1476 }; 1477 EXPORT_SYMBOL(rtw89_mac_size); 1478 1479 static const struct rtw89_dle_mem *get_dle_mem_cfg(struct rtw89_dev *rtwdev, 1480 enum rtw89_qta_mode mode) 1481 { 1482 struct rtw89_mac_info *mac = &rtwdev->mac; 1483 const struct rtw89_dle_mem *cfg; 1484 1485 cfg = &rtwdev->chip->dle_mem[mode]; 1486 if (!cfg) 1487 return NULL; 1488 1489 if (cfg->mode != mode) { 1490 rtw89_warn(rtwdev, "qta mode unmatch!\n"); 1491 return NULL; 1492 } 1493 1494 mac->dle_info.wde_pg_size = cfg->wde_size->pge_size; 1495 mac->dle_info.ple_pg_size = cfg->ple_size->pge_size; 1496 mac->dle_info.qta_mode = mode; 1497 mac->dle_info.c0_rx_qta = cfg->ple_min_qt->cma0_dma; 1498 mac->dle_info.c1_rx_qta = cfg->ple_min_qt->cma1_dma; 1499 1500 return cfg; 1501 } 1502 1503 static bool mac_is_txq_empty(struct rtw89_dev *rtwdev) 1504 { 1505 struct rtw89_mac_dle_dfi_qempty qempty; 1506 u32 qnum, qtmp, val32, msk32; 1507 int i, j, ret; 1508 1509 qnum = rtwdev->chip->wde_qempty_acq_num; 1510 qempty.dle_type = DLE_CTRL_TYPE_WDE; 1511 1512 for (i = 0; i < qnum; i++) { 1513 qempty.grpsel = i; 1514 ret = dle_dfi_qempty(rtwdev, &qempty); 1515 if (ret) { 1516 rtw89_warn(rtwdev, "dle dfi acq empty %d\n", ret); 1517 return false; 1518 } 1519 qtmp = qempty.qempty; 1520 for (j = 0 ; j < QEMP_ACQ_GRP_MACID_NUM; j++) { 1521 val32 = FIELD_GET(QEMP_ACQ_GRP_QSEL_MASK, qtmp); 1522 if (val32 != QEMP_ACQ_GRP_QSEL_MASK) 1523 return false; 1524 qtmp >>= QEMP_ACQ_GRP_QSEL_SH; 1525 } 1526 } 1527 1528 qempty.grpsel = rtwdev->chip->wde_qempty_mgq_sel; 1529 ret = dle_dfi_qempty(rtwdev, &qempty); 1530 if (ret) { 1531 rtw89_warn(rtwdev, "dle dfi mgq empty %d\n", ret); 1532 return false; 1533 } 1534 msk32 = B_CMAC0_MGQ_NORMAL | B_CMAC0_MGQ_NO_PWRSAV | B_CMAC0_CPUMGQ; 1535 if ((qempty.qempty & msk32) != msk32) 1536 return false; 1537 1538 if (rtwdev->dbcc_en) { 1539 msk32 |= B_CMAC1_MGQ_NORMAL | B_CMAC1_MGQ_NO_PWRSAV | B_CMAC1_CPUMGQ; 1540 if ((qempty.qempty & msk32) != msk32) 1541 return false; 1542 } 1543 1544 msk32 = B_AX_WDE_EMPTY_QTA_DMAC_WLAN_CPU | B_AX_WDE_EMPTY_QTA_DMAC_DATA_CPU | 1545 B_AX_PLE_EMPTY_QTA_DMAC_WLAN_CPU | B_AX_PLE_EMPTY_QTA_DMAC_H2C | 1546 B_AX_WDE_EMPTY_QUE_OTHERS | B_AX_PLE_EMPTY_QUE_DMAC_MPDU_TX | 1547 B_AX_WDE_EMPTY_QTA_DMAC_CPUIO | B_AX_PLE_EMPTY_QTA_DMAC_CPUIO | 1548 B_AX_WDE_EMPTY_QUE_DMAC_PKTIN | B_AX_WDE_EMPTY_QTA_DMAC_HIF | 1549 B_AX_PLE_EMPTY_QUE_DMAC_SEC_TX | B_AX_WDE_EMPTY_QTA_DMAC_PKTIN | 1550 B_AX_PLE_EMPTY_QTA_DMAC_B0_TXPL | B_AX_PLE_EMPTY_QTA_DMAC_B1_TXPL | 1551 B_AX_PLE_EMPTY_QTA_DMAC_MPDU_TX; 1552 val32 = rtw89_read32(rtwdev, R_AX_DLE_EMPTY0); 1553 1554 return (val32 & msk32) == msk32; 1555 } 1556 1557 static inline u32 dle_used_size(const struct rtw89_dle_size *wde, 1558 const struct rtw89_dle_size *ple) 1559 { 1560 return wde->pge_size * (wde->lnk_pge_num + wde->unlnk_pge_num) + 1561 ple->pge_size * (ple->lnk_pge_num + ple->unlnk_pge_num); 1562 } 1563 1564 static u32 dle_expected_used_size(struct rtw89_dev *rtwdev, 1565 enum rtw89_qta_mode mode) 1566 { 1567 u32 size = rtwdev->chip->fifo_size; 1568 1569 if (mode == RTW89_QTA_SCC) 1570 size -= rtwdev->chip->dle_scc_rsvd_size; 1571 1572 return size; 1573 } 1574 1575 static void dle_func_en(struct rtw89_dev *rtwdev, bool enable) 1576 { 1577 if (enable) 1578 rtw89_write32_set(rtwdev, R_AX_DMAC_FUNC_EN, 1579 B_AX_DLE_WDE_EN | B_AX_DLE_PLE_EN); 1580 else 1581 rtw89_write32_clr(rtwdev, R_AX_DMAC_FUNC_EN, 1582 B_AX_DLE_WDE_EN | B_AX_DLE_PLE_EN); 1583 } 1584 1585 static void dle_clk_en(struct rtw89_dev *rtwdev, bool enable) 1586 { 1587 if (enable) 1588 rtw89_write32_set(rtwdev, R_AX_DMAC_CLK_EN, 1589 B_AX_DLE_WDE_CLK_EN | B_AX_DLE_PLE_CLK_EN); 1590 else 1591 rtw89_write32_clr(rtwdev, R_AX_DMAC_CLK_EN, 1592 B_AX_DLE_WDE_CLK_EN | B_AX_DLE_PLE_CLK_EN); 1593 } 1594 1595 static int dle_mix_cfg(struct rtw89_dev *rtwdev, const struct rtw89_dle_mem *cfg) 1596 { 1597 const struct rtw89_dle_size *size_cfg; 1598 u32 val; 1599 u8 bound = 0; 1600 1601 val = rtw89_read32(rtwdev, R_AX_WDE_PKTBUF_CFG); 1602 size_cfg = cfg->wde_size; 1603 1604 switch (size_cfg->pge_size) { 1605 default: 1606 case RTW89_WDE_PG_64: 1607 val = u32_replace_bits(val, S_AX_WDE_PAGE_SEL_64, 1608 B_AX_WDE_PAGE_SEL_MASK); 1609 break; 1610 case RTW89_WDE_PG_128: 1611 val = u32_replace_bits(val, S_AX_WDE_PAGE_SEL_128, 1612 B_AX_WDE_PAGE_SEL_MASK); 1613 break; 1614 case RTW89_WDE_PG_256: 1615 rtw89_err(rtwdev, "[ERR]WDE DLE doesn't support 256 byte!\n"); 1616 return -EINVAL; 1617 } 1618 1619 val = u32_replace_bits(val, bound, B_AX_WDE_START_BOUND_MASK); 1620 val = u32_replace_bits(val, size_cfg->lnk_pge_num, 1621 B_AX_WDE_FREE_PAGE_NUM_MASK); 1622 rtw89_write32(rtwdev, R_AX_WDE_PKTBUF_CFG, val); 1623 1624 val = rtw89_read32(rtwdev, R_AX_PLE_PKTBUF_CFG); 1625 bound = (size_cfg->lnk_pge_num + size_cfg->unlnk_pge_num) 1626 * size_cfg->pge_size / DLE_BOUND_UNIT; 1627 size_cfg = cfg->ple_size; 1628 1629 switch (size_cfg->pge_size) { 1630 default: 1631 case RTW89_PLE_PG_64: 1632 rtw89_err(rtwdev, "[ERR]PLE DLE doesn't support 64 byte!\n"); 1633 return -EINVAL; 1634 case RTW89_PLE_PG_128: 1635 val = u32_replace_bits(val, S_AX_PLE_PAGE_SEL_128, 1636 B_AX_PLE_PAGE_SEL_MASK); 1637 break; 1638 case RTW89_PLE_PG_256: 1639 val = u32_replace_bits(val, S_AX_PLE_PAGE_SEL_256, 1640 B_AX_PLE_PAGE_SEL_MASK); 1641 break; 1642 } 1643 1644 val = u32_replace_bits(val, bound, B_AX_PLE_START_BOUND_MASK); 1645 val = u32_replace_bits(val, size_cfg->lnk_pge_num, 1646 B_AX_PLE_FREE_PAGE_NUM_MASK); 1647 rtw89_write32(rtwdev, R_AX_PLE_PKTBUF_CFG, val); 1648 1649 return 0; 1650 } 1651 1652 #define INVALID_QT_WCPU U16_MAX 1653 #define SET_QUOTA_VAL(_min_x, _max_x, _module, _idx) \ 1654 do { \ 1655 val = u32_encode_bits(_min_x, B_AX_ ## _module ## _MIN_SIZE_MASK) | \ 1656 u32_encode_bits(_max_x, B_AX_ ## _module ## _MAX_SIZE_MASK); \ 1657 rtw89_write32(rtwdev, \ 1658 R_AX_ ## _module ## _QTA ## _idx ## _CFG, \ 1659 val); \ 1660 } while (0) 1661 #define SET_QUOTA(_x, _module, _idx) \ 1662 SET_QUOTA_VAL(min_cfg->_x, max_cfg->_x, _module, _idx) 1663 1664 static void wde_quota_cfg(struct rtw89_dev *rtwdev, 1665 const struct rtw89_wde_quota *min_cfg, 1666 const struct rtw89_wde_quota *max_cfg, 1667 u16 ext_wde_min_qt_wcpu) 1668 { 1669 u16 min_qt_wcpu = ext_wde_min_qt_wcpu != INVALID_QT_WCPU ? 1670 ext_wde_min_qt_wcpu : min_cfg->wcpu; 1671 u32 val; 1672 1673 SET_QUOTA(hif, WDE, 0); 1674 SET_QUOTA_VAL(min_qt_wcpu, max_cfg->wcpu, WDE, 1); 1675 SET_QUOTA(pkt_in, WDE, 3); 1676 SET_QUOTA(cpu_io, WDE, 4); 1677 } 1678 1679 static void ple_quota_cfg(struct rtw89_dev *rtwdev, 1680 const struct rtw89_ple_quota *min_cfg, 1681 const struct rtw89_ple_quota *max_cfg) 1682 { 1683 u32 val; 1684 1685 SET_QUOTA(cma0_tx, PLE, 0); 1686 SET_QUOTA(cma1_tx, PLE, 1); 1687 SET_QUOTA(c2h, PLE, 2); 1688 SET_QUOTA(h2c, PLE, 3); 1689 SET_QUOTA(wcpu, PLE, 4); 1690 SET_QUOTA(mpdu_proc, PLE, 5); 1691 SET_QUOTA(cma0_dma, PLE, 6); 1692 SET_QUOTA(cma1_dma, PLE, 7); 1693 SET_QUOTA(bb_rpt, PLE, 8); 1694 SET_QUOTA(wd_rel, PLE, 9); 1695 SET_QUOTA(cpu_io, PLE, 10); 1696 if (rtwdev->chip->chip_id == RTL8852C) 1697 SET_QUOTA(tx_rpt, PLE, 11); 1698 } 1699 1700 int rtw89_mac_resize_ple_rx_quota(struct rtw89_dev *rtwdev, bool wow) 1701 { 1702 const struct rtw89_ple_quota *min_cfg, *max_cfg; 1703 const struct rtw89_dle_mem *cfg; 1704 u32 val; 1705 1706 if (rtwdev->chip->chip_id == RTL8852C) 1707 return 0; 1708 1709 if (rtwdev->mac.qta_mode != RTW89_QTA_SCC) { 1710 rtw89_err(rtwdev, "[ERR]support SCC mode only\n"); 1711 return -EINVAL; 1712 } 1713 1714 if (wow) 1715 cfg = get_dle_mem_cfg(rtwdev, RTW89_QTA_WOW); 1716 else 1717 cfg = get_dle_mem_cfg(rtwdev, RTW89_QTA_SCC); 1718 if (!cfg) { 1719 rtw89_err(rtwdev, "[ERR]get_dle_mem_cfg\n"); 1720 return -EINVAL; 1721 } 1722 1723 min_cfg = cfg->ple_min_qt; 1724 max_cfg = cfg->ple_max_qt; 1725 SET_QUOTA(cma0_dma, PLE, 6); 1726 SET_QUOTA(cma1_dma, PLE, 7); 1727 1728 return 0; 1729 } 1730 #undef SET_QUOTA 1731 1732 void rtw89_mac_hw_mgnt_sec(struct rtw89_dev *rtwdev, bool enable) 1733 { 1734 u32 msk32 = B_AX_UC_MGNT_DEC | B_AX_BMC_MGNT_DEC; 1735 1736 if (enable) 1737 rtw89_write32_set(rtwdev, R_AX_SEC_ENG_CTRL, msk32); 1738 else 1739 rtw89_write32_clr(rtwdev, R_AX_SEC_ENG_CTRL, msk32); 1740 } 1741 1742 static void dle_quota_cfg(struct rtw89_dev *rtwdev, 1743 const struct rtw89_dle_mem *cfg, 1744 u16 ext_wde_min_qt_wcpu) 1745 { 1746 wde_quota_cfg(rtwdev, cfg->wde_min_qt, cfg->wde_max_qt, ext_wde_min_qt_wcpu); 1747 ple_quota_cfg(rtwdev, cfg->ple_min_qt, cfg->ple_max_qt); 1748 } 1749 1750 static int dle_init(struct rtw89_dev *rtwdev, enum rtw89_qta_mode mode, 1751 enum rtw89_qta_mode ext_mode) 1752 { 1753 const struct rtw89_dle_mem *cfg, *ext_cfg; 1754 u16 ext_wde_min_qt_wcpu = INVALID_QT_WCPU; 1755 int ret = 0; 1756 u32 ini; 1757 1758 ret = rtw89_mac_check_mac_en(rtwdev, RTW89_MAC_0, RTW89_DMAC_SEL); 1759 if (ret) 1760 return ret; 1761 1762 cfg = get_dle_mem_cfg(rtwdev, mode); 1763 if (!cfg) { 1764 rtw89_err(rtwdev, "[ERR]get_dle_mem_cfg\n"); 1765 ret = -EINVAL; 1766 goto error; 1767 } 1768 1769 if (mode == RTW89_QTA_DLFW) { 1770 ext_cfg = get_dle_mem_cfg(rtwdev, ext_mode); 1771 if (!ext_cfg) { 1772 rtw89_err(rtwdev, "[ERR]get_dle_ext_mem_cfg %d\n", 1773 ext_mode); 1774 ret = -EINVAL; 1775 goto error; 1776 } 1777 ext_wde_min_qt_wcpu = ext_cfg->wde_min_qt->wcpu; 1778 } 1779 1780 if (dle_used_size(cfg->wde_size, cfg->ple_size) != 1781 dle_expected_used_size(rtwdev, mode)) { 1782 rtw89_err(rtwdev, "[ERR]wd/dle mem cfg\n"); 1783 ret = -EINVAL; 1784 goto error; 1785 } 1786 1787 dle_func_en(rtwdev, false); 1788 dle_clk_en(rtwdev, true); 1789 1790 ret = dle_mix_cfg(rtwdev, cfg); 1791 if (ret) { 1792 rtw89_err(rtwdev, "[ERR] dle mix cfg\n"); 1793 goto error; 1794 } 1795 dle_quota_cfg(rtwdev, cfg, ext_wde_min_qt_wcpu); 1796 1797 dle_func_en(rtwdev, true); 1798 1799 ret = read_poll_timeout(rtw89_read32, ini, 1800 (ini & WDE_MGN_INI_RDY) == WDE_MGN_INI_RDY, 1, 1801 2000, false, rtwdev, R_AX_WDE_INI_STATUS); 1802 if (ret) { 1803 rtw89_err(rtwdev, "[ERR]WDE cfg ready\n"); 1804 return ret; 1805 } 1806 1807 ret = read_poll_timeout(rtw89_read32, ini, 1808 (ini & WDE_MGN_INI_RDY) == WDE_MGN_INI_RDY, 1, 1809 2000, false, rtwdev, R_AX_PLE_INI_STATUS); 1810 if (ret) { 1811 rtw89_err(rtwdev, "[ERR]PLE cfg ready\n"); 1812 return ret; 1813 } 1814 1815 return 0; 1816 error: 1817 dle_func_en(rtwdev, false); 1818 rtw89_err(rtwdev, "[ERR]trxcfg wde 0x8900 = %x\n", 1819 rtw89_read32(rtwdev, R_AX_WDE_INI_STATUS)); 1820 rtw89_err(rtwdev, "[ERR]trxcfg ple 0x8D00 = %x\n", 1821 rtw89_read32(rtwdev, R_AX_PLE_INI_STATUS)); 1822 1823 return ret; 1824 } 1825 1826 static int preload_init_set(struct rtw89_dev *rtwdev, enum rtw89_mac_idx mac_idx, 1827 enum rtw89_qta_mode mode) 1828 { 1829 u32 reg, max_preld_size, min_rsvd_size; 1830 1831 max_preld_size = (mac_idx == RTW89_MAC_0 ? 1832 PRELD_B0_ENT_NUM : PRELD_B1_ENT_NUM) * PRELD_AMSDU_SIZE; 1833 reg = mac_idx == RTW89_MAC_0 ? 1834 R_AX_TXPKTCTL_B0_PRELD_CFG0 : R_AX_TXPKTCTL_B1_PRELD_CFG0; 1835 rtw89_write32_mask(rtwdev, reg, B_AX_B0_PRELD_USEMAXSZ_MASK, max_preld_size); 1836 rtw89_write32_set(rtwdev, reg, B_AX_B0_PRELD_FEN); 1837 1838 min_rsvd_size = PRELD_AMSDU_SIZE; 1839 reg = mac_idx == RTW89_MAC_0 ? 1840 R_AX_TXPKTCTL_B0_PRELD_CFG1 : R_AX_TXPKTCTL_B1_PRELD_CFG1; 1841 rtw89_write32_mask(rtwdev, reg, B_AX_B0_PRELD_NXT_TXENDWIN_MASK, PRELD_NEXT_WND); 1842 rtw89_write32_mask(rtwdev, reg, B_AX_B0_PRELD_NXT_RSVMINSZ_MASK, min_rsvd_size); 1843 1844 return 0; 1845 } 1846 1847 static bool is_qta_poh(struct rtw89_dev *rtwdev) 1848 { 1849 return rtwdev->hci.type == RTW89_HCI_TYPE_PCIE; 1850 } 1851 1852 static int preload_init(struct rtw89_dev *rtwdev, enum rtw89_mac_idx mac_idx, 1853 enum rtw89_qta_mode mode) 1854 { 1855 const struct rtw89_chip_info *chip = rtwdev->chip; 1856 1857 if (chip->chip_id == RTL8852A || chip->chip_id == RTL8852B || !is_qta_poh(rtwdev)) 1858 return 0; 1859 1860 return preload_init_set(rtwdev, mac_idx, mode); 1861 } 1862 1863 static bool dle_is_txq_empty(struct rtw89_dev *rtwdev) 1864 { 1865 u32 msk32; 1866 u32 val32; 1867 1868 msk32 = B_AX_WDE_EMPTY_QUE_CMAC0_ALL_AC | B_AX_WDE_EMPTY_QUE_CMAC0_MBH | 1869 B_AX_WDE_EMPTY_QUE_CMAC1_MBH | B_AX_WDE_EMPTY_QUE_CMAC0_WMM0 | 1870 B_AX_WDE_EMPTY_QUE_CMAC0_WMM1 | B_AX_WDE_EMPTY_QUE_OTHERS | 1871 B_AX_PLE_EMPTY_QUE_DMAC_MPDU_TX | B_AX_PLE_EMPTY_QTA_DMAC_H2C | 1872 B_AX_PLE_EMPTY_QUE_DMAC_SEC_TX | B_AX_WDE_EMPTY_QUE_DMAC_PKTIN | 1873 B_AX_WDE_EMPTY_QTA_DMAC_HIF | B_AX_WDE_EMPTY_QTA_DMAC_WLAN_CPU | 1874 B_AX_WDE_EMPTY_QTA_DMAC_PKTIN | B_AX_WDE_EMPTY_QTA_DMAC_CPUIO | 1875 B_AX_PLE_EMPTY_QTA_DMAC_B0_TXPL | 1876 B_AX_PLE_EMPTY_QTA_DMAC_B1_TXPL | 1877 B_AX_PLE_EMPTY_QTA_DMAC_MPDU_TX | 1878 B_AX_PLE_EMPTY_QTA_DMAC_CPUIO | 1879 B_AX_WDE_EMPTY_QTA_DMAC_DATA_CPU | 1880 B_AX_PLE_EMPTY_QTA_DMAC_WLAN_CPU; 1881 val32 = rtw89_read32(rtwdev, R_AX_DLE_EMPTY0); 1882 1883 if ((val32 & msk32) == msk32) 1884 return true; 1885 1886 return false; 1887 } 1888 1889 static void _patch_ss2f_path(struct rtw89_dev *rtwdev) 1890 { 1891 const struct rtw89_chip_info *chip = rtwdev->chip; 1892 1893 if (chip->chip_id == RTL8852A || chip->chip_id == RTL8852B) 1894 return; 1895 1896 rtw89_write32_mask(rtwdev, R_AX_SS2FINFO_PATH, B_AX_SS_DEST_QUEUE_MASK, 1897 SS2F_PATH_WLCPU); 1898 } 1899 1900 static int sta_sch_init(struct rtw89_dev *rtwdev) 1901 { 1902 u32 p_val; 1903 u8 val; 1904 int ret; 1905 1906 ret = rtw89_mac_check_mac_en(rtwdev, RTW89_MAC_0, RTW89_DMAC_SEL); 1907 if (ret) 1908 return ret; 1909 1910 val = rtw89_read8(rtwdev, R_AX_SS_CTRL); 1911 val |= B_AX_SS_EN; 1912 rtw89_write8(rtwdev, R_AX_SS_CTRL, val); 1913 1914 ret = read_poll_timeout(rtw89_read32, p_val, p_val & B_AX_SS_INIT_DONE_1, 1915 1, TRXCFG_WAIT_CNT, false, rtwdev, R_AX_SS_CTRL); 1916 if (ret) { 1917 rtw89_err(rtwdev, "[ERR]STA scheduler init\n"); 1918 return ret; 1919 } 1920 1921 rtw89_write32_set(rtwdev, R_AX_SS_CTRL, B_AX_SS_WARM_INIT_FLG); 1922 rtw89_write32_clr(rtwdev, R_AX_SS_CTRL, B_AX_SS_NONEMPTY_SS2FINFO_EN); 1923 1924 _patch_ss2f_path(rtwdev); 1925 1926 return 0; 1927 } 1928 1929 static int mpdu_proc_init(struct rtw89_dev *rtwdev) 1930 { 1931 int ret; 1932 1933 ret = rtw89_mac_check_mac_en(rtwdev, RTW89_MAC_0, RTW89_DMAC_SEL); 1934 if (ret) 1935 return ret; 1936 1937 rtw89_write32(rtwdev, R_AX_ACTION_FWD0, TRXCFG_MPDU_PROC_ACT_FRWD); 1938 rtw89_write32(rtwdev, R_AX_TF_FWD, TRXCFG_MPDU_PROC_TF_FRWD); 1939 rtw89_write32_set(rtwdev, R_AX_MPDU_PROC, 1940 B_AX_APPEND_FCS | B_AX_A_ICV_ERR); 1941 rtw89_write32(rtwdev, R_AX_CUT_AMSDU_CTRL, TRXCFG_MPDU_PROC_CUT_CTRL); 1942 1943 return 0; 1944 } 1945 1946 static int sec_eng_init(struct rtw89_dev *rtwdev) 1947 { 1948 const struct rtw89_chip_info *chip = rtwdev->chip; 1949 u32 val = 0; 1950 int ret; 1951 1952 ret = rtw89_mac_check_mac_en(rtwdev, RTW89_MAC_0, RTW89_DMAC_SEL); 1953 if (ret) 1954 return ret; 1955 1956 val = rtw89_read32(rtwdev, R_AX_SEC_ENG_CTRL); 1957 /* init clock */ 1958 val |= (B_AX_CLK_EN_CGCMP | B_AX_CLK_EN_WAPI | B_AX_CLK_EN_WEP_TKIP); 1959 /* init TX encryption */ 1960 val |= (B_AX_SEC_TX_ENC | B_AX_SEC_RX_DEC); 1961 val |= (B_AX_MC_DEC | B_AX_BC_DEC); 1962 if (chip->chip_id == RTL8852A || chip->chip_id == RTL8852B) 1963 val &= ~B_AX_TX_PARTIAL_MODE; 1964 rtw89_write32(rtwdev, R_AX_SEC_ENG_CTRL, val); 1965 1966 /* init MIC ICV append */ 1967 val = rtw89_read32(rtwdev, R_AX_SEC_MPDU_PROC); 1968 val |= (B_AX_APPEND_ICV | B_AX_APPEND_MIC); 1969 1970 /* option init */ 1971 rtw89_write32(rtwdev, R_AX_SEC_MPDU_PROC, val); 1972 1973 if (chip->chip_id == RTL8852C) 1974 rtw89_write32_mask(rtwdev, R_AX_SEC_DEBUG1, 1975 B_AX_TX_TIMEOUT_SEL_MASK, AX_TX_TO_VAL); 1976 1977 return 0; 1978 } 1979 1980 static int dmac_init(struct rtw89_dev *rtwdev, u8 mac_idx) 1981 { 1982 int ret; 1983 1984 ret = dle_init(rtwdev, rtwdev->mac.qta_mode, RTW89_QTA_INVALID); 1985 if (ret) { 1986 rtw89_err(rtwdev, "[ERR]DLE init %d\n", ret); 1987 return ret; 1988 } 1989 1990 ret = preload_init(rtwdev, RTW89_MAC_0, rtwdev->mac.qta_mode); 1991 if (ret) { 1992 rtw89_err(rtwdev, "[ERR]preload init %d\n", ret); 1993 return ret; 1994 } 1995 1996 ret = hfc_init(rtwdev, true, true, true); 1997 if (ret) { 1998 rtw89_err(rtwdev, "[ERR]HCI FC init %d\n", ret); 1999 return ret; 2000 } 2001 2002 ret = sta_sch_init(rtwdev); 2003 if (ret) { 2004 rtw89_err(rtwdev, "[ERR]STA SCH init %d\n", ret); 2005 return ret; 2006 } 2007 2008 ret = mpdu_proc_init(rtwdev); 2009 if (ret) { 2010 rtw89_err(rtwdev, "[ERR]MPDU Proc init %d\n", ret); 2011 return ret; 2012 } 2013 2014 ret = sec_eng_init(rtwdev); 2015 if (ret) { 2016 rtw89_err(rtwdev, "[ERR]Security Engine init %d\n", ret); 2017 return ret; 2018 } 2019 2020 return ret; 2021 } 2022 2023 static int addr_cam_init(struct rtw89_dev *rtwdev, u8 mac_idx) 2024 { 2025 u32 val, reg; 2026 u16 p_val; 2027 int ret; 2028 2029 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL); 2030 if (ret) 2031 return ret; 2032 2033 reg = rtw89_mac_reg_by_idx(R_AX_ADDR_CAM_CTRL, mac_idx); 2034 2035 val = rtw89_read32(rtwdev, reg); 2036 val |= u32_encode_bits(0x7f, B_AX_ADDR_CAM_RANGE_MASK) | 2037 B_AX_ADDR_CAM_CLR | B_AX_ADDR_CAM_EN; 2038 rtw89_write32(rtwdev, reg, val); 2039 2040 ret = read_poll_timeout(rtw89_read16, p_val, !(p_val & B_AX_ADDR_CAM_CLR), 2041 1, TRXCFG_WAIT_CNT, false, rtwdev, reg); 2042 if (ret) { 2043 rtw89_err(rtwdev, "[ERR]ADDR_CAM reset\n"); 2044 return ret; 2045 } 2046 2047 return 0; 2048 } 2049 2050 static int scheduler_init(struct rtw89_dev *rtwdev, u8 mac_idx) 2051 { 2052 u32 ret; 2053 u32 reg; 2054 u32 val; 2055 2056 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL); 2057 if (ret) 2058 return ret; 2059 2060 reg = rtw89_mac_reg_by_idx(R_AX_PREBKF_CFG_1, mac_idx); 2061 if (rtwdev->chip->chip_id == RTL8852C) 2062 rtw89_write32_mask(rtwdev, reg, B_AX_SIFS_MACTXEN_T1_MASK, 2063 SIFS_MACTXEN_T1_V1); 2064 else 2065 rtw89_write32_mask(rtwdev, reg, B_AX_SIFS_MACTXEN_T1_MASK, 2066 SIFS_MACTXEN_T1); 2067 2068 if (rtwdev->chip->chip_id == RTL8852B) { 2069 reg = rtw89_mac_reg_by_idx(R_AX_SCH_EXT_CTRL, mac_idx); 2070 rtw89_write32_set(rtwdev, reg, B_AX_PORT_RST_TSF_ADV); 2071 } 2072 2073 reg = rtw89_mac_reg_by_idx(R_AX_CCA_CFG_0, mac_idx); 2074 rtw89_write32_clr(rtwdev, reg, B_AX_BTCCA_EN); 2075 2076 reg = rtw89_mac_reg_by_idx(R_AX_PREBKF_CFG_0, mac_idx); 2077 if (rtwdev->chip->chip_id == RTL8852C) { 2078 val = rtw89_read32_mask(rtwdev, R_AX_SEC_ENG_CTRL, 2079 B_AX_TX_PARTIAL_MODE); 2080 if (!val) 2081 rtw89_write32_mask(rtwdev, reg, B_AX_PREBKF_TIME_MASK, 2082 SCH_PREBKF_24US); 2083 } else { 2084 rtw89_write32_mask(rtwdev, reg, B_AX_PREBKF_TIME_MASK, 2085 SCH_PREBKF_24US); 2086 } 2087 2088 return 0; 2089 } 2090 2091 int rtw89_mac_typ_fltr_opt(struct rtw89_dev *rtwdev, 2092 enum rtw89_machdr_frame_type type, 2093 enum rtw89_mac_fwd_target fwd_target, 2094 u8 mac_idx) 2095 { 2096 u32 reg; 2097 u32 val; 2098 2099 switch (fwd_target) { 2100 case RTW89_FWD_DONT_CARE: 2101 val = RX_FLTR_FRAME_DROP; 2102 break; 2103 case RTW89_FWD_TO_HOST: 2104 val = RX_FLTR_FRAME_TO_HOST; 2105 break; 2106 case RTW89_FWD_TO_WLAN_CPU: 2107 val = RX_FLTR_FRAME_TO_WLCPU; 2108 break; 2109 default: 2110 rtw89_err(rtwdev, "[ERR]set rx filter fwd target err\n"); 2111 return -EINVAL; 2112 } 2113 2114 switch (type) { 2115 case RTW89_MGNT: 2116 reg = rtw89_mac_reg_by_idx(R_AX_MGNT_FLTR, mac_idx); 2117 break; 2118 case RTW89_CTRL: 2119 reg = rtw89_mac_reg_by_idx(R_AX_CTRL_FLTR, mac_idx); 2120 break; 2121 case RTW89_DATA: 2122 reg = rtw89_mac_reg_by_idx(R_AX_DATA_FLTR, mac_idx); 2123 break; 2124 default: 2125 rtw89_err(rtwdev, "[ERR]set rx filter type err\n"); 2126 return -EINVAL; 2127 } 2128 rtw89_write32(rtwdev, reg, val); 2129 2130 return 0; 2131 } 2132 2133 static int rx_fltr_init(struct rtw89_dev *rtwdev, u8 mac_idx) 2134 { 2135 int ret, i; 2136 u32 mac_ftlr, plcp_ftlr; 2137 2138 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL); 2139 if (ret) 2140 return ret; 2141 2142 for (i = RTW89_MGNT; i <= RTW89_DATA; i++) { 2143 ret = rtw89_mac_typ_fltr_opt(rtwdev, i, RTW89_FWD_TO_HOST, 2144 mac_idx); 2145 if (ret) 2146 return ret; 2147 } 2148 mac_ftlr = rtwdev->hal.rx_fltr; 2149 plcp_ftlr = B_AX_CCK_CRC_CHK | B_AX_CCK_SIG_CHK | 2150 B_AX_LSIG_PARITY_CHK_EN | B_AX_SIGA_CRC_CHK | 2151 B_AX_VHT_SU_SIGB_CRC_CHK | B_AX_VHT_MU_SIGB_CRC_CHK | 2152 B_AX_HE_SIGB_CRC_CHK; 2153 rtw89_write32(rtwdev, rtw89_mac_reg_by_idx(R_AX_RX_FLTR_OPT, mac_idx), 2154 mac_ftlr); 2155 rtw89_write16(rtwdev, rtw89_mac_reg_by_idx(R_AX_PLCP_HDR_FLTR, mac_idx), 2156 plcp_ftlr); 2157 2158 return 0; 2159 } 2160 2161 static void _patch_dis_resp_chk(struct rtw89_dev *rtwdev, u8 mac_idx) 2162 { 2163 u32 reg, val32; 2164 u32 b_rsp_chk_nav, b_rsp_chk_cca; 2165 2166 b_rsp_chk_nav = B_AX_RSP_CHK_TXNAV | B_AX_RSP_CHK_INTRA_NAV | 2167 B_AX_RSP_CHK_BASIC_NAV; 2168 b_rsp_chk_cca = B_AX_RSP_CHK_SEC_CCA_80 | B_AX_RSP_CHK_SEC_CCA_40 | 2169 B_AX_RSP_CHK_SEC_CCA_20 | B_AX_RSP_CHK_BTCCA | 2170 B_AX_RSP_CHK_EDCCA | B_AX_RSP_CHK_CCA; 2171 2172 switch (rtwdev->chip->chip_id) { 2173 case RTL8852A: 2174 case RTL8852B: 2175 reg = rtw89_mac_reg_by_idx(R_AX_RSP_CHK_SIG, mac_idx); 2176 val32 = rtw89_read32(rtwdev, reg) & ~b_rsp_chk_nav; 2177 rtw89_write32(rtwdev, reg, val32); 2178 2179 reg = rtw89_mac_reg_by_idx(R_AX_TRXPTCL_RESP_0, mac_idx); 2180 val32 = rtw89_read32(rtwdev, reg) & ~b_rsp_chk_cca; 2181 rtw89_write32(rtwdev, reg, val32); 2182 break; 2183 default: 2184 reg = rtw89_mac_reg_by_idx(R_AX_RSP_CHK_SIG, mac_idx); 2185 val32 = rtw89_read32(rtwdev, reg) | b_rsp_chk_nav; 2186 rtw89_write32(rtwdev, reg, val32); 2187 2188 reg = rtw89_mac_reg_by_idx(R_AX_TRXPTCL_RESP_0, mac_idx); 2189 val32 = rtw89_read32(rtwdev, reg) | b_rsp_chk_cca; 2190 rtw89_write32(rtwdev, reg, val32); 2191 break; 2192 } 2193 } 2194 2195 static int cca_ctrl_init(struct rtw89_dev *rtwdev, u8 mac_idx) 2196 { 2197 u32 val, reg; 2198 int ret; 2199 2200 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL); 2201 if (ret) 2202 return ret; 2203 2204 reg = rtw89_mac_reg_by_idx(R_AX_CCA_CONTROL, mac_idx); 2205 val = rtw89_read32(rtwdev, reg); 2206 val |= (B_AX_TB_CHK_BASIC_NAV | B_AX_TB_CHK_BTCCA | 2207 B_AX_TB_CHK_EDCCA | B_AX_TB_CHK_CCA_P20 | 2208 B_AX_SIFS_CHK_BTCCA | B_AX_SIFS_CHK_CCA_P20 | 2209 B_AX_CTN_CHK_INTRA_NAV | 2210 B_AX_CTN_CHK_BASIC_NAV | B_AX_CTN_CHK_BTCCA | 2211 B_AX_CTN_CHK_EDCCA | B_AX_CTN_CHK_CCA_S80 | 2212 B_AX_CTN_CHK_CCA_S40 | B_AX_CTN_CHK_CCA_S20 | 2213 B_AX_CTN_CHK_CCA_P20); 2214 val &= ~(B_AX_TB_CHK_TX_NAV | B_AX_TB_CHK_CCA_S80 | 2215 B_AX_TB_CHK_CCA_S40 | B_AX_TB_CHK_CCA_S20 | 2216 B_AX_SIFS_CHK_CCA_S80 | B_AX_SIFS_CHK_CCA_S40 | 2217 B_AX_SIFS_CHK_CCA_S20 | B_AX_CTN_CHK_TXNAV | 2218 B_AX_SIFS_CHK_EDCCA); 2219 2220 rtw89_write32(rtwdev, reg, val); 2221 2222 _patch_dis_resp_chk(rtwdev, mac_idx); 2223 2224 return 0; 2225 } 2226 2227 static int nav_ctrl_init(struct rtw89_dev *rtwdev) 2228 { 2229 rtw89_write32_set(rtwdev, R_AX_WMAC_NAV_CTL, B_AX_WMAC_PLCP_UP_NAV_EN | 2230 B_AX_WMAC_TF_UP_NAV_EN | 2231 B_AX_WMAC_NAV_UPPER_EN); 2232 rtw89_write32_mask(rtwdev, R_AX_WMAC_NAV_CTL, B_AX_WMAC_NAV_UPPER_MASK, NAV_25MS); 2233 2234 return 0; 2235 } 2236 2237 static int spatial_reuse_init(struct rtw89_dev *rtwdev, u8 mac_idx) 2238 { 2239 u32 reg; 2240 int ret; 2241 2242 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL); 2243 if (ret) 2244 return ret; 2245 reg = rtw89_mac_reg_by_idx(R_AX_RX_SR_CTRL, mac_idx); 2246 rtw89_write8_clr(rtwdev, reg, B_AX_SR_EN); 2247 2248 return 0; 2249 } 2250 2251 static int tmac_init(struct rtw89_dev *rtwdev, u8 mac_idx) 2252 { 2253 u32 reg; 2254 int ret; 2255 2256 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL); 2257 if (ret) 2258 return ret; 2259 2260 reg = rtw89_mac_reg_by_idx(R_AX_MAC_LOOPBACK, mac_idx); 2261 rtw89_write32_clr(rtwdev, reg, B_AX_MACLBK_EN); 2262 2263 reg = rtw89_mac_reg_by_idx(R_AX_TCR0, mac_idx); 2264 rtw89_write32_mask(rtwdev, reg, B_AX_TCR_UDF_THSD_MASK, TCR_UDF_THSD); 2265 2266 reg = rtw89_mac_reg_by_idx(R_AX_TXD_FIFO_CTRL, mac_idx); 2267 rtw89_write32_mask(rtwdev, reg, B_AX_TXDFIFO_HIGH_MCS_THRE_MASK, TXDFIFO_HIGH_MCS_THRE); 2268 rtw89_write32_mask(rtwdev, reg, B_AX_TXDFIFO_LOW_MCS_THRE_MASK, TXDFIFO_LOW_MCS_THRE); 2269 2270 return 0; 2271 } 2272 2273 static int trxptcl_init(struct rtw89_dev *rtwdev, u8 mac_idx) 2274 { 2275 const struct rtw89_chip_info *chip = rtwdev->chip; 2276 const struct rtw89_rrsr_cfgs *rrsr = chip->rrsr_cfgs; 2277 u32 reg, val, sifs; 2278 int ret; 2279 2280 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL); 2281 if (ret) 2282 return ret; 2283 2284 reg = rtw89_mac_reg_by_idx(R_AX_TRXPTCL_RESP_0, mac_idx); 2285 val = rtw89_read32(rtwdev, reg); 2286 val &= ~B_AX_WMAC_SPEC_SIFS_CCK_MASK; 2287 val |= FIELD_PREP(B_AX_WMAC_SPEC_SIFS_CCK_MASK, WMAC_SPEC_SIFS_CCK); 2288 2289 switch (rtwdev->chip->chip_id) { 2290 case RTL8852A: 2291 sifs = WMAC_SPEC_SIFS_OFDM_52A; 2292 break; 2293 case RTL8852B: 2294 sifs = WMAC_SPEC_SIFS_OFDM_52B; 2295 break; 2296 default: 2297 sifs = WMAC_SPEC_SIFS_OFDM_52C; 2298 break; 2299 } 2300 val &= ~B_AX_WMAC_SPEC_SIFS_OFDM_MASK; 2301 val |= FIELD_PREP(B_AX_WMAC_SPEC_SIFS_OFDM_MASK, sifs); 2302 rtw89_write32(rtwdev, reg, val); 2303 2304 reg = rtw89_mac_reg_by_idx(R_AX_RXTRIG_TEST_USER_2, mac_idx); 2305 rtw89_write32_set(rtwdev, reg, B_AX_RXTRIG_FCSCHK_EN); 2306 2307 reg = rtw89_mac_reg_by_idx(rrsr->ref_rate.addr, mac_idx); 2308 rtw89_write32_mask(rtwdev, reg, rrsr->ref_rate.mask, rrsr->ref_rate.data); 2309 reg = rtw89_mac_reg_by_idx(rrsr->rsc.addr, mac_idx); 2310 rtw89_write32_mask(rtwdev, reg, rrsr->rsc.mask, rrsr->rsc.data); 2311 2312 return 0; 2313 } 2314 2315 static void rst_bacam(struct rtw89_dev *rtwdev) 2316 { 2317 u32 val32; 2318 int ret; 2319 2320 rtw89_write32_mask(rtwdev, R_AX_RESPBA_CAM_CTRL, B_AX_BACAM_RST_MASK, 2321 S_AX_BACAM_RST_ALL); 2322 2323 ret = read_poll_timeout_atomic(rtw89_read32_mask, val32, val32 == 0, 2324 1, 1000, false, 2325 rtwdev, R_AX_RESPBA_CAM_CTRL, B_AX_BACAM_RST_MASK); 2326 if (ret) 2327 rtw89_warn(rtwdev, "failed to reset BA CAM\n"); 2328 } 2329 2330 static int rmac_init(struct rtw89_dev *rtwdev, u8 mac_idx) 2331 { 2332 #define TRXCFG_RMAC_CCA_TO 32 2333 #define TRXCFG_RMAC_DATA_TO 15 2334 #define RX_MAX_LEN_UNIT 512 2335 #define PLD_RLS_MAX_PG 127 2336 #define RX_SPEC_MAX_LEN (11454 + RX_MAX_LEN_UNIT) 2337 int ret; 2338 u32 reg, rx_max_len, rx_qta; 2339 u16 val; 2340 2341 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL); 2342 if (ret) 2343 return ret; 2344 2345 if (mac_idx == RTW89_MAC_0) 2346 rst_bacam(rtwdev); 2347 2348 reg = rtw89_mac_reg_by_idx(R_AX_RESPBA_CAM_CTRL, mac_idx); 2349 rtw89_write8_set(rtwdev, reg, B_AX_SSN_SEL); 2350 2351 reg = rtw89_mac_reg_by_idx(R_AX_DLK_PROTECT_CTL, mac_idx); 2352 val = rtw89_read16(rtwdev, reg); 2353 val = u16_replace_bits(val, TRXCFG_RMAC_DATA_TO, 2354 B_AX_RX_DLK_DATA_TIME_MASK); 2355 val = u16_replace_bits(val, TRXCFG_RMAC_CCA_TO, 2356 B_AX_RX_DLK_CCA_TIME_MASK); 2357 rtw89_write16(rtwdev, reg, val); 2358 2359 reg = rtw89_mac_reg_by_idx(R_AX_RCR, mac_idx); 2360 rtw89_write8_mask(rtwdev, reg, B_AX_CH_EN_MASK, 0x1); 2361 2362 reg = rtw89_mac_reg_by_idx(R_AX_RX_FLTR_OPT, mac_idx); 2363 if (mac_idx == RTW89_MAC_0) 2364 rx_qta = rtwdev->mac.dle_info.c0_rx_qta; 2365 else 2366 rx_qta = rtwdev->mac.dle_info.c1_rx_qta; 2367 rx_qta = min_t(u32, rx_qta, PLD_RLS_MAX_PG); 2368 rx_max_len = rx_qta * rtwdev->mac.dle_info.ple_pg_size; 2369 rx_max_len = min_t(u32, rx_max_len, RX_SPEC_MAX_LEN); 2370 rx_max_len /= RX_MAX_LEN_UNIT; 2371 rtw89_write32_mask(rtwdev, reg, B_AX_RX_MPDU_MAX_LEN_MASK, rx_max_len); 2372 2373 if (rtwdev->chip->chip_id == RTL8852A && 2374 rtwdev->hal.cv == CHIP_CBV) { 2375 rtw89_write16_mask(rtwdev, 2376 rtw89_mac_reg_by_idx(R_AX_DLK_PROTECT_CTL, mac_idx), 2377 B_AX_RX_DLK_CCA_TIME_MASK, 0); 2378 rtw89_write16_set(rtwdev, rtw89_mac_reg_by_idx(R_AX_RCR, mac_idx), 2379 BIT(12)); 2380 } 2381 2382 reg = rtw89_mac_reg_by_idx(R_AX_PLCP_HDR_FLTR, mac_idx); 2383 rtw89_write8_clr(rtwdev, reg, B_AX_VHT_SU_SIGB_CRC_CHK); 2384 2385 return ret; 2386 } 2387 2388 static int cmac_com_init(struct rtw89_dev *rtwdev, u8 mac_idx) 2389 { 2390 enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id; 2391 u32 val, reg; 2392 int ret; 2393 2394 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL); 2395 if (ret) 2396 return ret; 2397 2398 reg = rtw89_mac_reg_by_idx(R_AX_TX_SUB_CARRIER_VALUE, mac_idx); 2399 val = rtw89_read32(rtwdev, reg); 2400 val = u32_replace_bits(val, 0, B_AX_TXSC_20M_MASK); 2401 val = u32_replace_bits(val, 0, B_AX_TXSC_40M_MASK); 2402 val = u32_replace_bits(val, 0, B_AX_TXSC_80M_MASK); 2403 rtw89_write32(rtwdev, reg, val); 2404 2405 if (chip_id == RTL8852A || chip_id == RTL8852B) { 2406 reg = rtw89_mac_reg_by_idx(R_AX_PTCL_RRSR1, mac_idx); 2407 rtw89_write32_mask(rtwdev, reg, B_AX_RRSR_RATE_EN_MASK, RRSR_OFDM_CCK_EN); 2408 } 2409 2410 return 0; 2411 } 2412 2413 static bool is_qta_dbcc(struct rtw89_dev *rtwdev, enum rtw89_qta_mode mode) 2414 { 2415 const struct rtw89_dle_mem *cfg; 2416 2417 cfg = get_dle_mem_cfg(rtwdev, mode); 2418 if (!cfg) { 2419 rtw89_err(rtwdev, "[ERR]get_dle_mem_cfg\n"); 2420 return false; 2421 } 2422 2423 return (cfg->ple_min_qt->cma1_dma && cfg->ple_max_qt->cma1_dma); 2424 } 2425 2426 static int ptcl_init(struct rtw89_dev *rtwdev, u8 mac_idx) 2427 { 2428 u32 val, reg; 2429 int ret; 2430 2431 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL); 2432 if (ret) 2433 return ret; 2434 2435 if (rtwdev->hci.type == RTW89_HCI_TYPE_PCIE) { 2436 reg = rtw89_mac_reg_by_idx(R_AX_SIFS_SETTING, mac_idx); 2437 val = rtw89_read32(rtwdev, reg); 2438 val = u32_replace_bits(val, S_AX_CTS2S_TH_1K, 2439 B_AX_HW_CTS2SELF_PKT_LEN_TH_MASK); 2440 val = u32_replace_bits(val, S_AX_CTS2S_TH_SEC_256B, 2441 B_AX_HW_CTS2SELF_PKT_LEN_TH_TWW_MASK); 2442 val |= B_AX_HW_CTS2SELF_EN; 2443 rtw89_write32(rtwdev, reg, val); 2444 2445 reg = rtw89_mac_reg_by_idx(R_AX_PTCL_FSM_MON, mac_idx); 2446 val = rtw89_read32(rtwdev, reg); 2447 val = u32_replace_bits(val, S_AX_PTCL_TO_2MS, B_AX_PTCL_TX_ARB_TO_THR_MASK); 2448 val &= ~B_AX_PTCL_TX_ARB_TO_MODE; 2449 rtw89_write32(rtwdev, reg, val); 2450 } 2451 2452 if (mac_idx == RTW89_MAC_0) { 2453 rtw89_write8_set(rtwdev, R_AX_PTCL_COMMON_SETTING_0, 2454 B_AX_CMAC_TX_MODE_0 | B_AX_CMAC_TX_MODE_1); 2455 rtw89_write8_clr(rtwdev, R_AX_PTCL_COMMON_SETTING_0, 2456 B_AX_PTCL_TRIGGER_SS_EN_0 | 2457 B_AX_PTCL_TRIGGER_SS_EN_1 | 2458 B_AX_PTCL_TRIGGER_SS_EN_UL); 2459 rtw89_write8_mask(rtwdev, R_AX_PTCLRPT_FULL_HDL, 2460 B_AX_SPE_RPT_PATH_MASK, FWD_TO_WLCPU); 2461 } else if (mac_idx == RTW89_MAC_1) { 2462 rtw89_write8_mask(rtwdev, R_AX_PTCLRPT_FULL_HDL_C1, 2463 B_AX_SPE_RPT_PATH_MASK, FWD_TO_WLCPU); 2464 } 2465 2466 return 0; 2467 } 2468 2469 static int cmac_dma_init(struct rtw89_dev *rtwdev, u8 mac_idx) 2470 { 2471 enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id; 2472 u32 reg; 2473 int ret; 2474 2475 if (chip_id != RTL8852A && chip_id != RTL8852B) 2476 return 0; 2477 2478 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL); 2479 if (ret) 2480 return ret; 2481 2482 reg = rtw89_mac_reg_by_idx(R_AX_RXDMA_CTRL_0, mac_idx); 2483 rtw89_write8_clr(rtwdev, reg, RX_FULL_MODE); 2484 2485 return 0; 2486 } 2487 2488 static int cmac_init(struct rtw89_dev *rtwdev, u8 mac_idx) 2489 { 2490 int ret; 2491 2492 ret = scheduler_init(rtwdev, mac_idx); 2493 if (ret) { 2494 rtw89_err(rtwdev, "[ERR]CMAC%d SCH init %d\n", mac_idx, ret); 2495 return ret; 2496 } 2497 2498 ret = addr_cam_init(rtwdev, mac_idx); 2499 if (ret) { 2500 rtw89_err(rtwdev, "[ERR]CMAC%d ADDR_CAM reset %d\n", mac_idx, 2501 ret); 2502 return ret; 2503 } 2504 2505 ret = rx_fltr_init(rtwdev, mac_idx); 2506 if (ret) { 2507 rtw89_err(rtwdev, "[ERR]CMAC%d RX filter init %d\n", mac_idx, 2508 ret); 2509 return ret; 2510 } 2511 2512 ret = cca_ctrl_init(rtwdev, mac_idx); 2513 if (ret) { 2514 rtw89_err(rtwdev, "[ERR]CMAC%d CCA CTRL init %d\n", mac_idx, 2515 ret); 2516 return ret; 2517 } 2518 2519 ret = nav_ctrl_init(rtwdev); 2520 if (ret) { 2521 rtw89_err(rtwdev, "[ERR]CMAC%d NAV CTRL init %d\n", mac_idx, 2522 ret); 2523 return ret; 2524 } 2525 2526 ret = spatial_reuse_init(rtwdev, mac_idx); 2527 if (ret) { 2528 rtw89_err(rtwdev, "[ERR]CMAC%d Spatial Reuse init %d\n", 2529 mac_idx, ret); 2530 return ret; 2531 } 2532 2533 ret = tmac_init(rtwdev, mac_idx); 2534 if (ret) { 2535 rtw89_err(rtwdev, "[ERR]CMAC%d TMAC init %d\n", mac_idx, ret); 2536 return ret; 2537 } 2538 2539 ret = trxptcl_init(rtwdev, mac_idx); 2540 if (ret) { 2541 rtw89_err(rtwdev, "[ERR]CMAC%d TRXPTCL init %d\n", mac_idx, ret); 2542 return ret; 2543 } 2544 2545 ret = rmac_init(rtwdev, mac_idx); 2546 if (ret) { 2547 rtw89_err(rtwdev, "[ERR]CMAC%d RMAC init %d\n", mac_idx, ret); 2548 return ret; 2549 } 2550 2551 ret = cmac_com_init(rtwdev, mac_idx); 2552 if (ret) { 2553 rtw89_err(rtwdev, "[ERR]CMAC%d Com init %d\n", mac_idx, ret); 2554 return ret; 2555 } 2556 2557 ret = ptcl_init(rtwdev, mac_idx); 2558 if (ret) { 2559 rtw89_err(rtwdev, "[ERR]CMAC%d PTCL init %d\n", mac_idx, ret); 2560 return ret; 2561 } 2562 2563 ret = cmac_dma_init(rtwdev, mac_idx); 2564 if (ret) { 2565 rtw89_err(rtwdev, "[ERR]CMAC%d DMA init %d\n", mac_idx, ret); 2566 return ret; 2567 } 2568 2569 return ret; 2570 } 2571 2572 static int rtw89_mac_read_phycap(struct rtw89_dev *rtwdev, 2573 struct rtw89_mac_c2h_info *c2h_info) 2574 { 2575 struct rtw89_mac_h2c_info h2c_info = {0}; 2576 u32 ret; 2577 2578 h2c_info.id = RTW89_FWCMD_H2CREG_FUNC_GET_FEATURE; 2579 h2c_info.content_len = 0; 2580 2581 ret = rtw89_fw_msg_reg(rtwdev, &h2c_info, c2h_info); 2582 if (ret) 2583 return ret; 2584 2585 if (c2h_info->id != RTW89_FWCMD_C2HREG_FUNC_PHY_CAP) 2586 return -EINVAL; 2587 2588 return 0; 2589 } 2590 2591 int rtw89_mac_setup_phycap(struct rtw89_dev *rtwdev) 2592 { 2593 struct rtw89_hal *hal = &rtwdev->hal; 2594 const struct rtw89_chip_info *chip = rtwdev->chip; 2595 struct rtw89_mac_c2h_info c2h_info = {0}; 2596 u8 tx_nss; 2597 u8 rx_nss; 2598 u8 tx_ant; 2599 u8 rx_ant; 2600 u32 ret; 2601 2602 ret = rtw89_mac_read_phycap(rtwdev, &c2h_info); 2603 if (ret) 2604 return ret; 2605 2606 tx_nss = RTW89_GET_C2H_PHYCAP_TX_NSS(c2h_info.c2hreg); 2607 rx_nss = RTW89_GET_C2H_PHYCAP_RX_NSS(c2h_info.c2hreg); 2608 tx_ant = RTW89_GET_C2H_PHYCAP_ANT_TX_NUM(c2h_info.c2hreg); 2609 rx_ant = RTW89_GET_C2H_PHYCAP_ANT_RX_NUM(c2h_info.c2hreg); 2610 2611 hal->tx_nss = tx_nss ? min_t(u8, tx_nss, chip->tx_nss) : chip->tx_nss; 2612 hal->rx_nss = rx_nss ? min_t(u8, rx_nss, chip->rx_nss) : chip->rx_nss; 2613 2614 if (tx_ant == 1) 2615 hal->antenna_tx = RF_B; 2616 if (rx_ant == 1) 2617 hal->antenna_rx = RF_B; 2618 2619 if (tx_nss == 1 && tx_ant == 2 && rx_ant == 2) { 2620 hal->antenna_tx = RF_B; 2621 hal->tx_path_diversity = true; 2622 } 2623 2624 rtw89_debug(rtwdev, RTW89_DBG_FW, 2625 "phycap hal/phy/chip: tx_nss=0x%x/0x%x/0x%x rx_nss=0x%x/0x%x/0x%x\n", 2626 hal->tx_nss, tx_nss, chip->tx_nss, 2627 hal->rx_nss, rx_nss, chip->rx_nss); 2628 rtw89_debug(rtwdev, RTW89_DBG_FW, 2629 "ant num/bitmap: tx=%d/0x%x rx=%d/0x%x\n", 2630 tx_ant, hal->antenna_tx, rx_ant, hal->antenna_rx); 2631 rtw89_debug(rtwdev, RTW89_DBG_FW, "TX path diversity=%d\n", hal->tx_path_diversity); 2632 2633 return 0; 2634 } 2635 2636 static int rtw89_hw_sch_tx_en_h2c(struct rtw89_dev *rtwdev, u8 band, 2637 u16 tx_en_u16, u16 mask_u16) 2638 { 2639 u32 ret; 2640 struct rtw89_mac_c2h_info c2h_info = {0}; 2641 struct rtw89_mac_h2c_info h2c_info = {0}; 2642 struct rtw89_h2creg_sch_tx_en *h2creg = 2643 (struct rtw89_h2creg_sch_tx_en *)h2c_info.h2creg; 2644 2645 h2c_info.id = RTW89_FWCMD_H2CREG_FUNC_SCH_TX_EN; 2646 h2c_info.content_len = sizeof(*h2creg) - RTW89_H2CREG_HDR_LEN; 2647 h2creg->tx_en = tx_en_u16; 2648 h2creg->mask = mask_u16; 2649 h2creg->band = band; 2650 2651 ret = rtw89_fw_msg_reg(rtwdev, &h2c_info, &c2h_info); 2652 if (ret) 2653 return ret; 2654 2655 if (c2h_info.id != RTW89_FWCMD_C2HREG_FUNC_TX_PAUSE_RPT) 2656 return -EINVAL; 2657 2658 return 0; 2659 } 2660 2661 static int rtw89_set_hw_sch_tx_en(struct rtw89_dev *rtwdev, u8 mac_idx, 2662 u16 tx_en, u16 tx_en_mask) 2663 { 2664 u32 reg = rtw89_mac_reg_by_idx(R_AX_CTN_TXEN, mac_idx); 2665 u16 val; 2666 int ret; 2667 2668 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL); 2669 if (ret) 2670 return ret; 2671 2672 if (test_bit(RTW89_FLAG_FW_RDY, rtwdev->flags)) 2673 return rtw89_hw_sch_tx_en_h2c(rtwdev, mac_idx, 2674 tx_en, tx_en_mask); 2675 2676 val = rtw89_read16(rtwdev, reg); 2677 val = (val & ~tx_en_mask) | (tx_en & tx_en_mask); 2678 rtw89_write16(rtwdev, reg, val); 2679 2680 return 0; 2681 } 2682 2683 static int rtw89_set_hw_sch_tx_en_v1(struct rtw89_dev *rtwdev, u8 mac_idx, 2684 u32 tx_en, u32 tx_en_mask) 2685 { 2686 u32 reg = rtw89_mac_reg_by_idx(R_AX_CTN_DRV_TXEN, mac_idx); 2687 u32 val; 2688 int ret; 2689 2690 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL); 2691 if (ret) 2692 return ret; 2693 2694 val = rtw89_read32(rtwdev, reg); 2695 val = (val & ~tx_en_mask) | (tx_en & tx_en_mask); 2696 rtw89_write32(rtwdev, reg, val); 2697 2698 return 0; 2699 } 2700 2701 int rtw89_mac_stop_sch_tx(struct rtw89_dev *rtwdev, u8 mac_idx, 2702 u32 *tx_en, enum rtw89_sch_tx_sel sel) 2703 { 2704 int ret; 2705 2706 *tx_en = rtw89_read16(rtwdev, 2707 rtw89_mac_reg_by_idx(R_AX_CTN_TXEN, mac_idx)); 2708 2709 switch (sel) { 2710 case RTW89_SCH_TX_SEL_ALL: 2711 ret = rtw89_set_hw_sch_tx_en(rtwdev, mac_idx, 0, 2712 B_AX_CTN_TXEN_ALL_MASK); 2713 if (ret) 2714 return ret; 2715 break; 2716 case RTW89_SCH_TX_SEL_HIQ: 2717 ret = rtw89_set_hw_sch_tx_en(rtwdev, mac_idx, 2718 0, B_AX_CTN_TXEN_HGQ); 2719 if (ret) 2720 return ret; 2721 break; 2722 case RTW89_SCH_TX_SEL_MG0: 2723 ret = rtw89_set_hw_sch_tx_en(rtwdev, mac_idx, 2724 0, B_AX_CTN_TXEN_MGQ); 2725 if (ret) 2726 return ret; 2727 break; 2728 case RTW89_SCH_TX_SEL_MACID: 2729 ret = rtw89_set_hw_sch_tx_en(rtwdev, mac_idx, 0, 2730 B_AX_CTN_TXEN_ALL_MASK); 2731 if (ret) 2732 return ret; 2733 break; 2734 default: 2735 return 0; 2736 } 2737 2738 return 0; 2739 } 2740 EXPORT_SYMBOL(rtw89_mac_stop_sch_tx); 2741 2742 int rtw89_mac_stop_sch_tx_v1(struct rtw89_dev *rtwdev, u8 mac_idx, 2743 u32 *tx_en, enum rtw89_sch_tx_sel sel) 2744 { 2745 int ret; 2746 2747 *tx_en = rtw89_read32(rtwdev, 2748 rtw89_mac_reg_by_idx(R_AX_CTN_DRV_TXEN, mac_idx)); 2749 2750 switch (sel) { 2751 case RTW89_SCH_TX_SEL_ALL: 2752 ret = rtw89_set_hw_sch_tx_en_v1(rtwdev, mac_idx, 0, 2753 B_AX_CTN_TXEN_ALL_MASK_V1); 2754 if (ret) 2755 return ret; 2756 break; 2757 case RTW89_SCH_TX_SEL_HIQ: 2758 ret = rtw89_set_hw_sch_tx_en_v1(rtwdev, mac_idx, 2759 0, B_AX_CTN_TXEN_HGQ); 2760 if (ret) 2761 return ret; 2762 break; 2763 case RTW89_SCH_TX_SEL_MG0: 2764 ret = rtw89_set_hw_sch_tx_en_v1(rtwdev, mac_idx, 2765 0, B_AX_CTN_TXEN_MGQ); 2766 if (ret) 2767 return ret; 2768 break; 2769 case RTW89_SCH_TX_SEL_MACID: 2770 ret = rtw89_set_hw_sch_tx_en_v1(rtwdev, mac_idx, 0, 2771 B_AX_CTN_TXEN_ALL_MASK_V1); 2772 if (ret) 2773 return ret; 2774 break; 2775 default: 2776 return 0; 2777 } 2778 2779 return 0; 2780 } 2781 EXPORT_SYMBOL(rtw89_mac_stop_sch_tx_v1); 2782 2783 int rtw89_mac_resume_sch_tx(struct rtw89_dev *rtwdev, u8 mac_idx, u32 tx_en) 2784 { 2785 int ret; 2786 2787 ret = rtw89_set_hw_sch_tx_en(rtwdev, mac_idx, tx_en, B_AX_CTN_TXEN_ALL_MASK); 2788 if (ret) 2789 return ret; 2790 2791 return 0; 2792 } 2793 EXPORT_SYMBOL(rtw89_mac_resume_sch_tx); 2794 2795 int rtw89_mac_resume_sch_tx_v1(struct rtw89_dev *rtwdev, u8 mac_idx, u32 tx_en) 2796 { 2797 int ret; 2798 2799 ret = rtw89_set_hw_sch_tx_en_v1(rtwdev, mac_idx, tx_en, 2800 B_AX_CTN_TXEN_ALL_MASK_V1); 2801 if (ret) 2802 return ret; 2803 2804 return 0; 2805 } 2806 EXPORT_SYMBOL(rtw89_mac_resume_sch_tx_v1); 2807 2808 u16 rtw89_mac_dle_buf_req(struct rtw89_dev *rtwdev, u16 buf_len, bool wd) 2809 { 2810 u32 val, reg; 2811 int ret; 2812 2813 reg = wd ? R_AX_WD_BUF_REQ : R_AX_PL_BUF_REQ; 2814 val = buf_len; 2815 val |= B_AX_WD_BUF_REQ_EXEC; 2816 rtw89_write32(rtwdev, reg, val); 2817 2818 reg = wd ? R_AX_WD_BUF_STATUS : R_AX_PL_BUF_STATUS; 2819 2820 ret = read_poll_timeout(rtw89_read32, val, val & B_AX_WD_BUF_STAT_DONE, 2821 1, 2000, false, rtwdev, reg); 2822 if (ret) 2823 return 0xffff; 2824 2825 return FIELD_GET(B_AX_WD_BUF_STAT_PKTID_MASK, val); 2826 } 2827 2828 int rtw89_mac_set_cpuio(struct rtw89_dev *rtwdev, 2829 struct rtw89_cpuio_ctrl *ctrl_para, bool wd) 2830 { 2831 u32 val, cmd_type, reg; 2832 int ret; 2833 2834 cmd_type = ctrl_para->cmd_type; 2835 2836 reg = wd ? R_AX_WD_CPUQ_OP_2 : R_AX_PL_CPUQ_OP_2; 2837 val = 0; 2838 val = u32_replace_bits(val, ctrl_para->start_pktid, 2839 B_AX_WD_CPUQ_OP_STRT_PKTID_MASK); 2840 val = u32_replace_bits(val, ctrl_para->end_pktid, 2841 B_AX_WD_CPUQ_OP_END_PKTID_MASK); 2842 rtw89_write32(rtwdev, reg, val); 2843 2844 reg = wd ? R_AX_WD_CPUQ_OP_1 : R_AX_PL_CPUQ_OP_1; 2845 val = 0; 2846 val = u32_replace_bits(val, ctrl_para->src_pid, 2847 B_AX_CPUQ_OP_SRC_PID_MASK); 2848 val = u32_replace_bits(val, ctrl_para->src_qid, 2849 B_AX_CPUQ_OP_SRC_QID_MASK); 2850 val = u32_replace_bits(val, ctrl_para->dst_pid, 2851 B_AX_CPUQ_OP_DST_PID_MASK); 2852 val = u32_replace_bits(val, ctrl_para->dst_qid, 2853 B_AX_CPUQ_OP_DST_QID_MASK); 2854 rtw89_write32(rtwdev, reg, val); 2855 2856 reg = wd ? R_AX_WD_CPUQ_OP_0 : R_AX_PL_CPUQ_OP_0; 2857 val = 0; 2858 val = u32_replace_bits(val, cmd_type, 2859 B_AX_CPUQ_OP_CMD_TYPE_MASK); 2860 val = u32_replace_bits(val, ctrl_para->macid, 2861 B_AX_CPUQ_OP_MACID_MASK); 2862 val = u32_replace_bits(val, ctrl_para->pkt_num, 2863 B_AX_CPUQ_OP_PKTNUM_MASK); 2864 val |= B_AX_WD_CPUQ_OP_EXEC; 2865 rtw89_write32(rtwdev, reg, val); 2866 2867 reg = wd ? R_AX_WD_CPUQ_OP_STATUS : R_AX_PL_CPUQ_OP_STATUS; 2868 2869 ret = read_poll_timeout(rtw89_read32, val, val & B_AX_WD_CPUQ_OP_STAT_DONE, 2870 1, 2000, false, rtwdev, reg); 2871 if (ret) 2872 return ret; 2873 2874 if (cmd_type == CPUIO_OP_CMD_GET_1ST_PID || 2875 cmd_type == CPUIO_OP_CMD_GET_NEXT_PID) 2876 ctrl_para->pktid = FIELD_GET(B_AX_WD_CPUQ_OP_PKTID_MASK, val); 2877 2878 return 0; 2879 } 2880 2881 static int dle_quota_change(struct rtw89_dev *rtwdev, enum rtw89_qta_mode mode) 2882 { 2883 const struct rtw89_dle_mem *cfg; 2884 struct rtw89_cpuio_ctrl ctrl_para = {0}; 2885 u16 pkt_id; 2886 int ret; 2887 2888 cfg = get_dle_mem_cfg(rtwdev, mode); 2889 if (!cfg) { 2890 rtw89_err(rtwdev, "[ERR]wd/dle mem cfg\n"); 2891 return -EINVAL; 2892 } 2893 2894 if (dle_used_size(cfg->wde_size, cfg->ple_size) != 2895 dle_expected_used_size(rtwdev, mode)) { 2896 rtw89_err(rtwdev, "[ERR]wd/dle mem cfg\n"); 2897 return -EINVAL; 2898 } 2899 2900 dle_quota_cfg(rtwdev, cfg, INVALID_QT_WCPU); 2901 2902 pkt_id = rtw89_mac_dle_buf_req(rtwdev, 0x20, true); 2903 if (pkt_id == 0xffff) { 2904 rtw89_err(rtwdev, "[ERR]WDE DLE buf req\n"); 2905 return -ENOMEM; 2906 } 2907 2908 ctrl_para.cmd_type = CPUIO_OP_CMD_ENQ_TO_HEAD; 2909 ctrl_para.start_pktid = pkt_id; 2910 ctrl_para.end_pktid = pkt_id; 2911 ctrl_para.pkt_num = 0; 2912 ctrl_para.dst_pid = WDE_DLE_PORT_ID_WDRLS; 2913 ctrl_para.dst_qid = WDE_DLE_QUEID_NO_REPORT; 2914 ret = rtw89_mac_set_cpuio(rtwdev, &ctrl_para, true); 2915 if (ret) { 2916 rtw89_err(rtwdev, "[ERR]WDE DLE enqueue to head\n"); 2917 return -EFAULT; 2918 } 2919 2920 pkt_id = rtw89_mac_dle_buf_req(rtwdev, 0x20, false); 2921 if (pkt_id == 0xffff) { 2922 rtw89_err(rtwdev, "[ERR]PLE DLE buf req\n"); 2923 return -ENOMEM; 2924 } 2925 2926 ctrl_para.cmd_type = CPUIO_OP_CMD_ENQ_TO_HEAD; 2927 ctrl_para.start_pktid = pkt_id; 2928 ctrl_para.end_pktid = pkt_id; 2929 ctrl_para.pkt_num = 0; 2930 ctrl_para.dst_pid = PLE_DLE_PORT_ID_PLRLS; 2931 ctrl_para.dst_qid = PLE_DLE_QUEID_NO_REPORT; 2932 ret = rtw89_mac_set_cpuio(rtwdev, &ctrl_para, false); 2933 if (ret) { 2934 rtw89_err(rtwdev, "[ERR]PLE DLE enqueue to head\n"); 2935 return -EFAULT; 2936 } 2937 2938 return 0; 2939 } 2940 2941 static int band_idle_ck_b(struct rtw89_dev *rtwdev, u8 mac_idx) 2942 { 2943 int ret; 2944 u32 reg; 2945 u8 val; 2946 2947 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL); 2948 if (ret) 2949 return ret; 2950 2951 reg = rtw89_mac_reg_by_idx(R_AX_PTCL_TX_CTN_SEL, mac_idx); 2952 2953 ret = read_poll_timeout(rtw89_read8, val, 2954 (val & B_AX_PTCL_TX_ON_STAT) == 0, 2955 SW_CVR_DUR_US, 2956 SW_CVR_DUR_US * PTCL_IDLE_POLL_CNT, 2957 false, rtwdev, reg); 2958 if (ret) 2959 return ret; 2960 2961 return 0; 2962 } 2963 2964 static int band1_enable(struct rtw89_dev *rtwdev) 2965 { 2966 int ret, i; 2967 u32 sleep_bak[4] = {0}; 2968 u32 pause_bak[4] = {0}; 2969 u32 tx_en; 2970 2971 ret = rtw89_chip_stop_sch_tx(rtwdev, 0, &tx_en, RTW89_SCH_TX_SEL_ALL); 2972 if (ret) { 2973 rtw89_err(rtwdev, "[ERR]stop sch tx %d\n", ret); 2974 return ret; 2975 } 2976 2977 for (i = 0; i < 4; i++) { 2978 sleep_bak[i] = rtw89_read32(rtwdev, R_AX_MACID_SLEEP_0 + i * 4); 2979 pause_bak[i] = rtw89_read32(rtwdev, R_AX_SS_MACID_PAUSE_0 + i * 4); 2980 rtw89_write32(rtwdev, R_AX_MACID_SLEEP_0 + i * 4, U32_MAX); 2981 rtw89_write32(rtwdev, R_AX_SS_MACID_PAUSE_0 + i * 4, U32_MAX); 2982 } 2983 2984 ret = band_idle_ck_b(rtwdev, 0); 2985 if (ret) { 2986 rtw89_err(rtwdev, "[ERR]tx idle poll %d\n", ret); 2987 return ret; 2988 } 2989 2990 ret = dle_quota_change(rtwdev, rtwdev->mac.qta_mode); 2991 if (ret) { 2992 rtw89_err(rtwdev, "[ERR]DLE quota change %d\n", ret); 2993 return ret; 2994 } 2995 2996 for (i = 0; i < 4; i++) { 2997 rtw89_write32(rtwdev, R_AX_MACID_SLEEP_0 + i * 4, sleep_bak[i]); 2998 rtw89_write32(rtwdev, R_AX_SS_MACID_PAUSE_0 + i * 4, pause_bak[i]); 2999 } 3000 3001 ret = rtw89_chip_resume_sch_tx(rtwdev, 0, tx_en); 3002 if (ret) { 3003 rtw89_err(rtwdev, "[ERR]CMAC1 resume sch tx %d\n", ret); 3004 return ret; 3005 } 3006 3007 ret = cmac_func_en(rtwdev, 1, true); 3008 if (ret) { 3009 rtw89_err(rtwdev, "[ERR]CMAC1 func en %d\n", ret); 3010 return ret; 3011 } 3012 3013 ret = cmac_init(rtwdev, 1); 3014 if (ret) { 3015 rtw89_err(rtwdev, "[ERR]CMAC1 init %d\n", ret); 3016 return ret; 3017 } 3018 3019 rtw89_write32_set(rtwdev, R_AX_SYS_ISO_CTRL_EXTEND, 3020 B_AX_R_SYM_FEN_WLBBFUN_1 | B_AX_R_SYM_FEN_WLBBGLB_1); 3021 3022 return 0; 3023 } 3024 3025 static void rtw89_wdrls_imr_enable(struct rtw89_dev *rtwdev) 3026 { 3027 const struct rtw89_imr_info *imr = rtwdev->chip->imr_info; 3028 3029 rtw89_write32_clr(rtwdev, R_AX_WDRLS_ERR_IMR, B_AX_WDRLS_IMR_EN_CLR); 3030 rtw89_write32_set(rtwdev, R_AX_WDRLS_ERR_IMR, imr->wdrls_imr_set); 3031 } 3032 3033 static void rtw89_wsec_imr_enable(struct rtw89_dev *rtwdev) 3034 { 3035 const struct rtw89_imr_info *imr = rtwdev->chip->imr_info; 3036 3037 rtw89_write32_set(rtwdev, imr->wsec_imr_reg, imr->wsec_imr_set); 3038 } 3039 3040 static void rtw89_mpdu_trx_imr_enable(struct rtw89_dev *rtwdev) 3041 { 3042 enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id; 3043 const struct rtw89_imr_info *imr = rtwdev->chip->imr_info; 3044 3045 rtw89_write32_clr(rtwdev, R_AX_MPDU_TX_ERR_IMR, 3046 B_AX_TX_GET_ERRPKTID_INT_EN | 3047 B_AX_TX_NXT_ERRPKTID_INT_EN | 3048 B_AX_TX_MPDU_SIZE_ZERO_INT_EN | 3049 B_AX_TX_OFFSET_ERR_INT_EN | 3050 B_AX_TX_HDR3_SIZE_ERR_INT_EN); 3051 if (chip_id == RTL8852C) 3052 rtw89_write32_clr(rtwdev, R_AX_MPDU_TX_ERR_IMR, 3053 B_AX_TX_ETH_TYPE_ERR_EN | 3054 B_AX_TX_LLC_PRE_ERR_EN | 3055 B_AX_TX_NW_TYPE_ERR_EN | 3056 B_AX_TX_KSRCH_ERR_EN); 3057 rtw89_write32_set(rtwdev, R_AX_MPDU_TX_ERR_IMR, 3058 imr->mpdu_tx_imr_set); 3059 3060 rtw89_write32_clr(rtwdev, R_AX_MPDU_RX_ERR_IMR, 3061 B_AX_GETPKTID_ERR_INT_EN | 3062 B_AX_MHDRLEN_ERR_INT_EN | 3063 B_AX_RPT_ERR_INT_EN); 3064 rtw89_write32_set(rtwdev, R_AX_MPDU_RX_ERR_IMR, 3065 imr->mpdu_rx_imr_set); 3066 } 3067 3068 static void rtw89_sta_sch_imr_enable(struct rtw89_dev *rtwdev) 3069 { 3070 const struct rtw89_imr_info *imr = rtwdev->chip->imr_info; 3071 3072 rtw89_write32_clr(rtwdev, R_AX_STA_SCHEDULER_ERR_IMR, 3073 B_AX_SEARCH_HANG_TIMEOUT_INT_EN | 3074 B_AX_RPT_HANG_TIMEOUT_INT_EN | 3075 B_AX_PLE_B_PKTID_ERR_INT_EN); 3076 rtw89_write32_set(rtwdev, R_AX_STA_SCHEDULER_ERR_IMR, 3077 imr->sta_sch_imr_set); 3078 } 3079 3080 static void rtw89_txpktctl_imr_enable(struct rtw89_dev *rtwdev) 3081 { 3082 const struct rtw89_imr_info *imr = rtwdev->chip->imr_info; 3083 3084 rtw89_write32_clr(rtwdev, imr->txpktctl_imr_b0_reg, 3085 imr->txpktctl_imr_b0_clr); 3086 rtw89_write32_set(rtwdev, imr->txpktctl_imr_b0_reg, 3087 imr->txpktctl_imr_b0_set); 3088 rtw89_write32_clr(rtwdev, imr->txpktctl_imr_b1_reg, 3089 imr->txpktctl_imr_b1_clr); 3090 rtw89_write32_set(rtwdev, imr->txpktctl_imr_b1_reg, 3091 imr->txpktctl_imr_b1_set); 3092 } 3093 3094 static void rtw89_wde_imr_enable(struct rtw89_dev *rtwdev) 3095 { 3096 const struct rtw89_imr_info *imr = rtwdev->chip->imr_info; 3097 3098 rtw89_write32_clr(rtwdev, R_AX_WDE_ERR_IMR, imr->wde_imr_clr); 3099 rtw89_write32_set(rtwdev, R_AX_WDE_ERR_IMR, imr->wde_imr_set); 3100 } 3101 3102 static void rtw89_ple_imr_enable(struct rtw89_dev *rtwdev) 3103 { 3104 const struct rtw89_imr_info *imr = rtwdev->chip->imr_info; 3105 3106 rtw89_write32_clr(rtwdev, R_AX_PLE_ERR_IMR, imr->ple_imr_clr); 3107 rtw89_write32_set(rtwdev, R_AX_PLE_ERR_IMR, imr->ple_imr_set); 3108 } 3109 3110 static void rtw89_pktin_imr_enable(struct rtw89_dev *rtwdev) 3111 { 3112 rtw89_write32_set(rtwdev, R_AX_PKTIN_ERR_IMR, 3113 B_AX_PKTIN_GETPKTID_ERR_INT_EN); 3114 } 3115 3116 static void rtw89_dispatcher_imr_enable(struct rtw89_dev *rtwdev) 3117 { 3118 const struct rtw89_imr_info *imr = rtwdev->chip->imr_info; 3119 3120 rtw89_write32_clr(rtwdev, R_AX_HOST_DISPATCHER_ERR_IMR, 3121 imr->host_disp_imr_clr); 3122 rtw89_write32_set(rtwdev, R_AX_HOST_DISPATCHER_ERR_IMR, 3123 imr->host_disp_imr_set); 3124 rtw89_write32_clr(rtwdev, R_AX_CPU_DISPATCHER_ERR_IMR, 3125 imr->cpu_disp_imr_clr); 3126 rtw89_write32_set(rtwdev, R_AX_CPU_DISPATCHER_ERR_IMR, 3127 imr->cpu_disp_imr_set); 3128 rtw89_write32_clr(rtwdev, R_AX_OTHER_DISPATCHER_ERR_IMR, 3129 imr->other_disp_imr_clr); 3130 rtw89_write32_set(rtwdev, R_AX_OTHER_DISPATCHER_ERR_IMR, 3131 imr->other_disp_imr_set); 3132 } 3133 3134 static void rtw89_cpuio_imr_enable(struct rtw89_dev *rtwdev) 3135 { 3136 rtw89_write32_clr(rtwdev, R_AX_CPUIO_ERR_IMR, B_AX_CPUIO_IMR_CLR); 3137 rtw89_write32_set(rtwdev, R_AX_CPUIO_ERR_IMR, B_AX_CPUIO_IMR_SET); 3138 } 3139 3140 static void rtw89_bbrpt_imr_enable(struct rtw89_dev *rtwdev) 3141 { 3142 const struct rtw89_imr_info *imr = rtwdev->chip->imr_info; 3143 3144 rtw89_write32_set(rtwdev, imr->bbrpt_com_err_imr_reg, 3145 B_AX_BBRPT_COM_NULL_PLPKTID_ERR_INT_EN); 3146 rtw89_write32_clr(rtwdev, imr->bbrpt_chinfo_err_imr_reg, 3147 B_AX_BBRPT_CHINFO_IMR_CLR); 3148 rtw89_write32_set(rtwdev, imr->bbrpt_chinfo_err_imr_reg, 3149 imr->bbrpt_err_imr_set); 3150 rtw89_write32_set(rtwdev, imr->bbrpt_dfs_err_imr_reg, 3151 B_AX_BBRPT_DFS_TO_ERR_INT_EN); 3152 rtw89_write32_set(rtwdev, R_AX_LA_ERRFLAG, B_AX_LA_IMR_DATA_LOSS_ERR); 3153 } 3154 3155 static void rtw89_scheduler_imr_enable(struct rtw89_dev *rtwdev, u8 mac_idx) 3156 { 3157 u32 reg; 3158 3159 reg = rtw89_mac_reg_by_idx(R_AX_SCHEDULE_ERR_IMR, mac_idx); 3160 rtw89_write32_clr(rtwdev, reg, B_AX_SORT_NON_IDLE_ERR_INT_EN | 3161 B_AX_FSM_TIMEOUT_ERR_INT_EN); 3162 rtw89_write32_set(rtwdev, reg, B_AX_FSM_TIMEOUT_ERR_INT_EN); 3163 } 3164 3165 static void rtw89_ptcl_imr_enable(struct rtw89_dev *rtwdev, u8 mac_idx) 3166 { 3167 const struct rtw89_imr_info *imr = rtwdev->chip->imr_info; 3168 u32 reg; 3169 3170 reg = rtw89_mac_reg_by_idx(R_AX_PTCL_IMR0, mac_idx); 3171 rtw89_write32_clr(rtwdev, reg, imr->ptcl_imr_clr); 3172 rtw89_write32_set(rtwdev, reg, imr->ptcl_imr_set); 3173 } 3174 3175 static void rtw89_cdma_imr_enable(struct rtw89_dev *rtwdev, u8 mac_idx) 3176 { 3177 const struct rtw89_imr_info *imr = rtwdev->chip->imr_info; 3178 enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id; 3179 u32 reg; 3180 3181 reg = rtw89_mac_reg_by_idx(imr->cdma_imr_0_reg, mac_idx); 3182 rtw89_write32_clr(rtwdev, reg, imr->cdma_imr_0_clr); 3183 rtw89_write32_set(rtwdev, reg, imr->cdma_imr_0_set); 3184 3185 if (chip_id == RTL8852C) { 3186 reg = rtw89_mac_reg_by_idx(imr->cdma_imr_1_reg, mac_idx); 3187 rtw89_write32_clr(rtwdev, reg, imr->cdma_imr_1_clr); 3188 rtw89_write32_set(rtwdev, reg, imr->cdma_imr_1_set); 3189 } 3190 } 3191 3192 static void rtw89_phy_intf_imr_enable(struct rtw89_dev *rtwdev, u8 mac_idx) 3193 { 3194 const struct rtw89_imr_info *imr = rtwdev->chip->imr_info; 3195 u32 reg; 3196 3197 reg = rtw89_mac_reg_by_idx(imr->phy_intf_imr_reg, mac_idx); 3198 rtw89_write32_clr(rtwdev, reg, imr->phy_intf_imr_clr); 3199 rtw89_write32_set(rtwdev, reg, imr->phy_intf_imr_set); 3200 } 3201 3202 static void rtw89_rmac_imr_enable(struct rtw89_dev *rtwdev, u8 mac_idx) 3203 { 3204 const struct rtw89_imr_info *imr = rtwdev->chip->imr_info; 3205 u32 reg; 3206 3207 reg = rtw89_mac_reg_by_idx(imr->rmac_imr_reg, mac_idx); 3208 rtw89_write32_clr(rtwdev, reg, imr->rmac_imr_clr); 3209 rtw89_write32_set(rtwdev, reg, imr->rmac_imr_set); 3210 } 3211 3212 static void rtw89_tmac_imr_enable(struct rtw89_dev *rtwdev, u8 mac_idx) 3213 { 3214 const struct rtw89_imr_info *imr = rtwdev->chip->imr_info; 3215 u32 reg; 3216 3217 reg = rtw89_mac_reg_by_idx(imr->tmac_imr_reg, mac_idx); 3218 rtw89_write32_clr(rtwdev, reg, imr->tmac_imr_clr); 3219 rtw89_write32_set(rtwdev, reg, imr->tmac_imr_set); 3220 } 3221 3222 static int rtw89_mac_enable_imr(struct rtw89_dev *rtwdev, u8 mac_idx, 3223 enum rtw89_mac_hwmod_sel sel) 3224 { 3225 int ret; 3226 3227 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, sel); 3228 if (ret) { 3229 rtw89_err(rtwdev, "MAC%d mac_idx%d is not ready\n", 3230 sel, mac_idx); 3231 return ret; 3232 } 3233 3234 if (sel == RTW89_DMAC_SEL) { 3235 rtw89_wdrls_imr_enable(rtwdev); 3236 rtw89_wsec_imr_enable(rtwdev); 3237 rtw89_mpdu_trx_imr_enable(rtwdev); 3238 rtw89_sta_sch_imr_enable(rtwdev); 3239 rtw89_txpktctl_imr_enable(rtwdev); 3240 rtw89_wde_imr_enable(rtwdev); 3241 rtw89_ple_imr_enable(rtwdev); 3242 rtw89_pktin_imr_enable(rtwdev); 3243 rtw89_dispatcher_imr_enable(rtwdev); 3244 rtw89_cpuio_imr_enable(rtwdev); 3245 rtw89_bbrpt_imr_enable(rtwdev); 3246 } else if (sel == RTW89_CMAC_SEL) { 3247 rtw89_scheduler_imr_enable(rtwdev, mac_idx); 3248 rtw89_ptcl_imr_enable(rtwdev, mac_idx); 3249 rtw89_cdma_imr_enable(rtwdev, mac_idx); 3250 rtw89_phy_intf_imr_enable(rtwdev, mac_idx); 3251 rtw89_rmac_imr_enable(rtwdev, mac_idx); 3252 rtw89_tmac_imr_enable(rtwdev, mac_idx); 3253 } else { 3254 return -EINVAL; 3255 } 3256 3257 return 0; 3258 } 3259 3260 static void rtw89_mac_err_imr_ctrl(struct rtw89_dev *rtwdev, bool en) 3261 { 3262 enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id; 3263 3264 rtw89_write32(rtwdev, R_AX_DMAC_ERR_IMR, 3265 en ? DMAC_ERR_IMR_EN : DMAC_ERR_IMR_DIS); 3266 rtw89_write32(rtwdev, R_AX_CMAC_ERR_IMR, 3267 en ? CMAC0_ERR_IMR_EN : CMAC0_ERR_IMR_DIS); 3268 if (chip_id != RTL8852B && rtwdev->mac.dle_info.c1_rx_qta) 3269 rtw89_write32(rtwdev, R_AX_CMAC_ERR_IMR_C1, 3270 en ? CMAC1_ERR_IMR_EN : CMAC1_ERR_IMR_DIS); 3271 } 3272 3273 static int rtw89_mac_dbcc_enable(struct rtw89_dev *rtwdev, bool enable) 3274 { 3275 int ret = 0; 3276 3277 if (enable) { 3278 ret = band1_enable(rtwdev); 3279 if (ret) { 3280 rtw89_err(rtwdev, "[ERR] band1_enable %d\n", ret); 3281 return ret; 3282 } 3283 3284 ret = rtw89_mac_enable_imr(rtwdev, RTW89_MAC_1, RTW89_CMAC_SEL); 3285 if (ret) { 3286 rtw89_err(rtwdev, "[ERR] enable CMAC1 IMR %d\n", ret); 3287 return ret; 3288 } 3289 } else { 3290 rtw89_err(rtwdev, "[ERR] disable dbcc is not implemented not\n"); 3291 return -EINVAL; 3292 } 3293 3294 return 0; 3295 } 3296 3297 static int set_host_rpr(struct rtw89_dev *rtwdev) 3298 { 3299 if (rtwdev->hci.type == RTW89_HCI_TYPE_PCIE) { 3300 rtw89_write32_mask(rtwdev, R_AX_WDRLS_CFG, 3301 B_AX_WDRLS_MODE_MASK, RTW89_RPR_MODE_POH); 3302 rtw89_write32_set(rtwdev, R_AX_RLSRPT0_CFG0, 3303 B_AX_RLSRPT0_FLTR_MAP_MASK); 3304 } else { 3305 rtw89_write32_mask(rtwdev, R_AX_WDRLS_CFG, 3306 B_AX_WDRLS_MODE_MASK, RTW89_RPR_MODE_STF); 3307 rtw89_write32_clr(rtwdev, R_AX_RLSRPT0_CFG0, 3308 B_AX_RLSRPT0_FLTR_MAP_MASK); 3309 } 3310 3311 rtw89_write32_mask(rtwdev, R_AX_RLSRPT0_CFG1, B_AX_RLSRPT0_AGGNUM_MASK, 30); 3312 rtw89_write32_mask(rtwdev, R_AX_RLSRPT0_CFG1, B_AX_RLSRPT0_TO_MASK, 255); 3313 3314 return 0; 3315 } 3316 3317 static int rtw89_mac_trx_init(struct rtw89_dev *rtwdev) 3318 { 3319 enum rtw89_qta_mode qta_mode = rtwdev->mac.qta_mode; 3320 int ret; 3321 3322 ret = dmac_init(rtwdev, 0); 3323 if (ret) { 3324 rtw89_err(rtwdev, "[ERR]DMAC init %d\n", ret); 3325 return ret; 3326 } 3327 3328 ret = cmac_init(rtwdev, 0); 3329 if (ret) { 3330 rtw89_err(rtwdev, "[ERR]CMAC%d init %d\n", 0, ret); 3331 return ret; 3332 } 3333 3334 if (is_qta_dbcc(rtwdev, qta_mode)) { 3335 ret = rtw89_mac_dbcc_enable(rtwdev, true); 3336 if (ret) { 3337 rtw89_err(rtwdev, "[ERR]dbcc_enable init %d\n", ret); 3338 return ret; 3339 } 3340 } 3341 3342 ret = rtw89_mac_enable_imr(rtwdev, RTW89_MAC_0, RTW89_DMAC_SEL); 3343 if (ret) { 3344 rtw89_err(rtwdev, "[ERR] enable DMAC IMR %d\n", ret); 3345 return ret; 3346 } 3347 3348 ret = rtw89_mac_enable_imr(rtwdev, RTW89_MAC_0, RTW89_CMAC_SEL); 3349 if (ret) { 3350 rtw89_err(rtwdev, "[ERR] to enable CMAC0 IMR %d\n", ret); 3351 return ret; 3352 } 3353 3354 rtw89_mac_err_imr_ctrl(rtwdev, true); 3355 3356 ret = set_host_rpr(rtwdev); 3357 if (ret) { 3358 rtw89_err(rtwdev, "[ERR] set host rpr %d\n", ret); 3359 return ret; 3360 } 3361 3362 return 0; 3363 } 3364 3365 static void rtw89_disable_fw_watchdog(struct rtw89_dev *rtwdev) 3366 { 3367 u32 val32; 3368 3369 rtw89_mac_mem_write(rtwdev, R_AX_WDT_CTRL, 3370 WDT_CTRL_ALL_DIS, RTW89_MAC_MEM_CPU_LOCAL); 3371 3372 val32 = rtw89_mac_mem_read(rtwdev, R_AX_WDT_STATUS, RTW89_MAC_MEM_CPU_LOCAL); 3373 val32 |= B_AX_FS_WDT_INT; 3374 val32 &= ~B_AX_FS_WDT_INT_MSK; 3375 rtw89_mac_mem_write(rtwdev, R_AX_WDT_STATUS, val32, RTW89_MAC_MEM_CPU_LOCAL); 3376 } 3377 3378 void rtw89_mac_disable_cpu(struct rtw89_dev *rtwdev) 3379 { 3380 clear_bit(RTW89_FLAG_FW_RDY, rtwdev->flags); 3381 3382 rtw89_write32_clr(rtwdev, R_AX_PLATFORM_ENABLE, B_AX_WCPU_EN); 3383 rtw89_write32_clr(rtwdev, R_AX_WCPU_FW_CTRL, B_AX_WCPU_FWDL_EN | 3384 B_AX_H2C_PATH_RDY | B_AX_FWDL_PATH_RDY); 3385 rtw89_write32_clr(rtwdev, R_AX_SYS_CLK_CTRL, B_AX_CPU_CLK_EN); 3386 3387 rtw89_disable_fw_watchdog(rtwdev); 3388 3389 rtw89_write32_clr(rtwdev, R_AX_PLATFORM_ENABLE, B_AX_PLATFORM_EN); 3390 rtw89_write32_set(rtwdev, R_AX_PLATFORM_ENABLE, B_AX_PLATFORM_EN); 3391 } 3392 3393 int rtw89_mac_enable_cpu(struct rtw89_dev *rtwdev, u8 boot_reason, bool dlfw) 3394 { 3395 u32 val; 3396 int ret; 3397 3398 if (rtw89_read32(rtwdev, R_AX_PLATFORM_ENABLE) & B_AX_WCPU_EN) 3399 return -EFAULT; 3400 3401 rtw89_write32(rtwdev, R_AX_UDM1, 0); 3402 rtw89_write32(rtwdev, R_AX_UDM2, 0); 3403 rtw89_write32(rtwdev, R_AX_HALT_H2C_CTRL, 0); 3404 rtw89_write32(rtwdev, R_AX_HALT_C2H_CTRL, 0); 3405 rtw89_write32(rtwdev, R_AX_HALT_H2C, 0); 3406 rtw89_write32(rtwdev, R_AX_HALT_C2H, 0); 3407 3408 rtw89_write32_set(rtwdev, R_AX_SYS_CLK_CTRL, B_AX_CPU_CLK_EN); 3409 3410 val = rtw89_read32(rtwdev, R_AX_WCPU_FW_CTRL); 3411 val &= ~(B_AX_WCPU_FWDL_EN | B_AX_H2C_PATH_RDY | B_AX_FWDL_PATH_RDY); 3412 val = u32_replace_bits(val, RTW89_FWDL_INITIAL_STATE, 3413 B_AX_WCPU_FWDL_STS_MASK); 3414 3415 if (dlfw) 3416 val |= B_AX_WCPU_FWDL_EN; 3417 3418 rtw89_write32(rtwdev, R_AX_WCPU_FW_CTRL, val); 3419 3420 if (rtwdev->chip->chip_id == RTL8852B) 3421 rtw89_write32_mask(rtwdev, R_AX_SEC_CTRL, 3422 B_AX_SEC_IDMEM_SIZE_CONFIG_MASK, 0x2); 3423 3424 rtw89_write16_mask(rtwdev, R_AX_BOOT_REASON, B_AX_BOOT_REASON_MASK, 3425 boot_reason); 3426 rtw89_write32_set(rtwdev, R_AX_PLATFORM_ENABLE, B_AX_WCPU_EN); 3427 3428 if (!dlfw) { 3429 mdelay(5); 3430 3431 ret = rtw89_fw_check_rdy(rtwdev); 3432 if (ret) 3433 return ret; 3434 } 3435 3436 return 0; 3437 } 3438 3439 static int rtw89_mac_dmac_pre_init(struct rtw89_dev *rtwdev) 3440 { 3441 enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id; 3442 u32 val; 3443 int ret; 3444 3445 if (chip_id == RTL8852C) 3446 val = B_AX_MAC_FUNC_EN | B_AX_DMAC_FUNC_EN | B_AX_DISPATCHER_EN | 3447 B_AX_PKT_BUF_EN | B_AX_H_AXIDMA_EN; 3448 else 3449 val = B_AX_MAC_FUNC_EN | B_AX_DMAC_FUNC_EN | B_AX_DISPATCHER_EN | 3450 B_AX_PKT_BUF_EN; 3451 rtw89_write32(rtwdev, R_AX_DMAC_FUNC_EN, val); 3452 3453 val = B_AX_DISPATCHER_CLK_EN; 3454 rtw89_write32(rtwdev, R_AX_DMAC_CLK_EN, val); 3455 3456 if (chip_id != RTL8852C) 3457 goto dle; 3458 3459 val = rtw89_read32(rtwdev, R_AX_HAXI_INIT_CFG1); 3460 val &= ~(B_AX_DMA_MODE_MASK | B_AX_STOP_AXI_MST); 3461 val |= FIELD_PREP(B_AX_DMA_MODE_MASK, DMA_MOD_PCIE_1B) | 3462 B_AX_TXHCI_EN_V1 | B_AX_RXHCI_EN_V1; 3463 rtw89_write32(rtwdev, R_AX_HAXI_INIT_CFG1, val); 3464 3465 rtw89_write32_clr(rtwdev, R_AX_HAXI_DMA_STOP1, 3466 B_AX_STOP_ACH0 | B_AX_STOP_ACH1 | B_AX_STOP_ACH3 | 3467 B_AX_STOP_ACH4 | B_AX_STOP_ACH5 | B_AX_STOP_ACH6 | 3468 B_AX_STOP_ACH7 | B_AX_STOP_CH8 | B_AX_STOP_CH9 | 3469 B_AX_STOP_CH12 | B_AX_STOP_ACH2); 3470 rtw89_write32_clr(rtwdev, R_AX_HAXI_DMA_STOP2, B_AX_STOP_CH10 | B_AX_STOP_CH11); 3471 rtw89_write32_set(rtwdev, R_AX_PLATFORM_ENABLE, B_AX_AXIDMA_EN); 3472 3473 dle: 3474 ret = dle_init(rtwdev, RTW89_QTA_DLFW, rtwdev->mac.qta_mode); 3475 if (ret) { 3476 rtw89_err(rtwdev, "[ERR]DLE pre init %d\n", ret); 3477 return ret; 3478 } 3479 3480 ret = hfc_init(rtwdev, true, false, true); 3481 if (ret) { 3482 rtw89_err(rtwdev, "[ERR]HCI FC pre init %d\n", ret); 3483 return ret; 3484 } 3485 3486 return ret; 3487 } 3488 3489 int rtw89_mac_enable_bb_rf(struct rtw89_dev *rtwdev) 3490 { 3491 rtw89_write8_set(rtwdev, R_AX_SYS_FUNC_EN, 3492 B_AX_FEN_BBRSTB | B_AX_FEN_BB_GLB_RSTN); 3493 rtw89_write32_set(rtwdev, R_AX_WLRF_CTRL, 3494 B_AX_WLRF1_CTRL_7 | B_AX_WLRF1_CTRL_1 | 3495 B_AX_WLRF_CTRL_7 | B_AX_WLRF_CTRL_1); 3496 rtw89_write8_set(rtwdev, R_AX_PHYREG_SET, PHYREG_SET_ALL_CYCLE); 3497 3498 return 0; 3499 } 3500 EXPORT_SYMBOL(rtw89_mac_enable_bb_rf); 3501 3502 int rtw89_mac_disable_bb_rf(struct rtw89_dev *rtwdev) 3503 { 3504 rtw89_write8_clr(rtwdev, R_AX_SYS_FUNC_EN, 3505 B_AX_FEN_BBRSTB | B_AX_FEN_BB_GLB_RSTN); 3506 rtw89_write32_clr(rtwdev, R_AX_WLRF_CTRL, 3507 B_AX_WLRF1_CTRL_7 | B_AX_WLRF1_CTRL_1 | 3508 B_AX_WLRF_CTRL_7 | B_AX_WLRF_CTRL_1); 3509 rtw89_write8_clr(rtwdev, R_AX_PHYREG_SET, PHYREG_SET_ALL_CYCLE); 3510 3511 return 0; 3512 } 3513 EXPORT_SYMBOL(rtw89_mac_disable_bb_rf); 3514 3515 int rtw89_mac_partial_init(struct rtw89_dev *rtwdev) 3516 { 3517 int ret; 3518 3519 ret = rtw89_mac_power_switch(rtwdev, true); 3520 if (ret) { 3521 rtw89_mac_power_switch(rtwdev, false); 3522 ret = rtw89_mac_power_switch(rtwdev, true); 3523 if (ret) 3524 return ret; 3525 } 3526 3527 rtw89_mac_ctrl_hci_dma_trx(rtwdev, true); 3528 3529 ret = rtw89_mac_dmac_pre_init(rtwdev); 3530 if (ret) 3531 return ret; 3532 3533 if (rtwdev->hci.ops->mac_pre_init) { 3534 ret = rtwdev->hci.ops->mac_pre_init(rtwdev); 3535 if (ret) 3536 return ret; 3537 } 3538 3539 ret = rtw89_fw_download(rtwdev, RTW89_FW_NORMAL); 3540 if (ret) 3541 return ret; 3542 3543 return 0; 3544 } 3545 3546 int rtw89_mac_init(struct rtw89_dev *rtwdev) 3547 { 3548 int ret; 3549 3550 ret = rtw89_mac_partial_init(rtwdev); 3551 if (ret) 3552 goto fail; 3553 3554 ret = rtw89_chip_enable_bb_rf(rtwdev); 3555 if (ret) 3556 goto fail; 3557 3558 ret = rtw89_mac_sys_init(rtwdev); 3559 if (ret) 3560 goto fail; 3561 3562 ret = rtw89_mac_trx_init(rtwdev); 3563 if (ret) 3564 goto fail; 3565 3566 if (rtwdev->hci.ops->mac_post_init) { 3567 ret = rtwdev->hci.ops->mac_post_init(rtwdev); 3568 if (ret) 3569 goto fail; 3570 } 3571 3572 rtw89_fw_send_all_early_h2c(rtwdev); 3573 rtw89_fw_h2c_set_ofld_cfg(rtwdev); 3574 3575 return ret; 3576 fail: 3577 rtw89_mac_power_switch(rtwdev, false); 3578 3579 return ret; 3580 } 3581 3582 static void rtw89_mac_dmac_tbl_init(struct rtw89_dev *rtwdev, u8 macid) 3583 { 3584 u8 i; 3585 3586 for (i = 0; i < 4; i++) { 3587 rtw89_write32(rtwdev, R_AX_FILTER_MODEL_ADDR, 3588 DMAC_TBL_BASE_ADDR + (macid << 4) + (i << 2)); 3589 rtw89_write32(rtwdev, R_AX_INDIR_ACCESS_ENTRY, 0); 3590 } 3591 } 3592 3593 static void rtw89_mac_cmac_tbl_init(struct rtw89_dev *rtwdev, u8 macid) 3594 { 3595 rtw89_write32(rtwdev, R_AX_FILTER_MODEL_ADDR, 3596 CMAC_TBL_BASE_ADDR + macid * CCTL_INFO_SIZE); 3597 rtw89_write32(rtwdev, R_AX_INDIR_ACCESS_ENTRY, 0x4); 3598 rtw89_write32(rtwdev, R_AX_INDIR_ACCESS_ENTRY + 4, 0x400A0004); 3599 rtw89_write32(rtwdev, R_AX_INDIR_ACCESS_ENTRY + 8, 0); 3600 rtw89_write32(rtwdev, R_AX_INDIR_ACCESS_ENTRY + 12, 0); 3601 rtw89_write32(rtwdev, R_AX_INDIR_ACCESS_ENTRY + 16, 0); 3602 rtw89_write32(rtwdev, R_AX_INDIR_ACCESS_ENTRY + 20, 0xE43000B); 3603 rtw89_write32(rtwdev, R_AX_INDIR_ACCESS_ENTRY + 24, 0); 3604 rtw89_write32(rtwdev, R_AX_INDIR_ACCESS_ENTRY + 28, 0xB8109); 3605 } 3606 3607 int rtw89_mac_set_macid_pause(struct rtw89_dev *rtwdev, u8 macid, bool pause) 3608 { 3609 u8 sh = FIELD_GET(GENMASK(4, 0), macid); 3610 u8 grp = macid >> 5; 3611 int ret; 3612 3613 /* If this is called by change_interface() in the case of P2P, it could 3614 * be power-off, so ignore this operation. 3615 */ 3616 if (test_bit(RTW89_FLAG_CHANGING_INTERFACE, rtwdev->flags) && 3617 !test_bit(RTW89_FLAG_POWERON, rtwdev->flags)) 3618 return 0; 3619 3620 ret = rtw89_mac_check_mac_en(rtwdev, RTW89_MAC_0, RTW89_CMAC_SEL); 3621 if (ret) 3622 return ret; 3623 3624 rtw89_fw_h2c_macid_pause(rtwdev, sh, grp, pause); 3625 3626 return 0; 3627 } 3628 3629 static const struct rtw89_port_reg rtw_port_base = { 3630 .port_cfg = R_AX_PORT_CFG_P0, 3631 .tbtt_prohib = R_AX_TBTT_PROHIB_P0, 3632 .bcn_area = R_AX_BCN_AREA_P0, 3633 .bcn_early = R_AX_BCNERLYINT_CFG_P0, 3634 .tbtt_early = R_AX_TBTTERLYINT_CFG_P0, 3635 .tbtt_agg = R_AX_TBTT_AGG_P0, 3636 .bcn_space = R_AX_BCN_SPACE_CFG_P0, 3637 .bcn_forcetx = R_AX_BCN_FORCETX_P0, 3638 .bcn_err_cnt = R_AX_BCN_ERR_CNT_P0, 3639 .bcn_err_flag = R_AX_BCN_ERR_FLAG_P0, 3640 .dtim_ctrl = R_AX_DTIM_CTRL_P0, 3641 .tbtt_shift = R_AX_TBTT_SHIFT_P0, 3642 .bcn_cnt_tmr = R_AX_BCN_CNT_TMR_P0, 3643 .tsftr_l = R_AX_TSFTR_LOW_P0, 3644 .tsftr_h = R_AX_TSFTR_HIGH_P0 3645 }; 3646 3647 #define BCN_INTERVAL 100 3648 #define BCN_ERLY_DEF 160 3649 #define BCN_SETUP_DEF 2 3650 #define BCN_HOLD_DEF 200 3651 #define BCN_MASK_DEF 0 3652 #define TBTT_ERLY_DEF 5 3653 #define BCN_SET_UNIT 32 3654 #define BCN_ERLY_SET_DLY (10 * 2) 3655 3656 static void rtw89_mac_port_cfg_func_sw(struct rtw89_dev *rtwdev, 3657 struct rtw89_vif *rtwvif) 3658 { 3659 struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif); 3660 const struct rtw89_port_reg *p = &rtw_port_base; 3661 3662 if (!rtw89_read32_port_mask(rtwdev, rtwvif, p->port_cfg, B_AX_PORT_FUNC_EN)) 3663 return; 3664 3665 rtw89_write32_port_clr(rtwdev, rtwvif, p->tbtt_prohib, B_AX_TBTT_SETUP_MASK); 3666 rtw89_write32_port_mask(rtwdev, rtwvif, p->tbtt_prohib, B_AX_TBTT_HOLD_MASK, 1); 3667 rtw89_write16_port_clr(rtwdev, rtwvif, p->tbtt_early, B_AX_TBTTERLY_MASK); 3668 rtw89_write16_port_clr(rtwdev, rtwvif, p->bcn_early, B_AX_BCNERLY_MASK); 3669 3670 msleep(vif->bss_conf.beacon_int + 1); 3671 3672 rtw89_write32_port_clr(rtwdev, rtwvif, p->port_cfg, B_AX_PORT_FUNC_EN | 3673 B_AX_BRK_SETUP); 3674 rtw89_write32_port_set(rtwdev, rtwvif, p->port_cfg, B_AX_TSFTR_RST); 3675 rtw89_write32_port(rtwdev, rtwvif, p->bcn_cnt_tmr, 0); 3676 } 3677 3678 static void rtw89_mac_port_cfg_tx_rpt(struct rtw89_dev *rtwdev, 3679 struct rtw89_vif *rtwvif, bool en) 3680 { 3681 const struct rtw89_port_reg *p = &rtw_port_base; 3682 3683 if (en) 3684 rtw89_write32_port_set(rtwdev, rtwvif, p->port_cfg, B_AX_TXBCN_RPT_EN); 3685 else 3686 rtw89_write32_port_clr(rtwdev, rtwvif, p->port_cfg, B_AX_TXBCN_RPT_EN); 3687 } 3688 3689 static void rtw89_mac_port_cfg_rx_rpt(struct rtw89_dev *rtwdev, 3690 struct rtw89_vif *rtwvif, bool en) 3691 { 3692 const struct rtw89_port_reg *p = &rtw_port_base; 3693 3694 if (en) 3695 rtw89_write32_port_set(rtwdev, rtwvif, p->port_cfg, B_AX_RXBCN_RPT_EN); 3696 else 3697 rtw89_write32_port_clr(rtwdev, rtwvif, p->port_cfg, B_AX_RXBCN_RPT_EN); 3698 } 3699 3700 static void rtw89_mac_port_cfg_net_type(struct rtw89_dev *rtwdev, 3701 struct rtw89_vif *rtwvif) 3702 { 3703 const struct rtw89_port_reg *p = &rtw_port_base; 3704 3705 rtw89_write32_port_mask(rtwdev, rtwvif, p->port_cfg, B_AX_NET_TYPE_MASK, 3706 rtwvif->net_type); 3707 } 3708 3709 static void rtw89_mac_port_cfg_bcn_prct(struct rtw89_dev *rtwdev, 3710 struct rtw89_vif *rtwvif) 3711 { 3712 const struct rtw89_port_reg *p = &rtw_port_base; 3713 bool en = rtwvif->net_type != RTW89_NET_TYPE_NO_LINK; 3714 u32 bits = B_AX_TBTT_PROHIB_EN | B_AX_BRK_SETUP; 3715 3716 if (en) 3717 rtw89_write32_port_set(rtwdev, rtwvif, p->port_cfg, bits); 3718 else 3719 rtw89_write32_port_clr(rtwdev, rtwvif, p->port_cfg, bits); 3720 } 3721 3722 static void rtw89_mac_port_cfg_rx_sw(struct rtw89_dev *rtwdev, 3723 struct rtw89_vif *rtwvif) 3724 { 3725 const struct rtw89_port_reg *p = &rtw_port_base; 3726 bool en = rtwvif->net_type == RTW89_NET_TYPE_INFRA || 3727 rtwvif->net_type == RTW89_NET_TYPE_AD_HOC; 3728 u32 bit = B_AX_RX_BSSID_FIT_EN; 3729 3730 if (en) 3731 rtw89_write32_port_set(rtwdev, rtwvif, p->port_cfg, bit); 3732 else 3733 rtw89_write32_port_clr(rtwdev, rtwvif, p->port_cfg, bit); 3734 } 3735 3736 static void rtw89_mac_port_cfg_rx_sync(struct rtw89_dev *rtwdev, 3737 struct rtw89_vif *rtwvif) 3738 { 3739 const struct rtw89_port_reg *p = &rtw_port_base; 3740 bool en = rtwvif->net_type == RTW89_NET_TYPE_INFRA || 3741 rtwvif->net_type == RTW89_NET_TYPE_AD_HOC; 3742 3743 if (en) 3744 rtw89_write32_port_set(rtwdev, rtwvif, p->port_cfg, B_AX_TSF_UDT_EN); 3745 else 3746 rtw89_write32_port_clr(rtwdev, rtwvif, p->port_cfg, B_AX_TSF_UDT_EN); 3747 } 3748 3749 static void rtw89_mac_port_cfg_tx_sw(struct rtw89_dev *rtwdev, 3750 struct rtw89_vif *rtwvif) 3751 { 3752 const struct rtw89_port_reg *p = &rtw_port_base; 3753 bool en = rtwvif->net_type == RTW89_NET_TYPE_AP_MODE || 3754 rtwvif->net_type == RTW89_NET_TYPE_AD_HOC; 3755 3756 if (en) 3757 rtw89_write32_port_set(rtwdev, rtwvif, p->port_cfg, B_AX_BCNTX_EN); 3758 else 3759 rtw89_write32_port_clr(rtwdev, rtwvif, p->port_cfg, B_AX_BCNTX_EN); 3760 } 3761 3762 static void rtw89_mac_port_cfg_bcn_intv(struct rtw89_dev *rtwdev, 3763 struct rtw89_vif *rtwvif) 3764 { 3765 struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif); 3766 const struct rtw89_port_reg *p = &rtw_port_base; 3767 u16 bcn_int = vif->bss_conf.beacon_int ? vif->bss_conf.beacon_int : BCN_INTERVAL; 3768 3769 rtw89_write32_port_mask(rtwdev, rtwvif, p->bcn_space, B_AX_BCN_SPACE_MASK, 3770 bcn_int); 3771 } 3772 3773 static void rtw89_mac_port_cfg_hiq_win(struct rtw89_dev *rtwdev, 3774 struct rtw89_vif *rtwvif) 3775 { 3776 static const u32 hiq_win_addr[RTW89_PORT_NUM] = { 3777 R_AX_P0MB_HGQ_WINDOW_CFG_0, R_AX_PORT_HGQ_WINDOW_CFG, 3778 R_AX_PORT_HGQ_WINDOW_CFG + 1, R_AX_PORT_HGQ_WINDOW_CFG + 2, 3779 R_AX_PORT_HGQ_WINDOW_CFG + 3, 3780 }; 3781 u8 win = rtwvif->net_type == RTW89_NET_TYPE_AP_MODE ? 16 : 0; 3782 u8 port = rtwvif->port; 3783 u32 reg; 3784 3785 reg = rtw89_mac_reg_by_idx(hiq_win_addr[port], rtwvif->mac_idx); 3786 rtw89_write8(rtwdev, reg, win); 3787 } 3788 3789 static void rtw89_mac_port_cfg_hiq_dtim(struct rtw89_dev *rtwdev, 3790 struct rtw89_vif *rtwvif) 3791 { 3792 struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif); 3793 const struct rtw89_port_reg *p = &rtw_port_base; 3794 u32 addr; 3795 3796 addr = rtw89_mac_reg_by_idx(R_AX_MD_TSFT_STMP_CTL, rtwvif->mac_idx); 3797 rtw89_write8_set(rtwdev, addr, B_AX_UPD_HGQMD | B_AX_UPD_TIMIE); 3798 3799 rtw89_write16_port_mask(rtwdev, rtwvif, p->dtim_ctrl, B_AX_DTIM_NUM_MASK, 3800 vif->bss_conf.dtim_period); 3801 } 3802 3803 static void rtw89_mac_port_cfg_bcn_setup_time(struct rtw89_dev *rtwdev, 3804 struct rtw89_vif *rtwvif) 3805 { 3806 const struct rtw89_port_reg *p = &rtw_port_base; 3807 3808 rtw89_write32_port_mask(rtwdev, rtwvif, p->tbtt_prohib, 3809 B_AX_TBTT_SETUP_MASK, BCN_SETUP_DEF); 3810 } 3811 3812 static void rtw89_mac_port_cfg_bcn_hold_time(struct rtw89_dev *rtwdev, 3813 struct rtw89_vif *rtwvif) 3814 { 3815 const struct rtw89_port_reg *p = &rtw_port_base; 3816 3817 rtw89_write32_port_mask(rtwdev, rtwvif, p->tbtt_prohib, 3818 B_AX_TBTT_HOLD_MASK, BCN_HOLD_DEF); 3819 } 3820 3821 static void rtw89_mac_port_cfg_bcn_mask_area(struct rtw89_dev *rtwdev, 3822 struct rtw89_vif *rtwvif) 3823 { 3824 const struct rtw89_port_reg *p = &rtw_port_base; 3825 3826 rtw89_write32_port_mask(rtwdev, rtwvif, p->bcn_area, 3827 B_AX_BCN_MSK_AREA_MASK, BCN_MASK_DEF); 3828 } 3829 3830 static void rtw89_mac_port_cfg_tbtt_early(struct rtw89_dev *rtwdev, 3831 struct rtw89_vif *rtwvif) 3832 { 3833 const struct rtw89_port_reg *p = &rtw_port_base; 3834 3835 rtw89_write16_port_mask(rtwdev, rtwvif, p->tbtt_early, 3836 B_AX_TBTTERLY_MASK, TBTT_ERLY_DEF); 3837 } 3838 3839 static void rtw89_mac_port_cfg_bss_color(struct rtw89_dev *rtwdev, 3840 struct rtw89_vif *rtwvif) 3841 { 3842 struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif); 3843 static const u32 masks[RTW89_PORT_NUM] = { 3844 B_AX_BSS_COLOB_AX_PORT_0_MASK, B_AX_BSS_COLOB_AX_PORT_1_MASK, 3845 B_AX_BSS_COLOB_AX_PORT_2_MASK, B_AX_BSS_COLOB_AX_PORT_3_MASK, 3846 B_AX_BSS_COLOB_AX_PORT_4_MASK, 3847 }; 3848 u8 port = rtwvif->port; 3849 u32 reg_base; 3850 u32 reg; 3851 u8 bss_color; 3852 3853 bss_color = vif->bss_conf.he_bss_color.color; 3854 reg_base = port >= 4 ? R_AX_PTCL_BSS_COLOR_1 : R_AX_PTCL_BSS_COLOR_0; 3855 reg = rtw89_mac_reg_by_idx(reg_base, rtwvif->mac_idx); 3856 rtw89_write32_mask(rtwdev, reg, masks[port], bss_color); 3857 } 3858 3859 static void rtw89_mac_port_cfg_mbssid(struct rtw89_dev *rtwdev, 3860 struct rtw89_vif *rtwvif) 3861 { 3862 u8 port = rtwvif->port; 3863 u32 reg; 3864 3865 if (rtwvif->net_type == RTW89_NET_TYPE_AP_MODE) 3866 return; 3867 3868 if (port == 0) { 3869 reg = rtw89_mac_reg_by_idx(R_AX_MBSSID_CTRL, rtwvif->mac_idx); 3870 rtw89_write32_clr(rtwdev, reg, B_AX_P0MB_ALL_MASK); 3871 } 3872 } 3873 3874 static void rtw89_mac_port_cfg_hiq_drop(struct rtw89_dev *rtwdev, 3875 struct rtw89_vif *rtwvif) 3876 { 3877 u8 port = rtwvif->port; 3878 u32 reg; 3879 u32 val; 3880 3881 reg = rtw89_mac_reg_by_idx(R_AX_MBSSID_DROP_0, rtwvif->mac_idx); 3882 val = rtw89_read32(rtwdev, reg); 3883 val &= ~FIELD_PREP(B_AX_PORT_DROP_4_0_MASK, BIT(port)); 3884 if (port == 0) 3885 val &= ~BIT(0); 3886 rtw89_write32(rtwdev, reg, val); 3887 } 3888 3889 static void rtw89_mac_port_cfg_func_en(struct rtw89_dev *rtwdev, 3890 struct rtw89_vif *rtwvif, bool enable) 3891 { 3892 const struct rtw89_port_reg *p = &rtw_port_base; 3893 3894 if (enable) 3895 rtw89_write32_port_set(rtwdev, rtwvif, p->port_cfg, 3896 B_AX_PORT_FUNC_EN); 3897 else 3898 rtw89_write32_port_clr(rtwdev, rtwvif, p->port_cfg, 3899 B_AX_PORT_FUNC_EN); 3900 } 3901 3902 static void rtw89_mac_port_cfg_bcn_early(struct rtw89_dev *rtwdev, 3903 struct rtw89_vif *rtwvif) 3904 { 3905 const struct rtw89_port_reg *p = &rtw_port_base; 3906 3907 rtw89_write32_port_mask(rtwdev, rtwvif, p->bcn_early, B_AX_BCNERLY_MASK, 3908 BCN_ERLY_DEF); 3909 } 3910 3911 static void rtw89_mac_port_cfg_tbtt_shift(struct rtw89_dev *rtwdev, 3912 struct rtw89_vif *rtwvif) 3913 { 3914 const struct rtw89_port_reg *p = &rtw_port_base; 3915 u16 val; 3916 3917 if (rtwdev->chip->chip_id != RTL8852C) 3918 return; 3919 3920 if (rtwvif->wifi_role != RTW89_WIFI_ROLE_P2P_CLIENT && 3921 rtwvif->wifi_role != RTW89_WIFI_ROLE_STATION) 3922 return; 3923 3924 val = FIELD_PREP(B_AX_TBTT_SHIFT_OFST_MAG, 1) | 3925 B_AX_TBTT_SHIFT_OFST_SIGN; 3926 3927 rtw89_write16_port_mask(rtwdev, rtwvif, p->tbtt_shift, 3928 B_AX_TBTT_SHIFT_OFST_MASK, val); 3929 } 3930 3931 void rtw89_mac_port_tsf_sync(struct rtw89_dev *rtwdev, 3932 struct rtw89_vif *rtwvif, 3933 struct rtw89_vif *rtwvif_src, 3934 u16 offset_tu) 3935 { 3936 u32 val, reg; 3937 3938 val = RTW89_PORT_OFFSET_TU_TO_32US(offset_tu); 3939 reg = rtw89_mac_reg_by_idx(R_AX_PORT0_TSF_SYNC + rtwvif->port * 4, 3940 rtwvif->mac_idx); 3941 3942 rtw89_write32_mask(rtwdev, reg, B_AX_SYNC_PORT_SRC, rtwvif_src->port); 3943 rtw89_write32_mask(rtwdev, reg, B_AX_SYNC_PORT_OFFSET_VAL, val); 3944 rtw89_write32_set(rtwdev, reg, B_AX_SYNC_NOW); 3945 } 3946 3947 static void rtw89_mac_port_tsf_sync_rand(struct rtw89_dev *rtwdev, 3948 struct rtw89_vif *rtwvif, 3949 struct rtw89_vif *rtwvif_src, 3950 u8 offset, int *n_offset) 3951 { 3952 if (rtwvif->net_type != RTW89_NET_TYPE_AP_MODE || rtwvif == rtwvif_src) 3953 return; 3954 3955 /* adjust offset randomly to avoid beacon conflict */ 3956 offset = offset - offset / 4 + get_random_u32() % (offset / 2); 3957 rtw89_mac_port_tsf_sync(rtwdev, rtwvif, rtwvif_src, 3958 (*n_offset) * offset); 3959 3960 (*n_offset)++; 3961 } 3962 3963 static void rtw89_mac_port_tsf_resync_all(struct rtw89_dev *rtwdev) 3964 { 3965 struct rtw89_vif *src = NULL, *tmp; 3966 u8 offset = 100, vif_aps = 0; 3967 int n_offset = 1; 3968 3969 rtw89_for_each_rtwvif(rtwdev, tmp) { 3970 if (!src || tmp->net_type == RTW89_NET_TYPE_INFRA) 3971 src = tmp; 3972 if (tmp->net_type == RTW89_NET_TYPE_AP_MODE) 3973 vif_aps++; 3974 } 3975 3976 if (vif_aps == 0) 3977 return; 3978 3979 offset /= (vif_aps + 1); 3980 3981 rtw89_for_each_rtwvif(rtwdev, tmp) 3982 rtw89_mac_port_tsf_sync_rand(rtwdev, tmp, src, offset, &n_offset); 3983 } 3984 3985 int rtw89_mac_vif_init(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif) 3986 { 3987 int ret; 3988 3989 ret = rtw89_mac_port_update(rtwdev, rtwvif); 3990 if (ret) 3991 return ret; 3992 3993 rtw89_mac_dmac_tbl_init(rtwdev, rtwvif->mac_id); 3994 rtw89_mac_cmac_tbl_init(rtwdev, rtwvif->mac_id); 3995 3996 ret = rtw89_mac_set_macid_pause(rtwdev, rtwvif->mac_id, false); 3997 if (ret) 3998 return ret; 3999 4000 ret = rtw89_fw_h2c_role_maintain(rtwdev, rtwvif, NULL, RTW89_ROLE_CREATE); 4001 if (ret) 4002 return ret; 4003 4004 ret = rtw89_fw_h2c_join_info(rtwdev, rtwvif, NULL, true); 4005 if (ret) 4006 return ret; 4007 4008 ret = rtw89_cam_init(rtwdev, rtwvif); 4009 if (ret) 4010 return ret; 4011 4012 ret = rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, NULL); 4013 if (ret) 4014 return ret; 4015 4016 ret = rtw89_fw_h2c_default_cmac_tbl(rtwdev, rtwvif); 4017 if (ret) 4018 return ret; 4019 4020 return 0; 4021 } 4022 4023 int rtw89_mac_vif_deinit(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif) 4024 { 4025 int ret; 4026 4027 ret = rtw89_fw_h2c_role_maintain(rtwdev, rtwvif, NULL, RTW89_ROLE_REMOVE); 4028 if (ret) 4029 return ret; 4030 4031 rtw89_cam_deinit(rtwdev, rtwvif); 4032 4033 ret = rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, NULL); 4034 if (ret) 4035 return ret; 4036 4037 return 0; 4038 } 4039 4040 int rtw89_mac_port_update(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif) 4041 { 4042 u8 port = rtwvif->port; 4043 4044 if (port >= RTW89_PORT_NUM) 4045 return -EINVAL; 4046 4047 rtw89_mac_port_cfg_func_sw(rtwdev, rtwvif); 4048 rtw89_mac_port_cfg_tx_rpt(rtwdev, rtwvif, false); 4049 rtw89_mac_port_cfg_rx_rpt(rtwdev, rtwvif, false); 4050 rtw89_mac_port_cfg_net_type(rtwdev, rtwvif); 4051 rtw89_mac_port_cfg_bcn_prct(rtwdev, rtwvif); 4052 rtw89_mac_port_cfg_rx_sw(rtwdev, rtwvif); 4053 rtw89_mac_port_cfg_rx_sync(rtwdev, rtwvif); 4054 rtw89_mac_port_cfg_tx_sw(rtwdev, rtwvif); 4055 rtw89_mac_port_cfg_bcn_intv(rtwdev, rtwvif); 4056 rtw89_mac_port_cfg_hiq_win(rtwdev, rtwvif); 4057 rtw89_mac_port_cfg_hiq_dtim(rtwdev, rtwvif); 4058 rtw89_mac_port_cfg_hiq_drop(rtwdev, rtwvif); 4059 rtw89_mac_port_cfg_bcn_setup_time(rtwdev, rtwvif); 4060 rtw89_mac_port_cfg_bcn_hold_time(rtwdev, rtwvif); 4061 rtw89_mac_port_cfg_bcn_mask_area(rtwdev, rtwvif); 4062 rtw89_mac_port_cfg_tbtt_early(rtwdev, rtwvif); 4063 rtw89_mac_port_cfg_tbtt_shift(rtwdev, rtwvif); 4064 rtw89_mac_port_cfg_bss_color(rtwdev, rtwvif); 4065 rtw89_mac_port_cfg_mbssid(rtwdev, rtwvif); 4066 rtw89_mac_port_cfg_func_en(rtwdev, rtwvif, true); 4067 rtw89_mac_port_tsf_resync_all(rtwdev); 4068 fsleep(BCN_ERLY_SET_DLY); 4069 rtw89_mac_port_cfg_bcn_early(rtwdev, rtwvif); 4070 4071 return 0; 4072 } 4073 4074 int rtw89_mac_port_get_tsf(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif, 4075 u64 *tsf) 4076 { 4077 const struct rtw89_port_reg *p = &rtw_port_base; 4078 u32 tsf_low, tsf_high; 4079 int ret; 4080 4081 ret = rtw89_mac_check_mac_en(rtwdev, rtwvif->mac_idx, RTW89_CMAC_SEL); 4082 if (ret) 4083 return ret; 4084 4085 tsf_low = rtw89_read32_port(rtwdev, rtwvif, p->tsftr_l); 4086 tsf_high = rtw89_read32_port(rtwdev, rtwvif, p->tsftr_h); 4087 *tsf = (u64)tsf_high << 32 | tsf_low; 4088 4089 return 0; 4090 } 4091 4092 static void rtw89_mac_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy, 4093 struct cfg80211_bss *bss, 4094 void *data) 4095 { 4096 const struct cfg80211_bss_ies *ies; 4097 const struct element *elem; 4098 bool *tolerated = data; 4099 4100 rcu_read_lock(); 4101 ies = rcu_dereference(bss->ies); 4102 elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, ies->data, 4103 ies->len); 4104 4105 if (!elem || elem->datalen < 10 || 4106 !(elem->data[10] & WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) 4107 *tolerated = false; 4108 rcu_read_unlock(); 4109 } 4110 4111 void rtw89_mac_set_he_obss_narrow_bw_ru(struct rtw89_dev *rtwdev, 4112 struct ieee80211_vif *vif) 4113 { 4114 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 4115 struct ieee80211_hw *hw = rtwdev->hw; 4116 bool tolerated = true; 4117 u32 reg; 4118 4119 if (!vif->bss_conf.he_support || vif->type != NL80211_IFTYPE_STATION) 4120 return; 4121 4122 if (!(vif->bss_conf.chandef.chan->flags & IEEE80211_CHAN_RADAR)) 4123 return; 4124 4125 cfg80211_bss_iter(hw->wiphy, &vif->bss_conf.chandef, 4126 rtw89_mac_check_he_obss_narrow_bw_ru_iter, 4127 &tolerated); 4128 4129 reg = rtw89_mac_reg_by_idx(R_AX_RXTRIG_TEST_USER_2, rtwvif->mac_idx); 4130 if (tolerated) 4131 rtw89_write32_clr(rtwdev, reg, B_AX_RXTRIG_RU26_DIS); 4132 else 4133 rtw89_write32_set(rtwdev, reg, B_AX_RXTRIG_RU26_DIS); 4134 } 4135 4136 void rtw89_mac_stop_ap(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif) 4137 { 4138 rtw89_mac_port_cfg_func_en(rtwdev, rtwvif, false); 4139 } 4140 4141 int rtw89_mac_add_vif(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif) 4142 { 4143 int ret; 4144 4145 rtwvif->mac_id = rtw89_core_acquire_bit_map(rtwdev->mac_id_map, 4146 RTW89_MAX_MAC_ID_NUM); 4147 if (rtwvif->mac_id == RTW89_MAX_MAC_ID_NUM) 4148 return -ENOSPC; 4149 4150 ret = rtw89_mac_vif_init(rtwdev, rtwvif); 4151 if (ret) 4152 goto release_mac_id; 4153 4154 return 0; 4155 4156 release_mac_id: 4157 rtw89_core_release_bit_map(rtwdev->mac_id_map, rtwvif->mac_id); 4158 4159 return ret; 4160 } 4161 4162 int rtw89_mac_remove_vif(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif) 4163 { 4164 int ret; 4165 4166 ret = rtw89_mac_vif_deinit(rtwdev, rtwvif); 4167 rtw89_core_release_bit_map(rtwdev->mac_id_map, rtwvif->mac_id); 4168 4169 return ret; 4170 } 4171 4172 static void 4173 rtw89_mac_c2h_macid_pause(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len) 4174 { 4175 } 4176 4177 static bool rtw89_is_op_chan(struct rtw89_dev *rtwdev, u8 band, u8 channel) 4178 { 4179 struct rtw89_hw_scan_info *scan_info = &rtwdev->scan_info; 4180 4181 return band == scan_info->op_band && channel == scan_info->op_pri_ch; 4182 } 4183 4184 static void 4185 rtw89_mac_c2h_scanofld_rsp(struct rtw89_dev *rtwdev, struct sk_buff *c2h, 4186 u32 len) 4187 { 4188 struct ieee80211_vif *vif = rtwdev->scan_info.scanning_vif; 4189 struct rtw89_vif *rtwvif = vif_to_rtwvif_safe(vif); 4190 struct rtw89_chan new; 4191 u8 reason, status, tx_fail, band, actual_period; 4192 u32 last_chan = rtwdev->scan_info.last_chan_idx; 4193 u16 chan; 4194 int ret; 4195 4196 tx_fail = RTW89_GET_MAC_C2H_SCANOFLD_TX_FAIL(c2h->data); 4197 status = RTW89_GET_MAC_C2H_SCANOFLD_STATUS(c2h->data); 4198 chan = RTW89_GET_MAC_C2H_SCANOFLD_PRI_CH(c2h->data); 4199 reason = RTW89_GET_MAC_C2H_SCANOFLD_RSP(c2h->data); 4200 band = RTW89_GET_MAC_C2H_SCANOFLD_BAND(c2h->data); 4201 actual_period = RTW89_GET_MAC_C2H_ACTUAL_PERIOD(c2h->data); 4202 4203 if (!(rtwdev->chip->support_bands & BIT(NL80211_BAND_6GHZ))) 4204 band = chan > 14 ? RTW89_BAND_5G : RTW89_BAND_2G; 4205 4206 rtw89_debug(rtwdev, RTW89_DBG_HW_SCAN, 4207 "band: %d, chan: %d, reason: %d, status: %d, tx_fail: %d, actual: %d\n", 4208 band, chan, reason, status, tx_fail, actual_period); 4209 4210 switch (reason) { 4211 case RTW89_SCAN_LEAVE_CH_NOTIFY: 4212 if (rtw89_is_op_chan(rtwdev, band, chan)) 4213 ieee80211_stop_queues(rtwdev->hw); 4214 return; 4215 case RTW89_SCAN_END_SCAN_NOTIFY: 4216 if (rtwvif && rtwvif->scan_req && 4217 last_chan < rtwvif->scan_req->n_channels) { 4218 ret = rtw89_hw_scan_offload(rtwdev, vif, true); 4219 if (ret) { 4220 rtw89_hw_scan_abort(rtwdev, vif); 4221 rtw89_warn(rtwdev, "HW scan failed: %d\n", ret); 4222 } 4223 } else { 4224 rtw89_hw_scan_complete(rtwdev, vif, false); 4225 } 4226 break; 4227 case RTW89_SCAN_ENTER_CH_NOTIFY: 4228 rtw89_chan_create(&new, chan, chan, band, RTW89_CHANNEL_WIDTH_20); 4229 rtw89_assign_entity_chan(rtwdev, RTW89_SUB_ENTITY_0, &new); 4230 if (rtw89_is_op_chan(rtwdev, band, chan)) { 4231 rtw89_store_op_chan(rtwdev, false); 4232 ieee80211_wake_queues(rtwdev->hw); 4233 } 4234 break; 4235 default: 4236 return; 4237 } 4238 } 4239 4240 static void 4241 rtw89_mac_c2h_rec_ack(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len) 4242 { 4243 rtw89_debug(rtwdev, RTW89_DBG_FW, 4244 "C2H rev ack recv, cat: %d, class: %d, func: %d, seq : %d\n", 4245 RTW89_GET_MAC_C2H_REV_ACK_CAT(c2h->data), 4246 RTW89_GET_MAC_C2H_REV_ACK_CLASS(c2h->data), 4247 RTW89_GET_MAC_C2H_REV_ACK_FUNC(c2h->data), 4248 RTW89_GET_MAC_C2H_REV_ACK_H2C_SEQ(c2h->data)); 4249 } 4250 4251 static void 4252 rtw89_mac_c2h_done_ack(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len) 4253 { 4254 rtw89_debug(rtwdev, RTW89_DBG_FW, 4255 "C2H done ack recv, cat: %d, class: %d, func: %d, ret: %d, seq : %d\n", 4256 RTW89_GET_MAC_C2H_DONE_ACK_CAT(c2h->data), 4257 RTW89_GET_MAC_C2H_DONE_ACK_CLASS(c2h->data), 4258 RTW89_GET_MAC_C2H_DONE_ACK_FUNC(c2h->data), 4259 RTW89_GET_MAC_C2H_DONE_ACK_H2C_RETURN(c2h->data), 4260 RTW89_GET_MAC_C2H_DONE_ACK_H2C_SEQ(c2h->data)); 4261 } 4262 4263 static void 4264 rtw89_mac_c2h_log(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len) 4265 { 4266 rtw89_info(rtwdev, "%*s", RTW89_GET_C2H_LOG_LEN(len), 4267 RTW89_GET_C2H_LOG_SRT_PRT(c2h->data)); 4268 } 4269 4270 static void 4271 rtw89_mac_c2h_bcn_cnt(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len) 4272 { 4273 } 4274 4275 static void 4276 rtw89_mac_c2h_pkt_ofld_rsp(struct rtw89_dev *rtwdev, struct sk_buff *c2h, 4277 u32 len) 4278 { 4279 } 4280 4281 static void 4282 rtw89_mac_c2h_tsf32_toggle_rpt(struct rtw89_dev *rtwdev, struct sk_buff *c2h, 4283 u32 len) 4284 { 4285 } 4286 4287 static void 4288 rtw89_mac_c2h_mcc_rcv_ack(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len) 4289 { 4290 u8 group = RTW89_GET_MAC_C2H_MCC_RCV_ACK_GROUP(c2h->data); 4291 u8 func = RTW89_GET_MAC_C2H_MCC_RCV_ACK_H2C_FUNC(c2h->data); 4292 4293 switch (func) { 4294 case H2C_FUNC_ADD_MCC: 4295 case H2C_FUNC_START_MCC: 4296 case H2C_FUNC_STOP_MCC: 4297 case H2C_FUNC_DEL_MCC_GROUP: 4298 case H2C_FUNC_RESET_MCC_GROUP: 4299 case H2C_FUNC_MCC_REQ_TSF: 4300 case H2C_FUNC_MCC_MACID_BITMAP: 4301 case H2C_FUNC_MCC_SYNC: 4302 case H2C_FUNC_MCC_SET_DURATION: 4303 break; 4304 default: 4305 rtw89_debug(rtwdev, RTW89_DBG_CHAN, 4306 "invalid MCC C2H RCV ACK: func %d\n", func); 4307 return; 4308 } 4309 4310 rtw89_debug(rtwdev, RTW89_DBG_CHAN, 4311 "MCC C2H RCV ACK: group %d, func %d\n", group, func); 4312 } 4313 4314 static void 4315 rtw89_mac_c2h_mcc_req_ack(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len) 4316 { 4317 u8 group = RTW89_GET_MAC_C2H_MCC_REQ_ACK_GROUP(c2h->data); 4318 u8 func = RTW89_GET_MAC_C2H_MCC_REQ_ACK_H2C_FUNC(c2h->data); 4319 u8 retcode = RTW89_GET_MAC_C2H_MCC_REQ_ACK_H2C_RETURN(c2h->data); 4320 struct rtw89_completion_data data = {}; 4321 unsigned int cond; 4322 bool next = false; 4323 4324 switch (func) { 4325 case H2C_FUNC_MCC_REQ_TSF: 4326 next = true; 4327 break; 4328 case H2C_FUNC_MCC_MACID_BITMAP: 4329 case H2C_FUNC_MCC_SYNC: 4330 case H2C_FUNC_MCC_SET_DURATION: 4331 break; 4332 case H2C_FUNC_ADD_MCC: 4333 case H2C_FUNC_START_MCC: 4334 case H2C_FUNC_STOP_MCC: 4335 case H2C_FUNC_DEL_MCC_GROUP: 4336 case H2C_FUNC_RESET_MCC_GROUP: 4337 default: 4338 rtw89_debug(rtwdev, RTW89_DBG_CHAN, 4339 "invalid MCC C2H REQ ACK: func %d\n", func); 4340 return; 4341 } 4342 4343 rtw89_debug(rtwdev, RTW89_DBG_CHAN, 4344 "MCC C2H REQ ACK: group %d, func %d, return code %d\n", 4345 group, func, retcode); 4346 4347 if (!retcode && next) 4348 return; 4349 4350 data.err = !!retcode; 4351 cond = RTW89_MCC_WAIT_COND(group, func); 4352 rtw89_complete_cond(&rtwdev->mcc.wait, cond, &data); 4353 } 4354 4355 static void 4356 rtw89_mac_c2h_mcc_tsf_rpt(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len) 4357 { 4358 u8 group = RTW89_GET_MAC_C2H_MCC_TSF_RPT_GROUP(c2h->data); 4359 struct rtw89_completion_data data = {}; 4360 struct rtw89_mac_mcc_tsf_rpt *rpt; 4361 unsigned int cond; 4362 4363 rpt = (struct rtw89_mac_mcc_tsf_rpt *)data.buf; 4364 rpt->macid_x = RTW89_GET_MAC_C2H_MCC_TSF_RPT_MACID_X(c2h->data); 4365 rpt->macid_y = RTW89_GET_MAC_C2H_MCC_TSF_RPT_MACID_Y(c2h->data); 4366 rpt->tsf_x_low = RTW89_GET_MAC_C2H_MCC_TSF_RPT_TSF_LOW_X(c2h->data); 4367 rpt->tsf_x_high = RTW89_GET_MAC_C2H_MCC_TSF_RPT_TSF_HIGH_X(c2h->data); 4368 rpt->tsf_y_low = RTW89_GET_MAC_C2H_MCC_TSF_RPT_TSF_LOW_Y(c2h->data); 4369 rpt->tsf_y_high = RTW89_GET_MAC_C2H_MCC_TSF_RPT_TSF_HIGH_Y(c2h->data); 4370 4371 rtw89_debug(rtwdev, RTW89_DBG_CHAN, 4372 "MCC C2H TSF RPT: macid %d> %llu, macid %d> %llu\n", 4373 rpt->macid_x, (u64)rpt->tsf_x_high << 32 | rpt->tsf_x_low, 4374 rpt->macid_y, (u64)rpt->tsf_y_high << 32 | rpt->tsf_y_low); 4375 4376 cond = RTW89_MCC_WAIT_COND(group, H2C_FUNC_MCC_REQ_TSF); 4377 rtw89_complete_cond(&rtwdev->mcc.wait, cond, &data); 4378 } 4379 4380 static void 4381 rtw89_mac_c2h_mcc_status_rpt(struct rtw89_dev *rtwdev, struct sk_buff *c2h, u32 len) 4382 { 4383 u8 group = RTW89_GET_MAC_C2H_MCC_STATUS_RPT_GROUP(c2h->data); 4384 u8 macid = RTW89_GET_MAC_C2H_MCC_STATUS_RPT_MACID(c2h->data); 4385 u8 status = RTW89_GET_MAC_C2H_MCC_STATUS_RPT_STATUS(c2h->data); 4386 u32 tsf_low = RTW89_GET_MAC_C2H_MCC_STATUS_RPT_TSF_LOW(c2h->data); 4387 u32 tsf_high = RTW89_GET_MAC_C2H_MCC_STATUS_RPT_TSF_HIGH(c2h->data); 4388 struct rtw89_completion_data data = {}; 4389 unsigned int cond; 4390 bool rsp = true; 4391 bool err; 4392 u8 func; 4393 4394 switch (status) { 4395 case RTW89_MAC_MCC_ADD_ROLE_OK: 4396 case RTW89_MAC_MCC_ADD_ROLE_FAIL: 4397 func = H2C_FUNC_ADD_MCC; 4398 err = status == RTW89_MAC_MCC_ADD_ROLE_FAIL; 4399 break; 4400 case RTW89_MAC_MCC_START_GROUP_OK: 4401 case RTW89_MAC_MCC_START_GROUP_FAIL: 4402 func = H2C_FUNC_START_MCC; 4403 err = status == RTW89_MAC_MCC_START_GROUP_FAIL; 4404 break; 4405 case RTW89_MAC_MCC_STOP_GROUP_OK: 4406 case RTW89_MAC_MCC_STOP_GROUP_FAIL: 4407 func = H2C_FUNC_STOP_MCC; 4408 err = status == RTW89_MAC_MCC_STOP_GROUP_FAIL; 4409 break; 4410 case RTW89_MAC_MCC_DEL_GROUP_OK: 4411 case RTW89_MAC_MCC_DEL_GROUP_FAIL: 4412 func = H2C_FUNC_DEL_MCC_GROUP; 4413 err = status == RTW89_MAC_MCC_DEL_GROUP_FAIL; 4414 break; 4415 case RTW89_MAC_MCC_RESET_GROUP_OK: 4416 case RTW89_MAC_MCC_RESET_GROUP_FAIL: 4417 func = H2C_FUNC_RESET_MCC_GROUP; 4418 err = status == RTW89_MAC_MCC_RESET_GROUP_FAIL; 4419 break; 4420 case RTW89_MAC_MCC_SWITCH_CH_OK: 4421 case RTW89_MAC_MCC_SWITCH_CH_FAIL: 4422 case RTW89_MAC_MCC_TXNULL0_OK: 4423 case RTW89_MAC_MCC_TXNULL0_FAIL: 4424 case RTW89_MAC_MCC_TXNULL1_OK: 4425 case RTW89_MAC_MCC_TXNULL1_FAIL: 4426 case RTW89_MAC_MCC_SWITCH_EARLY: 4427 case RTW89_MAC_MCC_TBTT: 4428 case RTW89_MAC_MCC_DURATION_START: 4429 case RTW89_MAC_MCC_DURATION_END: 4430 rsp = false; 4431 break; 4432 default: 4433 rtw89_debug(rtwdev, RTW89_DBG_CHAN, 4434 "invalid MCC C2H STS RPT: status %d\n", status); 4435 return; 4436 } 4437 4438 rtw89_debug(rtwdev, RTW89_DBG_CHAN, 4439 "MCC C2H STS RPT: group %d, macid %d, status %d, tsf %llu\n", 4440 group, macid, status, (u64)tsf_high << 32 | tsf_low); 4441 4442 if (!rsp) 4443 return; 4444 4445 data.err = err; 4446 cond = RTW89_MCC_WAIT_COND(group, func); 4447 rtw89_complete_cond(&rtwdev->mcc.wait, cond, &data); 4448 } 4449 4450 static 4451 void (* const rtw89_mac_c2h_ofld_handler[])(struct rtw89_dev *rtwdev, 4452 struct sk_buff *c2h, u32 len) = { 4453 [RTW89_MAC_C2H_FUNC_EFUSE_DUMP] = NULL, 4454 [RTW89_MAC_C2H_FUNC_READ_RSP] = NULL, 4455 [RTW89_MAC_C2H_FUNC_PKT_OFLD_RSP] = rtw89_mac_c2h_pkt_ofld_rsp, 4456 [RTW89_MAC_C2H_FUNC_BCN_RESEND] = NULL, 4457 [RTW89_MAC_C2H_FUNC_MACID_PAUSE] = rtw89_mac_c2h_macid_pause, 4458 [RTW89_MAC_C2H_FUNC_SCANOFLD_RSP] = rtw89_mac_c2h_scanofld_rsp, 4459 [RTW89_MAC_C2H_FUNC_TSF32_TOGL_RPT] = rtw89_mac_c2h_tsf32_toggle_rpt, 4460 }; 4461 4462 static 4463 void (* const rtw89_mac_c2h_info_handler[])(struct rtw89_dev *rtwdev, 4464 struct sk_buff *c2h, u32 len) = { 4465 [RTW89_MAC_C2H_FUNC_REC_ACK] = rtw89_mac_c2h_rec_ack, 4466 [RTW89_MAC_C2H_FUNC_DONE_ACK] = rtw89_mac_c2h_done_ack, 4467 [RTW89_MAC_C2H_FUNC_C2H_LOG] = rtw89_mac_c2h_log, 4468 [RTW89_MAC_C2H_FUNC_BCN_CNT] = rtw89_mac_c2h_bcn_cnt, 4469 }; 4470 4471 static 4472 void (* const rtw89_mac_c2h_mcc_handler[])(struct rtw89_dev *rtwdev, 4473 struct sk_buff *c2h, u32 len) = { 4474 [RTW89_MAC_C2H_FUNC_MCC_RCV_ACK] = rtw89_mac_c2h_mcc_rcv_ack, 4475 [RTW89_MAC_C2H_FUNC_MCC_REQ_ACK] = rtw89_mac_c2h_mcc_req_ack, 4476 [RTW89_MAC_C2H_FUNC_MCC_TSF_RPT] = rtw89_mac_c2h_mcc_tsf_rpt, 4477 [RTW89_MAC_C2H_FUNC_MCC_STATUS_RPT] = rtw89_mac_c2h_mcc_status_rpt, 4478 }; 4479 4480 bool rtw89_mac_c2h_chk_atomic(struct rtw89_dev *rtwdev, u8 class, u8 func) 4481 { 4482 switch (class) { 4483 default: 4484 return false; 4485 case RTW89_MAC_C2H_CLASS_MCC: 4486 return true; 4487 } 4488 } 4489 4490 void rtw89_mac_c2h_handle(struct rtw89_dev *rtwdev, struct sk_buff *skb, 4491 u32 len, u8 class, u8 func) 4492 { 4493 void (*handler)(struct rtw89_dev *rtwdev, 4494 struct sk_buff *c2h, u32 len) = NULL; 4495 4496 switch (class) { 4497 case RTW89_MAC_C2H_CLASS_INFO: 4498 if (func < RTW89_MAC_C2H_FUNC_INFO_MAX) 4499 handler = rtw89_mac_c2h_info_handler[func]; 4500 break; 4501 case RTW89_MAC_C2H_CLASS_OFLD: 4502 if (func < RTW89_MAC_C2H_FUNC_OFLD_MAX) 4503 handler = rtw89_mac_c2h_ofld_handler[func]; 4504 break; 4505 case RTW89_MAC_C2H_CLASS_MCC: 4506 if (func < NUM_OF_RTW89_MAC_C2H_FUNC_MCC) 4507 handler = rtw89_mac_c2h_mcc_handler[func]; 4508 break; 4509 case RTW89_MAC_C2H_CLASS_FWDBG: 4510 return; 4511 default: 4512 rtw89_info(rtwdev, "c2h class %d not support\n", class); 4513 return; 4514 } 4515 if (!handler) { 4516 rtw89_info(rtwdev, "c2h class %d func %d not support\n", class, 4517 func); 4518 return; 4519 } 4520 handler(rtwdev, skb, len); 4521 } 4522 4523 bool rtw89_mac_get_txpwr_cr(struct rtw89_dev *rtwdev, 4524 enum rtw89_phy_idx phy_idx, 4525 u32 reg_base, u32 *cr) 4526 { 4527 const struct rtw89_dle_mem *dle_mem = rtwdev->chip->dle_mem; 4528 enum rtw89_qta_mode mode = dle_mem->mode; 4529 u32 addr = rtw89_mac_reg_by_idx(reg_base, phy_idx); 4530 4531 if (addr < R_AX_PWR_RATE_CTRL || addr > CMAC1_END_ADDR) { 4532 rtw89_err(rtwdev, "[TXPWR] addr=0x%x exceed txpwr cr\n", 4533 addr); 4534 goto error; 4535 } 4536 4537 if (addr >= CMAC1_START_ADDR && addr <= CMAC1_END_ADDR) 4538 if (mode == RTW89_QTA_SCC) { 4539 rtw89_err(rtwdev, 4540 "[TXPWR] addr=0x%x but hw not enable\n", 4541 addr); 4542 goto error; 4543 } 4544 4545 *cr = addr; 4546 return true; 4547 4548 error: 4549 rtw89_err(rtwdev, "[TXPWR] check txpwr cr 0x%x(phy%d) fail\n", 4550 addr, phy_idx); 4551 4552 return false; 4553 } 4554 EXPORT_SYMBOL(rtw89_mac_get_txpwr_cr); 4555 4556 int rtw89_mac_cfg_ppdu_status(struct rtw89_dev *rtwdev, u8 mac_idx, bool enable) 4557 { 4558 u32 reg = rtw89_mac_reg_by_idx(R_AX_PPDU_STAT, mac_idx); 4559 int ret; 4560 4561 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL); 4562 if (ret) 4563 return ret; 4564 4565 if (!enable) { 4566 rtw89_write32_clr(rtwdev, reg, B_AX_PPDU_STAT_RPT_EN); 4567 return 0; 4568 } 4569 4570 rtw89_write32(rtwdev, reg, B_AX_PPDU_STAT_RPT_EN | 4571 B_AX_APP_MAC_INFO_RPT | 4572 B_AX_APP_RX_CNT_RPT | B_AX_APP_PLCP_HDR_RPT | 4573 B_AX_PPDU_STAT_RPT_CRC32); 4574 rtw89_write32_mask(rtwdev, R_AX_HW_RPT_FWD, B_AX_FWD_PPDU_STAT_MASK, 4575 RTW89_PRPT_DEST_HOST); 4576 4577 return 0; 4578 } 4579 EXPORT_SYMBOL(rtw89_mac_cfg_ppdu_status); 4580 4581 void rtw89_mac_update_rts_threshold(struct rtw89_dev *rtwdev, u8 mac_idx) 4582 { 4583 #define MAC_AX_TIME_TH_SH 5 4584 #define MAC_AX_LEN_TH_SH 4 4585 #define MAC_AX_TIME_TH_MAX 255 4586 #define MAC_AX_LEN_TH_MAX 255 4587 #define MAC_AX_TIME_TH_DEF 88 4588 #define MAC_AX_LEN_TH_DEF 4080 4589 struct ieee80211_hw *hw = rtwdev->hw; 4590 u32 rts_threshold = hw->wiphy->rts_threshold; 4591 u32 time_th, len_th; 4592 u32 reg; 4593 4594 if (rts_threshold == (u32)-1) { 4595 time_th = MAC_AX_TIME_TH_DEF; 4596 len_th = MAC_AX_LEN_TH_DEF; 4597 } else { 4598 time_th = MAC_AX_TIME_TH_MAX << MAC_AX_TIME_TH_SH; 4599 len_th = rts_threshold; 4600 } 4601 4602 time_th = min_t(u32, time_th >> MAC_AX_TIME_TH_SH, MAC_AX_TIME_TH_MAX); 4603 len_th = min_t(u32, len_th >> MAC_AX_LEN_TH_SH, MAC_AX_LEN_TH_MAX); 4604 4605 reg = rtw89_mac_reg_by_idx(R_AX_AGG_LEN_HT_0, mac_idx); 4606 rtw89_write16_mask(rtwdev, reg, B_AX_RTS_TXTIME_TH_MASK, time_th); 4607 rtw89_write16_mask(rtwdev, reg, B_AX_RTS_LEN_TH_MASK, len_th); 4608 } 4609 4610 void rtw89_mac_flush_txq(struct rtw89_dev *rtwdev, u32 queues, bool drop) 4611 { 4612 bool empty; 4613 int ret; 4614 4615 if (!test_bit(RTW89_FLAG_POWERON, rtwdev->flags)) 4616 return; 4617 4618 ret = read_poll_timeout(dle_is_txq_empty, empty, empty, 4619 10000, 200000, false, rtwdev); 4620 if (ret && !drop && (rtwdev->total_sta_assoc || rtwdev->scanning)) 4621 rtw89_info(rtwdev, "timed out to flush queues\n"); 4622 } 4623 4624 int rtw89_mac_coex_init(struct rtw89_dev *rtwdev, const struct rtw89_mac_ax_coex *coex) 4625 { 4626 u8 val; 4627 u16 val16; 4628 u32 val32; 4629 int ret; 4630 4631 rtw89_write8_set(rtwdev, R_AX_GPIO_MUXCFG, B_AX_ENBT); 4632 rtw89_write8_set(rtwdev, R_AX_BTC_FUNC_EN, B_AX_PTA_WL_TX_EN); 4633 rtw89_write8_set(rtwdev, R_AX_BT_COEX_CFG_2 + 1, B_AX_GNT_BT_POLARITY >> 8); 4634 rtw89_write8_set(rtwdev, R_AX_CSR_MODE, B_AX_STATIS_BT_EN | B_AX_WL_ACT_MSK); 4635 rtw89_write8_set(rtwdev, R_AX_CSR_MODE + 2, B_AX_BT_CNT_RST >> 16); 4636 rtw89_write8_clr(rtwdev, R_AX_TRXPTCL_RESP_0 + 3, B_AX_RSP_CHK_BTCCA >> 24); 4637 4638 val16 = rtw89_read16(rtwdev, R_AX_CCA_CFG_0); 4639 val16 = (val16 | B_AX_BTCCA_EN) & ~B_AX_BTCCA_BRK_TXOP_EN; 4640 rtw89_write16(rtwdev, R_AX_CCA_CFG_0, val16); 4641 4642 ret = rtw89_mac_read_lte(rtwdev, R_AX_LTE_SW_CFG_2, &val32); 4643 if (ret) { 4644 rtw89_err(rtwdev, "Read R_AX_LTE_SW_CFG_2 fail!\n"); 4645 return ret; 4646 } 4647 val32 = val32 & B_AX_WL_RX_CTRL; 4648 ret = rtw89_mac_write_lte(rtwdev, R_AX_LTE_SW_CFG_2, val32); 4649 if (ret) { 4650 rtw89_err(rtwdev, "Write R_AX_LTE_SW_CFG_2 fail!\n"); 4651 return ret; 4652 } 4653 4654 switch (coex->pta_mode) { 4655 case RTW89_MAC_AX_COEX_RTK_MODE: 4656 val = rtw89_read8(rtwdev, R_AX_GPIO_MUXCFG); 4657 val &= ~B_AX_BTMODE_MASK; 4658 val |= FIELD_PREP(B_AX_BTMODE_MASK, MAC_AX_BT_MODE_0_3); 4659 rtw89_write8(rtwdev, R_AX_GPIO_MUXCFG, val); 4660 4661 val = rtw89_read8(rtwdev, R_AX_TDMA_MODE); 4662 rtw89_write8(rtwdev, R_AX_TDMA_MODE, val | B_AX_RTK_BT_ENABLE); 4663 4664 val = rtw89_read8(rtwdev, R_AX_BT_COEX_CFG_5); 4665 val &= ~B_AX_BT_RPT_SAMPLE_RATE_MASK; 4666 val |= FIELD_PREP(B_AX_BT_RPT_SAMPLE_RATE_MASK, MAC_AX_RTK_RATE); 4667 rtw89_write8(rtwdev, R_AX_BT_COEX_CFG_5, val); 4668 break; 4669 case RTW89_MAC_AX_COEX_CSR_MODE: 4670 val = rtw89_read8(rtwdev, R_AX_GPIO_MUXCFG); 4671 val &= ~B_AX_BTMODE_MASK; 4672 val |= FIELD_PREP(B_AX_BTMODE_MASK, MAC_AX_BT_MODE_2); 4673 rtw89_write8(rtwdev, R_AX_GPIO_MUXCFG, val); 4674 4675 val16 = rtw89_read16(rtwdev, R_AX_CSR_MODE); 4676 val16 &= ~B_AX_BT_PRI_DETECT_TO_MASK; 4677 val16 |= FIELD_PREP(B_AX_BT_PRI_DETECT_TO_MASK, MAC_AX_CSR_PRI_TO); 4678 val16 &= ~B_AX_BT_TRX_INIT_DETECT_MASK; 4679 val16 |= FIELD_PREP(B_AX_BT_TRX_INIT_DETECT_MASK, MAC_AX_CSR_TRX_TO); 4680 val16 &= ~B_AX_BT_STAT_DELAY_MASK; 4681 val16 |= FIELD_PREP(B_AX_BT_STAT_DELAY_MASK, MAC_AX_CSR_DELAY); 4682 val16 |= B_AX_ENHANCED_BT; 4683 rtw89_write16(rtwdev, R_AX_CSR_MODE, val16); 4684 4685 rtw89_write8(rtwdev, R_AX_BT_COEX_CFG_2, MAC_AX_CSR_RATE); 4686 break; 4687 default: 4688 return -EINVAL; 4689 } 4690 4691 switch (coex->direction) { 4692 case RTW89_MAC_AX_COEX_INNER: 4693 val = rtw89_read8(rtwdev, R_AX_GPIO_MUXCFG + 1); 4694 val = (val & ~BIT(2)) | BIT(1); 4695 rtw89_write8(rtwdev, R_AX_GPIO_MUXCFG + 1, val); 4696 break; 4697 case RTW89_MAC_AX_COEX_OUTPUT: 4698 val = rtw89_read8(rtwdev, R_AX_GPIO_MUXCFG + 1); 4699 val = val | BIT(1) | BIT(0); 4700 rtw89_write8(rtwdev, R_AX_GPIO_MUXCFG + 1, val); 4701 break; 4702 case RTW89_MAC_AX_COEX_INPUT: 4703 val = rtw89_read8(rtwdev, R_AX_GPIO_MUXCFG + 1); 4704 val = val & ~(BIT(2) | BIT(1)); 4705 rtw89_write8(rtwdev, R_AX_GPIO_MUXCFG + 1, val); 4706 break; 4707 default: 4708 return -EINVAL; 4709 } 4710 4711 return 0; 4712 } 4713 EXPORT_SYMBOL(rtw89_mac_coex_init); 4714 4715 int rtw89_mac_coex_init_v1(struct rtw89_dev *rtwdev, 4716 const struct rtw89_mac_ax_coex *coex) 4717 { 4718 rtw89_write32_set(rtwdev, R_AX_BTC_CFG, 4719 B_AX_BTC_EN | B_AX_BTG_LNA1_GAIN_SEL); 4720 rtw89_write32_set(rtwdev, R_AX_BT_CNT_CFG, B_AX_BT_CNT_EN); 4721 rtw89_write16_set(rtwdev, R_AX_CCA_CFG_0, B_AX_BTCCA_EN); 4722 rtw89_write16_clr(rtwdev, R_AX_CCA_CFG_0, B_AX_BTCCA_BRK_TXOP_EN); 4723 4724 switch (coex->pta_mode) { 4725 case RTW89_MAC_AX_COEX_RTK_MODE: 4726 rtw89_write32_mask(rtwdev, R_AX_BTC_CFG, B_AX_BTC_MODE_MASK, 4727 MAC_AX_RTK_MODE); 4728 rtw89_write32_mask(rtwdev, R_AX_RTK_MODE_CFG_V1, 4729 B_AX_SAMPLE_CLK_MASK, MAC_AX_RTK_RATE); 4730 break; 4731 case RTW89_MAC_AX_COEX_CSR_MODE: 4732 rtw89_write32_mask(rtwdev, R_AX_BTC_CFG, B_AX_BTC_MODE_MASK, 4733 MAC_AX_CSR_MODE); 4734 break; 4735 default: 4736 return -EINVAL; 4737 } 4738 4739 return 0; 4740 } 4741 EXPORT_SYMBOL(rtw89_mac_coex_init_v1); 4742 4743 int rtw89_mac_cfg_gnt(struct rtw89_dev *rtwdev, 4744 const struct rtw89_mac_ax_coex_gnt *gnt_cfg) 4745 { 4746 u32 val = 0, ret; 4747 4748 if (gnt_cfg->band[0].gnt_bt) 4749 val |= B_AX_GNT_BT_RFC_S0_SW_VAL | B_AX_GNT_BT_BB_S0_SW_VAL; 4750 4751 if (gnt_cfg->band[0].gnt_bt_sw_en) 4752 val |= B_AX_GNT_BT_RFC_S0_SW_CTRL | B_AX_GNT_BT_BB_S0_SW_CTRL; 4753 4754 if (gnt_cfg->band[0].gnt_wl) 4755 val |= B_AX_GNT_WL_RFC_S0_SW_VAL | B_AX_GNT_WL_BB_S0_SW_VAL; 4756 4757 if (gnt_cfg->band[0].gnt_wl_sw_en) 4758 val |= B_AX_GNT_WL_RFC_S0_SW_CTRL | B_AX_GNT_WL_BB_S0_SW_CTRL; 4759 4760 if (gnt_cfg->band[1].gnt_bt) 4761 val |= B_AX_GNT_BT_RFC_S1_SW_VAL | B_AX_GNT_BT_BB_S1_SW_VAL; 4762 4763 if (gnt_cfg->band[1].gnt_bt_sw_en) 4764 val |= B_AX_GNT_BT_RFC_S1_SW_CTRL | B_AX_GNT_BT_BB_S1_SW_CTRL; 4765 4766 if (gnt_cfg->band[1].gnt_wl) 4767 val |= B_AX_GNT_WL_RFC_S1_SW_VAL | B_AX_GNT_WL_BB_S1_SW_VAL; 4768 4769 if (gnt_cfg->band[1].gnt_wl_sw_en) 4770 val |= B_AX_GNT_WL_RFC_S1_SW_CTRL | B_AX_GNT_WL_BB_S1_SW_CTRL; 4771 4772 ret = rtw89_mac_write_lte(rtwdev, R_AX_LTE_SW_CFG_1, val); 4773 if (ret) { 4774 rtw89_err(rtwdev, "Write LTE fail!\n"); 4775 return ret; 4776 } 4777 4778 return 0; 4779 } 4780 EXPORT_SYMBOL(rtw89_mac_cfg_gnt); 4781 4782 int rtw89_mac_cfg_gnt_v1(struct rtw89_dev *rtwdev, 4783 const struct rtw89_mac_ax_coex_gnt *gnt_cfg) 4784 { 4785 u32 val = 0; 4786 4787 if (gnt_cfg->band[0].gnt_bt) 4788 val |= B_AX_GNT_BT_RFC_S0_VAL | B_AX_GNT_BT_RX_VAL | 4789 B_AX_GNT_BT_TX_VAL; 4790 else 4791 val |= B_AX_WL_ACT_VAL; 4792 4793 if (gnt_cfg->band[0].gnt_bt_sw_en) 4794 val |= B_AX_GNT_BT_RFC_S0_SWCTRL | B_AX_GNT_BT_RX_SWCTRL | 4795 B_AX_GNT_BT_TX_SWCTRL | B_AX_WL_ACT_SWCTRL; 4796 4797 if (gnt_cfg->band[0].gnt_wl) 4798 val |= B_AX_GNT_WL_RFC_S0_VAL | B_AX_GNT_WL_RX_VAL | 4799 B_AX_GNT_WL_TX_VAL | B_AX_GNT_WL_BB_VAL; 4800 4801 if (gnt_cfg->band[0].gnt_wl_sw_en) 4802 val |= B_AX_GNT_WL_RFC_S0_SWCTRL | B_AX_GNT_WL_RX_SWCTRL | 4803 B_AX_GNT_WL_TX_SWCTRL | B_AX_GNT_WL_BB_SWCTRL; 4804 4805 if (gnt_cfg->band[1].gnt_bt) 4806 val |= B_AX_GNT_BT_RFC_S1_VAL | B_AX_GNT_BT_RX_VAL | 4807 B_AX_GNT_BT_TX_VAL; 4808 else 4809 val |= B_AX_WL_ACT_VAL; 4810 4811 if (gnt_cfg->band[1].gnt_bt_sw_en) 4812 val |= B_AX_GNT_BT_RFC_S1_SWCTRL | B_AX_GNT_BT_RX_SWCTRL | 4813 B_AX_GNT_BT_TX_SWCTRL | B_AX_WL_ACT_SWCTRL; 4814 4815 if (gnt_cfg->band[1].gnt_wl) 4816 val |= B_AX_GNT_WL_RFC_S1_VAL | B_AX_GNT_WL_RX_VAL | 4817 B_AX_GNT_WL_TX_VAL | B_AX_GNT_WL_BB_VAL; 4818 4819 if (gnt_cfg->band[1].gnt_wl_sw_en) 4820 val |= B_AX_GNT_WL_RFC_S1_SWCTRL | B_AX_GNT_WL_RX_SWCTRL | 4821 B_AX_GNT_WL_TX_SWCTRL | B_AX_GNT_WL_BB_SWCTRL; 4822 4823 rtw89_write32(rtwdev, R_AX_GNT_SW_CTRL, val); 4824 4825 return 0; 4826 } 4827 EXPORT_SYMBOL(rtw89_mac_cfg_gnt_v1); 4828 4829 int rtw89_mac_cfg_plt(struct rtw89_dev *rtwdev, struct rtw89_mac_ax_plt *plt) 4830 { 4831 u32 reg; 4832 u16 val; 4833 int ret; 4834 4835 ret = rtw89_mac_check_mac_en(rtwdev, plt->band, RTW89_CMAC_SEL); 4836 if (ret) 4837 return ret; 4838 4839 reg = rtw89_mac_reg_by_idx(R_AX_BT_PLT, plt->band); 4840 val = (plt->tx & RTW89_MAC_AX_PLT_LTE_RX ? B_AX_TX_PLT_GNT_LTE_RX : 0) | 4841 (plt->tx & RTW89_MAC_AX_PLT_GNT_BT_TX ? B_AX_TX_PLT_GNT_BT_TX : 0) | 4842 (plt->tx & RTW89_MAC_AX_PLT_GNT_BT_RX ? B_AX_TX_PLT_GNT_BT_RX : 0) | 4843 (plt->tx & RTW89_MAC_AX_PLT_GNT_WL ? B_AX_TX_PLT_GNT_WL : 0) | 4844 (plt->rx & RTW89_MAC_AX_PLT_LTE_RX ? B_AX_RX_PLT_GNT_LTE_RX : 0) | 4845 (plt->rx & RTW89_MAC_AX_PLT_GNT_BT_TX ? B_AX_RX_PLT_GNT_BT_TX : 0) | 4846 (plt->rx & RTW89_MAC_AX_PLT_GNT_BT_RX ? B_AX_RX_PLT_GNT_BT_RX : 0) | 4847 (plt->rx & RTW89_MAC_AX_PLT_GNT_WL ? B_AX_RX_PLT_GNT_WL : 0) | 4848 B_AX_PLT_EN; 4849 rtw89_write16(rtwdev, reg, val); 4850 4851 return 0; 4852 } 4853 4854 void rtw89_mac_cfg_sb(struct rtw89_dev *rtwdev, u32 val) 4855 { 4856 u32 fw_sb; 4857 4858 fw_sb = rtw89_read32(rtwdev, R_AX_SCOREBOARD); 4859 fw_sb = FIELD_GET(B_MAC_AX_SB_FW_MASK, fw_sb); 4860 fw_sb = fw_sb & ~B_MAC_AX_BTGS1_NOTIFY; 4861 if (!test_bit(RTW89_FLAG_POWERON, rtwdev->flags)) 4862 fw_sb = fw_sb | MAC_AX_NOTIFY_PWR_MAJOR; 4863 else 4864 fw_sb = fw_sb | MAC_AX_NOTIFY_TP_MAJOR; 4865 val = FIELD_GET(B_MAC_AX_SB_DRV_MASK, val); 4866 val = B_AX_TOGGLE | 4867 FIELD_PREP(B_MAC_AX_SB_DRV_MASK, val) | 4868 FIELD_PREP(B_MAC_AX_SB_FW_MASK, fw_sb); 4869 rtw89_write32(rtwdev, R_AX_SCOREBOARD, val); 4870 fsleep(1000); /* avoid BT FW loss information */ 4871 } 4872 4873 u32 rtw89_mac_get_sb(struct rtw89_dev *rtwdev) 4874 { 4875 return rtw89_read32(rtwdev, R_AX_SCOREBOARD); 4876 } 4877 4878 int rtw89_mac_cfg_ctrl_path(struct rtw89_dev *rtwdev, bool wl) 4879 { 4880 u8 val = rtw89_read8(rtwdev, R_AX_SYS_SDIO_CTRL + 3); 4881 4882 val = wl ? val | BIT(2) : val & ~BIT(2); 4883 rtw89_write8(rtwdev, R_AX_SYS_SDIO_CTRL + 3, val); 4884 4885 return 0; 4886 } 4887 EXPORT_SYMBOL(rtw89_mac_cfg_ctrl_path); 4888 4889 int rtw89_mac_cfg_ctrl_path_v1(struct rtw89_dev *rtwdev, bool wl) 4890 { 4891 struct rtw89_btc *btc = &rtwdev->btc; 4892 struct rtw89_btc_dm *dm = &btc->dm; 4893 struct rtw89_mac_ax_gnt *g = dm->gnt.band; 4894 int i; 4895 4896 if (wl) 4897 return 0; 4898 4899 for (i = 0; i < RTW89_PHY_MAX; i++) { 4900 g[i].gnt_bt_sw_en = 1; 4901 g[i].gnt_bt = 1; 4902 g[i].gnt_wl_sw_en = 1; 4903 g[i].gnt_wl = 0; 4904 } 4905 4906 return rtw89_mac_cfg_gnt_v1(rtwdev, &dm->gnt); 4907 } 4908 EXPORT_SYMBOL(rtw89_mac_cfg_ctrl_path_v1); 4909 4910 bool rtw89_mac_get_ctrl_path(struct rtw89_dev *rtwdev) 4911 { 4912 const struct rtw89_chip_info *chip = rtwdev->chip; 4913 u8 val = 0; 4914 4915 if (chip->chip_id == RTL8852C) 4916 return false; 4917 else if (chip->chip_id == RTL8852A || chip->chip_id == RTL8852B) 4918 val = rtw89_read8_mask(rtwdev, R_AX_SYS_SDIO_CTRL + 3, 4919 B_AX_LTE_MUX_CTRL_PATH >> 24); 4920 4921 return !!val; 4922 } 4923 4924 u16 rtw89_mac_get_plt_cnt(struct rtw89_dev *rtwdev, u8 band) 4925 { 4926 u32 reg; 4927 u16 cnt; 4928 4929 reg = rtw89_mac_reg_by_idx(R_AX_BT_PLT, band); 4930 cnt = rtw89_read32_mask(rtwdev, reg, B_AX_BT_PLT_PKT_CNT_MASK); 4931 rtw89_write16_set(rtwdev, reg, B_AX_BT_PLT_RST); 4932 4933 return cnt; 4934 } 4935 4936 static void rtw89_mac_bfee_standby_timer(struct rtw89_dev *rtwdev, u8 mac_idx, 4937 bool keep) 4938 { 4939 u32 reg; 4940 4941 rtw89_debug(rtwdev, RTW89_DBG_BF, "set bfee standby_timer to %d\n", keep); 4942 reg = rtw89_mac_reg_by_idx(R_AX_BFMEE_RESP_OPTION, mac_idx); 4943 if (keep) { 4944 set_bit(RTW89_FLAG_BFEE_TIMER_KEEP, rtwdev->flags); 4945 rtw89_write32_mask(rtwdev, reg, B_AX_BFMEE_BFRP_RX_STANDBY_TIMER_MASK, 4946 BFRP_RX_STANDBY_TIMER_KEEP); 4947 } else { 4948 clear_bit(RTW89_FLAG_BFEE_TIMER_KEEP, rtwdev->flags); 4949 rtw89_write32_mask(rtwdev, reg, B_AX_BFMEE_BFRP_RX_STANDBY_TIMER_MASK, 4950 BFRP_RX_STANDBY_TIMER_RELEASE); 4951 } 4952 } 4953 4954 static void rtw89_mac_bfee_ctrl(struct rtw89_dev *rtwdev, u8 mac_idx, bool en) 4955 { 4956 u32 reg; 4957 u32 mask = B_AX_BFMEE_HT_NDPA_EN | B_AX_BFMEE_VHT_NDPA_EN | 4958 B_AX_BFMEE_HE_NDPA_EN; 4959 4960 rtw89_debug(rtwdev, RTW89_DBG_BF, "set bfee ndpa_en to %d\n", en); 4961 reg = rtw89_mac_reg_by_idx(R_AX_BFMEE_RESP_OPTION, mac_idx); 4962 if (en) { 4963 set_bit(RTW89_FLAG_BFEE_EN, rtwdev->flags); 4964 rtw89_write32_set(rtwdev, reg, mask); 4965 } else { 4966 clear_bit(RTW89_FLAG_BFEE_EN, rtwdev->flags); 4967 rtw89_write32_clr(rtwdev, reg, mask); 4968 } 4969 } 4970 4971 static int rtw89_mac_init_bfee(struct rtw89_dev *rtwdev, u8 mac_idx) 4972 { 4973 u32 reg; 4974 u32 val32; 4975 int ret; 4976 4977 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL); 4978 if (ret) 4979 return ret; 4980 4981 /* AP mode set tx gid to 63 */ 4982 /* STA mode set tx gid to 0(default) */ 4983 reg = rtw89_mac_reg_by_idx(R_AX_BFMER_CTRL_0, mac_idx); 4984 rtw89_write32_set(rtwdev, reg, B_AX_BFMER_NDP_BFEN); 4985 4986 reg = rtw89_mac_reg_by_idx(R_AX_TRXPTCL_RESP_CSI_RRSC, mac_idx); 4987 rtw89_write32(rtwdev, reg, CSI_RRSC_BMAP); 4988 4989 reg = rtw89_mac_reg_by_idx(R_AX_BFMEE_RESP_OPTION, mac_idx); 4990 val32 = FIELD_PREP(B_AX_BFMEE_NDP_RX_STANDBY_TIMER_MASK, NDP_RX_STANDBY_TIMER); 4991 rtw89_write32(rtwdev, reg, val32); 4992 rtw89_mac_bfee_standby_timer(rtwdev, mac_idx, true); 4993 rtw89_mac_bfee_ctrl(rtwdev, mac_idx, true); 4994 4995 reg = rtw89_mac_reg_by_idx(R_AX_TRXPTCL_RESP_CSI_CTRL_0, mac_idx); 4996 rtw89_write32_set(rtwdev, reg, B_AX_BFMEE_BFPARAM_SEL | 4997 B_AX_BFMEE_USE_NSTS | 4998 B_AX_BFMEE_CSI_GID_SEL | 4999 B_AX_BFMEE_CSI_FORCE_RETE_EN); 5000 reg = rtw89_mac_reg_by_idx(R_AX_TRXPTCL_RESP_CSI_RATE, mac_idx); 5001 rtw89_write32(rtwdev, reg, 5002 u32_encode_bits(CSI_INIT_RATE_HT, B_AX_BFMEE_HT_CSI_RATE_MASK) | 5003 u32_encode_bits(CSI_INIT_RATE_VHT, B_AX_BFMEE_VHT_CSI_RATE_MASK) | 5004 u32_encode_bits(CSI_INIT_RATE_HE, B_AX_BFMEE_HE_CSI_RATE_MASK)); 5005 5006 reg = rtw89_mac_reg_by_idx(R_AX_CSIRPT_OPTION, mac_idx); 5007 rtw89_write32_set(rtwdev, reg, 5008 B_AX_CSIPRT_VHTSU_AID_EN | B_AX_CSIPRT_HESU_AID_EN); 5009 5010 return 0; 5011 } 5012 5013 static int rtw89_mac_set_csi_para_reg(struct rtw89_dev *rtwdev, 5014 struct ieee80211_vif *vif, 5015 struct ieee80211_sta *sta) 5016 { 5017 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 5018 u8 mac_idx = rtwvif->mac_idx; 5019 u8 nc = 1, nr = 3, ng = 0, cb = 1, cs = 1, ldpc_en = 1, stbc_en = 1; 5020 u8 port_sel = rtwvif->port; 5021 u8 sound_dim = 3, t; 5022 u8 *phy_cap = sta->deflink.he_cap.he_cap_elem.phy_cap_info; 5023 u32 reg; 5024 u16 val; 5025 int ret; 5026 5027 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL); 5028 if (ret) 5029 return ret; 5030 5031 if ((phy_cap[3] & IEEE80211_HE_PHY_CAP3_SU_BEAMFORMER) || 5032 (phy_cap[4] & IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER)) { 5033 ldpc_en &= !!(phy_cap[1] & IEEE80211_HE_PHY_CAP1_LDPC_CODING_IN_PAYLOAD); 5034 stbc_en &= !!(phy_cap[2] & IEEE80211_HE_PHY_CAP2_STBC_RX_UNDER_80MHZ); 5035 t = FIELD_GET(IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK, 5036 phy_cap[5]); 5037 sound_dim = min(sound_dim, t); 5038 } 5039 if ((sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) || 5040 (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) { 5041 ldpc_en &= !!(sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXLDPC); 5042 stbc_en &= !!(sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_RXSTBC_MASK); 5043 t = FIELD_GET(IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK, 5044 sta->deflink.vht_cap.cap); 5045 sound_dim = min(sound_dim, t); 5046 } 5047 nc = min(nc, sound_dim); 5048 nr = min(nr, sound_dim); 5049 5050 reg = rtw89_mac_reg_by_idx(R_AX_TRXPTCL_RESP_CSI_CTRL_0, mac_idx); 5051 rtw89_write32_set(rtwdev, reg, B_AX_BFMEE_BFPARAM_SEL); 5052 5053 val = FIELD_PREP(B_AX_BFMEE_CSIINFO0_NC_MASK, nc) | 5054 FIELD_PREP(B_AX_BFMEE_CSIINFO0_NR_MASK, nr) | 5055 FIELD_PREP(B_AX_BFMEE_CSIINFO0_NG_MASK, ng) | 5056 FIELD_PREP(B_AX_BFMEE_CSIINFO0_CB_MASK, cb) | 5057 FIELD_PREP(B_AX_BFMEE_CSIINFO0_CS_MASK, cs) | 5058 FIELD_PREP(B_AX_BFMEE_CSIINFO0_LDPC_EN, ldpc_en) | 5059 FIELD_PREP(B_AX_BFMEE_CSIINFO0_STBC_EN, stbc_en); 5060 5061 if (port_sel == 0) 5062 reg = rtw89_mac_reg_by_idx(R_AX_TRXPTCL_RESP_CSI_CTRL_0, mac_idx); 5063 else 5064 reg = rtw89_mac_reg_by_idx(R_AX_TRXPTCL_RESP_CSI_CTRL_1, mac_idx); 5065 5066 rtw89_write16(rtwdev, reg, val); 5067 5068 return 0; 5069 } 5070 5071 static int rtw89_mac_csi_rrsc(struct rtw89_dev *rtwdev, 5072 struct ieee80211_vif *vif, 5073 struct ieee80211_sta *sta) 5074 { 5075 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 5076 u32 rrsc = BIT(RTW89_MAC_BF_RRSC_6M) | BIT(RTW89_MAC_BF_RRSC_24M); 5077 u32 reg; 5078 u8 mac_idx = rtwvif->mac_idx; 5079 int ret; 5080 5081 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL); 5082 if (ret) 5083 return ret; 5084 5085 if (sta->deflink.he_cap.has_he) { 5086 rrsc |= (BIT(RTW89_MAC_BF_RRSC_HE_MSC0) | 5087 BIT(RTW89_MAC_BF_RRSC_HE_MSC3) | 5088 BIT(RTW89_MAC_BF_RRSC_HE_MSC5)); 5089 } 5090 if (sta->deflink.vht_cap.vht_supported) { 5091 rrsc |= (BIT(RTW89_MAC_BF_RRSC_VHT_MSC0) | 5092 BIT(RTW89_MAC_BF_RRSC_VHT_MSC3) | 5093 BIT(RTW89_MAC_BF_RRSC_VHT_MSC5)); 5094 } 5095 if (sta->deflink.ht_cap.ht_supported) { 5096 rrsc |= (BIT(RTW89_MAC_BF_RRSC_HT_MSC0) | 5097 BIT(RTW89_MAC_BF_RRSC_HT_MSC3) | 5098 BIT(RTW89_MAC_BF_RRSC_HT_MSC5)); 5099 } 5100 reg = rtw89_mac_reg_by_idx(R_AX_TRXPTCL_RESP_CSI_CTRL_0, mac_idx); 5101 rtw89_write32_set(rtwdev, reg, B_AX_BFMEE_BFPARAM_SEL); 5102 rtw89_write32_clr(rtwdev, reg, B_AX_BFMEE_CSI_FORCE_RETE_EN); 5103 rtw89_write32(rtwdev, 5104 rtw89_mac_reg_by_idx(R_AX_TRXPTCL_RESP_CSI_RRSC, mac_idx), 5105 rrsc); 5106 5107 return 0; 5108 } 5109 5110 void rtw89_mac_bf_assoc(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif, 5111 struct ieee80211_sta *sta) 5112 { 5113 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 5114 5115 if (rtw89_sta_has_beamformer_cap(sta)) { 5116 rtw89_debug(rtwdev, RTW89_DBG_BF, 5117 "initialize bfee for new association\n"); 5118 rtw89_mac_init_bfee(rtwdev, rtwvif->mac_idx); 5119 rtw89_mac_set_csi_para_reg(rtwdev, vif, sta); 5120 rtw89_mac_csi_rrsc(rtwdev, vif, sta); 5121 } 5122 } 5123 5124 void rtw89_mac_bf_disassoc(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif, 5125 struct ieee80211_sta *sta) 5126 { 5127 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 5128 5129 rtw89_mac_bfee_ctrl(rtwdev, rtwvif->mac_idx, false); 5130 } 5131 5132 void rtw89_mac_bf_set_gid_table(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif, 5133 struct ieee80211_bss_conf *conf) 5134 { 5135 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 5136 u8 mac_idx = rtwvif->mac_idx; 5137 __le32 *p; 5138 5139 rtw89_debug(rtwdev, RTW89_DBG_BF, "update bf GID table\n"); 5140 5141 p = (__le32 *)conf->mu_group.membership; 5142 rtw89_write32(rtwdev, rtw89_mac_reg_by_idx(R_AX_GID_POSITION_EN0, mac_idx), 5143 le32_to_cpu(p[0])); 5144 rtw89_write32(rtwdev, rtw89_mac_reg_by_idx(R_AX_GID_POSITION_EN1, mac_idx), 5145 le32_to_cpu(p[1])); 5146 5147 p = (__le32 *)conf->mu_group.position; 5148 rtw89_write32(rtwdev, rtw89_mac_reg_by_idx(R_AX_GID_POSITION0, mac_idx), 5149 le32_to_cpu(p[0])); 5150 rtw89_write32(rtwdev, rtw89_mac_reg_by_idx(R_AX_GID_POSITION1, mac_idx), 5151 le32_to_cpu(p[1])); 5152 rtw89_write32(rtwdev, rtw89_mac_reg_by_idx(R_AX_GID_POSITION2, mac_idx), 5153 le32_to_cpu(p[2])); 5154 rtw89_write32(rtwdev, rtw89_mac_reg_by_idx(R_AX_GID_POSITION3, mac_idx), 5155 le32_to_cpu(p[3])); 5156 } 5157 5158 struct rtw89_mac_bf_monitor_iter_data { 5159 struct rtw89_dev *rtwdev; 5160 struct ieee80211_sta *down_sta; 5161 int count; 5162 }; 5163 5164 static 5165 void rtw89_mac_bf_monitor_calc_iter(void *data, struct ieee80211_sta *sta) 5166 { 5167 struct rtw89_mac_bf_monitor_iter_data *iter_data = 5168 (struct rtw89_mac_bf_monitor_iter_data *)data; 5169 struct ieee80211_sta *down_sta = iter_data->down_sta; 5170 int *count = &iter_data->count; 5171 5172 if (down_sta == sta) 5173 return; 5174 5175 if (rtw89_sta_has_beamformer_cap(sta)) 5176 (*count)++; 5177 } 5178 5179 void rtw89_mac_bf_monitor_calc(struct rtw89_dev *rtwdev, 5180 struct ieee80211_sta *sta, bool disconnect) 5181 { 5182 struct rtw89_mac_bf_monitor_iter_data data; 5183 5184 data.rtwdev = rtwdev; 5185 data.down_sta = disconnect ? sta : NULL; 5186 data.count = 0; 5187 ieee80211_iterate_stations_atomic(rtwdev->hw, 5188 rtw89_mac_bf_monitor_calc_iter, 5189 &data); 5190 5191 rtw89_debug(rtwdev, RTW89_DBG_BF, "bfee STA count=%d\n", data.count); 5192 if (data.count) 5193 set_bit(RTW89_FLAG_BFEE_MON, rtwdev->flags); 5194 else 5195 clear_bit(RTW89_FLAG_BFEE_MON, rtwdev->flags); 5196 } 5197 5198 void _rtw89_mac_bf_monitor_track(struct rtw89_dev *rtwdev) 5199 { 5200 struct rtw89_traffic_stats *stats = &rtwdev->stats; 5201 struct rtw89_vif *rtwvif; 5202 bool en = stats->tx_tfc_lv <= stats->rx_tfc_lv; 5203 bool old = test_bit(RTW89_FLAG_BFEE_EN, rtwdev->flags); 5204 bool keep_timer = true; 5205 bool old_keep_timer; 5206 5207 old_keep_timer = test_bit(RTW89_FLAG_BFEE_TIMER_KEEP, rtwdev->flags); 5208 5209 if (stats->tx_tfc_lv <= RTW89_TFC_LOW && stats->rx_tfc_lv <= RTW89_TFC_LOW) 5210 keep_timer = false; 5211 5212 if (keep_timer != old_keep_timer) { 5213 rtw89_for_each_rtwvif(rtwdev, rtwvif) 5214 rtw89_mac_bfee_standby_timer(rtwdev, rtwvif->mac_idx, 5215 keep_timer); 5216 } 5217 5218 if (en == old) 5219 return; 5220 5221 rtw89_for_each_rtwvif(rtwdev, rtwvif) 5222 rtw89_mac_bfee_ctrl(rtwdev, rtwvif->mac_idx, en); 5223 } 5224 5225 static int 5226 __rtw89_mac_set_tx_time(struct rtw89_dev *rtwdev, struct rtw89_sta *rtwsta, 5227 u32 tx_time) 5228 { 5229 #define MAC_AX_DFLT_TX_TIME 5280 5230 u8 mac_idx = rtwsta->rtwvif->mac_idx; 5231 u32 max_tx_time = tx_time == 0 ? MAC_AX_DFLT_TX_TIME : tx_time; 5232 u32 reg; 5233 int ret = 0; 5234 5235 if (rtwsta->cctl_tx_time) { 5236 rtwsta->ampdu_max_time = (max_tx_time - 512) >> 9; 5237 ret = rtw89_fw_h2c_txtime_cmac_tbl(rtwdev, rtwsta); 5238 } else { 5239 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL); 5240 if (ret) { 5241 rtw89_warn(rtwdev, "failed to check cmac in set txtime\n"); 5242 return ret; 5243 } 5244 5245 reg = rtw89_mac_reg_by_idx(R_AX_AMPDU_AGG_LIMIT, mac_idx); 5246 rtw89_write32_mask(rtwdev, reg, B_AX_AMPDU_MAX_TIME_MASK, 5247 max_tx_time >> 5); 5248 } 5249 5250 return ret; 5251 } 5252 5253 int rtw89_mac_set_tx_time(struct rtw89_dev *rtwdev, struct rtw89_sta *rtwsta, 5254 bool resume, u32 tx_time) 5255 { 5256 int ret = 0; 5257 5258 if (!resume) { 5259 rtwsta->cctl_tx_time = true; 5260 ret = __rtw89_mac_set_tx_time(rtwdev, rtwsta, tx_time); 5261 } else { 5262 ret = __rtw89_mac_set_tx_time(rtwdev, rtwsta, tx_time); 5263 rtwsta->cctl_tx_time = false; 5264 } 5265 5266 return ret; 5267 } 5268 5269 int rtw89_mac_get_tx_time(struct rtw89_dev *rtwdev, struct rtw89_sta *rtwsta, 5270 u32 *tx_time) 5271 { 5272 u8 mac_idx = rtwsta->rtwvif->mac_idx; 5273 u32 reg; 5274 int ret = 0; 5275 5276 if (rtwsta->cctl_tx_time) { 5277 *tx_time = (rtwsta->ampdu_max_time + 1) << 9; 5278 } else { 5279 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL); 5280 if (ret) { 5281 rtw89_warn(rtwdev, "failed to check cmac in tx_time\n"); 5282 return ret; 5283 } 5284 5285 reg = rtw89_mac_reg_by_idx(R_AX_AMPDU_AGG_LIMIT, mac_idx); 5286 *tx_time = rtw89_read32_mask(rtwdev, reg, B_AX_AMPDU_MAX_TIME_MASK) << 5; 5287 } 5288 5289 return ret; 5290 } 5291 5292 int rtw89_mac_set_tx_retry_limit(struct rtw89_dev *rtwdev, 5293 struct rtw89_sta *rtwsta, 5294 bool resume, u8 tx_retry) 5295 { 5296 int ret = 0; 5297 5298 rtwsta->data_tx_cnt_lmt = tx_retry; 5299 5300 if (!resume) { 5301 rtwsta->cctl_tx_retry_limit = true; 5302 ret = rtw89_fw_h2c_txtime_cmac_tbl(rtwdev, rtwsta); 5303 } else { 5304 ret = rtw89_fw_h2c_txtime_cmac_tbl(rtwdev, rtwsta); 5305 rtwsta->cctl_tx_retry_limit = false; 5306 } 5307 5308 return ret; 5309 } 5310 5311 int rtw89_mac_get_tx_retry_limit(struct rtw89_dev *rtwdev, 5312 struct rtw89_sta *rtwsta, u8 *tx_retry) 5313 { 5314 u8 mac_idx = rtwsta->rtwvif->mac_idx; 5315 u32 reg; 5316 int ret = 0; 5317 5318 if (rtwsta->cctl_tx_retry_limit) { 5319 *tx_retry = rtwsta->data_tx_cnt_lmt; 5320 } else { 5321 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL); 5322 if (ret) { 5323 rtw89_warn(rtwdev, "failed to check cmac in rty_lmt\n"); 5324 return ret; 5325 } 5326 5327 reg = rtw89_mac_reg_by_idx(R_AX_TXCNT, mac_idx); 5328 *tx_retry = rtw89_read32_mask(rtwdev, reg, B_AX_L_TXCNT_LMT_MASK); 5329 } 5330 5331 return ret; 5332 } 5333 5334 int rtw89_mac_set_hw_muedca_ctrl(struct rtw89_dev *rtwdev, 5335 struct rtw89_vif *rtwvif, bool en) 5336 { 5337 u8 mac_idx = rtwvif->mac_idx; 5338 u16 set = B_AX_MUEDCA_EN_0 | B_AX_SET_MUEDCATIMER_TF_0; 5339 u32 reg; 5340 u32 ret; 5341 5342 ret = rtw89_mac_check_mac_en(rtwdev, mac_idx, RTW89_CMAC_SEL); 5343 if (ret) 5344 return ret; 5345 5346 reg = rtw89_mac_reg_by_idx(R_AX_MUEDCA_EN, mac_idx); 5347 if (en) 5348 rtw89_write16_set(rtwdev, reg, set); 5349 else 5350 rtw89_write16_clr(rtwdev, reg, set); 5351 5352 return 0; 5353 } 5354 5355 int rtw89_mac_write_xtal_si(struct rtw89_dev *rtwdev, u8 offset, u8 val, u8 mask) 5356 { 5357 u32 val32; 5358 int ret; 5359 5360 val32 = FIELD_PREP(B_AX_WL_XTAL_SI_ADDR_MASK, offset) | 5361 FIELD_PREP(B_AX_WL_XTAL_SI_DATA_MASK, val) | 5362 FIELD_PREP(B_AX_WL_XTAL_SI_BITMASK_MASK, mask) | 5363 FIELD_PREP(B_AX_WL_XTAL_SI_MODE_MASK, XTAL_SI_NORMAL_WRITE) | 5364 FIELD_PREP(B_AX_WL_XTAL_SI_CMD_POLL, 1); 5365 rtw89_write32(rtwdev, R_AX_WLAN_XTAL_SI_CTRL, val32); 5366 5367 ret = read_poll_timeout(rtw89_read32, val32, !(val32 & B_AX_WL_XTAL_SI_CMD_POLL), 5368 50, 50000, false, rtwdev, R_AX_WLAN_XTAL_SI_CTRL); 5369 if (ret) { 5370 rtw89_warn(rtwdev, "xtal si not ready(W): offset=%x val=%x mask=%x\n", 5371 offset, val, mask); 5372 return ret; 5373 } 5374 5375 return 0; 5376 } 5377 EXPORT_SYMBOL(rtw89_mac_write_xtal_si); 5378 5379 int rtw89_mac_read_xtal_si(struct rtw89_dev *rtwdev, u8 offset, u8 *val) 5380 { 5381 u32 val32; 5382 int ret; 5383 5384 val32 = FIELD_PREP(B_AX_WL_XTAL_SI_ADDR_MASK, offset) | 5385 FIELD_PREP(B_AX_WL_XTAL_SI_DATA_MASK, 0x00) | 5386 FIELD_PREP(B_AX_WL_XTAL_SI_BITMASK_MASK, 0x00) | 5387 FIELD_PREP(B_AX_WL_XTAL_SI_MODE_MASK, XTAL_SI_NORMAL_READ) | 5388 FIELD_PREP(B_AX_WL_XTAL_SI_CMD_POLL, 1); 5389 rtw89_write32(rtwdev, R_AX_WLAN_XTAL_SI_CTRL, val32); 5390 5391 ret = read_poll_timeout(rtw89_read32, val32, !(val32 & B_AX_WL_XTAL_SI_CMD_POLL), 5392 50, 50000, false, rtwdev, R_AX_WLAN_XTAL_SI_CTRL); 5393 if (ret) { 5394 rtw89_warn(rtwdev, "xtal si not ready(R): offset=%x\n", offset); 5395 return ret; 5396 } 5397 5398 *val = rtw89_read8(rtwdev, R_AX_WLAN_XTAL_SI_CTRL + 1); 5399 5400 return 0; 5401 } 5402 EXPORT_SYMBOL(rtw89_mac_read_xtal_si); 5403 5404 static 5405 void rtw89_mac_pkt_drop_sta(struct rtw89_dev *rtwdev, struct rtw89_sta *rtwsta) 5406 { 5407 static const enum rtw89_pkt_drop_sel sels[] = { 5408 RTW89_PKT_DROP_SEL_MACID_BE_ONCE, 5409 RTW89_PKT_DROP_SEL_MACID_BK_ONCE, 5410 RTW89_PKT_DROP_SEL_MACID_VI_ONCE, 5411 RTW89_PKT_DROP_SEL_MACID_VO_ONCE, 5412 }; 5413 struct rtw89_vif *rtwvif = rtwsta->rtwvif; 5414 struct rtw89_pkt_drop_params params = {0}; 5415 int i; 5416 5417 params.mac_band = RTW89_MAC_0; 5418 params.macid = rtwsta->mac_id; 5419 params.port = rtwvif->port; 5420 params.mbssid = 0; 5421 params.tf_trs = rtwvif->trigger; 5422 5423 for (i = 0; i < ARRAY_SIZE(sels); i++) { 5424 params.sel = sels[i]; 5425 rtw89_fw_h2c_pkt_drop(rtwdev, ¶ms); 5426 } 5427 } 5428 5429 static void rtw89_mac_pkt_drop_vif_iter(void *data, struct ieee80211_sta *sta) 5430 { 5431 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 5432 struct rtw89_vif *rtwvif = rtwsta->rtwvif; 5433 struct rtw89_dev *rtwdev = rtwvif->rtwdev; 5434 struct rtw89_vif *target = data; 5435 5436 if (rtwvif != target) 5437 return; 5438 5439 rtw89_mac_pkt_drop_sta(rtwdev, rtwsta); 5440 } 5441 5442 void rtw89_mac_pkt_drop_vif(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif) 5443 { 5444 ieee80211_iterate_stations_atomic(rtwdev->hw, 5445 rtw89_mac_pkt_drop_vif_iter, 5446 rtwvif); 5447 } 5448 5449 int rtw89_mac_ptk_drop_by_band_and_wait(struct rtw89_dev *rtwdev, 5450 enum rtw89_mac_idx band) 5451 { 5452 struct rtw89_pkt_drop_params params = {0}; 5453 bool empty; 5454 int i, ret = 0, try_cnt = 3; 5455 5456 params.mac_band = band; 5457 params.sel = RTW89_PKT_DROP_SEL_BAND_ONCE; 5458 5459 for (i = 0; i < try_cnt; i++) { 5460 ret = read_poll_timeout(mac_is_txq_empty, empty, empty, 50, 5461 50000, false, rtwdev); 5462 if (ret && !RTW89_CHK_FW_FEATURE(NO_PACKET_DROP, &rtwdev->fw)) 5463 rtw89_fw_h2c_pkt_drop(rtwdev, ¶ms); 5464 else 5465 return 0; 5466 } 5467 return ret; 5468 } 5469