blob: 00937b3ee8ecbb3302a81d9a9b280d0fc80cbba9 [file] [log] [blame]
Mohamed Abbas5da4b552008-04-21 15:41:51 -07001/******************************************************************************
2 *
Reinette Chatre01f81622009-01-08 10:20:02 -08003 * Copyright(c) 2007 - 2009 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 Berg7af2c462009-05-08 13:44:38 -070045 * Setting power level allow the card to go to sleep when not busy.
Mohamed Abbas5da4b552008-04-21 15:41:51 -070046 *
Johannes Berg7af2c462009-05-08 13:44:38 -070047 * The power level is set to INDEX_1 (the least deep state) by
48 * default, and will, in the future, be the deepest state unless
49 * otherwise required by pm_qos network latency requirements.
Mohamed Abbas5da4b552008-04-21 15:41:51 -070050 *
Johannes Berg7af2c462009-05-08 13:44:38 -070051 * Using INDEX_1 without pm_qos is ok because mac80211 will disable
52 * PS when even checking every beacon for the TIM bit would exceed
53 * the required latency.
Mohamed Abbas5da4b552008-04-21 15:41:51 -070054 */
55
Mohamed Abbas5da4b552008-04-21 15:41:51 -070056#define IWL_POWER_RANGE_0_MAX (2)
57#define IWL_POWER_RANGE_1_MAX (10)
58
59
Johannes Berg7af2c462009-05-08 13:44:38 -070060#define NOSLP cpu_to_le16(0), 0, 0
61#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
62#define TU_TO_USEC 1024
63#define SLP_TOUT(T) cpu_to_le32((T) * TU_TO_USEC)
64#define SLP_VEC(X0, X1, X2, X3, X4) {cpu_to_le32(X0), \
65 cpu_to_le32(X1), \
66 cpu_to_le32(X2), \
67 cpu_to_le32(X3), \
68 cpu_to_le32(X4)}
Mohamed Abbas5da4b552008-04-21 15:41:51 -070069/* default power management (not Tx power) table values */
Johannes Berg7af2c462009-05-08 13:44:38 -070070/* for DTIM period 0 through IWL_POWER_RANGE_0_MAX */
71static const struct iwl_power_vec_entry range_0[IWL_POWER_NUM] = {
Mohamed Abbas5da4b552008-04-21 15:41:51 -070072 {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
73 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
74 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
75 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 2, 2, 2, 0xFF)}, 0},
76 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 2, 4, 4, 0xFF)}, 1},
77 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 2, 4, 6, 0xFF)}, 2}
78};
79
80
Johannes Berg7af2c462009-05-08 13:44:38 -070081/* for DTIM period IWL_POWER_RANGE_0_MAX + 1 through IWL_POWER_RANGE_1_MAX */
82static const struct iwl_power_vec_entry range_1[IWL_POWER_NUM] = {
Mohamed Abbas5da4b552008-04-21 15:41:51 -070083 {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
84 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
85 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 3, 4, 7)}, 0},
86 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 4, 6, 7, 9)}, 0},
87 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 4, 6, 9, 10)}, 1},
88 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 4, 7, 10, 10)}, 2}
89};
90
Johannes Berg7af2c462009-05-08 13:44:38 -070091/* for DTIM period > IWL_POWER_RANGE_1_MAX */
92static const struct iwl_power_vec_entry range_2[IWL_POWER_NUM] = {
Mohamed Abbas5da4b552008-04-21 15:41:51 -070093 {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
94 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
95 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
96 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
97 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
98 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
99};
100
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700101/* default Thermal Throttling transaction table
102 * Current state | Throttling Down | Throttling Up
103 *=============================================================================
104 * Condition Nxt State Condition Nxt State Condition Nxt State
105 *-----------------------------------------------------------------------------
106 * IWL_TI_0 T >= 115 CT_KILL 115>T>=105 TI_1 N/A N/A
107 * IWL_TI_1 T >= 115 CT_KILL 115>T>=110 TI_2 T<=95 TI_0
108 * IWL_TI_2 T >= 115 CT_KILL T<=100 TI_1
109 * IWL_CT_KILL N/A N/A N/A N/A T<=95 TI_0
110 *=============================================================================
111 */
112static const struct iwl_tt_trans tt_range_0[IWL_TI_STATE_MAX - 1] = {
113 {IWL_TI_0, IWL_ABSOLUTE_ZERO, 104},
114 {IWL_TI_1, 105, CT_KILL_THRESHOLD},
115 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD + 1, IWL_ABSOLUTE_MAX}
116};
117static const struct iwl_tt_trans tt_range_1[IWL_TI_STATE_MAX - 1] = {
118 {IWL_TI_0, IWL_ABSOLUTE_ZERO, 95},
119 {IWL_TI_2, 110, CT_KILL_THRESHOLD},
120 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD + 1, IWL_ABSOLUTE_MAX}
121};
122static const struct iwl_tt_trans tt_range_2[IWL_TI_STATE_MAX - 1] = {
123 {IWL_TI_1, IWL_ABSOLUTE_ZERO, 100},
124 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD + 1, IWL_ABSOLUTE_MAX},
125 {IWL_TI_CT_KILL, CT_KILL_THRESHOLD + 1, IWL_ABSOLUTE_MAX}
126};
127static const struct iwl_tt_trans tt_range_3[IWL_TI_STATE_MAX - 1] = {
128 {IWL_TI_0, IWL_ABSOLUTE_ZERO, CT_KILL_EXIT_THRESHOLD},
129 {IWL_TI_CT_KILL, CT_KILL_EXIT_THRESHOLD + 1, IWL_ABSOLUTE_MAX},
130 {IWL_TI_CT_KILL, CT_KILL_EXIT_THRESHOLD + 1, IWL_ABSOLUTE_MAX}
131};
132
133/* Advance Thermal Throttling default restriction table */
134static const struct iwl_tt_restriction restriction_range[IWL_TI_STATE_MAX] = {
135 {IWL_TX_MULTI, true, IWL_RX_MULTI},
136 {IWL_TX_SINGLE, true, IWL_RX_MULTI},
137 {IWL_TX_SINGLE, false, IWL_RX_SINGLE},
138 {IWL_TX_NONE, false, IWL_RX_NONE}
139};
Winkler, Tomasd25aabb2009-01-27 14:27:58 -0800140
Mohamed Abbasca579612008-07-18 13:52:57 +0800141/* set card power command */
142static int iwl_set_power(struct iwl_priv *priv, void *cmd)
143{
Johannes Bergf0f74a02009-05-08 13:44:37 -0700144 return iwl_send_cmd_pdu(priv, POWER_TABLE_CMD,
145 sizeof(struct iwl_powertable_cmd), cmd);
Mohamed Abbasca579612008-07-18 13:52:57 +0800146}
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700147
148/* initialize to default */
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800149static void iwl_power_init_handle(struct iwl_priv *priv)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700150{
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700151 struct iwl_power_mgr *pow_data;
Johannes Berg7af2c462009-05-08 13:44:38 -0700152 int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_NUM;
Tomas Winklere7b63582008-09-03 11:26:49 +0800153 struct iwl_powertable_cmd *cmd;
154 int i;
Tomas Winkler3fdb68d2009-02-10 15:19:02 -0800155 u16 lctl;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700156
Tomas Winklere1623442009-01-27 14:27:56 -0800157 IWL_DEBUG_POWER(priv, "Initialize power \n");
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700158
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800159 pow_data = &priv->power_data;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700160
161 memset(pow_data, 0, sizeof(*pow_data));
162
163 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
164 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
165 memcpy(&pow_data->pwr_range_2[0], &range_2[0], size);
166
Tomas Winkler3fdb68d2009-02-10 15:19:02 -0800167 lctl = iwl_pcie_link_ctl(priv);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700168
Tomas Winklere1623442009-01-27 14:27:56 -0800169 IWL_DEBUG_POWER(priv, "adjust power command flags\n");
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700170
Johannes Berg7af2c462009-05-08 13:44:38 -0700171 for (i = 0; i < IWL_POWER_NUM; i++) {
Tomas Winklere7b63582008-09-03 11:26:49 +0800172 cmd = &pow_data->pwr_range_0[i].cmd;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700173
Tomas Winkler3fdb68d2009-02-10 15:19:02 -0800174 if (lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN)
Tomas Winklere7b63582008-09-03 11:26:49 +0800175 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
176 else
177 cmd->flags |= IWL_POWER_PCI_PM_MSK;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700178 }
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700179}
180
Tomas Winklera96a27f2008-10-23 23:48:56 -0700181/* adjust power command according to DTIM period and power level*/
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800182static int iwl_update_power_cmd(struct iwl_priv *priv,
183 struct iwl_powertable_cmd *cmd, u16 mode)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700184{
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700185 struct iwl_power_vec_entry *range;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700186 struct iwl_power_mgr *pow_data;
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800187 int i;
188 u32 max_sleep = 0;
189 u8 period;
190 bool skip;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700191
192 if (mode > IWL_POWER_INDEX_5) {
Tomas Winklere1623442009-01-27 14:27:56 -0800193 IWL_DEBUG_POWER(priv, "Error invalid power mode \n");
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800194 return -EINVAL;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700195 }
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800196
197 pow_data = &priv->power_data;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700198
199 if (pow_data->dtim_period <= IWL_POWER_RANGE_0_MAX)
200 range = &pow_data->pwr_range_0[0];
201 else if (pow_data->dtim_period <= IWL_POWER_RANGE_1_MAX)
202 range = &pow_data->pwr_range_1[0];
203 else
204 range = &pow_data->pwr_range_2[0];
205
206 period = pow_data->dtim_period;
Mohamed Abbasca579612008-07-18 13:52:57 +0800207 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700208
209 if (period == 0) {
210 period = 1;
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800211 skip = false;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700212 } else {
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800213 skip = !!range[mode].no_dtim;
214 }
215
216 if (skip) {
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700217 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
218 max_sleep = le32_to_cpu(slp_itrvl);
219 if (max_sleep == 0xFF)
220 max_sleep = period * (skip + 1);
221 else if (max_sleep > period)
222 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
223 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800224 } else {
225 max_sleep = period;
226 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700227 }
228
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800229 for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700230 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
231 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700232
Tomas Winklere1623442009-01-27 14:27:56 -0800233 IWL_DEBUG_POWER(priv, "Flags value = 0x%08X\n", cmd->flags);
234 IWL_DEBUG_POWER(priv, "Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
235 IWL_DEBUG_POWER(priv, "Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
236 IWL_DEBUG_POWER(priv, "Sleep interval vector = { %d , %d , %d , %d , %d }\n",
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700237 le32_to_cpu(cmd->sleep_interval[0]),
238 le32_to_cpu(cmd->sleep_interval[1]),
239 le32_to_cpu(cmd->sleep_interval[2]),
240 le32_to_cpu(cmd->sleep_interval[3]),
241 le32_to_cpu(cmd->sleep_interval[4]));
242
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800243 return 0;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700244}
245
246
247/*
Emmanuel Grumbacha33c2f42008-09-03 11:26:56 +0800248 * compute the final power mode index
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700249 */
Grumbach, Emmanuel04816442008-09-03 11:26:53 +0800250int iwl_power_update_mode(struct iwl_priv *priv, bool force)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700251{
252 struct iwl_power_mgr *setting = &(priv->power_data);
253 int ret = 0;
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700254 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700255 u16 uninitialized_var(final_mode);
Winkler, Tomasa71c8f62008-11-07 09:58:37 -0800256 bool update_chains;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700257
Grumbach, Emmanuel04816442008-09-03 11:26:53 +0800258 /* Don't update the RX chain when chain noise calibration is running */
Winkler, Tomasa71c8f62008-11-07 09:58:37 -0800259 update_chains = priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE ||
260 priv->chain_noise_data.state == IWL_CHAIN_NOISE_ALIVE;
Grumbach, Emmanuel04816442008-09-03 11:26:53 +0800261
Johannes Berg7af2c462009-05-08 13:44:38 -0700262 final_mode = priv->power_data.user_power_setting;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700263
Johannes Berg7af2c462009-05-08 13:44:38 -0700264 if (setting->power_disabled)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700265 final_mode = IWL_POWER_MODE_CAM;
266
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700267 if (tt->state >= IWL_TI_1) {
268 /* TT power setting overwrite user & system power setting */
269 final_mode = tt->tt_power_mode;
270 }
Johannes Berg7af2c462009-05-08 13:44:38 -0700271 if (iwl_is_ready_rf(priv) &&
Grumbach, Emmanuel04816442008-09-03 11:26:53 +0800272 ((setting->power_mode != final_mode) || force)) {
Mohamed Abbasca579612008-07-18 13:52:57 +0800273 struct iwl_powertable_cmd cmd;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700274
275 if (final_mode != IWL_POWER_MODE_CAM)
276 set_bit(STATUS_POWER_PMI, &priv->status);
277
Winkler, Tomas5cd19c52009-01-19 15:30:21 -0800278 iwl_update_power_cmd(priv, &cmd, final_mode);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700279 cmd.keep_alive_beacons = 0;
280
281 if (final_mode == IWL_POWER_INDEX_5)
282 cmd.flags |= IWL_POWER_FAST_PD;
283
Mohamed Abbasca579612008-07-18 13:52:57 +0800284 ret = iwl_set_power(priv, &cmd);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700285
286 if (final_mode == IWL_POWER_MODE_CAM)
287 clear_bit(STATUS_POWER_PMI, &priv->status);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700288
Winkler, Tomasa71c8f62008-11-07 09:58:37 -0800289 if (priv->cfg->ops->lib->update_chain_flags && update_chains)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700290 priv->cfg->ops->lib->update_chain_flags(priv);
Winkler, Tomasa71c8f62008-11-07 09:58:37 -0800291 else
Tomas Winklere1623442009-01-27 14:27:56 -0800292 IWL_DEBUG_POWER(priv, "Cannot update the power, chain noise "
Winkler, Tomasa71c8f62008-11-07 09:58:37 -0800293 "calibration running: %d\n",
294 priv->chain_noise_data.state);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700295 if (!ret)
296 setting->power_mode = final_mode;
297 }
298
299 return ret;
300}
301EXPORT_SYMBOL(iwl_power_update_mode);
302
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700303/* set user_power_setting */
304int iwl_power_set_user_mode(struct iwl_priv *priv, u16 mode)
305{
Johannes Berg7af2c462009-05-08 13:44:38 -0700306 if (mode >= IWL_POWER_NUM)
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700307 return -EINVAL;
308
309 priv->power_data.user_power_setting = mode;
310
Grumbach, Emmanuel04816442008-09-03 11:26:53 +0800311 return iwl_power_update_mode(priv, 0);
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700312}
313EXPORT_SYMBOL(iwl_power_set_user_mode);
314
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700315bool iwl_ht_enabled(struct iwl_priv *priv)
316{
317 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
318 struct iwl_tt_restriction *restriction;
319
320 if (!priv->power_data.adv_tt)
321 return true;
322 restriction = tt->restriction + tt->state;
323 return restriction->is_ht;
324}
325EXPORT_SYMBOL(iwl_ht_enabled);
326
327u8 iwl_tx_ant_restriction(struct iwl_priv *priv)
328{
329 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
330 struct iwl_tt_restriction *restriction;
331
332 if (!priv->power_data.adv_tt)
333 return IWL_TX_MULTI;
334 restriction = tt->restriction + tt->state;
335 return restriction->tx_stream;
336}
337EXPORT_SYMBOL(iwl_tx_ant_restriction);
338
339u8 iwl_rx_ant_restriction(struct iwl_priv *priv)
340{
341 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
342 struct iwl_tt_restriction *restriction;
343
344 if (!priv->power_data.adv_tt)
345 return IWL_RX_MULTI;
346 restriction = tt->restriction + tt->state;
347 return restriction->rx_stream;
348}
349EXPORT_SYMBOL(iwl_rx_ant_restriction);
350
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700351#define CT_KILL_EXIT_DURATION (5) /* 5 seconds duration */
352
353/*
354 * toggle the bit to wake up uCode and check the temperature
355 * if the temperature is below CT, uCode will stay awake and send card
356 * state notification with CT_KILL bit clear to inform Thermal Throttling
357 * Management to change state. Otherwise, uCode will go back to sleep
358 * without doing anything, driver should continue the 5 seconds timer
359 * to wake up uCode for temperature check until temperature drop below CT
360 */
361static void iwl_tt_check_exit_ct_kill(unsigned long data)
362{
363 struct iwl_priv *priv = (struct iwl_priv *)data;
364 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
365 unsigned long flags;
366
367 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
368 return;
369
370 if (tt->state == IWL_TI_CT_KILL) {
371 if (priv->power_data.ct_kill_toggle) {
372 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
373 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
374 priv->power_data.ct_kill_toggle = false;
375 } else {
376 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
377 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
378 priv->power_data.ct_kill_toggle = true;
379 }
380 iwl_read32(priv, CSR_UCODE_DRV_GP1);
381 spin_lock_irqsave(&priv->reg_lock, flags);
382 if (!iwl_grab_nic_access(priv))
383 iwl_release_nic_access(priv);
384 spin_unlock_irqrestore(&priv->reg_lock, flags);
385
386 /* Reschedule the ct_kill timer to occur in
387 * CT_KILL_EXIT_DURATION seconds to ensure we get a
388 * thermal update */
389 mod_timer(&priv->power_data.ct_kill_exit_tm, jiffies +
390 CT_KILL_EXIT_DURATION * HZ);
391 }
392}
393
394static void iwl_perform_ct_kill_task(struct iwl_priv *priv,
395 bool stop)
396{
397 if (stop) {
398 IWL_DEBUG_POWER(priv, "Stop all queues\n");
399 if (priv->mac80211_registered)
400 ieee80211_stop_queues(priv->hw);
401 IWL_DEBUG_POWER(priv,
402 "Schedule 5 seconds CT_KILL Timer\n");
403 mod_timer(&priv->power_data.ct_kill_exit_tm, jiffies +
404 CT_KILL_EXIT_DURATION * HZ);
405 } else {
406 IWL_DEBUG_POWER(priv, "Wake all queues\n");
407 if (priv->mac80211_registered)
408 ieee80211_wake_queues(priv->hw);
409 }
410}
411
412#define IWL_MINIMAL_POWER_THRESHOLD (CT_KILL_THRESHOLD_LEGACY)
413#define IWL_REDUCED_PERFORMANCE_THRESHOLD_2 (100)
414#define IWL_REDUCED_PERFORMANCE_THRESHOLD_1 (90)
415
416/*
417 * Legacy thermal throttling
418 * 1) Avoid NIC destruction due to high temperatures
419 * Chip will identify dangerously high temperatures that can
420 * harm the device and will power down
421 * 2) Avoid the NIC power down due to high temperature
422 * Throttle early enough to lower the power consumption before
423 * drastic steps are needed
424 */
425static void iwl_legacy_tt_handler(struct iwl_priv *priv, s32 temp)
426{
427 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
428 enum iwl_tt_state new_state;
429 struct iwl_power_mgr *setting = &priv->power_data;
430
431#ifdef CONFIG_IWLWIFI_DEBUG
432 if ((tt->tt_previous_temp) &&
433 (temp > tt->tt_previous_temp) &&
434 ((temp - tt->tt_previous_temp) >
435 IWL_TT_INCREASE_MARGIN)) {
436 IWL_DEBUG_POWER(priv,
437 "Temperature increase %d degree Celsius\n",
438 (temp - tt->tt_previous_temp));
439 }
440#endif
441 /* in Celsius */
442 if (temp >= IWL_MINIMAL_POWER_THRESHOLD)
443 new_state = IWL_TI_CT_KILL;
444 else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_2)
445 new_state = IWL_TI_2;
446 else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_1)
447 new_state = IWL_TI_1;
448 else
449 new_state = IWL_TI_0;
450
451#ifdef CONFIG_IWLWIFI_DEBUG
452 tt->tt_previous_temp = temp;
453#endif
454 if (tt->state != new_state) {
455 if (tt->state == IWL_TI_0) {
456 tt->sys_power_mode = setting->power_mode;
457 IWL_DEBUG_POWER(priv, "current power mode: %u\n",
458 setting->power_mode);
459 }
460 switch (new_state) {
461 case IWL_TI_0:
462 /* when system ready to go back to IWL_TI_0 state
463 * using system power mode instead of TT power mode
464 * revert back to the orginal power mode which was saved
465 * before enter Thermal Throttling state
466 * update priv->power_data.user_power_setting to the
467 * required power mode to make sure
468 * iwl_power_update_mode() will update power correctly.
469 */
470 priv->power_data.user_power_setting =
471 tt->sys_power_mode;
472 tt->tt_power_mode = tt->sys_power_mode;
473 break;
474 case IWL_TI_1:
475 tt->tt_power_mode = IWL_POWER_INDEX_3;
476 break;
477 case IWL_TI_2:
478 tt->tt_power_mode = IWL_POWER_INDEX_4;
479 break;
480 default:
481 tt->tt_power_mode = IWL_POWER_INDEX_5;
482 break;
483 }
484 if (iwl_power_update_mode(priv, true)) {
485 /* TT state not updated
486 * try again during next temperature read
487 */
488 IWL_ERR(priv, "Cannot update power mode, "
489 "TT state not updated\n");
490 } else {
491 if (new_state == IWL_TI_CT_KILL)
492 iwl_perform_ct_kill_task(priv, true);
493 else if (tt->state == IWL_TI_CT_KILL &&
494 new_state != IWL_TI_CT_KILL)
495 iwl_perform_ct_kill_task(priv, false);
496 tt->state = new_state;
497 IWL_DEBUG_POWER(priv, "Temperature state changed %u\n",
498 tt->state);
499 IWL_DEBUG_POWER(priv, "Power Index change to %u\n",
500 tt->tt_power_mode);
501 }
502 }
503}
504
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700505/*
506 * Advance thermal throttling
507 * 1) Avoid NIC destruction due to high temperatures
508 * Chip will identify dangerously high temperatures that can
509 * harm the device and will power down
510 * 2) Avoid the NIC power down due to high temperature
511 * Throttle early enough to lower the power consumption before
512 * drastic steps are needed
513 * Actions include relaxing the power down sleep thresholds and
514 * decreasing the number of TX streams
515 * 3) Avoid throughput performance impact as much as possible
516 *
517 *=============================================================================
518 * Condition Nxt State Condition Nxt State Condition Nxt State
519 *-----------------------------------------------------------------------------
520 * IWL_TI_0 T >= 115 CT_KILL 115>T>=105 TI_1 N/A N/A
521 * IWL_TI_1 T >= 115 CT_KILL 115>T>=110 TI_2 T<=95 TI_0
522 * IWL_TI_2 T >= 115 CT_KILL T<=100 TI_1
523 * IWL_CT_KILL N/A N/A N/A N/A T<=95 TI_0
524 *=============================================================================
525 */
526static void iwl_advance_tt_handler(struct iwl_priv *priv, s32 temp)
527{
528 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
529 int i;
530 bool changed = false;
531 enum iwl_tt_state old_state;
532 struct iwl_tt_trans *transaction;
533
534 old_state = tt->state;
535 for (i = 0; i < IWL_TI_STATE_MAX - 1; i++) {
536 /* based on the current TT state,
537 * find the curresponding transaction table
538 * each table has (IWL_TI_STATE_MAX - 1) entries
539 * tt->transaction + ((old_state * (IWL_TI_STATE_MAX - 1))
540 * will advance to the correct table.
541 * then based on the current temperature
542 * find the next state need to transaction to
543 * go through all the possible (IWL_TI_STATE_MAX - 1) entries
544 * in the current table to see if transaction is needed
545 */
546 transaction = tt->transaction +
547 ((old_state * (IWL_TI_STATE_MAX - 1)) + i);
548 if (temp >= transaction->tt_low &&
549 temp <= transaction->tt_high) {
550#ifdef CONFIG_IWLWIFI_DEBUG
551 if ((tt->tt_previous_temp) &&
552 (temp > tt->tt_previous_temp) &&
553 ((temp - tt->tt_previous_temp) >
554 IWL_TT_INCREASE_MARGIN)) {
555 IWL_DEBUG_POWER(priv,
556 "Temperature increase %d "
557 "degree Celsius\n",
558 (temp - tt->tt_previous_temp));
559 }
560 tt->tt_previous_temp = temp;
561#endif
562 if (old_state !=
563 transaction->next_state) {
564 changed = true;
565 tt->state =
566 transaction->next_state;
567 }
568 break;
569 }
570 }
571 if (changed) {
572 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
573 struct iwl_power_mgr *setting = &priv->power_data;
574
575 if (tt->state >= IWL_TI_1) {
576 /* if switching from IWL_TI_0 to other TT state
577 * save previous power setting in tt->sys_power_mode */
578 if (old_state == IWL_TI_0)
579 tt->sys_power_mode = setting->power_mode;
580 /* force PI = IWL_POWER_INDEX_5 in the case of TI > 0 */
581 tt->tt_power_mode = IWL_POWER_INDEX_5;
582 if (!iwl_ht_enabled(priv))
583 /* disable HT */
584 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
585 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
586 RXON_FLG_FAT_PROT_MSK |
587 RXON_FLG_HT_PROT_MSK);
588 else {
589 /* check HT capability and set
590 * according to the system HT capability
591 * in case get disabled before */
592 iwl_set_rxon_ht(priv, &priv->current_ht_config);
593 }
594
595 } else {
596 /* restore system power setting */
597 /* the previous power mode was saved in
598 * tt->sys_power_mode when system move into
599 * Thermal Throttling state
600 * set power_data.user_power_setting to the previous
601 * system power mode to make sure power will get
602 * updated correctly
603 */
604 priv->power_data.user_power_setting =
605 tt->sys_power_mode;
606 tt->tt_power_mode = tt->sys_power_mode;
607 /* check HT capability and set
608 * according to the system HT capability
609 * in case get disabled before */
610 iwl_set_rxon_ht(priv, &priv->current_ht_config);
611 }
612 if (iwl_power_update_mode(priv, true)) {
613 /* TT state not updated
614 * try again during next temperature read
615 */
616 IWL_ERR(priv, "Cannot update power mode, "
617 "TT state not updated\n");
618 tt->state = old_state;
619 } else {
620 IWL_DEBUG_POWER(priv,
621 "Thermal Throttling to new state: %u\n",
622 tt->state);
623 if (old_state != IWL_TI_CT_KILL &&
624 tt->state == IWL_TI_CT_KILL) {
625 IWL_DEBUG_POWER(priv, "Enter IWL_TI_CT_KILL\n");
626 iwl_perform_ct_kill_task(priv, true);
627
628 } else if (old_state == IWL_TI_CT_KILL &&
629 tt->state != IWL_TI_CT_KILL) {
630 IWL_DEBUG_POWER(priv, "Exit IWL_TI_CT_KILL\n");
631 iwl_perform_ct_kill_task(priv, false);
632 }
633 }
634 }
635}
636
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700637/* Card State Notification indicated reach critical temperature
638 * if PSP not enable, no Thermal Throttling function will be performed
639 * just set the GP1 bit to acknowledge the event
640 * otherwise, go into IWL_TI_CT_KILL state
641 * since Card State Notification will not provide any temperature reading
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700642 * for Legacy mode
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700643 * so just pass the CT_KILL temperature to iwl_legacy_tt_handler()
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700644 * for advance mode
645 * pass CT_KILL_THRESHOLD+1 to make sure move into IWL_TI_CT_KILL state
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700646 */
647void iwl_tt_enter_ct_kill(struct iwl_priv *priv)
648{
649 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
650
651 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
652 return;
653
654 if (tt->state != IWL_TI_CT_KILL) {
655 IWL_ERR(priv, "Device reached critical temperature "
656 "- ucode going to sleep!\n");
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700657 if (!priv->power_data.adv_tt)
658 iwl_legacy_tt_handler(priv,
659 IWL_MINIMAL_POWER_THRESHOLD);
660 else
661 iwl_advance_tt_handler(priv,
662 CT_KILL_THRESHOLD + 1);
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700663 }
664}
665EXPORT_SYMBOL(iwl_tt_enter_ct_kill);
666
667/* Card State Notification indicated out of critical temperature
668 * since Card State Notification will not provide any temperature reading
669 * so pass the IWL_REDUCED_PERFORMANCE_THRESHOLD_2 temperature
670 * to iwl_legacy_tt_handler() to get out of IWL_CT_KILL state
671 */
672void iwl_tt_exit_ct_kill(struct iwl_priv *priv)
673{
674 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
675
676 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
677 return;
678
679 /* stop ct_kill_exit_tm timer */
680 del_timer_sync(&priv->power_data.ct_kill_exit_tm);
681
682 if (tt->state == IWL_TI_CT_KILL) {
683 IWL_ERR(priv,
684 "Device temperature below critical"
685 "- ucode awake!\n");
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700686 if (!priv->power_data.adv_tt)
687 iwl_legacy_tt_handler(priv,
688 IWL_REDUCED_PERFORMANCE_THRESHOLD_2);
689 else
690 iwl_advance_tt_handler(priv, CT_KILL_EXIT_THRESHOLD);
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700691 }
692}
693EXPORT_SYMBOL(iwl_tt_exit_ct_kill);
694
695void iwl_tt_handler(struct iwl_priv *priv)
696{
697 s32 temp = priv->temperature; /* degrees CELSIUS except 4965 */
698
699 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
700 return;
701
702 if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
703 temp = KELVIN_TO_CELSIUS(priv->temperature);
704
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700705 if (!priv->power_data.adv_tt)
706 iwl_legacy_tt_handler(priv, temp);
707 else
708 iwl_advance_tt_handler(priv, temp);
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700709}
710EXPORT_SYMBOL(iwl_tt_handler);
711
712/* Thermal throttling initialization
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700713 * For advance thermal throttling:
714 * Initialize Thermal Index and temperature threshold table
715 * Initialize thermal throttling restriction table
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700716 */
717void iwl_tt_initialize(struct iwl_priv *priv)
718{
719 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
720 struct iwl_power_mgr *setting = &priv->power_data;
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700721 int size = sizeof(struct iwl_tt_trans) * (IWL_TI_STATE_MAX - 1);
722 struct iwl_tt_trans *transaction;
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700723
724 IWL_DEBUG_POWER(priv, "Initialize Thermal Throttling \n");
725
726 memset(tt, 0, sizeof(struct iwl_tt_mgmt));
727
728 tt->state = IWL_TI_0;
729 tt->sys_power_mode = setting->power_mode;
730 tt->tt_power_mode = tt->sys_power_mode;
731 init_timer(&priv->power_data.ct_kill_exit_tm);
732 priv->power_data.ct_kill_exit_tm.data = (unsigned long)priv;
733 priv->power_data.ct_kill_exit_tm.function = iwl_tt_check_exit_ct_kill;
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700734 switch (priv->hw_rev & CSR_HW_REV_TYPE_MSK) {
735 case CSR_HW_REV_TYPE_6x00:
736 case CSR_HW_REV_TYPE_6x50:
737 IWL_DEBUG_POWER(priv, "Advanced Thermal Throttling\n");
738 tt->restriction = kzalloc(sizeof(struct iwl_tt_restriction) *
739 IWL_TI_STATE_MAX, GFP_KERNEL);
740 tt->transaction = kzalloc(sizeof(struct iwl_tt_trans) *
741 IWL_TI_STATE_MAX * (IWL_TI_STATE_MAX - 1),
742 GFP_KERNEL);
743 if (!tt->restriction || !tt->transaction) {
744 IWL_ERR(priv, "Fallback to Legacy Throttling\n");
745 priv->power_data.adv_tt = false;
746 kfree(tt->restriction);
747 tt->restriction = NULL;
748 kfree(tt->transaction);
749 tt->transaction = NULL;
750 } else {
751 transaction = tt->transaction +
752 (IWL_TI_0 * (IWL_TI_STATE_MAX - 1));
753 memcpy(transaction, &tt_range_0[0], size);
754 transaction = tt->transaction +
755 (IWL_TI_1 * (IWL_TI_STATE_MAX - 1));
756 memcpy(transaction, &tt_range_1[0], size);
757 transaction = tt->transaction +
758 (IWL_TI_2 * (IWL_TI_STATE_MAX - 1));
759 memcpy(transaction, &tt_range_2[0], size);
760 transaction = tt->transaction +
761 (IWL_TI_CT_KILL * (IWL_TI_STATE_MAX - 1));
762 memcpy(transaction, &tt_range_3[0], size);
763 size = sizeof(struct iwl_tt_restriction) *
764 IWL_TI_STATE_MAX;
765 memcpy(tt->restriction,
766 &restriction_range[0], size);
767 priv->power_data.adv_tt = true;
768 }
769 break;
770 default:
771 IWL_DEBUG_POWER(priv, "Legacy Thermal Throttling\n");
772 priv->power_data.adv_tt = false;
773 break;
774 }
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700775}
776EXPORT_SYMBOL(iwl_tt_initialize);
777
778/* cleanup thermal throttling management related memory and timer */
779void iwl_tt_exit(struct iwl_priv *priv)
780{
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700781 struct iwl_tt_mgmt *tt = &priv->power_data.tt;
782
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700783 /* stop ct_kill_exit_tm timer if activated */
784 del_timer_sync(&priv->power_data.ct_kill_exit_tm);
Wey-Yi Guy46f93812009-07-24 11:13:03 -0700785
786 if (priv->power_data.adv_tt) {
787 /* free advance thermal throttling memory */
788 kfree(tt->restriction);
789 tt->restriction = NULL;
790 kfree(tt->transaction);
791 tt->transaction = NULL;
792 }
Wey-Yi Guy39b73fb12009-07-24 11:13:02 -0700793}
794EXPORT_SYMBOL(iwl_tt_exit);
795
Tomas Winklera96a27f2008-10-23 23:48:56 -0700796/* initialize to default */
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700797void iwl_power_initialize(struct iwl_priv *priv)
798{
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700799 iwl_power_init_handle(priv);
Johannes Berg7af2c462009-05-08 13:44:38 -0700800 priv->power_data.user_power_setting = IWL_POWER_INDEX_1;
801 /* default to disabled until mac80211 says otherwise */
802 priv->power_data.power_disabled = 1;
Mohamed Abbas5da4b552008-04-21 15:41:51 -0700803}
804EXPORT_SYMBOL(iwl_power_initialize);