1 /******************************************************************************
3 * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
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.
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
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
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *****************************************************************************/
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
34 #include <net/mac80211.h>
36 #include "iwl-eeprom.h"
39 #include "iwl-commands.h"
40 #include "iwl-debug.h"
41 #include "iwl-power.h"
44 * Setting power level allow the card to go to sleep when not busy
45 * there are three factor that decide the power level to go to, they
46 * are list here with its priority
47 * 1- critical_power_setting this will be set according to card temperature.
48 * 2- system_power_setting this will be set by system PM manager.
49 * 3- user_power_setting this will be set by user either by writing to sys or
52 * if system_power_setting and user_power_setting is set to auto
53 * the power level will be decided according to association status and battery
58 #define MSEC_TO_USEC 1024
59 #define IWL_POWER_RANGE_0_MAX (2)
60 #define IWL_POWER_RANGE_1_MAX (10)
64 #define IWL_POWER_ON_BATTERY IWL_POWER_INDEX_5
65 #define IWL_POWER_ON_AC_DISASSOC IWL_POWER_MODE_CAM
66 #define IWL_POWER_ON_AC_ASSOC IWL_POWER_MODE_CAM
69 #define IWL_CT_KILL_TEMPERATURE 110
70 #define IWL_MIN_POWER_TEMPERATURE 100
71 #define IWL_REDUCED_POWER_TEMPERATURE 95
73 /* default power management (not Tx power) table values */
75 static struct iwl_power_vec_entry range_0
[IWL_POWER_MAX
] = {
76 {{NOSLP
, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
77 {{SLP
, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
78 {{SLP
, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
79 {{SLP
, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 2, 2, 2, 0xFF)}, 0},
80 {{SLP
, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 2, 4, 4, 0xFF)}, 1},
81 {{SLP
, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 2, 4, 6, 0xFF)}, 2}
86 static struct iwl_power_vec_entry range_1
[IWL_POWER_MAX
] = {
87 {{NOSLP
, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
88 {{SLP
, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
89 {{SLP
, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 3, 4, 7)}, 0},
90 {{SLP
, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 4, 6, 7, 9)}, 0},
91 {{SLP
, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 4, 6, 9, 10)}, 1},
92 {{SLP
, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 4, 7, 10, 10)}, 2}
96 static struct iwl_power_vec_entry range_2
[IWL_POWER_MAX
] = {
97 {{NOSLP
, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
98 {{SLP
, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
99 {{SLP
, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
100 {{SLP
, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
101 {{SLP
, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
102 {{SLP
, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
105 /* set card power command */
106 static int iwl_set_power(struct iwl_priv
*priv
, void *cmd
)
108 return iwl_send_cmd_pdu_async(priv
, POWER_TABLE_CMD
,
109 sizeof(struct iwl_powertable_cmd
),
112 /* decide the right power level according to association status
115 static u16
iwl_get_auto_power_mode(struct iwl_priv
*priv
)
119 switch (priv
->power_data
.user_power_setting
) {
121 /* if running on battery */
122 if (priv
->power_data
.is_battery_active
)
123 mode
= IWL_POWER_ON_BATTERY
;
124 else if (iwl_is_associated(priv
))
125 mode
= IWL_POWER_ON_AC_ASSOC
;
127 mode
= IWL_POWER_ON_AC_DISASSOC
;
129 /* FIXME: remove battery and ac from here */
130 case IWL_POWER_BATTERY
:
131 mode
= IWL_POWER_INDEX_3
;
134 mode
= IWL_POWER_MODE_CAM
;
137 mode
= priv
->power_data
.user_power_setting
;
143 /* initialize to default */
144 static void iwl_power_init_handle(struct iwl_priv
*priv
)
146 struct iwl_power_mgr
*pow_data
;
147 int size
= sizeof(struct iwl_power_vec_entry
) * IWL_POWER_MAX
;
148 struct iwl_powertable_cmd
*cmd
;
152 IWL_DEBUG_POWER("Initialize power \n");
154 pow_data
= &priv
->power_data
;
156 memset(pow_data
, 0, sizeof(*pow_data
));
158 memcpy(&pow_data
->pwr_range_0
[0], &range_0
[0], size
);
159 memcpy(&pow_data
->pwr_range_1
[0], &range_1
[0], size
);
160 memcpy(&pow_data
->pwr_range_2
[0], &range_2
[0], size
);
162 pci_read_config_word(priv
->pci_dev
, PCI_CFG_LINK_CTRL
, &pci_pm
);
164 IWL_DEBUG_POWER("adjust power command flags\n");
166 for (i
= 0; i
< IWL_POWER_MAX
; i
++) {
167 cmd
= &pow_data
->pwr_range_0
[i
].cmd
;
169 if (pci_pm
& PCI_CFG_LINK_CTRL_VAL_L0S_EN
)
170 cmd
->flags
&= ~IWL_POWER_PCI_PM_MSK
;
172 cmd
->flags
|= IWL_POWER_PCI_PM_MSK
;
176 /* adjust power command according to DTIM period and power level*/
177 static int iwl_update_power_cmd(struct iwl_priv
*priv
,
178 struct iwl_powertable_cmd
*cmd
, u16 mode
)
180 struct iwl_power_vec_entry
*range
;
181 struct iwl_power_mgr
*pow_data
;
187 if (mode
> IWL_POWER_INDEX_5
) {
188 IWL_DEBUG_POWER("Error invalid power mode \n");
192 pow_data
= &priv
->power_data
;
194 if (pow_data
->dtim_period
<= IWL_POWER_RANGE_0_MAX
)
195 range
= &pow_data
->pwr_range_0
[0];
196 else if (pow_data
->dtim_period
<= IWL_POWER_RANGE_1_MAX
)
197 range
= &pow_data
->pwr_range_1
[0];
199 range
= &pow_data
->pwr_range_2
[0];
201 period
= pow_data
->dtim_period
;
202 memcpy(cmd
, &range
[mode
].cmd
, sizeof(struct iwl_powertable_cmd
));
208 skip
= !!range
[mode
].no_dtim
;
212 __le32 slp_itrvl
= cmd
->sleep_interval
[IWL_POWER_VEC_SIZE
- 1];
213 max_sleep
= le32_to_cpu(slp_itrvl
);
214 if (max_sleep
== 0xFF)
215 max_sleep
= period
* (skip
+ 1);
216 else if (max_sleep
> period
)
217 max_sleep
= (le32_to_cpu(slp_itrvl
) / period
) * period
;
218 cmd
->flags
|= IWL_POWER_SLEEP_OVER_DTIM_MSK
;
221 cmd
->flags
&= ~IWL_POWER_SLEEP_OVER_DTIM_MSK
;
224 for (i
= 0; i
< IWL_POWER_VEC_SIZE
; i
++)
225 if (le32_to_cpu(cmd
->sleep_interval
[i
]) > max_sleep
)
226 cmd
->sleep_interval
[i
] = cpu_to_le32(max_sleep
);
228 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd
->flags
);
229 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd
->tx_data_timeout
));
230 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd
->rx_data_timeout
));
231 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
232 le32_to_cpu(cmd
->sleep_interval
[0]),
233 le32_to_cpu(cmd
->sleep_interval
[1]),
234 le32_to_cpu(cmd
->sleep_interval
[2]),
235 le32_to_cpu(cmd
->sleep_interval
[3]),
236 le32_to_cpu(cmd
->sleep_interval
[4]));
243 * compute the final power mode index
245 int iwl_power_update_mode(struct iwl_priv
*priv
, bool force
)
247 struct iwl_power_mgr
*setting
= &(priv
->power_data
);
249 u16
uninitialized_var(final_mode
);
252 /* Don't update the RX chain when chain noise calibration is running */
253 update_chains
= priv
->chain_noise_data
.state
== IWL_CHAIN_NOISE_DONE
||
254 priv
->chain_noise_data
.state
== IWL_CHAIN_NOISE_ALIVE
;
256 /* If on battery, set to 3,
257 * if plugged into AC power, set to CAM ("continuously aware mode"),
260 switch (setting
->system_power_setting
) {
261 case IWL_POWER_SYS_AUTO
:
262 final_mode
= iwl_get_auto_power_mode(priv
);
264 case IWL_POWER_SYS_BATTERY
:
265 final_mode
= IWL_POWER_INDEX_3
;
267 case IWL_POWER_SYS_AC
:
268 final_mode
= IWL_POWER_MODE_CAM
;
271 final_mode
= IWL_POWER_INDEX_3
;
275 if (setting
->critical_power_setting
> final_mode
)
276 final_mode
= setting
->critical_power_setting
;
278 /* driver only support CAM for non STA network */
279 if (priv
->iw_mode
!= NL80211_IFTYPE_STATION
)
280 final_mode
= IWL_POWER_MODE_CAM
;
282 if (!iwl_is_rfkill(priv
) && !setting
->power_disabled
&&
283 ((setting
->power_mode
!= final_mode
) || force
)) {
284 struct iwl_powertable_cmd cmd
;
286 if (final_mode
!= IWL_POWER_MODE_CAM
)
287 set_bit(STATUS_POWER_PMI
, &priv
->status
);
289 iwl_update_power_cmd(priv
, &cmd
, final_mode
);
290 cmd
.keep_alive_beacons
= 0;
292 if (final_mode
== IWL_POWER_INDEX_5
)
293 cmd
.flags
|= IWL_POWER_FAST_PD
;
295 ret
= iwl_set_power(priv
, &cmd
);
297 if (final_mode
== IWL_POWER_MODE_CAM
)
298 clear_bit(STATUS_POWER_PMI
, &priv
->status
);
300 set_bit(STATUS_POWER_PMI
, &priv
->status
);
302 if (priv
->cfg
->ops
->lib
->update_chain_flags
&& update_chains
)
303 priv
->cfg
->ops
->lib
->update_chain_flags(priv
);
305 IWL_DEBUG_POWER("Cannot update the power, chain noise "
306 "calibration running: %d\n",
307 priv
->chain_noise_data
.state
);
309 setting
->power_mode
= final_mode
;
314 EXPORT_SYMBOL(iwl_power_update_mode
);
316 /* Allow other iwl code to disable/enable power management active
317 * this will be useful for rate scale to disable PM during heavy
320 int iwl_power_disable_management(struct iwl_priv
*priv
, u32 ms
)
325 if (priv
->power_data
.power_disabled
)
328 prev_mode
= priv
->power_data
.user_power_setting
;
329 priv
->power_data
.user_power_setting
= IWL_POWER_MODE_CAM
;
330 ret
= iwl_power_update_mode(priv
, 0);
331 priv
->power_data
.power_disabled
= 1;
332 priv
->power_data
.user_power_setting
= prev_mode
;
333 cancel_delayed_work(&priv
->set_power_save
);
335 queue_delayed_work(priv
->workqueue
, &priv
->set_power_save
,
336 msecs_to_jiffies(ms
));
341 EXPORT_SYMBOL(iwl_power_disable_management
);
343 /* Allow other iwl code to disable/enable power management active
344 * this will be useful for rate scale to disable PM during high
347 int iwl_power_enable_management(struct iwl_priv
*priv
)
351 priv
->power_data
.power_disabled
= 0;
352 ret
= iwl_power_update_mode(priv
, 0);
355 EXPORT_SYMBOL(iwl_power_enable_management
);
357 /* set user_power_setting */
358 int iwl_power_set_user_mode(struct iwl_priv
*priv
, u16 mode
)
360 if (mode
> IWL_POWER_LIMIT
)
363 priv
->power_data
.user_power_setting
= mode
;
365 return iwl_power_update_mode(priv
, 0);
367 EXPORT_SYMBOL(iwl_power_set_user_mode
);
369 /* set system_power_setting. This should be set by over all
372 int iwl_power_set_system_mode(struct iwl_priv
*priv
, u16 mode
)
374 if (mode
> IWL_POWER_LIMIT
)
377 priv
->power_data
.system_power_setting
= mode
;
379 return iwl_power_update_mode(priv
, 0);
381 EXPORT_SYMBOL(iwl_power_set_system_mode
);
383 /* initialize to default */
384 void iwl_power_initialize(struct iwl_priv
*priv
)
386 iwl_power_init_handle(priv
);
387 priv
->power_data
.user_power_setting
= IWL_POWER_AUTO
;
388 priv
->power_data
.system_power_setting
= IWL_POWER_SYS_AUTO
;
389 priv
->power_data
.power_disabled
= 0;
390 priv
->power_data
.is_battery_active
= 0;
391 priv
->power_data
.critical_power_setting
= 0;
393 EXPORT_SYMBOL(iwl_power_initialize
);
395 /* set critical_power_setting according to temperature value */
396 int iwl_power_temperature_change(struct iwl_priv
*priv
)
399 s32 temperature
= KELVIN_TO_CELSIUS(priv
->last_temperature
);
400 u16 new_critical
= priv
->power_data
.critical_power_setting
;
402 if (temperature
> IWL_CT_KILL_TEMPERATURE
)
404 else if (temperature
> IWL_MIN_POWER_TEMPERATURE
)
405 new_critical
= IWL_POWER_INDEX_5
;
406 else if (temperature
> IWL_REDUCED_POWER_TEMPERATURE
)
407 new_critical
= IWL_POWER_INDEX_3
;
409 new_critical
= IWL_POWER_MODE_CAM
;
411 if (new_critical
!= priv
->power_data
.critical_power_setting
)
412 priv
->power_data
.critical_power_setting
= new_critical
;
414 if (priv
->power_data
.critical_power_setting
>
415 priv
->power_data
.power_mode
)
416 ret
= iwl_power_update_mode(priv
, 0);
420 EXPORT_SYMBOL(iwl_power_temperature_change
);
422 static void iwl_bg_set_power_save(struct work_struct
*work
)
424 struct iwl_priv
*priv
= container_of(work
,
425 struct iwl_priv
, set_power_save
.work
);
426 IWL_DEBUG(IWL_DL_STATE
, "update power\n");
428 if (test_bit(STATUS_EXIT_PENDING
, &priv
->status
))
431 mutex_lock(&priv
->mutex
);
433 /* on starting association we disable power management
434 * until association, if association failed then this
435 * timer will expire and enable PM again.
437 if (!iwl_is_associated(priv
))
438 iwl_power_enable_management(priv
);
440 mutex_unlock(&priv
->mutex
);
442 void iwl_setup_power_deferred_work(struct iwl_priv
*priv
)
444 INIT_DELAYED_WORK(&priv
->set_power_save
, iwl_bg_set_power_save
);
446 EXPORT_SYMBOL(iwl_setup_power_deferred_work
);
448 void iwl_power_cancel_timeout(struct iwl_priv
*priv
)
450 cancel_delayed_work(&priv
->set_power_save
);
452 EXPORT_SYMBOL(iwl_power_cancel_timeout
);