iwlwifi: fix PS disable status race
[linux-2.6-block.git] / drivers / net / wireless / iwlwifi / iwl-power.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2009 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  *  Intel Linux Wireless <ilw@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>
32 #include <linux/init.h>
33
34 #include <net/mac80211.h>
35
36 #include "iwl-eeprom.h"
37 #include "iwl-dev.h"
38 #include "iwl-core.h"
39 #include "iwl-commands.h"
40 #include "iwl-debug.h"
41 #include "iwl-power.h"
42
43 /*
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
50  *      mac80211
51  *
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
54  * status.
55  *
56  */
57
58 #define MSEC_TO_USEC 1024
59 #define IWL_POWER_RANGE_0_MAX  (2)
60 #define IWL_POWER_RANGE_1_MAX  (10)
61
62
63
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
67
68
69 #define IWL_CT_KILL_TEMPERATURE         110
70 #define IWL_MIN_POWER_TEMPERATURE       100
71 #define IWL_REDUCED_POWER_TEMPERATURE   95
72
73 /* default power management (not Tx power) table values */
74 /* for TIM  0-10 */
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}
82 };
83
84
85 /* for TIM = 3-10 */
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}
93 };
94
95 /* for TIM > 11 */
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}
103 };
104
105
106 /* set card power command */
107 static int iwl_set_power(struct iwl_priv *priv, void *cmd)
108 {
109         return iwl_send_cmd_pdu(priv, POWER_TABLE_CMD,
110                                 sizeof(struct iwl_powertable_cmd), cmd);
111 }
112 /* decide the right power level according to association status
113  * and battery status
114  */
115 static u16 iwl_get_auto_power_mode(struct iwl_priv *priv)
116 {
117         u16 mode;
118
119         switch (priv->power_data.user_power_setting) {
120         case IWL_POWER_AUTO:
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;
126                 else
127                         mode = IWL_POWER_ON_AC_DISASSOC;
128                 break;
129         default:
130                 mode = priv->power_data.user_power_setting;
131                 break;
132         }
133         return mode;
134 }
135
136 /* initialize to default */
137 static void iwl_power_init_handle(struct iwl_priv *priv)
138 {
139         struct iwl_power_mgr *pow_data;
140         int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_MAX;
141         struct iwl_powertable_cmd *cmd;
142         int i;
143         u16 lctl;
144
145         IWL_DEBUG_POWER(priv, "Initialize power \n");
146
147         pow_data = &priv->power_data;
148
149         memset(pow_data, 0, sizeof(*pow_data));
150
151         memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
152         memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
153         memcpy(&pow_data->pwr_range_2[0], &range_2[0], size);
154
155         lctl = iwl_pcie_link_ctl(priv);
156
157         IWL_DEBUG_POWER(priv, "adjust power command flags\n");
158
159         for (i = 0; i < IWL_POWER_MAX; i++) {
160                 cmd = &pow_data->pwr_range_0[i].cmd;
161
162                 if (lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN)
163                         cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
164                 else
165                         cmd->flags |= IWL_POWER_PCI_PM_MSK;
166         }
167 }
168
169 /* adjust power command according to DTIM period and power level*/
170 static int iwl_update_power_cmd(struct iwl_priv *priv,
171                                 struct iwl_powertable_cmd *cmd, u16 mode)
172 {
173         struct iwl_power_vec_entry *range;
174         struct iwl_power_mgr *pow_data;
175         int i;
176         u32 max_sleep = 0;
177         u8 period;
178         bool skip;
179
180         if (mode > IWL_POWER_INDEX_5) {
181                 IWL_DEBUG_POWER(priv, "Error invalid power mode \n");
182                 return -EINVAL;
183         }
184
185         pow_data = &priv->power_data;
186
187         if (pow_data->dtim_period <= IWL_POWER_RANGE_0_MAX)
188                 range = &pow_data->pwr_range_0[0];
189         else if (pow_data->dtim_period <= IWL_POWER_RANGE_1_MAX)
190                 range = &pow_data->pwr_range_1[0];
191         else
192                 range = &pow_data->pwr_range_2[0];
193
194         period = pow_data->dtim_period;
195         memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
196
197         if (period == 0) {
198                 period = 1;
199                 skip = false;
200         } else {
201                 skip = !!range[mode].no_dtim;
202         }
203
204         if (skip) {
205                 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
206                 max_sleep = le32_to_cpu(slp_itrvl);
207                 if (max_sleep == 0xFF)
208                         max_sleep = period * (skip + 1);
209                 else if (max_sleep >  period)
210                         max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
211                 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
212         } else {
213                 max_sleep = period;
214                 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
215         }
216
217         for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
218                 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
219                         cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
220
221         IWL_DEBUG_POWER(priv, "Flags value = 0x%08X\n", cmd->flags);
222         IWL_DEBUG_POWER(priv, "Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
223         IWL_DEBUG_POWER(priv, "Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
224         IWL_DEBUG_POWER(priv, "Sleep interval vector = { %d , %d , %d , %d , %d }\n",
225                         le32_to_cpu(cmd->sleep_interval[0]),
226                         le32_to_cpu(cmd->sleep_interval[1]),
227                         le32_to_cpu(cmd->sleep_interval[2]),
228                         le32_to_cpu(cmd->sleep_interval[3]),
229                         le32_to_cpu(cmd->sleep_interval[4]));
230
231         return 0;
232 }
233
234
235 /*
236  * compute the final power mode index
237  */
238 int iwl_power_update_mode(struct iwl_priv *priv, bool force)
239 {
240         struct iwl_power_mgr *setting = &(priv->power_data);
241         int ret = 0;
242         u16 uninitialized_var(final_mode);
243         bool update_chains;
244
245         /* Don't update the RX chain when chain noise calibration is running */
246         update_chains = priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE ||
247                         priv->chain_noise_data.state == IWL_CHAIN_NOISE_ALIVE;
248
249         /* If on battery, set to 3,
250          * if plugged into AC power, set to CAM ("continuously aware mode"),
251          * else user level */
252
253         switch (setting->system_power_setting) {
254         case IWL_POWER_SYS_AUTO:
255                 final_mode = iwl_get_auto_power_mode(priv);
256                 break;
257         case IWL_POWER_SYS_BATTERY:
258                 final_mode = IWL_POWER_INDEX_3;
259                 break;
260         case IWL_POWER_SYS_AC:
261                 final_mode = IWL_POWER_MODE_CAM;
262                 break;
263         default:
264                 final_mode = IWL_POWER_INDEX_3;
265                 WARN_ON(1);
266         }
267
268         if (setting->critical_power_setting > final_mode)
269                 final_mode = setting->critical_power_setting;
270
271         /* driver only support CAM for non STA network */
272         if (priv->iw_mode != NL80211_IFTYPE_STATION)
273                 final_mode = IWL_POWER_MODE_CAM;
274
275         if (iwl_is_ready_rf(priv) && !setting->power_disabled &&
276             ((setting->power_mode != final_mode) || force)) {
277                 struct iwl_powertable_cmd cmd;
278
279                 if (final_mode != IWL_POWER_MODE_CAM)
280                         set_bit(STATUS_POWER_PMI, &priv->status);
281
282                 iwl_update_power_cmd(priv, &cmd, final_mode);
283                 cmd.keep_alive_beacons = 0;
284
285                 if (final_mode == IWL_POWER_INDEX_5)
286                         cmd.flags |= IWL_POWER_FAST_PD;
287
288                 ret = iwl_set_power(priv, &cmd);
289
290                 if (final_mode == IWL_POWER_MODE_CAM)
291                         clear_bit(STATUS_POWER_PMI, &priv->status);
292                 else
293                         set_bit(STATUS_POWER_PMI, &priv->status);
294
295                 if (priv->cfg->ops->lib->update_chain_flags && update_chains)
296                         priv->cfg->ops->lib->update_chain_flags(priv);
297                 else
298                         IWL_DEBUG_POWER(priv, "Cannot update the power, chain noise "
299                                         "calibration running: %d\n",
300                                         priv->chain_noise_data.state);
301                 if (!ret)
302                         setting->power_mode = final_mode;
303         }
304
305         return ret;
306 }
307 EXPORT_SYMBOL(iwl_power_update_mode);
308
309 /* Allow other iwl code to disable/enable power management active
310  * this will be useful for rate scale to disable PM during heavy
311  * Tx/Rx activities
312  */
313 int iwl_power_disable_management(struct iwl_priv *priv, u32 ms)
314 {
315         u16 prev_mode;
316         int ret = 0;
317
318         if (priv->power_data.power_disabled)
319                 return -EBUSY;
320
321         prev_mode = priv->power_data.user_power_setting;
322         priv->power_data.user_power_setting = IWL_POWER_MODE_CAM;
323         ret = iwl_power_update_mode(priv, 0);
324         priv->power_data.power_disabled = 1;
325         priv->power_data.user_power_setting = prev_mode;
326         cancel_delayed_work(&priv->set_power_save);
327         if (ms)
328                 queue_delayed_work(priv->workqueue, &priv->set_power_save,
329                                    msecs_to_jiffies(ms));
330
331
332         return ret;
333 }
334 EXPORT_SYMBOL(iwl_power_disable_management);
335
336 /* Allow other iwl code to disable/enable power management active
337  * this will be useful for rate scale to disable PM during high
338  * volume activities
339  */
340 int iwl_power_enable_management(struct iwl_priv *priv)
341 {
342         int ret = 0;
343
344         priv->power_data.power_disabled = 0;
345         ret = iwl_power_update_mode(priv, 0);
346         return ret;
347 }
348 EXPORT_SYMBOL(iwl_power_enable_management);
349
350 /* set user_power_setting */
351 int iwl_power_set_user_mode(struct iwl_priv *priv, u16 mode)
352 {
353         if (mode > IWL_POWER_MAX)
354                 return -EINVAL;
355
356         priv->power_data.user_power_setting = mode;
357
358         return iwl_power_update_mode(priv, 0);
359 }
360 EXPORT_SYMBOL(iwl_power_set_user_mode);
361
362 /* set system_power_setting. This should be set by over all
363  * PM application.
364  */
365 int iwl_power_set_system_mode(struct iwl_priv *priv, u16 mode)
366 {
367         if (mode < IWL_POWER_SYS_MAX)
368                 priv->power_data.system_power_setting = mode;
369         else
370                 return -EINVAL;
371         return iwl_power_update_mode(priv, 0);
372 }
373 EXPORT_SYMBOL(iwl_power_set_system_mode);
374
375 /* initialize to default */
376 void iwl_power_initialize(struct iwl_priv *priv)
377 {
378         iwl_power_init_handle(priv);
379         priv->power_data.user_power_setting = IWL_POWER_AUTO;
380         priv->power_data.system_power_setting = IWL_POWER_SYS_AUTO;
381         priv->power_data.power_disabled = 0;
382         priv->power_data.is_battery_active = 0;
383         priv->power_data.critical_power_setting = 0;
384 }
385 EXPORT_SYMBOL(iwl_power_initialize);
386
387 /* set critical_power_setting according to temperature value */
388 int iwl_power_temperature_change(struct iwl_priv *priv)
389 {
390         int ret = 0;
391         s32 temperature = KELVIN_TO_CELSIUS(priv->last_temperature);
392         u16 new_critical = priv->power_data.critical_power_setting;
393
394         if (temperature > IWL_CT_KILL_TEMPERATURE)
395                 return 0;
396         else if (temperature > IWL_MIN_POWER_TEMPERATURE)
397                 new_critical = IWL_POWER_INDEX_5;
398         else if (temperature > IWL_REDUCED_POWER_TEMPERATURE)
399                 new_critical = IWL_POWER_INDEX_3;
400         else
401                 new_critical = IWL_POWER_MODE_CAM;
402
403         if (new_critical != priv->power_data.critical_power_setting)
404                 priv->power_data.critical_power_setting = new_critical;
405
406         if (priv->power_data.critical_power_setting >
407                                 priv->power_data.power_mode)
408                 ret = iwl_power_update_mode(priv, 0);
409
410         return ret;
411 }
412 EXPORT_SYMBOL(iwl_power_temperature_change);
413
414 static void iwl_bg_set_power_save(struct work_struct *work)
415 {
416         struct iwl_priv *priv = container_of(work,
417                                 struct iwl_priv, set_power_save.work);
418         IWL_DEBUG_POWER(priv, "update power\n");
419
420         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
421                 return;
422
423         mutex_lock(&priv->mutex);
424
425         /* on starting association we disable power management
426          * until association, if association failed then this
427          * timer will expire and enable PM again.
428          */
429         if (!iwl_is_associated(priv))
430                 iwl_power_enable_management(priv);
431
432         mutex_unlock(&priv->mutex);
433 }
434 void iwl_setup_power_deferred_work(struct iwl_priv *priv)
435 {
436         INIT_DELAYED_WORK(&priv->set_power_save, iwl_bg_set_power_save);
437 }
438 EXPORT_SYMBOL(iwl_setup_power_deferred_work);
439
440 void iwl_power_cancel_timeout(struct iwl_priv *priv)
441 {
442         cancel_delayed_work(&priv->set_power_save);
443 }
444 EXPORT_SYMBOL(iwl_power_cancel_timeout);