iwl-power.c 13.1 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-commands.h"
M
Mohamed Abbas 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#include "iwl-debug.h"
#include "iwl-power.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.
 *
 */

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



#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


#define IWL_CT_KILL_TEMPERATURE		110
#define IWL_MIN_POWER_TEMPERATURE	100
#define IWL_REDUCED_POWER_TEMPERATURE	95

/* default power management (not Tx power) table values */
T
Tomas Winkler 已提交
74
/* for TIM  0-10 */
75
static struct iwl_power_vec_entry range_0[IWL_POWER_MAX] = {
M
Mohamed Abbas 已提交
76 77 78 79 80 81 82 83 84
	{{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}
};


T
Tomas Winkler 已提交
85
/* for TIM = 3-10 */
86
static struct iwl_power_vec_entry range_1[IWL_POWER_MAX] = {
M
Mohamed Abbas 已提交
87 88 89 90 91 92 93 94
	{{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}
};

T
Tomas Winkler 已提交
95
/* for TIM > 11 */
96
static struct iwl_power_vec_entry range_2[IWL_POWER_MAX] = {
M
Mohamed Abbas 已提交
97 98 99 100 101 102 103 104
	{{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}
};

105

106 107 108 109 110 111 112
/* 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);
}
M
Mohamed Abbas 已提交
113 114 115 116 117
/* decide the right power level according to association status
 * and battery status
 */
static u16 iwl_get_auto_power_mode(struct iwl_priv *priv)
{
118
	u16 mode;
M
Mohamed Abbas 已提交
119 120 121 122 123 124 125 126 127 128 129

	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;
130 131 132
	default:
		mode = priv->power_data.user_power_setting;
		break;
M
Mohamed Abbas 已提交
133 134 135 136 137
	}
	return mode;
}

/* initialize to default */
138
static void iwl_power_init_handle(struct iwl_priv *priv)
M
Mohamed Abbas 已提交
139 140
{
	struct iwl_power_mgr *pow_data;
141
	int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_MAX;
142 143
	struct iwl_powertable_cmd *cmd;
	int i;
144
	u16 lctl;
M
Mohamed Abbas 已提交
145

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

148
	pow_data = &priv->power_data;
M
Mohamed Abbas 已提交
149 150 151 152 153 154 155

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

156
	lctl = iwl_pcie_link_ctl(priv);
M
Mohamed Abbas 已提交
157

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

160 161
	for (i = 0; i < IWL_POWER_MAX; i++) {
		cmd = &pow_data->pwr_range_0[i].cmd;
M
Mohamed Abbas 已提交
162

163
		if (lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN)
164 165 166
			cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
		else
			cmd->flags |= IWL_POWER_PCI_PM_MSK;
M
Mohamed Abbas 已提交
167 168 169
	}
}

T
Tomas Winkler 已提交
170
/* adjust power command according to DTIM period and power level*/
171 172
static int iwl_update_power_cmd(struct iwl_priv *priv,
				struct iwl_powertable_cmd *cmd, u16 mode)
M
Mohamed Abbas 已提交
173 174 175
{
	struct iwl_power_vec_entry *range;
	struct iwl_power_mgr *pow_data;
176 177 178 179
	int i;
	u32 max_sleep = 0;
	u8 period;
	bool skip;
M
Mohamed Abbas 已提交
180 181

	if (mode > IWL_POWER_INDEX_5) {
182
		IWL_DEBUG_POWER(priv, "Error invalid power mode \n");
183
		return -EINVAL;
M
Mohamed Abbas 已提交
184
	}
185 186

	pow_data = &priv->power_data;
M
Mohamed Abbas 已提交
187 188 189 190 191 192 193 194 195

	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;
196
	memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
M
Mohamed Abbas 已提交
197 198 199

	if (period == 0) {
		period = 1;
200
		skip = false;
M
Mohamed Abbas 已提交
201
	} else {
202 203 204 205
		skip = !!range[mode].no_dtim;
	}

	if (skip) {
M
Mohamed Abbas 已提交
206 207 208 209 210 211 212
		__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;
213 214 215
	} else {
		max_sleep = period;
		cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
M
Mohamed Abbas 已提交
216 217
	}

218
	for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
M
Mohamed Abbas 已提交
219 220 221
		if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
			cmd->sleep_interval[i] = cpu_to_le32(max_sleep);

222 223 224 225
	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 已提交
226 227 228 229 230 231
			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]));

232
	return 0;
M
Mohamed Abbas 已提交
233 234 235 236
}


/*
237
 * compute the final power mode index
M
Mohamed Abbas 已提交
238
 */
239
int iwl_power_update_mode(struct iwl_priv *priv, bool force)
M
Mohamed Abbas 已提交
240 241 242 243
{
	struct iwl_power_mgr *setting = &(priv->power_data);
	int ret = 0;
	u16 uninitialized_var(final_mode);
244
	bool update_chains;
M
Mohamed Abbas 已提交
245

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

250 251 252
	/* If on battery, set to 3,
	 * if plugged into AC power, set to CAM ("continuously aware mode"),
	 * else user level */
M
Mohamed Abbas 已提交
253 254

	switch (setting->system_power_setting) {
255
	case IWL_POWER_SYS_AUTO:
M
Mohamed Abbas 已提交
256 257
		final_mode = iwl_get_auto_power_mode(priv);
		break;
258
	case IWL_POWER_SYS_BATTERY:
M
Mohamed Abbas 已提交
259 260
		final_mode = IWL_POWER_INDEX_3;
		break;
261
	case IWL_POWER_SYS_AC:
M
Mohamed Abbas 已提交
262 263 264
		final_mode = IWL_POWER_MODE_CAM;
		break;
	default:
265 266
		final_mode = IWL_POWER_INDEX_3;
		WARN_ON(1);
M
Mohamed Abbas 已提交
267 268 269 270 271 272
	}

	if (setting->critical_power_setting > final_mode)
		final_mode = setting->critical_power_setting;

	/* driver only support CAM for non STA network */
273
	if (priv->iw_mode != NL80211_IFTYPE_STATION)
M
Mohamed Abbas 已提交
274 275
		final_mode = IWL_POWER_MODE_CAM;

276
	if (iwl_is_ready_rf(priv) && !setting->power_disabled &&
277
	    ((setting->power_mode != final_mode) || force)) {
278
		struct iwl_powertable_cmd cmd;
M
Mohamed Abbas 已提交
279 280 281 282

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

283
		iwl_update_power_cmd(priv, &cmd, final_mode);
M
Mohamed Abbas 已提交
284 285 286 287 288
		cmd.keep_alive_beacons = 0;

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

289
		ret = iwl_set_power(priv, &cmd);
M
Mohamed Abbas 已提交
290 291 292 293 294 295

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

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

	return ret;
}
EXPORT_SYMBOL(iwl_power_update_mode);

/* Allow other iwl code to disable/enable power management active
T
Tomas Winkler 已提交
311
 * this will be useful for rate scale to disable PM during heavy
M
Mohamed Abbas 已提交
312 313
 * Tx/Rx activities
 */
314
int iwl_power_disable_management(struct iwl_priv *priv, u32 ms)
M
Mohamed Abbas 已提交
315 316 317 318 319 320 321 322 323 324 325 326
{
	u16 prev_mode;
	int ret = 0;

	if (priv->power_data.power_disabled)
		return -EBUSY;

	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;
327 328 329 330 331
	cancel_delayed_work(&priv->set_power_save);
	if (ms)
		queue_delayed_work(priv->workqueue, &priv->set_power_save,
				   msecs_to_jiffies(ms));

M
Mohamed Abbas 已提交
332 333 334 335 336 337

	return ret;
}
EXPORT_SYMBOL(iwl_power_disable_management);

/* Allow other iwl code to disable/enable power management active
T
Tomas Winkler 已提交
338 339
 * this will be useful for rate scale to disable PM during high
 * volume activities
M
Mohamed Abbas 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353
 */
int iwl_power_enable_management(struct iwl_priv *priv)
{
	int ret = 0;

	priv->power_data.power_disabled = 0;
	ret = iwl_power_update_mode(priv, 0);
	return ret;
}
EXPORT_SYMBOL(iwl_power_enable_management);

/* set user_power_setting */
int iwl_power_set_user_mode(struct iwl_priv *priv, u16 mode)
{
354
	if (mode > IWL_POWER_MAX)
M
Mohamed Abbas 已提交
355 356 357 358
		return -EINVAL;

	priv->power_data.user_power_setting = mode;

359
	return iwl_power_update_mode(priv, 0);
M
Mohamed Abbas 已提交
360 361 362 363 364 365 366 367
}
EXPORT_SYMBOL(iwl_power_set_user_mode);

/* set system_power_setting. This should be set by over all
 * PM application.
 */
int iwl_power_set_system_mode(struct iwl_priv *priv, u16 mode)
{
368 369 370
	if (mode < IWL_POWER_SYS_MAX)
		priv->power_data.system_power_setting = mode;
	else
M
Mohamed Abbas 已提交
371
		return -EINVAL;
372
	return iwl_power_update_mode(priv, 0);
M
Mohamed Abbas 已提交
373 374 375
}
EXPORT_SYMBOL(iwl_power_set_system_mode);

T
Tomas Winkler 已提交
376
/* initialize to default */
M
Mohamed Abbas 已提交
377 378 379 380
void iwl_power_initialize(struct iwl_priv *priv)
{
	iwl_power_init_handle(priv);
	priv->power_data.user_power_setting = IWL_POWER_AUTO;
381
	priv->power_data.system_power_setting = IWL_POWER_SYS_AUTO;
M
Mohamed Abbas 已提交
382
	priv->power_data.power_disabled = 0;
383
	priv->power_data.is_battery_active = 0;
M
Mohamed Abbas 已提交
384 385 386 387 388 389 390 391 392
	priv->power_data.critical_power_setting = 0;
}
EXPORT_SYMBOL(iwl_power_initialize);

/* set critical_power_setting according to temperature value */
int iwl_power_temperature_change(struct iwl_priv *priv)
{
	int ret = 0;
	s32 temperature = KELVIN_TO_CELSIUS(priv->last_temperature);
393
	u16 new_critical = priv->power_data.critical_power_setting;
M
Mohamed Abbas 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413

	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 (new_critical != priv->power_data.critical_power_setting)
		priv->power_data.critical_power_setting = new_critical;

	if (priv->power_data.critical_power_setting >
				priv->power_data.power_mode)
		ret = iwl_power_update_mode(priv, 0);

	return ret;
}
EXPORT_SYMBOL(iwl_power_temperature_change);
414 415 416 417 418

static void iwl_bg_set_power_save(struct work_struct *work)
{
	struct iwl_priv *priv = container_of(work,
				struct iwl_priv, set_power_save.work);
419
	IWL_DEBUG_POWER(priv, "update power\n");
420 421 422 423 424 425

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

	mutex_lock(&priv->mutex);

T
Tomas Winkler 已提交
426
	/* on starting association we disable power management
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
	 * until association, if association failed then this
	 * timer will expire and enable PM again.
	 */
	if (!iwl_is_associated(priv))
		iwl_power_enable_management(priv);

	mutex_unlock(&priv->mutex);
}
void iwl_setup_power_deferred_work(struct iwl_priv *priv)
{
	INIT_DELAYED_WORK(&priv->set_power_save, iwl_bg_set_power_save);
}
EXPORT_SYMBOL(iwl_setup_power_deferred_work);

void iwl_power_cancel_timeout(struct iwl_priv *priv)
{
	cancel_delayed_work(&priv->set_power_save);
}
EXPORT_SYMBOL(iwl_power_cancel_timeout);