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