blob: c43c8e66de73cd9a1de0f9435b0fa59d68e5219b [file] [log] [blame]
Mohamed Abbas5da4b552008-04-21 15:41:51 -07001/******************************************************************************
2 *
Wey-Yi Guy901069c2011-04-05 09:42:00 -07003 * Copyright(c) 2007 - 2011 Intel Corporation. All rights reserved.
Mohamed Abbas5da4b552008-04-21 15:41:51 -07004 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
Winkler, Tomas759ef892008-12-09 11:28:58 -080025 * Intel Linux Wireless <ilw@linux.intel.com>
Mohamed Abbas5da4b552008-04-21 15:41:51 -070026 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *****************************************************************************/
28
29
30#include <linux/kernel.h>
31#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Mohamed Abbas5da4b552008-04-21 15:41:51 -070033#include <linux/init.h>
34
35#include <net/mac80211.h>
36
37#include "iwl-eeprom.h"
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070038#include "iwl-dev.h"
Mohamed Abbas5da4b552008-04-21 15:41:51 -070039#include "iwl-core.h"
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -070040#include "iwl-io.h"
Tomas Winkler5a36ba02008-04-24 11:55:37 -070041#include "iwl-commands.h"
Mohamed Abbas5da4b552008-04-21 15:41:51 -070042#include "iwl-debug.h"
43#include "iwl-power.h"
Mohamed Abbas5da4b552008-04-21 15:41:51 -070044
45/*
Johannes Berge312c242009-08-07 15:41:51 -070046 * Setting power level allows the card to go to sleep when not busy.
Mohamed Abbas5da4b552008-04-21 15:41:51 -070047 *
Johannes Berge312c242009-08-07 15:41:51 -070048 * We calculate a sleep command based on the required latency, which
49 * we get from mac80211. In order to handle thermal throttling, we can
50 * also use pre-defined power levels.
Mohamed Abbas5da4b552008-04-21 15:41:51 -070051 */
52
Johannes Berge312c242009-08-07 15:41:51 -070053/*
54 * For now, keep using power level 1 instead of automatically
55 * adjusting ...
56 */
57bool no_sleep_autoadjust = true;
58module_param(no_sleep_autoadjust, bool, S_IRUGO);
59MODULE_PARM_DESC(no_sleep_autoadjust,
60 "don't automatically adjust sleep level "
61 "according to maximum network latency");
Mohamed Abbas5da4b552008-04-21 15:41:51 -070062
Johannes Berge312c242009-08-07 15:41:51 -070063/*
64 * This defines the old power levels. They are still used by default
65 * (level 1) and for thermal throttle (levels 3 through 5)
66 */
67
68struct iwl_power_vec_entry {
69 struct iwl_powertable_cmd cmd;
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -070070 u8 no_dtim; /* number of skip dtim */
Johannes Berge312c242009-08-07 15:41:51 -070071};
72
73#define IWL_DTIM_RANGE_0_MAX 2
74#define IWL_DTIM_RANGE_1_MAX 10
Mohamed Abbas5da4b552008-04-21 15:41:51 -070075
Johannes Berg7af2c462009-05-08 13:44:38 -070076#define NOSLP cpu_to_le16(0), 0, 0
77#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
Wey-Yi Guy35162ba2010-11-23 10:58:56 -080078#define ASLP (IWL_POWER_POWER_SAVE_ENA_MSK | \
79 IWL_POWER_POWER_MANAGEMENT_ENA_MSK | \
80 IWL_POWER_ADVANCE_PM_ENA_MSK)
81#define ASLP_TOUT(T) cpu_to_le32(T)
Johannes Berg7af2c462009-05-08 13:44:38 -070082#define TU_TO_USEC 1024
83#define SLP_TOUT(T) cpu_to_le32((T) * TU_TO_USEC)
84#define SLP_VEC(X0, X1, X2, X3, X4) {cpu_to_le32(X0), \
85 cpu_to_le32(X1), \
86 cpu_to_le32(X2), \
87 cpu_to_le32(X3), \
88 cpu_to_le32(X4)}
Mohamed Abbas5da4b552008-04-21 15:41:51 -070089/* default power management (not Tx power) table values */
Johannes Berge312c242009-08-07 15:41:51 -070090/* for DTIM period 0 through IWL_DTIM_RANGE_0_MAX */
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -070091/* DTIM 0 - 2 */
Johannes Berg7af2c462009-05-08 13:44:38 -070092static const struct iwl_power_vec_entry range_0[IWL_POWER_NUM] = {
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -070093 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 1, 2, 2, 0xFF)}, 0},
Mohamed Abbas5da4b552008-04-21 15:41:51 -070094 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
95 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 2, 2, 2, 0xFF)}, 0},
96 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 2, 4, 4, 0xFF)}, 1},
97 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 2, 4, 6, 0xFF)}, 2}
98};
99
100
Johannes Berge312c242009-08-07 15:41:51 -0700101/* for DTIM period IWL_DTIM_RANGE_0_MAX + 1 through IWL_DTIM_RANGE_1_MAX */
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700102/* DTIM 3 - 10 */
Johannes Berg7af2c462009-05-08 13:44:38 -0700103static const struct iwl_power_vec_entry range_1[IWL_POWER_NUM] = {
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700104 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
105 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 3, 4, 7)}, 0},
106 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 4, 6, 7, 9)}, 0},
107 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 4, 6, 9, 10)}, 1},
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700108 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 4, 6, 10, 10)}, 2}
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700109};
110
Johannes Berge312c242009-08-07 15:41:51 -0700111/* for DTIM period > IWL_DTIM_RANGE_1_MAX */
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700112/* DTIM 11 - */
Johannes Berg7af2c462009-05-08 13:44:38 -0700113static const struct iwl_power_vec_entry range_2[IWL_POWER_NUM] = {
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700114 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
115 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
116 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
117 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
118 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
119};
120
Wey-Yi Guy35162ba2010-11-23 10:58:56 -0800121/* advance power management */
122/* DTIM 0 - 2 */
123static const struct iwl_power_vec_entry apm_range_0[IWL_POWER_NUM] = {
124 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
125 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
126 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
127 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
128 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
129 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
130 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
131 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
132 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
133 SLP_VEC(1, 2, 6, 8, 0xFF), ASLP_TOUT(2)}, 2}
134};
135
136
137/* for DTIM period IWL_DTIM_RANGE_0_MAX + 1 through IWL_DTIM_RANGE_1_MAX */
138/* DTIM 3 - 10 */
139static const struct iwl_power_vec_entry apm_range_1[IWL_POWER_NUM] = {
140 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
141 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
142 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
143 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
144 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
145 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
146 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
147 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
148 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
149 SLP_VEC(1, 2, 6, 8, 0xFF), 0}, 2}
150};
151
152/* for DTIM period > IWL_DTIM_RANGE_1_MAX */
153/* DTIM 11 - */
154static const struct iwl_power_vec_entry apm_range_2[IWL_POWER_NUM] = {
155 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
156 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
157 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
158 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
159 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
160 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
161 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
162 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
163 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
164 SLP_VEC(1, 2, 6, 8, 0xFF), ASLP_TOUT(2)}, 2}
165};
166
Johannes Berge312c242009-08-07 15:41:51 -0700167static void iwl_static_sleep_cmd(struct iwl_priv *priv,
168 struct iwl_powertable_cmd *cmd,
169 enum iwl_power_level lvl, int period)
170{
171 const struct iwl_power_vec_entry *table;
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700172 int max_sleep[IWL_POWER_VEC_SIZE] = { 0 };
173 int i;
174 u8 skip;
175 u32 slp_itrvl;
Johannes Berge312c242009-08-07 15:41:51 -0700176
Wey-Yi Guy35162ba2010-11-23 10:58:56 -0800177 if (priv->cfg->adv_pm) {
178 table = apm_range_2;
179 if (period <= IWL_DTIM_RANGE_1_MAX)
180 table = apm_range_1;
181 if (period <= IWL_DTIM_RANGE_0_MAX)
182 table = apm_range_0;
183 } else {
184 table = range_2;
185 if (period <= IWL_DTIM_RANGE_1_MAX)
186 table = range_1;
187 if (period <= IWL_DTIM_RANGE_0_MAX)
188 table = range_0;
189 }
Johannes Berge312c242009-08-07 15:41:51 -0700190
191 BUG_ON(lvl < 0 || lvl >= IWL_POWER_NUM);
192
193 *cmd = table[lvl].cmd;
194
195 if (period == 0) {
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700196 skip = 0;
Johannes Berge312c242009-08-07 15:41:51 -0700197 period = 1;
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700198 for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
199 max_sleep[i] = 1;
200
Johannes Berge312c242009-08-07 15:41:51 -0700201 } else {
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700202 skip = table[lvl].no_dtim;
203 for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
204 max_sleep[i] = le32_to_cpu(cmd->sleep_interval[i]);
205 max_sleep[IWL_POWER_VEC_SIZE - 1] = skip + 1;
Johannes Berge312c242009-08-07 15:41:51 -0700206 }
207
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700208 slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
209 /* figure out the listen interval based on dtim period and skip */
210 if (slp_itrvl == 0xFF)
211 cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
212 cpu_to_le32(period * (skip + 1));
213
214 slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
215 if (slp_itrvl > period)
216 cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
217 cpu_to_le32((slp_itrvl / period) * period);
218
219 if (skip)
Johannes Berge312c242009-08-07 15:41:51 -0700220 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700221 else
Johannes Berge312c242009-08-07 15:41:51 -0700222 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
Johannes Berge312c242009-08-07 15:41:51 -0700223
Wey-Yi Guy1f37daf2010-11-23 10:58:55 -0800224 if (priv->cfg->base_params->shadow_reg_enable)
225 cmd->flags |= IWL_POWER_SHADOW_REG_ENA;
226 else
227 cmd->flags &= ~IWL_POWER_SHADOW_REG_ENA;
228
Stanislaw Gruszka88e58fc2011-01-28 16:47:47 +0100229 if (iwl_advanced_bt_coexist(priv)) {
Wey-Yi Guye3661762010-11-23 10:58:54 -0800230 if (!priv->cfg->bt_params->bt_sco_disable)
231 cmd->flags |= IWL_POWER_BT_SCO_ENA;
232 else
233 cmd->flags &= ~IWL_POWER_BT_SCO_ENA;
234 }
235
236
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700237 slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
Wey-Yi Guy570af862009-10-23 13:42:33 -0700238 if (slp_itrvl > IWL_CONN_MAX_LISTEN_INTERVAL)
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700239 cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
Wey-Yi Guy570af862009-10-23 13:42:33 -0700240 cpu_to_le32(IWL_CONN_MAX_LISTEN_INTERVAL);
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700241
242 /* enforce max sleep interval */
243 for (i = IWL_POWER_VEC_SIZE - 1; i >= 0 ; i--) {
244 if (le32_to_cpu(cmd->sleep_interval[i]) >
245 (max_sleep[i] * period))
246 cmd->sleep_interval[i] =
247 cpu_to_le32(max_sleep[i] * period);
248 if (i != (IWL_POWER_VEC_SIZE - 1)) {
249 if (le32_to_cpu(cmd->sleep_interval[i]) >
250 le32_to_cpu(cmd->sleep_interval[i+1]))
251 cmd->sleep_interval[i] =
252 cmd->sleep_interval[i+1];
253 }
254 }
Johannes Berge312c242009-08-07 15:41:51 -0700255
256 if (priv->power_data.pci_pm)
257 cmd->flags |= IWL_POWER_PCI_PM_MSK;
258 else
259 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
260
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700261 IWL_DEBUG_POWER(priv, "numSkipDtim = %u, dtimPeriod = %d\n",
262 skip, period);
Johannes Berge312c242009-08-07 15:41:51 -0700263 IWL_DEBUG_POWER(priv, "Sleep command for index %d\n", lvl + 1);
264}
265
Johannes Berge312c242009-08-07 15:41:51 -0700266static void iwl_power_sleep_cam_cmd(struct iwl_priv *priv,
267 struct iwl_powertable_cmd *cmd)
Mohamed Abbasca579612008-07-18 13:52:57 +0800268{
Johannes Berge312c242009-08-07 15:41:51 -0700269 memset(cmd, 0, sizeof(*cmd));
270
271 if (priv->power_data.pci_pm)
272 cmd->flags |= IWL_POWER_PCI_PM_MSK;
273
274 IWL_DEBUG_POWER(priv, "Sleep command for CAM\n");
Mohamed Abbasca579612008-07-18 13:52:57 +0800275}
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700276
Johannes Berge312c242009-08-07 15:41:51 -0700277static void iwl_power_fill_sleep_cmd(struct iwl_priv *priv,
278 struct iwl_powertable_cmd *cmd,
279 int dynps_ms, int wakeup_period)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700280{
Johannes Berg4c561a02009-08-28 09:44:48 -0700281 /*
282 * These are the original power level 3 sleep successions. The
283 * device may behave better with such succession and was also
284 * only tested with that. Just like the original sleep commands,
285 * also adjust the succession here to the wakeup_period below.
286 * The ranges are the same as for the sleep commands, 0-2, 3-9
287 * and >10, which is selected based on the DTIM interval for
288 * the sleep index but here we use the wakeup period since that
289 * is what we need to do for the latency requirements.
290 */
291 static const u8 slp_succ_r0[IWL_POWER_VEC_SIZE] = { 2, 2, 2, 2, 2 };
292 static const u8 slp_succ_r1[IWL_POWER_VEC_SIZE] = { 2, 4, 6, 7, 9 };
293 static const u8 slp_succ_r2[IWL_POWER_VEC_SIZE] = { 2, 7, 9, 9, 0xFF };
294 const u8 *slp_succ = slp_succ_r0;
Tomas Winklere7b63582008-09-03 11:26:49 +0800295 int i;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700296
Johannes Berg4c561a02009-08-28 09:44:48 -0700297 if (wakeup_period > IWL_DTIM_RANGE_0_MAX)
298 slp_succ = slp_succ_r1;
299 if (wakeup_period > IWL_DTIM_RANGE_1_MAX)
300 slp_succ = slp_succ_r2;
301
Johannes Berge312c242009-08-07 15:41:51 -0700302 memset(cmd, 0, sizeof(*cmd));
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700303
Johannes Berge312c242009-08-07 15:41:51 -0700304 cmd->flags = IWL_POWER_DRIVER_ALLOW_SLEEP_MSK |
305 IWL_POWER_FAST_PD; /* no use seeing frames for others */
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700306
Johannes Berge312c242009-08-07 15:41:51 -0700307 if (priv->power_data.pci_pm)
308 cmd->flags |= IWL_POWER_PCI_PM_MSK;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700309
Wey-Yi Guy1f37daf2010-11-23 10:58:55 -0800310 if (priv->cfg->base_params->shadow_reg_enable)
311 cmd->flags |= IWL_POWER_SHADOW_REG_ENA;
312 else
313 cmd->flags &= ~IWL_POWER_SHADOW_REG_ENA;
314
Stanislaw Gruszka88e58fc2011-01-28 16:47:47 +0100315 if (iwl_advanced_bt_coexist(priv)) {
Wey-Yi Guye3661762010-11-23 10:58:54 -0800316 if (!priv->cfg->bt_params->bt_sco_disable)
317 cmd->flags |= IWL_POWER_BT_SCO_ENA;
318 else
319 cmd->flags &= ~IWL_POWER_BT_SCO_ENA;
320 }
321
Johannes Berge312c242009-08-07 15:41:51 -0700322 cmd->rx_data_timeout = cpu_to_le32(1000 * dynps_ms);
323 cmd->tx_data_timeout = cpu_to_le32(1000 * dynps_ms);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700324
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800325 for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
Johannes Berg4c561a02009-08-28 09:44:48 -0700326 cmd->sleep_interval[i] =
327 cpu_to_le32(min_t(int, slp_succ[i], wakeup_period));
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700328
Johannes Berge312c242009-08-07 15:41:51 -0700329 IWL_DEBUG_POWER(priv, "Automatic sleep command\n");
330}
331
332static int iwl_set_power(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd)
333{
334 IWL_DEBUG_POWER(priv, "Sending power/sleep command\n");
Tomas Winklere1623442009-01-27 14:27:56 -0800335 IWL_DEBUG_POWER(priv, "Flags value = 0x%08X\n", cmd->flags);
336 IWL_DEBUG_POWER(priv, "Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
337 IWL_DEBUG_POWER(priv, "Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
338 IWL_DEBUG_POWER(priv, "Sleep interval vector = { %d , %d , %d , %d , %d }\n",
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700339 le32_to_cpu(cmd->sleep_interval[0]),
340 le32_to_cpu(cmd->sleep_interval[1]),
341 le32_to_cpu(cmd->sleep_interval[2]),
342 le32_to_cpu(cmd->sleep_interval[3]),
343 le32_to_cpu(cmd->sleep_interval[4]));
344
Johannes Berge312c242009-08-07 15:41:51 -0700345 return iwl_send_cmd_pdu(priv, POWER_TABLE_CMD,
346 sizeof(struct iwl_powertable_cmd), cmd);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700347}
348
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200349static void iwl_power_build_cmd(struct iwl_priv *priv,
350 struct iwl_powertable_cmd *cmd)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700351{
Johannes Berg4d695922010-01-21 05:24:04 -0800352 bool enabled = priv->hw->conf.flags & IEEE80211_CONF_PS;
Johannes Berge312c242009-08-07 15:41:51 -0700353 int dtimper;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700354
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200355 dtimper = priv->hw->conf.ps_dtim_period ?: 1;
356
357 if (priv->cfg->base_params->broken_powersave)
358 iwl_power_sleep_cam_cmd(priv, cmd);
Wey-Yi Guy9dc21532011-01-17 11:05:52 -0800359 else if (priv->hw->conf.flags & IEEE80211_CONF_IDLE)
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200360 iwl_static_sleep_cmd(priv, cmd, IWL_POWER_INDEX_5, 20);
361 else if (priv->cfg->ops->lib->tt_ops.lower_power_detection &&
362 priv->cfg->ops->lib->tt_ops.tt_power_mode &&
363 priv->cfg->ops->lib->tt_ops.lower_power_detection(priv)) {
364 /* in thermal throttling low power state */
365 iwl_static_sleep_cmd(priv, cmd,
366 priv->cfg->ops->lib->tt_ops.tt_power_mode(priv), dtimper);
367 } else if (!enabled)
368 iwl_power_sleep_cam_cmd(priv, cmd);
369 else if (priv->power_data.debug_sleep_level_override >= 0)
370 iwl_static_sleep_cmd(priv, cmd,
371 priv->power_data.debug_sleep_level_override,
372 dtimper);
373 else if (no_sleep_autoadjust)
374 iwl_static_sleep_cmd(priv, cmd, IWL_POWER_INDEX_1, dtimper);
375 else
376 iwl_power_fill_sleep_cmd(priv, cmd,
377 priv->hw->conf.dynamic_ps_timeout,
378 priv->hw->conf.max_sleep_period);
379}
380
381int iwl_power_set_mode(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd,
382 bool force)
383{
384 int ret;
385 bool update_chains;
386
387 lockdep_assert_held(&priv->mutex);
388
Grumbach, Emmanuel04816442008-09-03 11:26:53 +0800389 /* Don't update the RX chain when chain noise calibration is running */
Winkler, Tomasa71c8f62008-11-07 09:58:37 -0800390 update_chains = priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE ||
391 priv->chain_noise_data.state == IWL_CHAIN_NOISE_ALIVE;
Grumbach, Emmanuel04816442008-09-03 11:26:53 +0800392
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200393 if (!memcmp(&priv->power_data.sleep_cmd, cmd, sizeof(*cmd)) && !force)
394 return 0;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700395
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200396 if (!iwl_is_ready_rf(priv))
397 return -EIO;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700398
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200399 /* scan complete use sleep_power_next, need to be updated */
400 memcpy(&priv->power_data.sleep_cmd_next, cmd, sizeof(*cmd));
401 if (test_bit(STATUS_SCANNING, &priv->status) && !force) {
402 IWL_DEBUG_INFO(priv, "Defer power set mode while scanning\n");
403 return 0;
404 }
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700405
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200406 if (cmd->flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK)
407 set_bit(STATUS_POWER_PMI, &priv->status);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700408
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200409 ret = iwl_set_power(priv, cmd);
410 if (!ret) {
411 if (!(cmd->flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK))
412 clear_bit(STATUS_POWER_PMI, &priv->status);
413
414 if (priv->cfg->ops->lib->update_chain_flags && update_chains)
415 priv->cfg->ops->lib->update_chain_flags(priv);
416 else if (priv->cfg->ops->lib->update_chain_flags)
417 IWL_DEBUG_POWER(priv,
Wey-Yi Guy3a780d22009-08-07 15:41:47 -0700418 "Cannot update the power, chain noise "
Winkler, Tomasa71c8f62008-11-07 09:58:37 -0800419 "calibration running: %d\n",
420 priv->chain_noise_data.state);
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200421
422 memcpy(&priv->power_data.sleep_cmd, cmd, sizeof(*cmd));
423 } else
424 IWL_ERR(priv, "set power fail, ret = %d", ret);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700425
426 return ret;
427}
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200428
429int iwl_power_update_mode(struct iwl_priv *priv, bool force)
430{
431 struct iwl_powertable_cmd cmd;
432
433 iwl_power_build_cmd(priv, &cmd);
434 return iwl_power_set_mode(priv, &cmd, force);
435}
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700436
Tomas Winklera96a27f2008-10-23 23:48:56 -0700437/* initialize to default */
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700438void iwl_power_initialize(struct iwl_priv *priv)
439{
Johannes Berge312c242009-08-07 15:41:51 -0700440 u16 lctl = iwl_pcie_link_ctl(priv);
441
442 priv->power_data.pci_pm = !(lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN);
443
444 priv->power_data.debug_sleep_level_override = -1;
445
446 memset(&priv->power_data.sleep_cmd, 0,
447 sizeof(priv->power_data.sleep_cmd));
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700448}