iwl-power.c 8.7 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
#include "iwl-debug.h"
#include "iwl-power.h"

/*
J
Johannes Berg 已提交
44
 * Setting power level allow the card to go to sleep when not busy.
M
Mohamed Abbas 已提交
45
 *
J
Johannes Berg 已提交
46 47 48
 * 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 已提交
49
 *
J
Johannes Berg 已提交
50 51 52
 * 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 已提交
53 54 55 56 57 58
 */

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


J
Johannes Berg 已提交
59 60 61 62 63 64 65 66 67
#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 已提交
68
/* default power management (not Tx power) table values */
J
Johannes Berg 已提交
69 70
/* 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 已提交
71 72 73 74 75 76 77 78 79
	{{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 已提交
80 81
/* 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 已提交
82 83 84 85 86 87 88 89
	{{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 已提交
90 91
/* for DTIM period > IWL_POWER_RANGE_1_MAX */
static const struct iwl_power_vec_entry range_2[IWL_POWER_NUM] = {
M
Mohamed Abbas 已提交
92 93 94 95 96 97 98 99
	{{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}
};

100

101 102 103
/* set card power command */
static int iwl_set_power(struct iwl_priv *priv, void *cmd)
{
104 105
	return iwl_send_cmd_pdu(priv, POWER_TABLE_CMD,
				sizeof(struct iwl_powertable_cmd), cmd);
106
}
M
Mohamed Abbas 已提交
107 108

/* initialize to default */
109
static void iwl_power_init_handle(struct iwl_priv *priv)
M
Mohamed Abbas 已提交
110 111
{
	struct iwl_power_mgr *pow_data;
J
Johannes Berg 已提交
112
	int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_NUM;
113 114
	struct iwl_powertable_cmd *cmd;
	int i;
115
	u16 lctl;
M
Mohamed Abbas 已提交
116

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

119
	pow_data = &priv->power_data;
M
Mohamed Abbas 已提交
120 121 122 123 124 125 126

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

127
	lctl = iwl_pcie_link_ctl(priv);
M
Mohamed Abbas 已提交
128

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

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

134
		if (lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN)
135 136 137
			cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
		else
			cmd->flags |= IWL_POWER_PCI_PM_MSK;
M
Mohamed Abbas 已提交
138 139 140
	}
}

T
Tomas Winkler 已提交
141
/* adjust power command according to DTIM period and power level*/
142 143
static int iwl_update_power_cmd(struct iwl_priv *priv,
				struct iwl_powertable_cmd *cmd, u16 mode)
M
Mohamed Abbas 已提交
144 145 146
{
	struct iwl_power_vec_entry *range;
	struct iwl_power_mgr *pow_data;
147 148 149 150
	int i;
	u32 max_sleep = 0;
	u8 period;
	bool skip;
M
Mohamed Abbas 已提交
151 152

	if (mode > IWL_POWER_INDEX_5) {
153
		IWL_DEBUG_POWER(priv, "Error invalid power mode \n");
154
		return -EINVAL;
M
Mohamed Abbas 已提交
155
	}
156 157

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

	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;
167
	memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
M
Mohamed Abbas 已提交
168 169 170

	if (period == 0) {
		period = 1;
171
		skip = false;
M
Mohamed Abbas 已提交
172
	} else {
173 174 175 176
		skip = !!range[mode].no_dtim;
	}

	if (skip) {
M
Mohamed Abbas 已提交
177 178 179 180 181 182 183
		__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;
184 185 186
	} else {
		max_sleep = period;
		cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
M
Mohamed Abbas 已提交
187 188
	}

189
	for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
M
Mohamed Abbas 已提交
190 191 192
		if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
			cmd->sleep_interval[i] = cpu_to_le32(max_sleep);

193 194 195 196
	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 已提交
197 198 199 200 201 202
			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]));

203
	return 0;
M
Mohamed Abbas 已提交
204 205 206 207
}


/*
208
 * compute the final power mode index
M
Mohamed Abbas 已提交
209
 */
210
int iwl_power_update_mode(struct iwl_priv *priv, bool force)
M
Mohamed Abbas 已提交
211 212 213 214
{
	struct iwl_power_mgr *setting = &(priv->power_data);
	int ret = 0;
	u16 uninitialized_var(final_mode);
215
	bool update_chains;
M
Mohamed Abbas 已提交
216

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

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

J
Johannes Berg 已提交
223
	if (setting->power_disabled)
M
Mohamed Abbas 已提交
224 225
		final_mode = IWL_POWER_MODE_CAM;

J
Johannes Berg 已提交
226
	if (iwl_is_ready_rf(priv) &&
227
	    ((setting->power_mode != final_mode) || force)) {
228
		struct iwl_powertable_cmd cmd;
M
Mohamed Abbas 已提交
229 230 231 232

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

233
		iwl_update_power_cmd(priv, &cmd, final_mode);
M
Mohamed Abbas 已提交
234 235 236 237 238
		cmd.keep_alive_beacons = 0;

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

239
		ret = iwl_set_power(priv, &cmd);
M
Mohamed Abbas 已提交
240 241 242 243

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

244
		if (priv->cfg->ops->lib->update_chain_flags && update_chains)
M
Mohamed Abbas 已提交
245
			priv->cfg->ops->lib->update_chain_flags(priv);
246
		else
247
			IWL_DEBUG_POWER(priv, "Cannot update the power, chain noise "
248 249
					"calibration running: %d\n",
					priv->chain_noise_data.state);
M
Mohamed Abbas 已提交
250 251 252 253 254 255 256 257 258 259 260
		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 已提交
261
	if (mode >= IWL_POWER_NUM)
M
Mohamed Abbas 已提交
262 263 264 265
		return -EINVAL;

	priv->power_data.user_power_setting = mode;

266
	return iwl_power_update_mode(priv, 0);
M
Mohamed Abbas 已提交
267 268 269
}
EXPORT_SYMBOL(iwl_power_set_user_mode);

T
Tomas Winkler 已提交
270
/* initialize to default */
M
Mohamed Abbas 已提交
271 272 273
void iwl_power_initialize(struct iwl_priv *priv)
{
	iwl_power_init_handle(priv);
J
Johannes Berg 已提交
274 275 276
	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 已提交
277 278
}
EXPORT_SYMBOL(iwl_power_initialize);