iwlwifi: change email contact information
[deliverable/linux.git] / drivers / net / wireless / iwlwifi / iwl-power.c
CommitLineData
5da4b55f
MA
1/******************************************************************************
2 *
3 * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
4 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
759ef89f 25 * Intel Linux Wireless <ilw@linux.intel.com>
5da4b55f
MA
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *****************************************************************************/
28
29
30#include <linux/kernel.h>
31#include <linux/module.h>
5da4b55f
MA
32#include <linux/init.h>
33
34#include <net/mac80211.h>
35
36#include "iwl-eeprom.h"
3e0d4cb1 37#include "iwl-dev.h"
5da4b55f 38#include "iwl-core.h"
5a36ba0e 39#include "iwl-commands.h"
5da4b55f
MA
40#include "iwl-debug.h"
41#include "iwl-power.h"
42#include "iwl-helpers.h"
43
44/*
45 * Setting power level allow the card to go to sleep when not busy
46 * there are three factor that decide the power level to go to, they
47 * are list here with its priority
48 * 1- critical_power_setting this will be set according to card temperature.
49 * 2- system_power_setting this will be set by system PM manager.
50 * 3- user_power_setting this will be set by user either by writing to sys or
51 * mac80211
52 *
53 * if system_power_setting and user_power_setting is set to auto
54 * the power level will be decided according to association status and battery
55 * status.
56 *
57 */
58
59#define MSEC_TO_USEC 1024
60#define IWL_POWER_RANGE_0_MAX (2)
61#define IWL_POWER_RANGE_1_MAX (10)
62
63
64#define NOSLP __constant_cpu_to_le16(0), 0, 0
65#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
66#define SLP_TOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
67#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
68 __constant_cpu_to_le32(X1), \
69 __constant_cpu_to_le32(X2), \
70 __constant_cpu_to_le32(X3), \
71 __constant_cpu_to_le32(X4)}
72
73#define IWL_POWER_ON_BATTERY IWL_POWER_INDEX_5
74#define IWL_POWER_ON_AC_DISASSOC IWL_POWER_MODE_CAM
75#define IWL_POWER_ON_AC_ASSOC IWL_POWER_MODE_CAM
76
77
78#define IWL_CT_KILL_TEMPERATURE 110
79#define IWL_MIN_POWER_TEMPERATURE 100
80#define IWL_REDUCED_POWER_TEMPERATURE 95
81
82/* default power management (not Tx power) table values */
a96a27f9 83/* for TIM 0-10 */
298df1f6 84static struct iwl_power_vec_entry range_0[IWL_POWER_MAX] = {
5da4b55f
MA
85 {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
86 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
87 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
88 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 2, 2, 2, 0xFF)}, 0},
89 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 2, 4, 4, 0xFF)}, 1},
90 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 2, 4, 6, 0xFF)}, 2}
91};
92
93
a96a27f9 94/* for TIM = 3-10 */
298df1f6 95static struct iwl_power_vec_entry range_1[IWL_POWER_MAX] = {
5da4b55f
MA
96 {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
97 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
98 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 3, 4, 7)}, 0},
99 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 4, 6, 7, 9)}, 0},
100 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 4, 6, 9, 10)}, 1},
101 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 4, 7, 10, 10)}, 2}
102};
103
a96a27f9 104/* for TIM > 11 */
298df1f6 105static struct iwl_power_vec_entry range_2[IWL_POWER_MAX] = {
5da4b55f
MA
106 {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
107 {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
108 {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
109 {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
110 {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
111 {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
112};
113
ca579617
MA
114/* set card power command */
115static int iwl_set_power(struct iwl_priv *priv, void *cmd)
116{
117 return iwl_send_cmd_pdu_async(priv, POWER_TABLE_CMD,
118 sizeof(struct iwl_powertable_cmd),
119 cmd, NULL);
120}
5da4b55f
MA
121/* decide the right power level according to association status
122 * and battery status
123 */
124static u16 iwl_get_auto_power_mode(struct iwl_priv *priv)
125{
298df1f6 126 u16 mode;
5da4b55f
MA
127
128 switch (priv->power_data.user_power_setting) {
129 case IWL_POWER_AUTO:
130 /* if running on battery */
131 if (priv->power_data.is_battery_active)
132 mode = IWL_POWER_ON_BATTERY;
133 else if (iwl_is_associated(priv))
134 mode = IWL_POWER_ON_AC_ASSOC;
135 else
136 mode = IWL_POWER_ON_AC_DISASSOC;
137 break;
298df1f6 138 /* FIXME: remove battery and ac from here */
5da4b55f
MA
139 case IWL_POWER_BATTERY:
140 mode = IWL_POWER_INDEX_3;
141 break;
142 case IWL_POWER_AC:
143 mode = IWL_POWER_MODE_CAM;
144 break;
298df1f6
EK
145 default:
146 mode = priv->power_data.user_power_setting;
147 break;
5da4b55f
MA
148 }
149 return mode;
150}
151
152/* initialize to default */
153static int iwl_power_init_handle(struct iwl_priv *priv)
154{
5da4b55f 155 struct iwl_power_mgr *pow_data;
298df1f6 156 int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_MAX;
e7b63581
TW
157 struct iwl_powertable_cmd *cmd;
158 int i;
5da4b55f
MA
159 u16 pci_pm;
160
161 IWL_DEBUG_POWER("Initialize power \n");
162
163 pow_data = &(priv->power_data);
164
165 memset(pow_data, 0, sizeof(*pow_data));
166
167 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
168 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
169 memcpy(&pow_data->pwr_range_2[0], &range_2[0], size);
170
e7b63581 171 pci_read_config_word(priv->pci_dev, PCI_CFG_LINK_CTRL, &pci_pm);
5da4b55f 172
e7b63581 173 IWL_DEBUG_POWER("adjust power command flags\n");
5da4b55f 174
e7b63581
TW
175 for (i = 0; i < IWL_POWER_MAX; i++) {
176 cmd = &pow_data->pwr_range_0[i].cmd;
5da4b55f 177
e7b63581
TW
178 if (pci_pm & PCI_CFG_LINK_CTRL_VAL_L0S_EN)
179 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
180 else
181 cmd->flags |= IWL_POWER_PCI_PM_MSK;
5da4b55f 182 }
e7b63581 183 return 0;
5da4b55f
MA
184}
185
a96a27f9 186/* adjust power command according to DTIM period and power level*/
5da4b55f 187static int iwl_update_power_command(struct iwl_priv *priv,
ca579617 188 struct iwl_powertable_cmd *cmd,
5da4b55f
MA
189 u16 mode)
190{
191 int ret = 0, i;
192 u8 skip;
193 u32 max_sleep = 0;
194 struct iwl_power_vec_entry *range;
195 u8 period = 0;
196 struct iwl_power_mgr *pow_data;
197
198 if (mode > IWL_POWER_INDEX_5) {
199 IWL_DEBUG_POWER("Error invalid power mode \n");
200 return -1;
201 }
202 pow_data = &(priv->power_data);
203
204 if (pow_data->dtim_period <= IWL_POWER_RANGE_0_MAX)
205 range = &pow_data->pwr_range_0[0];
206 else if (pow_data->dtim_period <= IWL_POWER_RANGE_1_MAX)
207 range = &pow_data->pwr_range_1[0];
208 else
209 range = &pow_data->pwr_range_2[0];
210
211 period = pow_data->dtim_period;
ca579617 212 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
5da4b55f
MA
213
214 if (period == 0) {
215 period = 1;
216 skip = 0;
217 } else
218 skip = range[mode].no_dtim;
219
220 if (skip == 0) {
221 max_sleep = period;
222 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
223 } else {
224 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
225 max_sleep = le32_to_cpu(slp_itrvl);
226 if (max_sleep == 0xFF)
227 max_sleep = period * (skip + 1);
228 else if (max_sleep > period)
229 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
230 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
231 }
232
233 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
234 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
235 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
236 }
237
238 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
239 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
240 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
241 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
242 le32_to_cpu(cmd->sleep_interval[0]),
243 le32_to_cpu(cmd->sleep_interval[1]),
244 le32_to_cpu(cmd->sleep_interval[2]),
245 le32_to_cpu(cmd->sleep_interval[3]),
246 le32_to_cpu(cmd->sleep_interval[4]));
247
248 return ret;
249}
250
251
252/*
a33c2f47 253 * compute the final power mode index
5da4b55f 254 */
04816448 255int iwl_power_update_mode(struct iwl_priv *priv, bool force)
5da4b55f
MA
256{
257 struct iwl_power_mgr *setting = &(priv->power_data);
258 int ret = 0;
259 u16 uninitialized_var(final_mode);
a71c8f62 260 bool update_chains;
5da4b55f 261
04816448 262 /* Don't update the RX chain when chain noise calibration is running */
a71c8f62
WT
263 update_chains = priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE ||
264 priv->chain_noise_data.state == IWL_CHAIN_NOISE_ALIVE;
04816448 265
a33c2f47
EG
266 /* If on battery, set to 3,
267 * if plugged into AC power, set to CAM ("continuously aware mode"),
268 * else user level */
5da4b55f
MA
269
270 switch (setting->system_power_setting) {
298df1f6 271 case IWL_POWER_SYS_AUTO:
5da4b55f
MA
272 final_mode = iwl_get_auto_power_mode(priv);
273 break;
298df1f6 274 case IWL_POWER_SYS_BATTERY:
5da4b55f
MA
275 final_mode = IWL_POWER_INDEX_3;
276 break;
298df1f6 277 case IWL_POWER_SYS_AC:
5da4b55f
MA
278 final_mode = IWL_POWER_MODE_CAM;
279 break;
280 default:
298df1f6
EK
281 final_mode = IWL_POWER_INDEX_3;
282 WARN_ON(1);
5da4b55f
MA
283 }
284
285 if (setting->critical_power_setting > final_mode)
286 final_mode = setting->critical_power_setting;
287
288 /* driver only support CAM for non STA network */
05c914fe 289 if (priv->iw_mode != NL80211_IFTYPE_STATION)
5da4b55f
MA
290 final_mode = IWL_POWER_MODE_CAM;
291
292 if (!iwl_is_rfkill(priv) && !setting->power_disabled &&
04816448 293 ((setting->power_mode != final_mode) || force)) {
ca579617 294 struct iwl_powertable_cmd cmd;
5da4b55f
MA
295
296 if (final_mode != IWL_POWER_MODE_CAM)
297 set_bit(STATUS_POWER_PMI, &priv->status);
298
299 iwl_update_power_command(priv, &cmd, final_mode);
300 cmd.keep_alive_beacons = 0;
301
302 if (final_mode == IWL_POWER_INDEX_5)
303 cmd.flags |= IWL_POWER_FAST_PD;
304
ca579617 305 ret = iwl_set_power(priv, &cmd);
5da4b55f
MA
306
307 if (final_mode == IWL_POWER_MODE_CAM)
308 clear_bit(STATUS_POWER_PMI, &priv->status);
309 else
310 set_bit(STATUS_POWER_PMI, &priv->status);
311
a71c8f62 312 if (priv->cfg->ops->lib->update_chain_flags && update_chains)
5da4b55f 313 priv->cfg->ops->lib->update_chain_flags(priv);
a71c8f62
WT
314 else
315 IWL_DEBUG_POWER("Cannot update the power, chain noise "
316 "calibration running: %d\n",
317 priv->chain_noise_data.state);
5da4b55f
MA
318 if (!ret)
319 setting->power_mode = final_mode;
320 }
321
322 return ret;
323}
324EXPORT_SYMBOL(iwl_power_update_mode);
325
326/* Allow other iwl code to disable/enable power management active
a96a27f9 327 * this will be useful for rate scale to disable PM during heavy
5da4b55f
MA
328 * Tx/Rx activities
329 */
c90a74ba 330int iwl_power_disable_management(struct iwl_priv *priv, u32 ms)
5da4b55f
MA
331{
332 u16 prev_mode;
333 int ret = 0;
334
335 if (priv->power_data.power_disabled)
336 return -EBUSY;
337
338 prev_mode = priv->power_data.user_power_setting;
339 priv->power_data.user_power_setting = IWL_POWER_MODE_CAM;
340 ret = iwl_power_update_mode(priv, 0);
341 priv->power_data.power_disabled = 1;
342 priv->power_data.user_power_setting = prev_mode;
c90a74ba
EG
343 cancel_delayed_work(&priv->set_power_save);
344 if (ms)
345 queue_delayed_work(priv->workqueue, &priv->set_power_save,
346 msecs_to_jiffies(ms));
347
5da4b55f
MA
348
349 return ret;
350}
351EXPORT_SYMBOL(iwl_power_disable_management);
352
353/* Allow other iwl code to disable/enable power management active
a96a27f9
TW
354 * this will be useful for rate scale to disable PM during high
355 * volume activities
5da4b55f
MA
356 */
357int iwl_power_enable_management(struct iwl_priv *priv)
358{
359 int ret = 0;
360
361 priv->power_data.power_disabled = 0;
362 ret = iwl_power_update_mode(priv, 0);
363 return ret;
364}
365EXPORT_SYMBOL(iwl_power_enable_management);
366
367/* set user_power_setting */
368int iwl_power_set_user_mode(struct iwl_priv *priv, u16 mode)
369{
5da4b55f
MA
370 if (mode > IWL_POWER_LIMIT)
371 return -EINVAL;
372
373 priv->power_data.user_power_setting = mode;
374
04816448 375 return iwl_power_update_mode(priv, 0);
5da4b55f
MA
376}
377EXPORT_SYMBOL(iwl_power_set_user_mode);
378
5da4b55f
MA
379/* set system_power_setting. This should be set by over all
380 * PM application.
381 */
382int iwl_power_set_system_mode(struct iwl_priv *priv, u16 mode)
383{
5da4b55f
MA
384 if (mode > IWL_POWER_LIMIT)
385 return -EINVAL;
386
387 priv->power_data.system_power_setting = mode;
388
04816448 389 return iwl_power_update_mode(priv, 0);
5da4b55f
MA
390}
391EXPORT_SYMBOL(iwl_power_set_system_mode);
392
a96a27f9 393/* initialize to default */
5da4b55f
MA
394void iwl_power_initialize(struct iwl_priv *priv)
395{
396
397 iwl_power_init_handle(priv);
398 priv->power_data.user_power_setting = IWL_POWER_AUTO;
399 priv->power_data.power_disabled = 0;
298df1f6 400 priv->power_data.system_power_setting = IWL_POWER_SYS_AUTO;
5da4b55f
MA
401 priv->power_data.is_battery_active = 0;
402 priv->power_data.power_disabled = 0;
403 priv->power_data.critical_power_setting = 0;
404}
405EXPORT_SYMBOL(iwl_power_initialize);
406
407/* set critical_power_setting according to temperature value */
408int iwl_power_temperature_change(struct iwl_priv *priv)
409{
410 int ret = 0;
411 u16 new_critical = priv->power_data.critical_power_setting;
412 s32 temperature = KELVIN_TO_CELSIUS(priv->last_temperature);
413
414 if (temperature > IWL_CT_KILL_TEMPERATURE)
415 return 0;
416 else if (temperature > IWL_MIN_POWER_TEMPERATURE)
417 new_critical = IWL_POWER_INDEX_5;
418 else if (temperature > IWL_REDUCED_POWER_TEMPERATURE)
419 new_critical = IWL_POWER_INDEX_3;
420 else
421 new_critical = IWL_POWER_MODE_CAM;
422
423 if (new_critical != priv->power_data.critical_power_setting)
424 priv->power_data.critical_power_setting = new_critical;
425
426 if (priv->power_data.critical_power_setting >
427 priv->power_data.power_mode)
428 ret = iwl_power_update_mode(priv, 0);
429
430 return ret;
431}
432EXPORT_SYMBOL(iwl_power_temperature_change);
c90a74ba
EG
433
434static void iwl_bg_set_power_save(struct work_struct *work)
435{
436 struct iwl_priv *priv = container_of(work,
437 struct iwl_priv, set_power_save.work);
438 IWL_DEBUG(IWL_DL_STATE, "update power\n");
439
440 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
441 return;
442
443 mutex_lock(&priv->mutex);
444
a96a27f9 445 /* on starting association we disable power management
c90a74ba
EG
446 * until association, if association failed then this
447 * timer will expire and enable PM again.
448 */
449 if (!iwl_is_associated(priv))
450 iwl_power_enable_management(priv);
451
452 mutex_unlock(&priv->mutex);
453}
454void iwl_setup_power_deferred_work(struct iwl_priv *priv)
455{
456 INIT_DELAYED_WORK(&priv->set_power_save, iwl_bg_set_power_save);
457}
458EXPORT_SYMBOL(iwl_setup_power_deferred_work);
459
460void iwl_power_cancel_timeout(struct iwl_priv *priv)
461{
462 cancel_delayed_work(&priv->set_power_save);
463}
464EXPORT_SYMBOL(iwl_power_cancel_timeout);
This page took 0.137776 seconds and 5 git commands to generate.