1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright (C) 2012-2014, 2018-2024 Intel Corporation
4 * Copyright (C) 2013-2014 Intel Mobile Communications GmbH
5 * Copyright (C) 2017 Intel Deutschland GmbH
7 #include <net/mac80211.h>
11 /* Maps the driver specific channel width definition to the fw values */
12 u8 iwl_mvm_get_channel_width(const struct cfg80211_chan_def *chandef)
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;
27 WARN(1, "Invalid channel width=%u", chandef->width);
28 return IWL_PHY_CHANNEL_MODE20;
33 * Maps the driver specific control channel position (relative to the center
34 * freq) definitions to the the fw values
36 u8 iwl_mvm_get_ctrl_pos(const struct cfg80211_chan_def *chandef)
38 int offs = chandef->chan->center_freq - chandef->center_freq1;
39 int abs_offs = abs(offs);
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.
51 /* this results in a value 0-7, i.e. fitting into 0b0111 */
52 ret = (abs_offs - 10) / 20;
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)
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;
67 * Construct the generic fields of the PHY context command
69 static void iwl_mvm_phy_ctxt_cmd_hdr(struct iwl_mvm_phy_ctxt *ctxt,
70 struct iwl_phy_context_cmd *cmd,
73 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(ctxt->id,
75 cmd->action = cpu_to_le32(action);
78 static void iwl_mvm_phy_ctxt_set_rxchain(struct iwl_mvm *mvm,
79 struct iwl_mvm_phy_ctxt *ctxt,
84 u8 active_cnt, idle_cnt;
86 /* Set rx the chains */
87 idle_cnt = chains_static;
88 active_cnt = chains_dynamic;
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
97 if (active_cnt == 1 && iwl_mvm_rx_diversity_allowed(mvm, ctxt)) {
102 *rxchain_info = cpu_to_le32(iwl_mvm_get_valid_rx_ant(mvm) <<
103 PHY_RX_CHAIN_VALID_POS);
104 *rxchain_info |= cpu_to_le32(idle_cnt << PHY_RX_CHAIN_CNT_POS);
105 *rxchain_info |= cpu_to_le32(active_cnt <<
106 PHY_RX_CHAIN_MIMO_CNT_POS);
107 #ifdef CONFIG_IWLWIFI_DEBUGFS
108 if (unlikely(mvm->dbgfs_rx_phyinfo))
109 *rxchain_info = cpu_to_le32(mvm->dbgfs_rx_phyinfo);
114 * Add the phy configuration to the PHY context command
116 static void iwl_mvm_phy_ctxt_cmd_data_v1(struct iwl_mvm *mvm,
117 struct iwl_mvm_phy_ctxt *ctxt,
118 struct iwl_phy_context_cmd_v1 *cmd,
119 const struct cfg80211_chan_def *chandef,
120 u8 chains_static, u8 chains_dynamic)
122 struct iwl_phy_context_cmd_tail *tail =
123 iwl_mvm_chan_info_cmd_tail(mvm, &cmd->ci);
125 /* Set the channel info data */
126 iwl_mvm_set_chan_info_chandef(mvm, &cmd->ci, chandef);
128 iwl_mvm_phy_ctxt_set_rxchain(mvm, ctxt, &tail->rxchain_info,
129 chains_static, chains_dynamic);
131 tail->txchain_info = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
135 * Add the phy configuration to the PHY context command
137 static void iwl_mvm_phy_ctxt_cmd_data(struct iwl_mvm *mvm,
138 struct iwl_mvm_phy_ctxt *ctxt,
139 struct iwl_phy_context_cmd *cmd,
140 const struct cfg80211_chan_def *chandef,
141 u8 chains_static, u8 chains_dynamic)
143 cmd->lmac_id = cpu_to_le32(iwl_mvm_get_lmac_id(mvm,
144 chandef->chan->band));
146 /* Set the channel info data */
147 iwl_mvm_set_chan_info_chandef(mvm, &cmd->ci, chandef);
149 /* we only support RLC command version 2 */
150 if (iwl_fw_lookup_cmd_ver(mvm->fw, WIDE_ID(DATA_PATH_GROUP, RLC_CONFIG_CMD), 0) < 2)
151 iwl_mvm_phy_ctxt_set_rxchain(mvm, ctxt, &cmd->rxchain_info,
152 chains_static, chains_dynamic);
155 int iwl_mvm_phy_send_rlc(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
156 u8 chains_static, u8 chains_dynamic)
158 struct iwl_rlc_config_cmd cmd = {
159 .phy_id = cpu_to_le32(ctxt->id),
162 if (ctxt->rlc_disabled)
165 if (iwl_fw_lookup_cmd_ver(mvm->fw, WIDE_ID(DATA_PATH_GROUP,
166 RLC_CONFIG_CMD), 0) < 2)
169 BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_DRIVER_FORCE !=
170 PHY_RX_CHAIN_DRIVER_FORCE_MSK);
171 BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_VALID !=
172 PHY_RX_CHAIN_VALID_MSK);
173 BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_FORCE !=
174 PHY_RX_CHAIN_FORCE_SEL_MSK);
175 BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_FORCE_MIMO !=
176 PHY_RX_CHAIN_FORCE_MIMO_SEL_MSK);
177 BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_COUNT != PHY_RX_CHAIN_CNT_MSK);
178 BUILD_BUG_ON(IWL_RLC_CHAIN_INFO_MIMO_COUNT !=
179 PHY_RX_CHAIN_MIMO_CNT_MSK);
181 iwl_mvm_phy_ctxt_set_rxchain(mvm, ctxt, &cmd.rlc.rx_chain_info,
182 chains_static, chains_dynamic);
184 IWL_DEBUG_FW(mvm, "Send RLC command: phy=%d, rx_chain_info=0x%x\n",
185 ctxt->id, cmd.rlc.rx_chain_info);
187 return iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(RLC_CONFIG_CMD,
189 0, sizeof(cmd), &cmd);
193 * Send a command to apply the current phy configuration. The command is send
194 * only if something in the configuration changed: in case that this is the
195 * first time that the phy configuration is applied or in case that the phy
196 * configuration changed from the previous apply.
198 static int iwl_mvm_phy_ctxt_apply(struct iwl_mvm *mvm,
199 struct iwl_mvm_phy_ctxt *ctxt,
200 const struct cfg80211_chan_def *chandef,
201 const struct cfg80211_chan_def *ap,
202 u8 chains_static, u8 chains_dynamic,
206 int ver = iwl_fw_lookup_cmd_ver(mvm->fw, PHY_CONTEXT_CMD, 1);
208 if (ver < 5 || !ap || !ap->chan)
211 if (ver >= 3 && ver <= 6) {
212 struct iwl_phy_context_cmd cmd = {};
214 /* Set the command header fields */
215 iwl_mvm_phy_ctxt_cmd_hdr(ctxt, &cmd, action);
217 /* Set the command data */
218 iwl_mvm_phy_ctxt_cmd_data(mvm, ctxt, &cmd, chandef,
223 cmd.sbb_bandwidth = iwl_mvm_get_channel_width(ap);
224 cmd.sbb_ctrl_channel_loc = iwl_mvm_get_ctrl_pos(ap);
228 cmd.puncture_mask = cpu_to_le16(chandef->punctured);
230 ret = iwl_mvm_send_cmd_pdu(mvm, PHY_CONTEXT_CMD,
231 0, sizeof(cmd), &cmd);
232 } else if (ver < 3) {
233 struct iwl_phy_context_cmd_v1 cmd = {};
234 u16 len = sizeof(cmd) - iwl_mvm_chan_info_padding(mvm);
236 /* Set the command header fields */
237 iwl_mvm_phy_ctxt_cmd_hdr(ctxt,
238 (struct iwl_phy_context_cmd *)&cmd,
241 /* Set the command data */
242 iwl_mvm_phy_ctxt_cmd_data_v1(mvm, ctxt, &cmd, chandef,
245 ret = iwl_mvm_send_cmd_pdu(mvm, PHY_CONTEXT_CMD,
248 IWL_ERR(mvm, "PHY ctxt cmd error ver %d not supported\n", ver);
254 IWL_ERR(mvm, "PHY ctxt cmd error. ret=%d\n", ret);
258 if (action != FW_CTXT_ACTION_REMOVE)
259 return iwl_mvm_phy_send_rlc(mvm, ctxt, chains_static,
266 * Send a command to add a PHY context based on the current HW configuration.
268 int iwl_mvm_phy_ctxt_add(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
269 const struct cfg80211_chan_def *chandef,
270 const struct cfg80211_chan_def *ap,
271 u8 chains_static, u8 chains_dynamic)
275 WARN_ON(!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
277 lockdep_assert_held(&mvm->mutex);
279 ctxt->channel = chandef->chan;
280 ctxt->width = chandef->width;
281 ctxt->center_freq1 = chandef->center_freq1;
283 ret = iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef, ap,
284 chains_static, chains_dynamic,
296 * Update the number of references to the given PHY context. This is valid only
297 * in case the PHY context was already created, i.e., its reference count > 0.
299 void iwl_mvm_phy_ctxt_ref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
301 lockdep_assert_held(&mvm->mutex);
303 /* If we were taking the first ref, we should have
304 * called iwl_mvm_phy_ctxt_add.
311 * Send a command to modify the PHY context based on the current HW
312 * configuration. Note that the function does not check that the configuration
315 int iwl_mvm_phy_ctxt_changed(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
316 const struct cfg80211_chan_def *chandef,
317 const struct cfg80211_chan_def *ap,
318 u8 chains_static, u8 chains_dynamic)
320 enum iwl_ctxt_action action = FW_CTXT_ACTION_MODIFY;
322 lockdep_assert_held(&mvm->mutex);
324 if (WARN_ON_ONCE(!ctxt->ref))
327 if (iwl_fw_lookup_cmd_ver(mvm->fw, WIDE_ID(DATA_PATH_GROUP,
328 RLC_CONFIG_CMD), 0) >= 2 &&
329 ctxt->channel == chandef->chan &&
330 ctxt->width == chandef->width &&
331 ctxt->center_freq1 == chandef->center_freq1)
332 return iwl_mvm_phy_send_rlc(mvm, ctxt, chains_static,
335 if (fw_has_capa(&mvm->fw->ucode_capa,
336 IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT) &&
337 ctxt->channel->band != chandef->chan->band) {
340 /* ... remove it here ...*/
341 ret = iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef, NULL,
342 chains_static, chains_dynamic,
343 FW_CTXT_ACTION_REMOVE);
347 /* ... and proceed to add it again */
348 action = FW_CTXT_ACTION_ADD;
351 ctxt->channel = chandef->chan;
352 ctxt->width = chandef->width;
353 ctxt->center_freq1 = chandef->center_freq1;
355 return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef, ap,
356 chains_static, chains_dynamic,
360 void iwl_mvm_phy_ctxt_unref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
362 struct cfg80211_chan_def chandef;
363 lockdep_assert_held(&mvm->mutex);
365 if (WARN_ON_ONCE(!ctxt))
373 cfg80211_chandef_create(&chandef, ctxt->channel, NL80211_CHAN_NO_HT);
375 iwl_mvm_phy_ctxt_apply(mvm, ctxt, &chandef, NULL, 1, 1,
376 FW_CTXT_ACTION_REMOVE);
379 static void iwl_mvm_binding_iterator(void *_data, u8 *mac,
380 struct ieee80211_vif *vif)
382 unsigned long *data = _data;
383 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
385 if (!mvmvif->deflink.phy_ctxt)
388 if (vif->type == NL80211_IFTYPE_STATION ||
389 vif->type == NL80211_IFTYPE_AP)
390 __set_bit(mvmvif->deflink.phy_ctxt->id, data);
393 int iwl_mvm_phy_ctx_count(struct iwl_mvm *mvm)
395 unsigned long phy_ctxt_counter = 0;
397 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
398 IEEE80211_IFACE_ITER_NORMAL,
399 iwl_mvm_binding_iterator,
402 return hweight8(phy_ctxt_counter);