blob: 9c10462c20f0682a45276ff486f2dfb8fe99aa5b [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"
Wey-Yi Guy3f1e5f42011-06-03 13:52:40 -070039#include "iwl-agn.h"
Mohamed Abbas5da4b552008-04-21 15:41:51 -070040#include "iwl-core.h"
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -070041#include "iwl-io.h"
Tomas Winkler5a36ba02008-04-24 11:55:37 -070042#include "iwl-commands.h"
Mohamed Abbas5da4b552008-04-21 15:41:51 -070043#include "iwl-debug.h"
44#include "iwl-power.h"
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070045#include "iwl-trans.h"
Mohamed Abbas5da4b552008-04-21 15:41:51 -070046
47/*
Johannes Berge312c242009-08-07 15:41:51 -070048 * Setting power level allows the card to go to sleep when not busy.
Mohamed Abbas5da4b552008-04-21 15:41:51 -070049 *
Johannes Berge312c242009-08-07 15:41:51 -070050 * We calculate a sleep command based on the required latency, which
51 * we get from mac80211. In order to handle thermal throttling, we can
52 * also use pre-defined power levels.
Mohamed Abbas5da4b552008-04-21 15:41:51 -070053 */
54
Johannes Berge312c242009-08-07 15:41:51 -070055/*
Johannes Berge312c242009-08-07 15:41:51 -070056 * This defines the old power levels. They are still used by default
57 * (level 1) and for thermal throttle (levels 3 through 5)
58 */
59
60struct iwl_power_vec_entry {
61 struct iwl_powertable_cmd cmd;
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -070062 u8 no_dtim; /* number of skip dtim */
Johannes Berge312c242009-08-07 15:41:51 -070063};
64
65#define IWL_DTIM_RANGE_0_MAX 2
66#define IWL_DTIM_RANGE_1_MAX 10
Mohamed Abbas5da4b552008-04-21 15:41:51 -070067
Johannes Berg7af2c462009-05-08 13:44:38 -070068#define NOSLP cpu_to_le16(0), 0, 0
69#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
Wey-Yi Guy35162ba2010-11-23 10:58:56 -080070#define ASLP (IWL_POWER_POWER_SAVE_ENA_MSK | \
71 IWL_POWER_POWER_MANAGEMENT_ENA_MSK | \
72 IWL_POWER_ADVANCE_PM_ENA_MSK)
73#define ASLP_TOUT(T) cpu_to_le32(T)
Johannes Berg7af2c462009-05-08 13:44:38 -070074#define TU_TO_USEC 1024
75#define SLP_TOUT(T) cpu_to_le32((T) * TU_TO_USEC)
76#define SLP_VEC(X0, X1, X2, X3, X4) {cpu_to_le32(X0), \
77 cpu_to_le32(X1), \
78 cpu_to_le32(X2), \
79 cpu_to_le32(X3), \
80 cpu_to_le32(X4)}
Mohamed Abbas5da4b552008-04-21 15:41:51 -070081/* default power management (not Tx power) table values */
Johannes Berge312c242009-08-07 15:41:51 -070082/* for DTIM period 0 through IWL_DTIM_RANGE_0_MAX */
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -070083/* DTIM 0 - 2 */
Johannes Berg7af2c462009-05-08 13:44:38 -070084static const struct iwl_power_vec_entry range_0[IWL_POWER_NUM] = {
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -070085 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 1, 2, 2, 0xFF)}, 0},
Mohamed Abbas5da4b552008-04-21 15:41:51 -070086 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
87 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 2, 2, 2, 0xFF)}, 0},
88 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 2, 4, 4, 0xFF)}, 1},
89 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 2, 4, 6, 0xFF)}, 2}
90};
91
92
Johannes Berge312c242009-08-07 15:41:51 -070093/* for DTIM period IWL_DTIM_RANGE_0_MAX + 1 through IWL_DTIM_RANGE_1_MAX */
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -070094/* DTIM 3 - 10 */
Johannes Berg7af2c462009-05-08 13:44:38 -070095static const struct iwl_power_vec_entry range_1[IWL_POWER_NUM] = {
Mohamed Abbas5da4b552008-04-21 15:41:51 -070096 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
97 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 3, 4, 7)}, 0},
98 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 4, 6, 7, 9)}, 0},
99 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 4, 6, 9, 10)}, 1},
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700100 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 4, 6, 10, 10)}, 2}
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700101};
102
Johannes Berge312c242009-08-07 15:41:51 -0700103/* for DTIM period > IWL_DTIM_RANGE_1_MAX */
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700104/* DTIM 11 - */
Johannes Berg7af2c462009-05-08 13:44:38 -0700105static const struct iwl_power_vec_entry range_2[IWL_POWER_NUM] = {
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700106 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
107 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
108 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
109 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
110 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
111};
112
Wey-Yi Guy35162ba2010-11-23 10:58:56 -0800113/* advance power management */
114/* DTIM 0 - 2 */
115static const struct iwl_power_vec_entry apm_range_0[IWL_POWER_NUM] = {
116 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
117 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
118 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
119 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
120 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
121 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
122 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
123 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
124 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
125 SLP_VEC(1, 2, 6, 8, 0xFF), ASLP_TOUT(2)}, 2}
126};
127
128
129/* for DTIM period IWL_DTIM_RANGE_0_MAX + 1 through IWL_DTIM_RANGE_1_MAX */
130/* DTIM 3 - 10 */
131static const struct iwl_power_vec_entry apm_range_1[IWL_POWER_NUM] = {
132 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
133 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
134 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
135 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
136 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
137 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
138 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
139 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
140 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
141 SLP_VEC(1, 2, 6, 8, 0xFF), 0}, 2}
142};
143
144/* for DTIM period > IWL_DTIM_RANGE_1_MAX */
145/* DTIM 11 - */
146static const struct iwl_power_vec_entry apm_range_2[IWL_POWER_NUM] = {
147 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
148 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
149 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
150 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
151 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
152 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
153 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
154 SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
155 {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
156 SLP_VEC(1, 2, 6, 8, 0xFF), ASLP_TOUT(2)}, 2}
157};
158
Johannes Berge312c242009-08-07 15:41:51 -0700159static void iwl_static_sleep_cmd(struct iwl_priv *priv,
160 struct iwl_powertable_cmd *cmd,
161 enum iwl_power_level lvl, int period)
162{
163 const struct iwl_power_vec_entry *table;
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700164 int max_sleep[IWL_POWER_VEC_SIZE] = { 0 };
165 int i;
166 u8 skip;
167 u32 slp_itrvl;
Johannes Berge312c242009-08-07 15:41:51 -0700168
Wey-Yi Guy35162ba2010-11-23 10:58:56 -0800169 if (priv->cfg->adv_pm) {
170 table = apm_range_2;
171 if (period <= IWL_DTIM_RANGE_1_MAX)
172 table = apm_range_1;
173 if (period <= IWL_DTIM_RANGE_0_MAX)
174 table = apm_range_0;
175 } else {
176 table = range_2;
177 if (period <= IWL_DTIM_RANGE_1_MAX)
178 table = range_1;
179 if (period <= IWL_DTIM_RANGE_0_MAX)
180 table = range_0;
181 }
Johannes Berge312c242009-08-07 15:41:51 -0700182
Johannes Berg3e41ace2011-04-18 09:12:37 -0700183 if (WARN_ON(lvl < 0 || lvl >= IWL_POWER_NUM))
184 memset(cmd, 0, sizeof(*cmd));
185 else
186 *cmd = table[lvl].cmd;
Johannes Berge312c242009-08-07 15:41:51 -0700187
188 if (period == 0) {
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700189 skip = 0;
Johannes Berge312c242009-08-07 15:41:51 -0700190 period = 1;
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700191 for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
192 max_sleep[i] = 1;
193
Johannes Berge312c242009-08-07 15:41:51 -0700194 } else {
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700195 skip = table[lvl].no_dtim;
196 for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
197 max_sleep[i] = le32_to_cpu(cmd->sleep_interval[i]);
198 max_sleep[IWL_POWER_VEC_SIZE - 1] = skip + 1;
Johannes Berge312c242009-08-07 15:41:51 -0700199 }
200
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700201 slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
202 /* figure out the listen interval based on dtim period and skip */
203 if (slp_itrvl == 0xFF)
204 cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
205 cpu_to_le32(period * (skip + 1));
206
207 slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
208 if (slp_itrvl > period)
209 cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
210 cpu_to_le32((slp_itrvl / period) * period);
211
212 if (skip)
Johannes Berge312c242009-08-07 15:41:51 -0700213 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700214 else
Johannes Berge312c242009-08-07 15:41:51 -0700215 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
Johannes Berge312c242009-08-07 15:41:51 -0700216
Wey-Yi Guy1f37daf2010-11-23 10:58:55 -0800217 if (priv->cfg->base_params->shadow_reg_enable)
218 cmd->flags |= IWL_POWER_SHADOW_REG_ENA;
219 else
220 cmd->flags &= ~IWL_POWER_SHADOW_REG_ENA;
221
Stanislaw Gruszka88e58fc2011-01-28 16:47:47 +0100222 if (iwl_advanced_bt_coexist(priv)) {
Wey-Yi Guye3661762010-11-23 10:58:54 -0800223 if (!priv->cfg->bt_params->bt_sco_disable)
224 cmd->flags |= IWL_POWER_BT_SCO_ENA;
225 else
226 cmd->flags &= ~IWL_POWER_BT_SCO_ENA;
227 }
228
229
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700230 slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
Wey-Yi Guy570af862009-10-23 13:42:33 -0700231 if (slp_itrvl > IWL_CONN_MAX_LISTEN_INTERVAL)
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700232 cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
Wey-Yi Guy570af862009-10-23 13:42:33 -0700233 cpu_to_le32(IWL_CONN_MAX_LISTEN_INTERVAL);
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700234
235 /* enforce max sleep interval */
236 for (i = IWL_POWER_VEC_SIZE - 1; i >= 0 ; i--) {
237 if (le32_to_cpu(cmd->sleep_interval[i]) >
238 (max_sleep[i] * period))
239 cmd->sleep_interval[i] =
240 cpu_to_le32(max_sleep[i] * period);
241 if (i != (IWL_POWER_VEC_SIZE - 1)) {
242 if (le32_to_cpu(cmd->sleep_interval[i]) >
243 le32_to_cpu(cmd->sleep_interval[i+1]))
244 cmd->sleep_interval[i] =
245 cmd->sleep_interval[i+1];
246 }
247 }
Johannes Berge312c242009-08-07 15:41:51 -0700248
Wey-Yi Guyd57fa992011-06-10 11:23:36 -0700249 if (priv->power_data.bus_pm)
Johannes Berge312c242009-08-07 15:41:51 -0700250 cmd->flags |= IWL_POWER_PCI_PM_MSK;
251 else
252 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
253
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700254 IWL_DEBUG_POWER(priv, "numSkipDtim = %u, dtimPeriod = %d\n",
255 skip, period);
Johannes Berge312c242009-08-07 15:41:51 -0700256 IWL_DEBUG_POWER(priv, "Sleep command for index %d\n", lvl + 1);
257}
258
Johannes Berge312c242009-08-07 15:41:51 -0700259static void iwl_power_sleep_cam_cmd(struct iwl_priv *priv,
260 struct iwl_powertable_cmd *cmd)
Mohamed Abbasca579612008-07-18 13:52:57 +0800261{
Johannes Berge312c242009-08-07 15:41:51 -0700262 memset(cmd, 0, sizeof(*cmd));
263
Wey-Yi Guyd57fa992011-06-10 11:23:36 -0700264 if (priv->power_data.bus_pm)
Johannes Berge312c242009-08-07 15:41:51 -0700265 cmd->flags |= IWL_POWER_PCI_PM_MSK;
266
267 IWL_DEBUG_POWER(priv, "Sleep command for CAM\n");
Mohamed Abbasca579612008-07-18 13:52:57 +0800268}
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700269
Johannes Berge312c242009-08-07 15:41:51 -0700270static void iwl_power_fill_sleep_cmd(struct iwl_priv *priv,
271 struct iwl_powertable_cmd *cmd,
272 int dynps_ms, int wakeup_period)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700273{
Johannes Berg4c561a02009-08-28 09:44:48 -0700274 /*
275 * These are the original power level 3 sleep successions. The
276 * device may behave better with such succession and was also
277 * only tested with that. Just like the original sleep commands,
278 * also adjust the succession here to the wakeup_period below.
279 * The ranges are the same as for the sleep commands, 0-2, 3-9
280 * and >10, which is selected based on the DTIM interval for
281 * the sleep index but here we use the wakeup period since that
282 * is what we need to do for the latency requirements.
283 */
284 static const u8 slp_succ_r0[IWL_POWER_VEC_SIZE] = { 2, 2, 2, 2, 2 };
285 static const u8 slp_succ_r1[IWL_POWER_VEC_SIZE] = { 2, 4, 6, 7, 9 };
286 static const u8 slp_succ_r2[IWL_POWER_VEC_SIZE] = { 2, 7, 9, 9, 0xFF };
287 const u8 *slp_succ = slp_succ_r0;
Tomas Winklere7b63582008-09-03 11:26:49 +0800288 int i;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700289
Johannes Berg4c561a02009-08-28 09:44:48 -0700290 if (wakeup_period > IWL_DTIM_RANGE_0_MAX)
291 slp_succ = slp_succ_r1;
292 if (wakeup_period > IWL_DTIM_RANGE_1_MAX)
293 slp_succ = slp_succ_r2;
294
Johannes Berge312c242009-08-07 15:41:51 -0700295 memset(cmd, 0, sizeof(*cmd));
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700296
Johannes Berge312c242009-08-07 15:41:51 -0700297 cmd->flags = IWL_POWER_DRIVER_ALLOW_SLEEP_MSK |
298 IWL_POWER_FAST_PD; /* no use seeing frames for others */
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700299
Wey-Yi Guyd57fa992011-06-10 11:23:36 -0700300 if (priv->power_data.bus_pm)
Johannes Berge312c242009-08-07 15:41:51 -0700301 cmd->flags |= IWL_POWER_PCI_PM_MSK;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700302
Wey-Yi Guy1f37daf2010-11-23 10:58:55 -0800303 if (priv->cfg->base_params->shadow_reg_enable)
304 cmd->flags |= IWL_POWER_SHADOW_REG_ENA;
305 else
306 cmd->flags &= ~IWL_POWER_SHADOW_REG_ENA;
307
Stanislaw Gruszka88e58fc2011-01-28 16:47:47 +0100308 if (iwl_advanced_bt_coexist(priv)) {
Wey-Yi Guye3661762010-11-23 10:58:54 -0800309 if (!priv->cfg->bt_params->bt_sco_disable)
310 cmd->flags |= IWL_POWER_BT_SCO_ENA;
311 else
312 cmd->flags &= ~IWL_POWER_BT_SCO_ENA;
313 }
314
Johannes Berge312c242009-08-07 15:41:51 -0700315 cmd->rx_data_timeout = cpu_to_le32(1000 * dynps_ms);
316 cmd->tx_data_timeout = cpu_to_le32(1000 * dynps_ms);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700317
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800318 for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
Johannes Berg4c561a02009-08-28 09:44:48 -0700319 cmd->sleep_interval[i] =
320 cpu_to_le32(min_t(int, slp_succ[i], wakeup_period));
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700321
Johannes Berge312c242009-08-07 15:41:51 -0700322 IWL_DEBUG_POWER(priv, "Automatic sleep command\n");
323}
324
325static int iwl_set_power(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd)
326{
327 IWL_DEBUG_POWER(priv, "Sending power/sleep command\n");
Tomas Winklere1623442009-01-27 14:27:56 -0800328 IWL_DEBUG_POWER(priv, "Flags value = 0x%08X\n", cmd->flags);
329 IWL_DEBUG_POWER(priv, "Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
330 IWL_DEBUG_POWER(priv, "Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
331 IWL_DEBUG_POWER(priv, "Sleep interval vector = { %d , %d , %d , %d , %d }\n",
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700332 le32_to_cpu(cmd->sleep_interval[0]),
333 le32_to_cpu(cmd->sleep_interval[1]),
334 le32_to_cpu(cmd->sleep_interval[2]),
335 le32_to_cpu(cmd->sleep_interval[3]),
336 le32_to_cpu(cmd->sleep_interval[4]));
337
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -0700338 return trans_send_cmd_pdu(priv, POWER_TABLE_CMD, CMD_SYNC,
Johannes Berge312c242009-08-07 15:41:51 -0700339 sizeof(struct iwl_powertable_cmd), cmd);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700340}
341
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200342static void iwl_power_build_cmd(struct iwl_priv *priv,
343 struct iwl_powertable_cmd *cmd)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700344{
Johannes Berg4d695922010-01-21 05:24:04 -0800345 bool enabled = priv->hw->conf.flags & IEEE80211_CONF_PS;
Johannes Berge312c242009-08-07 15:41:51 -0700346 int dtimper;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700347
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200348 dtimper = priv->hw->conf.ps_dtim_period ?: 1;
349
Wey-Yi Guy23c0fcc2011-04-01 16:29:53 -0700350 if (priv->hw->conf.flags & IEEE80211_CONF_IDLE)
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200351 iwl_static_sleep_cmd(priv, cmd, IWL_POWER_INDEX_5, 20);
Wey-Yi Guyf42e7662011-04-18 09:30:09 -0700352 else if (iwl_tt_is_low_power_state(priv)) {
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200353 /* in thermal throttling low power state */
354 iwl_static_sleep_cmd(priv, cmd,
Wey-Yi Guyf42e7662011-04-18 09:30:09 -0700355 iwl_tt_current_power_mode(priv), dtimper);
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200356 } else if (!enabled)
357 iwl_power_sleep_cam_cmd(priv, cmd);
358 else if (priv->power_data.debug_sleep_level_override >= 0)
359 iwl_static_sleep_cmd(priv, cmd,
360 priv->power_data.debug_sleep_level_override,
361 dtimper);
Wey-Yi Guyf7538162011-06-11 10:00:06 -0700362 else if (iwlagn_mod_params.no_sleep_autoadjust) {
363 if (iwlagn_mod_params.power_level > IWL_POWER_INDEX_1 &&
364 iwlagn_mod_params.power_level <= IWL_POWER_INDEX_5)
365 iwl_static_sleep_cmd(priv, cmd,
366 iwlagn_mod_params.power_level, dtimper);
367 else
368 iwl_static_sleep_cmd(priv, cmd,
369 IWL_POWER_INDEX_1, dtimper);
370 } else
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200371 iwl_power_fill_sleep_cmd(priv, cmd,
372 priv->hw->conf.dynamic_ps_timeout,
373 priv->hw->conf.max_sleep_period);
374}
375
376int iwl_power_set_mode(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd,
377 bool force)
378{
379 int ret;
380 bool update_chains;
381
382 lockdep_assert_held(&priv->mutex);
383
Grumbach, Emmanuel04816442008-09-03 11:26:53 +0800384 /* Don't update the RX chain when chain noise calibration is running */
Winkler, Tomasa71c8f62008-11-07 09:58:37 -0800385 update_chains = priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE ||
386 priv->chain_noise_data.state == IWL_CHAIN_NOISE_ALIVE;
Grumbach, Emmanuel04816442008-09-03 11:26:53 +0800387
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200388 if (!memcmp(&priv->power_data.sleep_cmd, cmd, sizeof(*cmd)) && !force)
389 return 0;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700390
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200391 if (!iwl_is_ready_rf(priv))
392 return -EIO;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700393
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200394 /* scan complete use sleep_power_next, need to be updated */
395 memcpy(&priv->power_data.sleep_cmd_next, cmd, sizeof(*cmd));
396 if (test_bit(STATUS_SCANNING, &priv->status) && !force) {
397 IWL_DEBUG_INFO(priv, "Defer power set mode while scanning\n");
398 return 0;
399 }
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700400
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200401 if (cmd->flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK)
402 set_bit(STATUS_POWER_PMI, &priv->status);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700403
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200404 ret = iwl_set_power(priv, cmd);
405 if (!ret) {
406 if (!(cmd->flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK))
407 clear_bit(STATUS_POWER_PMI, &priv->status);
408
409 if (priv->cfg->ops->lib->update_chain_flags && update_chains)
410 priv->cfg->ops->lib->update_chain_flags(priv);
411 else if (priv->cfg->ops->lib->update_chain_flags)
412 IWL_DEBUG_POWER(priv,
Wey-Yi Guy3a780d22009-08-07 15:41:47 -0700413 "Cannot update the power, chain noise "
Winkler, Tomasa71c8f62008-11-07 09:58:37 -0800414 "calibration running: %d\n",
415 priv->chain_noise_data.state);
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200416
417 memcpy(&priv->power_data.sleep_cmd, cmd, sizeof(*cmd));
418 } else
419 IWL_ERR(priv, "set power fail, ret = %d", ret);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700420
421 return ret;
422}
Stanislaw Gruszkaac4f5452010-10-22 17:04:29 +0200423
424int iwl_power_update_mode(struct iwl_priv *priv, bool force)
425{
426 struct iwl_powertable_cmd cmd;
427
428 iwl_power_build_cmd(priv, &cmd);
429 return iwl_power_set_mode(priv, &cmd, force);
430}
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700431
Tomas Winklera96a27f2008-10-23 23:48:56 -0700432/* initialize to default */
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700433void iwl_power_initialize(struct iwl_priv *priv)
434{
Wey-Yi Guyd57fa992011-06-10 11:23:36 -0700435 priv->power_data.bus_pm = priv->bus.ops->get_pm_support(&priv->bus);
Johannes Berge312c242009-08-07 15:41:51 -0700436
437 priv->power_data.debug_sleep_level_override = -1;
438
439 memset(&priv->power_data.sleep_cmd, 0,
440 sizeof(priv->power_data.sleep_cmd));
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700441}