blob: 1a1a9f081cc712288b552e49f667601bc7e5d5a9 [file] [log] [blame]
Mohamed Abbas5da4b552008-04-21 15:41:51 -07001/******************************************************************************
2 *
Reinette Chatre1f447802010-01-15 13:43:41 -08003 * Copyright(c) 2007 - 2010 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>
Mohamed Abbas5da4b552008-04-21 15:41:51 -070032#include <linux/init.h>
33
34#include <net/mac80211.h>
35
36#include "iwl-eeprom.h"
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070037#include "iwl-dev.h"
Mohamed Abbas5da4b552008-04-21 15:41:51 -070038#include "iwl-core.h"
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -070039#include "iwl-io.h"
Tomas Winkler5a36ba02008-04-24 11:55:37 -070040#include "iwl-commands.h"
Mohamed Abbas5da4b552008-04-21 15:41:51 -070041#include "iwl-debug.h"
42#include "iwl-power.h"
Mohamed Abbas5da4b552008-04-21 15:41:51 -070043
44/*
Johannes Berge312c242009-08-07 15:41:51 -070045 * Setting power level allows the card to go to sleep when not busy.
Mohamed Abbas5da4b552008-04-21 15:41:51 -070046 *
Johannes Berge312c242009-08-07 15:41:51 -070047 * We calculate a sleep command based on the required latency, which
48 * we get from mac80211. In order to handle thermal throttling, we can
49 * also use pre-defined power levels.
Mohamed Abbas5da4b552008-04-21 15:41:51 -070050 */
51
Johannes Berge312c242009-08-07 15:41:51 -070052/*
53 * For now, keep using power level 1 instead of automatically
54 * adjusting ...
55 */
56bool no_sleep_autoadjust = true;
57module_param(no_sleep_autoadjust, bool, S_IRUGO);
58MODULE_PARM_DESC(no_sleep_autoadjust,
59 "don't automatically adjust sleep level "
60 "according to maximum network latency");
Mohamed Abbas5da4b552008-04-21 15:41:51 -070061
Johannes Berge312c242009-08-07 15:41:51 -070062/*
63 * This defines the old power levels. They are still used by default
64 * (level 1) and for thermal throttle (levels 3 through 5)
65 */
66
67struct iwl_power_vec_entry {
68 struct iwl_powertable_cmd cmd;
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -070069 u8 no_dtim; /* number of skip dtim */
Johannes Berge312c242009-08-07 15:41:51 -070070};
71
72#define IWL_DTIM_RANGE_0_MAX 2
73#define IWL_DTIM_RANGE_1_MAX 10
Mohamed Abbas5da4b552008-04-21 15:41:51 -070074
Johannes Berg7af2c462009-05-08 13:44:38 -070075#define NOSLP cpu_to_le16(0), 0, 0
76#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
77#define TU_TO_USEC 1024
78#define SLP_TOUT(T) cpu_to_le32((T) * TU_TO_USEC)
79#define SLP_VEC(X0, X1, X2, X3, X4) {cpu_to_le32(X0), \
80 cpu_to_le32(X1), \
81 cpu_to_le32(X2), \
82 cpu_to_le32(X3), \
83 cpu_to_le32(X4)}
Mohamed Abbas5da4b552008-04-21 15:41:51 -070084/* default power management (not Tx power) table values */
Johannes Berge312c242009-08-07 15:41:51 -070085/* for DTIM period 0 through IWL_DTIM_RANGE_0_MAX */
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -070086/* DTIM 0 - 2 */
Johannes Berg7af2c462009-05-08 13:44:38 -070087static const struct iwl_power_vec_entry range_0[IWL_POWER_NUM] = {
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -070088 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 1, 2, 2, 0xFF)}, 0},
Mohamed Abbas5da4b552008-04-21 15:41:51 -070089 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
90 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 2, 2, 2, 0xFF)}, 0},
91 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 2, 4, 4, 0xFF)}, 1},
92 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 2, 4, 6, 0xFF)}, 2}
93};
94
95
Johannes Berge312c242009-08-07 15:41:51 -070096/* for DTIM period IWL_DTIM_RANGE_0_MAX + 1 through IWL_DTIM_RANGE_1_MAX */
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -070097/* DTIM 3 - 10 */
Johannes Berg7af2c462009-05-08 13:44:38 -070098static const struct iwl_power_vec_entry range_1[IWL_POWER_NUM] = {
Mohamed Abbas5da4b552008-04-21 15:41:51 -070099 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
100 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 3, 4, 7)}, 0},
101 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 4, 6, 7, 9)}, 0},
102 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 4, 6, 9, 10)}, 1},
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700103 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 4, 6, 10, 10)}, 2}
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700104};
105
Johannes Berge312c242009-08-07 15:41:51 -0700106/* for DTIM period > IWL_DTIM_RANGE_1_MAX */
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700107/* DTIM 11 - */
Johannes Berg7af2c462009-05-08 13:44:38 -0700108static const struct iwl_power_vec_entry range_2[IWL_POWER_NUM] = {
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700109 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
110 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
111 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
112 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
113 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
114};
115
Johannes Berge312c242009-08-07 15:41:51 -0700116static void iwl_static_sleep_cmd(struct iwl_priv *priv,
117 struct iwl_powertable_cmd *cmd,
118 enum iwl_power_level lvl, int period)
119{
120 const struct iwl_power_vec_entry *table;
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700121 int max_sleep[IWL_POWER_VEC_SIZE] = { 0 };
122 int i;
123 u8 skip;
124 u32 slp_itrvl;
Johannes Berge312c242009-08-07 15:41:51 -0700125
126 table = range_2;
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700127 if (period <= IWL_DTIM_RANGE_1_MAX)
Johannes Berge312c242009-08-07 15:41:51 -0700128 table = range_1;
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700129 if (period <= IWL_DTIM_RANGE_0_MAX)
Johannes Berge312c242009-08-07 15:41:51 -0700130 table = range_0;
131
132 BUG_ON(lvl < 0 || lvl >= IWL_POWER_NUM);
133
134 *cmd = table[lvl].cmd;
135
136 if (period == 0) {
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700137 skip = 0;
Johannes Berge312c242009-08-07 15:41:51 -0700138 period = 1;
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700139 for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
140 max_sleep[i] = 1;
141
Johannes Berge312c242009-08-07 15:41:51 -0700142 } else {
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700143 skip = table[lvl].no_dtim;
144 for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
145 max_sleep[i] = le32_to_cpu(cmd->sleep_interval[i]);
146 max_sleep[IWL_POWER_VEC_SIZE - 1] = skip + 1;
Johannes Berge312c242009-08-07 15:41:51 -0700147 }
148
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700149 slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
150 /* figure out the listen interval based on dtim period and skip */
151 if (slp_itrvl == 0xFF)
152 cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
153 cpu_to_le32(period * (skip + 1));
154
155 slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
156 if (slp_itrvl > period)
157 cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
158 cpu_to_le32((slp_itrvl / period) * period);
159
160 if (skip)
Johannes Berge312c242009-08-07 15:41:51 -0700161 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700162 else
Johannes Berge312c242009-08-07 15:41:51 -0700163 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
Johannes Berge312c242009-08-07 15:41:51 -0700164
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700165 slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
Wey-Yi Guy570af862009-10-23 13:42:33 -0700166 if (slp_itrvl > IWL_CONN_MAX_LISTEN_INTERVAL)
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700167 cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
Wey-Yi Guy570af862009-10-23 13:42:33 -0700168 cpu_to_le32(IWL_CONN_MAX_LISTEN_INTERVAL);
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700169
170 /* enforce max sleep interval */
171 for (i = IWL_POWER_VEC_SIZE - 1; i >= 0 ; i--) {
172 if (le32_to_cpu(cmd->sleep_interval[i]) >
173 (max_sleep[i] * period))
174 cmd->sleep_interval[i] =
175 cpu_to_le32(max_sleep[i] * period);
176 if (i != (IWL_POWER_VEC_SIZE - 1)) {
177 if (le32_to_cpu(cmd->sleep_interval[i]) >
178 le32_to_cpu(cmd->sleep_interval[i+1]))
179 cmd->sleep_interval[i] =
180 cmd->sleep_interval[i+1];
181 }
182 }
Johannes Berge312c242009-08-07 15:41:51 -0700183
184 if (priv->power_data.pci_pm)
185 cmd->flags |= IWL_POWER_PCI_PM_MSK;
186 else
187 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
188
Wey-Yi Guy4ad177b2009-10-16 14:25:58 -0700189 IWL_DEBUG_POWER(priv, "numSkipDtim = %u, dtimPeriod = %d\n",
190 skip, period);
Johannes Berge312c242009-08-07 15:41:51 -0700191 IWL_DEBUG_POWER(priv, "Sleep command for index %d\n", lvl + 1);
192}
193
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700194/* default Thermal Throttling transaction table
195 * Current state | Throttling Down | Throttling Up
196 *=============================================================================
197 * Condition Nxt State Condition Nxt State Condition Nxt State
198 *-----------------------------------------------------------------------------
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700199 * IWL_TI_0 T >= 114 CT_KILL 114>T>=105 TI_1 N/A N/A
200 * IWL_TI_1 T >= 114 CT_KILL 114>T>=110 TI_2 T<=95 TI_0
201 * IWL_TI_2 T >= 114 CT_KILL T<=100 TI_1
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700202 * IWL_CT_KILL N/A N/A N/A N/A T<=95 TI_0
203 *=============================================================================
204 */
205static const struct iwl_tt_trans tt_range_0[IWL_TI_STATE_MAX - 1] = {
206 {IWL_TI_0, IWL_ABSOLUTE_ZERO, 104},
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700207 {IWL_TI_1, 105, CT_KILL_THRESHOLD - 1},
208 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700209};
210static const struct iwl_tt_trans tt_range_1[IWL_TI_STATE_MAX - 1] = {
211 {IWL_TI_0, IWL_ABSOLUTE_ZERO, 95},
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700212 {IWL_TI_2, 110, CT_KILL_THRESHOLD - 1},
213 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700214};
215static const struct iwl_tt_trans tt_range_2[IWL_TI_STATE_MAX - 1] = {
216 {IWL_TI_1, IWL_ABSOLUTE_ZERO, 100},
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700217 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX},
218 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700219};
220static const struct iwl_tt_trans tt_range_3[IWL_TI_STATE_MAX - 1] = {
221 {IWL_TI_0, IWL_ABSOLUTE_ZERO, CT_KILL_EXIT_THRESHOLD},
222 {IWL_TI_CT_KILL, CT_KILL_EXIT_THRESHOLD + 1, IWL_ABSOLUTE_MAX},
223 {IWL_TI_CT_KILL, CT_KILL_EXIT_THRESHOLD + 1, IWL_ABSOLUTE_MAX}
224};
225
226/* Advance Thermal Throttling default restriction table */
227static const struct iwl_tt_restriction restriction_range[IWL_TI_STATE_MAX] = {
Johannes Berg3ad3b922009-08-07 15:41:48 -0700228 {IWL_ANT_OK_MULTI, IWL_ANT_OK_MULTI, true },
229 {IWL_ANT_OK_SINGLE, IWL_ANT_OK_MULTI, true },
230 {IWL_ANT_OK_SINGLE, IWL_ANT_OK_SINGLE, false },
231 {IWL_ANT_OK_NONE, IWL_ANT_OK_NONE, false }
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700232};
Winkler, Tomasd25aabb2009-01-27 14:27:58 -0800233
Johannes Berge312c242009-08-07 15:41:51 -0700234
235static void iwl_power_sleep_cam_cmd(struct iwl_priv *priv,
236 struct iwl_powertable_cmd *cmd)
Mohamed Abbasca579612008-07-18 13:52:57 +0800237{
Johannes Berge312c242009-08-07 15:41:51 -0700238 memset(cmd, 0, sizeof(*cmd));
239
240 if (priv->power_data.pci_pm)
241 cmd->flags |= IWL_POWER_PCI_PM_MSK;
242
243 IWL_DEBUG_POWER(priv, "Sleep command for CAM\n");
Mohamed Abbasca579612008-07-18 13:52:57 +0800244}
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700245
Johannes Berge312c242009-08-07 15:41:51 -0700246static void iwl_power_fill_sleep_cmd(struct iwl_priv *priv,
247 struct iwl_powertable_cmd *cmd,
248 int dynps_ms, int wakeup_period)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700249{
Johannes Berg4c561a02009-08-28 09:44:48 -0700250 /*
251 * These are the original power level 3 sleep successions. The
252 * device may behave better with such succession and was also
253 * only tested with that. Just like the original sleep commands,
254 * also adjust the succession here to the wakeup_period below.
255 * The ranges are the same as for the sleep commands, 0-2, 3-9
256 * and >10, which is selected based on the DTIM interval for
257 * the sleep index but here we use the wakeup period since that
258 * is what we need to do for the latency requirements.
259 */
260 static const u8 slp_succ_r0[IWL_POWER_VEC_SIZE] = { 2, 2, 2, 2, 2 };
261 static const u8 slp_succ_r1[IWL_POWER_VEC_SIZE] = { 2, 4, 6, 7, 9 };
262 static const u8 slp_succ_r2[IWL_POWER_VEC_SIZE] = { 2, 7, 9, 9, 0xFF };
263 const u8 *slp_succ = slp_succ_r0;
Tomas Winklere7b63582008-09-03 11:26:49 +0800264 int i;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700265
Johannes Berg4c561a02009-08-28 09:44:48 -0700266 if (wakeup_period > IWL_DTIM_RANGE_0_MAX)
267 slp_succ = slp_succ_r1;
268 if (wakeup_period > IWL_DTIM_RANGE_1_MAX)
269 slp_succ = slp_succ_r2;
270
Johannes Berge312c242009-08-07 15:41:51 -0700271 memset(cmd, 0, sizeof(*cmd));
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700272
Johannes Berge312c242009-08-07 15:41:51 -0700273 cmd->flags = IWL_POWER_DRIVER_ALLOW_SLEEP_MSK |
274 IWL_POWER_FAST_PD; /* no use seeing frames for others */
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700275
Johannes Berge312c242009-08-07 15:41:51 -0700276 if (priv->power_data.pci_pm)
277 cmd->flags |= IWL_POWER_PCI_PM_MSK;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700278
Johannes Berge312c242009-08-07 15:41:51 -0700279 cmd->rx_data_timeout = cpu_to_le32(1000 * dynps_ms);
280 cmd->tx_data_timeout = cpu_to_le32(1000 * dynps_ms);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700281
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800282 for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
Johannes Berg4c561a02009-08-28 09:44:48 -0700283 cmd->sleep_interval[i] =
284 cpu_to_le32(min_t(int, slp_succ[i], wakeup_period));
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700285
Johannes Berge312c242009-08-07 15:41:51 -0700286 IWL_DEBUG_POWER(priv, "Automatic sleep command\n");
287}
288
289static int iwl_set_power(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd)
290{
291 IWL_DEBUG_POWER(priv, "Sending power/sleep command\n");
Tomas Winklere1623442009-01-27 14:27:56 -0800292 IWL_DEBUG_POWER(priv, "Flags value = 0x%08X\n", cmd->flags);
293 IWL_DEBUG_POWER(priv, "Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
294 IWL_DEBUG_POWER(priv, "Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
295 IWL_DEBUG_POWER(priv, "Sleep interval vector = { %d , %d , %d , %d , %d }\n",
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700296 le32_to_cpu(cmd->sleep_interval[0]),
297 le32_to_cpu(cmd->sleep_interval[1]),
298 le32_to_cpu(cmd->sleep_interval[2]),
299 le32_to_cpu(cmd->sleep_interval[3]),
300 le32_to_cpu(cmd->sleep_interval[4]));
301
Johannes Berge312c242009-08-07 15:41:51 -0700302 return iwl_send_cmd_pdu(priv, POWER_TABLE_CMD,
303 sizeof(struct iwl_powertable_cmd), cmd);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700304}
305
Reinette Chatred3a57192010-01-21 11:52:28 -0800306/* priv->mutex must be held */
Grumbach, Emmanuel04816442008-09-03 11:26:53 +0800307int iwl_power_update_mode(struct iwl_priv *priv, bool force)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700308{
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700309 int ret = 0;
Johannes Berg3ad3b922009-08-07 15:41:48 -0700310 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
Johannes Berg4d695922010-01-21 05:24:04 -0800311 bool enabled = priv->hw->conf.flags & IEEE80211_CONF_PS;
Winkler, Tomasa71c8f62008-11-07 09:58:37 -0800312 bool update_chains;
Johannes Berge312c242009-08-07 15:41:51 -0700313 struct iwl_powertable_cmd cmd;
314 int dtimper;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700315
Grumbach, Emmanuel04816442008-09-03 11:26:53 +0800316 /* Don't update the RX chain when chain noise calibration is running */
Winkler, Tomasa71c8f62008-11-07 09:58:37 -0800317 update_chains = priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE ||
318 priv->chain_noise_data.state == IWL_CHAIN_NOISE_ALIVE;
Grumbach, Emmanuel04816442008-09-03 11:26:53 +0800319
Johannes Berge312c242009-08-07 15:41:51 -0700320 if (priv->vif)
Johannes Berg56007a02010-01-26 14:19:52 +0100321 dtimper = priv->hw->conf.ps_dtim_period;
Johannes Berge312c242009-08-07 15:41:51 -0700322 else
323 dtimper = 1;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700324
Johannes Berg559a4742009-09-11 10:50:37 -0700325 if (priv->cfg->broken_powersave)
326 iwl_power_sleep_cam_cmd(priv, &cmd);
Johannes Berg78f5fb72009-09-25 14:24:27 -0700327 else if (priv->cfg->supports_idle &&
328 priv->hw->conf.flags & IEEE80211_CONF_IDLE)
329 iwl_static_sleep_cmd(priv, &cmd, IWL_POWER_INDEX_5, 20);
Johannes Berg559a4742009-09-11 10:50:37 -0700330 else if (tt->state >= IWL_TI_1)
Johannes Berge312c242009-08-07 15:41:51 -0700331 iwl_static_sleep_cmd(priv, &cmd, tt->tt_power_mode, dtimper);
332 else if (!enabled)
333 iwl_power_sleep_cam_cmd(priv, &cmd);
334 else if (priv->power_data.debug_sleep_level_override >= 0)
335 iwl_static_sleep_cmd(priv, &cmd,
336 priv->power_data.debug_sleep_level_override,
337 dtimper);
338 else if (no_sleep_autoadjust)
339 iwl_static_sleep_cmd(priv, &cmd, IWL_POWER_INDEX_1, dtimper);
340 else
341 iwl_power_fill_sleep_cmd(priv, &cmd,
342 priv->hw->conf.dynamic_ps_timeout,
343 priv->hw->conf.max_sleep_period);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700344
Johannes Berg7af2c462009-05-08 13:44:38 -0700345 if (iwl_is_ready_rf(priv) &&
Johannes Berge312c242009-08-07 15:41:51 -0700346 (memcmp(&priv->power_data.sleep_cmd, &cmd, sizeof(cmd)) || force)) {
347 if (cmd.flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700348 set_bit(STATUS_POWER_PMI, &priv->status);
349
Mohamed Abbasca579612008-07-18 13:52:57 +0800350 ret = iwl_set_power(priv, &cmd);
Wey-Yi Guy3a780d22009-08-07 15:41:47 -0700351 if (!ret) {
Johannes Berge312c242009-08-07 15:41:51 -0700352 if (!(cmd.flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK))
Wey-Yi Guy3a780d22009-08-07 15:41:47 -0700353 clear_bit(STATUS_POWER_PMI, &priv->status);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700354
Wey-Yi Guy3a780d22009-08-07 15:41:47 -0700355 if (priv->cfg->ops->lib->update_chain_flags &&
356 update_chains)
357 priv->cfg->ops->lib->update_chain_flags(priv);
Reinette Chatreb57d46a2009-08-28 09:44:49 -0700358 else if (priv->cfg->ops->lib->update_chain_flags)
Wey-Yi Guy3a780d22009-08-07 15:41:47 -0700359 IWL_DEBUG_POWER(priv,
360 "Cannot update the power, chain noise "
Winkler, Tomasa71c8f62008-11-07 09:58:37 -0800361 "calibration running: %d\n",
362 priv->chain_noise_data.state);
Johannes Berge312c242009-08-07 15:41:51 -0700363 memcpy(&priv->power_data.sleep_cmd, &cmd, sizeof(cmd));
Wey-Yi Guy3a780d22009-08-07 15:41:47 -0700364 } else
365 IWL_ERR(priv, "set power fail, ret = %d", ret);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700366 }
367
368 return ret;
369}
370EXPORT_SYMBOL(iwl_power_update_mode);
371
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700372bool iwl_ht_enabled(struct iwl_priv *priv)
373{
Johannes Berg3ad3b922009-08-07 15:41:48 -0700374 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700375 struct iwl_tt_restriction *restriction;
376
Johannes Berg3ad3b922009-08-07 15:41:48 -0700377 if (!priv->thermal_throttle.advanced_tt)
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700378 return true;
379 restriction = tt->restriction + tt->state;
380 return restriction->is_ht;
381}
382EXPORT_SYMBOL(iwl_ht_enabled);
383
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700384bool iwl_within_ct_kill_margin(struct iwl_priv *priv)
385{
386 s32 temp = priv->temperature; /* degrees CELSIUS except 4965 */
387 bool within_margin = false;
388
389 if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
390 temp = KELVIN_TO_CELSIUS(priv->temperature);
391
392 if (!priv->thermal_throttle.advanced_tt)
393 within_margin = ((temp + IWL_TT_CT_KILL_MARGIN) >=
394 CT_KILL_THRESHOLD_LEGACY) ? true : false;
395 else
396 within_margin = ((temp + IWL_TT_CT_KILL_MARGIN) >=
397 CT_KILL_THRESHOLD) ? true : false;
398 return within_margin;
399}
400
Johannes Berg3ad3b922009-08-07 15:41:48 -0700401enum iwl_antenna_ok iwl_tx_ant_restriction(struct iwl_priv *priv)
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700402{
Johannes Berg3ad3b922009-08-07 15:41:48 -0700403 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700404 struct iwl_tt_restriction *restriction;
405
Johannes Berg3ad3b922009-08-07 15:41:48 -0700406 if (!priv->thermal_throttle.advanced_tt)
407 return IWL_ANT_OK_MULTI;
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700408 restriction = tt->restriction + tt->state;
409 return restriction->tx_stream;
410}
411EXPORT_SYMBOL(iwl_tx_ant_restriction);
412
Johannes Berg3ad3b922009-08-07 15:41:48 -0700413enum iwl_antenna_ok iwl_rx_ant_restriction(struct iwl_priv *priv)
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700414{
Johannes Berg3ad3b922009-08-07 15:41:48 -0700415 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700416 struct iwl_tt_restriction *restriction;
417
Johannes Berg3ad3b922009-08-07 15:41:48 -0700418 if (!priv->thermal_throttle.advanced_tt)
419 return IWL_ANT_OK_MULTI;
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700420 restriction = tt->restriction + tt->state;
421 return restriction->rx_stream;
422}
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700423
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700424#define CT_KILL_EXIT_DURATION (5) /* 5 seconds duration */
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700425#define CT_KILL_WAITING_DURATION (300) /* 300ms duration */
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700426
427/*
428 * toggle the bit to wake up uCode and check the temperature
429 * if the temperature is below CT, uCode will stay awake and send card
430 * state notification with CT_KILL bit clear to inform Thermal Throttling
431 * Management to change state. Otherwise, uCode will go back to sleep
432 * without doing anything, driver should continue the 5 seconds timer
433 * to wake up uCode for temperature check until temperature drop below CT
434 */
435static void iwl_tt_check_exit_ct_kill(unsigned long data)
436{
437 struct iwl_priv *priv = (struct iwl_priv *)data;
Johannes Berg3ad3b922009-08-07 15:41:48 -0700438 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700439 unsigned long flags;
440
441 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
442 return;
443
444 if (tt->state == IWL_TI_CT_KILL) {
Johannes Berg3ad3b922009-08-07 15:41:48 -0700445 if (priv->thermal_throttle.ct_kill_toggle) {
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700446 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
447 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Johannes Berg3ad3b922009-08-07 15:41:48 -0700448 priv->thermal_throttle.ct_kill_toggle = false;
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700449 } else {
450 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
451 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Johannes Berg3ad3b922009-08-07 15:41:48 -0700452 priv->thermal_throttle.ct_kill_toggle = true;
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700453 }
454 iwl_read32(priv, CSR_UCODE_DRV_GP1);
455 spin_lock_irqsave(&priv->reg_lock, flags);
456 if (!iwl_grab_nic_access(priv))
457 iwl_release_nic_access(priv);
458 spin_unlock_irqrestore(&priv->reg_lock, flags);
459
460 /* Reschedule the ct_kill timer to occur in
461 * CT_KILL_EXIT_DURATION seconds to ensure we get a
462 * thermal update */
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700463 IWL_DEBUG_POWER(priv, "schedule ct_kill exit timer\n");
Johannes Berg3ad3b922009-08-07 15:41:48 -0700464 mod_timer(&priv->thermal_throttle.ct_kill_exit_tm, jiffies +
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700465 CT_KILL_EXIT_DURATION * HZ);
466 }
467}
468
469static void iwl_perform_ct_kill_task(struct iwl_priv *priv,
470 bool stop)
471{
472 if (stop) {
473 IWL_DEBUG_POWER(priv, "Stop all queues\n");
474 if (priv->mac80211_registered)
475 ieee80211_stop_queues(priv->hw);
476 IWL_DEBUG_POWER(priv,
477 "Schedule 5 seconds CT_KILL Timer\n");
Johannes Berg3ad3b922009-08-07 15:41:48 -0700478 mod_timer(&priv->thermal_throttle.ct_kill_exit_tm, jiffies +
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700479 CT_KILL_EXIT_DURATION * HZ);
480 } else {
481 IWL_DEBUG_POWER(priv, "Wake all queues\n");
482 if (priv->mac80211_registered)
483 ieee80211_wake_queues(priv->hw);
484 }
485}
486
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700487static void iwl_tt_ready_for_ct_kill(unsigned long data)
488{
489 struct iwl_priv *priv = (struct iwl_priv *)data;
490 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
491
492 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
493 return;
494
495 /* temperature timer expired, ready to go into CT_KILL state */
496 if (tt->state != IWL_TI_CT_KILL) {
497 IWL_DEBUG_POWER(priv, "entering CT_KILL state when temperature timer expired\n");
498 tt->state = IWL_TI_CT_KILL;
499 set_bit(STATUS_CT_KILL, &priv->status);
500 iwl_perform_ct_kill_task(priv, true);
501 }
502}
503
504static void iwl_prepare_ct_kill_task(struct iwl_priv *priv)
505{
506 IWL_DEBUG_POWER(priv, "Prepare to enter IWL_TI_CT_KILL\n");
507 /* make request to retrieve statistics information */
Wey-Yi Guyef8d5522009-11-13 11:56:28 -0800508 iwl_send_statistics_request(priv, CMD_SYNC, false);
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700509 /* Reschedule the ct_kill wait timer */
510 mod_timer(&priv->thermal_throttle.ct_kill_waiting_tm,
511 jiffies + msecs_to_jiffies(CT_KILL_WAITING_DURATION));
512}
513
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700514#define IWL_MINIMAL_POWER_THRESHOLD (CT_KILL_THRESHOLD_LEGACY)
515#define IWL_REDUCED_PERFORMANCE_THRESHOLD_2 (100)
516#define IWL_REDUCED_PERFORMANCE_THRESHOLD_1 (90)
517
518/*
519 * Legacy thermal throttling
520 * 1) Avoid NIC destruction due to high temperatures
521 * Chip will identify dangerously high temperatures that can
522 * harm the device and will power down
523 * 2) Avoid the NIC power down due to high temperature
524 * Throttle early enough to lower the power consumption before
525 * drastic steps are needed
526 */
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700527static void iwl_legacy_tt_handler(struct iwl_priv *priv, s32 temp, bool force)
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700528{
Johannes Berg3ad3b922009-08-07 15:41:48 -0700529 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
Wey-Yi Guyee9f2982009-08-07 15:41:46 -0700530 enum iwl_tt_state old_state;
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700531
532#ifdef CONFIG_IWLWIFI_DEBUG
533 if ((tt->tt_previous_temp) &&
534 (temp > tt->tt_previous_temp) &&
535 ((temp - tt->tt_previous_temp) >
536 IWL_TT_INCREASE_MARGIN)) {
537 IWL_DEBUG_POWER(priv,
538 "Temperature increase %d degree Celsius\n",
539 (temp - tt->tt_previous_temp));
540 }
541#endif
Wey-Yi Guyee9f2982009-08-07 15:41:46 -0700542 old_state = tt->state;
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700543 /* in Celsius */
544 if (temp >= IWL_MINIMAL_POWER_THRESHOLD)
Wey-Yi Guyee9f2982009-08-07 15:41:46 -0700545 tt->state = IWL_TI_CT_KILL;
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700546 else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_2)
Wey-Yi Guyee9f2982009-08-07 15:41:46 -0700547 tt->state = IWL_TI_2;
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700548 else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_1)
Wey-Yi Guyee9f2982009-08-07 15:41:46 -0700549 tt->state = IWL_TI_1;
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700550 else
Wey-Yi Guyee9f2982009-08-07 15:41:46 -0700551 tt->state = IWL_TI_0;
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700552
553#ifdef CONFIG_IWLWIFI_DEBUG
554 tt->tt_previous_temp = temp;
555#endif
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700556 /* stop ct_kill_waiting_tm timer */
557 del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
Wey-Yi Guyee9f2982009-08-07 15:41:46 -0700558 if (tt->state != old_state) {
Wey-Yi Guyee9f2982009-08-07 15:41:46 -0700559 switch (tt->state) {
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700560 case IWL_TI_0:
Johannes Berge312c242009-08-07 15:41:51 -0700561 /*
562 * When the system is ready to go back to IWL_TI_0
563 * we only have to call iwl_power_update_mode() to
564 * do so.
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700565 */
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700566 break;
567 case IWL_TI_1:
568 tt->tt_power_mode = IWL_POWER_INDEX_3;
569 break;
570 case IWL_TI_2:
571 tt->tt_power_mode = IWL_POWER_INDEX_4;
572 break;
573 default:
574 tt->tt_power_mode = IWL_POWER_INDEX_5;
575 break;
576 }
Wey-Yi Guya28027cd2009-08-07 15:41:45 -0700577 mutex_lock(&priv->mutex);
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700578 if (old_state == IWL_TI_CT_KILL)
579 clear_bit(STATUS_CT_KILL, &priv->status);
580 if (tt->state != IWL_TI_CT_KILL &&
581 iwl_power_update_mode(priv, true)) {
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700582 /* TT state not updated
583 * try again during next temperature read
584 */
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700585 if (old_state == IWL_TI_CT_KILL)
586 set_bit(STATUS_CT_KILL, &priv->status);
Wey-Yi Guyee9f2982009-08-07 15:41:46 -0700587 tt->state = old_state;
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700588 IWL_ERR(priv, "Cannot update power mode, "
589 "TT state not updated\n");
590 } else {
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700591 if (tt->state == IWL_TI_CT_KILL) {
592 if (force) {
593 set_bit(STATUS_CT_KILL, &priv->status);
594 iwl_perform_ct_kill_task(priv, true);
595 } else {
596 iwl_prepare_ct_kill_task(priv);
597 tt->state = old_state;
598 }
599 } else if (old_state == IWL_TI_CT_KILL &&
Wey-Yi Guyee9f2982009-08-07 15:41:46 -0700600 tt->state != IWL_TI_CT_KILL)
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700601 iwl_perform_ct_kill_task(priv, false);
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700602 IWL_DEBUG_POWER(priv, "Temperature state changed %u\n",
603 tt->state);
604 IWL_DEBUG_POWER(priv, "Power Index change to %u\n",
605 tt->tt_power_mode);
606 }
Wey-Yi Guya28027cd2009-08-07 15:41:45 -0700607 mutex_unlock(&priv->mutex);
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700608 }
609}
610
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700611/*
612 * Advance thermal throttling
613 * 1) Avoid NIC destruction due to high temperatures
614 * Chip will identify dangerously high temperatures that can
615 * harm the device and will power down
616 * 2) Avoid the NIC power down due to high temperature
617 * Throttle early enough to lower the power consumption before
618 * drastic steps are needed
619 * Actions include relaxing the power down sleep thresholds and
620 * decreasing the number of TX streams
621 * 3) Avoid throughput performance impact as much as possible
622 *
623 *=============================================================================
624 * Condition Nxt State Condition Nxt State Condition Nxt State
625 *-----------------------------------------------------------------------------
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700626 * IWL_TI_0 T >= 114 CT_KILL 114>T>=105 TI_1 N/A N/A
627 * IWL_TI_1 T >= 114 CT_KILL 114>T>=110 TI_2 T<=95 TI_0
628 * IWL_TI_2 T >= 114 CT_KILL T<=100 TI_1
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700629 * IWL_CT_KILL N/A N/A N/A N/A T<=95 TI_0
630 *=============================================================================
631 */
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700632static void iwl_advance_tt_handler(struct iwl_priv *priv, s32 temp, bool force)
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700633{
Johannes Berg3ad3b922009-08-07 15:41:48 -0700634 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700635 int i;
636 bool changed = false;
637 enum iwl_tt_state old_state;
638 struct iwl_tt_trans *transaction;
639
640 old_state = tt->state;
641 for (i = 0; i < IWL_TI_STATE_MAX - 1; i++) {
642 /* based on the current TT state,
643 * find the curresponding transaction table
644 * each table has (IWL_TI_STATE_MAX - 1) entries
645 * tt->transaction + ((old_state * (IWL_TI_STATE_MAX - 1))
646 * will advance to the correct table.
647 * then based on the current temperature
648 * find the next state need to transaction to
649 * go through all the possible (IWL_TI_STATE_MAX - 1) entries
650 * in the current table to see if transaction is needed
651 */
652 transaction = tt->transaction +
653 ((old_state * (IWL_TI_STATE_MAX - 1)) + i);
654 if (temp >= transaction->tt_low &&
655 temp <= transaction->tt_high) {
656#ifdef CONFIG_IWLWIFI_DEBUG
657 if ((tt->tt_previous_temp) &&
658 (temp > tt->tt_previous_temp) &&
659 ((temp - tt->tt_previous_temp) >
660 IWL_TT_INCREASE_MARGIN)) {
661 IWL_DEBUG_POWER(priv,
662 "Temperature increase %d "
663 "degree Celsius\n",
664 (temp - tt->tt_previous_temp));
665 }
666 tt->tt_previous_temp = temp;
667#endif
668 if (old_state !=
669 transaction->next_state) {
670 changed = true;
671 tt->state =
672 transaction->next_state;
673 }
674 break;
675 }
676 }
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700677 /* stop ct_kill_waiting_tm timer */
678 del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700679 if (changed) {
680 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700681
682 if (tt->state >= IWL_TI_1) {
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700683 /* force PI = IWL_POWER_INDEX_5 in the case of TI > 0 */
684 tt->tt_power_mode = IWL_POWER_INDEX_5;
685 if (!iwl_ht_enabled(priv))
686 /* disable HT */
687 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
688 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
Wey-Yi Guy7aafef12009-08-07 15:41:38 -0700689 RXON_FLG_HT40_PROT_MSK |
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700690 RXON_FLG_HT_PROT_MSK);
691 else {
692 /* check HT capability and set
693 * according to the system HT capability
694 * in case get disabled before */
695 iwl_set_rxon_ht(priv, &priv->current_ht_config);
696 }
697
698 } else {
Johannes Berge312c242009-08-07 15:41:51 -0700699 /*
700 * restore system power setting -- it will be
701 * recalculated automatically.
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700702 */
Johannes Berge312c242009-08-07 15:41:51 -0700703
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700704 /* check HT capability and set
705 * according to the system HT capability
706 * in case get disabled before */
707 iwl_set_rxon_ht(priv, &priv->current_ht_config);
708 }
Wey-Yi Guya28027cd2009-08-07 15:41:45 -0700709 mutex_lock(&priv->mutex);
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700710 if (old_state == IWL_TI_CT_KILL)
711 clear_bit(STATUS_CT_KILL, &priv->status);
712 if (tt->state != IWL_TI_CT_KILL &&
713 iwl_power_update_mode(priv, true)) {
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700714 /* TT state not updated
715 * try again during next temperature read
716 */
717 IWL_ERR(priv, "Cannot update power mode, "
718 "TT state not updated\n");
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700719 if (old_state == IWL_TI_CT_KILL)
720 set_bit(STATUS_CT_KILL, &priv->status);
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700721 tt->state = old_state;
722 } else {
723 IWL_DEBUG_POWER(priv,
724 "Thermal Throttling to new state: %u\n",
725 tt->state);
726 if (old_state != IWL_TI_CT_KILL &&
727 tt->state == IWL_TI_CT_KILL) {
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700728 if (force) {
729 IWL_DEBUG_POWER(priv,
730 "Enter IWL_TI_CT_KILL\n");
731 set_bit(STATUS_CT_KILL, &priv->status);
732 iwl_perform_ct_kill_task(priv, true);
733 } else {
734 iwl_prepare_ct_kill_task(priv);
735 tt->state = old_state;
736 }
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700737 } else if (old_state == IWL_TI_CT_KILL &&
738 tt->state != IWL_TI_CT_KILL) {
739 IWL_DEBUG_POWER(priv, "Exit IWL_TI_CT_KILL\n");
740 iwl_perform_ct_kill_task(priv, false);
741 }
742 }
Wey-Yi Guya28027cd2009-08-07 15:41:45 -0700743 mutex_unlock(&priv->mutex);
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700744 }
745}
746
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700747/* Card State Notification indicated reach critical temperature
748 * if PSP not enable, no Thermal Throttling function will be performed
749 * just set the GP1 bit to acknowledge the event
750 * otherwise, go into IWL_TI_CT_KILL state
751 * since Card State Notification will not provide any temperature reading
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700752 * for Legacy mode
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700753 * so just pass the CT_KILL temperature to iwl_legacy_tt_handler()
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700754 * for advance mode
755 * pass CT_KILL_THRESHOLD+1 to make sure move into IWL_TI_CT_KILL state
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700756 */
Wey-Yi Guya28027cd2009-08-07 15:41:45 -0700757static void iwl_bg_ct_enter(struct work_struct *work)
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700758{
Wey-Yi Guya28027cd2009-08-07 15:41:45 -0700759 struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_enter);
Johannes Berg3ad3b922009-08-07 15:41:48 -0700760 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700761
762 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
763 return;
764
Wey-Yi Guya28027cd2009-08-07 15:41:45 -0700765 if (!iwl_is_ready(priv))
766 return;
767
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700768 if (tt->state != IWL_TI_CT_KILL) {
769 IWL_ERR(priv, "Device reached critical temperature "
770 "- ucode going to sleep!\n");
Johannes Berg3ad3b922009-08-07 15:41:48 -0700771 if (!priv->thermal_throttle.advanced_tt)
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700772 iwl_legacy_tt_handler(priv,
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700773 IWL_MINIMAL_POWER_THRESHOLD,
774 true);
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700775 else
776 iwl_advance_tt_handler(priv,
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700777 CT_KILL_THRESHOLD + 1, true);
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700778 }
779}
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700780
781/* Card State Notification indicated out of critical temperature
782 * since Card State Notification will not provide any temperature reading
783 * so pass the IWL_REDUCED_PERFORMANCE_THRESHOLD_2 temperature
784 * to iwl_legacy_tt_handler() to get out of IWL_CT_KILL state
785 */
Wey-Yi Guya28027cd2009-08-07 15:41:45 -0700786static void iwl_bg_ct_exit(struct work_struct *work)
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700787{
Wey-Yi Guya28027cd2009-08-07 15:41:45 -0700788 struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_exit);
Johannes Berg3ad3b922009-08-07 15:41:48 -0700789 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700790
791 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
792 return;
793
Wey-Yi Guya28027cd2009-08-07 15:41:45 -0700794 if (!iwl_is_ready(priv))
795 return;
796
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700797 /* stop ct_kill_exit_tm timer */
Johannes Berg3ad3b922009-08-07 15:41:48 -0700798 del_timer_sync(&priv->thermal_throttle.ct_kill_exit_tm);
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700799
800 if (tt->state == IWL_TI_CT_KILL) {
801 IWL_ERR(priv,
802 "Device temperature below critical"
803 "- ucode awake!\n");
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700804 /*
805 * exit from CT_KILL state
806 * reset the current temperature reading
807 */
808 priv->temperature = 0;
Johannes Berg3ad3b922009-08-07 15:41:48 -0700809 if (!priv->thermal_throttle.advanced_tt)
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700810 iwl_legacy_tt_handler(priv,
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700811 IWL_REDUCED_PERFORMANCE_THRESHOLD_2,
812 true);
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700813 else
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700814 iwl_advance_tt_handler(priv, CT_KILL_EXIT_THRESHOLD,
815 true);
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700816 }
817}
Wey-Yi Guya28027cd2009-08-07 15:41:45 -0700818
819void iwl_tt_enter_ct_kill(struct iwl_priv *priv)
820{
821 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
822 return;
823
824 IWL_DEBUG_POWER(priv, "Queueing critical temperature enter.\n");
825 queue_work(priv->workqueue, &priv->ct_enter);
826}
827EXPORT_SYMBOL(iwl_tt_enter_ct_kill);
828
829void iwl_tt_exit_ct_kill(struct iwl_priv *priv)
830{
831 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
832 return;
833
834 IWL_DEBUG_POWER(priv, "Queueing critical temperature exit.\n");
835 queue_work(priv->workqueue, &priv->ct_exit);
836}
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700837EXPORT_SYMBOL(iwl_tt_exit_ct_kill);
838
Wey-Yi Guya28027cd2009-08-07 15:41:45 -0700839static void iwl_bg_tt_work(struct work_struct *work)
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700840{
Wey-Yi Guya28027cd2009-08-07 15:41:45 -0700841 struct iwl_priv *priv = container_of(work, struct iwl_priv, tt_work);
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700842 s32 temp = priv->temperature; /* degrees CELSIUS except 4965 */
843
844 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
845 return;
846
847 if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
848 temp = KELVIN_TO_CELSIUS(priv->temperature);
849
Johannes Berg3ad3b922009-08-07 15:41:48 -0700850 if (!priv->thermal_throttle.advanced_tt)
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700851 iwl_legacy_tt_handler(priv, temp, false);
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700852 else
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700853 iwl_advance_tt_handler(priv, temp, false);
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700854}
Wey-Yi Guya28027cd2009-08-07 15:41:45 -0700855
856void iwl_tt_handler(struct iwl_priv *priv)
857{
858 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
859 return;
860
861 IWL_DEBUG_POWER(priv, "Queueing thermal throttling work.\n");
862 queue_work(priv->workqueue, &priv->tt_work);
863}
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700864EXPORT_SYMBOL(iwl_tt_handler);
865
866/* Thermal throttling initialization
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700867 * For advance thermal throttling:
868 * Initialize Thermal Index and temperature threshold table
869 * Initialize thermal throttling restriction table
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700870 */
871void iwl_tt_initialize(struct iwl_priv *priv)
872{
Johannes Berg3ad3b922009-08-07 15:41:48 -0700873 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700874 int size = sizeof(struct iwl_tt_trans) * (IWL_TI_STATE_MAX - 1);
875 struct iwl_tt_trans *transaction;
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700876
877 IWL_DEBUG_POWER(priv, "Initialize Thermal Throttling \n");
878
879 memset(tt, 0, sizeof(struct iwl_tt_mgmt));
880
881 tt->state = IWL_TI_0;
Johannes Berg3ad3b922009-08-07 15:41:48 -0700882 init_timer(&priv->thermal_throttle.ct_kill_exit_tm);
883 priv->thermal_throttle.ct_kill_exit_tm.data = (unsigned long)priv;
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700884 priv->thermal_throttle.ct_kill_exit_tm.function =
885 iwl_tt_check_exit_ct_kill;
886 init_timer(&priv->thermal_throttle.ct_kill_waiting_tm);
887 priv->thermal_throttle.ct_kill_waiting_tm.data = (unsigned long)priv;
888 priv->thermal_throttle.ct_kill_waiting_tm.function =
889 iwl_tt_ready_for_ct_kill;
Wey-Yi Guya28027cd2009-08-07 15:41:45 -0700890 /* setup deferred ct kill work */
891 INIT_WORK(&priv->tt_work, iwl_bg_tt_work);
892 INIT_WORK(&priv->ct_enter, iwl_bg_ct_enter);
893 INIT_WORK(&priv->ct_exit, iwl_bg_ct_exit);
894
Wey-Yi Guy6047b4f2009-10-23 13:42:27 -0700895 if (priv->cfg->adv_thermal_throttle) {
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700896 IWL_DEBUG_POWER(priv, "Advanced Thermal Throttling\n");
897 tt->restriction = kzalloc(sizeof(struct iwl_tt_restriction) *
898 IWL_TI_STATE_MAX, GFP_KERNEL);
899 tt->transaction = kzalloc(sizeof(struct iwl_tt_trans) *
900 IWL_TI_STATE_MAX * (IWL_TI_STATE_MAX - 1),
901 GFP_KERNEL);
902 if (!tt->restriction || !tt->transaction) {
903 IWL_ERR(priv, "Fallback to Legacy Throttling\n");
Johannes Berg3ad3b922009-08-07 15:41:48 -0700904 priv->thermal_throttle.advanced_tt = false;
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700905 kfree(tt->restriction);
906 tt->restriction = NULL;
907 kfree(tt->transaction);
908 tt->transaction = NULL;
909 } else {
910 transaction = tt->transaction +
911 (IWL_TI_0 * (IWL_TI_STATE_MAX - 1));
912 memcpy(transaction, &tt_range_0[0], size);
913 transaction = tt->transaction +
914 (IWL_TI_1 * (IWL_TI_STATE_MAX - 1));
915 memcpy(transaction, &tt_range_1[0], size);
916 transaction = tt->transaction +
917 (IWL_TI_2 * (IWL_TI_STATE_MAX - 1));
918 memcpy(transaction, &tt_range_2[0], size);
919 transaction = tt->transaction +
920 (IWL_TI_CT_KILL * (IWL_TI_STATE_MAX - 1));
921 memcpy(transaction, &tt_range_3[0], size);
922 size = sizeof(struct iwl_tt_restriction) *
923 IWL_TI_STATE_MAX;
924 memcpy(tt->restriction,
925 &restriction_range[0], size);
Johannes Berg3ad3b922009-08-07 15:41:48 -0700926 priv->thermal_throttle.advanced_tt = true;
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700927 }
Wey-Yi Guy6047b4f2009-10-23 13:42:27 -0700928 } else {
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700929 IWL_DEBUG_POWER(priv, "Legacy Thermal Throttling\n");
Johannes Berg3ad3b922009-08-07 15:41:48 -0700930 priv->thermal_throttle.advanced_tt = false;
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700931 }
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700932}
933EXPORT_SYMBOL(iwl_tt_initialize);
934
935/* cleanup thermal throttling management related memory and timer */
936void iwl_tt_exit(struct iwl_priv *priv)
937{
Johannes Berg3ad3b922009-08-07 15:41:48 -0700938 struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700939
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700940 /* stop ct_kill_exit_tm timer if activated */
Johannes Berg3ad3b922009-08-07 15:41:48 -0700941 del_timer_sync(&priv->thermal_throttle.ct_kill_exit_tm);
Wey-Yi Guy7812b162009-10-02 13:43:58 -0700942 /* stop ct_kill_waiting_tm timer if activated */
943 del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
Wey-Yi Guya28027cd2009-08-07 15:41:45 -0700944 cancel_work_sync(&priv->tt_work);
945 cancel_work_sync(&priv->ct_enter);
946 cancel_work_sync(&priv->ct_exit);
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700947
Johannes Berg3ad3b922009-08-07 15:41:48 -0700948 if (priv->thermal_throttle.advanced_tt) {
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700949 /* free advance thermal throttling memory */
950 kfree(tt->restriction);
951 tt->restriction = NULL;
952 kfree(tt->transaction);
953 tt->transaction = NULL;
954 }
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700955}
956EXPORT_SYMBOL(iwl_tt_exit);
957
Tomas Winklera96a27f2008-10-23 23:48:56 -0700958/* initialize to default */
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700959void iwl_power_initialize(struct iwl_priv *priv)
960{
Johannes Berge312c242009-08-07 15:41:51 -0700961 u16 lctl = iwl_pcie_link_ctl(priv);
962
963 priv->power_data.pci_pm = !(lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN);
964
965 priv->power_data.debug_sleep_level_override = -1;
966
967 memset(&priv->power_data.sleep_cmd, 0,
968 sizeof(priv->power_data.sleep_cmd));
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700969}
970EXPORT_SYMBOL(iwl_power_initialize);