1 /******************************************************************************
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
8 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
10 * Copyright(c) 2017 Intel Deutschland GmbH
11 * Copyright(c) 2018 Intel Corporation
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of version 2 of the GNU General Public License as
15 * published by the Free Software Foundation.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * The full GNU General Public License is included in this distribution
23 * in the file called COPYING.
25 * Contact Information:
26 * Intel Linux Wireless <linuxwifi@intel.com>
27 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
32 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
33 * Copyright(c) 2018 Intel Corporation
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 *****************************************************************************/
64 #include <net/mac80211.h>
68 /* Maps the driver specific channel width definition to the fw values */
69 u8 iwl_mvm_get_channel_width(struct cfg80211_chan_def *chandef)
71 switch (chandef->width) {
72 case NL80211_CHAN_WIDTH_20_NOHT:
73 case NL80211_CHAN_WIDTH_20:
74 return PHY_VHT_CHANNEL_MODE20;
75 case NL80211_CHAN_WIDTH_40:
76 return PHY_VHT_CHANNEL_MODE40;
77 case NL80211_CHAN_WIDTH_80:
78 return PHY_VHT_CHANNEL_MODE80;
79 case NL80211_CHAN_WIDTH_160:
80 return PHY_VHT_CHANNEL_MODE160;
82 WARN(1, "Invalid channel width=%u", chandef->width);
83 return PHY_VHT_CHANNEL_MODE20;
88 * Maps the driver specific control channel position (relative to the center
89 * freq) definitions to the the fw values
91 u8 iwl_mvm_get_ctrl_pos(struct cfg80211_chan_def *chandef)
93 switch (chandef->chan->center_freq - chandef->center_freq1) {
95 return PHY_VHT_CTRL_POS_4_BELOW;
97 return PHY_VHT_CTRL_POS_3_BELOW;
99 return PHY_VHT_CTRL_POS_2_BELOW;
101 return PHY_VHT_CTRL_POS_1_BELOW;
103 return PHY_VHT_CTRL_POS_1_ABOVE;
105 return PHY_VHT_CTRL_POS_2_ABOVE;
107 return PHY_VHT_CTRL_POS_3_ABOVE;
109 return PHY_VHT_CTRL_POS_4_ABOVE;
111 WARN(1, "Invalid channel definition");
114 * The FW is expected to check the control channel position only
115 * when in HT/VHT and the channel width is not 20MHz. Return
116 * this value as the default one.
118 return PHY_VHT_CTRL_POS_1_BELOW;
123 * Construct the generic fields of the PHY context command
125 static void iwl_mvm_phy_ctxt_cmd_hdr(struct iwl_mvm_phy_ctxt *ctxt,
126 struct iwl_phy_context_cmd *cmd,
127 u32 action, u32 apply_time)
129 memset(cmd, 0, sizeof(struct iwl_phy_context_cmd));
131 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(ctxt->id,
133 cmd->action = cpu_to_le32(action);
134 cmd->apply_time = cpu_to_le32(apply_time);
138 * Add the phy configuration to the PHY context command
140 static void iwl_mvm_phy_ctxt_cmd_data(struct iwl_mvm *mvm,
141 struct iwl_phy_context_cmd *cmd,
142 struct cfg80211_chan_def *chandef,
143 u8 chains_static, u8 chains_dynamic)
145 u8 active_cnt, idle_cnt;
146 struct iwl_phy_context_cmd_tail *tail =
147 iwl_mvm_chan_info_cmd_tail(mvm, &cmd->ci);
149 /* Set the channel info data */
150 iwl_mvm_set_chan_info_chandef(mvm, &cmd->ci, chandef);
152 /* Set rx the chains */
153 idle_cnt = chains_static;
154 active_cnt = chains_dynamic;
156 /* In scenarios where we only ever use a single-stream rates,
157 * i.e. legacy 11b/g/a associations, single-stream APs or even
158 * static SMPS, enable both chains to get diversity, improving
159 * the case where we're far enough from the AP that attenuation
160 * between the two antennas is sufficiently different to impact
163 if (active_cnt == 1 && iwl_mvm_rx_diversity_allowed(mvm)) {
168 tail->rxchain_info = cpu_to_le32(iwl_mvm_get_valid_rx_ant(mvm) <<
169 PHY_RX_CHAIN_VALID_POS);
170 tail->rxchain_info |= cpu_to_le32(idle_cnt << PHY_RX_CHAIN_CNT_POS);
171 tail->rxchain_info |= cpu_to_le32(active_cnt <<
172 PHY_RX_CHAIN_MIMO_CNT_POS);
173 #ifdef CONFIG_IWLWIFI_DEBUGFS
174 if (unlikely(mvm->dbgfs_rx_phyinfo))
175 tail->rxchain_info = cpu_to_le32(mvm->dbgfs_rx_phyinfo);
178 tail->txchain_info = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
182 * Send a command to apply the current phy configuration. The command is send
183 * only if something in the configuration changed: in case that this is the
184 * first time that the phy configuration is applied or in case that the phy
185 * configuration changed from the previous apply.
187 static int iwl_mvm_phy_ctxt_apply(struct iwl_mvm *mvm,
188 struct iwl_mvm_phy_ctxt *ctxt,
189 struct cfg80211_chan_def *chandef,
190 u8 chains_static, u8 chains_dynamic,
191 u32 action, u32 apply_time)
193 struct iwl_phy_context_cmd cmd;
195 u16 len = sizeof(cmd) - iwl_mvm_chan_info_padding(mvm);
197 /* Set the command header fields */
198 iwl_mvm_phy_ctxt_cmd_hdr(ctxt, &cmd, action, apply_time);
200 /* Set the command data */
201 iwl_mvm_phy_ctxt_cmd_data(mvm, &cmd, chandef,
202 chains_static, chains_dynamic);
204 ret = iwl_mvm_send_cmd_pdu(mvm, PHY_CONTEXT_CMD, 0, len, &cmd);
206 IWL_ERR(mvm, "PHY ctxt cmd error. ret=%d\n", ret);
211 * Send a command to add a PHY context based on the current HW configuration.
213 int iwl_mvm_phy_ctxt_add(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
214 struct cfg80211_chan_def *chandef,
215 u8 chains_static, u8 chains_dynamic)
217 WARN_ON(!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
219 lockdep_assert_held(&mvm->mutex);
221 ctxt->channel = chandef->chan;
223 return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
224 chains_static, chains_dynamic,
225 FW_CTXT_ACTION_ADD, 0);
229 * Update the number of references to the given PHY context. This is valid only
230 * in case the PHY context was already created, i.e., its reference count > 0.
232 void iwl_mvm_phy_ctxt_ref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
234 lockdep_assert_held(&mvm->mutex);
239 * Send a command to modify the PHY context based on the current HW
240 * configuration. Note that the function does not check that the configuration
243 int iwl_mvm_phy_ctxt_changed(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
244 struct cfg80211_chan_def *chandef,
245 u8 chains_static, u8 chains_dynamic)
247 enum iwl_ctxt_action action = FW_CTXT_ACTION_MODIFY;
249 lockdep_assert_held(&mvm->mutex);
251 if (fw_has_capa(&mvm->fw->ucode_capa,
252 IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT) &&
253 ctxt->channel->band != chandef->chan->band) {
256 /* ... remove it here ...*/
257 ret = iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
258 chains_static, chains_dynamic,
259 FW_CTXT_ACTION_REMOVE, 0);
263 /* ... and proceed to add it again */
264 action = FW_CTXT_ACTION_ADD;
267 ctxt->channel = chandef->chan;
268 ctxt->width = chandef->width;
269 return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
270 chains_static, chains_dynamic,
274 void iwl_mvm_phy_ctxt_unref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
276 lockdep_assert_held(&mvm->mutex);
278 if (WARN_ON_ONCE(!ctxt))
284 * Move unused phy's to a default channel. When the phy is moved the,
285 * fw will cleanup immediate quiet bit if it was previously set,
286 * otherwise we might not be able to reuse this phy.
288 if (ctxt->ref == 0) {
289 struct ieee80211_channel *chan;
290 struct cfg80211_chan_def chandef;
292 chan = &mvm->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[0];
293 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
294 iwl_mvm_phy_ctxt_changed(mvm, ctxt, &chandef, 1, 1);
298 static void iwl_mvm_binding_iterator(void *_data, u8 *mac,
299 struct ieee80211_vif *vif)
301 unsigned long *data = _data;
302 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
304 if (!mvmvif->phy_ctxt)
307 if (vif->type == NL80211_IFTYPE_STATION ||
308 vif->type == NL80211_IFTYPE_AP)
309 __set_bit(mvmvif->phy_ctxt->id, data);
312 int iwl_mvm_phy_ctx_count(struct iwl_mvm *mvm)
314 unsigned long phy_ctxt_counter = 0;
316 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
317 IEEE80211_IFACE_ITER_NORMAL,
318 iwl_mvm_binding_iterator,
321 return hweight8(phy_ctxt_counter);