1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* 3 * Copyright (C) 2012-2014, 2018-2023 Intel Corporation 4 * Copyright (C) 2013-2014 Intel Mobile Communications GmbH 5 * Copyright (C) 2017 Intel Deutschland GmbH 6 */ 7 #include <net/mac80211.h> 8 #include "fw-api.h" 9 #include "mvm.h" 10 11 /* Maps the driver specific channel width definition to the fw values */ 12 u8 iwl_mvm_get_channel_width(struct cfg80211_chan_def *chandef) 13 { 14 switch (chandef->width) { 15 case NL80211_CHAN_WIDTH_20_NOHT: 16 case NL80211_CHAN_WIDTH_20: 17 return IWL_PHY_CHANNEL_MODE20; 18 case NL80211_CHAN_WIDTH_40: 19 return IWL_PHY_CHANNEL_MODE40; 20 case NL80211_CHAN_WIDTH_80: 21 return IWL_PHY_CHANNEL_MODE80; 22 case NL80211_CHAN_WIDTH_160: 23 return IWL_PHY_CHANNEL_MODE160; 24 case NL80211_CHAN_WIDTH_320: 25 return IWL_PHY_CHANNEL_MODE320; 26 default: 27 WARN(1, "Invalid channel width=%u", chandef->width); 28 return IWL_PHY_CHANNEL_MODE20; 29 } 30 } 31 32 /* 33 * Maps the driver specific control channel position (relative to the center 34 * freq) definitions to the the fw values 35 */ 36 u8 iwl_mvm_get_ctrl_pos(struct cfg80211_chan_def *chandef) 37 { 38 int offs = chandef->chan->center_freq - chandef->center_freq1; 39 int abs_offs = abs(offs); 40 u8 ret; 41 42 if (offs == 0) { 43 /* 44 * The FW is expected to check the control channel position only 45 * when in HT/VHT and the channel width is not 20MHz. Return 46 * this value as the default one. 47 */ 48 return 0; 49 } 50 51 /* this results in a value 0-7, i.e. fitting into 0b0111 */ 52 ret = (abs_offs - 10) / 20; 53 /* 54 * But we need the value to be in 0b1011 because 0b0100 is 55 * IWL_PHY_CTRL_POS_ABOVE, so shift bit 2 up to land in 56 * IWL_PHY_CTRL_POS_OFFS_EXT (0b1000) 57 */ 58 ret = (ret & IWL_PHY_CTRL_POS_OFFS_MSK) | 59 ((ret & BIT(2)) << 1); 60 /* and add the above bit */ 61 ret |= (offs > 0) * IWL_PHY_CTRL_POS_ABOVE; 62 63 return ret; 64 } 65 66 /* 67 * Construct the generic fields of the PHY context command 68 */ 69 static void iwl_mvm_phy_ctxt_cmd_hdr(struct iwl_mvm_phy_ctxt *ctxt, 70 struct iwl_phy_context_cmd *cmd, 71 u32 action) 72 { 73 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(ctxt->id, 74 ctxt->color)); 75 cmd->action = cpu_to_le32(action); 76 } 77 78 static void iwl_mvm_phy_ctxt_set_rxchain(struct iwl_mvm *mvm, 79 struct iwl_mvm_phy_ctxt *ctxt, 80 __le32 *rxchain_info, 81 u8 chains_static, 82 u8 chains_dynamic) 83 { 84 u8 active_cnt, idle_cnt; 85 86 /* Set rx the chains */ 87 idle_cnt = chains_static; 88 active_cnt = chains_dynamic; 89 90 /* In scenarios where we only ever use a single-stream rates, 91 * i.e. legacy 11b/g/a associations, single-stream APs or even 92 * static SMPS, enable both chains to get diversity, improving 93 * the case where we're far enough from the AP that attenuation 94 * between the two antennas is sufficiently different to impact 95 * performance. 96 */ 97 if (active_cnt == 1 && iwl_mvm_rx_diversity_allowed(mvm, ctxt)) { 98 idle_cnt = 2; 99 active_cnt = 2; 100 } 101 102 /* 103 * If the firmware requested it, then we know that it supports 104 * getting zero for the values to indicate "use one, but pick 105 * which one yourself", which means it can dynamically pick one 106 * that e.g. has better RSSI. 107 */ 108 if (mvm->fw_static_smps_request && active_cnt == 1 && idle_cnt == 1) { 109 idle_cnt = 0; 110 active_cnt = 0; 111 } 112 113 *rxchain_info = cpu_to_le32(iwl_mvm_get_valid_rx_ant(mvm) << 114 PHY_RX_CHAIN_VALID_POS); 115 *rxchain_info |= cpu_to_le32(idle_cnt << PHY_RX_CHAIN_CNT_POS); 116 *rxchain_info |= cpu_to_le32(active_cnt << 117 PHY_RX_CHAIN_MIMO_CNT_POS); 118 #ifdef CONFIG_IWLWIFI_DEBUGFS 119 if (unlikely(mvm->dbgfs_rx_phyinfo)) 120 *rxchain_info = cpu_to_le32(mvm->dbgfs_rx_phyinfo); 121 #endif 122 } 123 124 /* 125 * Add the phy configuration to the PHY context command 126 */ 127 static void iwl_mvm_phy_ctxt_cmd_data_v1(struct iwl_mvm *mvm, 128 struct iwl_mvm_phy_ctxt *ctxt, 129 struct iwl_phy_context_cmd_v1 *cmd, 130 struct cfg80211_chan_def *chandef, 131 u8 chains_static, u8 chains_dynamic) 132 { 133 struct iwl_phy_context_cmd_tail *tail = 134 iwl_mvm_chan_info_cmd_tail(mvm, &cmd->ci); 135 136 /* Set the channel info data */ 137 iwl_mvm_set_chan_info_chandef(mvm, &cmd->ci, chandef); 138 139 iwl_mvm_phy_ctxt_set_rxchain(mvm, ctxt, &tail->rxchain_info, 140 chains_static, chains_dynamic); 141 142 tail->txchain_info = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm)); 143 } 144 145 /* 146 * Add the phy configuration to the PHY context command 147 */ 148 static void iwl_mvm_phy_ctxt_cmd_data(struct iwl_mvm *mvm, 149 struct iwl_mvm_phy_ctxt *ctxt, 150 struct iwl_phy_context_cmd *cmd, 151 struct cfg80211_chan_def *chandef, 152 u8 chains_static, u8 chains_dynamic) 153 { 154 cmd->lmac_id = cpu_to_le32(iwl_mvm_get_lmac_id(mvm, 155 chandef->chan->band)); 156 157 /* Set the channel info data */ 158 iwl_mvm_set_chan_info_chandef(mvm, &cmd->ci, chandef); 159 160 /* we only support RLC command version 2 */ 161 if (iwl_fw_lookup_cmd_ver(mvm->fw, WIDE_ID(DATA_PATH_GROUP, RLC_CONFIG_CMD), 0) < 2) 162 iwl_mvm_phy_ctxt_set_rxchain(mvm, ctxt, &cmd->rxchain_info, 163 chains_static, chains_dynamic); 164 } 165 166 int iwl_mvm_phy_send_rlc(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt, 167 u8 chains_static, u8 chains_dynamic) 168 { 169 struct iwl_rlc_config_cmd cmd = { 170 .phy_id = cpu_to_le32(ctxt->id), 171 }; 172 173 if (ctxt->rlc_disabled) 174 return 0; 175 176 if (iwl_fw_lookup_cmd_ver(mvm->fw, WIDE_ID(DATA_PATH_GROUP, 177 RLC_CONFIG_CMD), 0) < 2) 178 return 0; 179 180 BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_DRIVER_FORCE != 181 PHY_RX_CHAIN_DRIVER_FORCE_MSK); 182 BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_VALID != 183 PHY_RX_CHAIN_VALID_MSK); 184 BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_FORCE != 185 PHY_RX_CHAIN_FORCE_SEL_MSK); 186 BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_FORCE_MIMO != 187 PHY_RX_CHAIN_FORCE_MIMO_SEL_MSK); 188 BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_COUNT != PHY_RX_CHAIN_CNT_MSK); 189 BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_MIMO_COUNT != 190 PHY_RX_CHAIN_MIMO_CNT_MSK); 191 192 iwl_mvm_phy_ctxt_set_rxchain(mvm, ctxt, &cmd.rlc.rx_chain_info, 193 chains_static, chains_dynamic); 194 195 IWL_DEBUG_FW(mvm, "Send RLC command: phy=%d, rx_chain_info=0x%x\n", 196 ctxt->id, cmd.rlc.rx_chain_info); 197 198 return iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(RLC_CONFIG_CMD, 199 DATA_PATH_GROUP, 2), 200 0, sizeof(cmd), &cmd); 201 } 202 203 /* 204 * Send a command to apply the current phy configuration. The command is send 205 * only if something in the configuration changed: in case that this is the 206 * first time that the phy configuration is applied or in case that the phy 207 * configuration changed from the previous apply. 208 */ 209 static int iwl_mvm_phy_ctxt_apply(struct iwl_mvm *mvm, 210 struct iwl_mvm_phy_ctxt *ctxt, 211 struct cfg80211_chan_def *chandef, 212 u8 chains_static, u8 chains_dynamic, 213 u32 action) 214 { 215 int ret; 216 int ver = iwl_fw_lookup_cmd_ver(mvm->fw, PHY_CONTEXT_CMD, 1); 217 218 if (ver == 3 || ver == 4) { 219 struct iwl_phy_context_cmd cmd = {}; 220 221 /* Set the command header fields */ 222 iwl_mvm_phy_ctxt_cmd_hdr(ctxt, &cmd, action); 223 224 /* Set the command data */ 225 iwl_mvm_phy_ctxt_cmd_data(mvm, ctxt, &cmd, chandef, 226 chains_static, 227 chains_dynamic); 228 229 ret = iwl_mvm_send_cmd_pdu(mvm, PHY_CONTEXT_CMD, 230 0, sizeof(cmd), &cmd); 231 } else if (ver < 3) { 232 struct iwl_phy_context_cmd_v1 cmd = {}; 233 u16 len = sizeof(cmd) - iwl_mvm_chan_info_padding(mvm); 234 235 /* Set the command header fields */ 236 iwl_mvm_phy_ctxt_cmd_hdr(ctxt, 237 (struct iwl_phy_context_cmd *)&cmd, 238 action); 239 240 /* Set the command data */ 241 iwl_mvm_phy_ctxt_cmd_data_v1(mvm, ctxt, &cmd, chandef, 242 chains_static, 243 chains_dynamic); 244 ret = iwl_mvm_send_cmd_pdu(mvm, PHY_CONTEXT_CMD, 245 0, len, &cmd); 246 } else { 247 IWL_ERR(mvm, "PHY ctxt cmd error ver %d not supported\n", ver); 248 return -EOPNOTSUPP; 249 } 250 251 252 if (ret) { 253 IWL_ERR(mvm, "PHY ctxt cmd error. ret=%d\n", ret); 254 return ret; 255 } 256 257 if (action != FW_CTXT_ACTION_REMOVE) 258 return iwl_mvm_phy_send_rlc(mvm, ctxt, chains_static, 259 chains_dynamic); 260 261 return 0; 262 } 263 264 /* 265 * Send a command to add a PHY context based on the current HW configuration. 266 */ 267 int iwl_mvm_phy_ctxt_add(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt, 268 struct cfg80211_chan_def *chandef, 269 u8 chains_static, u8 chains_dynamic) 270 { 271 int ret; 272 273 WARN_ON(!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) && 274 ctxt->ref); 275 lockdep_assert_held(&mvm->mutex); 276 277 ctxt->channel = chandef->chan; 278 ctxt->width = chandef->width; 279 ctxt->center_freq1 = chandef->center_freq1; 280 281 ret = iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef, 282 chains_static, chains_dynamic, 283 FW_CTXT_ACTION_ADD); 284 285 if (ret) 286 return ret; 287 288 ctxt->ref++; 289 290 return 0; 291 } 292 293 /* 294 * Update the number of references to the given PHY context. This is valid only 295 * in case the PHY context was already created, i.e., its reference count > 0. 296 */ 297 void iwl_mvm_phy_ctxt_ref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt) 298 { 299 lockdep_assert_held(&mvm->mutex); 300 301 /* If we were taking the first ref, we should have 302 * called iwl_mvm_phy_ctxt_add. 303 */ 304 WARN_ON(!ctxt->ref); 305 ctxt->ref++; 306 } 307 308 /* 309 * Send a command to modify the PHY context based on the current HW 310 * configuration. Note that the function does not check that the configuration 311 * changed. 312 */ 313 int iwl_mvm_phy_ctxt_changed(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt, 314 struct cfg80211_chan_def *chandef, 315 u8 chains_static, u8 chains_dynamic) 316 { 317 enum iwl_ctxt_action action = FW_CTXT_ACTION_MODIFY; 318 319 lockdep_assert_held(&mvm->mutex); 320 321 if (WARN_ON_ONCE(!ctxt->ref)) 322 return -EINVAL; 323 324 if (iwl_fw_lookup_cmd_ver(mvm->fw, WIDE_ID(DATA_PATH_GROUP, 325 RLC_CONFIG_CMD), 0) >= 2 && 326 ctxt->channel == chandef->chan && 327 ctxt->width == chandef->width && 328 ctxt->center_freq1 == chandef->center_freq1) 329 return iwl_mvm_phy_send_rlc(mvm, ctxt, chains_static, 330 chains_dynamic); 331 332 if (fw_has_capa(&mvm->fw->ucode_capa, 333 IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT) && 334 ctxt->channel->band != chandef->chan->band) { 335 int ret; 336 337 /* ... remove it here ...*/ 338 ret = iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef, 339 chains_static, chains_dynamic, 340 FW_CTXT_ACTION_REMOVE); 341 if (ret) 342 return ret; 343 344 /* ... and proceed to add it again */ 345 action = FW_CTXT_ACTION_ADD; 346 } 347 348 ctxt->channel = chandef->chan; 349 ctxt->width = chandef->width; 350 ctxt->center_freq1 = chandef->center_freq1; 351 352 return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef, 353 chains_static, chains_dynamic, 354 action); 355 } 356 357 void iwl_mvm_phy_ctxt_unref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt) 358 { 359 struct cfg80211_chan_def chandef; 360 lockdep_assert_held(&mvm->mutex); 361 362 if (WARN_ON_ONCE(!ctxt)) 363 return; 364 365 ctxt->ref--; 366 367 if (ctxt->ref) 368 return; 369 370 cfg80211_chandef_create(&chandef, ctxt->channel, NL80211_CHAN_NO_HT); 371 372 iwl_mvm_phy_ctxt_apply(mvm, ctxt, &chandef, 1, 1, 373 FW_CTXT_ACTION_REMOVE); 374 } 375 376 static void iwl_mvm_binding_iterator(void *_data, u8 *mac, 377 struct ieee80211_vif *vif) 378 { 379 unsigned long *data = _data; 380 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 381 382 if (!mvmvif->deflink.phy_ctxt) 383 return; 384 385 if (vif->type == NL80211_IFTYPE_STATION || 386 vif->type == NL80211_IFTYPE_AP) 387 __set_bit(mvmvif->deflink.phy_ctxt->id, data); 388 } 389 390 int iwl_mvm_phy_ctx_count(struct iwl_mvm *mvm) 391 { 392 unsigned long phy_ctxt_counter = 0; 393 394 ieee80211_iterate_active_interfaces_atomic(mvm->hw, 395 IEEE80211_IFACE_ITER_NORMAL, 396 iwl_mvm_binding_iterator, 397 &phy_ctxt_counter); 398 399 return hweight8(phy_ctxt_counter); 400 } 401