Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Wey-Yi Guy | 901069c | 2011-04-05 09:42:00 -0700 | [diff] [blame] | 3 | * Copyright(c) 2007 - 2011 Intel Corporation. All rights reserved. |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 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: |
Winkler, Tomas | 759ef89 | 2008-12-09 11:28:58 -0800 | [diff] [blame] | 25 | * Intel Linux Wireless <ilw@linux.intel.com> |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 32 | #include <linux/slab.h> |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 33 | #include <linux/init.h> |
| 34 | |
| 35 | #include <net/mac80211.h> |
| 36 | |
| 37 | #include "iwl-eeprom.h" |
Tomas Winkler | 3e0d4cb | 2008-04-24 11:55:38 -0700 | [diff] [blame] | 38 | #include "iwl-dev.h" |
Wey-Yi Guy | 3f1e5f4 | 2011-06-03 13:52:40 -0700 | [diff] [blame] | 39 | #include "iwl-agn.h" |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 40 | #include "iwl-core.h" |
Wey-Yi Guy | 39b73fb1 | 2009-07-24 11:13:02 -0700 | [diff] [blame] | 41 | #include "iwl-io.h" |
Tomas Winkler | 5a36ba0 | 2008-04-24 11:55:37 -0700 | [diff] [blame] | 42 | #include "iwl-commands.h" |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 43 | #include "iwl-debug.h" |
| 44 | #include "iwl-power.h" |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 45 | |
| 46 | /* |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 47 | * Setting power level allows the card to go to sleep when not busy. |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 48 | * |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 49 | * We calculate a sleep command based on the required latency, which |
| 50 | * we get from mac80211. In order to handle thermal throttling, we can |
| 51 | * also use pre-defined power levels. |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 52 | */ |
| 53 | |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 54 | /* |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 55 | * This defines the old power levels. They are still used by default |
| 56 | * (level 1) and for thermal throttle (levels 3 through 5) |
| 57 | */ |
| 58 | |
| 59 | struct iwl_power_vec_entry { |
| 60 | struct iwl_powertable_cmd cmd; |
Wey-Yi Guy | 4ad177b | 2009-10-16 14:25:58 -0700 | [diff] [blame] | 61 | u8 no_dtim; /* number of skip dtim */ |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | #define IWL_DTIM_RANGE_0_MAX 2 |
| 65 | #define IWL_DTIM_RANGE_1_MAX 10 |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 66 | |
Johannes Berg | 7af2c46 | 2009-05-08 13:44:38 -0700 | [diff] [blame] | 67 | #define NOSLP cpu_to_le16(0), 0, 0 |
| 68 | #define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0 |
Wey-Yi Guy | 35162ba | 2010-11-23 10:58:56 -0800 | [diff] [blame] | 69 | #define ASLP (IWL_POWER_POWER_SAVE_ENA_MSK | \ |
| 70 | IWL_POWER_POWER_MANAGEMENT_ENA_MSK | \ |
| 71 | IWL_POWER_ADVANCE_PM_ENA_MSK) |
| 72 | #define ASLP_TOUT(T) cpu_to_le32(T) |
Johannes Berg | 7af2c46 | 2009-05-08 13:44:38 -0700 | [diff] [blame] | 73 | #define TU_TO_USEC 1024 |
| 74 | #define SLP_TOUT(T) cpu_to_le32((T) * TU_TO_USEC) |
| 75 | #define SLP_VEC(X0, X1, X2, X3, X4) {cpu_to_le32(X0), \ |
| 76 | cpu_to_le32(X1), \ |
| 77 | cpu_to_le32(X2), \ |
| 78 | cpu_to_le32(X3), \ |
| 79 | cpu_to_le32(X4)} |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 80 | /* default power management (not Tx power) table values */ |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 81 | /* for DTIM period 0 through IWL_DTIM_RANGE_0_MAX */ |
Wey-Yi Guy | 4ad177b | 2009-10-16 14:25:58 -0700 | [diff] [blame] | 82 | /* DTIM 0 - 2 */ |
Johannes Berg | 7af2c46 | 2009-05-08 13:44:38 -0700 | [diff] [blame] | 83 | static const struct iwl_power_vec_entry range_0[IWL_POWER_NUM] = { |
Wey-Yi Guy | 4ad177b | 2009-10-16 14:25:58 -0700 | [diff] [blame] | 84 | {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 1, 2, 2, 0xFF)}, 0}, |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 85 | {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0}, |
| 86 | {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 2, 2, 2, 0xFF)}, 0}, |
| 87 | {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 2, 4, 4, 0xFF)}, 1}, |
| 88 | {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 2, 4, 6, 0xFF)}, 2} |
| 89 | }; |
| 90 | |
| 91 | |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 92 | /* for DTIM period IWL_DTIM_RANGE_0_MAX + 1 through IWL_DTIM_RANGE_1_MAX */ |
Wey-Yi Guy | 4ad177b | 2009-10-16 14:25:58 -0700 | [diff] [blame] | 93 | /* DTIM 3 - 10 */ |
Johannes Berg | 7af2c46 | 2009-05-08 13:44:38 -0700 | [diff] [blame] | 94 | static const struct iwl_power_vec_entry range_1[IWL_POWER_NUM] = { |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 95 | {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0}, |
| 96 | {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 3, 4, 7)}, 0}, |
| 97 | {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 4, 6, 7, 9)}, 0}, |
| 98 | {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 4, 6, 9, 10)}, 1}, |
Wey-Yi Guy | 4ad177b | 2009-10-16 14:25:58 -0700 | [diff] [blame] | 99 | {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 4, 6, 10, 10)}, 2} |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 102 | /* for DTIM period > IWL_DTIM_RANGE_1_MAX */ |
Wey-Yi Guy | 4ad177b | 2009-10-16 14:25:58 -0700 | [diff] [blame] | 103 | /* DTIM 11 - */ |
Johannes Berg | 7af2c46 | 2009-05-08 13:44:38 -0700 | [diff] [blame] | 104 | static const struct iwl_power_vec_entry range_2[IWL_POWER_NUM] = { |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 105 | {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 0xFF)}, 0}, |
| 106 | {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 0xFF)}, 0}, |
| 107 | {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0}, |
| 108 | {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0}, |
| 109 | {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0} |
| 110 | }; |
| 111 | |
Wey-Yi Guy | 35162ba | 2010-11-23 10:58:56 -0800 | [diff] [blame] | 112 | /* advance power management */ |
| 113 | /* DTIM 0 - 2 */ |
| 114 | static const struct iwl_power_vec_entry apm_range_0[IWL_POWER_NUM] = { |
| 115 | {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50), |
| 116 | SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0}, |
| 117 | {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50), |
| 118 | SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0}, |
| 119 | {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50), |
| 120 | SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0}, |
| 121 | {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50), |
| 122 | SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0}, |
| 123 | {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50), |
| 124 | SLP_VEC(1, 2, 6, 8, 0xFF), ASLP_TOUT(2)}, 2} |
| 125 | }; |
| 126 | |
| 127 | |
| 128 | /* for DTIM period IWL_DTIM_RANGE_0_MAX + 1 through IWL_DTIM_RANGE_1_MAX */ |
| 129 | /* DTIM 3 - 10 */ |
| 130 | static const struct iwl_power_vec_entry apm_range_1[IWL_POWER_NUM] = { |
| 131 | {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50), |
| 132 | SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0}, |
| 133 | {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50), |
| 134 | SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0}, |
| 135 | {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50), |
| 136 | SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0}, |
| 137 | {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50), |
| 138 | SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0}, |
| 139 | {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50), |
| 140 | SLP_VEC(1, 2, 6, 8, 0xFF), 0}, 2} |
| 141 | }; |
| 142 | |
| 143 | /* for DTIM period > IWL_DTIM_RANGE_1_MAX */ |
| 144 | /* DTIM 11 - */ |
| 145 | static const struct iwl_power_vec_entry apm_range_2[IWL_POWER_NUM] = { |
| 146 | {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50), |
| 147 | SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0}, |
| 148 | {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50), |
| 149 | SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0}, |
| 150 | {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50), |
| 151 | SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0}, |
| 152 | {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50), |
| 153 | SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0}, |
| 154 | {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50), |
| 155 | SLP_VEC(1, 2, 6, 8, 0xFF), ASLP_TOUT(2)}, 2} |
| 156 | }; |
| 157 | |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 158 | static void iwl_static_sleep_cmd(struct iwl_priv *priv, |
| 159 | struct iwl_powertable_cmd *cmd, |
| 160 | enum iwl_power_level lvl, int period) |
| 161 | { |
| 162 | const struct iwl_power_vec_entry *table; |
Wey-Yi Guy | 4ad177b | 2009-10-16 14:25:58 -0700 | [diff] [blame] | 163 | int max_sleep[IWL_POWER_VEC_SIZE] = { 0 }; |
| 164 | int i; |
| 165 | u8 skip; |
| 166 | u32 slp_itrvl; |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 167 | |
Wey-Yi Guy | 35162ba | 2010-11-23 10:58:56 -0800 | [diff] [blame] | 168 | if (priv->cfg->adv_pm) { |
| 169 | table = apm_range_2; |
| 170 | if (period <= IWL_DTIM_RANGE_1_MAX) |
| 171 | table = apm_range_1; |
| 172 | if (period <= IWL_DTIM_RANGE_0_MAX) |
| 173 | table = apm_range_0; |
| 174 | } else { |
| 175 | table = range_2; |
| 176 | if (period <= IWL_DTIM_RANGE_1_MAX) |
| 177 | table = range_1; |
| 178 | if (period <= IWL_DTIM_RANGE_0_MAX) |
| 179 | table = range_0; |
| 180 | } |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 181 | |
Johannes Berg | 3e41ace | 2011-04-18 09:12:37 -0700 | [diff] [blame] | 182 | if (WARN_ON(lvl < 0 || lvl >= IWL_POWER_NUM)) |
| 183 | memset(cmd, 0, sizeof(*cmd)); |
| 184 | else |
| 185 | *cmd = table[lvl].cmd; |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 186 | |
| 187 | if (period == 0) { |
Wey-Yi Guy | 4ad177b | 2009-10-16 14:25:58 -0700 | [diff] [blame] | 188 | skip = 0; |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 189 | period = 1; |
Wey-Yi Guy | 4ad177b | 2009-10-16 14:25:58 -0700 | [diff] [blame] | 190 | for (i = 0; i < IWL_POWER_VEC_SIZE; i++) |
| 191 | max_sleep[i] = 1; |
| 192 | |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 193 | } else { |
Wey-Yi Guy | 4ad177b | 2009-10-16 14:25:58 -0700 | [diff] [blame] | 194 | skip = table[lvl].no_dtim; |
| 195 | for (i = 0; i < IWL_POWER_VEC_SIZE; i++) |
| 196 | max_sleep[i] = le32_to_cpu(cmd->sleep_interval[i]); |
| 197 | max_sleep[IWL_POWER_VEC_SIZE - 1] = skip + 1; |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Wey-Yi Guy | 4ad177b | 2009-10-16 14:25:58 -0700 | [diff] [blame] | 200 | slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]); |
| 201 | /* figure out the listen interval based on dtim period and skip */ |
| 202 | if (slp_itrvl == 0xFF) |
| 203 | cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] = |
| 204 | cpu_to_le32(period * (skip + 1)); |
| 205 | |
| 206 | slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]); |
| 207 | if (slp_itrvl > period) |
| 208 | cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] = |
| 209 | cpu_to_le32((slp_itrvl / period) * period); |
| 210 | |
| 211 | if (skip) |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 212 | cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK; |
Wey-Yi Guy | 4ad177b | 2009-10-16 14:25:58 -0700 | [diff] [blame] | 213 | else |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 214 | cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK; |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 215 | |
Wey-Yi Guy | 1f37daf | 2010-11-23 10:58:55 -0800 | [diff] [blame] | 216 | if (priv->cfg->base_params->shadow_reg_enable) |
| 217 | cmd->flags |= IWL_POWER_SHADOW_REG_ENA; |
| 218 | else |
| 219 | cmd->flags &= ~IWL_POWER_SHADOW_REG_ENA; |
| 220 | |
Stanislaw Gruszka | 88e58fc | 2011-01-28 16:47:47 +0100 | [diff] [blame] | 221 | if (iwl_advanced_bt_coexist(priv)) { |
Wey-Yi Guy | e366176 | 2010-11-23 10:58:54 -0800 | [diff] [blame] | 222 | if (!priv->cfg->bt_params->bt_sco_disable) |
| 223 | cmd->flags |= IWL_POWER_BT_SCO_ENA; |
| 224 | else |
| 225 | cmd->flags &= ~IWL_POWER_BT_SCO_ENA; |
| 226 | } |
| 227 | |
| 228 | |
Wey-Yi Guy | 4ad177b | 2009-10-16 14:25:58 -0700 | [diff] [blame] | 229 | slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]); |
Wey-Yi Guy | 570af86 | 2009-10-23 13:42:33 -0700 | [diff] [blame] | 230 | if (slp_itrvl > IWL_CONN_MAX_LISTEN_INTERVAL) |
Wey-Yi Guy | 4ad177b | 2009-10-16 14:25:58 -0700 | [diff] [blame] | 231 | cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] = |
Wey-Yi Guy | 570af86 | 2009-10-23 13:42:33 -0700 | [diff] [blame] | 232 | cpu_to_le32(IWL_CONN_MAX_LISTEN_INTERVAL); |
Wey-Yi Guy | 4ad177b | 2009-10-16 14:25:58 -0700 | [diff] [blame] | 233 | |
| 234 | /* enforce max sleep interval */ |
| 235 | for (i = IWL_POWER_VEC_SIZE - 1; i >= 0 ; i--) { |
| 236 | if (le32_to_cpu(cmd->sleep_interval[i]) > |
| 237 | (max_sleep[i] * period)) |
| 238 | cmd->sleep_interval[i] = |
| 239 | cpu_to_le32(max_sleep[i] * period); |
| 240 | if (i != (IWL_POWER_VEC_SIZE - 1)) { |
| 241 | if (le32_to_cpu(cmd->sleep_interval[i]) > |
| 242 | le32_to_cpu(cmd->sleep_interval[i+1])) |
| 243 | cmd->sleep_interval[i] = |
| 244 | cmd->sleep_interval[i+1]; |
| 245 | } |
| 246 | } |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 247 | |
Wey-Yi Guy | d57fa99 | 2011-06-10 11:23:36 -0700 | [diff] [blame] | 248 | if (priv->power_data.bus_pm) |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 249 | cmd->flags |= IWL_POWER_PCI_PM_MSK; |
| 250 | else |
| 251 | cmd->flags &= ~IWL_POWER_PCI_PM_MSK; |
| 252 | |
Wey-Yi Guy | 4ad177b | 2009-10-16 14:25:58 -0700 | [diff] [blame] | 253 | IWL_DEBUG_POWER(priv, "numSkipDtim = %u, dtimPeriod = %d\n", |
| 254 | skip, period); |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 255 | IWL_DEBUG_POWER(priv, "Sleep command for index %d\n", lvl + 1); |
| 256 | } |
| 257 | |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 258 | static void iwl_power_sleep_cam_cmd(struct iwl_priv *priv, |
| 259 | struct iwl_powertable_cmd *cmd) |
Mohamed Abbas | ca57961 | 2008-07-18 13:52:57 +0800 | [diff] [blame] | 260 | { |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 261 | memset(cmd, 0, sizeof(*cmd)); |
| 262 | |
Wey-Yi Guy | d57fa99 | 2011-06-10 11:23:36 -0700 | [diff] [blame] | 263 | if (priv->power_data.bus_pm) |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 264 | cmd->flags |= IWL_POWER_PCI_PM_MSK; |
| 265 | |
| 266 | IWL_DEBUG_POWER(priv, "Sleep command for CAM\n"); |
Mohamed Abbas | ca57961 | 2008-07-18 13:52:57 +0800 | [diff] [blame] | 267 | } |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 268 | |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 269 | static void iwl_power_fill_sleep_cmd(struct iwl_priv *priv, |
| 270 | struct iwl_powertable_cmd *cmd, |
| 271 | int dynps_ms, int wakeup_period) |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 272 | { |
Johannes Berg | 4c561a0 | 2009-08-28 09:44:48 -0700 | [diff] [blame] | 273 | /* |
| 274 | * These are the original power level 3 sleep successions. The |
| 275 | * device may behave better with such succession and was also |
| 276 | * only tested with that. Just like the original sleep commands, |
| 277 | * also adjust the succession here to the wakeup_period below. |
| 278 | * The ranges are the same as for the sleep commands, 0-2, 3-9 |
| 279 | * and >10, which is selected based on the DTIM interval for |
| 280 | * the sleep index but here we use the wakeup period since that |
| 281 | * is what we need to do for the latency requirements. |
| 282 | */ |
| 283 | static const u8 slp_succ_r0[IWL_POWER_VEC_SIZE] = { 2, 2, 2, 2, 2 }; |
| 284 | static const u8 slp_succ_r1[IWL_POWER_VEC_SIZE] = { 2, 4, 6, 7, 9 }; |
| 285 | static const u8 slp_succ_r2[IWL_POWER_VEC_SIZE] = { 2, 7, 9, 9, 0xFF }; |
| 286 | const u8 *slp_succ = slp_succ_r0; |
Tomas Winkler | e7b6358 | 2008-09-03 11:26:49 +0800 | [diff] [blame] | 287 | int i; |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 288 | |
Johannes Berg | 4c561a0 | 2009-08-28 09:44:48 -0700 | [diff] [blame] | 289 | if (wakeup_period > IWL_DTIM_RANGE_0_MAX) |
| 290 | slp_succ = slp_succ_r1; |
| 291 | if (wakeup_period > IWL_DTIM_RANGE_1_MAX) |
| 292 | slp_succ = slp_succ_r2; |
| 293 | |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 294 | memset(cmd, 0, sizeof(*cmd)); |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 295 | |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 296 | cmd->flags = IWL_POWER_DRIVER_ALLOW_SLEEP_MSK | |
| 297 | IWL_POWER_FAST_PD; /* no use seeing frames for others */ |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 298 | |
Wey-Yi Guy | d57fa99 | 2011-06-10 11:23:36 -0700 | [diff] [blame] | 299 | if (priv->power_data.bus_pm) |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 300 | cmd->flags |= IWL_POWER_PCI_PM_MSK; |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 301 | |
Wey-Yi Guy | 1f37daf | 2010-11-23 10:58:55 -0800 | [diff] [blame] | 302 | if (priv->cfg->base_params->shadow_reg_enable) |
| 303 | cmd->flags |= IWL_POWER_SHADOW_REG_ENA; |
| 304 | else |
| 305 | cmd->flags &= ~IWL_POWER_SHADOW_REG_ENA; |
| 306 | |
Stanislaw Gruszka | 88e58fc | 2011-01-28 16:47:47 +0100 | [diff] [blame] | 307 | if (iwl_advanced_bt_coexist(priv)) { |
Wey-Yi Guy | e366176 | 2010-11-23 10:58:54 -0800 | [diff] [blame] | 308 | if (!priv->cfg->bt_params->bt_sco_disable) |
| 309 | cmd->flags |= IWL_POWER_BT_SCO_ENA; |
| 310 | else |
| 311 | cmd->flags &= ~IWL_POWER_BT_SCO_ENA; |
| 312 | } |
| 313 | |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 314 | cmd->rx_data_timeout = cpu_to_le32(1000 * dynps_ms); |
| 315 | cmd->tx_data_timeout = cpu_to_le32(1000 * dynps_ms); |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 316 | |
Winkler, Tomas | 5cd19c5 | 2009-01-19 15:30:21 -0800 | [diff] [blame] | 317 | for (i = 0; i < IWL_POWER_VEC_SIZE; i++) |
Johannes Berg | 4c561a0 | 2009-08-28 09:44:48 -0700 | [diff] [blame] | 318 | cmd->sleep_interval[i] = |
| 319 | cpu_to_le32(min_t(int, slp_succ[i], wakeup_period)); |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 320 | |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 321 | IWL_DEBUG_POWER(priv, "Automatic sleep command\n"); |
| 322 | } |
| 323 | |
| 324 | static int iwl_set_power(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd) |
| 325 | { |
| 326 | IWL_DEBUG_POWER(priv, "Sending power/sleep command\n"); |
Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 327 | IWL_DEBUG_POWER(priv, "Flags value = 0x%08X\n", cmd->flags); |
| 328 | IWL_DEBUG_POWER(priv, "Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout)); |
| 329 | IWL_DEBUG_POWER(priv, "Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout)); |
| 330 | IWL_DEBUG_POWER(priv, "Sleep interval vector = { %d , %d , %d , %d , %d }\n", |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 331 | le32_to_cpu(cmd->sleep_interval[0]), |
| 332 | le32_to_cpu(cmd->sleep_interval[1]), |
| 333 | le32_to_cpu(cmd->sleep_interval[2]), |
| 334 | le32_to_cpu(cmd->sleep_interval[3]), |
| 335 | le32_to_cpu(cmd->sleep_interval[4])); |
| 336 | |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 337 | return iwl_send_cmd_pdu(priv, POWER_TABLE_CMD, |
| 338 | sizeof(struct iwl_powertable_cmd), cmd); |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Stanislaw Gruszka | ac4f545 | 2010-10-22 17:04:29 +0200 | [diff] [blame] | 341 | static void iwl_power_build_cmd(struct iwl_priv *priv, |
| 342 | struct iwl_powertable_cmd *cmd) |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 343 | { |
Johannes Berg | 4d69592 | 2010-01-21 05:24:04 -0800 | [diff] [blame] | 344 | bool enabled = priv->hw->conf.flags & IEEE80211_CONF_PS; |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 345 | int dtimper; |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 346 | |
Stanislaw Gruszka | ac4f545 | 2010-10-22 17:04:29 +0200 | [diff] [blame] | 347 | dtimper = priv->hw->conf.ps_dtim_period ?: 1; |
| 348 | |
Wey-Yi Guy | 23c0fcc | 2011-04-01 16:29:53 -0700 | [diff] [blame] | 349 | if (priv->hw->conf.flags & IEEE80211_CONF_IDLE) |
Stanislaw Gruszka | ac4f545 | 2010-10-22 17:04:29 +0200 | [diff] [blame] | 350 | iwl_static_sleep_cmd(priv, cmd, IWL_POWER_INDEX_5, 20); |
Wey-Yi Guy | f42e766 | 2011-04-18 09:30:09 -0700 | [diff] [blame] | 351 | else if (iwl_tt_is_low_power_state(priv)) { |
Stanislaw Gruszka | ac4f545 | 2010-10-22 17:04:29 +0200 | [diff] [blame] | 352 | /* in thermal throttling low power state */ |
| 353 | iwl_static_sleep_cmd(priv, cmd, |
Wey-Yi Guy | f42e766 | 2011-04-18 09:30:09 -0700 | [diff] [blame] | 354 | iwl_tt_current_power_mode(priv), dtimper); |
Stanislaw Gruszka | ac4f545 | 2010-10-22 17:04:29 +0200 | [diff] [blame] | 355 | } else if (!enabled) |
| 356 | iwl_power_sleep_cam_cmd(priv, cmd); |
| 357 | else if (priv->power_data.debug_sleep_level_override >= 0) |
| 358 | iwl_static_sleep_cmd(priv, cmd, |
| 359 | priv->power_data.debug_sleep_level_override, |
| 360 | dtimper); |
Wey-Yi Guy | 3f1e5f4 | 2011-06-03 13:52:40 -0700 | [diff] [blame] | 361 | else if (iwlagn_mod_params.no_sleep_autoadjust) |
Stanislaw Gruszka | ac4f545 | 2010-10-22 17:04:29 +0200 | [diff] [blame] | 362 | iwl_static_sleep_cmd(priv, cmd, IWL_POWER_INDEX_1, dtimper); |
| 363 | else |
| 364 | iwl_power_fill_sleep_cmd(priv, cmd, |
| 365 | priv->hw->conf.dynamic_ps_timeout, |
| 366 | priv->hw->conf.max_sleep_period); |
| 367 | } |
| 368 | |
| 369 | int iwl_power_set_mode(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd, |
| 370 | bool force) |
| 371 | { |
| 372 | int ret; |
| 373 | bool update_chains; |
| 374 | |
| 375 | lockdep_assert_held(&priv->mutex); |
| 376 | |
Grumbach, Emmanuel | 0481644 | 2008-09-03 11:26:53 +0800 | [diff] [blame] | 377 | /* Don't update the RX chain when chain noise calibration is running */ |
Winkler, Tomas | a71c8f6 | 2008-11-07 09:58:37 -0800 | [diff] [blame] | 378 | update_chains = priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE || |
| 379 | priv->chain_noise_data.state == IWL_CHAIN_NOISE_ALIVE; |
Grumbach, Emmanuel | 0481644 | 2008-09-03 11:26:53 +0800 | [diff] [blame] | 380 | |
Stanislaw Gruszka | ac4f545 | 2010-10-22 17:04:29 +0200 | [diff] [blame] | 381 | if (!memcmp(&priv->power_data.sleep_cmd, cmd, sizeof(*cmd)) && !force) |
| 382 | return 0; |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 383 | |
Stanislaw Gruszka | ac4f545 | 2010-10-22 17:04:29 +0200 | [diff] [blame] | 384 | if (!iwl_is_ready_rf(priv)) |
| 385 | return -EIO; |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 386 | |
Stanislaw Gruszka | ac4f545 | 2010-10-22 17:04:29 +0200 | [diff] [blame] | 387 | /* scan complete use sleep_power_next, need to be updated */ |
| 388 | memcpy(&priv->power_data.sleep_cmd_next, cmd, sizeof(*cmd)); |
| 389 | if (test_bit(STATUS_SCANNING, &priv->status) && !force) { |
| 390 | IWL_DEBUG_INFO(priv, "Defer power set mode while scanning\n"); |
| 391 | return 0; |
| 392 | } |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 393 | |
Stanislaw Gruszka | ac4f545 | 2010-10-22 17:04:29 +0200 | [diff] [blame] | 394 | if (cmd->flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK) |
| 395 | set_bit(STATUS_POWER_PMI, &priv->status); |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 396 | |
Stanislaw Gruszka | ac4f545 | 2010-10-22 17:04:29 +0200 | [diff] [blame] | 397 | ret = iwl_set_power(priv, cmd); |
| 398 | if (!ret) { |
| 399 | if (!(cmd->flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK)) |
| 400 | clear_bit(STATUS_POWER_PMI, &priv->status); |
| 401 | |
| 402 | if (priv->cfg->ops->lib->update_chain_flags && update_chains) |
| 403 | priv->cfg->ops->lib->update_chain_flags(priv); |
| 404 | else if (priv->cfg->ops->lib->update_chain_flags) |
| 405 | IWL_DEBUG_POWER(priv, |
Wey-Yi Guy | 3a780d2 | 2009-08-07 15:41:47 -0700 | [diff] [blame] | 406 | "Cannot update the power, chain noise " |
Winkler, Tomas | a71c8f6 | 2008-11-07 09:58:37 -0800 | [diff] [blame] | 407 | "calibration running: %d\n", |
| 408 | priv->chain_noise_data.state); |
Stanislaw Gruszka | ac4f545 | 2010-10-22 17:04:29 +0200 | [diff] [blame] | 409 | |
| 410 | memcpy(&priv->power_data.sleep_cmd, cmd, sizeof(*cmd)); |
| 411 | } else |
| 412 | IWL_ERR(priv, "set power fail, ret = %d", ret); |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 413 | |
| 414 | return ret; |
| 415 | } |
Stanislaw Gruszka | ac4f545 | 2010-10-22 17:04:29 +0200 | [diff] [blame] | 416 | |
| 417 | int iwl_power_update_mode(struct iwl_priv *priv, bool force) |
| 418 | { |
| 419 | struct iwl_powertable_cmd cmd; |
| 420 | |
| 421 | iwl_power_build_cmd(priv, &cmd); |
| 422 | return iwl_power_set_mode(priv, &cmd, force); |
| 423 | } |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 424 | |
Tomas Winkler | a96a27f | 2008-10-23 23:48:56 -0700 | [diff] [blame] | 425 | /* initialize to default */ |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 426 | void iwl_power_initialize(struct iwl_priv *priv) |
| 427 | { |
Wey-Yi Guy | d57fa99 | 2011-06-10 11:23:36 -0700 | [diff] [blame] | 428 | priv->power_data.bus_pm = priv->bus.ops->get_pm_support(&priv->bus); |
Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 429 | |
| 430 | priv->power_data.debug_sleep_level_override = -1; |
| 431 | |
| 432 | memset(&priv->power_data.sleep_cmd, 0, |
| 433 | sizeof(priv->power_data.sleep_cmd)); |
Mohamed Abbas | 5da4b55 | 2008-04-21 15:41:51 -0700 | [diff] [blame] | 434 | } |