Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 1 | /****************************************************************************** |
| 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 Grumbach | 51368bf | 2013-12-30 13:15:54 +0200 | [diff] [blame] | 8 | * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved. |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 9 | * |
| 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. |
| 13 | * |
| 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. |
| 18 | * |
| 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, |
| 22 | * USA |
| 23 | * |
| 24 | * The full GNU General Public License is included in this distribution |
Emmanuel Grumbach | 410dc5a | 2013-02-18 09:22:28 +0200 | [diff] [blame] | 25 | * in the file called COPYING. |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 26 | * |
| 27 | * Contact Information: |
| 28 | * Intel Linux Wireless <ilw@linux.intel.com> |
| 29 | * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 |
| 30 | * |
| 31 | * BSD LICENSE |
| 32 | * |
Emmanuel Grumbach | 51368bf | 2013-12-30 13:15:54 +0200 | [diff] [blame] | 33 | * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved. |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 34 | * 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 | |
| 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) |
| 70 | { |
| 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 | */ |
| 91 | static inline u8 iwl_mvm_get_ctrl_pos(struct cfg80211_chan_def *chandef) |
| 92 | { |
| 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 | */ |
| 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) |
| 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 | */ |
| 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) |
| 144 | { |
Emmanuel Grumbach | 3322354 | 2013-03-09 20:38:19 +0200 | [diff] [blame] | 145 | u8 active_cnt, idle_cnt; |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 146 | |
| 147 | /* Set the channel info data */ |
| 148 | cmd->ci.band = (chandef->chan->band == IEEE80211_BAND_2GHZ ? |
| 149 | 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 Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 156 | idle_cnt = chains_static; |
| 157 | active_cnt = chains_dynamic; |
| 158 | |
Johannes Berg | 1a095d3 | 2014-05-12 11:14:51 +0200 | [diff] [blame] | 159 | /* 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 Grumbach | 5c90422 | 2014-05-18 09:16:45 +0300 | [diff] [blame^] | 166 | if (active_cnt == 1 && iwl_mvm_rx_diversity_allowed(mvm)) { |
Johannes Berg | 1a095d3 | 2014-05-12 11:14:51 +0200 | [diff] [blame] | 167 | idle_cnt = 2; |
| 168 | active_cnt = 2; |
| 169 | } |
| 170 | |
Johannes Berg | 4ed735e | 2014-02-12 21:47:44 +0100 | [diff] [blame] | 171 | cmd->rxchain_info = cpu_to_le32(mvm->fw->valid_rx_ant << |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 172 | 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); |
| 176 | |
Johannes Berg | 4ed735e | 2014-02-12 21:47:44 +0100 | [diff] [blame] | 177 | cmd->txchain_info = cpu_to_le32(mvm->fw->valid_tx_ant); |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Send a command to apply the current phy configuration. The command is send |
| 182 | * only if something in the configuration changed: in case that this is the |
| 183 | * first time that the phy configuration is applied or in case that the phy |
| 184 | * configuration changed from the previous apply. |
| 185 | */ |
| 186 | static int iwl_mvm_phy_ctxt_apply(struct iwl_mvm *mvm, |
| 187 | struct iwl_mvm_phy_ctxt *ctxt, |
| 188 | struct cfg80211_chan_def *chandef, |
| 189 | u8 chains_static, u8 chains_dynamic, |
| 190 | u32 action, u32 apply_time) |
| 191 | { |
| 192 | struct iwl_phy_context_cmd cmd; |
| 193 | int ret; |
| 194 | |
| 195 | /* Set the command header fields */ |
| 196 | iwl_mvm_phy_ctxt_cmd_hdr(ctxt, &cmd, action, apply_time); |
| 197 | |
| 198 | /* Set the command data */ |
| 199 | iwl_mvm_phy_ctxt_cmd_data(mvm, &cmd, chandef, |
| 200 | chains_static, chains_dynamic); |
| 201 | |
Emmanuel Grumbach | a102292 | 2014-05-12 11:36:41 +0300 | [diff] [blame] | 202 | ret = iwl_mvm_send_cmd_pdu(mvm, PHY_CONTEXT_CMD, 0, |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 203 | sizeof(struct iwl_phy_context_cmd), |
| 204 | &cmd); |
| 205 | if (ret) |
| 206 | IWL_ERR(mvm, "PHY ctxt cmd error. ret=%d\n", ret); |
| 207 | return ret; |
| 208 | } |
| 209 | |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 210 | /* |
| 211 | * Send a command to add a PHY context based on the current HW configuration. |
| 212 | */ |
| 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) |
| 216 | { |
Johannes Berg | debff61 | 2013-05-14 13:53:45 +0200 | [diff] [blame] | 217 | WARN_ON(!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) && |
| 218 | ctxt->ref); |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 219 | lockdep_assert_held(&mvm->mutex); |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 220 | |
| 221 | ctxt->channel = chandef->chan; |
Ilan Peer | fe0f2de | 2013-03-21 10:23:52 +0200 | [diff] [blame] | 222 | |
Johannes Berg | 7a53174 | 2014-04-30 14:28:40 +0200 | [diff] [blame] | 223 | return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef, |
| 224 | chains_static, chains_dynamic, |
| 225 | FW_CTXT_ACTION_ADD, 0); |
Ilan Peer | fe0f2de | 2013-03-21 10:23:52 +0200 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /* |
| 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. |
| 231 | */ |
| 232 | void iwl_mvm_phy_ctxt_ref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt) |
| 233 | { |
Ilan Peer | fe0f2de | 2013-03-21 10:23:52 +0200 | [diff] [blame] | 234 | lockdep_assert_held(&mvm->mutex); |
Ilan Peer | fe0f2de | 2013-03-21 10:23:52 +0200 | [diff] [blame] | 235 | ctxt->ref++; |
Johannes Berg | 8ca151b | 2013-01-24 14:25:36 +0100 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /* |
| 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 |
| 241 | * changed. |
| 242 | */ |
| 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) |
| 246 | { |
| 247 | lockdep_assert_held(&mvm->mutex); |
| 248 | |
| 249 | ctxt->channel = chandef->chan; |
| 250 | return iwl_mvm_phy_ctxt_apply(mvm, ctxt, chandef, |
| 251 | chains_static, chains_dynamic, |
| 252 | FW_CTXT_ACTION_MODIFY, 0); |
| 253 | } |
| 254 | |
Ilan Peer | fe0f2de | 2013-03-21 10:23:52 +0200 | [diff] [blame] | 255 | void iwl_mvm_phy_ctxt_unref(struct iwl_mvm *mvm, struct iwl_mvm_phy_ctxt *ctxt) |
| 256 | { |
| 257 | lockdep_assert_held(&mvm->mutex); |
Johannes Berg | e282101 | 2013-05-08 15:55:31 +0200 | [diff] [blame] | 258 | |
| 259 | if (WARN_ON_ONCE(!ctxt)) |
| 260 | return; |
| 261 | |
Ilan Peer | fe0f2de | 2013-03-21 10:23:52 +0200 | [diff] [blame] | 262 | ctxt->ref--; |
Ilan Peer | fe0f2de | 2013-03-21 10:23:52 +0200 | [diff] [blame] | 263 | } |