iwlwifi: differentiate 4965 and 5000 hw ampdu queues number
[linux-2.6] / 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 /* decide the right power level according to association status
116  * and battery status
117  */
118 static u16 iwl_get_auto_power_mode(struct iwl_priv *priv)
119 {
120         u16 mode = priv->power_data.user_power_setting;
121
122         switch (priv->power_data.user_power_setting) {
123         case IWL_POWER_AUTO:
124                 /* if running on battery */
125                 if (priv->power_data.is_battery_active)
126                         mode = IWL_POWER_ON_BATTERY;
127                 else if (iwl_is_associated(priv))
128                         mode = IWL_POWER_ON_AC_ASSOC;
129                 else
130                         mode = IWL_POWER_ON_AC_DISASSOC;
131                 break;
132         case IWL_POWER_BATTERY:
133                 mode = IWL_POWER_INDEX_3;
134                 break;
135         case IWL_POWER_AC:
136                 mode = IWL_POWER_MODE_CAM;
137                 break;
138         }
139         return mode;
140 }
141
142 /* initialize to default */
143 static int iwl_power_init_handle(struct iwl_priv *priv)
144 {
145         int ret = 0, i;
146         struct iwl_power_mgr *pow_data;
147         int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_AC;
148         u16 pci_pm;
149
150         IWL_DEBUG_POWER("Initialize power \n");
151
152         pow_data = &(priv->power_data);
153
154         memset(pow_data, 0, sizeof(*pow_data));
155
156         memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
157         memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
158         memcpy(&pow_data->pwr_range_2[0], &range_2[0], size);
159
160         ret = pci_read_config_word(priv->pci_dev,
161                                   PCI_LINK_CTRL, &pci_pm);
162         if (ret != 0)
163                 return 0;
164         else {
165                 struct iwl4965_powertable_cmd *cmd;
166
167                 IWL_DEBUG_POWER("adjust power command flags\n");
168
169                 for (i = 0; i < IWL_POWER_AC; i++) {
170                         cmd = &pow_data->pwr_range_0[i].cmd;
171
172                         if (pci_pm & 0x1)
173                                 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
174                         else
175                                 cmd->flags |= IWL_POWER_PCI_PM_MSK;
176                 }
177         }
178         return ret;
179 }
180
181 /* adjust power command according to dtim period and power level*/
182 static int iwl_update_power_command(struct iwl_priv *priv,
183                                     struct iwl4965_powertable_cmd *cmd,
184                                     u16 mode)
185 {
186         int ret = 0, i;
187         u8 skip;
188         u32 max_sleep = 0;
189         struct iwl_power_vec_entry *range;
190         u8 period = 0;
191         struct iwl_power_mgr *pow_data;
192
193         if (mode > IWL_POWER_INDEX_5) {
194                 IWL_DEBUG_POWER("Error invalid power mode \n");
195                 return -1;
196         }
197         pow_data = &(priv->power_data);
198
199         if (pow_data->dtim_period <= IWL_POWER_RANGE_0_MAX)
200                 range = &pow_data->pwr_range_0[0];
201         else if (pow_data->dtim_period <= IWL_POWER_RANGE_1_MAX)
202                 range = &pow_data->pwr_range_1[0];
203         else
204                 range = &pow_data->pwr_range_2[0];
205
206         period = pow_data->dtim_period;
207         memcpy(cmd, &range[mode].cmd, sizeof(struct iwl4965_powertable_cmd));
208
209         if (period == 0) {
210                 period = 1;
211                 skip = 0;
212         } else
213                 skip = range[mode].no_dtim;
214
215         if (skip == 0) {
216                 max_sleep = period;
217                 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
218         } else {
219                 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
220                 max_sleep = le32_to_cpu(slp_itrvl);
221                 if (max_sleep == 0xFF)
222                         max_sleep = period * (skip + 1);
223                 else if (max_sleep >  period)
224                         max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
225                 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
226         }
227
228         for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
229                 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
230                         cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
231         }
232
233         IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
234         IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
235         IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
236         IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
237                         le32_to_cpu(cmd->sleep_interval[0]),
238                         le32_to_cpu(cmd->sleep_interval[1]),
239                         le32_to_cpu(cmd->sleep_interval[2]),
240                         le32_to_cpu(cmd->sleep_interval[3]),
241                         le32_to_cpu(cmd->sleep_interval[4]));
242
243         return ret;
244 }
245
246
247 /*
248  * calucaute the final power mode index
249  */
250 int iwl_power_update_mode(struct iwl_priv *priv, u8 refresh)
251 {
252         struct iwl_power_mgr *setting = &(priv->power_data);
253         int ret = 0;
254         u16 uninitialized_var(final_mode);
255
256        /* If on battery, set to 3,
257         * if plugged into AC power, set to CAM ("continuously aware mode"),
258         * else user level */
259
260         switch (setting->system_power_setting) {
261         case IWL_POWER_AUTO:
262                 final_mode = iwl_get_auto_power_mode(priv);
263                 break;
264         case IWL_POWER_BATTERY:
265                 final_mode = IWL_POWER_INDEX_3;
266                 break;
267         case IWL_POWER_AC:
268                 final_mode = IWL_POWER_MODE_CAM;
269                 break;
270         default:
271                 final_mode = setting->system_power_setting;
272         }
273
274         if (setting->critical_power_setting > final_mode)
275                 final_mode = setting->critical_power_setting;
276
277         /* driver only support CAM for non STA network */
278         if (priv->iw_mode != IEEE80211_IF_TYPE_STA)
279                 final_mode = IWL_POWER_MODE_CAM;
280
281         if (!iwl_is_rfkill(priv) && !setting->power_disabled &&
282             ((setting->power_mode != final_mode) || refresh)) {
283                 struct iwl4965_powertable_cmd cmd;
284
285                 if (final_mode != IWL_POWER_MODE_CAM)
286                         set_bit(STATUS_POWER_PMI, &priv->status);
287
288                 iwl_update_power_command(priv, &cmd, final_mode);
289                 cmd.keep_alive_beacons = 0;
290
291                 if (final_mode == IWL_POWER_INDEX_5)
292                         cmd.flags |= IWL_POWER_FAST_PD;
293
294                 if (priv->cfg->ops->lib->set_power)
295                         ret = priv->cfg->ops->lib->set_power(priv, &cmd);
296
297                 if (final_mode == IWL_POWER_MODE_CAM)
298                         clear_bit(STATUS_POWER_PMI, &priv->status);
299                 else
300                         set_bit(STATUS_POWER_PMI, &priv->status);
301
302                 if (priv->cfg->ops->lib->update_chain_flags)
303                         priv->cfg->ops->lib->update_chain_flags(priv);
304
305                 if (!ret)
306                         setting->power_mode = final_mode;
307         }
308
309         return ret;
310 }
311 EXPORT_SYMBOL(iwl_power_update_mode);
312
313 /* Allow other iwl code to disable/enable power management active
314  * this will be usefull for rate scale to disable PM during heavy
315  * Tx/Rx activities
316  */
317 int iwl_power_disable_management(struct iwl_priv *priv)
318 {
319         u16 prev_mode;
320         int ret = 0;
321
322         if (priv->power_data.power_disabled)
323                 return -EBUSY;
324
325         prev_mode = priv->power_data.user_power_setting;
326         priv->power_data.user_power_setting = IWL_POWER_MODE_CAM;
327         ret = iwl_power_update_mode(priv, 0);
328         priv->power_data.power_disabled = 1;
329         priv->power_data.user_power_setting = prev_mode;
330
331         return ret;
332 }
333 EXPORT_SYMBOL(iwl_power_disable_management);
334
335 /* Allow other iwl code to disable/enable power management active
336  * this will be usefull for rate scale to disable PM during hight
337  * valume activities
338  */
339 int iwl_power_enable_management(struct iwl_priv *priv)
340 {
341         int ret = 0;
342
343         priv->power_data.power_disabled = 0;
344         ret = iwl_power_update_mode(priv, 0);
345         return ret;
346 }
347 EXPORT_SYMBOL(iwl_power_enable_management);
348
349 /* set user_power_setting */
350 int iwl_power_set_user_mode(struct iwl_priv *priv, u16 mode)
351 {
352         int ret = 0;
353
354         if (mode > IWL_POWER_LIMIT)
355                 return -EINVAL;
356
357         priv->power_data.user_power_setting = mode;
358
359         ret = iwl_power_update_mode(priv, 0);
360
361         return ret;
362 }
363 EXPORT_SYMBOL(iwl_power_set_user_mode);
364
365
366 /* set system_power_setting. This should be set by over all
367  * PM application.
368  */
369 int iwl_power_set_system_mode(struct iwl_priv *priv, u16 mode)
370 {
371         int ret = 0;
372
373         if (mode > IWL_POWER_LIMIT)
374                 return -EINVAL;
375
376         priv->power_data.system_power_setting = mode;
377
378         ret = iwl_power_update_mode(priv, 0);
379
380         return ret;
381 }
382 EXPORT_SYMBOL(iwl_power_set_system_mode);
383
384 /* initilize to default */
385 void iwl_power_initialize(struct iwl_priv *priv)
386 {
387
388         iwl_power_init_handle(priv);
389         priv->power_data.user_power_setting = IWL_POWER_AUTO;
390         priv->power_data.power_disabled = 0;
391         priv->power_data.system_power_setting = IWL_POWER_AUTO;
392         priv->power_data.is_battery_active = 0;
393         priv->power_data.power_disabled = 0;
394         priv->power_data.critical_power_setting = 0;
395 }
396 EXPORT_SYMBOL(iwl_power_initialize);
397
398 /* set critical_power_setting according to temperature value */
399 int iwl_power_temperature_change(struct iwl_priv *priv)
400 {
401         int ret = 0;
402         u16 new_critical = priv->power_data.critical_power_setting;
403         s32 temperature = KELVIN_TO_CELSIUS(priv->last_temperature);
404
405         if (temperature > IWL_CT_KILL_TEMPERATURE)
406                 return 0;
407         else if (temperature > IWL_MIN_POWER_TEMPERATURE)
408                 new_critical = IWL_POWER_INDEX_5;
409         else if (temperature > IWL_REDUCED_POWER_TEMPERATURE)
410                 new_critical = IWL_POWER_INDEX_3;
411         else
412                 new_critical = IWL_POWER_MODE_CAM;
413
414         if (new_critical != priv->power_data.critical_power_setting)
415                 priv->power_data.critical_power_setting = new_critical;
416
417         if (priv->power_data.critical_power_setting >
418                                 priv->power_data.power_mode)
419                 ret = iwl_power_update_mode(priv, 0);
420
421         return ret;
422 }
423 EXPORT_SYMBOL(iwl_power_temperature_change);