power.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2007 - 2013 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. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/slab.h>
  31. #include <linux/init.h>
  32. #include <net/mac80211.h>
  33. #include "iwl-io.h"
  34. #include "iwl-debug.h"
  35. #include "iwl-trans.h"
  36. #include "iwl-modparams.h"
  37. #include "dev.h"
  38. #include "agn.h"
  39. #include "commands.h"
  40. #include "power.h"
  41. /*
  42. * Setting power level allows the card to go to sleep when not busy.
  43. *
  44. * We calculate a sleep command based on the required latency, which
  45. * we get from mac80211. In order to handle thermal throttling, we can
  46. * also use pre-defined power levels.
  47. */
  48. /*
  49. * This defines the old power levels. They are still used by default
  50. * (level 1) and for thermal throttle (levels 3 through 5)
  51. */
  52. struct iwl_power_vec_entry {
  53. struct iwl_powertable_cmd cmd;
  54. u8 no_dtim; /* number of skip dtim */
  55. };
  56. #define IWL_DTIM_RANGE_0_MAX 2
  57. #define IWL_DTIM_RANGE_1_MAX 10
  58. #define NOSLP cpu_to_le16(0), 0, 0
  59. #define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
  60. #define ASLP (IWL_POWER_POWER_SAVE_ENA_MSK | \
  61. IWL_POWER_POWER_MANAGEMENT_ENA_MSK | \
  62. IWL_POWER_ADVANCE_PM_ENA_MSK)
  63. #define ASLP_TOUT(T) cpu_to_le32(T)
  64. #define TU_TO_USEC 1024
  65. #define SLP_TOUT(T) cpu_to_le32((T) * TU_TO_USEC)
  66. #define SLP_VEC(X0, X1, X2, X3, X4) {cpu_to_le32(X0), \
  67. cpu_to_le32(X1), \
  68. cpu_to_le32(X2), \
  69. cpu_to_le32(X3), \
  70. cpu_to_le32(X4)}
  71. /* default power management (not Tx power) table values */
  72. /* for DTIM period 0 through IWL_DTIM_RANGE_0_MAX */
  73. /* DTIM 0 - 2 */
  74. static const struct iwl_power_vec_entry range_0[IWL_POWER_NUM] = {
  75. {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 1, 2, 2, 0xFF)}, 0},
  76. {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
  77. {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 2, 2, 2, 0xFF)}, 0},
  78. {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 2, 4, 4, 0xFF)}, 1},
  79. {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 2, 4, 6, 0xFF)}, 2}
  80. };
  81. /* for DTIM period IWL_DTIM_RANGE_0_MAX + 1 through IWL_DTIM_RANGE_1_MAX */
  82. /* DTIM 3 - 10 */
  83. static const struct iwl_power_vec_entry range_1[IWL_POWER_NUM] = {
  84. {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
  85. {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 3, 4, 7)}, 0},
  86. {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 4, 6, 7, 9)}, 0},
  87. {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 4, 6, 9, 10)}, 1},
  88. {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 4, 6, 10, 10)}, 2}
  89. };
  90. /* for DTIM period > IWL_DTIM_RANGE_1_MAX */
  91. /* DTIM 11 - */
  92. static const struct iwl_power_vec_entry range_2[IWL_POWER_NUM] = {
  93. {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
  94. {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
  95. {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
  96. {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
  97. {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
  98. };
  99. /* advance power management */
  100. /* DTIM 0 - 2 */
  101. static const struct iwl_power_vec_entry apm_range_0[IWL_POWER_NUM] = {
  102. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  103. SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
  104. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  105. SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
  106. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  107. SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
  108. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  109. SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
  110. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  111. SLP_VEC(1, 2, 6, 8, 0xFF), ASLP_TOUT(2)}, 2}
  112. };
  113. /* for DTIM period IWL_DTIM_RANGE_0_MAX + 1 through IWL_DTIM_RANGE_1_MAX */
  114. /* DTIM 3 - 10 */
  115. static const struct iwl_power_vec_entry apm_range_1[IWL_POWER_NUM] = {
  116. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  117. SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
  118. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  119. SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
  120. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  121. SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
  122. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  123. SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
  124. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  125. SLP_VEC(1, 2, 6, 8, 0xFF), 0}, 2}
  126. };
  127. /* for DTIM period > IWL_DTIM_RANGE_1_MAX */
  128. /* DTIM 11 - */
  129. static const struct iwl_power_vec_entry apm_range_2[IWL_POWER_NUM] = {
  130. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  131. SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
  132. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  133. SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
  134. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  135. SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
  136. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  137. SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
  138. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  139. SLP_VEC(1, 2, 6, 8, 0xFF), ASLP_TOUT(2)}, 2}
  140. };
  141. static void iwl_static_sleep_cmd(struct iwl_priv *priv,
  142. struct iwl_powertable_cmd *cmd,
  143. enum iwl_power_level lvl, int period)
  144. {
  145. const struct iwl_power_vec_entry *table;
  146. int max_sleep[IWL_POWER_VEC_SIZE] = { 0 };
  147. int i;
  148. u8 skip;
  149. u32 slp_itrvl;
  150. if (priv->cfg->adv_pm) {
  151. table = apm_range_2;
  152. if (period <= IWL_DTIM_RANGE_1_MAX)
  153. table = apm_range_1;
  154. if (period <= IWL_DTIM_RANGE_0_MAX)
  155. table = apm_range_0;
  156. } else {
  157. table = range_2;
  158. if (period <= IWL_DTIM_RANGE_1_MAX)
  159. table = range_1;
  160. if (period <= IWL_DTIM_RANGE_0_MAX)
  161. table = range_0;
  162. }
  163. if (WARN_ON(lvl < 0 || lvl >= IWL_POWER_NUM))
  164. memset(cmd, 0, sizeof(*cmd));
  165. else
  166. *cmd = table[lvl].cmd;
  167. if (period == 0) {
  168. skip = 0;
  169. period = 1;
  170. for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
  171. max_sleep[i] = 1;
  172. } else {
  173. skip = table[lvl].no_dtim;
  174. for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
  175. max_sleep[i] = le32_to_cpu(cmd->sleep_interval[i]);
  176. max_sleep[IWL_POWER_VEC_SIZE - 1] = skip + 1;
  177. }
  178. slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
  179. /* figure out the listen interval based on dtim period and skip */
  180. if (slp_itrvl == 0xFF)
  181. cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
  182. cpu_to_le32(period * (skip + 1));
  183. slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
  184. if (slp_itrvl > period)
  185. cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
  186. cpu_to_le32((slp_itrvl / period) * period);
  187. if (skip)
  188. cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
  189. else
  190. cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
  191. if (priv->cfg->base_params->shadow_reg_enable)
  192. cmd->flags |= IWL_POWER_SHADOW_REG_ENA;
  193. else
  194. cmd->flags &= ~IWL_POWER_SHADOW_REG_ENA;
  195. if (iwl_advanced_bt_coexist(priv)) {
  196. if (!priv->cfg->bt_params->bt_sco_disable)
  197. cmd->flags |= IWL_POWER_BT_SCO_ENA;
  198. else
  199. cmd->flags &= ~IWL_POWER_BT_SCO_ENA;
  200. }
  201. slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
  202. if (slp_itrvl > IWL_CONN_MAX_LISTEN_INTERVAL)
  203. cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
  204. cpu_to_le32(IWL_CONN_MAX_LISTEN_INTERVAL);
  205. /* enforce max sleep interval */
  206. for (i = IWL_POWER_VEC_SIZE - 1; i >= 0 ; i--) {
  207. if (le32_to_cpu(cmd->sleep_interval[i]) >
  208. (max_sleep[i] * period))
  209. cmd->sleep_interval[i] =
  210. cpu_to_le32(max_sleep[i] * period);
  211. if (i != (IWL_POWER_VEC_SIZE - 1)) {
  212. if (le32_to_cpu(cmd->sleep_interval[i]) >
  213. le32_to_cpu(cmd->sleep_interval[i+1]))
  214. cmd->sleep_interval[i] =
  215. cmd->sleep_interval[i+1];
  216. }
  217. }
  218. if (priv->power_data.bus_pm)
  219. cmd->flags |= IWL_POWER_PCI_PM_MSK;
  220. else
  221. cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
  222. IWL_DEBUG_POWER(priv, "numSkipDtim = %u, dtimPeriod = %d\n",
  223. skip, period);
  224. /* The power level here is 0-4 (used as array index), but user expects
  225. to see 1-5 (according to spec). */
  226. IWL_DEBUG_POWER(priv, "Sleep command for index %d\n", lvl + 1);
  227. }
  228. static void iwl_power_sleep_cam_cmd(struct iwl_priv *priv,
  229. struct iwl_powertable_cmd *cmd)
  230. {
  231. memset(cmd, 0, sizeof(*cmd));
  232. if (priv->power_data.bus_pm)
  233. cmd->flags |= IWL_POWER_PCI_PM_MSK;
  234. IWL_DEBUG_POWER(priv, "Sleep command for CAM\n");
  235. }
  236. static int iwl_set_power(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd)
  237. {
  238. IWL_DEBUG_POWER(priv, "Sending power/sleep command\n");
  239. IWL_DEBUG_POWER(priv, "Flags value = 0x%08X\n", cmd->flags);
  240. IWL_DEBUG_POWER(priv, "Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
  241. IWL_DEBUG_POWER(priv, "Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
  242. IWL_DEBUG_POWER(priv, "Sleep interval vector = { %d , %d , %d , %d , %d }\n",
  243. le32_to_cpu(cmd->sleep_interval[0]),
  244. le32_to_cpu(cmd->sleep_interval[1]),
  245. le32_to_cpu(cmd->sleep_interval[2]),
  246. le32_to_cpu(cmd->sleep_interval[3]),
  247. le32_to_cpu(cmd->sleep_interval[4]));
  248. return iwl_dvm_send_cmd_pdu(priv, POWER_TABLE_CMD, CMD_SYNC,
  249. sizeof(struct iwl_powertable_cmd), cmd);
  250. }
  251. static void iwl_power_build_cmd(struct iwl_priv *priv,
  252. struct iwl_powertable_cmd *cmd)
  253. {
  254. bool enabled = priv->hw->conf.flags & IEEE80211_CONF_PS;
  255. int dtimper;
  256. dtimper = priv->hw->conf.ps_dtim_period ?: 1;
  257. if (priv->wowlan)
  258. iwl_static_sleep_cmd(priv, cmd, IWL_POWER_INDEX_5, dtimper);
  259. else if (!priv->cfg->base_params->no_idle_support &&
  260. priv->hw->conf.flags & IEEE80211_CONF_IDLE)
  261. iwl_static_sleep_cmd(priv, cmd, IWL_POWER_INDEX_5, 20);
  262. else if (iwl_tt_is_low_power_state(priv)) {
  263. /* in thermal throttling low power state */
  264. iwl_static_sleep_cmd(priv, cmd,
  265. iwl_tt_current_power_mode(priv), dtimper);
  266. } else if (!enabled)
  267. iwl_power_sleep_cam_cmd(priv, cmd);
  268. else if (priv->power_data.debug_sleep_level_override >= 0)
  269. iwl_static_sleep_cmd(priv, cmd,
  270. priv->power_data.debug_sleep_level_override,
  271. dtimper);
  272. else {
  273. /* Note that the user parameter is 1-5 (according to spec),
  274. but we pass 0-4 because it acts as an array index. */
  275. if (iwlwifi_mod_params.power_level > IWL_POWER_INDEX_1 &&
  276. iwlwifi_mod_params.power_level <= IWL_POWER_NUM)
  277. iwl_static_sleep_cmd(priv, cmd,
  278. iwlwifi_mod_params.power_level - 1, dtimper);
  279. else
  280. iwl_static_sleep_cmd(priv, cmd,
  281. IWL_POWER_INDEX_1, dtimper);
  282. }
  283. }
  284. int iwl_power_set_mode(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd,
  285. bool force)
  286. {
  287. int ret;
  288. bool update_chains;
  289. lockdep_assert_held(&priv->mutex);
  290. /* Don't update the RX chain when chain noise calibration is running */
  291. update_chains = priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE ||
  292. priv->chain_noise_data.state == IWL_CHAIN_NOISE_ALIVE;
  293. if (!memcmp(&priv->power_data.sleep_cmd, cmd, sizeof(*cmd)) && !force)
  294. return 0;
  295. if (!iwl_is_ready_rf(priv))
  296. return -EIO;
  297. /* scan complete use sleep_power_next, need to be updated */
  298. memcpy(&priv->power_data.sleep_cmd_next, cmd, sizeof(*cmd));
  299. if (test_bit(STATUS_SCANNING, &priv->status) && !force) {
  300. IWL_DEBUG_INFO(priv, "Defer power set mode while scanning\n");
  301. return 0;
  302. }
  303. if (cmd->flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK)
  304. iwl_dvm_set_pmi(priv, true);
  305. ret = iwl_set_power(priv, cmd);
  306. if (!ret) {
  307. if (!(cmd->flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK))
  308. iwl_dvm_set_pmi(priv, false);
  309. if (update_chains)
  310. iwl_update_chain_flags(priv);
  311. else
  312. IWL_DEBUG_POWER(priv,
  313. "Cannot update the power, chain noise "
  314. "calibration running: %d\n",
  315. priv->chain_noise_data.state);
  316. memcpy(&priv->power_data.sleep_cmd, cmd, sizeof(*cmd));
  317. } else
  318. IWL_ERR(priv, "set power fail, ret = %d", ret);
  319. return ret;
  320. }
  321. int iwl_power_update_mode(struct iwl_priv *priv, bool force)
  322. {
  323. struct iwl_powertable_cmd cmd;
  324. iwl_power_build_cmd(priv, &cmd);
  325. return iwl_power_set_mode(priv, &cmd, force);
  326. }
  327. /* initialize to default */
  328. void iwl_power_initialize(struct iwl_priv *priv)
  329. {
  330. priv->power_data.bus_pm = priv->trans->pm_support;
  331. priv->power_data.debug_sleep_level_override = -1;
  332. memset(&priv->power_data.sleep_cmd, 0,
  333. sizeof(priv->power_data.sleep_cmd));
  334. }