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 - 2013 Intel Corporation. All rights reserved.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24 * The full GNU General Public License is included in this distribution
25 * in the file called COPYING.
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
33 * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
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 the fw values */
69 static inline 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 static inline 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 valid_rx_chains, active_cnt, idle_cnt;
147 /* Set the channel info data */
148 cmd->ci.band = (chandef->chan->band == IEEE80211_BAND_2GHZ ?
149 PHY_BAND_24 : PHY_BAND_5);
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);
155 /* Set rx the chains */
158 * Need to add on chain noise calibration limitations, and
159 * BT coex considerations.
161 valid_rx_chains = mvm->nvm_data->valid_rx_ant;
162 idle_cnt = chains_static;
163 active_cnt = chains_dynamic;
165 cmd->rxchain_info = cpu_to_le32(valid_rx_chains <<
166 PHY_RX_CHAIN_VALID_POS);
167 cmd->rxchain_info |= cpu_to_le32(idle_cnt << PHY_RX_CHAIN_CNT_POS);
168 cmd->rxchain_info |= cpu_to_le32(active_cnt <<
169 PHY_RX_CHAIN_MIMO_CNT_POS);
171 cmd->txchain_info = cpu_to_le32(mvm->nvm_data->valid_tx_ant);
175 * Send a command to apply the current phy configuration. The command is send
176 * only if something in the configuration changed: in case that this is the
177 * first time that the phy configuration is applied or in case that the phy
178 * configuration changed from the previous apply.
180 static int iwl_mvm_phy_ctxt_apply(struct iwl_mvm *mvm,
181 struct iwl_mvm_phy_ctxt *ctxt,
182 struct cfg80211_chan_def *chandef,
183 u8 chains_static, u8 chains_dynamic,
184 u32 action, u32 apply_time)
186 struct iwl_phy_context_cmd cmd;
189 /* Set the command header fields */
190 iwl_mvm_phy_ctxt_cmd_hdr(ctxt, &cmd, action, apply_time);
192 /* Set the command data */
193 iwl_mvm_phy_ctxt_cmd_data(mvm, &cmd, chandef,
194 chains_static, chains_dynamic);
196 ret = iwl_mvm_send_cmd_pdu(mvm, PHY_CONTEXT_CMD, CMD_SYNC,
197 sizeof(struct iwl_phy_context_cmd),
200 IWL_ERR(mvm, "PHY ctxt cmd error. ret=%d\n", ret);
205 struct phy_ctx_used_data {
206 unsigned long used[BITS_TO_LONGS(NUM_PHY_CTX)];
209 static void iwl_mvm_phy_ctx_used_iter(struct ieee80211_hw *hw,
210 struct ieee80211_chanctx_conf *ctx,
213 struct phy_ctx_used_data *data = _data;
214 struct iwl_mvm_phy_ctxt *phy_ctxt = (void *)ctx->drv_priv;
216 __set_bit(phy_ctxt->id, data->used);
220 * Send a command to add a PHY context based on the current HW configuration.
222 int iwl_mvm_phy_ctxt_add(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
223 struct cfg80211_chan_def *chandef,
224 u8 chains_static, u8 chains_dynamic)
226 struct phy_ctx_used_data data = {
231 * If this is a regular PHY context (not the ROC one)
232 * skip the ROC PHY context's ID.
234 if (ctxt != &mvm->phy_ctxt_roc)
235 __set_bit(mvm->phy_ctxt_roc.id, data.used);
237 lockdep_assert_held(&mvm->mutex);
240 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
241 ieee80211_iter_chan_contexts_atomic(
242 mvm->hw, iwl_mvm_phy_ctx_used_iter, &data);
244 ctxt->id = find_first_zero_bit(data.used, NUM_PHY_CTX);
245 if (WARN_ONCE(ctxt->id == NUM_PHY_CTX,
246 "Failed to init PHY context - no free ID!\n"))
250 ctxt->channel = chandef->chan;
251 return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
252 chains_static, chains_dynamic,
253 FW_CTXT_ACTION_ADD, 0);
257 * Send a command to modify the PHY context based on the current HW
258 * configuration. Note that the function does not check that the configuration
261 int iwl_mvm_phy_ctxt_changed(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt,
262 struct cfg80211_chan_def *chandef,
263 u8 chains_static, u8 chains_dynamic)
265 lockdep_assert_held(&mvm->mutex);
267 ctxt->channel = chandef->chan;
268 return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef,
269 chains_static, chains_dynamic,
270 FW_CTXT_ACTION_MODIFY, 0);
274 * Send a command to the FW to remove the given phy context.
275 * Once the command is sent, regardless of success or failure, the context is
278 void iwl_mvm_phy_ctxt_remove(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt)
280 struct iwl_phy_context_cmd cmd;
283 lockdep_assert_held(&mvm->mutex);
285 iwl_mvm_phy_ctxt_cmd_hdr(ctxt, &cmd, FW_CTXT_ACTION_REMOVE, 0);
286 ret = iwl_mvm_send_cmd_pdu(mvm, PHY_CONTEXT_CMD, CMD_SYNC,
287 sizeof(struct iwl_phy_context_cmd),
290 IWL_ERR(mvm, "Failed to send PHY remove: ctxt id=%d\n",