iwl-power.c 14 KB

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