OSDN Git Service

e3c71beb01efddda45297a1f75dc64fff0569d8f
[linux-kernel-docs/linux-2.6.git] / drivers / net / wireless / iwlwifi / iwl-power.c
1 /******************************************************************************
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>
32 #include <linux/version.h>
33 #include <linux/init.h>
34
35 #include <net/mac80211.h>
36
37 #include "iwl-eeprom.h"
38 #include "iwl-dev.h"
39 #include "iwl-core.h"
40 #include "iwl-commands.h"
41 #include "iwl-debug.h"
42 #include "iwl-power.h"
43 #include "iwl-helpers.h"
44
45 /*
46  * Setting power level allow the card to go to sleep when not busy
47  * there are three factor that decide the power level to go to, they
48  * are list here with its priority
49  *  1- critical_power_setting this will be set according to card temperature.
50  *  2- system_power_setting this will be set by system PM manager.
51  *  3- user_power_setting this will be set by user either by writing to sys or
52  *      mac80211
53  *
54  * if system_power_setting and user_power_setting is set to auto
55  * the power level will be decided according to association status and battery
56  * status.
57  *
58  */
59
60 #define MSEC_TO_USEC 1024
61 #define IWL_POWER_RANGE_0_MAX  (2)
62 #define IWL_POWER_RANGE_1_MAX  (10)
63
64
65 #define NOSLP __constant_cpu_to_le16(0), 0, 0
66 #define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
67 #define SLP_TOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
68 #define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
69                                      __constant_cpu_to_le32(X1), \
70                                      __constant_cpu_to_le32(X2), \
71                                      __constant_cpu_to_le32(X3), \
72                                      __constant_cpu_to_le32(X4)}
73
74 #define IWL_POWER_ON_BATTERY            IWL_POWER_INDEX_5
75 #define IWL_POWER_ON_AC_DISASSOC        IWL_POWER_MODE_CAM
76 #define IWL_POWER_ON_AC_ASSOC           IWL_POWER_MODE_CAM
77
78
79 #define IWL_CT_KILL_TEMPERATURE         110
80 #define IWL_MIN_POWER_TEMPERATURE       100
81 #define IWL_REDUCED_POWER_TEMPERATURE   95
82
83 /* default power management (not Tx power) table values */
84 /* for tim  0-10 */
85 static struct iwl_power_vec_entry range_0[IWL_POWER_AC] = {
86         {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
87         {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
88         {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
89         {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 2, 2, 2, 0xFF)}, 0},
90         {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 2, 4, 4, 0xFF)}, 1},
91         {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 2, 4, 6, 0xFF)}, 2}
92 };
93
94
95 /* for tim = 3-10 */
96 static struct iwl_power_vec_entry range_1[IWL_POWER_AC] = {
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, 4)}, 0},
99         {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 3, 4, 7)}, 0},
100         {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 4, 6, 7, 9)}, 0},
101         {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 4, 6, 9, 10)}, 1},
102         {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 4, 7, 10, 10)}, 2}
103 };
104
105 /* for tim > 11 */
106 static struct iwl_power_vec_entry range_2[IWL_POWER_AC] = {
107         {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
108         {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
109         {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
110         {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
111         {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
112         {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
113 };
114
115 /* set card power command */
116 static int iwl_set_power(struct iwl_priv *priv, void *cmd)
117 {
118         return iwl_send_cmd_pdu_async(priv, POWER_TABLE_CMD,
119                                       sizeof(struct iwl_powertable_cmd),
120                                       cmd, NULL);
121 }
122 /* decide the right power level according to association status
123  * and battery status
124  */
125 static u16 iwl_get_auto_power_mode(struct iwl_priv *priv)
126 {
127         u16 mode = priv->power_data.user_power_setting;
128
129         switch (priv->power_data.user_power_setting) {
130         case IWL_POWER_AUTO:
131                 /* if running on battery */
132                 if (priv->power_data.is_battery_active)
133                         mode = IWL_POWER_ON_BATTERY;
134                 else if (iwl_is_associated(priv))
135                         mode = IWL_POWER_ON_AC_ASSOC;
136                 else
137                         mode = IWL_POWER_ON_AC_DISASSOC;
138                 break;
139         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;
145         }
146         return mode;
147 }
148
149 /* initialize to default */
150 static int iwl_power_init_handle(struct iwl_priv *priv)
151 {
152         int ret = 0, i;
153         struct iwl_power_mgr *pow_data;
154         int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_AC;
155         u16 pci_pm;
156
157         IWL_DEBUG_POWER("Initialize power \n");
158
159         pow_data = &(priv->power_data);
160
161         memset(pow_data, 0, sizeof(*pow_data));
162
163         memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
164         memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
165         memcpy(&pow_data->pwr_range_2[0], &range_2[0], size);
166
167         ret = pci_read_config_word(priv->pci_dev,
168                                   PCI_LINK_CTRL, &pci_pm);
169         if (ret != 0)
170                 return 0;
171         else {
172                 struct iwl_powertable_cmd *cmd;
173
174                 IWL_DEBUG_POWER("adjust power command flags\n");
175
176                 for (i = 0; i < IWL_POWER_AC; i++) {
177                         cmd = &pow_data->pwr_range_0[i].cmd;
178
179                         if (pci_pm & 0x1)
180                                 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
181                         else
182                                 cmd->flags |= IWL_POWER_PCI_PM_MSK;
183                 }
184         }
185         return ret;
186 }
187
188 /* adjust power command according to dtim period and power level*/
189 static int iwl_update_power_command(struct iwl_priv *priv,
190                                     struct iwl_powertable_cmd *cmd,
191                                     u16 mode)
192 {
193         int ret = 0, i;
194         u8 skip;
195         u32 max_sleep = 0;
196         struct iwl_power_vec_entry *range;
197         u8 period = 0;
198         struct iwl_power_mgr *pow_data;
199
200         if (mode > IWL_POWER_INDEX_5) {
201                 IWL_DEBUG_POWER("Error invalid power mode \n");
202                 return -1;
203         }
204         pow_data = &(priv->power_data);
205
206         if (pow_data->dtim_period <= IWL_POWER_RANGE_0_MAX)
207                 range = &pow_data->pwr_range_0[0];
208         else if (pow_data->dtim_period <= IWL_POWER_RANGE_1_MAX)
209                 range = &pow_data->pwr_range_1[0];
210         else
211                 range = &pow_data->pwr_range_2[0];
212
213         period = pow_data->dtim_period;
214         memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
215
216         if (period == 0) {
217                 period = 1;
218                 skip = 0;
219         } else
220                 skip = range[mode].no_dtim;
221
222         if (skip == 0) {
223                 max_sleep = period;
224                 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
225         } else {
226                 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
227                 max_sleep = le32_to_cpu(slp_itrvl);
228                 if (max_sleep == 0xFF)
229                         max_sleep = period * (skip + 1);
230                 else if (max_sleep >  period)
231                         max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
232                 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
233         }
234
235         for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
236                 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
237                         cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
238         }
239
240         IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
241         IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
242         IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
243         IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
244                         le32_to_cpu(cmd->sleep_interval[0]),
245                         le32_to_cpu(cmd->sleep_interval[1]),
246                         le32_to_cpu(cmd->sleep_interval[2]),
247                         le32_to_cpu(cmd->sleep_interval[3]),
248                         le32_to_cpu(cmd->sleep_interval[4]));
249
250         return ret;
251 }
252
253
254 /*
255  * calucaute the final power mode index
256  */
257 int iwl_power_update_mode(struct iwl_priv *priv, u8 refresh)
258 {
259         struct iwl_power_mgr *setting = &(priv->power_data);
260         int ret = 0;
261         u16 uninitialized_var(final_mode);
262
263        /* If on battery, set to 3,
264         * if plugged into AC power, set to CAM ("continuously aware mode"),
265         * else user level */
266
267         switch (setting->system_power_setting) {
268         case IWL_POWER_AUTO:
269                 final_mode = iwl_get_auto_power_mode(priv);
270                 break;
271         case IWL_POWER_BATTERY:
272                 final_mode = IWL_POWER_INDEX_3;
273                 break;
274         case IWL_POWER_AC:
275                 final_mode = IWL_POWER_MODE_CAM;
276                 break;
277         default:
278                 final_mode = setting->system_power_setting;
279         }
280
281         if (setting->critical_power_setting > final_mode)
282                 final_mode = setting->critical_power_setting;
283
284         /* driver only support CAM for non STA network */
285         if (priv->iw_mode != IEEE80211_IF_TYPE_STA)
286                 final_mode = IWL_POWER_MODE_CAM;
287
288         if (!iwl_is_rfkill(priv) && !setting->power_disabled &&
289             ((setting->power_mode != final_mode) || refresh)) {
290                 struct iwl_powertable_cmd cmd;
291
292                 if (final_mode != IWL_POWER_MODE_CAM)
293                         set_bit(STATUS_POWER_PMI, &priv->status);
294
295                 iwl_update_power_command(priv, &cmd, final_mode);
296                 cmd.keep_alive_beacons = 0;
297
298                 if (final_mode == IWL_POWER_INDEX_5)
299                         cmd.flags |= IWL_POWER_FAST_PD;
300
301                 ret = iwl_set_power(priv, &cmd);
302
303                 if (final_mode == IWL_POWER_MODE_CAM)
304                         clear_bit(STATUS_POWER_PMI, &priv->status);
305                 else
306                         set_bit(STATUS_POWER_PMI, &priv->status);
307
308                 if (priv->cfg->ops->lib->update_chain_flags)
309                         priv->cfg->ops->lib->update_chain_flags(priv);
310
311                 if (!ret)
312                         setting->power_mode = final_mode;
313         }
314
315         return ret;
316 }
317 EXPORT_SYMBOL(iwl_power_update_mode);
318
319 /* Allow other iwl code to disable/enable power management active
320  * this will be usefull for rate scale to disable PM during heavy
321  * Tx/Rx activities
322  */
323 int iwl_power_disable_management(struct iwl_priv *priv)
324 {
325         u16 prev_mode;
326         int ret = 0;
327
328         if (priv->power_data.power_disabled)
329                 return -EBUSY;
330
331         prev_mode = priv->power_data.user_power_setting;
332         priv->power_data.user_power_setting = IWL_POWER_MODE_CAM;
333         ret = iwl_power_update_mode(priv, 0);
334         priv->power_data.power_disabled = 1;
335         priv->power_data.user_power_setting = prev_mode;
336
337         return ret;
338 }
339 EXPORT_SYMBOL(iwl_power_disable_management);
340
341 /* Allow other iwl code to disable/enable power management active
342  * this will be usefull for rate scale to disable PM during hight
343  * valume activities
344  */
345 int iwl_power_enable_management(struct iwl_priv *priv)
346 {
347         int ret = 0;
348
349         priv->power_data.power_disabled = 0;
350         ret = iwl_power_update_mode(priv, 0);
351         return ret;
352 }
353 EXPORT_SYMBOL(iwl_power_enable_management);
354
355 /* set user_power_setting */
356 int iwl_power_set_user_mode(struct iwl_priv *priv, u16 mode)
357 {
358         int ret = 0;
359
360         if (mode > IWL_POWER_LIMIT)
361                 return -EINVAL;
362
363         priv->power_data.user_power_setting = mode;
364
365         ret = iwl_power_update_mode(priv, 0);
366
367         return ret;
368 }
369 EXPORT_SYMBOL(iwl_power_set_user_mode);
370
371
372 /* set system_power_setting. This should be set by over all
373  * PM application.
374  */
375 int iwl_power_set_system_mode(struct iwl_priv *priv, u16 mode)
376 {
377         int ret = 0;
378
379         if (mode > IWL_POWER_LIMIT)
380                 return -EINVAL;
381
382         priv->power_data.system_power_setting = mode;
383
384         ret = iwl_power_update_mode(priv, 0);
385
386         return ret;
387 }
388 EXPORT_SYMBOL(iwl_power_set_system_mode);
389
390 /* initilize to default */
391 void iwl_power_initialize(struct iwl_priv *priv)
392 {
393
394         iwl_power_init_handle(priv);
395         priv->power_data.user_power_setting = IWL_POWER_AUTO;
396         priv->power_data.power_disabled = 0;
397         priv->power_data.system_power_setting = IWL_POWER_AUTO;
398         priv->power_data.is_battery_active = 0;
399         priv->power_data.power_disabled = 0;
400         priv->power_data.critical_power_setting = 0;
401 }
402 EXPORT_SYMBOL(iwl_power_initialize);
403
404 /* set critical_power_setting according to temperature value */
405 int iwl_power_temperature_change(struct iwl_priv *priv)
406 {
407         int ret = 0;
408         u16 new_critical = priv->power_data.critical_power_setting;
409         s32 temperature = KELVIN_TO_CELSIUS(priv->last_temperature);
410
411         if (temperature > IWL_CT_KILL_TEMPERATURE)
412                 return 0;
413         else if (temperature > IWL_MIN_POWER_TEMPERATURE)
414                 new_critical = IWL_POWER_INDEX_5;
415         else if (temperature > IWL_REDUCED_POWER_TEMPERATURE)
416                 new_critical = IWL_POWER_INDEX_3;
417         else
418                 new_critical = IWL_POWER_MODE_CAM;
419
420         if (new_critical != priv->power_data.critical_power_setting)
421                 priv->power_data.critical_power_setting = new_critical;
422
423         if (priv->power_data.critical_power_setting >
424                                 priv->power_data.power_mode)
425                 ret = iwl_power_update_mode(priv, 0);
426
427         return ret;
428 }
429 EXPORT_SYMBOL(iwl_power_temperature_change);