iwl-power.c 27.3 KB
Newer Older
M
Mohamed Abbas 已提交
1 2
/******************************************************************************
 *
3
 * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
M
Mohamed Abbas 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * Portions of this file are derived from the ipw3945 project, as well
 * as portions of the ieee80211 subsystem header files.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
 *
 * The full GNU General Public License is included in this distribution in the
 * file called LICENSE.
 *
 * Contact Information:
25
 *  Intel Linux Wireless <ilw@linux.intel.com>
M
Mohamed Abbas 已提交
26 27 28 29 30 31 32 33 34 35 36
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *****************************************************************************/


#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>

#include <net/mac80211.h>

#include "iwl-eeprom.h"
37
#include "iwl-dev.h"
M
Mohamed Abbas 已提交
38
#include "iwl-core.h"
39
#include "iwl-io.h"
40
#include "iwl-commands.h"
M
Mohamed Abbas 已提交
41 42 43 44
#include "iwl-debug.h"
#include "iwl-power.h"

/*
J
Johannes Berg 已提交
45
 * Setting power level allow the card to go to sleep when not busy.
M
Mohamed Abbas 已提交
46
 *
J
Johannes Berg 已提交
47 48 49
 * The power level is set to INDEX_1 (the least deep state) by
 * default, and will, in the future, be the deepest state unless
 * otherwise required by pm_qos network latency requirements.
M
Mohamed Abbas 已提交
50
 *
J
Johannes Berg 已提交
51 52 53
 * Using INDEX_1 without pm_qos is ok because mac80211 will disable
 * PS when even checking every beacon for the TIM bit would exceed
 * the required latency.
M
Mohamed Abbas 已提交
54 55 56 57 58 59
 */

#define IWL_POWER_RANGE_0_MAX  (2)
#define IWL_POWER_RANGE_1_MAX  (10)


J
Johannes Berg 已提交
60 61 62 63 64 65 66 67 68
#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)}
M
Mohamed Abbas 已提交
69
/* default power management (not Tx power) table values */
J
Johannes Berg 已提交
70 71
/* for DTIM period 0 through IWL_POWER_RANGE_0_MAX */
static const struct iwl_power_vec_entry range_0[IWL_POWER_NUM] = {
M
Mohamed Abbas 已提交
72 73 74 75 76 77 78 79 80
	{{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
	{{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},
	{{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 2, 4, 4, 0xFF)}, 1},
	{{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 2, 4, 6, 0xFF)}, 2}
};


J
Johannes Berg 已提交
81 82
/* for DTIM period IWL_POWER_RANGE_0_MAX + 1 through IWL_POWER_RANGE_1_MAX */
static const struct iwl_power_vec_entry range_1[IWL_POWER_NUM] = {
M
Mohamed Abbas 已提交
83 84 85 86 87 88 89 90
	{{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
	{{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},
	{{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 4, 6, 9, 10)}, 1},
	{{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 4, 7, 10, 10)}, 2}
};

J
Johannes Berg 已提交
91 92
/* for DTIM period > IWL_POWER_RANGE_1_MAX */
static const struct iwl_power_vec_entry range_2[IWL_POWER_NUM] = {
M
Mohamed Abbas 已提交
93 94 95 96 97 98 99 100
	{{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
	{{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},
	{{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
	{{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
};

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
/* default Thermal Throttling transaction table
 * Current state   |         Throttling Down               |  Throttling Up
 *=============================================================================
 *                 Condition Nxt State  Condition Nxt State Condition Nxt State
 *-----------------------------------------------------------------------------
 *     IWL_TI_0     T >= 115   CT_KILL  115>T>=105   TI_1      N/A      N/A
 *     IWL_TI_1     T >= 115   CT_KILL  115>T>=110   TI_2     T<=95     TI_0
 *     IWL_TI_2     T >= 115   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},
	{IWL_TI_CT_KILL, CT_KILL_THRESHOLD + 1, 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},
	{IWL_TI_CT_KILL, CT_KILL_THRESHOLD + 1, 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 + 1, IWL_ABSOLUTE_MAX},
	{IWL_TI_CT_KILL, CT_KILL_THRESHOLD + 1, 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}
};

/* Advance Thermal Throttling default restriction table */
static const struct iwl_tt_restriction restriction_range[IWL_TI_STATE_MAX] = {
	{IWL_TX_MULTI, true, IWL_RX_MULTI},
	{IWL_TX_SINGLE, true, IWL_RX_MULTI},
	{IWL_TX_SINGLE, false, IWL_RX_SINGLE},
	{IWL_TX_NONE, false, IWL_RX_NONE}
};
140

141 142 143
/* set card power command */
static int iwl_set_power(struct iwl_priv *priv, void *cmd)
{
144 145
	return iwl_send_cmd_pdu(priv, POWER_TABLE_CMD,
				sizeof(struct iwl_powertable_cmd), cmd);
146
}
M
Mohamed Abbas 已提交
147 148

/* initialize to default */
149
static void iwl_power_init_handle(struct iwl_priv *priv)
M
Mohamed Abbas 已提交
150 151
{
	struct iwl_power_mgr *pow_data;
J
Johannes Berg 已提交
152
	int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_NUM;
153 154
	struct iwl_powertable_cmd *cmd;
	int i;
155
	u16 lctl;
M
Mohamed Abbas 已提交
156

157
	IWL_DEBUG_POWER(priv, "Initialize power \n");
M
Mohamed Abbas 已提交
158

159
	pow_data = &priv->power_data;
M
Mohamed Abbas 已提交
160 161 162 163 164 165 166

	memset(pow_data, 0, sizeof(*pow_data));

	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);

167
	lctl = iwl_pcie_link_ctl(priv);
M
Mohamed Abbas 已提交
168

169
	IWL_DEBUG_POWER(priv, "adjust power command flags\n");
M
Mohamed Abbas 已提交
170

J
Johannes Berg 已提交
171
	for (i = 0; i < IWL_POWER_NUM; i++) {
172
		cmd = &pow_data->pwr_range_0[i].cmd;
M
Mohamed Abbas 已提交
173

174
		if (lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN)
175 176 177
			cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
		else
			cmd->flags |= IWL_POWER_PCI_PM_MSK;
M
Mohamed Abbas 已提交
178 179 180
	}
}

T
Tomas Winkler 已提交
181
/* adjust power command according to DTIM period and power level*/
182 183
static int iwl_update_power_cmd(struct iwl_priv *priv,
				struct iwl_powertable_cmd *cmd, u16 mode)
M
Mohamed Abbas 已提交
184 185 186
{
	struct iwl_power_vec_entry *range;
	struct iwl_power_mgr *pow_data;
187 188 189 190
	int i;
	u32 max_sleep = 0;
	u8 period;
	bool skip;
M
Mohamed Abbas 已提交
191 192

	if (mode > IWL_POWER_INDEX_5) {
193
		IWL_DEBUG_POWER(priv, "Error invalid power mode \n");
194
		return -EINVAL;
M
Mohamed Abbas 已提交
195
	}
196 197

	pow_data = &priv->power_data;
M
Mohamed Abbas 已提交
198 199 200 201 202 203 204 205 206

	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];

	period = pow_data->dtim_period;
207
	memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
M
Mohamed Abbas 已提交
208 209 210

	if (period == 0) {
		period = 1;
211
		skip = false;
M
Mohamed Abbas 已提交
212
	} else {
213 214 215 216
		skip = !!range[mode].no_dtim;
	}

	if (skip) {
M
Mohamed Abbas 已提交
217 218 219 220 221 222 223
		__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;
224 225 226
	} else {
		max_sleep = period;
		cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
M
Mohamed Abbas 已提交
227 228
	}

229
	for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
M
Mohamed Abbas 已提交
230 231 232
		if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
			cmd->sleep_interval[i] = cpu_to_le32(max_sleep);

233 234 235 236
	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",
M
Mohamed Abbas 已提交
237 238 239 240 241 242
			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]));

243
	return 0;
M
Mohamed Abbas 已提交
244 245 246 247
}


/*
248
 * compute the final power mode index
M
Mohamed Abbas 已提交
249
 */
250
int iwl_power_update_mode(struct iwl_priv *priv, bool force)
M
Mohamed Abbas 已提交
251 252 253
{
	struct iwl_power_mgr *setting = &(priv->power_data);
	int ret = 0;
254
	struct iwl_tt_mgmt *tt = &priv->power_data.tt;
M
Mohamed Abbas 已提交
255
	u16 uninitialized_var(final_mode);
256
	bool update_chains;
M
Mohamed Abbas 已提交
257

258
	/* Don't update the RX chain when chain noise calibration is running */
259 260
	update_chains = priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE ||
			priv->chain_noise_data.state == IWL_CHAIN_NOISE_ALIVE;
261

J
Johannes Berg 已提交
262
	final_mode = priv->power_data.user_power_setting;
M
Mohamed Abbas 已提交
263

J
Johannes Berg 已提交
264
	if (setting->power_disabled)
M
Mohamed Abbas 已提交
265 266
		final_mode = IWL_POWER_MODE_CAM;

267 268 269 270
	if (tt->state >= IWL_TI_1) {
		/* TT power setting overwrite user & system power setting */
		final_mode = tt->tt_power_mode;
	}
J
Johannes Berg 已提交
271
	if (iwl_is_ready_rf(priv) &&
272
	    ((setting->power_mode != final_mode) || force)) {
273
		struct iwl_powertable_cmd cmd;
M
Mohamed Abbas 已提交
274 275 276 277

		if (final_mode != IWL_POWER_MODE_CAM)
			set_bit(STATUS_POWER_PMI, &priv->status);

278
		iwl_update_power_cmd(priv, &cmd, final_mode);
M
Mohamed Abbas 已提交
279 280 281 282 283
		cmd.keep_alive_beacons = 0;

		if (final_mode == IWL_POWER_INDEX_5)
			cmd.flags |= IWL_POWER_FAST_PD;

284
		ret = iwl_set_power(priv, &cmd);
M
Mohamed Abbas 已提交
285 286 287 288

		if (final_mode == IWL_POWER_MODE_CAM)
			clear_bit(STATUS_POWER_PMI, &priv->status);

289
		if (priv->cfg->ops->lib->update_chain_flags && update_chains)
M
Mohamed Abbas 已提交
290
			priv->cfg->ops->lib->update_chain_flags(priv);
291
		else
292
			IWL_DEBUG_POWER(priv, "Cannot update the power, chain noise "
293 294
					"calibration running: %d\n",
					priv->chain_noise_data.state);
M
Mohamed Abbas 已提交
295 296 297 298 299 300 301 302 303 304 305
		if (!ret)
			setting->power_mode = final_mode;
	}

	return ret;
}
EXPORT_SYMBOL(iwl_power_update_mode);

/* set user_power_setting */
int iwl_power_set_user_mode(struct iwl_priv *priv, u16 mode)
{
J
Johannes Berg 已提交
306
	if (mode >= IWL_POWER_NUM)
M
Mohamed Abbas 已提交
307 308 309 310
		return -EINVAL;

	priv->power_data.user_power_setting = mode;

311
	return iwl_power_update_mode(priv, 0);
M
Mohamed Abbas 已提交
312 313 314
}
EXPORT_SYMBOL(iwl_power_set_user_mode);

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
bool iwl_ht_enabled(struct iwl_priv *priv)
{
	struct iwl_tt_mgmt *tt = &priv->power_data.tt;
	struct iwl_tt_restriction *restriction;

	if (!priv->power_data.adv_tt)
		return true;
	restriction = tt->restriction + tt->state;
	return restriction->is_ht;
}
EXPORT_SYMBOL(iwl_ht_enabled);

u8 iwl_tx_ant_restriction(struct iwl_priv *priv)
{
	struct iwl_tt_mgmt *tt = &priv->power_data.tt;
	struct iwl_tt_restriction *restriction;

	if (!priv->power_data.adv_tt)
		return IWL_TX_MULTI;
	restriction = tt->restriction + tt->state;
	return restriction->tx_stream;
}
EXPORT_SYMBOL(iwl_tx_ant_restriction);

u8 iwl_rx_ant_restriction(struct iwl_priv *priv)
{
	struct iwl_tt_mgmt *tt = &priv->power_data.tt;
	struct iwl_tt_restriction *restriction;

	if (!priv->power_data.adv_tt)
		return IWL_RX_MULTI;
	restriction = tt->restriction + tt->state;
	return restriction->rx_stream;
}
EXPORT_SYMBOL(iwl_rx_ant_restriction);

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
#define CT_KILL_EXIT_DURATION (5)	/* 5 seconds 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
 */
static void iwl_tt_check_exit_ct_kill(unsigned long data)
{
	struct iwl_priv *priv = (struct iwl_priv *)data;
	struct iwl_tt_mgmt *tt = &priv->power_data.tt;
	unsigned long flags;

	if (test_bit(STATUS_EXIT_PENDING, &priv->status))
		return;

	if (tt->state == IWL_TI_CT_KILL) {
		if (priv->power_data.ct_kill_toggle) {
			iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
				    CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
			priv->power_data.ct_kill_toggle = false;
		} else {
			iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
				    CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
			priv->power_data.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 */
		mod_timer(&priv->power_data.ct_kill_exit_tm, jiffies +
			  CT_KILL_EXIT_DURATION * HZ);
	}
}

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->power_data.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);
	}
}

#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)
{
	struct iwl_tt_mgmt *tt = &priv->power_data.tt;
428
	enum iwl_tt_state old_state;
429 430 431 432 433 434 435 436 437 438 439 440
	struct iwl_power_mgr *setting = &priv->power_data;

#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
441
	old_state = tt->state;
442 443
	/* in Celsius */
	if (temp >= IWL_MINIMAL_POWER_THRESHOLD)
444
		tt->state = IWL_TI_CT_KILL;
445
	else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_2)
446
		tt->state = IWL_TI_2;
447
	else if (temp >= IWL_REDUCED_PERFORMANCE_THRESHOLD_1)
448
		tt->state = IWL_TI_1;
449
	else
450
		tt->state = IWL_TI_0;
451 452 453 454

#ifdef CONFIG_IWLWIFI_DEBUG
	tt->tt_previous_temp = temp;
#endif
455 456
	if (tt->state != old_state) {
		if (old_state == IWL_TI_0) {
457 458 459 460
			tt->sys_power_mode = setting->power_mode;
			IWL_DEBUG_POWER(priv, "current power mode: %u\n",
				setting->power_mode);
		}
461
		switch (tt->state) {
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
		case IWL_TI_0:
			/* when system ready to go back to IWL_TI_0 state
			 * using system power mode instead of TT power mode
			 * revert back to the orginal power mode which was saved
			 * before enter Thermal Throttling state
			 * update priv->power_data.user_power_setting to the
			 * required power mode to make sure
			 * iwl_power_update_mode() will update power correctly.
			 */
			priv->power_data.user_power_setting =
				tt->sys_power_mode;
			tt->tt_power_mode = tt->sys_power_mode;
			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;
		}
485
		mutex_lock(&priv->mutex);
486 487 488 489
		if (iwl_power_update_mode(priv, true)) {
			/* TT state not updated
			 * try again during next temperature read
			 */
490
			tt->state = old_state;
491 492 493
			IWL_ERR(priv, "Cannot update power mode, "
					"TT state not updated\n");
		} else {
494
			if (tt->state == IWL_TI_CT_KILL)
495
				iwl_perform_ct_kill_task(priv, true);
496 497
			else if (old_state == IWL_TI_CT_KILL &&
				 tt->state != IWL_TI_CT_KILL)
498 499 500 501 502 503
				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);
		}
504
		mutex_unlock(&priv->mutex);
505 506 507
	}
}

508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
/*
 * 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 >= 115   CT_KILL  115>T>=105   TI_1      N/A      N/A
 *     IWL_TI_1     T >= 115   CT_KILL  115>T>=110   TI_2     T<=95     TI_0
 *     IWL_TI_2     T >= 115   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)
{
	struct iwl_tt_mgmt *tt = &priv->power_data.tt;
	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;
		}
	}
	if (changed) {
		struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
		struct iwl_power_mgr *setting = &priv->power_data;

		if (tt->state >= IWL_TI_1) {
			/* if switching from IWL_TI_0 to other TT state
			 * save previous power setting in tt->sys_power_mode */
			if (old_state == IWL_TI_0)
				tt->sys_power_mode = setting->power_mode;
			/* 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 |
589
					RXON_FLG_HT40_PROT_MSK |
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
					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 */
			/* the previous power mode was saved in
			 * tt->sys_power_mode when system move into
			 * Thermal Throttling state
			 * set power_data.user_power_setting to the previous
			 * system power mode to make sure power will get
			 * updated correctly
			 */
			priv->power_data.user_power_setting =
				tt->sys_power_mode;
			tt->tt_power_mode = tt->sys_power_mode;
			/* 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);
		}
615
		mutex_lock(&priv->mutex);
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
		if (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");
			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) {
				IWL_DEBUG_POWER(priv, "Enter IWL_TI_CT_KILL\n");
				iwl_perform_ct_kill_task(priv, true);

			} 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);
			}
		}
638
		mutex_unlock(&priv->mutex);
639 640 641
	}
}

642 643 644 645 646
/* 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
647
 * for Legacy mode
648
 * so just pass the CT_KILL temperature to iwl_legacy_tt_handler()
649 650
 * for advance mode
 * pass CT_KILL_THRESHOLD+1 to make sure move into IWL_TI_CT_KILL state
651
 */
652
static void iwl_bg_ct_enter(struct work_struct *work)
653
{
654
	struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_enter);
655 656 657 658 659
	struct iwl_tt_mgmt *tt = &priv->power_data.tt;

	if (test_bit(STATUS_EXIT_PENDING, &priv->status))
		return;

660 661 662
	if (!iwl_is_ready(priv))
		return;

663 664 665
	if (tt->state != IWL_TI_CT_KILL) {
		IWL_ERR(priv, "Device reached critical temperature "
			      "- ucode going to sleep!\n");
666 667 668 669 670 671
		if (!priv->power_data.adv_tt)
			iwl_legacy_tt_handler(priv,
					      IWL_MINIMAL_POWER_THRESHOLD);
		else
			iwl_advance_tt_handler(priv,
					       CT_KILL_THRESHOLD + 1);
672 673 674 675 676 677 678 679
	}
}

/* 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
 */
680
static void iwl_bg_ct_exit(struct work_struct *work)
681
{
682
	struct iwl_priv *priv = container_of(work, struct iwl_priv, ct_exit);
683 684 685 686 687
	struct iwl_tt_mgmt *tt = &priv->power_data.tt;

	if (test_bit(STATUS_EXIT_PENDING, &priv->status))
		return;

688 689 690
	if (!iwl_is_ready(priv))
		return;

691 692 693 694 695 696 697
	/* stop ct_kill_exit_tm timer */
	del_timer_sync(&priv->power_data.ct_kill_exit_tm);

	if (tt->state == IWL_TI_CT_KILL) {
		IWL_ERR(priv,
			"Device temperature below critical"
			"- ucode awake!\n");
698 699 700 701 702
		if (!priv->power_data.adv_tt)
			iwl_legacy_tt_handler(priv,
					IWL_REDUCED_PERFORMANCE_THRESHOLD_2);
		else
			iwl_advance_tt_handler(priv, CT_KILL_EXIT_THRESHOLD);
703 704
	}
}
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723

void iwl_tt_enter_ct_kill(struct iwl_priv *priv)
{
	if (test_bit(STATUS_EXIT_PENDING, &priv->status))
		return;

	IWL_DEBUG_POWER(priv, "Queueing critical temperature enter.\n");
	queue_work(priv->workqueue, &priv->ct_enter);
}
EXPORT_SYMBOL(iwl_tt_enter_ct_kill);

void iwl_tt_exit_ct_kill(struct iwl_priv *priv)
{
	if (test_bit(STATUS_EXIT_PENDING, &priv->status))
		return;

	IWL_DEBUG_POWER(priv, "Queueing critical temperature exit.\n");
	queue_work(priv->workqueue, &priv->ct_exit);
}
724 725
EXPORT_SYMBOL(iwl_tt_exit_ct_kill);

726
static void iwl_bg_tt_work(struct work_struct *work)
727
{
728
	struct iwl_priv *priv = container_of(work, struct iwl_priv, tt_work);
729 730 731 732 733 734 735 736
	s32 temp = priv->temperature; /* degrees CELSIUS except 4965 */

	if (test_bit(STATUS_EXIT_PENDING, &priv->status))
		return;

	if ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)
		temp = KELVIN_TO_CELSIUS(priv->temperature);

737 738 739 740
	if (!priv->power_data.adv_tt)
		iwl_legacy_tt_handler(priv, temp);
	else
		iwl_advance_tt_handler(priv, temp);
741
}
742 743 744 745 746 747 748 749 750

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);
}
751 752 753
EXPORT_SYMBOL(iwl_tt_handler);

/* Thermal throttling initialization
754 755 756
 * For advance thermal throttling:
 *     Initialize Thermal Index and temperature threshold table
 *     Initialize thermal throttling restriction table
757 758 759 760 761
 */
void iwl_tt_initialize(struct iwl_priv *priv)
{
	struct iwl_tt_mgmt *tt = &priv->power_data.tt;
	struct iwl_power_mgr *setting = &priv->power_data;
762 763
	int size = sizeof(struct iwl_tt_trans) * (IWL_TI_STATE_MAX - 1);
	struct iwl_tt_trans *transaction;
764 765 766 767 768 769 770 771 772 773 774

	IWL_DEBUG_POWER(priv, "Initialize Thermal Throttling \n");

	memset(tt, 0, sizeof(struct iwl_tt_mgmt));

	tt->state = IWL_TI_0;
	tt->sys_power_mode = setting->power_mode;
	tt->tt_power_mode = tt->sys_power_mode;
	init_timer(&priv->power_data.ct_kill_exit_tm);
	priv->power_data.ct_kill_exit_tm.data = (unsigned long)priv;
	priv->power_data.ct_kill_exit_tm.function = iwl_tt_check_exit_ct_kill;
775 776 777 778 779 780

	/* 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);

781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
	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->power_data.adv_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->power_data.adv_tt = true;
		}
		break;
	default:
		IWL_DEBUG_POWER(priv, "Legacy Thermal Throttling\n");
		priv->power_data.adv_tt = false;
		break;
	}
822 823 824 825 826 827
}
EXPORT_SYMBOL(iwl_tt_initialize);

/* cleanup thermal throttling management related memory and timer */
void iwl_tt_exit(struct iwl_priv *priv)
{
828 829
	struct iwl_tt_mgmt *tt = &priv->power_data.tt;

830 831
	/* stop ct_kill_exit_tm timer if activated */
	del_timer_sync(&priv->power_data.ct_kill_exit_tm);
832 833 834
	cancel_work_sync(&priv->tt_work);
	cancel_work_sync(&priv->ct_enter);
	cancel_work_sync(&priv->ct_exit);
835 836 837 838 839 840 841 842

	if (priv->power_data.adv_tt) {
		/* free advance thermal throttling memory */
		kfree(tt->restriction);
		tt->restriction = NULL;
		kfree(tt->transaction);
		tt->transaction = NULL;
	}
843 844 845
}
EXPORT_SYMBOL(iwl_tt_exit);

T
Tomas Winkler 已提交
846
/* initialize to default */
M
Mohamed Abbas 已提交
847 848 849
void iwl_power_initialize(struct iwl_priv *priv)
{
	iwl_power_init_handle(priv);
J
Johannes Berg 已提交
850 851 852
	priv->power_data.user_power_setting = IWL_POWER_INDEX_1;
	/* default to disabled until mac80211 says otherwise */
	priv->power_data.power_disabled = 1;
M
Mohamed Abbas 已提交
853 854
}
EXPORT_SYMBOL(iwl_power_initialize);