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 "coex.h" 8 #include "debug.h" 9 #include "fw.h" 10 #include "mac.h" 11 #include "phy.h" 12 #include "ps.h" 13 #include "reg.h" 14 #include "sar.h" 15 #include "ser.h" 16 #include "util.h" 17 #include "wow.h" 18 19 static void rtw89_ops_tx(struct ieee80211_hw *hw, 20 struct ieee80211_tx_control *control, 21 struct sk_buff *skb) 22 { 23 struct rtw89_dev *rtwdev = hw->priv; 24 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 25 struct ieee80211_vif *vif = info->control.vif; 26 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 27 struct ieee80211_sta *sta = control->sta; 28 u32 flags = IEEE80211_SKB_CB(skb)->flags; 29 int ret, qsel; 30 31 if (rtwvif->offchan && !(flags & IEEE80211_TX_CTL_TX_OFFCHAN) && sta) { 32 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 33 34 rtw89_debug(rtwdev, RTW89_DBG_TXRX, "ops_tx during offchan\n"); 35 skb_queue_tail(&rtwsta->roc_queue, skb); 36 return; 37 } 38 39 ret = rtw89_core_tx_write(rtwdev, vif, sta, skb, &qsel); 40 if (ret) { 41 rtw89_err(rtwdev, "failed to transmit skb: %d\n", ret); 42 ieee80211_free_txskb(hw, skb); 43 return; 44 } 45 rtw89_core_tx_kick_off(rtwdev, qsel); 46 } 47 48 static void rtw89_ops_wake_tx_queue(struct ieee80211_hw *hw, 49 struct ieee80211_txq *txq) 50 { 51 struct rtw89_dev *rtwdev = hw->priv; 52 53 ieee80211_schedule_txq(hw, txq); 54 queue_work(rtwdev->txq_wq, &rtwdev->txq_work); 55 } 56 57 static int rtw89_ops_start(struct ieee80211_hw *hw) 58 { 59 struct rtw89_dev *rtwdev = hw->priv; 60 int ret; 61 62 mutex_lock(&rtwdev->mutex); 63 ret = rtw89_core_start(rtwdev); 64 mutex_unlock(&rtwdev->mutex); 65 66 return ret; 67 } 68 69 static void rtw89_ops_stop(struct ieee80211_hw *hw) 70 { 71 struct rtw89_dev *rtwdev = hw->priv; 72 73 mutex_lock(&rtwdev->mutex); 74 rtw89_core_stop(rtwdev); 75 mutex_unlock(&rtwdev->mutex); 76 } 77 78 static int rtw89_ops_config(struct ieee80211_hw *hw, u32 changed) 79 { 80 struct rtw89_dev *rtwdev = hw->priv; 81 82 /* let previous ips work finish to ensure we don't leave ips twice */ 83 cancel_work_sync(&rtwdev->ips_work); 84 85 mutex_lock(&rtwdev->mutex); 86 rtw89_leave_ps_mode(rtwdev); 87 88 if ((changed & IEEE80211_CONF_CHANGE_IDLE) && 89 !(hw->conf.flags & IEEE80211_CONF_IDLE)) 90 rtw89_leave_ips(rtwdev); 91 92 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) { 93 rtw89_config_entity_chandef(rtwdev, RTW89_SUB_ENTITY_0, 94 &hw->conf.chandef); 95 rtw89_set_channel(rtwdev); 96 } 97 98 if ((changed & IEEE80211_CONF_CHANGE_IDLE) && 99 (hw->conf.flags & IEEE80211_CONF_IDLE) && 100 !rtwdev->scanning) 101 rtw89_enter_ips(rtwdev); 102 103 mutex_unlock(&rtwdev->mutex); 104 105 return 0; 106 } 107 108 static int rtw89_ops_add_interface(struct ieee80211_hw *hw, 109 struct ieee80211_vif *vif) 110 { 111 struct rtw89_dev *rtwdev = hw->priv; 112 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 113 int ret = 0; 114 115 rtw89_debug(rtwdev, RTW89_DBG_STATE, "add vif %pM type %d, p2p %d\n", 116 vif->addr, vif->type, vif->p2p); 117 118 mutex_lock(&rtwdev->mutex); 119 120 rtw89_leave_ips_by_hwflags(rtwdev); 121 122 if (RTW89_CHK_FW_FEATURE(BEACON_FILTER, &rtwdev->fw)) 123 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER | 124 IEEE80211_VIF_SUPPORTS_CQM_RSSI; 125 126 rtwvif->rtwdev = rtwdev; 127 rtwvif->roc.state = RTW89_ROC_IDLE; 128 rtwvif->offchan = false; 129 list_add_tail(&rtwvif->list, &rtwdev->rtwvifs_list); 130 INIT_WORK(&rtwvif->update_beacon_work, rtw89_core_update_beacon_work); 131 INIT_DELAYED_WORK(&rtwvif->roc.roc_work, rtw89_roc_work); 132 rtw89_leave_ps_mode(rtwdev); 133 134 rtw89_traffic_stats_init(rtwdev, &rtwvif->stats); 135 rtw89_vif_type_mapping(vif, false); 136 rtwvif->port = rtw89_core_acquire_bit_map(rtwdev->hw_port, 137 RTW89_PORT_NUM); 138 if (rtwvif->port == RTW89_PORT_NUM) { 139 ret = -ENOSPC; 140 list_del_init(&rtwvif->list); 141 goto out; 142 } 143 144 rtwvif->bcn_hit_cond = 0; 145 rtwvif->mac_idx = RTW89_MAC_0; 146 rtwvif->phy_idx = RTW89_PHY_0; 147 rtwvif->sub_entity_idx = RTW89_SUB_ENTITY_0; 148 rtwvif->hit_rule = 0; 149 ether_addr_copy(rtwvif->mac_addr, vif->addr); 150 INIT_LIST_HEAD(&rtwvif->general_pkt_list); 151 152 ret = rtw89_mac_add_vif(rtwdev, rtwvif); 153 if (ret) { 154 rtw89_core_release_bit_map(rtwdev->hw_port, rtwvif->port); 155 list_del_init(&rtwvif->list); 156 goto out; 157 } 158 159 rtw89_core_txq_init(rtwdev, vif->txq); 160 161 rtw89_btc_ntfy_role_info(rtwdev, rtwvif, NULL, BTC_ROLE_START); 162 163 rtw89_recalc_lps(rtwdev); 164 out: 165 mutex_unlock(&rtwdev->mutex); 166 167 return ret; 168 } 169 170 static void rtw89_ops_remove_interface(struct ieee80211_hw *hw, 171 struct ieee80211_vif *vif) 172 { 173 struct rtw89_dev *rtwdev = hw->priv; 174 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 175 176 rtw89_debug(rtwdev, RTW89_DBG_STATE, "remove vif %pM type %d p2p %d\n", 177 vif->addr, vif->type, vif->p2p); 178 179 cancel_work_sync(&rtwvif->update_beacon_work); 180 cancel_delayed_work_sync(&rtwvif->roc.roc_work); 181 182 mutex_lock(&rtwdev->mutex); 183 rtw89_leave_ps_mode(rtwdev); 184 rtw89_btc_ntfy_role_info(rtwdev, rtwvif, NULL, BTC_ROLE_STOP); 185 rtw89_mac_remove_vif(rtwdev, rtwvif); 186 rtw89_core_release_bit_map(rtwdev->hw_port, rtwvif->port); 187 list_del_init(&rtwvif->list); 188 rtw89_recalc_lps(rtwdev); 189 rtw89_enter_ips_by_hwflags(rtwdev); 190 191 mutex_unlock(&rtwdev->mutex); 192 } 193 194 static int rtw89_ops_change_interface(struct ieee80211_hw *hw, 195 struct ieee80211_vif *vif, 196 enum nl80211_iftype type, bool p2p) 197 { 198 struct rtw89_dev *rtwdev = hw->priv; 199 int ret; 200 201 set_bit(RTW89_FLAG_CHANGING_INTERFACE, rtwdev->flags); 202 203 rtw89_debug(rtwdev, RTW89_DBG_STATE, "change vif %pM (%d)->(%d), p2p (%d)->(%d)\n", 204 vif->addr, vif->type, type, vif->p2p, p2p); 205 206 rtw89_ops_remove_interface(hw, vif); 207 208 vif->type = type; 209 vif->p2p = p2p; 210 211 ret = rtw89_ops_add_interface(hw, vif); 212 if (ret) 213 rtw89_warn(rtwdev, "failed to change interface %d\n", ret); 214 215 clear_bit(RTW89_FLAG_CHANGING_INTERFACE, rtwdev->flags); 216 217 return ret; 218 } 219 220 static void rtw89_ops_configure_filter(struct ieee80211_hw *hw, 221 unsigned int changed_flags, 222 unsigned int *new_flags, 223 u64 multicast) 224 { 225 struct rtw89_dev *rtwdev = hw->priv; 226 227 mutex_lock(&rtwdev->mutex); 228 rtw89_leave_ps_mode(rtwdev); 229 230 *new_flags &= FIF_ALLMULTI | FIF_OTHER_BSS | FIF_FCSFAIL | 231 FIF_BCN_PRBRESP_PROMISC | FIF_PROBE_REQ; 232 233 if (changed_flags & FIF_ALLMULTI) { 234 if (*new_flags & FIF_ALLMULTI) 235 rtwdev->hal.rx_fltr &= ~B_AX_A_MC; 236 else 237 rtwdev->hal.rx_fltr |= B_AX_A_MC; 238 } 239 if (changed_flags & FIF_FCSFAIL) { 240 if (*new_flags & FIF_FCSFAIL) 241 rtwdev->hal.rx_fltr |= B_AX_A_CRC32_ERR; 242 else 243 rtwdev->hal.rx_fltr &= ~B_AX_A_CRC32_ERR; 244 } 245 if (changed_flags & FIF_OTHER_BSS) { 246 if (*new_flags & FIF_OTHER_BSS) 247 rtwdev->hal.rx_fltr &= ~B_AX_A_A1_MATCH; 248 else 249 rtwdev->hal.rx_fltr |= B_AX_A_A1_MATCH; 250 } 251 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) { 252 if (*new_flags & FIF_BCN_PRBRESP_PROMISC) { 253 rtwdev->hal.rx_fltr &= ~B_AX_A_BCN_CHK_EN; 254 rtwdev->hal.rx_fltr &= ~B_AX_A_BC; 255 rtwdev->hal.rx_fltr &= ~B_AX_A_A1_MATCH; 256 } else { 257 rtwdev->hal.rx_fltr |= B_AX_A_BCN_CHK_EN; 258 rtwdev->hal.rx_fltr |= B_AX_A_BC; 259 rtwdev->hal.rx_fltr |= B_AX_A_A1_MATCH; 260 } 261 } 262 if (changed_flags & FIF_PROBE_REQ) { 263 if (*new_flags & FIF_PROBE_REQ) { 264 rtwdev->hal.rx_fltr &= ~B_AX_A_BC_CAM_MATCH; 265 rtwdev->hal.rx_fltr &= ~B_AX_A_UC_CAM_MATCH; 266 } else { 267 rtwdev->hal.rx_fltr |= B_AX_A_BC_CAM_MATCH; 268 rtwdev->hal.rx_fltr |= B_AX_A_UC_CAM_MATCH; 269 } 270 } 271 272 rtw89_write32_mask(rtwdev, 273 rtw89_mac_reg_by_idx(R_AX_RX_FLTR_OPT, RTW89_MAC_0), 274 B_AX_RX_FLTR_CFG_MASK, 275 rtwdev->hal.rx_fltr); 276 if (!rtwdev->dbcc_en) 277 goto out; 278 rtw89_write32_mask(rtwdev, 279 rtw89_mac_reg_by_idx(R_AX_RX_FLTR_OPT, RTW89_MAC_1), 280 B_AX_RX_FLTR_CFG_MASK, 281 rtwdev->hal.rx_fltr); 282 283 out: 284 mutex_unlock(&rtwdev->mutex); 285 } 286 287 static const u8 ac_to_fw_idx[IEEE80211_NUM_ACS] = { 288 [IEEE80211_AC_VO] = 3, 289 [IEEE80211_AC_VI] = 2, 290 [IEEE80211_AC_BE] = 0, 291 [IEEE80211_AC_BK] = 1, 292 }; 293 294 static u8 rtw89_aifsn_to_aifs(struct rtw89_dev *rtwdev, 295 struct rtw89_vif *rtwvif, u8 aifsn) 296 { 297 struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif); 298 const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, RTW89_SUB_ENTITY_0); 299 u8 slot_time; 300 u8 sifs; 301 302 slot_time = vif->bss_conf.use_short_slot ? 9 : 20; 303 sifs = chan->band_type == RTW89_BAND_5G ? 16 : 10; 304 305 return aifsn * slot_time + sifs; 306 } 307 308 static void ____rtw89_conf_tx_edca(struct rtw89_dev *rtwdev, 309 struct rtw89_vif *rtwvif, u16 ac) 310 { 311 struct ieee80211_tx_queue_params *params = &rtwvif->tx_params[ac]; 312 u32 val; 313 u8 ecw_max, ecw_min; 314 u8 aifs; 315 316 /* 2^ecw - 1 = cw; ecw = log2(cw + 1) */ 317 ecw_max = ilog2(params->cw_max + 1); 318 ecw_min = ilog2(params->cw_min + 1); 319 aifs = rtw89_aifsn_to_aifs(rtwdev, rtwvif, params->aifs); 320 val = FIELD_PREP(FW_EDCA_PARAM_TXOPLMT_MSK, params->txop) | 321 FIELD_PREP(FW_EDCA_PARAM_CWMAX_MSK, ecw_max) | 322 FIELD_PREP(FW_EDCA_PARAM_CWMIN_MSK, ecw_min) | 323 FIELD_PREP(FW_EDCA_PARAM_AIFS_MSK, aifs); 324 rtw89_fw_h2c_set_edca(rtwdev, rtwvif, ac_to_fw_idx[ac], val); 325 } 326 327 static const u32 ac_to_mu_edca_param[IEEE80211_NUM_ACS] = { 328 [IEEE80211_AC_VO] = R_AX_MUEDCA_VO_PARAM_0, 329 [IEEE80211_AC_VI] = R_AX_MUEDCA_VI_PARAM_0, 330 [IEEE80211_AC_BE] = R_AX_MUEDCA_BE_PARAM_0, 331 [IEEE80211_AC_BK] = R_AX_MUEDCA_BK_PARAM_0, 332 }; 333 334 static void ____rtw89_conf_tx_mu_edca(struct rtw89_dev *rtwdev, 335 struct rtw89_vif *rtwvif, u16 ac) 336 { 337 struct ieee80211_tx_queue_params *params = &rtwvif->tx_params[ac]; 338 struct ieee80211_he_mu_edca_param_ac_rec *mu_edca; 339 u8 aifs, aifsn; 340 u16 timer_32us; 341 u32 reg; 342 u32 val; 343 344 if (!params->mu_edca) 345 return; 346 347 mu_edca = ¶ms->mu_edca_param_rec; 348 aifsn = FIELD_GET(GENMASK(3, 0), mu_edca->aifsn); 349 aifs = aifsn ? rtw89_aifsn_to_aifs(rtwdev, rtwvif, aifsn) : 0; 350 timer_32us = mu_edca->mu_edca_timer << 8; 351 352 val = FIELD_PREP(B_AX_MUEDCA_BE_PARAM_0_TIMER_MASK, timer_32us) | 353 FIELD_PREP(B_AX_MUEDCA_BE_PARAM_0_CW_MASK, mu_edca->ecw_min_max) | 354 FIELD_PREP(B_AX_MUEDCA_BE_PARAM_0_AIFS_MASK, aifs); 355 reg = rtw89_mac_reg_by_idx(ac_to_mu_edca_param[ac], rtwvif->mac_idx); 356 rtw89_write32(rtwdev, reg, val); 357 358 rtw89_mac_set_hw_muedca_ctrl(rtwdev, rtwvif, true); 359 } 360 361 static void __rtw89_conf_tx(struct rtw89_dev *rtwdev, 362 struct rtw89_vif *rtwvif, u16 ac) 363 { 364 ____rtw89_conf_tx_edca(rtwdev, rtwvif, ac); 365 ____rtw89_conf_tx_mu_edca(rtwdev, rtwvif, ac); 366 } 367 368 static void rtw89_conf_tx(struct rtw89_dev *rtwdev, 369 struct rtw89_vif *rtwvif) 370 { 371 u16 ac; 372 373 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 374 __rtw89_conf_tx(rtwdev, rtwvif, ac); 375 } 376 377 static void rtw89_station_mode_sta_assoc(struct rtw89_dev *rtwdev, 378 struct ieee80211_vif *vif, 379 struct ieee80211_bss_conf *conf) 380 { 381 struct ieee80211_sta *sta; 382 383 if (vif->type != NL80211_IFTYPE_STATION) 384 return; 385 386 sta = ieee80211_find_sta(vif, conf->bssid); 387 if (!sta) { 388 rtw89_err(rtwdev, "can't find sta to set sta_assoc state\n"); 389 return; 390 } 391 392 rtw89_vif_type_mapping(vif, true); 393 394 rtw89_core_sta_assoc(rtwdev, vif, sta); 395 } 396 397 static void rtw89_ops_bss_info_changed(struct ieee80211_hw *hw, 398 struct ieee80211_vif *vif, 399 struct ieee80211_bss_conf *conf, 400 u64 changed) 401 { 402 struct rtw89_dev *rtwdev = hw->priv; 403 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 404 405 mutex_lock(&rtwdev->mutex); 406 rtw89_leave_ps_mode(rtwdev); 407 408 if (changed & BSS_CHANGED_ASSOC) { 409 if (vif->cfg.assoc) { 410 rtw89_station_mode_sta_assoc(rtwdev, vif, conf); 411 rtw89_phy_set_bss_color(rtwdev, vif); 412 rtw89_chip_cfg_txpwr_ul_tb_offset(rtwdev, vif); 413 rtw89_mac_port_update(rtwdev, rtwvif); 414 rtw89_mac_set_he_obss_narrow_bw_ru(rtwdev, vif); 415 } else { 416 /* Abort ongoing scan if cancel_scan isn't issued 417 * when disconnected by peer 418 */ 419 if (rtwdev->scanning) 420 rtw89_hw_scan_abort(rtwdev, vif); 421 } 422 } 423 424 if (changed & BSS_CHANGED_BSSID) { 425 ether_addr_copy(rtwvif->bssid, conf->bssid); 426 rtw89_cam_bssid_changed(rtwdev, rtwvif); 427 rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, NULL); 428 } 429 430 if (changed & BSS_CHANGED_BEACON) 431 rtw89_fw_h2c_update_beacon(rtwdev, rtwvif); 432 433 if (changed & BSS_CHANGED_ERP_SLOT) 434 rtw89_conf_tx(rtwdev, rtwvif); 435 436 if (changed & BSS_CHANGED_HE_BSS_COLOR) 437 rtw89_phy_set_bss_color(rtwdev, vif); 438 439 if (changed & BSS_CHANGED_MU_GROUPS) 440 rtw89_mac_bf_set_gid_table(rtwdev, vif, conf); 441 442 if (changed & BSS_CHANGED_P2P_PS) 443 rtw89_process_p2p_ps(rtwdev, vif); 444 445 if (changed & BSS_CHANGED_CQM) 446 rtw89_fw_h2c_set_bcn_fltr_cfg(rtwdev, vif, true); 447 448 if (changed & BSS_CHANGED_PS) 449 rtw89_recalc_lps(rtwdev); 450 451 mutex_unlock(&rtwdev->mutex); 452 } 453 454 static int rtw89_ops_start_ap(struct ieee80211_hw *hw, 455 struct ieee80211_vif *vif, 456 struct ieee80211_bss_conf *link_conf) 457 { 458 struct rtw89_dev *rtwdev = hw->priv; 459 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 460 461 mutex_lock(&rtwdev->mutex); 462 ether_addr_copy(rtwvif->bssid, vif->bss_conf.bssid); 463 rtw89_cam_bssid_changed(rtwdev, rtwvif); 464 rtw89_mac_port_update(rtwdev, rtwvif); 465 rtw89_fw_h2c_assoc_cmac_tbl(rtwdev, vif, NULL); 466 rtw89_fw_h2c_role_maintain(rtwdev, rtwvif, NULL, RTW89_ROLE_TYPE_CHANGE); 467 rtw89_fw_h2c_join_info(rtwdev, rtwvif, NULL, true); 468 rtw89_fw_h2c_cam(rtwdev, rtwvif, NULL, NULL); 469 rtw89_chip_rfk_channel(rtwdev); 470 mutex_unlock(&rtwdev->mutex); 471 472 return 0; 473 } 474 475 static 476 void rtw89_ops_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 477 struct ieee80211_bss_conf *link_conf) 478 { 479 struct rtw89_dev *rtwdev = hw->priv; 480 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 481 482 mutex_lock(&rtwdev->mutex); 483 rtw89_mac_stop_ap(rtwdev, rtwvif); 484 rtw89_fw_h2c_assoc_cmac_tbl(rtwdev, vif, NULL); 485 rtw89_fw_h2c_join_info(rtwdev, rtwvif, NULL, true); 486 mutex_unlock(&rtwdev->mutex); 487 } 488 489 static int rtw89_ops_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, 490 bool set) 491 { 492 struct rtw89_dev *rtwdev = hw->priv; 493 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 494 struct rtw89_vif *rtwvif = rtwsta->rtwvif; 495 496 ieee80211_queue_work(rtwdev->hw, &rtwvif->update_beacon_work); 497 498 return 0; 499 } 500 501 static int rtw89_ops_conf_tx(struct ieee80211_hw *hw, 502 struct ieee80211_vif *vif, 503 unsigned int link_id, u16 ac, 504 const struct ieee80211_tx_queue_params *params) 505 { 506 struct rtw89_dev *rtwdev = hw->priv; 507 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 508 509 mutex_lock(&rtwdev->mutex); 510 rtw89_leave_ps_mode(rtwdev); 511 rtwvif->tx_params[ac] = *params; 512 __rtw89_conf_tx(rtwdev, rtwvif, ac); 513 mutex_unlock(&rtwdev->mutex); 514 515 return 0; 516 } 517 518 static int __rtw89_ops_sta_state(struct ieee80211_hw *hw, 519 struct ieee80211_vif *vif, 520 struct ieee80211_sta *sta, 521 enum ieee80211_sta_state old_state, 522 enum ieee80211_sta_state new_state) 523 { 524 struct rtw89_dev *rtwdev = hw->priv; 525 526 if (old_state == IEEE80211_STA_NOTEXIST && 527 new_state == IEEE80211_STA_NONE) 528 return rtw89_core_sta_add(rtwdev, vif, sta); 529 530 if (old_state == IEEE80211_STA_AUTH && 531 new_state == IEEE80211_STA_ASSOC) { 532 if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) 533 return 0; /* defer to bss_info_changed to have vif info */ 534 return rtw89_core_sta_assoc(rtwdev, vif, sta); 535 } 536 537 if (old_state == IEEE80211_STA_ASSOC && 538 new_state == IEEE80211_STA_AUTH) 539 return rtw89_core_sta_disassoc(rtwdev, vif, sta); 540 541 if (old_state == IEEE80211_STA_AUTH && 542 new_state == IEEE80211_STA_NONE) 543 return rtw89_core_sta_disconnect(rtwdev, vif, sta); 544 545 if (old_state == IEEE80211_STA_NONE && 546 new_state == IEEE80211_STA_NOTEXIST) 547 return rtw89_core_sta_remove(rtwdev, vif, sta); 548 549 return 0; 550 } 551 552 static int rtw89_ops_sta_state(struct ieee80211_hw *hw, 553 struct ieee80211_vif *vif, 554 struct ieee80211_sta *sta, 555 enum ieee80211_sta_state old_state, 556 enum ieee80211_sta_state new_state) 557 { 558 struct rtw89_dev *rtwdev = hw->priv; 559 int ret; 560 561 mutex_lock(&rtwdev->mutex); 562 rtw89_leave_ps_mode(rtwdev); 563 ret = __rtw89_ops_sta_state(hw, vif, sta, old_state, new_state); 564 mutex_unlock(&rtwdev->mutex); 565 566 return ret; 567 } 568 569 static int rtw89_ops_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd, 570 struct ieee80211_vif *vif, 571 struct ieee80211_sta *sta, 572 struct ieee80211_key_conf *key) 573 { 574 struct rtw89_dev *rtwdev = hw->priv; 575 int ret = 0; 576 577 mutex_lock(&rtwdev->mutex); 578 rtw89_leave_ps_mode(rtwdev); 579 580 switch (cmd) { 581 case SET_KEY: 582 rtw89_btc_ntfy_specific_packet(rtwdev, PACKET_EAPOL_END); 583 ret = rtw89_cam_sec_key_add(rtwdev, vif, sta, key); 584 if (ret && ret != -EOPNOTSUPP) { 585 rtw89_err(rtwdev, "failed to add key to sec cam\n"); 586 goto out; 587 } 588 break; 589 case DISABLE_KEY: 590 rtw89_hci_flush_queues(rtwdev, BIT(rtwdev->hw->queues) - 1, 591 false); 592 rtw89_mac_flush_txq(rtwdev, BIT(rtwdev->hw->queues) - 1, false); 593 ret = rtw89_cam_sec_key_del(rtwdev, vif, sta, key, true); 594 if (ret) { 595 rtw89_err(rtwdev, "failed to remove key from sec cam\n"); 596 goto out; 597 } 598 break; 599 } 600 601 out: 602 mutex_unlock(&rtwdev->mutex); 603 604 return ret; 605 } 606 607 static int rtw89_ops_ampdu_action(struct ieee80211_hw *hw, 608 struct ieee80211_vif *vif, 609 struct ieee80211_ampdu_params *params) 610 { 611 struct rtw89_dev *rtwdev = hw->priv; 612 struct ieee80211_sta *sta = params->sta; 613 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 614 u16 tid = params->tid; 615 struct ieee80211_txq *txq = sta->txq[tid]; 616 struct rtw89_txq *rtwtxq = (struct rtw89_txq *)txq->drv_priv; 617 618 switch (params->action) { 619 case IEEE80211_AMPDU_TX_START: 620 return IEEE80211_AMPDU_TX_START_IMMEDIATE; 621 case IEEE80211_AMPDU_TX_STOP_CONT: 622 case IEEE80211_AMPDU_TX_STOP_FLUSH: 623 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT: 624 mutex_lock(&rtwdev->mutex); 625 clear_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags); 626 mutex_unlock(&rtwdev->mutex); 627 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid); 628 break; 629 case IEEE80211_AMPDU_TX_OPERATIONAL: 630 mutex_lock(&rtwdev->mutex); 631 set_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags); 632 rtwsta->ampdu_params[tid].agg_num = params->buf_size; 633 rtwsta->ampdu_params[tid].amsdu = params->amsdu; 634 rtw89_leave_ps_mode(rtwdev); 635 mutex_unlock(&rtwdev->mutex); 636 break; 637 case IEEE80211_AMPDU_RX_START: 638 mutex_lock(&rtwdev->mutex); 639 rtw89_fw_h2c_ba_cam(rtwdev, rtwsta, true, params); 640 mutex_unlock(&rtwdev->mutex); 641 break; 642 case IEEE80211_AMPDU_RX_STOP: 643 mutex_lock(&rtwdev->mutex); 644 rtw89_fw_h2c_ba_cam(rtwdev, rtwsta, false, params); 645 mutex_unlock(&rtwdev->mutex); 646 break; 647 default: 648 WARN_ON(1); 649 return -ENOTSUPP; 650 } 651 652 return 0; 653 } 654 655 static int rtw89_ops_set_rts_threshold(struct ieee80211_hw *hw, u32 value) 656 { 657 struct rtw89_dev *rtwdev = hw->priv; 658 659 mutex_lock(&rtwdev->mutex); 660 rtw89_leave_ps_mode(rtwdev); 661 if (test_bit(RTW89_FLAG_POWERON, rtwdev->flags)) 662 rtw89_mac_update_rts_threshold(rtwdev, RTW89_MAC_0); 663 mutex_unlock(&rtwdev->mutex); 664 665 return 0; 666 } 667 668 static void rtw89_ops_sta_statistics(struct ieee80211_hw *hw, 669 struct ieee80211_vif *vif, 670 struct ieee80211_sta *sta, 671 struct station_info *sinfo) 672 { 673 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 674 675 sinfo->txrate = rtwsta->ra_report.txrate; 676 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE); 677 } 678 679 static 680 void __rtw89_drop_packets(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif) 681 { 682 struct rtw89_vif *rtwvif; 683 684 if (vif) { 685 rtwvif = (struct rtw89_vif *)vif->drv_priv; 686 rtw89_mac_pkt_drop_vif(rtwdev, rtwvif); 687 } else { 688 rtw89_for_each_rtwvif(rtwdev, rtwvif) 689 rtw89_mac_pkt_drop_vif(rtwdev, rtwvif); 690 } 691 } 692 693 static void rtw89_ops_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 694 u32 queues, bool drop) 695 { 696 struct rtw89_dev *rtwdev = hw->priv; 697 698 mutex_lock(&rtwdev->mutex); 699 rtw89_leave_lps(rtwdev); 700 rtw89_hci_flush_queues(rtwdev, queues, drop); 701 702 if (drop && !RTW89_CHK_FW_FEATURE(NO_PACKET_DROP, &rtwdev->fw)) 703 __rtw89_drop_packets(rtwdev, vif); 704 else 705 rtw89_mac_flush_txq(rtwdev, queues, drop); 706 707 mutex_unlock(&rtwdev->mutex); 708 } 709 710 struct rtw89_iter_bitrate_mask_data { 711 struct rtw89_dev *rtwdev; 712 struct ieee80211_vif *vif; 713 const struct cfg80211_bitrate_mask *mask; 714 }; 715 716 static void rtw89_ra_mask_info_update_iter(void *data, struct ieee80211_sta *sta) 717 { 718 struct rtw89_iter_bitrate_mask_data *br_data = data; 719 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 720 struct ieee80211_vif *vif = rtwvif_to_vif(rtwsta->rtwvif); 721 722 if (vif != br_data->vif || vif->p2p) 723 return; 724 725 rtwsta->use_cfg_mask = true; 726 rtwsta->mask = *br_data->mask; 727 rtw89_phy_ra_updata_sta(br_data->rtwdev, sta, IEEE80211_RC_SUPP_RATES_CHANGED); 728 } 729 730 static void rtw89_ra_mask_info_update(struct rtw89_dev *rtwdev, 731 struct ieee80211_vif *vif, 732 const struct cfg80211_bitrate_mask *mask) 733 { 734 struct rtw89_iter_bitrate_mask_data br_data = { .rtwdev = rtwdev, 735 .vif = vif, 736 .mask = mask}; 737 738 ieee80211_iterate_stations_atomic(rtwdev->hw, rtw89_ra_mask_info_update_iter, 739 &br_data); 740 } 741 742 static int rtw89_ops_set_bitrate_mask(struct ieee80211_hw *hw, 743 struct ieee80211_vif *vif, 744 const struct cfg80211_bitrate_mask *mask) 745 { 746 struct rtw89_dev *rtwdev = hw->priv; 747 748 mutex_lock(&rtwdev->mutex); 749 rtw89_phy_rate_pattern_vif(rtwdev, vif, mask); 750 rtw89_ra_mask_info_update(rtwdev, vif, mask); 751 mutex_unlock(&rtwdev->mutex); 752 753 return 0; 754 } 755 756 static 757 int rtw89_ops_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant) 758 { 759 struct rtw89_dev *rtwdev = hw->priv; 760 struct rtw89_hal *hal = &rtwdev->hal; 761 762 if (rx_ant != hw->wiphy->available_antennas_rx && rx_ant != hal->antenna_rx) 763 return -EINVAL; 764 765 mutex_lock(&rtwdev->mutex); 766 hal->antenna_tx = tx_ant; 767 hal->antenna_rx = rx_ant; 768 hal->tx_path_diversity = false; 769 mutex_unlock(&rtwdev->mutex); 770 771 return 0; 772 } 773 774 static 775 int rtw89_ops_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant) 776 { 777 struct rtw89_dev *rtwdev = hw->priv; 778 struct rtw89_hal *hal = &rtwdev->hal; 779 780 *tx_ant = hal->antenna_tx; 781 *rx_ant = hal->antenna_rx; 782 783 return 0; 784 } 785 786 static void rtw89_ops_sw_scan_start(struct ieee80211_hw *hw, 787 struct ieee80211_vif *vif, 788 const u8 *mac_addr) 789 { 790 struct rtw89_dev *rtwdev = hw->priv; 791 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 792 793 mutex_lock(&rtwdev->mutex); 794 rtw89_core_scan_start(rtwdev, rtwvif, mac_addr, false); 795 mutex_unlock(&rtwdev->mutex); 796 } 797 798 static void rtw89_ops_sw_scan_complete(struct ieee80211_hw *hw, 799 struct ieee80211_vif *vif) 800 { 801 struct rtw89_dev *rtwdev = hw->priv; 802 803 mutex_lock(&rtwdev->mutex); 804 rtw89_core_scan_complete(rtwdev, vif, false); 805 mutex_unlock(&rtwdev->mutex); 806 } 807 808 static void rtw89_ops_reconfig_complete(struct ieee80211_hw *hw, 809 enum ieee80211_reconfig_type reconfig_type) 810 { 811 struct rtw89_dev *rtwdev = hw->priv; 812 813 if (reconfig_type == IEEE80211_RECONFIG_TYPE_RESTART) 814 rtw89_ser_recfg_done(rtwdev); 815 } 816 817 static int rtw89_ops_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 818 struct ieee80211_scan_request *req) 819 { 820 struct rtw89_dev *rtwdev = hw->priv; 821 struct rtw89_vif *rtwvif = vif_to_rtwvif_safe(vif); 822 int ret = 0; 823 824 if (!RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD, &rtwdev->fw)) 825 return 1; 826 827 if (rtwdev->scanning || rtwvif->offchan) 828 return -EBUSY; 829 830 mutex_lock(&rtwdev->mutex); 831 rtw89_hw_scan_start(rtwdev, vif, req); 832 ret = rtw89_hw_scan_offload(rtwdev, vif, true); 833 if (ret) { 834 rtw89_hw_scan_abort(rtwdev, vif); 835 rtw89_err(rtwdev, "HW scan failed with status: %d\n", ret); 836 } 837 mutex_unlock(&rtwdev->mutex); 838 839 return ret; 840 } 841 842 static void rtw89_ops_cancel_hw_scan(struct ieee80211_hw *hw, 843 struct ieee80211_vif *vif) 844 { 845 struct rtw89_dev *rtwdev = hw->priv; 846 847 if (!RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD, &rtwdev->fw)) 848 return; 849 850 if (!rtwdev->scanning) 851 return; 852 853 mutex_lock(&rtwdev->mutex); 854 rtw89_hw_scan_abort(rtwdev, vif); 855 mutex_unlock(&rtwdev->mutex); 856 } 857 858 static void rtw89_ops_sta_rc_update(struct ieee80211_hw *hw, 859 struct ieee80211_vif *vif, 860 struct ieee80211_sta *sta, u32 changed) 861 { 862 struct rtw89_dev *rtwdev = hw->priv; 863 864 rtw89_phy_ra_updata_sta(rtwdev, sta, changed); 865 } 866 867 static int rtw89_ops_add_chanctx(struct ieee80211_hw *hw, 868 struct ieee80211_chanctx_conf *ctx) 869 { 870 struct rtw89_dev *rtwdev = hw->priv; 871 int ret; 872 873 mutex_lock(&rtwdev->mutex); 874 ret = rtw89_chanctx_ops_add(rtwdev, ctx); 875 mutex_unlock(&rtwdev->mutex); 876 877 return ret; 878 } 879 880 static void rtw89_ops_remove_chanctx(struct ieee80211_hw *hw, 881 struct ieee80211_chanctx_conf *ctx) 882 { 883 struct rtw89_dev *rtwdev = hw->priv; 884 885 mutex_lock(&rtwdev->mutex); 886 rtw89_chanctx_ops_remove(rtwdev, ctx); 887 mutex_unlock(&rtwdev->mutex); 888 } 889 890 static void rtw89_ops_change_chanctx(struct ieee80211_hw *hw, 891 struct ieee80211_chanctx_conf *ctx, 892 u32 changed) 893 { 894 struct rtw89_dev *rtwdev = hw->priv; 895 896 mutex_lock(&rtwdev->mutex); 897 rtw89_chanctx_ops_change(rtwdev, ctx, changed); 898 mutex_unlock(&rtwdev->mutex); 899 } 900 901 static int rtw89_ops_assign_vif_chanctx(struct ieee80211_hw *hw, 902 struct ieee80211_vif *vif, 903 struct ieee80211_bss_conf *link_conf, 904 struct ieee80211_chanctx_conf *ctx) 905 { 906 struct rtw89_dev *rtwdev = hw->priv; 907 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 908 int ret; 909 910 mutex_lock(&rtwdev->mutex); 911 ret = rtw89_chanctx_ops_assign_vif(rtwdev, rtwvif, ctx); 912 mutex_unlock(&rtwdev->mutex); 913 914 return ret; 915 } 916 917 static void rtw89_ops_unassign_vif_chanctx(struct ieee80211_hw *hw, 918 struct ieee80211_vif *vif, 919 struct ieee80211_bss_conf *link_conf, 920 struct ieee80211_chanctx_conf *ctx) 921 { 922 struct rtw89_dev *rtwdev = hw->priv; 923 struct rtw89_vif *rtwvif = (struct rtw89_vif *)vif->drv_priv; 924 925 mutex_lock(&rtwdev->mutex); 926 rtw89_chanctx_ops_unassign_vif(rtwdev, rtwvif, ctx); 927 mutex_unlock(&rtwdev->mutex); 928 } 929 930 static int rtw89_ops_remain_on_channel(struct ieee80211_hw *hw, 931 struct ieee80211_vif *vif, 932 struct ieee80211_channel *chan, 933 int duration, 934 enum ieee80211_roc_type type) 935 { 936 struct rtw89_dev *rtwdev = hw->priv; 937 struct rtw89_vif *rtwvif = vif_to_rtwvif_safe(vif); 938 struct rtw89_roc *roc = &rtwvif->roc; 939 940 if (!vif) 941 return -EINVAL; 942 943 mutex_lock(&rtwdev->mutex); 944 945 if (roc->state != RTW89_ROC_IDLE) { 946 mutex_unlock(&rtwdev->mutex); 947 return -EBUSY; 948 } 949 950 if (rtwdev->scanning) 951 rtw89_hw_scan_abort(rtwdev, vif); 952 953 if (type == IEEE80211_ROC_TYPE_MGMT_TX) 954 roc->state = RTW89_ROC_MGMT; 955 else 956 roc->state = RTW89_ROC_NORMAL; 957 958 roc->duration = duration; 959 roc->chan = *chan; 960 roc->type = type; 961 962 rtw89_roc_start(rtwdev, rtwvif); 963 964 mutex_unlock(&rtwdev->mutex); 965 966 return 0; 967 } 968 969 static int rtw89_ops_cancel_remain_on_channel(struct ieee80211_hw *hw, 970 struct ieee80211_vif *vif) 971 { 972 struct rtw89_dev *rtwdev = hw->priv; 973 struct rtw89_vif *rtwvif = vif_to_rtwvif_safe(vif); 974 975 if (!rtwvif) 976 return -EINVAL; 977 978 cancel_delayed_work_sync(&rtwvif->roc.roc_work); 979 980 mutex_lock(&rtwdev->mutex); 981 rtw89_roc_end(rtwdev, rtwvif); 982 mutex_unlock(&rtwdev->mutex); 983 984 return 0; 985 } 986 987 static void rtw89_set_tid_config_iter(void *data, struct ieee80211_sta *sta) 988 { 989 struct cfg80211_tid_config *tid_config = data; 990 struct rtw89_sta *rtwsta = (struct rtw89_sta *)sta->drv_priv; 991 struct rtw89_dev *rtwdev = rtwsta->rtwvif->rtwdev; 992 993 rtw89_core_set_tid_config(rtwdev, sta, tid_config); 994 } 995 996 static int rtw89_ops_set_tid_config(struct ieee80211_hw *hw, 997 struct ieee80211_vif *vif, 998 struct ieee80211_sta *sta, 999 struct cfg80211_tid_config *tid_config) 1000 { 1001 struct rtw89_dev *rtwdev = hw->priv; 1002 1003 mutex_lock(&rtwdev->mutex); 1004 if (sta) 1005 rtw89_core_set_tid_config(rtwdev, sta, tid_config); 1006 else 1007 ieee80211_iterate_stations_atomic(rtwdev->hw, 1008 rtw89_set_tid_config_iter, 1009 tid_config); 1010 mutex_unlock(&rtwdev->mutex); 1011 1012 return 0; 1013 } 1014 1015 #ifdef CONFIG_PM 1016 static int rtw89_ops_suspend(struct ieee80211_hw *hw, 1017 struct cfg80211_wowlan *wowlan) 1018 { 1019 struct rtw89_dev *rtwdev = hw->priv; 1020 int ret; 1021 1022 set_bit(RTW89_FLAG_FORBIDDEN_TRACK_WROK, rtwdev->flags); 1023 cancel_delayed_work_sync(&rtwdev->track_work); 1024 1025 mutex_lock(&rtwdev->mutex); 1026 ret = rtw89_wow_suspend(rtwdev, wowlan); 1027 mutex_unlock(&rtwdev->mutex); 1028 1029 if (ret) { 1030 rtw89_warn(rtwdev, "failed to suspend for wow %d\n", ret); 1031 clear_bit(RTW89_FLAG_FORBIDDEN_TRACK_WROK, rtwdev->flags); 1032 return 1; 1033 } 1034 1035 return 0; 1036 } 1037 1038 static int rtw89_ops_resume(struct ieee80211_hw *hw) 1039 { 1040 struct rtw89_dev *rtwdev = hw->priv; 1041 int ret; 1042 1043 mutex_lock(&rtwdev->mutex); 1044 ret = rtw89_wow_resume(rtwdev); 1045 if (ret) 1046 rtw89_warn(rtwdev, "failed to resume for wow %d\n", ret); 1047 mutex_unlock(&rtwdev->mutex); 1048 1049 clear_bit(RTW89_FLAG_FORBIDDEN_TRACK_WROK, rtwdev->flags); 1050 ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->track_work, 1051 RTW89_TRACK_WORK_PERIOD); 1052 1053 return ret ? 1 : 0; 1054 } 1055 1056 static void rtw89_ops_set_wakeup(struct ieee80211_hw *hw, bool enabled) 1057 { 1058 struct rtw89_dev *rtwdev = hw->priv; 1059 1060 device_set_wakeup_enable(rtwdev->dev, enabled); 1061 } 1062 #endif 1063 1064 const struct ieee80211_ops rtw89_ops = { 1065 .tx = rtw89_ops_tx, 1066 .wake_tx_queue = rtw89_ops_wake_tx_queue, 1067 .start = rtw89_ops_start, 1068 .stop = rtw89_ops_stop, 1069 .config = rtw89_ops_config, 1070 .add_interface = rtw89_ops_add_interface, 1071 .change_interface = rtw89_ops_change_interface, 1072 .remove_interface = rtw89_ops_remove_interface, 1073 .configure_filter = rtw89_ops_configure_filter, 1074 .bss_info_changed = rtw89_ops_bss_info_changed, 1075 .start_ap = rtw89_ops_start_ap, 1076 .stop_ap = rtw89_ops_stop_ap, 1077 .set_tim = rtw89_ops_set_tim, 1078 .conf_tx = rtw89_ops_conf_tx, 1079 .sta_state = rtw89_ops_sta_state, 1080 .set_key = rtw89_ops_set_key, 1081 .ampdu_action = rtw89_ops_ampdu_action, 1082 .set_rts_threshold = rtw89_ops_set_rts_threshold, 1083 .sta_statistics = rtw89_ops_sta_statistics, 1084 .flush = rtw89_ops_flush, 1085 .set_bitrate_mask = rtw89_ops_set_bitrate_mask, 1086 .set_antenna = rtw89_ops_set_antenna, 1087 .get_antenna = rtw89_ops_get_antenna, 1088 .sw_scan_start = rtw89_ops_sw_scan_start, 1089 .sw_scan_complete = rtw89_ops_sw_scan_complete, 1090 .reconfig_complete = rtw89_ops_reconfig_complete, 1091 .hw_scan = rtw89_ops_hw_scan, 1092 .cancel_hw_scan = rtw89_ops_cancel_hw_scan, 1093 .add_chanctx = rtw89_ops_add_chanctx, 1094 .remove_chanctx = rtw89_ops_remove_chanctx, 1095 .change_chanctx = rtw89_ops_change_chanctx, 1096 .assign_vif_chanctx = rtw89_ops_assign_vif_chanctx, 1097 .unassign_vif_chanctx = rtw89_ops_unassign_vif_chanctx, 1098 .remain_on_channel = rtw89_ops_remain_on_channel, 1099 .cancel_remain_on_channel = rtw89_ops_cancel_remain_on_channel, 1100 .set_sar_specs = rtw89_ops_set_sar_specs, 1101 .sta_rc_update = rtw89_ops_sta_rc_update, 1102 .set_tid_config = rtw89_ops_set_tid_config, 1103 #ifdef CONFIG_PM 1104 .suspend = rtw89_ops_suspend, 1105 .resume = rtw89_ops_resume, 1106 .set_wakeup = rtw89_ops_set_wakeup, 1107 #endif 1108 }; 1109 EXPORT_SYMBOL(rtw89_ops); 1110