1 /******************************************************************************
3 * Copyright(c) 2007 - 2010 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/slab.h>
33 #include <linux/init.h>
35 #include <net/mac80211.h>
37 #include "iwl-eeprom.h"
41 #include "iwl-commands.h"
42 #include "iwl-debug.h"
43 #include "iwl-power.h"
46 * Setting power level allows the card to go to sleep when not busy.
48 * We calculate a sleep command based on the required latency, which
49 * we get from mac80211. In order to handle thermal throttling, we can
50 * also use pre-defined power levels.
54 * For now, keep using power level 1 instead of automatically
57 bool no_sleep_autoadjust
= true;
58 module_param(no_sleep_autoadjust
, bool, S_IRUGO
);
59 MODULE_PARM_DESC(no_sleep_autoadjust
,
60 "don't automatically adjust sleep level "
61 "according to maximum network latency");
64 * This defines the old power levels. They are still used by default
65 * (level 1) and for thermal throttle (levels 3 through 5)
68 struct iwl_power_vec_entry
{
69 struct iwl_powertable_cmd cmd
;
70 u8 no_dtim
; /* number of skip dtim */
73 #define IWL_DTIM_RANGE_0_MAX 2
74 #define IWL_DTIM_RANGE_1_MAX 10
76 #define NOSLP cpu_to_le16(0), 0, 0
77 #define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
78 #define TU_TO_USEC 1024
79 #define SLP_TOUT(T) cpu_to_le32((T) * TU_TO_USEC)
80 #define SLP_VEC(X0, X1, X2, X3, X4) {cpu_to_le32(X0), \
85 /* default power management (not Tx power) table values */
86 /* for DTIM period 0 through IWL_DTIM_RANGE_0_MAX */
88 static const struct iwl_power_vec_entry range_0
[IWL_POWER_NUM
] = {
89 {{SLP
, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 1, 2, 2, 0xFF)}, 0},
90 {{SLP
, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
91 {{SLP
, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 2, 2, 2, 0xFF)}, 0},
92 {{SLP
, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 2, 4, 4, 0xFF)}, 1},
93 {{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 */
99 static const struct iwl_power_vec_entry range_1
[IWL_POWER_NUM
] = {
100 {{SLP
, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
101 {{SLP
, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 3, 4, 7)}, 0},
102 {{SLP
, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 4, 6, 7, 9)}, 0},
103 {{SLP
, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 4, 6, 9, 10)}, 1},
104 {{SLP
, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 4, 6, 10, 10)}, 2}
107 /* for DTIM period > IWL_DTIM_RANGE_1_MAX */
109 static const struct iwl_power_vec_entry range_2
[IWL_POWER_NUM
] = {
110 {{SLP
, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
111 {{SLP
, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
112 {{SLP
, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
113 {{SLP
, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
114 {{SLP
, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
117 static void iwl_static_sleep_cmd(struct iwl_priv
*priv
,
118 struct iwl_powertable_cmd
*cmd
,
119 enum iwl_power_level lvl
, int period
)
121 const struct iwl_power_vec_entry
*table
;
122 int max_sleep
[IWL_POWER_VEC_SIZE
] = { 0 };
128 if (period
<= IWL_DTIM_RANGE_1_MAX
)
130 if (period
<= IWL_DTIM_RANGE_0_MAX
)
133 BUG_ON(lvl
< 0 || lvl
>= IWL_POWER_NUM
);
135 *cmd
= table
[lvl
].cmd
;
140 for (i
= 0; i
< IWL_POWER_VEC_SIZE
; i
++)
144 skip
= table
[lvl
].no_dtim
;
145 for (i
= 0; i
< IWL_POWER_VEC_SIZE
; i
++)
146 max_sleep
[i
] = le32_to_cpu(cmd
->sleep_interval
[i
]);
147 max_sleep
[IWL_POWER_VEC_SIZE
- 1] = skip
+ 1;
150 slp_itrvl
= le32_to_cpu(cmd
->sleep_interval
[IWL_POWER_VEC_SIZE
- 1]);
151 /* figure out the listen interval based on dtim period and skip */
152 if (slp_itrvl
== 0xFF)
153 cmd
->sleep_interval
[IWL_POWER_VEC_SIZE
- 1] =
154 cpu_to_le32(period
* (skip
+ 1));
156 slp_itrvl
= le32_to_cpu(cmd
->sleep_interval
[IWL_POWER_VEC_SIZE
- 1]);
157 if (slp_itrvl
> period
)
158 cmd
->sleep_interval
[IWL_POWER_VEC_SIZE
- 1] =
159 cpu_to_le32((slp_itrvl
/ period
) * period
);
162 cmd
->flags
|= IWL_POWER_SLEEP_OVER_DTIM_MSK
;
164 cmd
->flags
&= ~IWL_POWER_SLEEP_OVER_DTIM_MSK
;
166 if (priv
->cfg
->bt_params
&&
167 priv
->cfg
->bt_params
->advanced_bt_coexist
) {
168 if (!priv
->cfg
->bt_params
->bt_sco_disable
)
169 cmd
->flags
|= IWL_POWER_BT_SCO_ENA
;
171 cmd
->flags
&= ~IWL_POWER_BT_SCO_ENA
;
175 slp_itrvl
= le32_to_cpu(cmd
->sleep_interval
[IWL_POWER_VEC_SIZE
- 1]);
176 if (slp_itrvl
> IWL_CONN_MAX_LISTEN_INTERVAL
)
177 cmd
->sleep_interval
[IWL_POWER_VEC_SIZE
- 1] =
178 cpu_to_le32(IWL_CONN_MAX_LISTEN_INTERVAL
);
180 /* enforce max sleep interval */
181 for (i
= IWL_POWER_VEC_SIZE
- 1; i
>= 0 ; i
--) {
182 if (le32_to_cpu(cmd
->sleep_interval
[i
]) >
183 (max_sleep
[i
] * period
))
184 cmd
->sleep_interval
[i
] =
185 cpu_to_le32(max_sleep
[i
] * period
);
186 if (i
!= (IWL_POWER_VEC_SIZE
- 1)) {
187 if (le32_to_cpu(cmd
->sleep_interval
[i
]) >
188 le32_to_cpu(cmd
->sleep_interval
[i
+1]))
189 cmd
->sleep_interval
[i
] =
190 cmd
->sleep_interval
[i
+1];
194 if (priv
->power_data
.pci_pm
)
195 cmd
->flags
|= IWL_POWER_PCI_PM_MSK
;
197 cmd
->flags
&= ~IWL_POWER_PCI_PM_MSK
;
199 IWL_DEBUG_POWER(priv
, "numSkipDtim = %u, dtimPeriod = %d\n",
201 IWL_DEBUG_POWER(priv
, "Sleep command for index %d\n", lvl
+ 1);
204 static void iwl_power_sleep_cam_cmd(struct iwl_priv
*priv
,
205 struct iwl_powertable_cmd
*cmd
)
207 memset(cmd
, 0, sizeof(*cmd
));
209 if (priv
->power_data
.pci_pm
)
210 cmd
->flags
|= IWL_POWER_PCI_PM_MSK
;
212 IWL_DEBUG_POWER(priv
, "Sleep command for CAM\n");
215 static void iwl_power_fill_sleep_cmd(struct iwl_priv
*priv
,
216 struct iwl_powertable_cmd
*cmd
,
217 int dynps_ms
, int wakeup_period
)
220 * These are the original power level 3 sleep successions. The
221 * device may behave better with such succession and was also
222 * only tested with that. Just like the original sleep commands,
223 * also adjust the succession here to the wakeup_period below.
224 * The ranges are the same as for the sleep commands, 0-2, 3-9
225 * and >10, which is selected based on the DTIM interval for
226 * the sleep index but here we use the wakeup period since that
227 * is what we need to do for the latency requirements.
229 static const u8 slp_succ_r0
[IWL_POWER_VEC_SIZE
] = { 2, 2, 2, 2, 2 };
230 static const u8 slp_succ_r1
[IWL_POWER_VEC_SIZE
] = { 2, 4, 6, 7, 9 };
231 static const u8 slp_succ_r2
[IWL_POWER_VEC_SIZE
] = { 2, 7, 9, 9, 0xFF };
232 const u8
*slp_succ
= slp_succ_r0
;
235 if (wakeup_period
> IWL_DTIM_RANGE_0_MAX
)
236 slp_succ
= slp_succ_r1
;
237 if (wakeup_period
> IWL_DTIM_RANGE_1_MAX
)
238 slp_succ
= slp_succ_r2
;
240 memset(cmd
, 0, sizeof(*cmd
));
242 cmd
->flags
= IWL_POWER_DRIVER_ALLOW_SLEEP_MSK
|
243 IWL_POWER_FAST_PD
; /* no use seeing frames for others */
245 if (priv
->power_data
.pci_pm
)
246 cmd
->flags
|= IWL_POWER_PCI_PM_MSK
;
248 if (priv
->cfg
->bt_params
&&
249 priv
->cfg
->bt_params
->advanced_bt_coexist
) {
250 if (!priv
->cfg
->bt_params
->bt_sco_disable
)
251 cmd
->flags
|= IWL_POWER_BT_SCO_ENA
;
253 cmd
->flags
&= ~IWL_POWER_BT_SCO_ENA
;
256 cmd
->rx_data_timeout
= cpu_to_le32(1000 * dynps_ms
);
257 cmd
->tx_data_timeout
= cpu_to_le32(1000 * dynps_ms
);
259 for (i
= 0; i
< IWL_POWER_VEC_SIZE
; i
++)
260 cmd
->sleep_interval
[i
] =
261 cpu_to_le32(min_t(int, slp_succ
[i
], wakeup_period
));
263 IWL_DEBUG_POWER(priv
, "Automatic sleep command\n");
266 static int iwl_set_power(struct iwl_priv
*priv
, struct iwl_powertable_cmd
*cmd
)
268 IWL_DEBUG_POWER(priv
, "Sending power/sleep command\n");
269 IWL_DEBUG_POWER(priv
, "Flags value = 0x%08X\n", cmd
->flags
);
270 IWL_DEBUG_POWER(priv
, "Tx timeout = %u\n", le32_to_cpu(cmd
->tx_data_timeout
));
271 IWL_DEBUG_POWER(priv
, "Rx timeout = %u\n", le32_to_cpu(cmd
->rx_data_timeout
));
272 IWL_DEBUG_POWER(priv
, "Sleep interval vector = { %d , %d , %d , %d , %d }\n",
273 le32_to_cpu(cmd
->sleep_interval
[0]),
274 le32_to_cpu(cmd
->sleep_interval
[1]),
275 le32_to_cpu(cmd
->sleep_interval
[2]),
276 le32_to_cpu(cmd
->sleep_interval
[3]),
277 le32_to_cpu(cmd
->sleep_interval
[4]));
279 return iwl_send_cmd_pdu(priv
, POWER_TABLE_CMD
,
280 sizeof(struct iwl_powertable_cmd
), cmd
);
283 static void iwl_power_build_cmd(struct iwl_priv
*priv
,
284 struct iwl_powertable_cmd
*cmd
)
286 bool enabled
= priv
->hw
->conf
.flags
& IEEE80211_CONF_PS
;
289 dtimper
= priv
->hw
->conf
.ps_dtim_period
?: 1;
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 priv
->cfg
->ops
->lib
->tt_ops
.tt_power_mode
&&
298 priv
->cfg
->ops
->lib
->tt_ops
.lower_power_detection(priv
)) {
299 /* 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
);
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 priv
->power_data
.debug_sleep_level_override
,
308 else if (no_sleep_autoadjust
)
309 iwl_static_sleep_cmd(priv
, cmd
, IWL_POWER_INDEX_1
, dtimper
);
311 iwl_power_fill_sleep_cmd(priv
, cmd
,
312 priv
->hw
->conf
.dynamic_ps_timeout
,
313 priv
->hw
->conf
.max_sleep_period
);
316 int iwl_power_set_mode(struct iwl_priv
*priv
, struct iwl_powertable_cmd
*cmd
,
322 lockdep_assert_held(&priv
->mutex
);
324 /* Don't update the RX chain when chain noise calibration is running */
325 update_chains
= priv
->chain_noise_data
.state
== IWL_CHAIN_NOISE_DONE
||
326 priv
->chain_noise_data
.state
== IWL_CHAIN_NOISE_ALIVE
;
328 if (!memcmp(&priv
->power_data
.sleep_cmd
, cmd
, sizeof(*cmd
)) && !force
)
331 if (!iwl_is_ready_rf(priv
))
334 /* scan complete use sleep_power_next, need to be updated */
335 memcpy(&priv
->power_data
.sleep_cmd_next
, cmd
, sizeof(*cmd
));
336 if (test_bit(STATUS_SCANNING
, &priv
->status
) && !force
) {
337 IWL_DEBUG_INFO(priv
, "Defer power set mode while scanning\n");
341 if (cmd
->flags
& IWL_POWER_DRIVER_ALLOW_SLEEP_MSK
)
342 set_bit(STATUS_POWER_PMI
, &priv
->status
);
344 ret
= iwl_set_power(priv
, cmd
);
346 if (!(cmd
->flags
& IWL_POWER_DRIVER_ALLOW_SLEEP_MSK
))
347 clear_bit(STATUS_POWER_PMI
, &priv
->status
);
349 if (priv
->cfg
->ops
->lib
->update_chain_flags
&& update_chains
)
350 priv
->cfg
->ops
->lib
->update_chain_flags(priv
);
351 else if (priv
->cfg
->ops
->lib
->update_chain_flags
)
352 IWL_DEBUG_POWER(priv
,
353 "Cannot update the power, chain noise "
354 "calibration running: %d\n",
355 priv
->chain_noise_data
.state
);
357 memcpy(&priv
->power_data
.sleep_cmd
, cmd
, sizeof(*cmd
));
359 IWL_ERR(priv
, "set power fail, ret = %d", ret
);
363 EXPORT_SYMBOL(iwl_power_set_mode
);
365 int iwl_power_update_mode(struct iwl_priv
*priv
, bool force
)
367 struct iwl_powertable_cmd cmd
;
369 iwl_power_build_cmd(priv
, &cmd
);
370 return iwl_power_set_mode(priv
, &cmd
, force
);
372 EXPORT_SYMBOL(iwl_power_update_mode
);
374 /* initialize to default */
375 void iwl_power_initialize(struct iwl_priv
*priv
)
377 u16 lctl
= iwl_pcie_link_ctl(priv
);
379 priv
->power_data
.pci_pm
= !(lctl
& PCI_CFG_LINK_CTRL_VAL_L0S_EN
);
381 priv
->power_data
.debug_sleep_level_override
= -1;
383 memset(&priv
->power_data
.sleep_cmd
, 0,
384 sizeof(priv
->power_data
.sleep_cmd
));
386 EXPORT_SYMBOL(iwl_power_initialize
);