iwl-power.c 12.4 KB
Newer Older
M
Mohamed Abbas 已提交
1 2
/******************************************************************************
 *
3
 * Copyright(c) 2007 - 2010 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
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *****************************************************************************/


#include <linux/kernel.h>
#include <linux/module.h>
32
#include <linux/slab.h>
M
Mohamed Abbas 已提交
33 34 35 36 37
#include <linux/init.h>

#include <net/mac80211.h>

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

/*
46
 * Setting power level allows the card to go to sleep when not busy.
M
Mohamed Abbas 已提交
47
 *
48 49 50
 * 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.
M
Mohamed Abbas 已提交
51 52
 */

53 54 55 56 57 58 59 60 61
/*
 * 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");
M
Mohamed Abbas 已提交
62

63 64 65 66 67 68 69
/*
 * 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;
W
Wey-Yi Guy 已提交
70
	u8 no_dtim;	/* number of skip dtim */
71 72 73 74
};

#define IWL_DTIM_RANGE_0_MAX	2
#define IWL_DTIM_RANGE_1_MAX	10
M
Mohamed Abbas 已提交
75

J
Johannes Berg 已提交
76 77 78 79 80 81 82 83 84
#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 已提交
85
/* default power management (not Tx power) table values */
86
/* for DTIM period 0 through IWL_DTIM_RANGE_0_MAX */
W
Wey-Yi Guy 已提交
87
/* DTIM 0 - 2 */
J
Johannes Berg 已提交
88
static const struct iwl_power_vec_entry range_0[IWL_POWER_NUM] = {
W
Wey-Yi Guy 已提交
89
	{{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 1, 2, 2, 0xFF)}, 0},
M
Mohamed Abbas 已提交
90 91 92 93 94 95 96
	{{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}
};


97
/* for DTIM period IWL_DTIM_RANGE_0_MAX + 1 through IWL_DTIM_RANGE_1_MAX */
W
Wey-Yi Guy 已提交
98
/* DTIM 3 - 10 */
J
Johannes Berg 已提交
99
static const struct iwl_power_vec_entry range_1[IWL_POWER_NUM] = {
M
Mohamed Abbas 已提交
100 101 102 103
	{{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},
W
Wey-Yi Guy 已提交
104
	{{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 4, 6, 10, 10)}, 2}
M
Mohamed Abbas 已提交
105 106
};

107
/* for DTIM period > IWL_DTIM_RANGE_1_MAX */
W
Wey-Yi Guy 已提交
108
/* DTIM 11 - */
J
Johannes Berg 已提交
109
static const struct iwl_power_vec_entry range_2[IWL_POWER_NUM] = {
M
Mohamed Abbas 已提交
110 111 112 113 114 115 116
	{{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}
};

117 118 119 120 121
static void iwl_static_sleep_cmd(struct iwl_priv *priv,
				 struct iwl_powertable_cmd *cmd,
				 enum iwl_power_level lvl, int period)
{
	const struct iwl_power_vec_entry *table;
W
Wey-Yi Guy 已提交
122 123 124 125
	int max_sleep[IWL_POWER_VEC_SIZE] = { 0 };
	int i;
	u8 skip;
	u32 slp_itrvl;
126 127

	table = range_2;
W
Wey-Yi Guy 已提交
128
	if (period <= IWL_DTIM_RANGE_1_MAX)
129
		table = range_1;
W
Wey-Yi Guy 已提交
130
	if (period <= IWL_DTIM_RANGE_0_MAX)
131 132 133 134 135 136 137
		table = range_0;

	BUG_ON(lvl < 0 || lvl >= IWL_POWER_NUM);

	*cmd = table[lvl].cmd;

	if (period == 0) {
W
Wey-Yi Guy 已提交
138
		skip = 0;
139
		period = 1;
W
Wey-Yi Guy 已提交
140 141 142
		for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
			max_sleep[i] =  1;

143
	} else {
W
Wey-Yi Guy 已提交
144 145 146 147
		skip = table[lvl].no_dtim;
		for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
			max_sleep[i] = le32_to_cpu(cmd->sleep_interval[i]);
		max_sleep[IWL_POWER_VEC_SIZE - 1] = skip + 1;
148 149
	}

W
Wey-Yi Guy 已提交
150 151 152 153 154 155 156 157 158 159 160 161
	slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
	/* figure out the listen interval based on dtim period and skip */
	if (slp_itrvl == 0xFF)
		cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
			cpu_to_le32(period * (skip + 1));

	slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
	if (slp_itrvl > period)
		cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
			cpu_to_le32((slp_itrvl / period) * period);

	if (skip)
162
		cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
W
Wey-Yi Guy 已提交
163
	else
164 165
		cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;

166 167 168 169 170 171 172 173 174
	if (priv->cfg->bt_params &&
	    priv->cfg->bt_params->advanced_bt_coexist) {
		if (!priv->cfg->bt_params->bt_sco_disable)
			cmd->flags |= IWL_POWER_BT_SCO_ENA;
		else
			cmd->flags &= ~IWL_POWER_BT_SCO_ENA;
	}


W
Wey-Yi Guy 已提交
175
	slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
W
Wey-Yi Guy 已提交
176
	if (slp_itrvl > IWL_CONN_MAX_LISTEN_INTERVAL)
W
Wey-Yi Guy 已提交
177
		cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
W
Wey-Yi Guy 已提交
178
			cpu_to_le32(IWL_CONN_MAX_LISTEN_INTERVAL);
W
Wey-Yi Guy 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192

	/* enforce max sleep interval */
	for (i = IWL_POWER_VEC_SIZE - 1; i >= 0 ; i--) {
		if (le32_to_cpu(cmd->sleep_interval[i]) >
		    (max_sleep[i] * period))
			cmd->sleep_interval[i] =
				cpu_to_le32(max_sleep[i] * period);
		if (i != (IWL_POWER_VEC_SIZE - 1)) {
			if (le32_to_cpu(cmd->sleep_interval[i]) >
			    le32_to_cpu(cmd->sleep_interval[i+1]))
				cmd->sleep_interval[i] =
					cmd->sleep_interval[i+1];
		}
	}
193 194 195 196 197 198

	if (priv->power_data.pci_pm)
		cmd->flags |= IWL_POWER_PCI_PM_MSK;
	else
		cmd->flags &= ~IWL_POWER_PCI_PM_MSK;

W
Wey-Yi Guy 已提交
199 200
	IWL_DEBUG_POWER(priv, "numSkipDtim = %u, dtimPeriod = %d\n",
			skip, period);
201 202 203 204 205
	IWL_DEBUG_POWER(priv, "Sleep command for index %d\n", lvl + 1);
}

static void iwl_power_sleep_cam_cmd(struct iwl_priv *priv,
				    struct iwl_powertable_cmd *cmd)
M
Mohamed Abbas 已提交
206
{
207
	memset(cmd, 0, sizeof(*cmd));
M
Mohamed Abbas 已提交
208

209 210
	if (priv->power_data.pci_pm)
		cmd->flags |= IWL_POWER_PCI_PM_MSK;
M
Mohamed Abbas 已提交
211

212
	IWL_DEBUG_POWER(priv, "Sleep command for CAM\n");
M
Mohamed Abbas 已提交
213 214
}

215 216 217
static void iwl_power_fill_sleep_cmd(struct iwl_priv *priv,
				     struct iwl_powertable_cmd *cmd,
				     int dynps_ms, int wakeup_period)
M
Mohamed Abbas 已提交
218
{
219 220 221 222 223 224 225 226 227 228 229 230 231 232
	/*
	 * 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;
233
	int i;
M
Mohamed Abbas 已提交
234

235 236 237 238 239
	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;

240
	memset(cmd, 0, sizeof(*cmd));
241

242 243
	cmd->flags = IWL_POWER_DRIVER_ALLOW_SLEEP_MSK |
		     IWL_POWER_FAST_PD; /* no use seeing frames for others */
M
Mohamed Abbas 已提交
244

245 246
	if (priv->power_data.pci_pm)
		cmd->flags |= IWL_POWER_PCI_PM_MSK;
M
Mohamed Abbas 已提交
247

248 249 250 251 252 253 254 255
	if (priv->cfg->bt_params &&
	    priv->cfg->bt_params->advanced_bt_coexist) {
		if (!priv->cfg->bt_params->bt_sco_disable)
			cmd->flags |= IWL_POWER_BT_SCO_ENA;
		else
			cmd->flags &= ~IWL_POWER_BT_SCO_ENA;
	}

256 257
	cmd->rx_data_timeout = cpu_to_le32(1000 * dynps_ms);
	cmd->tx_data_timeout = cpu_to_le32(1000 * dynps_ms);
M
Mohamed Abbas 已提交
258

259
	for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
260 261
		cmd->sleep_interval[i] =
			cpu_to_le32(min_t(int, slp_succ[i], wakeup_period));
262 263 264

	IWL_DEBUG_POWER(priv, "Automatic sleep command\n");
}
M
Mohamed Abbas 已提交
265

266 267 268
static int iwl_set_power(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd)
{
	IWL_DEBUG_POWER(priv, "Sending power/sleep command\n");
269 270 271 272
	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 已提交
273 274 275 276 277 278
			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]));

279 280
	return iwl_send_cmd_pdu(priv, POWER_TABLE_CMD,
				sizeof(struct iwl_powertable_cmd), cmd);
M
Mohamed Abbas 已提交
281 282
}

283 284
static void iwl_power_build_cmd(struct iwl_priv *priv,
				struct iwl_powertable_cmd *cmd)
M
Mohamed Abbas 已提交
285
{
286
	bool enabled = priv->hw->conf.flags & IEEE80211_CONF_PS;
287
	int dtimper;
M
Mohamed Abbas 已提交
288

289
	dtimper = priv->hw->conf.ps_dtim_period ?: 1;
290

291
	if (priv->cfg->base_params->broken_powersave)
292
		iwl_power_sleep_cam_cmd(priv, cmd);
293
	else if (priv->cfg->base_params->supports_idle &&
294
		 priv->hw->conf.flags & IEEE80211_CONF_IDLE)
295
		iwl_static_sleep_cmd(priv, cmd, IWL_POWER_INDEX_5, 20);
296
	else if (priv->cfg->ops->lib->tt_ops.lower_power_detection &&
297 298 299
		 priv->cfg->ops->lib->tt_ops.tt_power_mode &&
		 priv->cfg->ops->lib->tt_ops.lower_power_detection(priv)) {
		/* in thermal throttling low power state */
300
		iwl_static_sleep_cmd(priv, cmd,
301
		    priv->cfg->ops->lib->tt_ops.tt_power_mode(priv), dtimper);
302
	} else if (!enabled)
303
		iwl_power_sleep_cam_cmd(priv, cmd);
304
	else if (priv->power_data.debug_sleep_level_override >= 0)
305
		iwl_static_sleep_cmd(priv, cmd,
306 307 308
				     priv->power_data.debug_sleep_level_override,
				     dtimper);
	else if (no_sleep_autoadjust)
309
		iwl_static_sleep_cmd(priv, cmd, IWL_POWER_INDEX_1, dtimper);
310
	else
311
		iwl_power_fill_sleep_cmd(priv, cmd,
312 313
					 priv->hw->conf.dynamic_ps_timeout,
					 priv->hw->conf.max_sleep_period);
314 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
}

int iwl_power_set_mode(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd,
		       bool force)
{
	int ret;
	bool update_chains;

	lockdep_assert_held(&priv->mutex);

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

	if (!memcmp(&priv->power_data.sleep_cmd, cmd, sizeof(*cmd)) && !force)
		return 0;

	if (!iwl_is_ready_rf(priv))
		return -EIO;

	/* scan complete use sleep_power_next, need to be updated */
	memcpy(&priv->power_data.sleep_cmd_next, cmd, sizeof(*cmd));
	if (test_bit(STATUS_SCANNING, &priv->status) && !force) {
		IWL_DEBUG_INFO(priv, "Defer power set mode while scanning\n");
		return 0;
	}

	if (cmd->flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK)
		set_bit(STATUS_POWER_PMI, &priv->status);
M
Mohamed Abbas 已提交
343

344 345 346 347 348 349 350 351 352
	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,
353
					"Cannot update the power, chain noise "
354 355
					"calibration running: %d\n",
					priv->chain_noise_data.state);
356 357 358 359

		memcpy(&priv->power_data.sleep_cmd, cmd, sizeof(*cmd));
	} else
		IWL_ERR(priv, "set power fail, ret = %d", ret);
M
Mohamed Abbas 已提交
360 361 362

	return ret;
}
363 364 365 366 367 368 369 370 371
EXPORT_SYMBOL(iwl_power_set_mode);

int iwl_power_update_mode(struct iwl_priv *priv, bool force)
{
	struct iwl_powertable_cmd cmd;

	iwl_power_build_cmd(priv, &cmd);
	return iwl_power_set_mode(priv, &cmd, force);
}
M
Mohamed Abbas 已提交
372 373
EXPORT_SYMBOL(iwl_power_update_mode);

T
Tomas Winkler 已提交
374
/* initialize to default */
M
Mohamed Abbas 已提交
375 376
void iwl_power_initialize(struct iwl_priv *priv)
{
377 378 379 380 381 382 383 384
	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));
M
Mohamed Abbas 已提交
385 386
}
EXPORT_SYMBOL(iwl_power_initialize);