1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* 3 * Copyright (C) 2022 - 2024 Intel Corporation 4 */ 5 #include "mvm.h" 6 #include "time-event.h" 7 8 static u32 iwl_mvm_get_free_fw_link_id(struct iwl_mvm *mvm, 9 struct iwl_mvm_vif *mvm_vif) 10 { 11 u32 link_id; 12 13 lockdep_assert_held(&mvm->mutex); 14 15 link_id = ffz(mvm->fw_link_ids_map); 16 17 /* this case can happen if there're deactivated but not removed links */ 18 if (link_id > IWL_MVM_FW_MAX_LINK_ID) 19 return IWL_MVM_FW_LINK_ID_INVALID; 20 21 mvm->fw_link_ids_map |= BIT(link_id); 22 return link_id; 23 } 24 25 static void iwl_mvm_release_fw_link_id(struct iwl_mvm *mvm, u32 link_id) 26 { 27 lockdep_assert_held(&mvm->mutex); 28 29 if (!WARN_ON(link_id > IWL_MVM_FW_MAX_LINK_ID)) 30 mvm->fw_link_ids_map &= ~BIT(link_id); 31 } 32 33 static int iwl_mvm_link_cmd_send(struct iwl_mvm *mvm, 34 struct iwl_link_config_cmd *cmd, 35 enum iwl_ctxt_action action) 36 { 37 int ret; 38 39 cmd->action = cpu_to_le32(action); 40 ret = iwl_mvm_send_cmd_pdu(mvm, 41 WIDE_ID(MAC_CONF_GROUP, LINK_CONFIG_CMD), 0, 42 sizeof(*cmd), cmd); 43 if (ret) 44 IWL_ERR(mvm, "Failed to send LINK_CONFIG_CMD (action:%d): %d\n", 45 action, ret); 46 return ret; 47 } 48 49 int iwl_mvm_add_link(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 50 struct ieee80211_bss_conf *link_conf) 51 { 52 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 53 unsigned int link_id = link_conf->link_id; 54 struct iwl_mvm_vif_link_info *link_info = mvmvif->link[link_id]; 55 struct iwl_link_config_cmd cmd = {}; 56 unsigned int cmd_id = WIDE_ID(MAC_CONF_GROUP, LINK_CONFIG_CMD); 57 u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 1); 58 59 if (WARN_ON_ONCE(!link_info)) 60 return -EINVAL; 61 62 if (link_info->fw_link_id == IWL_MVM_FW_LINK_ID_INVALID) { 63 link_info->fw_link_id = iwl_mvm_get_free_fw_link_id(mvm, 64 mvmvif); 65 if (link_info->fw_link_id >= ARRAY_SIZE(mvm->link_id_to_link_conf)) 66 return -EINVAL; 67 68 rcu_assign_pointer(mvm->link_id_to_link_conf[link_info->fw_link_id], 69 link_conf); 70 } 71 72 /* Update SF - Disable if needed. if this fails, SF might still be on 73 * while many macs are bound, which is forbidden - so fail the binding. 74 */ 75 if (iwl_mvm_sf_update(mvm, vif, false)) 76 return -EINVAL; 77 78 cmd.link_id = cpu_to_le32(link_info->fw_link_id); 79 cmd.mac_id = cpu_to_le32(mvmvif->id); 80 cmd.spec_link_id = link_conf->link_id; 81 WARN_ON_ONCE(link_info->phy_ctxt); 82 cmd.phy_id = cpu_to_le32(FW_CTXT_INVALID); 83 84 memcpy(cmd.local_link_addr, link_conf->addr, ETH_ALEN); 85 86 if (vif->type == NL80211_IFTYPE_ADHOC && link_conf->bssid) 87 memcpy(cmd.ibss_bssid_addr, link_conf->bssid, ETH_ALEN); 88 89 if (cmd_ver < 2) 90 cmd.listen_lmac = cpu_to_le32(link_info->listen_lmac); 91 92 return iwl_mvm_link_cmd_send(mvm, &cmd, FW_CTXT_ACTION_ADD); 93 } 94 95 int iwl_mvm_link_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 96 struct ieee80211_bss_conf *link_conf, 97 u32 changes, bool active) 98 { 99 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 100 unsigned int link_id = link_conf->link_id; 101 struct iwl_mvm_vif_link_info *link_info = mvmvif->link[link_id]; 102 struct iwl_mvm_phy_ctxt *phyctxt; 103 struct iwl_link_config_cmd cmd = {}; 104 u32 ht_flag, flags = 0, flags_mask = 0; 105 int ret; 106 unsigned int cmd_id = WIDE_ID(MAC_CONF_GROUP, LINK_CONFIG_CMD); 107 u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 1); 108 109 if (WARN_ON_ONCE(!link_info || 110 link_info->fw_link_id == IWL_MVM_FW_LINK_ID_INVALID)) 111 return -EINVAL; 112 113 if (changes & LINK_CONTEXT_MODIFY_ACTIVE) { 114 /* When activating a link, phy context should be valid; 115 * when deactivating a link, it also should be valid since 116 * the link was active before. So, do nothing in this case. 117 * Since a link is added first with FW_CTXT_INVALID, then we 118 * can get here in case it's removed before it was activated. 119 */ 120 if (!link_info->phy_ctxt) 121 return 0; 122 123 /* Catch early if driver tries to activate or deactivate a link 124 * twice. 125 */ 126 WARN_ON_ONCE(active == link_info->active); 127 128 /* When deactivating a link session protection should 129 * be stopped 130 */ 131 if (!active && vif->type == NL80211_IFTYPE_STATION) 132 iwl_mvm_stop_session_protection(mvm, vif); 133 } 134 135 cmd.link_id = cpu_to_le32(link_info->fw_link_id); 136 137 /* The phy_id, link address and listen_lmac can be modified only until 138 * the link becomes active, otherwise they will be ignored. 139 */ 140 phyctxt = link_info->phy_ctxt; 141 if (phyctxt) 142 cmd.phy_id = cpu_to_le32(phyctxt->id); 143 else 144 cmd.phy_id = cpu_to_le32(FW_CTXT_INVALID); 145 cmd.mac_id = cpu_to_le32(mvmvif->id); 146 147 memcpy(cmd.local_link_addr, link_conf->addr, ETH_ALEN); 148 149 cmd.active = cpu_to_le32(active); 150 151 if (vif->type == NL80211_IFTYPE_ADHOC && link_conf->bssid) 152 memcpy(cmd.ibss_bssid_addr, link_conf->bssid, ETH_ALEN); 153 154 iwl_mvm_set_fw_basic_rates(mvm, vif, link_conf, 155 &cmd.cck_rates, &cmd.ofdm_rates); 156 157 cmd.cck_short_preamble = cpu_to_le32(link_conf->use_short_preamble); 158 cmd.short_slot = cpu_to_le32(link_conf->use_short_slot); 159 160 /* The fw does not distinguish between ht and fat */ 161 ht_flag = LINK_PROT_FLG_HT_PROT | LINK_PROT_FLG_FAT_PROT; 162 iwl_mvm_set_fw_protection_flags(mvm, vif, link_conf, 163 &cmd.protection_flags, 164 ht_flag, LINK_PROT_FLG_TGG_PROTECT); 165 166 iwl_mvm_set_fw_qos_params(mvm, vif, link_conf, cmd.ac, 167 &cmd.qos_flags); 168 169 170 cmd.bi = cpu_to_le32(link_conf->beacon_int); 171 cmd.dtim_interval = cpu_to_le32(link_conf->beacon_int * 172 link_conf->dtim_period); 173 174 if (!link_conf->he_support || iwlwifi_mod_params.disable_11ax || 175 (vif->type == NL80211_IFTYPE_STATION && !vif->cfg.assoc)) { 176 changes &= ~LINK_CONTEXT_MODIFY_HE_PARAMS; 177 goto send_cmd; 178 } 179 180 cmd.htc_trig_based_pkt_ext = link_conf->htc_trig_based_pkt_ext; 181 182 if (link_conf->uora_exists) { 183 cmd.rand_alloc_ecwmin = 184 link_conf->uora_ocw_range & 0x7; 185 cmd.rand_alloc_ecwmax = 186 (link_conf->uora_ocw_range >> 3) & 0x7; 187 } 188 189 /* TODO how to set ndp_fdbk_buff_th_exp? */ 190 191 if (iwl_mvm_set_fw_mu_edca_params(mvm, mvmvif->link[link_id], 192 &cmd.trig_based_txf[0])) { 193 flags |= LINK_FLG_MU_EDCA_CW; 194 flags_mask |= LINK_FLG_MU_EDCA_CW; 195 } 196 197 if (changes & LINK_CONTEXT_MODIFY_EHT_PARAMS) { 198 struct ieee80211_chanctx_conf *ctx; 199 struct cfg80211_chan_def *def = NULL; 200 201 rcu_read_lock(); 202 ctx = rcu_dereference(link_conf->chanctx_conf); 203 if (ctx) 204 def = iwl_mvm_chanctx_def(mvm, ctx); 205 206 if (iwlwifi_mod_params.disable_11be || 207 !link_conf->eht_support || !def || 208 iwl_fw_lookup_cmd_ver(mvm->fw, PHY_CONTEXT_CMD, 1) >= 6) 209 changes &= ~LINK_CONTEXT_MODIFY_EHT_PARAMS; 210 else 211 cmd.puncture_mask = cpu_to_le16(def->punctured); 212 rcu_read_unlock(); 213 } 214 215 cmd.bss_color = link_conf->he_bss_color.color; 216 217 if (!link_conf->he_bss_color.enabled) { 218 flags |= LINK_FLG_BSS_COLOR_DIS; 219 flags_mask |= LINK_FLG_BSS_COLOR_DIS; 220 } 221 222 cmd.frame_time_rts_th = cpu_to_le16(link_conf->frame_time_rts_th); 223 224 /* Block 26-tone RU OFDMA transmissions */ 225 if (link_info->he_ru_2mhz_block) { 226 flags |= LINK_FLG_RU_2MHZ_BLOCK; 227 flags_mask |= LINK_FLG_RU_2MHZ_BLOCK; 228 } 229 230 if (link_conf->nontransmitted) { 231 ether_addr_copy(cmd.ref_bssid_addr, 232 link_conf->transmitter_bssid); 233 cmd.bssid_index = link_conf->bssid_index; 234 } 235 236 send_cmd: 237 cmd.modify_mask = cpu_to_le32(changes); 238 cmd.flags = cpu_to_le32(flags); 239 cmd.flags_mask = cpu_to_le32(flags_mask); 240 cmd.spec_link_id = link_conf->link_id; 241 if (cmd_ver < 2) 242 cmd.listen_lmac = cpu_to_le32(link_info->listen_lmac); 243 244 ret = iwl_mvm_link_cmd_send(mvm, &cmd, FW_CTXT_ACTION_MODIFY); 245 if (!ret && (changes & LINK_CONTEXT_MODIFY_ACTIVE)) 246 link_info->active = active; 247 248 return ret; 249 } 250 251 int iwl_mvm_remove_link(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 252 struct ieee80211_bss_conf *link_conf) 253 { 254 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 255 unsigned int link_id = link_conf->link_id; 256 struct iwl_mvm_vif_link_info *link_info = mvmvif->link[link_id]; 257 struct iwl_link_config_cmd cmd = {}; 258 int ret; 259 260 /* mac80211 thought we have the link, but it was never configured */ 261 if (WARN_ON(!link_info || 262 link_info->fw_link_id >= ARRAY_SIZE(mvm->link_id_to_link_conf))) 263 return 0; 264 265 RCU_INIT_POINTER(mvm->link_id_to_link_conf[link_info->fw_link_id], 266 NULL); 267 cmd.link_id = cpu_to_le32(link_info->fw_link_id); 268 iwl_mvm_release_fw_link_id(mvm, link_info->fw_link_id); 269 link_info->fw_link_id = IWL_MVM_FW_LINK_ID_INVALID; 270 cmd.spec_link_id = link_conf->link_id; 271 cmd.phy_id = cpu_to_le32(FW_CTXT_INVALID); 272 273 ret = iwl_mvm_link_cmd_send(mvm, &cmd, FW_CTXT_ACTION_REMOVE); 274 275 if (!ret) 276 if (iwl_mvm_sf_update(mvm, vif, true)) 277 IWL_ERR(mvm, "Failed to update SF state\n"); 278 279 return ret; 280 } 281 282 /* link should be deactivated before removal, so in most cases we need to 283 * perform these two operations together 284 */ 285 int iwl_mvm_disable_link(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 286 struct ieee80211_bss_conf *link_conf) 287 { 288 int ret; 289 290 ret = iwl_mvm_link_changed(mvm, vif, link_conf, 291 LINK_CONTEXT_MODIFY_ACTIVE, false); 292 if (ret) 293 return ret; 294 295 ret = iwl_mvm_remove_link(mvm, vif, link_conf); 296 if (ret) 297 return ret; 298 299 return ret; 300 } 301