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