iwlwifi: reliable entering of critical temperature state
[safe/jmp/linux-2.6] / drivers / net / wireless / iwlwifi / iwl-power.c
index 55ec31e..9c6b149 100644 (file)
@@ -1,6 +1,6 @@
 /******************************************************************************
  *
- * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
+ * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
  *
  * Portions of this file are derived from the ipw3945 project, as well
  * as portions of the ieee80211 subsystem header files.
@@ -22,7 +22,7 @@
  * file called LICENSE.
  *
  * Contact Information:
- * James P. Ketrenos <ipw2100-admin@linux.intel.com>
+ *  Intel Linux Wireless <ilw@linux.intel.com>
  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  *****************************************************************************/
 
 #include "iwl-eeprom.h"
 #include "iwl-dev.h"
 #include "iwl-core.h"
+#include "iwl-io.h"
 #include "iwl-commands.h"
 #include "iwl-debug.h"
 #include "iwl-power.h"
-#include "iwl-helpers.h"
 
 /*
- * Setting power level allow the card to go to sleep when not busy
- * there are three factor that decide the power level to go to, they
- * are list here with its priority
- *  1- critical_power_setting this will be set according to card temperature.
- *  2- system_power_setting this will be set by system PM manager.
- *  3- user_power_setting this will be set by user either by writing to sys or
- *     mac80211
- *
- * if system_power_setting and user_power_setting is set to auto
- * the power level will be decided according to association status and battery
- * status.
+ * Setting power level allows the card to go to sleep when not busy.
  *
+ * We calculate a sleep command based on the required latency, which
+ * we get from mac80211. In order to handle thermal throttling, we can
+ * also use pre-defined power levels.
  */
 
-#define MSEC_TO_USEC 1024
-#define IWL_POWER_RANGE_0_MAX  (2)
-#define IWL_POWER_RANGE_1_MAX  (10)
-
-
-#define NOSLP __constant_cpu_to_le16(0), 0, 0
-#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
-#define SLP_TOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
-#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
-                                    __constant_cpu_to_le32(X1), \
-                                    __constant_cpu_to_le32(X2), \
-                                    __constant_cpu_to_le32(X3), \
-                                    __constant_cpu_to_le32(X4)}
+/*
+ * For now, keep using power level 1 instead of automatically
+ * adjusting ...
+ */
+bool no_sleep_autoadjust = true;
+module_param(no_sleep_autoadjust, bool, S_IRUGO);
+MODULE_PARM_DESC(no_sleep_autoadjust,
+                "don't automatically adjust sleep level "
+                "according to maximum network latency");
 
-#define IWL_POWER_ON_BATTERY           IWL_POWER_INDEX_5
-#define IWL_POWER_ON_AC_DISASSOC       IWL_POWER_MODE_CAM
-#define IWL_POWER_ON_AC_ASSOC          IWL_POWER_MODE_CAM
+/*
+ * This defines the old power levels. They are still used by default
+ * (level 1) and for thermal throttle (levels 3 through 5)
+ */
 
+struct iwl_power_vec_entry {
+       struct iwl_powertable_cmd cmd;
+       u8 no_dtim;
+};
 
-#define IWL_CT_KILL_TEMPERATURE                110
-#define IWL_MIN_POWER_TEMPERATURE      100
-#define IWL_REDUCED_POWER_TEMPERATURE  95
+#define IWL_DTIM_RANGE_0_MAX   2
+#define IWL_DTIM_RANGE_1_MAX   10
 
+#define NOSLP cpu_to_le16(0), 0, 0
+#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
+#define TU_TO_USEC 1024
+#define SLP_TOUT(T) cpu_to_le32((T) * TU_TO_USEC)
+#define SLP_VEC(X0, X1, X2, X3, X4) {cpu_to_le32(X0), \
+                                    cpu_to_le32(X1), \
+                                    cpu_to_le32(X2), \
+                                    cpu_to_le32(X3), \
+                                    cpu_to_le32(X4)}
 /* default power management (not Tx power) table values */
-/* for tim  0-10 */
-static struct iwl_power_vec_entry range_0[IWL_POWER_MAX] = {
-       {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
+/* for DTIM period 0 through IWL_DTIM_RANGE_0_MAX */
+static const struct iwl_power_vec_entry range_0[IWL_POWER_NUM] = {
        {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
        {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
        {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 2, 2, 2, 0xFF)}, 0},
@@ -91,9 +92,8 @@ static struct iwl_power_vec_entry range_0[IWL_POWER_MAX] = {
 };
 
 
-/* for tim = 3-10 */
-static struct iwl_power_vec_entry range_1[IWL_POWER_MAX] = {
-       {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
+/* for DTIM period IWL_DTIM_RANGE_0_MAX + 1 through IWL_DTIM_RANGE_1_MAX */
+static const struct iwl_power_vec_entry range_1[IWL_POWER_NUM] = {
        {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
        {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 3, 4, 7)}, 0},
        {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 4, 6, 7, 9)}, 0},
@@ -101,9 +101,8 @@ static struct iwl_power_vec_entry range_1[IWL_POWER_MAX] = {
        {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 4, 7, 10, 10)}, 2}
 };
 
-/* for tim > 11 */
-static struct iwl_power_vec_entry range_2[IWL_POWER_MAX] = {
-       {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
+/* for DTIM period > IWL_DTIM_RANGE_1_MAX */
+static const struct iwl_power_vec_entry range_2[IWL_POWER_NUM] = {
        {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
        {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
        {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
@@ -111,355 +110,835 @@ static struct iwl_power_vec_entry range_2[IWL_POWER_MAX] = {
        {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
 };
 
-/* set card power command */
-static int iwl_set_power(struct iwl_priv *priv, void *cmd)
-{
-       return iwl_send_cmd_pdu_async(priv, POWER_TABLE_CMD,
-                                     sizeof(struct iwl_powertable_cmd),
-                                     cmd, NULL);
-}
-/* decide the right power level according to association status
- * and battery status
- */
-static u16 iwl_get_auto_power_mode(struct iwl_priv *priv)
+static void iwl_static_sleep_cmd(struct iwl_priv *priv,
+                                struct iwl_powertable_cmd *cmd,
+                                enum iwl_power_level lvl, int period)
 {
-       u16 mode;
-
-       switch (priv->power_data.user_power_setting) {
-       case IWL_POWER_AUTO:
-               /* if running on battery */
-               if (priv->power_data.is_battery_active)
-                       mode = IWL_POWER_ON_BATTERY;
-               else if (iwl_is_associated(priv))
-                       mode = IWL_POWER_ON_AC_ASSOC;
-               else
-                       mode = IWL_POWER_ON_AC_DISASSOC;
-               break;
-       /* FIXME: remove battery and ac from here */
-       case IWL_POWER_BATTERY:
-               mode = IWL_POWER_INDEX_3;
-               break;
-       case IWL_POWER_AC:
-               mode = IWL_POWER_MODE_CAM;
-               break;
-       default:
-               mode = priv->power_data.user_power_setting;
-               break;
+       const struct iwl_power_vec_entry *table;
+       int max_sleep, i;
+       bool skip;
+
+       table = range_2;
+       if (period < IWL_DTIM_RANGE_1_MAX)
+               table = range_1;
+       if (period < IWL_DTIM_RANGE_0_MAX)
+               table = range_0;
+
+       BUG_ON(lvl < 0 || lvl >= IWL_POWER_NUM);
+
+       *cmd = table[lvl].cmd;
+
+       if (period == 0) {
+               skip = false;
+               period = 1;
+       } else {
+               skip = !!table[lvl].no_dtim;
        }
-       return mode;
-}
 
-/* initialize to default */
-static int iwl_power_init_handle(struct iwl_priv *priv)
-{
-       struct iwl_power_mgr *pow_data;
-       int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_MAX;
-       struct iwl_powertable_cmd *cmd;
-       int i;
-       u16 pci_pm;
+       if (skip) {
+               __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
+               max_sleep = le32_to_cpu(slp_itrvl);
+               if (max_sleep == 0xFF)
+                       max_sleep = period * (skip + 1);
+               else if (max_sleep > period)
+                       max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
+               cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
+       } else {
+               max_sleep = period;
+               cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
+       }
+
+       for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
+               if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
+                       cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
 
-       IWL_DEBUG_POWER("Initialize power \n");
+       if (priv->power_data.pci_pm)
+               cmd->flags |= IWL_POWER_PCI_PM_MSK;
+       else
+               cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
 
-       pow_data = &(priv->power_data);
+       IWL_DEBUG_POWER(priv, "Sleep command for index %d\n", lvl + 1);
+}
 
-       memset(pow_data, 0, sizeof(*pow_data));
+/* default Thermal Throttling transaction table
+ * Current state   |         Throttling Down               |  Throttling Up
+ *=============================================================================
+ *                 Condition Nxt State  Condition Nxt State Condition Nxt State
+ *-----------------------------------------------------------------------------
+ *     IWL_TI_0     T >= 114   CT_KILL  114>T>=105   TI_1      N/A      N/A
+ *     IWL_TI_1     T >= 114   CT_KILL  114>T>=110   TI_2     T<=95     TI_0
+ *     IWL_TI_2     T >= 114   CT_KILL                        T<=100    TI_1
+ *    IWL_CT_KILL      N/A       N/A       N/A        N/A     T<=95     TI_0
+ *=============================================================================
+ */
+static const struct iwl_tt_trans tt_range_0[IWL_TI_STATE_MAX - 1] = {
+       {IWL_TI_0, IWL_ABSOLUTE_ZERO, 104},
+       {IWL_TI_1, 105, CT_KILL_THRESHOLD - 1},
+       {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
+};
+static const struct iwl_tt_trans tt_range_1[IWL_TI_STATE_MAX - 1] = {
+       {IWL_TI_0, IWL_ABSOLUTE_ZERO, 95},
+       {IWL_TI_2, 110, CT_KILL_THRESHOLD - 1},
+       {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
+};
+static const struct iwl_tt_trans tt_range_2[IWL_TI_STATE_MAX - 1] = {
+       {IWL_TI_1, IWL_ABSOLUTE_ZERO, 100},
+       {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX},
+       {IWL_TI_CT_KILL, CT_KILL_THRESHOLD, IWL_ABSOLUTE_MAX}
+};
+static const struct iwl_tt_trans tt_range_3[IWL_TI_STATE_MAX - 1] = {
+       {IWL_TI_0, IWL_ABSOLUTE_ZERO, CT_KILL_EXIT_THRESHOLD},
+       {IWL_TI_CT_KILL, CT_KILL_EXIT_THRESHOLD + 1, IWL_ABSOLUTE_MAX},
+       {IWL_TI_CT_KILL, CT_KILL_EXIT_THRESHOLD + 1, IWL_ABSOLUTE_MAX}
+};
 
-       memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
-       memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
-       memcpy(&pow_data->pwr_range_2[0], &range_2[0], size);
+/* Advance Thermal Throttling default restriction table */
+static const struct iwl_tt_restriction restriction_range[IWL_TI_STATE_MAX] = {
+       {IWL_ANT_OK_MULTI, IWL_ANT_OK_MULTI, true },
+       {IWL_ANT_OK_SINGLE, IWL_ANT_OK_MULTI, true },
+       {IWL_ANT_OK_SINGLE, IWL_ANT_OK_SINGLE, false },
+       {IWL_ANT_OK_NONE, IWL_ANT_OK_NONE, false }
+};
 
-       pci_read_config_word(priv->pci_dev, PCI_CFG_LINK_CTRL, &pci_pm);
 
-       IWL_DEBUG_POWER("adjust power command flags\n");
+static void iwl_power_sleep_cam_cmd(struct iwl_priv *priv,
+                                   struct iwl_powertable_cmd *cmd)
+{
+       memset(cmd, 0, sizeof(*cmd));
 
-       for (i = 0; i < IWL_POWER_MAX; i++) {
-               cmd = &pow_data->pwr_range_0[i].cmd;
+       if (priv->power_data.pci_pm)
+               cmd->flags |= IWL_POWER_PCI_PM_MSK;
 
-               if (pci_pm & PCI_CFG_LINK_CTRL_VAL_L0S_EN)
-                       cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
-               else
-                       cmd->flags |= IWL_POWER_PCI_PM_MSK;
-       }
-       return 0;
+       IWL_DEBUG_POWER(priv, "Sleep command for CAM\n");
 }
 
-/* adjust power command according to dtim period and power level*/
-static int iwl_update_power_command(struct iwl_priv *priv,
-                                   struct iwl_powertable_cmd *cmd,
-                                   u16 mode)
+static void iwl_power_fill_sleep_cmd(struct iwl_priv *priv,
+                                    struct iwl_powertable_cmd *cmd,
+                                    int dynps_ms, int wakeup_period)
 {
-       int ret = 0, i;
-       u8 skip;
-       u32 max_sleep = 0;
-       struct iwl_power_vec_entry *range;
-       u8 period = 0;
-       struct iwl_power_mgr *pow_data;
-
-       if (mode > IWL_POWER_INDEX_5) {
-               IWL_DEBUG_POWER("Error invalid power mode \n");
-               return -1;
-       }
-       pow_data = &(priv->power_data);
+       /*
+        * These are the original power level 3 sleep successions. The
+        * device may behave better with such succession and was also
+        * only tested with that. Just like the original sleep commands,
+        * also adjust the succession here to the wakeup_period below.
+        * The ranges are the same as for the sleep commands, 0-2, 3-9
+        * and >10, which is selected based on the DTIM interval for
+        * the sleep index but here we use the wakeup period since that
+        * is what we need to do for the latency requirements.
+        */
+       static const u8 slp_succ_r0[IWL_POWER_VEC_SIZE] = { 2, 2, 2, 2, 2 };
+       static const u8 slp_succ_r1[IWL_POWER_VEC_SIZE] = { 2, 4, 6, 7, 9 };
+       static const u8 slp_succ_r2[IWL_POWER_VEC_SIZE] = { 2, 7, 9, 9, 0xFF };
+       const u8 *slp_succ = slp_succ_r0;
+       int i;
 
-       if (pow_data->dtim_period <= IWL_POWER_RANGE_0_MAX)
-               range = &pow_data->pwr_range_0[0];
-       else if (pow_data->dtim_period <= IWL_POWER_RANGE_1_MAX)
-               range = &pow_data->pwr_range_1[0];
-       else
-               range = &pow_data->pwr_range_2[0];
+       if (wakeup_period > IWL_DTIM_RANGE_0_MAX)
+               slp_succ = slp_succ_r1;
+       if (wakeup_period > IWL_DTIM_RANGE_1_MAX)
+               slp_succ = slp_succ_r2;
 
-       period = pow_data->dtim_period;
-       memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
+       memset(cmd, 0, sizeof(*cmd));
 
-       if (period == 0) {
-               period = 1;
-               skip = 0;
-       } else
-               skip = range[mode].no_dtim;
+       cmd->flags = IWL_POWER_DRIVER_ALLOW_SLEEP_MSK |
+                    IWL_POWER_FAST_PD; /* no use seeing frames for others */
 
-       if (skip == 0) {
-               max_sleep = period;
-               cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
-       } else {
-               __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
-               max_sleep = le32_to_cpu(slp_itrvl);
-               if (max_sleep == 0xFF)
-                       max_sleep = period * (skip + 1);
-               else if (max_sleep >  period)
-                       max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
-               cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
-       }
+       if (priv->power_data.pci_pm)
+               cmd->flags |= IWL_POWER_PCI_PM_MSK;
 
-       for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
-               if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
-                       cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
-       }
+       cmd->rx_data_timeout = cpu_to_le32(1000 * dynps_ms);
+       cmd->tx_data_timeout = cpu_to_le32(1000 * dynps_ms);
+
+       for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
+               cmd->sleep_interval[i] =
+                       cpu_to_le32(min_t(int, slp_succ[i], wakeup_period));
+
+       IWL_DEBUG_POWER(priv, "Automatic sleep command\n");
+}
 
-       IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
-       IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
-       IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
-       IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
+static int iwl_set_power(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd)
+{
+       IWL_DEBUG_POWER(priv, "Sending power/sleep command\n");
+       IWL_DEBUG_POWER(priv, "Flags value = 0x%08X\n", cmd->flags);
+       IWL_DEBUG_POWER(priv, "Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
+       IWL_DEBUG_POWER(priv, "Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
+       IWL_DEBUG_POWER(priv, "Sleep interval vector = { %d , %d , %d , %d , %d }\n",
                        le32_to_cpu(cmd->sleep_interval[0]),
                        le32_to_cpu(cmd->sleep_interval[1]),
                        le32_to_cpu(cmd->sleep_interval[2]),
                        le32_to_cpu(cmd->sleep_interval[3]),
                        le32_to_cpu(cmd->sleep_interval[4]));
 
-       return ret;
+       return iwl_send_cmd_pdu(priv, POWER_TABLE_CMD,
+                               sizeof(struct iwl_powertable_cmd), cmd);
 }
 
 
-/*
- * compute the final power mode index
- */
 int iwl_power_update_mode(struct iwl_priv *priv, bool force)
 {
-       struct iwl_power_mgr *setting = &(priv->power_data);
        int ret = 0;
-       u16 uninitialized_var(final_mode);
+       struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
+       bool enabled = (priv->iw_mode == NL80211_IFTYPE_STATION) &&
+                       (priv->hw->conf.flags & IEEE80211_CONF_PS);
+       bool update_chains;
+       struct iwl_powertable_cmd cmd;
+       int dtimper;
 
        /* Don't update the RX chain when chain noise calibration is running */
-       if (priv->chain_noise_data.state != IWL_CHAIN_NOISE_DONE &&
-           priv->chain_noise_data.state != IWL_CHAIN_NOISE_ALIVE) {
-               IWL_DEBUG_POWER("Cannot update the power, chain noise "
-                       "calibration running: %d\n",
-                       priv->chain_noise_data.state);
-               return -EAGAIN;
-       }
+       update_chains = priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE ||
+                       priv->chain_noise_data.state == IWL_CHAIN_NOISE_ALIVE;
 
-       /* If on battery, set to 3,
-        * if plugged into AC power, set to CAM ("continuously aware mode"),
-        * else user level */
+       if (priv->vif)
+               dtimper = priv->vif->bss_conf.dtim_period;
+       else
+               dtimper = 1;
+
+       if (priv->cfg->broken_powersave)
+               iwl_power_sleep_cam_cmd(priv, &cmd);
+       else if (priv->cfg->supports_idle &&
+                priv->hw->conf.flags & IEEE80211_CONF_IDLE)
+               iwl_static_sleep_cmd(priv, &cmd, IWL_POWER_INDEX_5, 20);
+       else if (tt->state >= IWL_TI_1)
+               iwl_static_sleep_cmd(priv, &cmd, tt->tt_power_mode, dtimper);
+       else if (!enabled)
+               iwl_power_sleep_cam_cmd(priv, &cmd);
+       else if (priv->power_data.debug_sleep_level_override >= 0)
+               iwl_static_sleep_cmd(priv, &cmd,
+                                    priv->power_data.debug_sleep_level_override,
+                                    dtimper);
+       else if (no_sleep_autoadjust)
+               iwl_static_sleep_cmd(priv, &cmd, IWL_POWER_INDEX_1, dtimper);
+       else
+               iwl_power_fill_sleep_cmd(priv, &cmd,
+                                        priv->hw->conf.dynamic_ps_timeout,
+                                        priv->hw->conf.max_sleep_period);
 
-       switch (setting->system_power_setting) {
-       case IWL_POWER_SYS_AUTO:
-               final_mode = iwl_get_auto_power_mode(priv);
-               break;
-       case IWL_POWER_SYS_BATTERY:
-               final_mode = IWL_POWER_INDEX_3;
-               break;
-       case IWL_POWER_SYS_AC:
-               final_mode = IWL_POWER_MODE_CAM;
-               break;
-       default:
-               final_mode = IWL_POWER_INDEX_3;
-               WARN_ON(1);
-       }
+       if (iwl_is_ready_rf(priv) &&
+           (memcmp(&priv->power_data.sleep_cmd, &cmd, sizeof(cmd)) || force)) {
+               if (cmd.flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK)
+                       set_bit(STATUS_POWER_PMI, &priv->status);
 
-       if (setting->critical_power_setting > final_mode)
-               final_mode = setting->critical_power_setting;
+               ret = iwl_set_power(priv, &cmd);
+               if (!ret) {
+                       if (!(cmd.flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK))
+                               clear_bit(STATUS_POWER_PMI, &priv->status);
+
+                       if (priv->cfg->ops->lib->update_chain_flags &&
+                           update_chains)
+                               priv->cfg->ops->lib->update_chain_flags(priv);
+                       else if (priv->cfg->ops->lib->update_chain_flags)
+                               IWL_DEBUG_POWER(priv,
+                                       "Cannot update the power, chain noise "
+                                       "calibration running: %d\n",
+                                       priv->chain_noise_data.state);
+                       memcpy(&priv->power_data.sleep_cmd, &cmd, sizeof(cmd));
+               } else
+                       IWL_ERR(priv, "set power fail, ret = %d", ret);
+       }
 
-       /* driver only support CAM for non STA network */
-       if (priv->iw_mode != IEEE80211_IF_TYPE_STA)
-               final_mode = IWL_POWER_MODE_CAM;
+       return ret;
+}
+EXPORT_SYMBOL(iwl_power_update_mode);
 
-       if (!iwl_is_rfkill(priv) && !setting->power_disabled &&
-           ((setting->power_mode != final_mode) || force)) {
-               struct iwl_powertable_cmd cmd;
+bool iwl_ht_enabled(struct iwl_priv *priv)
+{
+       struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
+       struct iwl_tt_restriction *restriction;
 
-               if (final_mode != IWL_POWER_MODE_CAM)
-                       set_bit(STATUS_POWER_PMI, &priv->status);
+       if (!priv->thermal_throttle.advanced_tt)
+               return true;
+       restriction = tt->restriction + tt->state;
+       return restriction->is_ht;
+}
+EXPORT_SYMBOL(iwl_ht_enabled);
 
-               iwl_update_power_command(priv, &cmd, final_mode);
-               cmd.keep_alive_beacons = 0;
+bool iwl_within_ct_kill_margin(struct iwl_priv *priv)
+{
+       s32 temp = priv->temperature; /* degrees CELSIUS except 4965 */
+       bool within_margin = false;
 
-               if (final_mode == IWL_POWER_INDEX_5)
-                       cmd.flags |= IWL_POWER_FAST_PD;
+       if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
+               temp = KELVIN_TO_CELSIUS(priv->temperature);
 
-               ret = iwl_set_power(priv, &cmd);
+       if (!priv->thermal_throttle.advanced_tt)
+               within_margin = ((temp + IWL_TT_CT_KILL_MARGIN) >=
+                               CT_KILL_THRESHOLD_LEGACY) ? true : false;
+       else
+               within_margin = ((temp + IWL_TT_CT_KILL_MARGIN) >=
+                               CT_KILL_THRESHOLD) ? true : false;
+       return within_margin;
+}
 
-               if (final_mode == IWL_POWER_MODE_CAM)
-                       clear_bit(STATUS_POWER_PMI, &priv->status);
-               else
-                       set_bit(STATUS_POWER_PMI, &priv->status);
+enum iwl_antenna_ok iwl_tx_ant_restriction(struct iwl_priv *priv)
+{
+       struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
+       struct iwl_tt_restriction *restriction;
 
-               if (priv->cfg->ops->lib->update_chain_flags)
-                       priv->cfg->ops->lib->update_chain_flags(priv);
+       if (!priv->thermal_throttle.advanced_tt)
+               return IWL_ANT_OK_MULTI;
+       restriction = tt->restriction + tt->state;
+       return restriction->tx_stream;
+}
+EXPORT_SYMBOL(iwl_tx_ant_restriction);
 
-               if (!ret)
-                       setting->power_mode = final_mode;
-       }
+enum iwl_antenna_ok iwl_rx_ant_restriction(struct iwl_priv *priv)
+{
+       struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
+       struct iwl_tt_restriction *restriction;
 
-       return ret;
+       if (!priv->thermal_throttle.advanced_tt)
+               return IWL_ANT_OK_MULTI;
+       restriction = tt->restriction + tt->state;
+       return restriction->rx_stream;
 }
-EXPORT_SYMBOL(iwl_power_update_mode);
 
-/* Allow other iwl code to disable/enable power management active
- * this will be usefull for rate scale to disable PM during heavy
- * Tx/Rx activities
+#define CT_KILL_EXIT_DURATION (5)      /* 5 seconds duration */
+#define CT_KILL_WAITING_DURATION (300) /* 300ms duration */
+
+/*
+ * toggle the bit to wake up uCode and check the temperature
+ * if the temperature is below CT, uCode will stay awake and send card
+ * state notification with CT_KILL bit clear to inform Thermal Throttling
+ * Management to change state. Otherwise, uCode will go back to sleep
+ * without doing anything, driver should continue the 5 seconds timer
+ * to wake up uCode for temperature check until temperature drop below CT
  */
-int iwl_power_disable_management(struct iwl_priv *priv, u32 ms)
+static void iwl_tt_check_exit_ct_kill(unsigned long data)
 {
-       u16 prev_mode;
-       int ret = 0;
-
-       if (priv->power_data.power_disabled)
-               return -EBUSY;
+       struct iwl_priv *priv = (struct iwl_priv *)data;
+       struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
+       unsigned long flags;
 
-       prev_mode = priv->power_data.user_power_setting;
-       priv->power_data.user_power_setting = IWL_POWER_MODE_CAM;
-       ret = iwl_power_update_mode(priv, 0);
-       priv->power_data.power_disabled = 1;
-       priv->power_data.user_power_setting = prev_mode;
-       cancel_delayed_work(&priv->set_power_save);
-       if (ms)
-               queue_delayed_work(priv->workqueue, &priv->set_power_save,
-                                  msecs_to_jiffies(ms));
+       if (test_bit(STATUS_EXIT_PENDING, &priv->status))
+               return;
 
+       if (tt->state == IWL_TI_CT_KILL) {
+               if (priv->thermal_throttle.ct_kill_toggle) {
+                       iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
+                                   CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
+                       priv->thermal_throttle.ct_kill_toggle = false;
+               } else {
+                       iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
+                                   CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
+                       priv->thermal_throttle.ct_kill_toggle = true;
+               }
+               iwl_read32(priv, CSR_UCODE_DRV_GP1);
+               spin_lock_irqsave(&priv->reg_lock, flags);
+               if (!iwl_grab_nic_access(priv))
+                       iwl_release_nic_access(priv);
+               spin_unlock_irqrestore(&priv->reg_lock, flags);
+
+               /* Reschedule the ct_kill timer to occur in
+                * CT_KILL_EXIT_DURATION seconds to ensure we get a
+                * thermal update */
+               IWL_DEBUG_POWER(priv, "schedule ct_kill exit timer\n");
+               mod_timer(&priv->thermal_throttle.ct_kill_exit_tm, jiffies +
+                         CT_KILL_EXIT_DURATION * HZ);
+       }
+}
 
-       return ret;
+static void iwl_perform_ct_kill_task(struct iwl_priv *priv,
+                          bool stop)
+{
+       if (stop) {
+               IWL_DEBUG_POWER(priv, "Stop all queues\n");
+               if (priv->mac80211_registered)
+                       ieee80211_stop_queues(priv->hw);
+               IWL_DEBUG_POWER(priv,
+                               "Schedule 5 seconds CT_KILL Timer\n");
+               mod_timer(&priv->thermal_throttle.ct_kill_exit_tm, jiffies +
+                         CT_KILL_EXIT_DURATION * HZ);
+       } else {
+               IWL_DEBUG_POWER(priv, "Wake all queues\n");
+               if (priv->mac80211_registered)
+                       ieee80211_wake_queues(priv->hw);
+       }
 }
-EXPORT_SYMBOL(iwl_power_disable_management);
 
-/* Allow other iwl code to disable/enable power management active
- * this will be usefull for rate scale to disable PM during hight
- * valume activities
- */
-int iwl_power_enable_management(struct iwl_priv *priv)
+static void iwl_tt_ready_for_ct_kill(unsigned long data)
 {
-       int ret = 0;
+       struct iwl_priv *priv = (struct iwl_priv *)data;
+       struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
 
-       priv->power_data.power_disabled = 0;
-       ret = iwl_power_update_mode(priv, 0);
-       return ret;
+       if (test_bit(STATUS_EXIT_PENDING, &priv->status))
+               return;
+
+       /* temperature timer expired, ready to go into CT_KILL state */
+       if (tt->state != IWL_TI_CT_KILL) {
+               IWL_DEBUG_POWER(priv, "entering CT_KILL state when temperature timer expired\n");
+               tt->state = IWL_TI_CT_KILL;
+               set_bit(STATUS_CT_KILL, &priv->status);
+               iwl_perform_ct_kill_task(priv, true);
+       }
 }
-EXPORT_SYMBOL(iwl_power_enable_management);
 
-/* set user_power_setting */
-int iwl_power_set_user_mode(struct iwl_priv *priv, u16 mode)
+static void iwl_prepare_ct_kill_task(struct iwl_priv *priv)
 {
-       if (mode > IWL_POWER_LIMIT)
-               return -EINVAL;
+       IWL_DEBUG_POWER(priv, "Prepare to enter IWL_TI_CT_KILL\n");
+       /* make request to retrieve statistics information */
+       iwl_send_statistics_request(priv, 0);
+       /* Reschedule the ct_kill wait timer */
+       mod_timer(&priv->thermal_throttle.ct_kill_waiting_tm,
+                jiffies + msecs_to_jiffies(CT_KILL_WAITING_DURATION));
+}
 
-       priv->power_data.user_power_setting = mode;
+#define IWL_MINIMAL_POWER_THRESHOLD            (CT_KILL_THRESHOLD_LEGACY)
+#define IWL_REDUCED_PERFORMANCE_THRESHOLD_2    (100)
+#define IWL_REDUCED_PERFORMANCE_THRESHOLD_1    (90)
+
+/*
+ * Legacy thermal throttling
+ * 1) Avoid NIC destruction due to high temperatures
+ *     Chip will identify dangerously high temperatures that can
+ *     harm the device and will power down
+ * 2) Avoid the NIC power down due to high temperature
+ *     Throttle early enough to lower the power consumption before
+ *     drastic steps are needed
+ */
+static void iwl_legacy_tt_handler(struct iwl_priv *priv, s32 temp, bool force)
+{
+       struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
+       enum iwl_tt_state old_state;
+
+#ifdef CONFIG_IWLWIFI_DEBUG
+       if ((tt->tt_previous_temp) &&
+           (temp > tt->tt_previous_temp) &&
+           ((temp - tt->tt_previous_temp) >
+           IWL_TT_INCREASE_MARGIN)) {
+               IWL_DEBUG_POWER(priv,
+                       "Temperature increase %d degree Celsius\n",
+                       (temp - tt->tt_previous_temp));
+       }
+#endif
+       old_state = tt->state;
+       /* in Celsius */
+       if (temp >= IWL_MINIMAL_POWER_THRESHOLD)
+               tt->state = IWL_TI_CT_KILL;
+       else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_2)
+               tt->state = IWL_TI_2;
+       else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_1)
+               tt->state = IWL_TI_1;
+       else
+               tt->state = IWL_TI_0;
+
+#ifdef CONFIG_IWLWIFI_DEBUG
+       tt->tt_previous_temp = temp;
+#endif
+       /* stop ct_kill_waiting_tm timer */
+       del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
+       if (tt->state != old_state) {
+               switch (tt->state) {
+               case IWL_TI_0:
+                       /*
+                        * When the system is ready to go back to IWL_TI_0
+                        * we only have to call iwl_power_update_mode() to
+                        * do so.
+                        */
+                       break;
+               case IWL_TI_1:
+                       tt->tt_power_mode = IWL_POWER_INDEX_3;
+                       break;
+               case IWL_TI_2:
+                       tt->tt_power_mode = IWL_POWER_INDEX_4;
+                       break;
+               default:
+                       tt->tt_power_mode = IWL_POWER_INDEX_5;
+                       break;
+               }
+               mutex_lock(&priv->mutex);
+               if (old_state == IWL_TI_CT_KILL)
+                       clear_bit(STATUS_CT_KILL, &priv->status);
+               if (tt->state != IWL_TI_CT_KILL &&
+                   iwl_power_update_mode(priv, true)) {
+                       /* TT state not updated
+                        * try again during next temperature read
+                        */
+                       if (old_state == IWL_TI_CT_KILL)
+                               set_bit(STATUS_CT_KILL, &priv->status);
+                       tt->state = old_state;
+                       IWL_ERR(priv, "Cannot update power mode, "
+                                       "TT state not updated\n");
+               } else {
+                       if (tt->state == IWL_TI_CT_KILL) {
+                               if (force) {
+                                       set_bit(STATUS_CT_KILL, &priv->status);
+                                       iwl_perform_ct_kill_task(priv, true);
+                               } else {
+                                       iwl_prepare_ct_kill_task(priv);
+                                       tt->state = old_state;
+                               }
+                       } else if (old_state == IWL_TI_CT_KILL &&
+                                tt->state != IWL_TI_CT_KILL)
+                               iwl_perform_ct_kill_task(priv, false);
+                       IWL_DEBUG_POWER(priv, "Temperature state changed %u\n",
+                                       tt->state);
+                       IWL_DEBUG_POWER(priv, "Power Index change to %u\n",
+                                       tt->tt_power_mode);
+               }
+               mutex_unlock(&priv->mutex);
+       }
+}
 
-       return iwl_power_update_mode(priv, 0);
+/*
+ * Advance thermal throttling
+ * 1) Avoid NIC destruction due to high temperatures
+ *     Chip will identify dangerously high temperatures that can
+ *     harm the device and will power down
+ * 2) Avoid the NIC power down due to high temperature
+ *     Throttle early enough to lower the power consumption before
+ *     drastic steps are needed
+ *     Actions include relaxing the power down sleep thresholds and
+ *     decreasing the number of TX streams
+ * 3) Avoid throughput performance impact as much as possible
+ *
+ *=============================================================================
+ *                 Condition Nxt State  Condition Nxt State Condition Nxt State
+ *-----------------------------------------------------------------------------
+ *     IWL_TI_0     T >= 114   CT_KILL  114>T>=105   TI_1      N/A      N/A
+ *     IWL_TI_1     T >= 114   CT_KILL  114>T>=110   TI_2     T<=95     TI_0
+ *     IWL_TI_2     T >= 114   CT_KILL                        T<=100    TI_1
+ *    IWL_CT_KILL      N/A       N/A       N/A        N/A     T<=95     TI_0
+ *=============================================================================
+ */
+static void iwl_advance_tt_handler(struct iwl_priv *priv, s32 temp, bool force)
+{
+       struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
+       int i;
+       bool changed = false;
+       enum iwl_tt_state old_state;
+       struct iwl_tt_trans *transaction;
+
+       old_state = tt->state;
+       for (i = 0; i < IWL_TI_STATE_MAX - 1; i++) {
+               /* based on the current TT state,
+                * find the curresponding transaction table
+                * each table has (IWL_TI_STATE_MAX - 1) entries
+                * tt->transaction + ((old_state * (IWL_TI_STATE_MAX - 1))
+                * will advance to the correct table.
+                * then based on the current temperature
+                * find the next state need to transaction to
+                * go through all the possible (IWL_TI_STATE_MAX - 1) entries
+                * in the current table to see if transaction is needed
+                */
+               transaction = tt->transaction +
+                       ((old_state * (IWL_TI_STATE_MAX - 1)) + i);
+               if (temp >= transaction->tt_low &&
+                   temp <= transaction->tt_high) {
+#ifdef CONFIG_IWLWIFI_DEBUG
+                       if ((tt->tt_previous_temp) &&
+                           (temp > tt->tt_previous_temp) &&
+                           ((temp - tt->tt_previous_temp) >
+                           IWL_TT_INCREASE_MARGIN)) {
+                               IWL_DEBUG_POWER(priv,
+                                       "Temperature increase %d "
+                                       "degree Celsius\n",
+                                       (temp - tt->tt_previous_temp));
+                       }
+                       tt->tt_previous_temp = temp;
+#endif
+                       if (old_state !=
+                           transaction->next_state) {
+                               changed = true;
+                               tt->state =
+                                       transaction->next_state;
+                       }
+                       break;
+               }
+       }
+       /* stop ct_kill_waiting_tm timer */
+       del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
+       if (changed) {
+               struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
+
+               if (tt->state >= IWL_TI_1) {
+                       /* force PI = IWL_POWER_INDEX_5 in the case of TI > 0 */
+                       tt->tt_power_mode = IWL_POWER_INDEX_5;
+                       if (!iwl_ht_enabled(priv))
+                               /* disable HT */
+                               rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
+                                       RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
+                                       RXON_FLG_HT40_PROT_MSK |
+                                       RXON_FLG_HT_PROT_MSK);
+                       else {
+                               /* check HT capability and set
+                                * according to the system HT capability
+                                * in case get disabled before */
+                               iwl_set_rxon_ht(priv, &priv->current_ht_config);
+                       }
+
+               } else {
+                       /*
+                        * restore system power setting -- it will be
+                        * recalculated automatically.
+                        */
+
+                       /* check HT capability and set
+                        * according to the system HT capability
+                        * in case get disabled before */
+                       iwl_set_rxon_ht(priv, &priv->current_ht_config);
+               }
+               mutex_lock(&priv->mutex);
+               if (old_state == IWL_TI_CT_KILL)
+                       clear_bit(STATUS_CT_KILL, &priv->status);
+               if (tt->state != IWL_TI_CT_KILL &&
+                   iwl_power_update_mode(priv, true)) {
+                       /* TT state not updated
+                        * try again during next temperature read
+                        */
+                       IWL_ERR(priv, "Cannot update power mode, "
+                                       "TT state not updated\n");
+                       if (old_state == IWL_TI_CT_KILL)
+                               set_bit(STATUS_CT_KILL, &priv->status);
+                       tt->state = old_state;
+               } else {
+                       IWL_DEBUG_POWER(priv,
+                                       "Thermal Throttling to new state: %u\n",
+                                       tt->state);
+                       if (old_state != IWL_TI_CT_KILL &&
+                           tt->state == IWL_TI_CT_KILL) {
+                               if (force) {
+                                       IWL_DEBUG_POWER(priv,
+                                               "Enter IWL_TI_CT_KILL\n");
+                                       set_bit(STATUS_CT_KILL, &priv->status);
+                                       iwl_perform_ct_kill_task(priv, true);
+                               } else {
+                                       iwl_prepare_ct_kill_task(priv);
+                                       tt->state = old_state;
+                               }
+                       } else if (old_state == IWL_TI_CT_KILL &&
+                                 tt->state != IWL_TI_CT_KILL) {
+                               IWL_DEBUG_POWER(priv, "Exit IWL_TI_CT_KILL\n");
+                               iwl_perform_ct_kill_task(priv, false);
+                       }
+               }
+               mutex_unlock(&priv->mutex);
+       }
 }
-EXPORT_SYMBOL(iwl_power_set_user_mode);
 
-/* set system_power_setting. This should be set by over all
- * PM application.
+/* Card State Notification indicated reach critical temperature
+ * if PSP not enable, no Thermal Throttling function will be performed
+ * just set the GP1 bit to acknowledge the event
+ * otherwise, go into IWL_TI_CT_KILL state
+ * since Card State Notification will not provide any temperature reading
+ * for Legacy mode
+ * so just pass the CT_KILL temperature to iwl_legacy_tt_handler()
+ * for advance mode
+ * pass CT_KILL_THRESHOLD+1 to make sure move into IWL_TI_CT_KILL state
  */
-int iwl_power_set_system_mode(struct iwl_priv *priv, u16 mode)
+static void iwl_bg_ct_enter(struct work_struct *work)
 {
-       if (mode > IWL_POWER_LIMIT)
-               return -EINVAL;
+       struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_enter);
+       struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
 
-       priv->power_data.system_power_setting = mode;
+       if (test_bit(STATUS_EXIT_PENDING, &priv->status))
+               return;
+
+       if (!iwl_is_ready(priv))
+               return;
 
-       return iwl_power_update_mode(priv, 0);
+       if (tt->state != IWL_TI_CT_KILL) {
+               IWL_ERR(priv, "Device reached critical temperature "
+                             "- ucode going to sleep!\n");
+               if (!priv->thermal_throttle.advanced_tt)
+                       iwl_legacy_tt_handler(priv,
+                                             IWL_MINIMAL_POWER_THRESHOLD,
+                                             true);
+               else
+                       iwl_advance_tt_handler(priv,
+                                              CT_KILL_THRESHOLD + 1, true);
+       }
 }
-EXPORT_SYMBOL(iwl_power_set_system_mode);
 
-/* initilize to default */
-void iwl_power_initialize(struct iwl_priv *priv)
+/* Card State Notification indicated out of critical temperature
+ * since Card State Notification will not provide any temperature reading
+ * so pass the IWL_REDUCED_PERFORMANCE_THRESHOLD_2 temperature
+ * to iwl_legacy_tt_handler() to get out of IWL_CT_KILL state
+ */
+static void iwl_bg_ct_exit(struct work_struct *work)
 {
+       struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_exit);
+       struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
+
+       if (test_bit(STATUS_EXIT_PENDING, &priv->status))
+               return;
+
+       if (!iwl_is_ready(priv))
+               return;
 
-       iwl_power_init_handle(priv);
-       priv->power_data.user_power_setting = IWL_POWER_AUTO;
-       priv->power_data.power_disabled = 0;
-       priv->power_data.system_power_setting = IWL_POWER_SYS_AUTO;
-       priv->power_data.is_battery_active = 0;
-       priv->power_data.power_disabled = 0;
-       priv->power_data.critical_power_setting = 0;
+       /* stop ct_kill_exit_tm timer */
+       del_timer_sync(&priv->thermal_throttle.ct_kill_exit_tm);
+
+       if (tt->state == IWL_TI_CT_KILL) {
+               IWL_ERR(priv,
+                       "Device temperature below critical"
+                       "- ucode awake!\n");
+               /*
+                * exit from CT_KILL state
+                * reset the current temperature reading
+                */
+               priv->temperature = 0;
+               if (!priv->thermal_throttle.advanced_tt)
+                       iwl_legacy_tt_handler(priv,
+                                             IWL_REDUCED_PERFORMANCE_THRESHOLD_2,
+                                             true);
+               else
+                       iwl_advance_tt_handler(priv, CT_KILL_EXIT_THRESHOLD,
+                                              true);
+       }
 }
-EXPORT_SYMBOL(iwl_power_initialize);
 
-/* set critical_power_setting according to temperature value */
-int iwl_power_temperature_change(struct iwl_priv *priv)
+void iwl_tt_enter_ct_kill(struct iwl_priv *priv)
 {
-       int ret = 0;
-       u16 new_critical = priv->power_data.critical_power_setting;
-       s32 temperature = KELVIN_TO_CELSIUS(priv->last_temperature);
-
-       if (temperature > IWL_CT_KILL_TEMPERATURE)
-               return 0;
-       else if (temperature > IWL_MIN_POWER_TEMPERATURE)
-               new_critical = IWL_POWER_INDEX_5;
-       else if (temperature > IWL_REDUCED_POWER_TEMPERATURE)
-               new_critical = IWL_POWER_INDEX_3;
-       else
-               new_critical = IWL_POWER_MODE_CAM;
+       if (test_bit(STATUS_EXIT_PENDING, &priv->status))
+               return;
 
-       if (new_critical != priv->power_data.critical_power_setting)
-               priv->power_data.critical_power_setting = new_critical;
+       IWL_DEBUG_POWER(priv, "Queueing critical temperature enter.\n");
+       queue_work(priv->workqueue, &priv->ct_enter);
+}
+EXPORT_SYMBOL(iwl_tt_enter_ct_kill);
 
-       if (priv->power_data.critical_power_setting >
-                               priv->power_data.power_mode)
-               ret = iwl_power_update_mode(priv, 0);
+void iwl_tt_exit_ct_kill(struct iwl_priv *priv)
+{
+       if (test_bit(STATUS_EXIT_PENDING, &priv->status))
+               return;
 
-       return ret;
+       IWL_DEBUG_POWER(priv, "Queueing critical temperature exit.\n");
+       queue_work(priv->workqueue, &priv->ct_exit);
 }
-EXPORT_SYMBOL(iwl_power_temperature_change);
+EXPORT_SYMBOL(iwl_tt_exit_ct_kill);
 
-static void iwl_bg_set_power_save(struct work_struct *work)
+static void iwl_bg_tt_work(struct work_struct *work)
 {
-       struct iwl_priv *priv = container_of(work,
-                               struct iwl_priv, set_power_save.work);
-       IWL_DEBUG(IWL_DL_STATE, "update power\n");
+       struct iwl_priv *priv = container_of(work, struct iwl_priv, tt_work);
+       s32 temp = priv->temperature; /* degrees CELSIUS except 4965 */
 
        if (test_bit(STATUS_EXIT_PENDING, &priv->status))
                return;
 
-       mutex_lock(&priv->mutex);
+       if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
+               temp = KELVIN_TO_CELSIUS(priv->temperature);
 
-       /* on starting association we disable power managment
-        * until association, if association failed then this
-        * timer will expire and enable PM again.
-        */
-       if (!iwl_is_associated(priv))
-               iwl_power_enable_management(priv);
+       if (!priv->thermal_throttle.advanced_tt)
+               iwl_legacy_tt_handler(priv, temp, false);
+       else
+               iwl_advance_tt_handler(priv, temp, false);
+}
+
+void iwl_tt_handler(struct iwl_priv *priv)
+{
+       if (test_bit(STATUS_EXIT_PENDING, &priv->status))
+               return;
+
+       IWL_DEBUG_POWER(priv, "Queueing thermal throttling work.\n");
+       queue_work(priv->workqueue, &priv->tt_work);
+}
+EXPORT_SYMBOL(iwl_tt_handler);
 
-       mutex_unlock(&priv->mutex);
+/* Thermal throttling initialization
+ * For advance thermal throttling:
+ *     Initialize Thermal Index and temperature threshold table
+ *     Initialize thermal throttling restriction table
+ */
+void iwl_tt_initialize(struct iwl_priv *priv)
+{
+       struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
+       int size = sizeof(struct iwl_tt_trans) * (IWL_TI_STATE_MAX - 1);
+       struct iwl_tt_trans *transaction;
+
+       IWL_DEBUG_POWER(priv, "Initialize Thermal Throttling \n");
+
+       memset(tt, 0, sizeof(struct iwl_tt_mgmt));
+
+       tt->state = IWL_TI_0;
+       init_timer(&priv->thermal_throttle.ct_kill_exit_tm);
+       priv->thermal_throttle.ct_kill_exit_tm.data = (unsigned long)priv;
+       priv->thermal_throttle.ct_kill_exit_tm.function =
+               iwl_tt_check_exit_ct_kill;
+       init_timer(&priv->thermal_throttle.ct_kill_waiting_tm);
+       priv->thermal_throttle.ct_kill_waiting_tm.data = (unsigned long)priv;
+       priv->thermal_throttle.ct_kill_waiting_tm.function =
+               iwl_tt_ready_for_ct_kill;
+       /* setup deferred ct kill work */
+       INIT_WORK(&priv->tt_work, iwl_bg_tt_work);
+       INIT_WORK(&priv->ct_enter, iwl_bg_ct_enter);
+       INIT_WORK(&priv->ct_exit, iwl_bg_ct_exit);
+
+       switch (priv->hw_rev & CSR_HW_REV_TYPE_MSK) {
+       case CSR_HW_REV_TYPE_6x00:
+       case CSR_HW_REV_TYPE_6x50:
+               IWL_DEBUG_POWER(priv, "Advanced Thermal Throttling\n");
+               tt->restriction = kzalloc(sizeof(struct iwl_tt_restriction) *
+                                        IWL_TI_STATE_MAX, GFP_KERNEL);
+               tt->transaction = kzalloc(sizeof(struct iwl_tt_trans) *
+                       IWL_TI_STATE_MAX * (IWL_TI_STATE_MAX - 1),
+                       GFP_KERNEL);
+               if (!tt->restriction || !tt->transaction) {
+                       IWL_ERR(priv, "Fallback to Legacy Throttling\n");
+                       priv->thermal_throttle.advanced_tt = false;
+                       kfree(tt->restriction);
+                       tt->restriction = NULL;
+                       kfree(tt->transaction);
+                       tt->transaction = NULL;
+               } else {
+                       transaction = tt->transaction +
+                               (IWL_TI_0 * (IWL_TI_STATE_MAX - 1));
+                       memcpy(transaction, &tt_range_0[0], size);
+                       transaction = tt->transaction +
+                               (IWL_TI_1 * (IWL_TI_STATE_MAX - 1));
+                       memcpy(transaction, &tt_range_1[0], size);
+                       transaction = tt->transaction +
+                               (IWL_TI_2 * (IWL_TI_STATE_MAX - 1));
+                       memcpy(transaction, &tt_range_2[0], size);
+                       transaction = tt->transaction +
+                               (IWL_TI_CT_KILL * (IWL_TI_STATE_MAX - 1));
+                       memcpy(transaction, &tt_range_3[0], size);
+                       size = sizeof(struct iwl_tt_restriction) *
+                               IWL_TI_STATE_MAX;
+                       memcpy(tt->restriction,
+                               &restriction_range[0], size);
+                       priv->thermal_throttle.advanced_tt = true;
+               }
+               break;
+       default:
+               IWL_DEBUG_POWER(priv, "Legacy Thermal Throttling\n");
+               priv->thermal_throttle.advanced_tt = false;
+               break;
+       }
 }
-void iwl_setup_power_deferred_work(struct iwl_priv *priv)
+EXPORT_SYMBOL(iwl_tt_initialize);
+
+/* cleanup thermal throttling management related memory and timer */
+void iwl_tt_exit(struct iwl_priv *priv)
 {
-       INIT_DELAYED_WORK(&priv->set_power_save, iwl_bg_set_power_save);
+       struct iwl_tt_mgmt *tt = &priv->thermal_throttle;
+
+       /* stop ct_kill_exit_tm timer if activated */
+       del_timer_sync(&priv->thermal_throttle.ct_kill_exit_tm);
+       /* stop ct_kill_waiting_tm timer if activated */
+       del_timer_sync(&priv->thermal_throttle.ct_kill_waiting_tm);
+       cancel_work_sync(&priv->tt_work);
+       cancel_work_sync(&priv->ct_enter);
+       cancel_work_sync(&priv->ct_exit);
+
+       if (priv->thermal_throttle.advanced_tt) {
+               /* free advance thermal throttling memory */
+               kfree(tt->restriction);
+               tt->restriction = NULL;
+               kfree(tt->transaction);
+               tt->transaction = NULL;
+       }
 }
-EXPORT_SYMBOL(iwl_setup_power_deferred_work);
+EXPORT_SYMBOL(iwl_tt_exit);
 
-void iwl_power_cancel_timeout(struct iwl_priv *priv)
+/* initialize to default */
+void iwl_power_initialize(struct iwl_priv *priv)
 {
-       cancel_delayed_work(&priv->set_power_save);
+       u16 lctl = iwl_pcie_link_ctl(priv);
+
+       priv->power_data.pci_pm = !(lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN);
+
+       priv->power_data.debug_sleep_level_override = -1;
+
+       memset(&priv->power_data.sleep_cmd, 0,
+               sizeof(priv->power_data.sleep_cmd));
 }
-EXPORT_SYMBOL(iwl_power_cancel_timeout);
+EXPORT_SYMBOL(iwl_power_initialize);