Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[deliverable/linux.git] / drivers / net / wireless / iwlwifi / mvm / power.c
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 *
8 * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
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
25 * in the file called COPYING.
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 *
33 * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
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 <linux/kernel.h>
65 #include <linux/module.h>
66 #include <linux/slab.h>
67 #include <linux/init.h>
68
69 #include <net/mac80211.h>
70
71 #include "iwl-debug.h"
72 #include "mvm.h"
73 #include "iwl-modparams.h"
74 #include "fw-api-power.h"
75
76 #define POWER_KEEP_ALIVE_PERIOD_SEC 25
77
78 static int iwl_mvm_beacon_filter_send_cmd(struct iwl_mvm *mvm,
79 struct iwl_beacon_filter_cmd *cmd)
80 {
81 int ret;
82
83 ret = iwl_mvm_send_cmd_pdu(mvm, REPLY_BEACON_FILTERING_CMD, CMD_SYNC,
84 sizeof(struct iwl_beacon_filter_cmd), cmd);
85
86 if (!ret) {
87 IWL_DEBUG_POWER(mvm, "ba_enable_beacon_abort is: %d\n",
88 cmd->ba_enable_beacon_abort);
89 IWL_DEBUG_POWER(mvm, "ba_escape_timer is: %d\n",
90 cmd->ba_escape_timer);
91 IWL_DEBUG_POWER(mvm, "bf_debug_flag is: %d\n",
92 cmd->bf_debug_flag);
93 IWL_DEBUG_POWER(mvm, "bf_enable_beacon_filter is: %d\n",
94 cmd->bf_enable_beacon_filter);
95 IWL_DEBUG_POWER(mvm, "bf_energy_delta is: %d\n",
96 cmd->bf_energy_delta);
97 IWL_DEBUG_POWER(mvm, "bf_escape_timer is: %d\n",
98 cmd->bf_escape_timer);
99 IWL_DEBUG_POWER(mvm, "bf_roaming_energy_delta is: %d\n",
100 cmd->bf_roaming_energy_delta);
101 IWL_DEBUG_POWER(mvm, "bf_roaming_state is: %d\n",
102 cmd->bf_roaming_state);
103 IWL_DEBUG_POWER(mvm, "bf_temperature_delta is: %d\n",
104 cmd->bf_temperature_delta);
105 }
106 return ret;
107 }
108
109 static int iwl_mvm_update_beacon_abort(struct iwl_mvm *mvm,
110 struct ieee80211_vif *vif, bool enable)
111 {
112 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
113 struct iwl_beacon_filter_cmd cmd = {
114 IWL_BF_CMD_CONFIG_DEFAULTS,
115 .bf_enable_beacon_filter = 1,
116 .ba_enable_beacon_abort = enable,
117 };
118
119 if (!mvmvif->bf_enabled)
120 return 0;
121
122 iwl_mvm_beacon_filter_debugfs_parameters(vif, &cmd);
123 return iwl_mvm_beacon_filter_send_cmd(mvm, &cmd);
124 }
125
126 static void iwl_mvm_power_log(struct iwl_mvm *mvm,
127 struct iwl_powertable_cmd *cmd)
128 {
129 IWL_DEBUG_POWER(mvm,
130 "Sending power table command for power level %d, flags = 0x%X\n",
131 iwlmvm_mod_params.power_scheme,
132 le16_to_cpu(cmd->flags));
133 IWL_DEBUG_POWER(mvm, "Keep alive = %u sec\n", cmd->keep_alive_seconds);
134
135 if (cmd->flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK)) {
136 IWL_DEBUG_POWER(mvm, "Rx timeout = %u usec\n",
137 le32_to_cpu(cmd->rx_data_timeout));
138 IWL_DEBUG_POWER(mvm, "Tx timeout = %u usec\n",
139 le32_to_cpu(cmd->tx_data_timeout));
140 IWL_DEBUG_POWER(mvm, "LP RX RSSI threshold = %u\n",
141 cmd->lprx_rssi_threshold);
142 if (cmd->flags & cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK))
143 IWL_DEBUG_POWER(mvm, "DTIM periods to skip = %u\n",
144 le32_to_cpu(cmd->skip_dtim_periods));
145 }
146 }
147
148 void iwl_mvm_power_build_cmd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
149 struct iwl_powertable_cmd *cmd)
150 {
151 struct ieee80211_hw *hw = mvm->hw;
152 struct ieee80211_chanctx_conf *chanctx_conf;
153 struct ieee80211_channel *chan;
154 int dtimper, dtimper_msec;
155 int keep_alive;
156 bool radar_detect = false;
157 struct iwl_mvm_vif *mvmvif __maybe_unused =
158 iwl_mvm_vif_from_mac80211(vif);
159
160 /*
161 * Regardless of power management state the driver must set
162 * keep alive period. FW will use it for sending keep alive NDPs
163 * immediately after association.
164 */
165 cmd->keep_alive_seconds = POWER_KEEP_ALIVE_PERIOD_SEC;
166
167 if (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM)
168 return;
169
170 cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK);
171 if (!vif->bss_conf.assoc)
172 cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK);
173
174 #ifdef CONFIG_IWLWIFI_DEBUGFS
175 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_DISABLE_POWER_OFF &&
176 mvmvif->dbgfs_pm.disable_power_off)
177 cmd->flags &= cpu_to_le16(~POWER_FLAGS_POWER_SAVE_ENA_MSK);
178 #endif
179 if (!vif->bss_conf.ps)
180 return;
181
182 cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK);
183
184 dtimper = hw->conf.ps_dtim_period ?: 1;
185
186 /* Check if radar detection is required on current channel */
187 rcu_read_lock();
188 chanctx_conf = rcu_dereference(vif->chanctx_conf);
189 WARN_ON(!chanctx_conf);
190 if (chanctx_conf) {
191 chan = chanctx_conf->def.chan;
192 radar_detect = chan->flags & IEEE80211_CHAN_RADAR;
193 }
194 rcu_read_unlock();
195
196 /* Check skip over DTIM conditions */
197 if (!radar_detect && (dtimper <= 10) &&
198 (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_LP ||
199 mvm->cur_ucode == IWL_UCODE_WOWLAN)) {
200 cmd->flags |= cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
201 cmd->skip_dtim_periods = cpu_to_le32(3);
202 }
203
204 /* Check that keep alive period is at least 3 * DTIM */
205 dtimper_msec = dtimper * vif->bss_conf.beacon_int;
206 keep_alive = max_t(int, 3 * dtimper_msec,
207 MSEC_PER_SEC * cmd->keep_alive_seconds);
208 keep_alive = DIV_ROUND_UP(keep_alive, MSEC_PER_SEC);
209 cmd->keep_alive_seconds = keep_alive;
210
211 if (mvm->cur_ucode != IWL_UCODE_WOWLAN) {
212 cmd->rx_data_timeout = cpu_to_le32(100 * USEC_PER_MSEC);
213 cmd->tx_data_timeout = cpu_to_le32(100 * USEC_PER_MSEC);
214 } else {
215 cmd->rx_data_timeout = cpu_to_le32(10 * USEC_PER_MSEC);
216 cmd->tx_data_timeout = cpu_to_le32(10 * USEC_PER_MSEC);
217 }
218
219 #ifdef CONFIG_IWLWIFI_DEBUGFS
220 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_KEEP_ALIVE)
221 cmd->keep_alive_seconds = mvmvif->dbgfs_pm.keep_alive_seconds;
222 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_OVER_DTIM) {
223 if (mvmvif->dbgfs_pm.skip_over_dtim)
224 cmd->flags |=
225 cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
226 else
227 cmd->flags &=
228 cpu_to_le16(~POWER_FLAGS_SKIP_OVER_DTIM_MSK);
229 }
230 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_RX_DATA_TIMEOUT)
231 cmd->rx_data_timeout =
232 cpu_to_le32(mvmvif->dbgfs_pm.rx_data_timeout);
233 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_TX_DATA_TIMEOUT)
234 cmd->tx_data_timeout =
235 cpu_to_le32(mvmvif->dbgfs_pm.tx_data_timeout);
236 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_DTIM_PERIODS)
237 cmd->skip_dtim_periods =
238 cpu_to_le32(mvmvif->dbgfs_pm.skip_dtim_periods);
239 #endif /* CONFIG_IWLWIFI_DEBUGFS */
240 }
241
242 int iwl_mvm_power_update_mode(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
243 {
244 int ret;
245 bool ba_enable;
246 struct iwl_powertable_cmd cmd = {};
247
248 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
249 return 0;
250
251 /*
252 * TODO: The following vif_count verification is temporary condition.
253 * Avoid power mode update if more than one interface is currently
254 * active. Remove this condition when FW will support power management
255 * on multiple MACs.
256 */
257 IWL_DEBUG_POWER(mvm, "Currently %d interfaces active\n",
258 mvm->vif_count);
259 if (mvm->vif_count > 1)
260 return 0;
261
262 iwl_mvm_power_build_cmd(mvm, vif, &cmd);
263 iwl_mvm_power_log(mvm, &cmd);
264
265 ret = iwl_mvm_send_cmd_pdu(mvm, POWER_TABLE_CMD, CMD_SYNC,
266 sizeof(cmd), &cmd);
267 if (ret)
268 return ret;
269
270 ba_enable = !!(cmd.flags &
271 cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK));
272
273 return iwl_mvm_update_beacon_abort(mvm, vif, ba_enable);
274 }
275
276 int iwl_mvm_power_disable(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
277 {
278 struct iwl_powertable_cmd cmd = {};
279 struct iwl_mvm_vif *mvmvif __maybe_unused =
280 iwl_mvm_vif_from_mac80211(vif);
281
282 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
283 return 0;
284
285 if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
286 cmd.flags |= cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK);
287
288 #ifdef CONFIG_IWLWIFI_DEBUGFS
289 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_DISABLE_POWER_OFF &&
290 mvmvif->dbgfs_pm.disable_power_off)
291 cmd.flags &= cpu_to_le16(~POWER_FLAGS_POWER_SAVE_ENA_MSK);
292 #endif
293 iwl_mvm_power_log(mvm, &cmd);
294
295 return iwl_mvm_send_cmd_pdu(mvm, POWER_TABLE_CMD, CMD_ASYNC,
296 sizeof(cmd), &cmd);
297 }
298
299 #ifdef CONFIG_IWLWIFI_DEBUGFS
300 void
301 iwl_mvm_beacon_filter_debugfs_parameters(struct ieee80211_vif *vif,
302 struct iwl_beacon_filter_cmd *cmd)
303 {
304 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
305 struct iwl_dbgfs_bf *dbgfs_bf = &mvmvif->dbgfs_bf;
306
307 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ENERGY_DELTA)
308 cmd->bf_energy_delta = dbgfs_bf->bf_energy_delta;
309 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ROAMING_ENERGY_DELTA)
310 cmd->bf_roaming_energy_delta =
311 dbgfs_bf->bf_roaming_energy_delta;
312 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ROAMING_STATE)
313 cmd->bf_roaming_state = dbgfs_bf->bf_roaming_state;
314 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMPERATURE_DELTA)
315 cmd->bf_temperature_delta = dbgfs_bf->bf_temperature_delta;
316 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_DEBUG_FLAG)
317 cmd->bf_debug_flag = dbgfs_bf->bf_debug_flag;
318 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ESCAPE_TIMER)
319 cmd->bf_escape_timer = cpu_to_le32(dbgfs_bf->bf_escape_timer);
320 if (dbgfs_bf->mask & MVM_DEBUGFS_BA_ESCAPE_TIMER)
321 cmd->ba_escape_timer = cpu_to_le32(dbgfs_bf->ba_escape_timer);
322 if (dbgfs_bf->mask & MVM_DEBUGFS_BA_ENABLE_BEACON_ABORT)
323 cmd->ba_enable_beacon_abort = dbgfs_bf->ba_enable_beacon_abort;
324 }
325 #endif
326
327 int iwl_mvm_enable_beacon_filter(struct iwl_mvm *mvm,
328 struct ieee80211_vif *vif)
329 {
330 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
331 struct iwl_beacon_filter_cmd cmd = {
332 IWL_BF_CMD_CONFIG_DEFAULTS,
333 .bf_enable_beacon_filter = 1,
334 };
335 int ret;
336
337 if (mvmvif != mvm->bf_allowed_vif ||
338 vif->type != NL80211_IFTYPE_STATION || vif->p2p)
339 return 0;
340
341 iwl_mvm_beacon_filter_debugfs_parameters(vif, &cmd);
342 ret = iwl_mvm_beacon_filter_send_cmd(mvm, &cmd);
343
344 if (!ret)
345 mvmvif->bf_enabled = true;
346
347 return ret;
348 }
349
350 int iwl_mvm_disable_beacon_filter(struct iwl_mvm *mvm,
351 struct ieee80211_vif *vif)
352 {
353 struct iwl_beacon_filter_cmd cmd = {};
354 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
355 int ret;
356
357 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
358 return 0;
359
360 ret = iwl_mvm_beacon_filter_send_cmd(mvm, &cmd);
361
362 if (!ret)
363 mvmvif->bf_enabled = false;
364
365 return ret;
366 }
This page took 0.069045 seconds and 6 git commands to generate.