blob: a099c9e30e55dcdfe0d933a3e64c4947719bd593 [file] [log] [blame]
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -07001/******************************************************************************
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:
25 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
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>
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -070032#include <linux/init.h>
33
34#include <net/mac80211.h>
35
36#include "iwl-eeprom.h"
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070037#include "iwl-dev.h"
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -070038#include "iwl-core.h"
Tomas Winkler5a36ba02008-04-24 11:55:37 -070039#include "iwl-commands.h"
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -070040#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 */
83/* for tim 0-10 */
Esti Kummer298df1f2008-07-18 13:52:58 +080084static struct iwl_power_vec_entry range_0[IWL_POWER_MAX] = {
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -070085 {{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
94/* for tim = 3-10 */
Esti Kummer298df1f2008-07-18 13:52:58 +080095static struct iwl_power_vec_entry range_1[IWL_POWER_MAX] = {
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -070096 {{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
104/* for tim > 11 */
Esti Kummer298df1f2008-07-18 13:52:58 +0800105static struct iwl_power_vec_entry range_2[IWL_POWER_MAX] = {
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700106 {{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
Mohamed Abbasca579612008-07-18 13:52:57 +0800114/* 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}
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700121/* 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{
Esti Kummer298df1f2008-07-18 13:52:58 +0800126 u16 mode;
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700127
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;
Esti Kummer298df1f2008-07-18 13:52:58 +0800138 /* FIXME: remove battery and ac from here */
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700139 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;
Esti Kummer298df1f2008-07-18 13:52:58 +0800145 default:
146 mode = priv->power_data.user_power_setting;
147 break;
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700148 }
149 return mode;
150}
151
152/* initialize to default */
153static int iwl_power_init_handle(struct iwl_priv *priv)
154{
155 int ret = 0, i;
156 struct iwl_power_mgr *pow_data;
Esti Kummer298df1f2008-07-18 13:52:58 +0800157 int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_MAX;
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700158 u16 pci_pm;
159
160 IWL_DEBUG_POWER("Initialize power \n");
161
162 pow_data = &(priv->power_data);
163
164 memset(pow_data, 0, sizeof(*pow_data));
165
166 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
167 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
168 memcpy(&pow_data->pwr_range_2[0], &range_2[0], size);
169
170 ret = pci_read_config_word(priv->pci_dev,
171 PCI_LINK_CTRL, &pci_pm);
172 if (ret != 0)
173 return 0;
174 else {
Mohamed Abbasca579612008-07-18 13:52:57 +0800175 struct iwl_powertable_cmd *cmd;
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700176
177 IWL_DEBUG_POWER("adjust power command flags\n");
178
Esti Kummer298df1f2008-07-18 13:52:58 +0800179 for (i = 0; i < IWL_POWER_MAX; i++) {
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700180 cmd = &pow_data->pwr_range_0[i].cmd;
181
182 if (pci_pm & 0x1)
183 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
184 else
185 cmd->flags |= IWL_POWER_PCI_PM_MSK;
186 }
187 }
188 return ret;
189}
190
191/* adjust power command according to dtim period and power level*/
192static int iwl_update_power_command(struct iwl_priv *priv,
Mohamed Abbasca579612008-07-18 13:52:57 +0800193 struct iwl_powertable_cmd *cmd,
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700194 u16 mode)
195{
196 int ret = 0, i;
197 u8 skip;
198 u32 max_sleep = 0;
199 struct iwl_power_vec_entry *range;
200 u8 period = 0;
201 struct iwl_power_mgr *pow_data;
202
203 if (mode > IWL_POWER_INDEX_5) {
204 IWL_DEBUG_POWER("Error invalid power mode \n");
205 return -1;
206 }
207 pow_data = &(priv->power_data);
208
209 if (pow_data->dtim_period <= IWL_POWER_RANGE_0_MAX)
210 range = &pow_data->pwr_range_0[0];
211 else if (pow_data->dtim_period <= IWL_POWER_RANGE_1_MAX)
212 range = &pow_data->pwr_range_1[0];
213 else
214 range = &pow_data->pwr_range_2[0];
215
216 period = pow_data->dtim_period;
Mohamed Abbasca579612008-07-18 13:52:57 +0800217 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700218
219 if (period == 0) {
220 period = 1;
221 skip = 0;
222 } else
223 skip = range[mode].no_dtim;
224
225 if (skip == 0) {
226 max_sleep = period;
227 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
228 } else {
229 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
230 max_sleep = le32_to_cpu(slp_itrvl);
231 if (max_sleep == 0xFF)
232 max_sleep = period * (skip + 1);
233 else if (max_sleep > period)
234 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
235 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
236 }
237
238 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
239 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
240 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
241 }
242
243 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
244 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
245 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
246 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
247 le32_to_cpu(cmd->sleep_interval[0]),
248 le32_to_cpu(cmd->sleep_interval[1]),
249 le32_to_cpu(cmd->sleep_interval[2]),
250 le32_to_cpu(cmd->sleep_interval[3]),
251 le32_to_cpu(cmd->sleep_interval[4]));
252
253 return ret;
254}
255
256
257/*
258 * calucaute the final power mode index
259 */
260int iwl_power_update_mode(struct iwl_priv *priv, u8 refresh)
261{
262 struct iwl_power_mgr *setting = &(priv->power_data);
263 int ret = 0;
264 u16 uninitialized_var(final_mode);
265
266 /* If on battery, set to 3,
267 * if plugged into AC power, set to CAM ("continuously aware mode"),
268 * else user level */
269
270 switch (setting->system_power_setting) {
Esti Kummer298df1f2008-07-18 13:52:58 +0800271 case IWL_POWER_SYS_AUTO:
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700272 final_mode = iwl_get_auto_power_mode(priv);
273 break;
Esti Kummer298df1f2008-07-18 13:52:58 +0800274 case IWL_POWER_SYS_BATTERY:
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700275 final_mode = IWL_POWER_INDEX_3;
276 break;
Esti Kummer298df1f2008-07-18 13:52:58 +0800277 case IWL_POWER_SYS_AC:
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700278 final_mode = IWL_POWER_MODE_CAM;
279 break;
280 default:
Esti Kummer298df1f2008-07-18 13:52:58 +0800281 final_mode = IWL_POWER_INDEX_3;
282 WARN_ON(1);
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700283 }
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 */
289 if (priv->iw_mode != IEEE80211_IF_TYPE_STA)
290 final_mode = IWL_POWER_MODE_CAM;
291
292 if (!iwl_is_rfkill(priv) && !setting->power_disabled &&
293 ((setting->power_mode != final_mode) || refresh)) {
Mohamed Abbasca579612008-07-18 13:52:57 +0800294 struct iwl_powertable_cmd cmd;
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700295
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
Mohamed Abbasca579612008-07-18 13:52:57 +0800305 ret = iwl_set_power(priv, &cmd);
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700306
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
312 if (priv->cfg->ops->lib->update_chain_flags)
313 priv->cfg->ops->lib->update_chain_flags(priv);
314
315 if (!ret)
316 setting->power_mode = final_mode;
317 }
318
319 return ret;
320}
321EXPORT_SYMBOL(iwl_power_update_mode);
322
323/* Allow other iwl code to disable/enable power management active
324 * this will be usefull for rate scale to disable PM during heavy
325 * Tx/Rx activities
326 */
327int iwl_power_disable_management(struct iwl_priv *priv)
328{
329 u16 prev_mode;
330 int ret = 0;
331
332 if (priv->power_data.power_disabled)
333 return -EBUSY;
334
335 prev_mode = priv->power_data.user_power_setting;
336 priv->power_data.user_power_setting = IWL_POWER_MODE_CAM;
337 ret = iwl_power_update_mode(priv, 0);
338 priv->power_data.power_disabled = 1;
339 priv->power_data.user_power_setting = prev_mode;
340
341 return ret;
342}
343EXPORT_SYMBOL(iwl_power_disable_management);
344
345/* Allow other iwl code to disable/enable power management active
346 * this will be usefull for rate scale to disable PM during hight
347 * valume activities
348 */
349int iwl_power_enable_management(struct iwl_priv *priv)
350{
351 int ret = 0;
352
353 priv->power_data.power_disabled = 0;
354 ret = iwl_power_update_mode(priv, 0);
355 return ret;
356}
357EXPORT_SYMBOL(iwl_power_enable_management);
358
359/* set user_power_setting */
360int iwl_power_set_user_mode(struct iwl_priv *priv, u16 mode)
361{
362 int ret = 0;
363
364 if (mode > IWL_POWER_LIMIT)
365 return -EINVAL;
366
367 priv->power_data.user_power_setting = mode;
368
369 ret = iwl_power_update_mode(priv, 0);
370
371 return ret;
372}
373EXPORT_SYMBOL(iwl_power_set_user_mode);
374
375
376/* set system_power_setting. This should be set by over all
377 * PM application.
378 */
379int iwl_power_set_system_mode(struct iwl_priv *priv, u16 mode)
380{
381 int ret = 0;
382
383 if (mode > IWL_POWER_LIMIT)
384 return -EINVAL;
385
386 priv->power_data.system_power_setting = mode;
387
388 ret = iwl_power_update_mode(priv, 0);
389
390 return ret;
391}
392EXPORT_SYMBOL(iwl_power_set_system_mode);
393
394/* initilize to default */
395void iwl_power_initialize(struct iwl_priv *priv)
396{
397
398 iwl_power_init_handle(priv);
399 priv->power_data.user_power_setting = IWL_POWER_AUTO;
400 priv->power_data.power_disabled = 0;
Esti Kummer298df1f2008-07-18 13:52:58 +0800401 priv->power_data.system_power_setting = IWL_POWER_SYS_AUTO;
Mohamed Abbas5da4b55f2008-04-21 15:41:51 -0700402 priv->power_data.is_battery_active = 0;
403 priv->power_data.power_disabled = 0;
404 priv->power_data.critical_power_setting = 0;
405}
406EXPORT_SYMBOL(iwl_power_initialize);
407
408/* set critical_power_setting according to temperature value */
409int iwl_power_temperature_change(struct iwl_priv *priv)
410{
411 int ret = 0;
412 u16 new_critical = priv->power_data.critical_power_setting;
413 s32 temperature = KELVIN_TO_CELSIUS(priv->last_temperature);
414
415 if (temperature > IWL_CT_KILL_TEMPERATURE)
416 return 0;
417 else if (temperature > IWL_MIN_POWER_TEMPERATURE)
418 new_critical = IWL_POWER_INDEX_5;
419 else if (temperature > IWL_REDUCED_POWER_TEMPERATURE)
420 new_critical = IWL_POWER_INDEX_3;
421 else
422 new_critical = IWL_POWER_MODE_CAM;
423
424 if (new_critical != priv->power_data.critical_power_setting)
425 priv->power_data.critical_power_setting = new_critical;
426
427 if (priv->power_data.critical_power_setting >
428 priv->power_data.power_mode)
429 ret = iwl_power_update_mode(priv, 0);
430
431 return ret;
432}
433EXPORT_SYMBOL(iwl_power_temperature_change);