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 = vif_to_rtwvif(vif); 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 = sta_to_rtwsta(sta); 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 61 lockdep_assert_wiphy(hw->wiphy); 62 63 return rtw89_core_start(rtwdev); 64 } 65 66 static void rtw89_ops_stop(struct ieee80211_hw *hw, bool suspend) 67 { 68 struct rtw89_dev *rtwdev = hw->priv; 69 70 lockdep_assert_wiphy(hw->wiphy); 71 72 rtw89_core_stop(rtwdev); 73 } 74 75 static int rtw89_ops_config(struct ieee80211_hw *hw, int radio_idx, u32 changed) 76 { 77 struct rtw89_dev *rtwdev = hw->priv; 78 79 lockdep_assert_wiphy(hw->wiphy); 80 81 /* let previous ips work finish to ensure we don't leave ips twice */ 82 wiphy_work_cancel(hw->wiphy, &rtwdev->ips_work); 83 84 rtw89_leave_ps_mode(rtwdev); 85 86 if ((changed & IEEE80211_CONF_CHANGE_IDLE) && 87 !(hw->conf.flags & IEEE80211_CONF_IDLE)) 88 rtw89_leave_ips(rtwdev); 89 90 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) { 91 rtw89_config_entity_chandef(rtwdev, RTW89_CHANCTX_0, 92 &hw->conf.chandef); 93 rtw89_set_channel(rtwdev); 94 } 95 96 if ((changed & IEEE80211_CONF_CHANGE_IDLE) && 97 (hw->conf.flags & IEEE80211_CONF_IDLE) && 98 !rtwdev->scanning) 99 rtw89_enter_ips(rtwdev); 100 101 if (changed & IEEE80211_CONF_CHANGE_MONITOR) 102 rtw89_physts_parsing_init(rtwdev); 103 104 return 0; 105 } 106 107 static int __rtw89_ops_add_iface_link(struct rtw89_dev *rtwdev, 108 struct rtw89_vif_link *rtwvif_link) 109 { 110 struct ieee80211_bss_conf *bss_conf; 111 int ret; 112 113 rtw89_leave_ps_mode(rtwdev); 114 115 rtw89_vif_type_mapping(rtwvif_link, false); 116 117 wiphy_work_init(&rtwvif_link->update_beacon_work, rtw89_core_update_beacon_work); 118 wiphy_delayed_work_init(&rtwvif_link->csa_beacon_work, rtw89_core_csa_beacon_work); 119 wiphy_delayed_work_init(&rtwvif_link->mcc_gc_detect_beacon_work, 120 rtw89_mcc_gc_detect_beacon_work); 121 122 INIT_LIST_HEAD(&rtwvif_link->general_pkt_list); 123 124 rtw89_p2p_noa_once_init(rtwvif_link); 125 126 rtwvif_link->hit_rule = 0; 127 rtwvif_link->bcn_hit_cond = 0; 128 rtwvif_link->chanctx_assigned = false; 129 rtwvif_link->chanctx_idx = RTW89_CHANCTX_0; 130 rtwvif_link->reg_6ghz_power = RTW89_REG_6GHZ_POWER_DFLT; 131 rtwvif_link->rand_tsf_done = false; 132 rtwvif_link->detect_bcn_count = 0; 133 rtwvif_link->last_sync_bcn_tsf = 0; 134 135 rcu_read_lock(); 136 137 bss_conf = rtw89_vif_rcu_dereference_link(rtwvif_link, true); 138 ether_addr_copy(rtwvif_link->mac_addr, bss_conf->addr); 139 140 rcu_read_unlock(); 141 142 ret = rtw89_mac_add_vif(rtwdev, rtwvif_link); 143 if (ret) 144 return ret; 145 146 rtw89_btc_ntfy_role_info(rtwdev, rtwvif_link, NULL, BTC_ROLE_START); 147 return 0; 148 } 149 150 static void __rtw89_ops_remove_iface_link(struct rtw89_dev *rtwdev, 151 struct rtw89_vif_link *rtwvif_link) 152 { 153 lockdep_assert_wiphy(rtwdev->hw->wiphy); 154 155 wiphy_work_cancel(rtwdev->hw->wiphy, &rtwvif_link->update_beacon_work); 156 wiphy_delayed_work_cancel(rtwdev->hw->wiphy, &rtwvif_link->csa_beacon_work); 157 wiphy_delayed_work_cancel(rtwdev->hw->wiphy, 158 &rtwvif_link->mcc_gc_detect_beacon_work); 159 160 rtw89_p2p_noa_once_deinit(rtwvif_link); 161 162 rtw89_leave_ps_mode(rtwdev); 163 164 rtw89_btc_ntfy_role_info(rtwdev, rtwvif_link, NULL, BTC_ROLE_STOP); 165 166 rtw89_mac_remove_vif(rtwdev, rtwvif_link); 167 } 168 169 static int rtw89_ops_add_interface(struct ieee80211_hw *hw, 170 struct ieee80211_vif *vif) 171 { 172 struct rtw89_dev *rtwdev = hw->priv; 173 struct rtw89_vif *rtwvif = vif_to_rtwvif(vif); 174 struct rtw89_vif_link *rtwvif_link; 175 u8 mac_id, port; 176 int ret = 0; 177 178 lockdep_assert_wiphy(hw->wiphy); 179 180 rtw89_debug(rtwdev, RTW89_DBG_STATE, "add vif %pM type %d, p2p %d\n", 181 vif->addr, vif->type, vif->p2p); 182 183 rtw89_leave_ips_by_hwflags(rtwdev); 184 185 if (RTW89_CHK_FW_FEATURE(BEACON_FILTER, &rtwdev->fw)) 186 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER | 187 IEEE80211_VIF_SUPPORTS_CQM_RSSI; 188 189 mac_id = rtw89_acquire_mac_id(rtwdev); 190 if (mac_id == RTW89_MAX_MAC_ID_NUM) 191 return -ENOSPC; 192 193 port = rtw89_core_acquire_bit_map(rtwdev->hw_port, RTW89_PORT_NUM); 194 if (port == RTW89_PORT_NUM) { 195 ret = -ENOSPC; 196 goto release_macid; 197 } 198 199 rtw89_init_vif(rtwdev, rtwvif, mac_id, port); 200 201 rtw89_core_txq_init(rtwdev, vif->txq); 202 203 if (!rtw89_rtwvif_in_list(rtwdev, rtwvif)) { 204 list_add_tail(&rtwvif->list, &rtwdev->rtwvifs_list); 205 INIT_LIST_HEAD(&rtwvif->mgnt_entry); 206 INIT_LIST_HEAD(&rtwvif->dlink_pool); 207 } 208 209 ether_addr_copy(rtwvif->mac_addr, vif->addr); 210 211 rtwvif->offchan = false; 212 rtwvif->roc.state = RTW89_ROC_IDLE; 213 wiphy_delayed_work_init(&rtwvif->roc.roc_work, rtw89_roc_work); 214 215 rtw89_traffic_stats_init(rtwdev, &rtwvif->stats); 216 217 rtwvif_link = rtw89_vif_set_link(rtwvif, RTW89_VIF_IDLE_LINK_ID); 218 if (!rtwvif_link) { 219 ret = -EINVAL; 220 goto release_port; 221 } 222 223 ret = __rtw89_ops_add_iface_link(rtwdev, rtwvif_link); 224 if (ret) 225 goto unset_link; 226 227 rtwdev->pure_monitor_mode_vif = vif->type == NL80211_IFTYPE_MONITOR ? 228 rtwvif : NULL; 229 rtw89_recalc_lps(rtwdev); 230 return 0; 231 232 unset_link: 233 rtw89_vif_unset_link(rtwvif, RTW89_VIF_IDLE_LINK_ID); 234 release_port: 235 list_del_init(&rtwvif->list); 236 rtw89_core_release_bit_map(rtwdev->hw_port, port); 237 release_macid: 238 rtw89_release_mac_id(rtwdev, mac_id); 239 240 return ret; 241 } 242 243 static void rtw89_ops_remove_interface(struct ieee80211_hw *hw, 244 struct ieee80211_vif *vif) 245 { 246 struct rtw89_dev *rtwdev = hw->priv; 247 struct rtw89_vif *rtwvif = vif_to_rtwvif(vif); 248 u8 macid = rtw89_vif_get_main_macid(rtwvif); 249 u8 port = rtw89_vif_get_main_port(rtwvif); 250 struct rtw89_vif_link *rtwvif_link; 251 252 lockdep_assert_wiphy(hw->wiphy); 253 254 rtw89_debug(rtwdev, RTW89_DBG_STATE, "remove vif %pM type %d p2p %d\n", 255 vif->addr, vif->type, vif->p2p); 256 257 wiphy_delayed_work_cancel(hw->wiphy, &rtwvif->roc.roc_work); 258 259 rtwvif_link = rtwvif->links[RTW89_VIF_IDLE_LINK_ID]; 260 if (unlikely(!rtwvif_link)) { 261 rtw89_err(rtwdev, 262 "%s: rtwvif link (link_id %u) is not active\n", 263 __func__, RTW89_VIF_IDLE_LINK_ID); 264 goto bottom; 265 } 266 267 __rtw89_ops_remove_iface_link(rtwdev, rtwvif_link); 268 269 rtw89_vif_unset_link(rtwvif, RTW89_VIF_IDLE_LINK_ID); 270 271 bottom: 272 list_del_init(&rtwvif->list); 273 rtw89_core_release_bit_map(rtwdev->hw_port, port); 274 rtw89_release_mac_id(rtwdev, macid); 275 276 rtwdev->pure_monitor_mode_vif = NULL; 277 278 rtw89_recalc_lps(rtwdev); 279 rtw89_enter_ips_by_hwflags(rtwdev); 280 } 281 282 static int rtw89_ops_change_interface(struct ieee80211_hw *hw, 283 struct ieee80211_vif *vif, 284 enum nl80211_iftype type, bool p2p) 285 { 286 struct rtw89_dev *rtwdev = hw->priv; 287 int ret; 288 289 set_bit(RTW89_FLAG_CHANGING_INTERFACE, rtwdev->flags); 290 291 rtw89_debug(rtwdev, RTW89_DBG_STATE, "change vif %pM (%d)->(%d), p2p (%d)->(%d)\n", 292 vif->addr, vif->type, type, vif->p2p, p2p); 293 294 rtw89_ops_remove_interface(hw, vif); 295 296 vif->type = type; 297 vif->p2p = p2p; 298 299 ret = rtw89_ops_add_interface(hw, vif); 300 if (ret) 301 rtw89_warn(rtwdev, "failed to change interface %d\n", ret); 302 303 clear_bit(RTW89_FLAG_CHANGING_INTERFACE, rtwdev->flags); 304 305 return ret; 306 } 307 308 static void rtw89_ops_configure_filter(struct ieee80211_hw *hw, 309 unsigned int changed_flags, 310 unsigned int *new_flags, 311 u64 multicast) 312 { 313 struct rtw89_dev *rtwdev = hw->priv; 314 u32 rx_fltr; 315 316 lockdep_assert_wiphy(hw->wiphy); 317 318 rtw89_leave_ps_mode(rtwdev); 319 320 *new_flags &= FIF_ALLMULTI | FIF_OTHER_BSS | FIF_FCSFAIL | 321 FIF_BCN_PRBRESP_PROMISC | FIF_PROBE_REQ; 322 323 if (changed_flags & FIF_ALLMULTI) { 324 if (*new_flags & FIF_ALLMULTI) 325 rtwdev->hal.rx_fltr &= ~B_AX_A_MC; 326 else 327 rtwdev->hal.rx_fltr |= B_AX_A_MC; 328 } 329 if (changed_flags & FIF_FCSFAIL) { 330 if (*new_flags & FIF_FCSFAIL) 331 rtwdev->hal.rx_fltr |= B_AX_A_CRC32_ERR; 332 else 333 rtwdev->hal.rx_fltr &= ~B_AX_A_CRC32_ERR; 334 } 335 if (changed_flags & FIF_OTHER_BSS) { 336 if (*new_flags & FIF_OTHER_BSS) 337 rtwdev->hal.rx_fltr &= ~B_AX_A_A1_MATCH; 338 else 339 rtwdev->hal.rx_fltr |= B_AX_A_A1_MATCH; 340 } 341 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) { 342 if (*new_flags & FIF_BCN_PRBRESP_PROMISC) { 343 rtwdev->hal.rx_fltr &= ~B_AX_A_BCN_CHK_EN; 344 rtwdev->hal.rx_fltr &= ~B_AX_A_BC; 345 rtwdev->hal.rx_fltr &= ~B_AX_A_A1_MATCH; 346 } else { 347 rtwdev->hal.rx_fltr |= B_AX_A_BCN_CHK_EN; 348 rtwdev->hal.rx_fltr |= B_AX_A_BC; 349 rtwdev->hal.rx_fltr |= B_AX_A_A1_MATCH; 350 } 351 } 352 if (changed_flags & FIF_PROBE_REQ) { 353 if (*new_flags & FIF_PROBE_REQ) { 354 rtwdev->hal.rx_fltr &= ~B_AX_A_BC_CAM_MATCH; 355 rtwdev->hal.rx_fltr &= ~B_AX_A_UC_CAM_MATCH; 356 } else { 357 rtwdev->hal.rx_fltr |= B_AX_A_BC_CAM_MATCH; 358 rtwdev->hal.rx_fltr |= B_AX_A_UC_CAM_MATCH; 359 } 360 } 361 362 if (rtwdev->hw->conf.flags & IEEE80211_CONF_MONITOR) 363 rtwdev->hal.rx_fltr |= B_AX_SNIFFER_MODE; 364 else 365 rtwdev->hal.rx_fltr &= ~B_AX_SNIFFER_MODE; 366 367 rx_fltr = rtwdev->hal.rx_fltr; 368 369 /* mac80211 doesn't configure filter when HW scan, driver need to 370 * set by itself. However, during P2P scan might have configure 371 * filter to overwrite filter that HW scan needed, so we need to 372 * check scan and append related filter 373 */ 374 if (rtwdev->scanning) { 375 rx_fltr &= ~B_AX_A_BCN_CHK_EN; 376 rx_fltr &= ~B_AX_A_BC; 377 rx_fltr &= ~B_AX_A_A1_MATCH; 378 } 379 380 rtw89_mac_set_rx_fltr(rtwdev, RTW89_MAC_0, rx_fltr); 381 if (!rtwdev->dbcc_en) 382 return; 383 rtw89_mac_set_rx_fltr(rtwdev, RTW89_MAC_1, rx_fltr); 384 } 385 386 static const u8 ac_to_fw_idx[IEEE80211_NUM_ACS] = { 387 [IEEE80211_AC_VO] = 3, 388 [IEEE80211_AC_VI] = 2, 389 [IEEE80211_AC_BE] = 0, 390 [IEEE80211_AC_BK] = 1, 391 }; 392 393 static u8 rtw89_aifsn_to_aifs(struct rtw89_dev *rtwdev, 394 struct rtw89_vif_link *rtwvif_link, u8 aifsn) 395 { 396 const struct rtw89_chan *chan = rtw89_chan_get(rtwdev, 397 rtwvif_link->chanctx_idx); 398 struct ieee80211_bss_conf *bss_conf; 399 u8 slot_time; 400 u8 sifs; 401 402 rcu_read_lock(); 403 404 bss_conf = rtw89_vif_rcu_dereference_link(rtwvif_link, true); 405 slot_time = bss_conf->use_short_slot ? 9 : 20; 406 407 rcu_read_unlock(); 408 409 sifs = chan->band_type == RTW89_BAND_2G ? 10 : 16; 410 411 return aifsn * slot_time + sifs; 412 } 413 414 static void ____rtw89_conf_tx_edca(struct rtw89_dev *rtwdev, 415 struct rtw89_vif_link *rtwvif_link, u16 ac) 416 { 417 struct ieee80211_tx_queue_params *params = &rtwvif_link->tx_params[ac]; 418 u32 val; 419 u8 ecw_max, ecw_min; 420 u8 aifs; 421 422 /* 2^ecw - 1 = cw; ecw = log2(cw + 1) */ 423 ecw_max = ilog2(params->cw_max + 1); 424 ecw_min = ilog2(params->cw_min + 1); 425 aifs = rtw89_aifsn_to_aifs(rtwdev, rtwvif_link, params->aifs); 426 val = FIELD_PREP(FW_EDCA_PARAM_TXOPLMT_MSK, params->txop) | 427 FIELD_PREP(FW_EDCA_PARAM_CWMAX_MSK, ecw_max) | 428 FIELD_PREP(FW_EDCA_PARAM_CWMIN_MSK, ecw_min) | 429 FIELD_PREP(FW_EDCA_PARAM_AIFS_MSK, aifs); 430 rtw89_fw_h2c_set_edca(rtwdev, rtwvif_link, ac_to_fw_idx[ac], val); 431 } 432 433 #define R_MUEDCA_ACS_PARAM(acs) {R_AX_MUEDCA_ ## acs ## _PARAM_0, \ 434 R_BE_MUEDCA_ ## acs ## _PARAM_0} 435 436 static const u32 ac_to_mu_edca_param[IEEE80211_NUM_ACS][RTW89_CHIP_GEN_NUM] = { 437 [IEEE80211_AC_VO] = R_MUEDCA_ACS_PARAM(VO), 438 [IEEE80211_AC_VI] = R_MUEDCA_ACS_PARAM(VI), 439 [IEEE80211_AC_BE] = R_MUEDCA_ACS_PARAM(BE), 440 [IEEE80211_AC_BK] = R_MUEDCA_ACS_PARAM(BK), 441 }; 442 443 static void ____rtw89_conf_tx_mu_edca(struct rtw89_dev *rtwdev, 444 struct rtw89_vif_link *rtwvif_link, u16 ac) 445 { 446 struct ieee80211_tx_queue_params *params = &rtwvif_link->tx_params[ac]; 447 struct ieee80211_he_mu_edca_param_ac_rec *mu_edca; 448 int gen = rtwdev->chip->chip_gen; 449 u8 aifs, aifsn; 450 u16 timer_32us; 451 u32 reg; 452 u32 val; 453 454 if (!params->mu_edca) 455 return; 456 457 mu_edca = ¶ms->mu_edca_param_rec; 458 aifsn = FIELD_GET(GENMASK(3, 0), mu_edca->aifsn); 459 aifs = aifsn ? rtw89_aifsn_to_aifs(rtwdev, rtwvif_link, aifsn) : 0; 460 timer_32us = mu_edca->mu_edca_timer << 8; 461 462 val = FIELD_PREP(B_AX_MUEDCA_BE_PARAM_0_TIMER_MASK, timer_32us) | 463 FIELD_PREP(B_AX_MUEDCA_BE_PARAM_0_CW_MASK, mu_edca->ecw_min_max) | 464 FIELD_PREP(B_AX_MUEDCA_BE_PARAM_0_AIFS_MASK, aifs); 465 reg = rtw89_mac_reg_by_idx(rtwdev, ac_to_mu_edca_param[ac][gen], 466 rtwvif_link->mac_idx); 467 rtw89_write32(rtwdev, reg, val); 468 469 rtw89_mac_set_hw_muedca_ctrl(rtwdev, rtwvif_link, true); 470 } 471 472 static void __rtw89_conf_tx(struct rtw89_dev *rtwdev, 473 struct rtw89_vif_link *rtwvif_link, u16 ac) 474 { 475 ____rtw89_conf_tx_edca(rtwdev, rtwvif_link, ac); 476 ____rtw89_conf_tx_mu_edca(rtwdev, rtwvif_link, ac); 477 } 478 479 static void rtw89_conf_tx(struct rtw89_dev *rtwdev, 480 struct rtw89_vif_link *rtwvif_link) 481 { 482 u16 ac; 483 484 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 485 __rtw89_conf_tx(rtwdev, rtwvif_link, ac); 486 } 487 488 static int __rtw89_ops_sta_add(struct rtw89_dev *rtwdev, 489 struct ieee80211_vif *vif, 490 struct ieee80211_sta *sta) 491 { 492 struct rtw89_vif *rtwvif = vif_to_rtwvif(vif); 493 struct rtw89_sta *rtwsta = sta_to_rtwsta(sta); 494 struct rtw89_vif_link *rtwvif_link; 495 struct rtw89_sta_link *rtwsta_link; 496 bool acquire_macid = false; 497 u8 macid; 498 int ret; 499 int i; 500 501 if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) { 502 rtwvif->mlo_mode = RTW89_MLO_MODE_MLSR; 503 504 /* for station mode, assign the mac_id from itself */ 505 macid = rtw89_vif_get_main_macid(rtwvif); 506 } else { 507 macid = rtw89_acquire_mac_id(rtwdev); 508 if (macid == RTW89_MAX_MAC_ID_NUM) 509 return -ENOSPC; 510 511 acquire_macid = true; 512 } 513 514 rtw89_init_sta(rtwdev, rtwvif, rtwsta, macid); 515 516 for (i = 0; i < ARRAY_SIZE(sta->txq); i++) 517 rtw89_core_txq_init(rtwdev, sta->txq[i]); 518 519 INIT_LIST_HEAD(&rtwsta->dlink_pool); 520 521 skb_queue_head_init(&rtwsta->roc_queue); 522 bitmap_zero(rtwsta->pairwise_sec_cam_map, RTW89_MAX_SEC_CAM_NUM); 523 524 rtwsta_link = rtw89_sta_set_link(rtwsta, sta->deflink.link_id); 525 if (!rtwsta_link) { 526 ret = -EINVAL; 527 goto err; 528 } 529 530 rtwvif_link = rtwsta_link->rtwvif_link; 531 532 ret = rtw89_core_sta_link_add(rtwdev, rtwvif_link, rtwsta_link); 533 if (ret) 534 goto unset_link; 535 536 if (vif->type == NL80211_IFTYPE_AP || sta->tdls) 537 rtw89_queue_chanctx_change(rtwdev, RTW89_CHANCTX_REMOTE_STA_CHANGE); 538 539 rtw89_fw_h2c_init_trx_protect(rtwdev); 540 541 return 0; 542 543 unset_link: 544 rtw89_sta_unset_link(rtwsta, sta->deflink.link_id); 545 err: 546 if (acquire_macid) 547 rtw89_release_mac_id(rtwdev, macid); 548 549 return ret; 550 } 551 552 static int __rtw89_ops_sta_assoc(struct rtw89_dev *rtwdev, 553 struct ieee80211_vif *vif, 554 struct ieee80211_sta *sta, 555 bool station_mode) 556 { 557 struct rtw89_vif *rtwvif = vif_to_rtwvif(vif); 558 struct rtw89_sta *rtwsta = sta_to_rtwsta(sta); 559 struct rtw89_vif_link *rtwvif_link; 560 struct rtw89_sta_link *rtwsta_link; 561 unsigned int link_id; 562 int ret; 563 564 rtw89_sta_for_each_link(rtwsta, rtwsta_link, link_id) { 565 rtwvif_link = rtwsta_link->rtwvif_link; 566 567 if (station_mode) 568 rtw89_vif_type_mapping(rtwvif_link, true); 569 570 ret = rtw89_core_sta_link_assoc(rtwdev, rtwvif_link, rtwsta_link); 571 if (ret) 572 return ret; 573 } 574 575 rtwdev->total_sta_assoc++; 576 if (sta->tdls) 577 rtwvif->tdls_peer++; 578 579 return 0; 580 } 581 582 static int __rtw89_ops_sta_disassoc(struct rtw89_dev *rtwdev, 583 struct ieee80211_vif *vif, 584 struct ieee80211_sta *sta) 585 { 586 struct rtw89_vif *rtwvif = vif_to_rtwvif(vif); 587 struct rtw89_sta *rtwsta = sta_to_rtwsta(sta); 588 struct rtw89_vif_link *rtwvif_link; 589 struct rtw89_sta_link *rtwsta_link; 590 unsigned int link_id; 591 int ret; 592 593 rtw89_sta_for_each_link(rtwsta, rtwsta_link, link_id) { 594 rtwvif_link = rtwsta_link->rtwvif_link; 595 ret = rtw89_core_sta_link_disassoc(rtwdev, rtwvif_link, rtwsta_link); 596 if (ret) 597 return ret; 598 } 599 600 rtwsta->disassoc = true; 601 602 rtwdev->total_sta_assoc--; 603 if (sta->tdls) 604 rtwvif->tdls_peer--; 605 606 return 0; 607 } 608 609 static int __rtw89_ops_sta_disconnect(struct rtw89_dev *rtwdev, 610 struct ieee80211_vif *vif, 611 struct ieee80211_sta *sta) 612 { 613 struct rtw89_sta *rtwsta = sta_to_rtwsta(sta); 614 struct rtw89_vif_link *rtwvif_link; 615 struct rtw89_sta_link *rtwsta_link; 616 unsigned int link_id; 617 int ret; 618 619 rtw89_core_free_sta_pending_ba(rtwdev, sta); 620 rtw89_core_free_sta_pending_forbid_ba(rtwdev, sta); 621 rtw89_core_free_sta_pending_roc_tx(rtwdev, sta); 622 623 rtw89_sta_for_each_link(rtwsta, rtwsta_link, link_id) { 624 rtwvif_link = rtwsta_link->rtwvif_link; 625 ret = rtw89_core_sta_link_disconnect(rtwdev, rtwvif_link, rtwsta_link); 626 if (ret) 627 return ret; 628 } 629 630 return 0; 631 } 632 633 static int __rtw89_ops_sta_remove(struct rtw89_dev *rtwdev, 634 struct ieee80211_vif *vif, 635 struct ieee80211_sta *sta) 636 { 637 struct rtw89_sta *rtwsta = sta_to_rtwsta(sta); 638 u8 macid = rtw89_sta_get_main_macid(rtwsta); 639 struct rtw89_vif_link *rtwvif_link; 640 struct rtw89_sta_link *rtwsta_link; 641 unsigned int link_id; 642 int ret; 643 644 rtw89_sta_for_each_link(rtwsta, rtwsta_link, link_id) { 645 rtwvif_link = rtwsta_link->rtwvif_link; 646 ret = rtw89_core_sta_link_remove(rtwdev, rtwvif_link, rtwsta_link); 647 if (ret) 648 return ret; 649 650 rtw89_sta_unset_link(rtwsta, link_id); 651 } 652 653 if (vif->type == NL80211_IFTYPE_AP || sta->tdls) { 654 rtw89_release_mac_id(rtwdev, macid); 655 rtw89_queue_chanctx_change(rtwdev, RTW89_CHANCTX_REMOTE_STA_CHANGE); 656 } 657 658 return 0; 659 } 660 661 static void rtw89_station_mode_sta_assoc(struct rtw89_dev *rtwdev, 662 struct ieee80211_vif *vif) 663 { 664 struct ieee80211_sta *sta; 665 666 if (vif->type != NL80211_IFTYPE_STATION) 667 return; 668 669 sta = ieee80211_find_sta(vif, vif->cfg.ap_addr); 670 if (!sta) { 671 rtw89_err(rtwdev, "can't find sta to set sta_assoc state\n"); 672 return; 673 } 674 675 __rtw89_ops_sta_assoc(rtwdev, vif, sta, true); 676 } 677 678 static void __rtw89_ops_bss_link_assoc(struct rtw89_dev *rtwdev, 679 struct rtw89_vif_link *rtwvif_link) 680 { 681 rtw89_phy_set_bss_color(rtwdev, rtwvif_link); 682 rtw89_chip_cfg_txpwr_ul_tb_offset(rtwdev, rtwvif_link); 683 rtw89_mac_port_update(rtwdev, rtwvif_link); 684 rtw89_mac_set_he_obss_narrow_bw_ru(rtwdev, rtwvif_link); 685 rtw89_mac_set_he_tb(rtwdev, rtwvif_link); 686 } 687 688 static void __rtw89_ops_bss_assoc(struct rtw89_dev *rtwdev, 689 struct ieee80211_vif *vif) 690 { 691 struct rtw89_vif *rtwvif = vif_to_rtwvif(vif); 692 struct rtw89_vif_link *rtwvif_link; 693 unsigned int link_id; 694 695 rtw89_vif_for_each_link(rtwvif, rtwvif_link, link_id) 696 __rtw89_ops_bss_link_assoc(rtwdev, rtwvif_link); 697 } 698 699 static void rtw89_ops_vif_cfg_changed(struct ieee80211_hw *hw, 700 struct ieee80211_vif *vif, u64 changed) 701 { 702 struct rtw89_dev *rtwdev = hw->priv; 703 struct rtw89_vif *rtwvif = vif_to_rtwvif(vif); 704 705 lockdep_assert_wiphy(hw->wiphy); 706 707 rtw89_leave_ps_mode(rtwdev); 708 709 if (changed & BSS_CHANGED_ASSOC) { 710 if (vif->cfg.assoc) { 711 rtw89_station_mode_sta_assoc(rtwdev, vif); 712 __rtw89_ops_bss_assoc(rtwdev, vif); 713 714 rtw89_queue_chanctx_work(rtwdev); 715 } else { 716 /* Abort ongoing scan if cancel_scan isn't issued 717 * when disconnected by peer 718 */ 719 if (rtwdev->scanning) 720 rtw89_hw_scan_abort(rtwdev, rtwdev->scan_info.scanning_vif); 721 } 722 } 723 724 if (changed & BSS_CHANGED_PS) 725 rtw89_recalc_lps(rtwdev); 726 727 if (changed & BSS_CHANGED_ARP_FILTER) 728 rtwvif->ip_addr = vif->cfg.arp_addr_list[0]; 729 730 if (changed & BSS_CHANGED_MLD_VALID_LINKS) { 731 struct rtw89_vif_link *cur = rtw89_get_designated_link(rtwvif); 732 u16 mac_id; 733 734 if (RTW89_CHK_FW_FEATURE_GROUP(WITH_RFK_PRE_NOTIFY, &rtwdev->fw)) 735 rtw89_chip_rfk_channel(rtwdev, cur); 736 737 if (hweight16(vif->active_links) == 1) { 738 mac_id = cur->mac_id; 739 rtwvif->mlo_mode = RTW89_MLO_MODE_MLSR; 740 } else { 741 /* Specify for all MAC ID */ 742 mac_id = 0xffff; 743 rtwvif->mlo_mode = RTW89_MLO_MODE_EMLSR; 744 } 745 746 rtw89_fw_h2c_tx_history(rtwdev, mac_id); 747 } 748 } 749 750 static void rtw89_ops_link_info_changed(struct ieee80211_hw *hw, 751 struct ieee80211_vif *vif, 752 struct ieee80211_bss_conf *conf, 753 u64 changed) 754 { 755 struct rtw89_dev *rtwdev = hw->priv; 756 struct rtw89_vif *rtwvif = vif_to_rtwvif(vif); 757 struct rtw89_vif_link *rtwvif_link; 758 759 lockdep_assert_wiphy(hw->wiphy); 760 761 rtw89_leave_ps_mode(rtwdev); 762 763 rtwvif_link = rtwvif->links[conf->link_id]; 764 if (unlikely(!rtwvif_link)) { 765 rtw89_err(rtwdev, 766 "%s: rtwvif link (link_id %u) is not active\n", 767 __func__, conf->link_id); 768 return; 769 } 770 771 if (changed & BSS_CHANGED_BSSID) { 772 ether_addr_copy(rtwvif_link->bssid, conf->bssid); 773 rtw89_cam_bssid_changed(rtwdev, rtwvif_link); 774 rtw89_fw_h2c_cam(rtwdev, rtwvif_link, NULL, NULL, RTW89_ROLE_INFO_CHANGE); 775 WRITE_ONCE(rtwvif_link->sync_bcn_tsf, 0); 776 } 777 778 if (changed & BSS_CHANGED_BEACON) 779 rtw89_chip_h2c_update_beacon(rtwdev, rtwvif_link); 780 781 if (changed & BSS_CHANGED_ERP_SLOT) 782 rtw89_conf_tx(rtwdev, rtwvif_link); 783 784 if (changed & BSS_CHANGED_HE_BSS_COLOR) 785 rtw89_phy_set_bss_color(rtwdev, rtwvif_link); 786 787 if (changed & BSS_CHANGED_MU_GROUPS) 788 rtw89_mac_bf_set_gid_table(rtwdev, vif, conf); 789 790 if (changed & BSS_CHANGED_P2P_PS) 791 rtw89_core_update_p2p_ps(rtwdev, rtwvif_link, conf); 792 793 if (changed & BSS_CHANGED_CQM) 794 rtw89_fw_h2c_set_bcn_fltr_cfg(rtwdev, rtwvif_link, true); 795 796 if (changed & BSS_CHANGED_TPE) 797 rtw89_reg_6ghz_recalc(rtwdev, rtwvif_link, true); 798 } 799 800 static int rtw89_ops_start_ap(struct ieee80211_hw *hw, 801 struct ieee80211_vif *vif, 802 struct ieee80211_bss_conf *link_conf) 803 { 804 struct rtw89_dev *rtwdev = hw->priv; 805 struct rtw89_vif *rtwvif = vif_to_rtwvif(vif); 806 struct rtw89_vif_link *rtwvif_link; 807 const struct rtw89_chan *chan; 808 int ret = 0; 809 810 lockdep_assert_wiphy(hw->wiphy); 811 812 rtwvif_link = rtwvif->links[link_conf->link_id]; 813 if (unlikely(!rtwvif_link)) { 814 rtw89_err(rtwdev, 815 "%s: rtwvif link (link_id %u) is not active\n", 816 __func__, link_conf->link_id); 817 return -ENOLINK; 818 } 819 820 chan = rtw89_chan_get(rtwdev, rtwvif_link->chanctx_idx); 821 if (chan->band_type == RTW89_BAND_6G) 822 return -EOPNOTSUPP; 823 824 if (rtwdev->scanning) 825 rtw89_hw_scan_abort(rtwdev, rtwdev->scan_info.scanning_vif); 826 827 ether_addr_copy(rtwvif_link->bssid, link_conf->bssid); 828 rtw89_cam_bssid_changed(rtwdev, rtwvif_link); 829 ret = rtw89_mac_port_update(rtwdev, rtwvif_link); 830 if (ret) { 831 rtw89_warn(rtwdev, "failed to update mac port\n"); 832 return ret; 833 } 834 835 ret = rtw89_chip_h2c_assoc_cmac_tbl(rtwdev, rtwvif_link, NULL); 836 if (ret) { 837 rtw89_warn(rtwdev, "failed to send h2c cmac table\n"); 838 return ret; 839 } 840 841 ret = rtw89_fw_h2c_role_maintain(rtwdev, rtwvif_link, NULL, RTW89_ROLE_TYPE_CHANGE); 842 if (ret) { 843 rtw89_warn(rtwdev, "failed to send h2c role info\n"); 844 return ret; 845 } 846 847 ret = rtw89_fw_h2c_join_info(rtwdev, rtwvif_link, NULL, true); 848 if (ret) { 849 rtw89_warn(rtwdev, "failed to send h2c join info\n"); 850 return ret; 851 } 852 853 ret = rtw89_fw_h2c_cam(rtwdev, rtwvif_link, NULL, NULL, RTW89_ROLE_TYPE_CHANGE); 854 if (ret) { 855 rtw89_warn(rtwdev, "failed to send h2c cam\n"); 856 return ret; 857 } 858 859 rtw89_chip_rfk_channel(rtwdev, rtwvif_link); 860 861 if (RTW89_CHK_FW_FEATURE(NOTIFY_AP_INFO, &rtwdev->fw)) { 862 ret = rtw89_fw_h2c_ap_info_refcount(rtwdev, true); 863 if (ret) 864 return ret; 865 } 866 867 rtw89_queue_chanctx_work(rtwdev); 868 869 return 0; 870 } 871 872 static 873 void rtw89_ops_stop_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 874 struct ieee80211_bss_conf *link_conf) 875 { 876 struct rtw89_dev *rtwdev = hw->priv; 877 struct rtw89_vif *rtwvif = vif_to_rtwvif(vif); 878 struct rtw89_vif_link *rtwvif_link; 879 880 lockdep_assert_wiphy(hw->wiphy); 881 882 rtwvif_link = rtwvif->links[link_conf->link_id]; 883 if (unlikely(!rtwvif_link)) { 884 rtw89_err(rtwdev, 885 "%s: rtwvif link (link_id %u) is not active\n", 886 __func__, link_conf->link_id); 887 return; 888 } 889 890 if (RTW89_CHK_FW_FEATURE(NOTIFY_AP_INFO, &rtwdev->fw)) 891 rtw89_fw_h2c_ap_info_refcount(rtwdev, false); 892 893 rtw89_mac_stop_ap(rtwdev, rtwvif_link); 894 rtw89_chip_h2c_assoc_cmac_tbl(rtwdev, rtwvif_link, NULL); 895 rtw89_fw_h2c_join_info(rtwdev, rtwvif_link, NULL, true); 896 } 897 898 static int rtw89_ops_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta, 899 bool set) 900 { 901 struct rtw89_sta *rtwsta = sta_to_rtwsta(sta); 902 struct rtw89_vif *rtwvif = rtwsta->rtwvif; 903 struct rtw89_vif_link *rtwvif_link; 904 unsigned int link_id; 905 906 rtw89_vif_for_each_link(rtwvif, rtwvif_link, link_id) 907 wiphy_work_queue(hw->wiphy, &rtwvif_link->update_beacon_work); 908 909 return 0; 910 } 911 912 static int rtw89_ops_conf_tx(struct ieee80211_hw *hw, 913 struct ieee80211_vif *vif, 914 unsigned int link_id, u16 ac, 915 const struct ieee80211_tx_queue_params *params) 916 { 917 struct rtw89_dev *rtwdev = hw->priv; 918 struct rtw89_vif *rtwvif = vif_to_rtwvif(vif); 919 struct rtw89_vif_link *rtwvif_link; 920 921 lockdep_assert_wiphy(hw->wiphy); 922 923 rtw89_leave_ps_mode(rtwdev); 924 925 rtwvif_link = rtwvif->links[link_id]; 926 if (unlikely(!rtwvif_link)) { 927 rtw89_err(rtwdev, 928 "%s: rtwvif link (link_id %u) is not active\n", 929 __func__, link_id); 930 return -ENOLINK; 931 } 932 933 rtwvif_link->tx_params[ac] = *params; 934 __rtw89_conf_tx(rtwdev, rtwvif_link, ac); 935 936 return 0; 937 } 938 939 static int __rtw89_ops_sta_state(struct ieee80211_hw *hw, 940 struct ieee80211_vif *vif, 941 struct ieee80211_sta *sta, 942 enum ieee80211_sta_state old_state, 943 enum ieee80211_sta_state new_state) 944 { 945 struct rtw89_dev *rtwdev = hw->priv; 946 947 if (old_state == IEEE80211_STA_NOTEXIST && 948 new_state == IEEE80211_STA_NONE) 949 return __rtw89_ops_sta_add(rtwdev, vif, sta); 950 951 if (old_state == IEEE80211_STA_AUTH && 952 new_state == IEEE80211_STA_ASSOC) { 953 if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) 954 return 0; /* defer to bss_info_changed to have vif info */ 955 return __rtw89_ops_sta_assoc(rtwdev, vif, sta, false); 956 } 957 958 if (old_state == IEEE80211_STA_ASSOC && 959 new_state == IEEE80211_STA_AUTH) 960 return __rtw89_ops_sta_disassoc(rtwdev, vif, sta); 961 962 if (old_state == IEEE80211_STA_AUTH && 963 new_state == IEEE80211_STA_NONE) 964 return __rtw89_ops_sta_disconnect(rtwdev, vif, sta); 965 966 if (old_state == IEEE80211_STA_NONE && 967 new_state == IEEE80211_STA_NOTEXIST) 968 return __rtw89_ops_sta_remove(rtwdev, vif, sta); 969 970 return 0; 971 } 972 973 static int rtw89_ops_sta_state(struct ieee80211_hw *hw, 974 struct ieee80211_vif *vif, 975 struct ieee80211_sta *sta, 976 enum ieee80211_sta_state old_state, 977 enum ieee80211_sta_state new_state) 978 { 979 struct rtw89_dev *rtwdev = hw->priv; 980 981 lockdep_assert_wiphy(hw->wiphy); 982 983 rtw89_leave_ps_mode(rtwdev); 984 return __rtw89_ops_sta_state(hw, vif, sta, old_state, new_state); 985 } 986 987 static int rtw89_ops_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd, 988 struct ieee80211_vif *vif, 989 struct ieee80211_sta *sta, 990 struct ieee80211_key_conf *key) 991 { 992 struct rtw89_dev *rtwdev = hw->priv; 993 int ret; 994 995 lockdep_assert_wiphy(hw->wiphy); 996 997 rtw89_leave_ps_mode(rtwdev); 998 999 switch (cmd) { 1000 case SET_KEY: 1001 rtw89_btc_ntfy_specific_packet(rtwdev, PACKET_EAPOL_END); 1002 ret = rtw89_cam_sec_key_add(rtwdev, vif, sta, key); 1003 if (ret && ret != -EOPNOTSUPP) { 1004 rtw89_err(rtwdev, "failed to add key to sec cam\n"); 1005 return ret; 1006 } 1007 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) 1008 rtw89_core_tid_rx_stats_reset(rtwdev); 1009 break; 1010 case DISABLE_KEY: 1011 flush_work(&rtwdev->txq_work); 1012 rtw89_hci_flush_queues(rtwdev, BIT(rtwdev->hw->queues) - 1, 1013 false); 1014 rtw89_mac_flush_txq(rtwdev, BIT(rtwdev->hw->queues) - 1, false); 1015 ret = rtw89_cam_sec_key_del(rtwdev, vif, sta, key, true); 1016 if (ret) { 1017 rtw89_err(rtwdev, "failed to remove key from sec cam\n"); 1018 return ret; 1019 } 1020 break; 1021 } 1022 1023 return ret; 1024 } 1025 1026 static int rtw89_ops_ampdu_action(struct ieee80211_hw *hw, 1027 struct ieee80211_vif *vif, 1028 struct ieee80211_ampdu_params *params) 1029 { 1030 struct rtw89_dev *rtwdev = hw->priv; 1031 struct ieee80211_sta *sta = params->sta; 1032 struct rtw89_sta *rtwsta = sta_to_rtwsta(sta); 1033 struct rtw89_vif *rtwvif = vif_to_rtwvif(vif); 1034 u16 tid = params->tid; 1035 struct ieee80211_txq *txq = sta->txq[tid]; 1036 struct rtw89_txq *rtwtxq = (struct rtw89_txq *)txq->drv_priv; 1037 1038 lockdep_assert_wiphy(rtwdev->hw->wiphy); 1039 1040 switch (params->action) { 1041 case IEEE80211_AMPDU_TX_START: 1042 return IEEE80211_AMPDU_TX_START_IMMEDIATE; 1043 case IEEE80211_AMPDU_TX_STOP_CONT: 1044 case IEEE80211_AMPDU_TX_STOP_FLUSH: 1045 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT: 1046 clear_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags); 1047 clear_bit(tid, rtwsta->ampdu_map); 1048 rtw89_chip_h2c_ampdu_cmac_tbl(rtwdev, rtwvif, rtwsta); 1049 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid); 1050 rtw89_leave_ps_mode(rtwdev); 1051 rtw89_phy_ra_recalc_agg_limit(rtwdev); 1052 break; 1053 case IEEE80211_AMPDU_TX_OPERATIONAL: 1054 set_bit(RTW89_TXQ_F_AMPDU, &rtwtxq->flags); 1055 rtwsta->ampdu_params[tid].agg_num = params->buf_size; 1056 rtwsta->ampdu_params[tid].amsdu = params->amsdu; 1057 set_bit(tid, rtwsta->ampdu_map); 1058 rtw89_leave_ps_mode(rtwdev); 1059 rtw89_chip_h2c_ampdu_cmac_tbl(rtwdev, rtwvif, rtwsta); 1060 rtw89_phy_ra_recalc_agg_limit(rtwdev); 1061 break; 1062 case IEEE80211_AMPDU_RX_START: 1063 rtw89_core_tid_rx_stats_ctrl(rtwdev, rtwsta, params, true); 1064 rtw89_chip_h2c_ba_cam(rtwdev, rtwsta, true, params); 1065 break; 1066 case IEEE80211_AMPDU_RX_STOP: 1067 rtw89_core_tid_rx_stats_ctrl(rtwdev, rtwsta, params, false); 1068 rtw89_chip_h2c_ba_cam(rtwdev, rtwsta, false, params); 1069 break; 1070 default: 1071 WARN_ON(1); 1072 return -ENOTSUPP; 1073 } 1074 1075 return 0; 1076 } 1077 1078 static int rtw89_ops_set_rts_threshold(struct ieee80211_hw *hw, int radio_idx, 1079 u32 value) 1080 { 1081 struct rtw89_dev *rtwdev = hw->priv; 1082 1083 lockdep_assert_wiphy(hw->wiphy); 1084 1085 rtw89_leave_ps_mode(rtwdev); 1086 if (test_bit(RTW89_FLAG_POWERON, rtwdev->flags)) 1087 rtw89_mac_update_rts_threshold(rtwdev); 1088 1089 return 0; 1090 } 1091 1092 static void rtw89_ops_sta_statistics(struct ieee80211_hw *hw, 1093 struct ieee80211_vif *vif, 1094 struct ieee80211_sta *sta, 1095 struct station_info *sinfo) 1096 { 1097 struct rtw89_sta *rtwsta = sta_to_rtwsta(sta); 1098 struct rtw89_sta_link *rtwsta_link; 1099 1100 rtwsta_link = rtw89_get_designated_link(rtwsta); 1101 if (unlikely(!rtwsta_link)) 1102 return; 1103 1104 sinfo->txrate = rtwsta_link->ra_report.txrate; 1105 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE); 1106 } 1107 1108 static 1109 void __rtw89_drop_packets(struct rtw89_dev *rtwdev, struct ieee80211_vif *vif) 1110 { 1111 struct rtw89_vif *rtwvif; 1112 1113 if (vif) { 1114 rtwvif = vif_to_rtwvif(vif); 1115 rtw89_mac_pkt_drop_vif(rtwdev, rtwvif); 1116 } else { 1117 rtw89_for_each_rtwvif(rtwdev, rtwvif) 1118 rtw89_mac_pkt_drop_vif(rtwdev, rtwvif); 1119 } 1120 } 1121 1122 static void rtw89_ops_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1123 u32 queues, bool drop) 1124 { 1125 struct rtw89_dev *rtwdev = hw->priv; 1126 1127 lockdep_assert_wiphy(hw->wiphy); 1128 1129 rtw89_leave_lps(rtwdev); 1130 rtw89_hci_flush_queues(rtwdev, queues, drop); 1131 1132 if (drop && !RTW89_CHK_FW_FEATURE(NO_PACKET_DROP, &rtwdev->fw)) 1133 __rtw89_drop_packets(rtwdev, vif); 1134 else 1135 rtw89_mac_flush_txq(rtwdev, queues, drop); 1136 } 1137 1138 struct rtw89_iter_bitrate_mask_data { 1139 struct rtw89_dev *rtwdev; 1140 struct ieee80211_vif *vif; 1141 const struct cfg80211_bitrate_mask *mask; 1142 }; 1143 1144 static void rtw89_ra_mask_info_update_iter(void *data, struct ieee80211_sta *sta) 1145 { 1146 struct rtw89_iter_bitrate_mask_data *br_data = data; 1147 struct rtw89_sta *rtwsta = sta_to_rtwsta(sta); 1148 struct rtw89_vif *rtwvif = rtwsta->rtwvif; 1149 struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif); 1150 struct rtw89_sta_link *rtwsta_link; 1151 unsigned int link_id; 1152 1153 if (vif != br_data->vif || vif->p2p) 1154 return; 1155 1156 rtw89_sta_for_each_link(rtwsta, rtwsta_link, link_id) { 1157 rtwsta_link->use_cfg_mask = true; 1158 rtwsta_link->mask = *br_data->mask; 1159 } 1160 1161 rtw89_phy_ra_update_sta(br_data->rtwdev, sta, IEEE80211_RC_SUPP_RATES_CHANGED); 1162 } 1163 1164 static void rtw89_ra_mask_info_update(struct rtw89_dev *rtwdev, 1165 struct ieee80211_vif *vif, 1166 const struct cfg80211_bitrate_mask *mask) 1167 { 1168 struct rtw89_iter_bitrate_mask_data br_data = { .rtwdev = rtwdev, 1169 .vif = vif, 1170 .mask = mask}; 1171 1172 ieee80211_iterate_stations_atomic(rtwdev->hw, rtw89_ra_mask_info_update_iter, 1173 &br_data); 1174 } 1175 1176 static int rtw89_ops_set_bitrate_mask(struct ieee80211_hw *hw, 1177 struct ieee80211_vif *vif, 1178 const struct cfg80211_bitrate_mask *mask) 1179 { 1180 struct rtw89_dev *rtwdev = hw->priv; 1181 1182 lockdep_assert_wiphy(hw->wiphy); 1183 1184 rtw89_phy_rate_pattern_vif(rtwdev, vif, mask); 1185 rtw89_ra_mask_info_update(rtwdev, vif, mask); 1186 1187 return 0; 1188 } 1189 1190 static 1191 int rtw89_ops_set_antenna(struct ieee80211_hw *hw, int radio_idx, u32 tx_ant, u32 rx_ant) 1192 { 1193 struct rtw89_dev *rtwdev = hw->priv; 1194 struct rtw89_hal *hal = &rtwdev->hal; 1195 const struct rtw89_chip_info *chip; 1196 1197 lockdep_assert_wiphy(hw->wiphy); 1198 1199 chip = rtwdev->chip; 1200 1201 if (hal->ant_diversity) { 1202 if (tx_ant != rx_ant || hweight32(tx_ant) != 1) 1203 return -EINVAL; 1204 } else if (chip->ops->cfg_txrx_path) { 1205 /* With cfg_txrx_path ops, chips can configure rx_ant */ 1206 } else if (rx_ant != hw->wiphy->available_antennas_rx && rx_ant != hal->antenna_rx) { 1207 return -EINVAL; 1208 } 1209 1210 hal->antenna_tx = tx_ant; 1211 hal->antenna_rx = rx_ant; 1212 hal->tx_path_diversity = false; 1213 hal->ant_diversity_fixed = true; 1214 1215 return 0; 1216 } 1217 1218 static 1219 int rtw89_ops_get_antenna(struct ieee80211_hw *hw, int radio_idx, u32 *tx_ant, 1220 u32 *rx_ant) 1221 { 1222 struct rtw89_dev *rtwdev = hw->priv; 1223 struct rtw89_hal *hal = &rtwdev->hal; 1224 1225 *tx_ant = hal->antenna_tx; 1226 *rx_ant = hal->antenna_rx; 1227 1228 return 0; 1229 } 1230 1231 static void rtw89_ops_sw_scan_start(struct ieee80211_hw *hw, 1232 struct ieee80211_vif *vif, 1233 const u8 *mac_addr) 1234 { 1235 struct rtw89_dev *rtwdev = hw->priv; 1236 struct rtw89_vif *rtwvif = vif_to_rtwvif(vif); 1237 struct rtw89_vif_link *rtwvif_link; 1238 1239 lockdep_assert_wiphy(hw->wiphy); 1240 1241 rtwvif_link = rtw89_get_designated_link(rtwvif); 1242 if (unlikely(!rtwvif_link)) { 1243 rtw89_err(rtwdev, "sw scan start: find no designated link\n"); 1244 return; 1245 } 1246 1247 rtw89_leave_lps(rtwdev); 1248 1249 rtw89_core_scan_start(rtwdev, rtwvif_link, mac_addr, false); 1250 } 1251 1252 static void rtw89_ops_sw_scan_complete(struct ieee80211_hw *hw, 1253 struct ieee80211_vif *vif) 1254 { 1255 struct rtw89_dev *rtwdev = hw->priv; 1256 struct rtw89_vif *rtwvif = vif_to_rtwvif(vif); 1257 struct rtw89_vif_link *rtwvif_link; 1258 1259 lockdep_assert_wiphy(hw->wiphy); 1260 1261 rtwvif_link = rtw89_get_designated_link(rtwvif); 1262 if (unlikely(!rtwvif_link)) { 1263 rtw89_err(rtwdev, "sw scan complete: find no designated link\n"); 1264 return; 1265 } 1266 1267 rtw89_core_scan_complete(rtwdev, rtwvif_link, false); 1268 } 1269 1270 static void rtw89_ops_reconfig_complete(struct ieee80211_hw *hw, 1271 enum ieee80211_reconfig_type reconfig_type) 1272 { 1273 struct rtw89_dev *rtwdev = hw->priv; 1274 1275 if (reconfig_type == IEEE80211_RECONFIG_TYPE_RESTART) 1276 rtw89_ser_recfg_done(rtwdev); 1277 } 1278 1279 static int rtw89_ops_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif, 1280 struct ieee80211_scan_request *req) 1281 { 1282 struct rtw89_dev *rtwdev = hw->priv; 1283 struct rtw89_vif *rtwvif = vif_to_rtwvif(vif); 1284 struct rtw89_vif_link *rtwvif_link; 1285 struct rtw89_hal *hal = &rtwdev->hal; 1286 int ret; 1287 1288 lockdep_assert_wiphy(hw->wiphy); 1289 1290 if (!RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD, &rtwdev->fw)) 1291 return 1; 1292 1293 if (hal->disabled_dm_bitmap & BIT(RTW89_DM_HW_SCAN)) { 1294 rtw89_debug(rtwdev, RTW89_DBG_HW_SCAN, 1295 "reject hw scan due to disabled_dm\n"); 1296 return -EBUSY; 1297 } 1298 1299 if (rtwdev->scanning || rtwvif->offchan) 1300 return -EBUSY; 1301 1302 rtwvif_link = rtw89_get_designated_link(rtwvif); 1303 if (unlikely(!rtwvif_link)) { 1304 rtw89_err(rtwdev, "hw scan: find no designated link\n"); 1305 return -ENOLINK; 1306 } 1307 1308 rtw89_leave_lps(rtwdev); 1309 rtw89_leave_ips_by_hwflags(rtwdev); 1310 1311 ret = rtw89_hw_scan_start(rtwdev, rtwvif_link, req); 1312 if (ret) 1313 return ret; 1314 1315 ret = rtw89_hw_scan_offload(rtwdev, rtwvif_link, true); 1316 if (ret) { 1317 rtw89_hw_scan_abort(rtwdev, rtwvif_link); 1318 rtw89_err(rtwdev, "HW scan failed with status: %d\n", ret); 1319 } 1320 1321 return ret; 1322 } 1323 1324 static void rtw89_ops_cancel_hw_scan(struct ieee80211_hw *hw, 1325 struct ieee80211_vif *vif) 1326 { 1327 struct rtw89_dev *rtwdev = hw->priv; 1328 struct rtw89_vif *rtwvif = vif_to_rtwvif(vif); 1329 struct rtw89_vif_link *rtwvif_link; 1330 1331 lockdep_assert_wiphy(hw->wiphy); 1332 1333 if (!RTW89_CHK_FW_FEATURE(SCAN_OFFLOAD, &rtwdev->fw)) 1334 return; 1335 1336 if (!rtwdev->scanning) 1337 return; 1338 1339 rtwvif_link = rtw89_get_designated_link(rtwvif); 1340 if (unlikely(!rtwvif_link)) { 1341 rtw89_err(rtwdev, "cancel hw scan: find no designated link\n"); 1342 return; 1343 } 1344 1345 rtw89_hw_scan_abort(rtwdev, rtwvif_link); 1346 } 1347 1348 static void rtw89_ops_sta_rc_update(struct ieee80211_hw *hw, 1349 struct ieee80211_vif *vif, 1350 struct ieee80211_link_sta *link_sta, 1351 u32 changed) 1352 { 1353 struct rtw89_sta *rtwsta = sta_to_rtwsta(link_sta->sta); 1354 struct rtw89_dev *rtwdev = hw->priv; 1355 struct rtw89_sta_link *rtwsta_link; 1356 1357 rtwsta_link = rtwsta->links[link_sta->link_id]; 1358 if (unlikely(!rtwsta_link)) 1359 return; 1360 1361 rtw89_phy_ra_update_sta_link(rtwdev, rtwsta_link, changed); 1362 } 1363 1364 static int rtw89_ops_add_chanctx(struct ieee80211_hw *hw, 1365 struct ieee80211_chanctx_conf *ctx) 1366 { 1367 struct rtw89_dev *rtwdev = hw->priv; 1368 1369 lockdep_assert_wiphy(hw->wiphy); 1370 1371 return rtw89_chanctx_ops_add(rtwdev, ctx); 1372 } 1373 1374 static void rtw89_ops_remove_chanctx(struct ieee80211_hw *hw, 1375 struct ieee80211_chanctx_conf *ctx) 1376 { 1377 struct rtw89_dev *rtwdev = hw->priv; 1378 1379 lockdep_assert_wiphy(hw->wiphy); 1380 1381 rtw89_chanctx_ops_remove(rtwdev, ctx); 1382 } 1383 1384 static void rtw89_ops_change_chanctx(struct ieee80211_hw *hw, 1385 struct ieee80211_chanctx_conf *ctx, 1386 u32 changed) 1387 { 1388 struct rtw89_dev *rtwdev = hw->priv; 1389 1390 lockdep_assert_wiphy(hw->wiphy); 1391 1392 rtw89_chanctx_ops_change(rtwdev, ctx, changed); 1393 } 1394 1395 static int rtw89_ops_assign_vif_chanctx(struct ieee80211_hw *hw, 1396 struct ieee80211_vif *vif, 1397 struct ieee80211_bss_conf *link_conf, 1398 struct ieee80211_chanctx_conf *ctx) 1399 { 1400 struct rtw89_dev *rtwdev = hw->priv; 1401 struct rtw89_vif *rtwvif = vif_to_rtwvif(vif); 1402 struct rtw89_vif_link *rtwvif_link; 1403 1404 lockdep_assert_wiphy(hw->wiphy); 1405 1406 rtwvif_link = rtwvif->links[link_conf->link_id]; 1407 if (unlikely(!rtwvif_link)) { 1408 rtw89_err(rtwdev, 1409 "%s: rtwvif link (link_id %u) is not active\n", 1410 __func__, link_conf->link_id); 1411 return -ENOLINK; 1412 } 1413 1414 return rtw89_chanctx_ops_assign_vif(rtwdev, rtwvif_link, ctx); 1415 } 1416 1417 static void rtw89_ops_unassign_vif_chanctx(struct ieee80211_hw *hw, 1418 struct ieee80211_vif *vif, 1419 struct ieee80211_bss_conf *link_conf, 1420 struct ieee80211_chanctx_conf *ctx) 1421 { 1422 struct rtw89_dev *rtwdev = hw->priv; 1423 struct rtw89_vif *rtwvif = vif_to_rtwvif(vif); 1424 struct rtw89_vif_link *rtwvif_link; 1425 1426 lockdep_assert_wiphy(hw->wiphy); 1427 1428 rtwvif_link = rtwvif->links[link_conf->link_id]; 1429 if (unlikely(!rtwvif_link)) { 1430 rtw89_err(rtwdev, 1431 "%s: rtwvif link (link_id %u) is not active\n", 1432 __func__, link_conf->link_id); 1433 return; 1434 } 1435 1436 rtw89_chanctx_ops_unassign_vif(rtwdev, rtwvif_link, ctx); 1437 } 1438 1439 static 1440 int rtw89_ops_switch_vif_chanctx(struct ieee80211_hw *hw, 1441 struct ieee80211_vif_chanctx_switch *vifs, 1442 int n_vifs, 1443 enum ieee80211_chanctx_switch_mode mode) 1444 { 1445 struct rtw89_dev *rtwdev = hw->priv; 1446 bool replace; 1447 int ret; 1448 int i; 1449 1450 lockdep_assert_wiphy(hw->wiphy); 1451 1452 switch (mode) { 1453 case CHANCTX_SWMODE_REASSIGN_VIF: 1454 replace = false; 1455 break; 1456 case CHANCTX_SWMODE_SWAP_CONTEXTS: 1457 replace = true; 1458 break; 1459 default: 1460 return -EOPNOTSUPP; 1461 } 1462 1463 for (i = 0; i < n_vifs; i++) { 1464 struct ieee80211_vif_chanctx_switch *p = &vifs[i]; 1465 struct ieee80211_bss_conf *link_conf = p->link_conf; 1466 struct rtw89_vif *rtwvif = vif_to_rtwvif(p->vif); 1467 struct rtw89_vif_link *rtwvif_link; 1468 1469 rtwvif_link = rtwvif->links[link_conf->link_id]; 1470 if (unlikely(!rtwvif_link)) { 1471 rtw89_err(rtwdev, 1472 "%s: rtwvif link (link_id %u) is not active\n", 1473 __func__, link_conf->link_id); 1474 return -ENOLINK; 1475 } 1476 1477 ret = rtw89_chanctx_ops_reassign_vif(rtwdev, rtwvif_link, 1478 p->old_ctx, p->new_ctx, 1479 replace); 1480 if (ret) 1481 return ret; 1482 } 1483 1484 return 0; 1485 } 1486 1487 static void rtw89_ops_channel_switch_beacon(struct ieee80211_hw *hw, 1488 struct ieee80211_vif *vif, 1489 struct cfg80211_chan_def *chandef) 1490 { 1491 struct rtw89_vif *rtwvif = vif_to_rtwvif(vif); 1492 struct rtw89_dev *rtwdev = hw->priv; 1493 struct rtw89_vif_link *rtwvif_link; 1494 1495 BUILD_BUG_ON(RTW89_MLD_NON_STA_LINK_NUM != 1); 1496 1497 rtwvif_link = rtw89_get_designated_link(rtwvif); 1498 if (unlikely(!rtwvif_link)) { 1499 rtw89_err(rtwdev, "chsw bcn: find no designated link\n"); 1500 return; 1501 } 1502 1503 wiphy_delayed_work_queue(hw->wiphy, &rtwvif_link->csa_beacon_work, 0); 1504 } 1505 1506 static int rtw89_ops_remain_on_channel(struct ieee80211_hw *hw, 1507 struct ieee80211_vif *vif, 1508 struct ieee80211_channel *chan, 1509 int duration, 1510 enum ieee80211_roc_type type) 1511 { 1512 struct rtw89_dev *rtwdev = hw->priv; 1513 struct rtw89_vif *rtwvif = vif_to_rtwvif_safe(vif); 1514 struct rtw89_roc *roc = &rtwvif->roc; 1515 1516 lockdep_assert_wiphy(hw->wiphy); 1517 1518 if (!rtwvif) 1519 return -EINVAL; 1520 1521 if (roc->state != RTW89_ROC_IDLE) { 1522 return -EBUSY; 1523 } 1524 1525 if (rtwdev->scanning) 1526 rtw89_hw_scan_abort(rtwdev, rtwdev->scan_info.scanning_vif); 1527 1528 if (type == IEEE80211_ROC_TYPE_MGMT_TX) 1529 roc->state = RTW89_ROC_MGMT; 1530 else 1531 roc->state = RTW89_ROC_NORMAL; 1532 1533 roc->duration = duration; 1534 roc->chan = *chan; 1535 roc->type = type; 1536 1537 rtw89_roc_start(rtwdev, rtwvif); 1538 1539 return 0; 1540 } 1541 1542 static int rtw89_ops_cancel_remain_on_channel(struct ieee80211_hw *hw, 1543 struct ieee80211_vif *vif) 1544 { 1545 struct rtw89_dev *rtwdev = hw->priv; 1546 struct rtw89_vif *rtwvif = vif_to_rtwvif_safe(vif); 1547 1548 lockdep_assert_wiphy(hw->wiphy); 1549 1550 if (!rtwvif) 1551 return -EINVAL; 1552 1553 wiphy_delayed_work_cancel(hw->wiphy, &rtwvif->roc.roc_work); 1554 1555 rtw89_roc_end(rtwdev, rtwvif); 1556 1557 return 0; 1558 } 1559 1560 static void rtw89_set_tid_config_iter(void *data, struct ieee80211_sta *sta) 1561 { 1562 struct cfg80211_tid_config *tid_config = data; 1563 struct rtw89_sta *rtwsta = sta_to_rtwsta(sta); 1564 struct rtw89_dev *rtwdev = rtwsta->rtwdev; 1565 1566 rtw89_core_set_tid_config(rtwdev, sta, tid_config); 1567 } 1568 1569 static int rtw89_ops_set_tid_config(struct ieee80211_hw *hw, 1570 struct ieee80211_vif *vif, 1571 struct ieee80211_sta *sta, 1572 struct cfg80211_tid_config *tid_config) 1573 { 1574 struct rtw89_dev *rtwdev = hw->priv; 1575 1576 lockdep_assert_wiphy(hw->wiphy); 1577 1578 if (sta) 1579 rtw89_core_set_tid_config(rtwdev, sta, tid_config); 1580 else 1581 ieee80211_iterate_stations_atomic(rtwdev->hw, 1582 rtw89_set_tid_config_iter, 1583 tid_config); 1584 1585 return 0; 1586 } 1587 1588 static bool rtw89_can_work_on_links(struct rtw89_dev *rtwdev, 1589 struct ieee80211_vif *vif, u16 links) 1590 { 1591 struct rtw89_vif *rtwvif = vif_to_rtwvif(vif); 1592 u8 w = hweight16(links); 1593 1594 if (vif->type != NL80211_IFTYPE_STATION && 1595 w > RTW89_MLD_NON_STA_LINK_NUM) 1596 return false; 1597 1598 return w <= rtwvif->links_inst_valid_num; 1599 } 1600 1601 static bool rtw89_ops_can_activate_links(struct ieee80211_hw *hw, 1602 struct ieee80211_vif *vif, 1603 u16 active_links) 1604 { 1605 struct rtw89_dev *rtwdev = hw->priv; 1606 struct rtw89_vif *rtwvif = vif_to_rtwvif(vif); 1607 u16 current_links = vif->active_links; 1608 struct rtw89_vif_ml_trans trans = { 1609 .mediate_links = current_links | active_links, 1610 .links_to_del = current_links & ~active_links, 1611 .links_to_add = active_links & ~current_links, 1612 }; 1613 1614 lockdep_assert_wiphy(hw->wiphy); 1615 1616 if (!rtw89_can_work_on_links(rtwdev, vif, active_links)) 1617 return false; 1618 1619 /* 1620 * Leave LPS at the beginning of ieee80211_set_active_links(). 1621 * Because the entire process takes the same lock as our track 1622 * work, LPS will not enter during ieee80211_set_active_links(). 1623 */ 1624 rtw89_leave_lps(rtwdev); 1625 1626 rtwvif->ml_trans = trans; 1627 1628 return true; 1629 } 1630 1631 static void __rtw89_ops_clr_vif_links(struct rtw89_dev *rtwdev, 1632 struct rtw89_vif *rtwvif, 1633 unsigned long clr_links) 1634 { 1635 struct rtw89_vif_link *rtwvif_link; 1636 unsigned int link_id; 1637 1638 for_each_set_bit(link_id, &clr_links, IEEE80211_MLD_MAX_NUM_LINKS) { 1639 rtwvif_link = rtwvif->links[link_id]; 1640 if (unlikely(!rtwvif_link)) 1641 continue; 1642 1643 rtw89_fw_h2c_trx_protect(rtwdev, rtwvif_link->phy_idx, false); 1644 1645 __rtw89_ops_remove_iface_link(rtwdev, rtwvif_link); 1646 1647 rtw89_vif_unset_link(rtwvif, link_id); 1648 } 1649 } 1650 1651 static int __rtw89_ops_set_vif_links(struct rtw89_dev *rtwdev, 1652 struct rtw89_vif *rtwvif, 1653 unsigned long set_links) 1654 { 1655 struct rtw89_vif_link *rtwvif_link; 1656 unsigned int link_id; 1657 int ret; 1658 1659 for_each_set_bit(link_id, &set_links, IEEE80211_MLD_MAX_NUM_LINKS) { 1660 rtwvif_link = rtw89_vif_set_link(rtwvif, link_id); 1661 if (!rtwvif_link) 1662 return -EINVAL; 1663 1664 ret = __rtw89_ops_add_iface_link(rtwdev, rtwvif_link); 1665 if (ret) { 1666 rtw89_err(rtwdev, "%s: failed to add iface (link id %u)\n", 1667 __func__, link_id); 1668 return ret; 1669 } 1670 rtw89_fw_h2c_trx_protect(rtwdev, rtwvif_link->phy_idx, true); 1671 } 1672 1673 return 0; 1674 } 1675 1676 static void rtw89_vif_update_fw_links(struct rtw89_dev *rtwdev, 1677 struct rtw89_vif *rtwvif, 1678 u16 current_links, bool en) 1679 { 1680 struct rtw89_vif_ml_trans *trans = &rtwvif->ml_trans; 1681 struct rtw89_vif_link *rtwvif_link; 1682 unsigned int link_id; 1683 unsigned long links; 1684 1685 /* Do follow-up when all updating links exist. */ 1686 if (current_links != trans->mediate_links) 1687 return; 1688 1689 if (en) 1690 links = trans->links_to_add; 1691 else 1692 links = trans->links_to_del; 1693 1694 for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) { 1695 rtwvif_link = rtwvif->links[link_id]; 1696 if (unlikely(!rtwvif_link)) 1697 continue; 1698 1699 rtw89_fw_h2c_mlo_link_cfg(rtwdev, rtwvif_link, en); 1700 } 1701 } 1702 1703 static 1704 int rtw89_ops_change_vif_links(struct ieee80211_hw *hw, 1705 struct ieee80211_vif *vif, 1706 u16 old_links, u16 new_links, 1707 struct ieee80211_bss_conf *old[IEEE80211_MLD_MAX_NUM_LINKS]) 1708 { 1709 struct rtw89_dev *rtwdev = hw->priv; 1710 struct rtw89_vif *rtwvif = vif_to_rtwvif(vif); 1711 unsigned long clr_links = old_links & ~new_links; 1712 unsigned long set_links = new_links & ~old_links; 1713 bool removing_links = !old_links || clr_links; 1714 struct rtw89_link_conf_container *snap; 1715 int ret = 0; 1716 int i; 1717 1718 lockdep_assert_wiphy(hw->wiphy); 1719 1720 rtw89_debug(rtwdev, RTW89_DBG_STATE, 1721 "%s: old_links (0x%08x) -> new_links (0x%08x)\n", 1722 __func__, old_links, new_links); 1723 1724 if (!rtw89_can_work_on_links(rtwdev, vif, new_links)) 1725 return -EOPNOTSUPP; 1726 1727 if (removing_links) { 1728 snap = kzalloc_obj(*snap); 1729 if (!snap) 1730 return -ENOMEM; 1731 1732 for (i = 0; i < ARRAY_SIZE(snap->link_conf); i++) 1733 snap->link_conf[i] = old[i]; 1734 1735 rcu_assign_pointer(rtwvif->snap_link_confs, snap); 1736 } 1737 1738 /* might depend on @snap; don't change order */ 1739 rtw89_leave_ips_by_hwflags(rtwdev); 1740 1741 if (rtwdev->scanning) 1742 rtw89_hw_scan_abort(rtwdev, rtwdev->scan_info.scanning_vif); 1743 1744 rtw89_vif_update_fw_links(rtwdev, rtwvif, old_links, true); 1745 1746 if (!old_links) 1747 __rtw89_ops_clr_vif_links(rtwdev, rtwvif, 1748 BIT(RTW89_VIF_IDLE_LINK_ID)); 1749 else if (clr_links) 1750 __rtw89_ops_clr_vif_links(rtwdev, rtwvif, clr_links); 1751 1752 if (removing_links) { 1753 /* @snap is required if and only if during removing links. 1754 * However, it's done here. So, cleanup @snap immediately. 1755 */ 1756 rcu_assign_pointer(rtwvif->snap_link_confs, NULL); 1757 1758 /* The pointers in @old will free after this function return, 1759 * so synchronously wait for all readers of snap to be done. 1760 */ 1761 synchronize_rcu(); 1762 kfree(snap); 1763 } 1764 1765 if (set_links) { 1766 ret = __rtw89_ops_set_vif_links(rtwdev, rtwvif, set_links); 1767 if (ret) 1768 __rtw89_ops_clr_vif_links(rtwdev, rtwvif, set_links); 1769 } else if (!new_links) { 1770 ret = __rtw89_ops_set_vif_links(rtwdev, rtwvif, 1771 BIT(RTW89_VIF_IDLE_LINK_ID)); 1772 if (ret) 1773 __rtw89_ops_clr_vif_links(rtwdev, rtwvif, 1774 BIT(RTW89_VIF_IDLE_LINK_ID)); 1775 } 1776 1777 if (!ret) 1778 rtw89_vif_update_fw_links(rtwdev, rtwvif, new_links, false); 1779 1780 rtw89_enter_ips_by_hwflags(rtwdev); 1781 return ret; 1782 } 1783 1784 static void __rtw89_ops_clr_sta_links(struct rtw89_dev *rtwdev, 1785 struct rtw89_sta *rtwsta, 1786 unsigned long clr_links) 1787 { 1788 struct rtw89_vif_link *rtwvif_link; 1789 struct rtw89_sta_link *rtwsta_link; 1790 unsigned int link_id; 1791 1792 for_each_set_bit(link_id, &clr_links, IEEE80211_MLD_MAX_NUM_LINKS) { 1793 rtwsta_link = rtwsta->links[link_id]; 1794 if (unlikely(!rtwsta_link)) 1795 continue; 1796 1797 rtwvif_link = rtwsta_link->rtwvif_link; 1798 1799 rtw89_core_sta_link_disassoc(rtwdev, rtwvif_link, rtwsta_link); 1800 rtw89_core_sta_link_disconnect(rtwdev, rtwvif_link, rtwsta_link); 1801 rtw89_core_sta_link_remove(rtwdev, rtwvif_link, rtwsta_link); 1802 1803 rtw89_sta_unset_link(rtwsta, link_id); 1804 } 1805 } 1806 1807 static int __rtw89_ops_set_sta_links(struct rtw89_dev *rtwdev, 1808 struct rtw89_sta *rtwsta, 1809 unsigned long set_links) 1810 { 1811 struct rtw89_vif_link *rtwvif_link; 1812 struct rtw89_sta_link *rtwsta_link; 1813 unsigned int link_id; 1814 u8 sec_cam_idx; 1815 int ret; 1816 1817 for_each_set_bit(link_id, &set_links, IEEE80211_MLD_MAX_NUM_LINKS) { 1818 rtwsta_link = rtw89_sta_set_link(rtwsta, link_id); 1819 if (!rtwsta_link) 1820 return -EINVAL; 1821 1822 rtwvif_link = rtwsta_link->rtwvif_link; 1823 1824 ret = rtw89_core_sta_link_add(rtwdev, rtwvif_link, rtwsta_link); 1825 if (ret) { 1826 rtw89_err(rtwdev, "%s: failed to add sta (link id %u)\n", 1827 __func__, link_id); 1828 return ret; 1829 } 1830 1831 rtw89_vif_type_mapping(rtwvif_link, true); 1832 1833 ret = rtw89_core_sta_link_assoc(rtwdev, rtwvif_link, rtwsta_link); 1834 if (ret) { 1835 rtw89_err(rtwdev, "%s: failed to assoc sta (link id %u)\n", 1836 __func__, link_id); 1837 return ret; 1838 } 1839 1840 __rtw89_ops_bss_link_assoc(rtwdev, rtwvif_link); 1841 1842 for_each_set_bit(sec_cam_idx, rtwsta->pairwise_sec_cam_map, 1843 RTW89_MAX_SEC_CAM_NUM) { 1844 ret = rtw89_cam_attach_link_sec_cam(rtwdev, 1845 rtwvif_link, 1846 rtwsta_link, 1847 sec_cam_idx); 1848 if (ret) { 1849 rtw89_err(rtwdev, 1850 "%s: failed to apply pairwise key (link id %u)\n", 1851 __func__, link_id); 1852 return ret; 1853 } 1854 } 1855 } 1856 1857 return 0; 1858 } 1859 1860 static 1861 int rtw89_ops_change_sta_links(struct ieee80211_hw *hw, 1862 struct ieee80211_vif *vif, 1863 struct ieee80211_sta *sta, 1864 u16 old_links, u16 new_links) 1865 { 1866 struct rtw89_dev *rtwdev = hw->priv; 1867 struct rtw89_sta *rtwsta = sta_to_rtwsta(sta); 1868 unsigned long clr_links = old_links & ~new_links; 1869 unsigned long set_links = new_links & ~old_links; 1870 int ret = 0; 1871 1872 lockdep_assert_wiphy(hw->wiphy); 1873 1874 rtw89_debug(rtwdev, RTW89_DBG_STATE, 1875 "%s: old_links (0x%08x) -> new_links (0x%08x)\n", 1876 __func__, old_links, new_links); 1877 1878 if (!rtw89_can_work_on_links(rtwdev, vif, new_links)) 1879 return -EOPNOTSUPP; 1880 1881 rtw89_leave_ps_mode(rtwdev); 1882 1883 if (clr_links) 1884 __rtw89_ops_clr_sta_links(rtwdev, rtwsta, clr_links); 1885 1886 if (set_links) { 1887 ret = __rtw89_ops_set_sta_links(rtwdev, rtwsta, set_links); 1888 if (ret) 1889 __rtw89_ops_clr_sta_links(rtwdev, rtwsta, set_links); 1890 } 1891 1892 return ret; 1893 } 1894 1895 #ifdef CONFIG_PM 1896 static int rtw89_ops_suspend(struct ieee80211_hw *hw, 1897 struct cfg80211_wowlan *wowlan) 1898 { 1899 struct rtw89_dev *rtwdev = hw->priv; 1900 int ret; 1901 1902 lockdep_assert_wiphy(hw->wiphy); 1903 1904 set_bit(RTW89_FLAG_FORBIDDEN_TRACK_WORK, rtwdev->flags); 1905 wiphy_delayed_work_cancel(hw->wiphy, &rtwdev->track_work); 1906 wiphy_delayed_work_cancel(hw->wiphy, &rtwdev->track_ps_work); 1907 1908 ret = rtw89_wow_suspend(rtwdev, wowlan); 1909 if (ret) { 1910 rtw89_warn(rtwdev, "failed to suspend for wow %d\n", ret); 1911 clear_bit(RTW89_FLAG_FORBIDDEN_TRACK_WORK, rtwdev->flags); 1912 return 1; 1913 } 1914 1915 return 0; 1916 } 1917 1918 static int rtw89_ops_resume(struct ieee80211_hw *hw) 1919 { 1920 struct rtw89_dev *rtwdev = hw->priv; 1921 int ret; 1922 1923 lockdep_assert_wiphy(hw->wiphy); 1924 1925 ret = rtw89_wow_resume(rtwdev); 1926 if (ret) 1927 rtw89_warn(rtwdev, "failed to resume for wow %d\n", ret); 1928 1929 clear_bit(RTW89_FLAG_FORBIDDEN_TRACK_WORK, rtwdev->flags); 1930 wiphy_delayed_work_queue(hw->wiphy, &rtwdev->track_work, 1931 RTW89_TRACK_WORK_PERIOD); 1932 wiphy_delayed_work_queue(hw->wiphy, &rtwdev->track_ps_work, 1933 RTW89_TRACK_PS_WORK_PERIOD); 1934 1935 return ret ? 1 : 0; 1936 } 1937 1938 static void rtw89_ops_set_wakeup(struct ieee80211_hw *hw, bool enabled) 1939 { 1940 struct rtw89_dev *rtwdev = hw->priv; 1941 1942 device_set_wakeup_enable(rtwdev->dev, enabled); 1943 } 1944 1945 static void rtw89_set_rekey_data(struct ieee80211_hw *hw, 1946 struct ieee80211_vif *vif, 1947 struct cfg80211_gtk_rekey_data *data) 1948 { 1949 struct rtw89_dev *rtwdev = hw->priv; 1950 struct rtw89_wow_param *rtw_wow = &rtwdev->wow; 1951 struct rtw89_wow_gtk_info *gtk_info = &rtw_wow->gtk_info; 1952 1953 lockdep_assert_wiphy(hw->wiphy); 1954 1955 if (data->kek_len > sizeof(gtk_info->kek) || 1956 data->kck_len > sizeof(gtk_info->kck)) { 1957 rtw89_warn(rtwdev, "kek or kck length over fw limit\n"); 1958 return; 1959 } 1960 1961 memcpy(gtk_info->kek, data->kek, data->kek_len); 1962 memcpy(gtk_info->kck, data->kck, data->kck_len); 1963 } 1964 #endif 1965 1966 static int rtw89_ops_get_survey(struct ieee80211_hw *hw, int idx, 1967 struct survey_info *survey) 1968 { 1969 struct ieee80211_conf *conf = &hw->conf; 1970 struct rtw89_dev *rtwdev = hw->priv; 1971 struct rtw89_bb_ctx *bb; 1972 1973 if (idx == 0) { 1974 survey->channel = conf->chandef.chan; 1975 survey->filled = SURVEY_INFO_NOISE_DBM; 1976 survey->noise = RTW89_NOISE_DEFAULT; 1977 1978 return 0; 1979 } 1980 1981 rtw89_for_each_active_bb(rtwdev, bb) { 1982 struct rtw89_env_monitor_info *env = &bb->env_monitor; 1983 struct rtw89_nhm_report *rpt; 1984 1985 rpt = list_first_entry_or_null(&env->nhm_rpt_list, typeof(*rpt), list); 1986 if (!rpt) 1987 continue; 1988 1989 survey->filled = SURVEY_INFO_NOISE_DBM; 1990 survey->noise = rpt->noise - MAX_RSSI; 1991 survey->channel = rpt->channel; 1992 list_del_init(&rpt->list); 1993 1994 return 0; 1995 } 1996 1997 return -EINVAL; 1998 } 1999 2000 static void rtw89_ops_rfkill_poll(struct ieee80211_hw *hw) 2001 { 2002 struct rtw89_dev *rtwdev = hw->priv; 2003 2004 lockdep_assert_wiphy(hw->wiphy); 2005 2006 /* wl_disable GPIO get floating when entering LPS */ 2007 if (test_bit(RTW89_FLAG_RUNNING, rtwdev->flags) || 2008 test_bit(RTW89_FLAG_SHUTDOWN, rtwdev->flags)) 2009 return; 2010 2011 rtw89_core_rfkill_poll(rtwdev, false); 2012 } 2013 2014 const struct ieee80211_ops rtw89_ops = { 2015 .tx = rtw89_ops_tx, 2016 .wake_tx_queue = rtw89_ops_wake_tx_queue, 2017 .start = rtw89_ops_start, 2018 .stop = rtw89_ops_stop, 2019 .config = rtw89_ops_config, 2020 .add_interface = rtw89_ops_add_interface, 2021 .change_interface = rtw89_ops_change_interface, 2022 .remove_interface = rtw89_ops_remove_interface, 2023 .configure_filter = rtw89_ops_configure_filter, 2024 .vif_cfg_changed = rtw89_ops_vif_cfg_changed, 2025 .link_info_changed = rtw89_ops_link_info_changed, 2026 .start_ap = rtw89_ops_start_ap, 2027 .stop_ap = rtw89_ops_stop_ap, 2028 .set_tim = rtw89_ops_set_tim, 2029 .conf_tx = rtw89_ops_conf_tx, 2030 .sta_state = rtw89_ops_sta_state, 2031 .set_key = rtw89_ops_set_key, 2032 .ampdu_action = rtw89_ops_ampdu_action, 2033 .get_survey = rtw89_ops_get_survey, 2034 .set_rts_threshold = rtw89_ops_set_rts_threshold, 2035 .sta_statistics = rtw89_ops_sta_statistics, 2036 .flush = rtw89_ops_flush, 2037 .set_bitrate_mask = rtw89_ops_set_bitrate_mask, 2038 .set_antenna = rtw89_ops_set_antenna, 2039 .get_antenna = rtw89_ops_get_antenna, 2040 .sw_scan_start = rtw89_ops_sw_scan_start, 2041 .sw_scan_complete = rtw89_ops_sw_scan_complete, 2042 .reconfig_complete = rtw89_ops_reconfig_complete, 2043 .hw_scan = rtw89_ops_hw_scan, 2044 .cancel_hw_scan = rtw89_ops_cancel_hw_scan, 2045 .add_chanctx = rtw89_ops_add_chanctx, 2046 .remove_chanctx = rtw89_ops_remove_chanctx, 2047 .change_chanctx = rtw89_ops_change_chanctx, 2048 .assign_vif_chanctx = rtw89_ops_assign_vif_chanctx, 2049 .unassign_vif_chanctx = rtw89_ops_unassign_vif_chanctx, 2050 .switch_vif_chanctx = rtw89_ops_switch_vif_chanctx, 2051 .channel_switch_beacon = rtw89_ops_channel_switch_beacon, 2052 .remain_on_channel = rtw89_ops_remain_on_channel, 2053 .cancel_remain_on_channel = rtw89_ops_cancel_remain_on_channel, 2054 .set_sar_specs = rtw89_ops_set_sar_specs, 2055 .link_sta_rc_update = rtw89_ops_sta_rc_update, 2056 .set_tid_config = rtw89_ops_set_tid_config, 2057 .can_activate_links = rtw89_ops_can_activate_links, 2058 .change_vif_links = rtw89_ops_change_vif_links, 2059 .change_sta_links = rtw89_ops_change_sta_links, 2060 #ifdef CONFIG_PM 2061 .suspend = rtw89_ops_suspend, 2062 .resume = rtw89_ops_resume, 2063 .set_wakeup = rtw89_ops_set_wakeup, 2064 .set_rekey_data = rtw89_set_rekey_data, 2065 #endif 2066 .rfkill_poll = rtw89_ops_rfkill_poll, 2067 }; 2068 EXPORT_SYMBOL(rtw89_ops); 2069