iwl-power.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2007 - 2011 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-eeprom.h"
  34. #include "iwl-dev.h"
  35. #include "iwl-core.h"
  36. #include "iwl-io.h"
  37. #include "iwl-commands.h"
  38. #include "iwl-debug.h"
  39. #include "iwl-power.h"
  40. /*
  41. * Setting power level allows the card to go to sleep when not busy.
  42. *
  43. * We calculate a sleep command based on the required latency, which
  44. * we get from mac80211. In order to handle thermal throttling, we can
  45. * also use pre-defined power levels.
  46. */
  47. /*
  48. * For now, keep using power level 1 instead of automatically
  49. * adjusting ...
  50. */
  51. bool no_sleep_autoadjust = true;
  52. module_param(no_sleep_autoadjust, bool, S_IRUGO);
  53. MODULE_PARM_DESC(no_sleep_autoadjust,
  54. "don't automatically adjust sleep level "
  55. "according to maximum network latency");
  56. /*
  57. * This defines the old power levels. They are still used by default
  58. * (level 1) and for thermal throttle (levels 3 through 5)
  59. */
  60. struct iwl_power_vec_entry {
  61. struct iwl_powertable_cmd cmd;
  62. u8 no_dtim; /* number of skip dtim */
  63. };
  64. #define IWL_DTIM_RANGE_0_MAX 2
  65. #define IWL_DTIM_RANGE_1_MAX 10
  66. #define NOSLP cpu_to_le16(0), 0, 0
  67. #define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
  68. #define ASLP (IWL_POWER_POWER_SAVE_ENA_MSK | \
  69. IWL_POWER_POWER_MANAGEMENT_ENA_MSK | \
  70. IWL_POWER_ADVANCE_PM_ENA_MSK)
  71. #define ASLP_TOUT(T) cpu_to_le32(T)
  72. #define TU_TO_USEC 1024
  73. #define SLP_TOUT(T) cpu_to_le32((T) * TU_TO_USEC)
  74. #define SLP_VEC(X0, X1, X2, X3, X4) {cpu_to_le32(X0), \
  75. cpu_to_le32(X1), \
  76. cpu_to_le32(X2), \
  77. cpu_to_le32(X3), \
  78. cpu_to_le32(X4)}
  79. /* default power management (not Tx power) table values */
  80. /* for DTIM period 0 through IWL_DTIM_RANGE_0_MAX */
  81. /* DTIM 0 - 2 */
  82. static const struct iwl_power_vec_entry range_0[IWL_POWER_NUM] = {
  83. {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 1, 2, 2, 0xFF)}, 0},
  84. {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
  85. {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 2, 2, 2, 0xFF)}, 0},
  86. {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 2, 4, 4, 0xFF)}, 1},
  87. {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 2, 4, 6, 0xFF)}, 2}
  88. };
  89. /* for DTIM period IWL_DTIM_RANGE_0_MAX + 1 through IWL_DTIM_RANGE_1_MAX */
  90. /* DTIM 3 - 10 */
  91. static const struct iwl_power_vec_entry range_1[IWL_POWER_NUM] = {
  92. {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
  93. {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 3, 4, 7)}, 0},
  94. {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 4, 6, 7, 9)}, 0},
  95. {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 4, 6, 9, 10)}, 1},
  96. {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 4, 6, 10, 10)}, 2}
  97. };
  98. /* for DTIM period > IWL_DTIM_RANGE_1_MAX */
  99. /* DTIM 11 - */
  100. static const struct iwl_power_vec_entry range_2[IWL_POWER_NUM] = {
  101. {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
  102. {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
  103. {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
  104. {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
  105. {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
  106. };
  107. /* advance power management */
  108. /* DTIM 0 - 2 */
  109. static const struct iwl_power_vec_entry apm_range_0[IWL_POWER_NUM] = {
  110. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  111. SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
  112. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  113. SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
  114. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  115. SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
  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, 6, 8, 0xFF), ASLP_TOUT(2)}, 2}
  120. };
  121. /* for DTIM period IWL_DTIM_RANGE_0_MAX + 1 through IWL_DTIM_RANGE_1_MAX */
  122. /* DTIM 3 - 10 */
  123. static const struct iwl_power_vec_entry apm_range_1[IWL_POWER_NUM] = {
  124. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  125. SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
  126. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  127. SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
  128. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  129. SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
  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, 6, 8, 0xFF), 0}, 2}
  134. };
  135. /* for DTIM period > IWL_DTIM_RANGE_1_MAX */
  136. /* DTIM 11 - */
  137. static const struct iwl_power_vec_entry apm_range_2[IWL_POWER_NUM] = {
  138. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  139. SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
  140. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  141. SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
  142. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  143. SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
  144. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  145. SLP_VEC(1, 2, 4, 6, 0xFF), 0}, 0},
  146. {{ASLP, 0, 0, ASLP_TOUT(50), ASLP_TOUT(50),
  147. SLP_VEC(1, 2, 6, 8, 0xFF), ASLP_TOUT(2)}, 2}
  148. };
  149. static void iwl_static_sleep_cmd(struct iwl_priv *priv,
  150. struct iwl_powertable_cmd *cmd,
  151. enum iwl_power_level lvl, int period)
  152. {
  153. const struct iwl_power_vec_entry *table;
  154. int max_sleep[IWL_POWER_VEC_SIZE] = { 0 };
  155. int i;
  156. u8 skip;
  157. u32 slp_itrvl;
  158. if (priv->cfg->adv_pm) {
  159. table = apm_range_2;
  160. if (period <= IWL_DTIM_RANGE_1_MAX)
  161. table = apm_range_1;
  162. if (period <= IWL_DTIM_RANGE_0_MAX)
  163. table = apm_range_0;
  164. } else {
  165. table = range_2;
  166. if (period <= IWL_DTIM_RANGE_1_MAX)
  167. table = range_1;
  168. if (period <= IWL_DTIM_RANGE_0_MAX)
  169. table = range_0;
  170. }
  171. if (WARN_ON(lvl < 0 || lvl >= IWL_POWER_NUM))
  172. memset(cmd, 0, sizeof(*cmd));
  173. else
  174. *cmd = table[lvl].cmd;
  175. if (period == 0) {
  176. skip = 0;
  177. period = 1;
  178. for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
  179. max_sleep[i] = 1;
  180. } else {
  181. skip = table[lvl].no_dtim;
  182. for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
  183. max_sleep[i] = le32_to_cpu(cmd->sleep_interval[i]);
  184. max_sleep[IWL_POWER_VEC_SIZE - 1] = skip + 1;
  185. }
  186. slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
  187. /* figure out the listen interval based on dtim period and skip */
  188. if (slp_itrvl == 0xFF)
  189. cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
  190. cpu_to_le32(period * (skip + 1));
  191. slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
  192. if (slp_itrvl > period)
  193. cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
  194. cpu_to_le32((slp_itrvl / period) * period);
  195. if (skip)
  196. cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
  197. else
  198. cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
  199. if (priv->cfg->base_params->shadow_reg_enable)
  200. cmd->flags |= IWL_POWER_SHADOW_REG_ENA;
  201. else
  202. cmd->flags &= ~IWL_POWER_SHADOW_REG_ENA;
  203. if (iwl_advanced_bt_coexist(priv)) {
  204. if (!priv->cfg->bt_params->bt_sco_disable)
  205. cmd->flags |= IWL_POWER_BT_SCO_ENA;
  206. else
  207. cmd->flags &= ~IWL_POWER_BT_SCO_ENA;
  208. }
  209. slp_itrvl = le32_to_cpu(cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1]);
  210. if (slp_itrvl > IWL_CONN_MAX_LISTEN_INTERVAL)
  211. cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1] =
  212. cpu_to_le32(IWL_CONN_MAX_LISTEN_INTERVAL);
  213. /* enforce max sleep interval */
  214. for (i = IWL_POWER_VEC_SIZE - 1; i >= 0 ; i--) {
  215. if (le32_to_cpu(cmd->sleep_interval[i]) >
  216. (max_sleep[i] * period))
  217. cmd->sleep_interval[i] =
  218. cpu_to_le32(max_sleep[i] * period);
  219. if (i != (IWL_POWER_VEC_SIZE - 1)) {
  220. if (le32_to_cpu(cmd->sleep_interval[i]) >
  221. le32_to_cpu(cmd->sleep_interval[i+1]))
  222. cmd->sleep_interval[i] =
  223. cmd->sleep_interval[i+1];
  224. }
  225. }
  226. if (priv->power_data.pci_pm)
  227. cmd->flags |= IWL_POWER_PCI_PM_MSK;
  228. else
  229. cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
  230. IWL_DEBUG_POWER(priv, "numSkipDtim = %u, dtimPeriod = %d\n",
  231. skip, period);
  232. IWL_DEBUG_POWER(priv, "Sleep command for index %d\n", lvl + 1);
  233. }
  234. static void iwl_power_sleep_cam_cmd(struct iwl_priv *priv,
  235. struct iwl_powertable_cmd *cmd)
  236. {
  237. memset(cmd, 0, sizeof(*cmd));
  238. if (priv->power_data.pci_pm)
  239. cmd->flags |= IWL_POWER_PCI_PM_MSK;
  240. IWL_DEBUG_POWER(priv, "Sleep command for CAM\n");
  241. }
  242. static void iwl_power_fill_sleep_cmd(struct iwl_priv *priv,
  243. struct iwl_powertable_cmd *cmd,
  244. int dynps_ms, int wakeup_period)
  245. {
  246. /*
  247. * These are the original power level 3 sleep successions. The
  248. * device may behave better with such succession and was also
  249. * only tested with that. Just like the original sleep commands,
  250. * also adjust the succession here to the wakeup_period below.
  251. * The ranges are the same as for the sleep commands, 0-2, 3-9
  252. * and >10, which is selected based on the DTIM interval for
  253. * the sleep index but here we use the wakeup period since that
  254. * is what we need to do for the latency requirements.
  255. */
  256. static const u8 slp_succ_r0[IWL_POWER_VEC_SIZE] = { 2, 2, 2, 2, 2 };
  257. static const u8 slp_succ_r1[IWL_POWER_VEC_SIZE] = { 2, 4, 6, 7, 9 };
  258. static const u8 slp_succ_r2[IWL_POWER_VEC_SIZE] = { 2, 7, 9, 9, 0xFF };
  259. const u8 *slp_succ = slp_succ_r0;
  260. int i;
  261. if (wakeup_period > IWL_DTIM_RANGE_0_MAX)
  262. slp_succ = slp_succ_r1;
  263. if (wakeup_period > IWL_DTIM_RANGE_1_MAX)
  264. slp_succ = slp_succ_r2;
  265. memset(cmd, 0, sizeof(*cmd));
  266. cmd->flags = IWL_POWER_DRIVER_ALLOW_SLEEP_MSK |
  267. IWL_POWER_FAST_PD; /* no use seeing frames for others */
  268. if (priv->power_data.pci_pm)
  269. cmd->flags |= IWL_POWER_PCI_PM_MSK;
  270. if (priv->cfg->base_params->shadow_reg_enable)
  271. cmd->flags |= IWL_POWER_SHADOW_REG_ENA;
  272. else
  273. cmd->flags &= ~IWL_POWER_SHADOW_REG_ENA;
  274. if (iwl_advanced_bt_coexist(priv)) {
  275. if (!priv->cfg->bt_params->bt_sco_disable)
  276. cmd->flags |= IWL_POWER_BT_SCO_ENA;
  277. else
  278. cmd->flags &= ~IWL_POWER_BT_SCO_ENA;
  279. }
  280. cmd->rx_data_timeout = cpu_to_le32(1000 * dynps_ms);
  281. cmd->tx_data_timeout = cpu_to_le32(1000 * dynps_ms);
  282. for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
  283. cmd->sleep_interval[i] =
  284. cpu_to_le32(min_t(int, slp_succ[i], wakeup_period));
  285. IWL_DEBUG_POWER(priv, "Automatic sleep command\n");
  286. }
  287. static int iwl_set_power(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd)
  288. {
  289. IWL_DEBUG_POWER(priv, "Sending power/sleep command\n");
  290. IWL_DEBUG_POWER(priv, "Flags value = 0x%08X\n", cmd->flags);
  291. IWL_DEBUG_POWER(priv, "Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
  292. IWL_DEBUG_POWER(priv, "Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
  293. IWL_DEBUG_POWER(priv, "Sleep interval vector = { %d , %d , %d , %d , %d }\n",
  294. le32_to_cpu(cmd->sleep_interval[0]),
  295. le32_to_cpu(cmd->sleep_interval[1]),
  296. le32_to_cpu(cmd->sleep_interval[2]),
  297. le32_to_cpu(cmd->sleep_interval[3]),
  298. le32_to_cpu(cmd->sleep_interval[4]));
  299. return iwl_send_cmd_pdu(priv, POWER_TABLE_CMD,
  300. sizeof(struct iwl_powertable_cmd), cmd);
  301. }
  302. static void iwl_power_build_cmd(struct iwl_priv *priv,
  303. struct iwl_powertable_cmd *cmd)
  304. {
  305. bool enabled = priv->hw->conf.flags & IEEE80211_CONF_PS;
  306. int dtimper;
  307. dtimper = priv->hw->conf.ps_dtim_period ?: 1;
  308. if (!priv->cfg->base_params->no_idle_support &&
  309. priv->hw->conf.flags & IEEE80211_CONF_IDLE)
  310. iwl_static_sleep_cmd(priv, cmd, IWL_POWER_INDEX_5, 20);
  311. else if (iwl_tt_is_low_power_state(priv)) {
  312. /* in thermal throttling low power state */
  313. iwl_static_sleep_cmd(priv, cmd,
  314. iwl_tt_current_power_mode(priv), dtimper);
  315. } else if (!enabled)
  316. iwl_power_sleep_cam_cmd(priv, cmd);
  317. else if (priv->power_data.debug_sleep_level_override >= 0)
  318. iwl_static_sleep_cmd(priv, cmd,
  319. priv->power_data.debug_sleep_level_override,
  320. dtimper);
  321. else if (no_sleep_autoadjust)
  322. iwl_static_sleep_cmd(priv, cmd, IWL_POWER_INDEX_1, dtimper);
  323. else
  324. iwl_power_fill_sleep_cmd(priv, cmd,
  325. priv->hw->conf.dynamic_ps_timeout,
  326. priv->hw->conf.max_sleep_period);
  327. }
  328. int iwl_power_set_mode(struct iwl_priv *priv, struct iwl_powertable_cmd *cmd,
  329. bool force)
  330. {
  331. int ret;
  332. bool update_chains;
  333. lockdep_assert_held(&priv->mutex);
  334. /* Don't update the RX chain when chain noise calibration is running */
  335. update_chains = priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE ||
  336. priv->chain_noise_data.state == IWL_CHAIN_NOISE_ALIVE;
  337. if (!memcmp(&priv->power_data.sleep_cmd, cmd, sizeof(*cmd)) && !force)
  338. return 0;
  339. if (!iwl_is_ready_rf(priv))
  340. return -EIO;
  341. /* scan complete use sleep_power_next, need to be updated */
  342. memcpy(&priv->power_data.sleep_cmd_next, cmd, sizeof(*cmd));
  343. if (test_bit(STATUS_SCANNING, &priv->status) && !force) {
  344. IWL_DEBUG_INFO(priv, "Defer power set mode while scanning\n");
  345. return 0;
  346. }
  347. if (cmd->flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK)
  348. set_bit(STATUS_POWER_PMI, &priv->status);
  349. ret = iwl_set_power(priv, cmd);
  350. if (!ret) {
  351. if (!(cmd->flags & IWL_POWER_DRIVER_ALLOW_SLEEP_MSK))
  352. clear_bit(STATUS_POWER_PMI, &priv->status);
  353. if (priv->cfg->ops->lib->update_chain_flags && update_chains)
  354. priv->cfg->ops->lib->update_chain_flags(priv);
  355. else if (priv->cfg->ops->lib->update_chain_flags)
  356. IWL_DEBUG_POWER(priv,
  357. "Cannot update the power, chain noise "
  358. "calibration running: %d\n",
  359. priv->chain_noise_data.state);
  360. memcpy(&priv->power_data.sleep_cmd, cmd, sizeof(*cmd));
  361. } else
  362. IWL_ERR(priv, "set power fail, ret = %d", ret);
  363. return ret;
  364. }
  365. int iwl_power_update_mode(struct iwl_priv *priv, bool force)
  366. {
  367. struct iwl_powertable_cmd cmd;
  368. iwl_power_build_cmd(priv, &cmd);
  369. return iwl_power_set_mode(priv, &cmd, force);
  370. }
  371. /* initialize to default */
  372. void iwl_power_initialize(struct iwl_priv *priv)
  373. {
  374. u16 lctl = iwl_pcie_link_ctl(priv);
  375. priv->power_data.pci_pm = !(lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN);
  376. priv->power_data.debug_sleep_level_override = -1;
  377. memset(&priv->power_data.sleep_cmd, 0,
  378. sizeof(priv->power_data.sleep_cmd));
  379. }