blob: 7f5434b34d0d737e87b5511070b4cdc4cb96315e [file] [log] [blame]
Johannes Berg8ca151b2013-01-24 14:25:36 +01001/******************************************************************************
2 *
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.
5 *
6 * GPL LICENSE SUMMARY
7 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +02008 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +02009 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
Sara Sharon730a1892017-02-07 18:37:40 +020010 * Copyright(c) 2017 Intel Deutschland GmbH
Andrei Otcheretianski9f4ef1d2018-03-04 20:06:29 +020011 * Copyright(c) 2018 Intel Corporation
Johannes Berg8ca151b2013-01-24 14:25:36 +010012 *
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.
16 *
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.
21 *
Johannes Berg8ca151b2013-01-24 14:25:36 +010022 * The full GNU General Public License is included in this distribution
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020023 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010024 *
25 * Contact Information:
Emmanuel Grumbachcb2f8272015-11-17 15:39:56 +020026 * Intel Linux Wireless <linuxwifi@intel.com>
Johannes Berg8ca151b2013-01-24 14:25:36 +010027 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28 *
29 * BSD LICENSE
30 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020031 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +020032 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
Andrei Otcheretianski9f4ef1d2018-03-04 20:06:29 +020033 * Copyright(c) 2018 Intel Corporation
Johannes Berg8ca151b2013-01-24 14:25:36 +010034 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
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
45 * distribution.
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.
49 *
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.
61 *
62 *****************************************************************************/
63
64#include <net/mac80211.h>
65#include "fw-api.h"
66#include "mvm.h"
67
Sara Sharon0d365ae2015-03-31 12:24:05 +030068/* Maps the driver specific channel width definition to the fw values */
Arik Nemtsov6ce73e62014-09-11 13:00:19 +030069u8 iwl_mvm_get_channel_width(struct cfg80211_chan_def *chandef)
Johannes Berg8ca151b2013-01-24 14:25:36 +010070{
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;
81 default:
82 WARN(1, "Invalid channel width=%u", chandef->width);
83 return PHY_VHT_CHANNEL_MODE20;
84 }
85}
86
87/*
88 * Maps the driver specific control channel position (relative to the center
89 * freq) definitions to the the fw values
90 */
Arik Nemtsov6ce73e62014-09-11 13:00:19 +030091u8 iwl_mvm_get_ctrl_pos(struct cfg80211_chan_def *chandef)
Johannes Berg8ca151b2013-01-24 14:25:36 +010092{
93 switch (chandef->chan->center_freq - chandef->center_freq1) {
94 case -70:
95 return PHY_VHT_CTRL_POS_4_BELOW;
96 case -50:
97 return PHY_VHT_CTRL_POS_3_BELOW;
98 case -30:
99 return PHY_VHT_CTRL_POS_2_BELOW;
100 case -10:
101 return PHY_VHT_CTRL_POS_1_BELOW;
102 case 10:
103 return PHY_VHT_CTRL_POS_1_ABOVE;
104 case 30:
105 return PHY_VHT_CTRL_POS_2_ABOVE;
106 case 50:
107 return PHY_VHT_CTRL_POS_3_ABOVE;
108 case 70:
109 return PHY_VHT_CTRL_POS_4_ABOVE;
110 default:
111 WARN(1, "Invalid channel definition");
112 case 0:
113 /*
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.
117 */
118 return PHY_VHT_CTRL_POS_1_BELOW;
119 }
120}
121
122/*
123 * Construct the generic fields of the PHY context command
124 */
125static 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)
128{
129 memset(cmd, 0, sizeof(struct iwl_phy_context_cmd));
130
131 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(ctxt->id,
132 ctxt->color));
133 cmd->action = cpu_to_le32(action);
134 cmd->apply_time = cpu_to_le32(apply_time);
135}
136
137/*
138 * Add the phy configuration to the PHY context command
139 */
140static 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)
144{
Emmanuel Grumbach33223542013-03-09 20:38:19 +0200145 u8 active_cnt, idle_cnt;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100146
147 /* Set the channel info data */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200148 cmd->ci.band = (chandef->chan->band == NL80211_BAND_2GHZ ?
Johannes Berg8ca151b2013-01-24 14:25:36 +0100149 PHY_BAND_24 : PHY_BAND_5);
150
151 cmd->ci.channel = chandef->chan->hw_value;
152 cmd->ci.width = iwl_mvm_get_channel_width(chandef);
153 cmd->ci.ctrl_pos = iwl_mvm_get_ctrl_pos(chandef);
154
155 /* Set rx the chains */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100156 idle_cnt = chains_static;
157 active_cnt = chains_dynamic;
158
Johannes Berg1a095d32014-05-12 11:14:51 +0200159 /* In scenarios where we only ever use a single-stream rates,
160 * i.e. legacy 11b/g/a associations, single-stream APs or even
161 * static SMPS, enable both chains to get diversity, improving
162 * the case where we're far enough from the AP that attenuation
163 * between the two antennas is sufficiently different to impact
164 * performance.
165 */
Emmanuel Grumbach5c904222014-05-18 09:16:45 +0300166 if (active_cnt == 1 && iwl_mvm_rx_diversity_allowed(mvm)) {
Johannes Berg1a095d32014-05-12 11:14:51 +0200167 idle_cnt = 2;
168 active_cnt = 2;
169 }
170
Liad Kaufman0b837952015-01-22 19:01:35 +0200171 cmd->rxchain_info = cpu_to_le32(iwl_mvm_get_valid_rx_ant(mvm) <<
Johannes Berg8ca151b2013-01-24 14:25:36 +0100172 PHY_RX_CHAIN_VALID_POS);
173 cmd->rxchain_info |= cpu_to_le32(idle_cnt << PHY_RX_CHAIN_CNT_POS);
174 cmd->rxchain_info |= cpu_to_le32(active_cnt <<
175 PHY_RX_CHAIN_MIMO_CNT_POS);
Emmanuel Grumbachaa5e1832015-03-07 19:35:37 +0200176#ifdef CONFIG_IWLWIFI_DEBUGFS
Emmanuel Grumbachddf89ab2015-02-08 10:56:43 +0200177 if (unlikely(mvm->dbgfs_rx_phyinfo))
178 cmd->rxchain_info = cpu_to_le32(mvm->dbgfs_rx_phyinfo);
Emmanuel Grumbachaa5e1832015-03-07 19:35:37 +0200179#endif
Johannes Berg8ca151b2013-01-24 14:25:36 +0100180
Moshe Harela0544272014-12-08 21:13:14 +0200181 cmd->txchain_info = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100182}
183
184/*
185 * Send a command to apply the current phy configuration. The command is send
186 * only if something in the configuration changed: in case that this is the
187 * first time that the phy configuration is applied or in case that the phy
188 * configuration changed from the previous apply.
189 */
190static int iwl_mvm_phy_ctxt_apply(struct iwl_mvm *mvm,
191 struct iwl_mvm_phy_ctxt *ctxt,
192 struct cfg80211_chan_def *chandef,
193 u8 chains_static, u8 chains_dynamic,
194 u32 action, u32 apply_time)
195{
196 struct iwl_phy_context_cmd cmd;
197 int ret;
198
199 /* Set the command header fields */
200 iwl_mvm_phy_ctxt_cmd_hdr(ctxt, &cmd, action, apply_time);
201
202 /* Set the command data */
203 iwl_mvm_phy_ctxt_cmd_data(mvm, &cmd, chandef,
204 chains_static, chains_dynamic);
205
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300206 ret = iwl_mvm_send_cmd_pdu(mvm, PHY_CONTEXT_CMD, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100207 sizeof(struct iwl_phy_context_cmd),
208 &cmd);
209 if (ret)
210 IWL_ERR(mvm, "PHY ctxt cmd error. ret=%d\n", ret);
211 return ret;
212}
213
Johannes Berg8ca151b2013-01-24 14:25:36 +0100214/*
215 * Send a command to add a PHY context based on the current HW configuration.
216 */
217int iwl_mvm_phy_ctxt_add(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
218 struct cfg80211_chan_def *chandef,
219 u8 chains_static, u8 chains_dynamic)
220{
Johannes Bergdebff612013-05-14 13:53:45 +0200221 WARN_ON(!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
222 ctxt->ref);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100223 lockdep_assert_held(&mvm->mutex);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100224
225 ctxt->channel = chandef->chan;
Ilan Peerfe0f2de2013-03-21 10:23:52 +0200226
Johannes Berg7a531742014-04-30 14:28:40 +0200227 return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
228 chains_static, chains_dynamic,
229 FW_CTXT_ACTION_ADD, 0);
Ilan Peerfe0f2de2013-03-21 10:23:52 +0200230}
231
232/*
233 * Update the number of references to the given PHY context. This is valid only
234 * in case the PHY context was already created, i.e., its reference count > 0.
235 */
236void iwl_mvm_phy_ctxt_ref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
237{
Ilan Peerfe0f2de2013-03-21 10:23:52 +0200238 lockdep_assert_held(&mvm->mutex);
Ilan Peerfe0f2de2013-03-21 10:23:52 +0200239 ctxt->ref++;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100240}
241
242/*
243 * Send a command to modify the PHY context based on the current HW
244 * configuration. Note that the function does not check that the configuration
245 * changed.
246 */
247int iwl_mvm_phy_ctxt_changed(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
248 struct cfg80211_chan_def *chandef,
249 u8 chains_static, u8 chains_dynamic)
250{
Johannes Bergd172a5e2017-06-02 15:15:53 +0200251 enum iwl_ctxt_action action = FW_CTXT_ACTION_MODIFY;
Sara Sharon730a1892017-02-07 18:37:40 +0200252
Johannes Berg8ca151b2013-01-24 14:25:36 +0100253 lockdep_assert_held(&mvm->mutex);
254
Johannes Berg91109f42017-05-26 13:11:44 +0200255 if (fw_has_capa(&mvm->fw->ucode_capa,
256 IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT) &&
Sara Sharon730a1892017-02-07 18:37:40 +0200257 ctxt->channel->band != chandef->chan->band) {
258 int ret;
259
260 /* ... remove it here ...*/
261 ret = iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
262 chains_static, chains_dynamic,
263 FW_CTXT_ACTION_REMOVE, 0);
264 if (ret)
265 return ret;
266
267 /* ... and proceed to add it again */
268 action = FW_CTXT_ACTION_ADD;
269 }
270
Johannes Berg8ca151b2013-01-24 14:25:36 +0100271 ctxt->channel = chandef->chan;
Emmanuel Grumbach7a20bcc2017-09-14 15:45:44 +0300272 ctxt->width = chandef->width;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100273 return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
274 chains_static, chains_dynamic,
Sara Sharon730a1892017-02-07 18:37:40 +0200275 action, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100276}
277
Ilan Peerfe0f2de2013-03-21 10:23:52 +0200278void iwl_mvm_phy_ctxt_unref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
279{
280 lockdep_assert_held(&mvm->mutex);
Johannes Berge2821012013-05-08 15:55:31 +0200281
282 if (WARN_ON_ONCE(!ctxt))
283 return;
284
Ilan Peerfe0f2de2013-03-21 10:23:52 +0200285 ctxt->ref--;
Andrei Otcheretianski9f4ef1d2018-03-04 20:06:29 +0200286
287 /*
288 * Move unused phy's to a default channel. When the phy is moved the,
289 * fw will cleanup immediate quiet bit if it was previously set,
290 * otherwise we might not be able to reuse this phy.
291 */
292 if (ctxt->ref == 0) {
293 struct ieee80211_channel *chan;
294 struct cfg80211_chan_def chandef;
295
296 chan = &mvm->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels[0];
297 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
298 iwl_mvm_phy_ctxt_changed(mvm, ctxt, &chandef, 1, 1);
299 }
Ilan Peerfe0f2de2013-03-21 10:23:52 +0200300}
Arik Nemtsovcf7b4912014-05-15 11:44:40 +0300301
302static void iwl_mvm_binding_iterator(void *_data, u8 *mac,
303 struct ieee80211_vif *vif)
304{
305 unsigned long *data = _data;
306 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
307
308 if (!mvmvif->phy_ctxt)
309 return;
310
311 if (vif->type == NL80211_IFTYPE_STATION ||
312 vif->type == NL80211_IFTYPE_AP)
313 __set_bit(mvmvif->phy_ctxt->id, data);
314}
315
316int iwl_mvm_phy_ctx_count(struct iwl_mvm *mvm)
317{
318 unsigned long phy_ctxt_counter = 0;
319
320 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
321 IEEE80211_IFACE_ITER_NORMAL,
322 iwl_mvm_binding_iterator,
323 &phy_ctxt_counter);
324
325 return hweight8(phy_ctxt_counter);
326}