iwl-power.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2007 - 2009 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/init.h>
  31. #include <net/mac80211.h>
  32. #include "iwl-eeprom.h"
  33. #include "iwl-dev.h"
  34. #include "iwl-core.h"
  35. #include "iwl-commands.h"
  36. #include "iwl-debug.h"
  37. #include "iwl-power.h"
  38. /*
  39. * Setting power level allow the card to go to sleep when not busy
  40. * there are three factor that decide the power level to go to, they
  41. * are list here with its priority
  42. * 1- critical_power_setting this will be set according to card temperature.
  43. * 2- system_power_setting this will be set by system PM manager.
  44. * 3- user_power_setting this will be set by user either by writing to sys or
  45. * mac80211
  46. *
  47. * if system_power_setting and user_power_setting is set to auto
  48. * the power level will be decided according to association status and battery
  49. * status.
  50. *
  51. */
  52. #define MSEC_TO_USEC 1024
  53. #define IWL_POWER_RANGE_0_MAX (2)
  54. #define IWL_POWER_RANGE_1_MAX (10)
  55. #define IWL_POWER_ON_BATTERY IWL_POWER_INDEX_5
  56. #define IWL_POWER_ON_AC_DISASSOC IWL_POWER_MODE_CAM
  57. #define IWL_POWER_ON_AC_ASSOC IWL_POWER_MODE_CAM
  58. #define IWL_CT_KILL_TEMPERATURE 110
  59. #define IWL_MIN_POWER_TEMPERATURE 100
  60. #define IWL_REDUCED_POWER_TEMPERATURE 95
  61. /* default power management (not Tx power) table values */
  62. /* for TIM 0-10 */
  63. static struct iwl_power_vec_entry range_0[IWL_POWER_MAX] = {
  64. {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
  65. {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
  66. {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 2, 2, 0xFF)}, 0},
  67. {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 2, 2, 2, 0xFF)}, 0},
  68. {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 2, 4, 4, 0xFF)}, 1},
  69. {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 2, 4, 6, 0xFF)}, 2}
  70. };
  71. /* for TIM = 3-10 */
  72. static struct iwl_power_vec_entry range_1[IWL_POWER_MAX] = {
  73. {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
  74. {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
  75. {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(1, 2, 3, 4, 7)}, 0},
  76. {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 4, 6, 7, 9)}, 0},
  77. {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 4, 6, 9, 10)}, 1},
  78. {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(2, 4, 7, 10, 10)}, 2}
  79. };
  80. /* for TIM > 11 */
  81. static struct iwl_power_vec_entry range_2[IWL_POWER_MAX] = {
  82. {{NOSLP, SLP_TOUT(0), SLP_TOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
  83. {{SLP, SLP_TOUT(200), SLP_TOUT(500), SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
  84. {{SLP, SLP_TOUT(200), SLP_TOUT(300), SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
  85. {{SLP, SLP_TOUT(50), SLP_TOUT(100), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
  86. {{SLP, SLP_TOUT(50), SLP_TOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
  87. {{SLP, SLP_TOUT(25), SLP_TOUT(25), SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
  88. };
  89. /* set card power command */
  90. static int iwl_set_power(struct iwl_priv *priv, void *cmd)
  91. {
  92. return iwl_send_cmd_pdu(priv, POWER_TABLE_CMD,
  93. sizeof(struct iwl_powertable_cmd), cmd);
  94. }
  95. /* decide the right power level according to association status
  96. * and battery status
  97. */
  98. static u16 iwl_get_auto_power_mode(struct iwl_priv *priv)
  99. {
  100. u16 mode;
  101. switch (priv->power_data.user_power_setting) {
  102. case IWL_POWER_AUTO:
  103. /* if running on battery */
  104. if (priv->power_data.is_battery_active)
  105. mode = IWL_POWER_ON_BATTERY;
  106. else if (iwl_is_associated(priv))
  107. mode = IWL_POWER_ON_AC_ASSOC;
  108. else
  109. mode = IWL_POWER_ON_AC_DISASSOC;
  110. break;
  111. default:
  112. mode = priv->power_data.user_power_setting;
  113. break;
  114. }
  115. return mode;
  116. }
  117. /* initialize to default */
  118. static void iwl_power_init_handle(struct iwl_priv *priv)
  119. {
  120. struct iwl_power_mgr *pow_data;
  121. int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_MAX;
  122. struct iwl_powertable_cmd *cmd;
  123. int i;
  124. u16 lctl;
  125. IWL_DEBUG_POWER(priv, "Initialize power \n");
  126. pow_data = &priv->power_data;
  127. memset(pow_data, 0, sizeof(*pow_data));
  128. memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
  129. memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
  130. memcpy(&pow_data->pwr_range_2[0], &range_2[0], size);
  131. lctl = iwl_pcie_link_ctl(priv);
  132. IWL_DEBUG_POWER(priv, "adjust power command flags\n");
  133. for (i = 0; i < IWL_POWER_MAX; i++) {
  134. cmd = &pow_data->pwr_range_0[i].cmd;
  135. if (lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN)
  136. cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
  137. else
  138. cmd->flags |= IWL_POWER_PCI_PM_MSK;
  139. }
  140. }
  141. /* adjust power command according to DTIM period and power level*/
  142. static int iwl_update_power_cmd(struct iwl_priv *priv,
  143. struct iwl_powertable_cmd *cmd, u16 mode)
  144. {
  145. struct iwl_power_vec_entry *range;
  146. struct iwl_power_mgr *pow_data;
  147. int i;
  148. u32 max_sleep = 0;
  149. u8 period;
  150. bool skip;
  151. if (mode > IWL_POWER_INDEX_5) {
  152. IWL_DEBUG_POWER(priv, "Error invalid power mode \n");
  153. return -EINVAL;
  154. }
  155. pow_data = &priv->power_data;
  156. if (pow_data->dtim_period <= IWL_POWER_RANGE_0_MAX)
  157. range = &pow_data->pwr_range_0[0];
  158. else if (pow_data->dtim_period <= IWL_POWER_RANGE_1_MAX)
  159. range = &pow_data->pwr_range_1[0];
  160. else
  161. range = &pow_data->pwr_range_2[0];
  162. period = pow_data->dtim_period;
  163. memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
  164. if (period == 0) {
  165. period = 1;
  166. skip = false;
  167. } else {
  168. skip = !!range[mode].no_dtim;
  169. }
  170. if (skip) {
  171. __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
  172. max_sleep = le32_to_cpu(slp_itrvl);
  173. if (max_sleep == 0xFF)
  174. max_sleep = period * (skip + 1);
  175. else if (max_sleep > period)
  176. max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
  177. cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
  178. } else {
  179. max_sleep = period;
  180. cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
  181. }
  182. for (i = 0; i < IWL_POWER_VEC_SIZE; i++)
  183. if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
  184. cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
  185. IWL_DEBUG_POWER(priv, "Flags value = 0x%08X\n", cmd->flags);
  186. IWL_DEBUG_POWER(priv, "Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
  187. IWL_DEBUG_POWER(priv, "Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
  188. IWL_DEBUG_POWER(priv, "Sleep interval vector = { %d , %d , %d , %d , %d }\n",
  189. le32_to_cpu(cmd->sleep_interval[0]),
  190. le32_to_cpu(cmd->sleep_interval[1]),
  191. le32_to_cpu(cmd->sleep_interval[2]),
  192. le32_to_cpu(cmd->sleep_interval[3]),
  193. le32_to_cpu(cmd->sleep_interval[4]));
  194. return 0;
  195. }
  196. /*
  197. * compute the final power mode index
  198. */
  199. int iwl_power_update_mode(struct iwl_priv *priv, bool force)
  200. {
  201. struct iwl_power_mgr *setting = &(priv->power_data);
  202. int ret = 0;
  203. u16 uninitialized_var(final_mode);
  204. bool update_chains;
  205. /* Don't update the RX chain when chain noise calibration is running */
  206. update_chains = priv->chain_noise_data.state == IWL_CHAIN_NOISE_DONE ||
  207. priv->chain_noise_data.state == IWL_CHAIN_NOISE_ALIVE;
  208. /* If on battery, set to 3,
  209. * if plugged into AC power, set to CAM ("continuously aware mode"),
  210. * else user level */
  211. switch (setting->system_power_setting) {
  212. case IWL_POWER_SYS_AUTO:
  213. final_mode = iwl_get_auto_power_mode(priv);
  214. break;
  215. case IWL_POWER_SYS_BATTERY:
  216. final_mode = IWL_POWER_INDEX_3;
  217. break;
  218. case IWL_POWER_SYS_AC:
  219. final_mode = IWL_POWER_MODE_CAM;
  220. break;
  221. default:
  222. final_mode = IWL_POWER_INDEX_3;
  223. WARN_ON(1);
  224. }
  225. if (setting->critical_power_setting > final_mode)
  226. final_mode = setting->critical_power_setting;
  227. /* driver only support CAM for non STA network */
  228. if (priv->iw_mode != NL80211_IFTYPE_STATION)
  229. final_mode = IWL_POWER_MODE_CAM;
  230. if (iwl_is_ready_rf(priv) && !setting->power_disabled &&
  231. ((setting->power_mode != final_mode) || force)) {
  232. struct iwl_powertable_cmd cmd;
  233. if (final_mode != IWL_POWER_MODE_CAM)
  234. set_bit(STATUS_POWER_PMI, &priv->status);
  235. iwl_update_power_cmd(priv, &cmd, final_mode);
  236. cmd.keep_alive_beacons = 0;
  237. if (final_mode == IWL_POWER_INDEX_5)
  238. cmd.flags |= IWL_POWER_FAST_PD;
  239. ret = iwl_set_power(priv, &cmd);
  240. if (final_mode == IWL_POWER_MODE_CAM)
  241. clear_bit(STATUS_POWER_PMI, &priv->status);
  242. else
  243. set_bit(STATUS_POWER_PMI, &priv->status);
  244. if (priv->cfg->ops->lib->update_chain_flags && update_chains)
  245. priv->cfg->ops->lib->update_chain_flags(priv);
  246. else
  247. IWL_DEBUG_POWER(priv, "Cannot update the power, chain noise "
  248. "calibration running: %d\n",
  249. priv->chain_noise_data.state);
  250. if (!ret)
  251. setting->power_mode = final_mode;
  252. }
  253. return ret;
  254. }
  255. EXPORT_SYMBOL(iwl_power_update_mode);
  256. /* Allow other iwl code to disable/enable power management active
  257. * this will be useful for rate scale to disable PM during heavy
  258. * Tx/Rx activities
  259. */
  260. int iwl_power_disable_management(struct iwl_priv *priv, u32 ms)
  261. {
  262. u16 prev_mode;
  263. int ret = 0;
  264. if (priv->power_data.power_disabled)
  265. return -EBUSY;
  266. prev_mode = priv->power_data.user_power_setting;
  267. priv->power_data.user_power_setting = IWL_POWER_MODE_CAM;
  268. ret = iwl_power_update_mode(priv, 0);
  269. priv->power_data.power_disabled = 1;
  270. priv->power_data.user_power_setting = prev_mode;
  271. cancel_delayed_work(&priv->set_power_save);
  272. if (ms)
  273. queue_delayed_work(priv->workqueue, &priv->set_power_save,
  274. msecs_to_jiffies(ms));
  275. return ret;
  276. }
  277. EXPORT_SYMBOL(iwl_power_disable_management);
  278. /* Allow other iwl code to disable/enable power management active
  279. * this will be useful for rate scale to disable PM during high
  280. * volume activities
  281. */
  282. int iwl_power_enable_management(struct iwl_priv *priv)
  283. {
  284. int ret = 0;
  285. priv->power_data.power_disabled = 0;
  286. ret = iwl_power_update_mode(priv, 0);
  287. return ret;
  288. }
  289. EXPORT_SYMBOL(iwl_power_enable_management);
  290. /* set user_power_setting */
  291. int iwl_power_set_user_mode(struct iwl_priv *priv, u16 mode)
  292. {
  293. if (mode > IWL_POWER_MAX)
  294. return -EINVAL;
  295. priv->power_data.user_power_setting = mode;
  296. return iwl_power_update_mode(priv, 0);
  297. }
  298. EXPORT_SYMBOL(iwl_power_set_user_mode);
  299. /* set system_power_setting. This should be set by over all
  300. * PM application.
  301. */
  302. int iwl_power_set_system_mode(struct iwl_priv *priv, u16 mode)
  303. {
  304. if (mode < IWL_POWER_SYS_MAX)
  305. priv->power_data.system_power_setting = mode;
  306. else
  307. return -EINVAL;
  308. return iwl_power_update_mode(priv, 0);
  309. }
  310. EXPORT_SYMBOL(iwl_power_set_system_mode);
  311. /* initialize to default */
  312. void iwl_power_initialize(struct iwl_priv *priv)
  313. {
  314. iwl_power_init_handle(priv);
  315. priv->power_data.user_power_setting = IWL_POWER_AUTO;
  316. priv->power_data.system_power_setting = IWL_POWER_SYS_AUTO;
  317. priv->power_data.power_disabled = 0;
  318. priv->power_data.is_battery_active = 0;
  319. priv->power_data.critical_power_setting = 0;
  320. }
  321. EXPORT_SYMBOL(iwl_power_initialize);
  322. /* set critical_power_setting according to temperature value */
  323. int iwl_power_temperature_change(struct iwl_priv *priv)
  324. {
  325. int ret = 0;
  326. s32 temperature = KELVIN_TO_CELSIUS(priv->last_temperature);
  327. u16 new_critical = priv->power_data.critical_power_setting;
  328. if (temperature > IWL_CT_KILL_TEMPERATURE)
  329. return 0;
  330. else if (temperature > IWL_MIN_POWER_TEMPERATURE)
  331. new_critical = IWL_POWER_INDEX_5;
  332. else if (temperature > IWL_REDUCED_POWER_TEMPERATURE)
  333. new_critical = IWL_POWER_INDEX_3;
  334. else
  335. new_critical = IWL_POWER_MODE_CAM;
  336. if (new_critical != priv->power_data.critical_power_setting)
  337. priv->power_data.critical_power_setting = new_critical;
  338. if (priv->power_data.critical_power_setting >
  339. priv->power_data.power_mode)
  340. ret = iwl_power_update_mode(priv, 0);
  341. return ret;
  342. }
  343. EXPORT_SYMBOL(iwl_power_temperature_change);
  344. static void iwl_bg_set_power_save(struct work_struct *work)
  345. {
  346. struct iwl_priv *priv = container_of(work,
  347. struct iwl_priv, set_power_save.work);
  348. IWL_DEBUG_POWER(priv, "update power\n");
  349. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  350. return;
  351. mutex_lock(&priv->mutex);
  352. /* on starting association we disable power management
  353. * until association, if association failed then this
  354. * timer will expire and enable PM again.
  355. */
  356. if (!iwl_is_associated(priv))
  357. iwl_power_enable_management(priv);
  358. mutex_unlock(&priv->mutex);
  359. }
  360. void iwl_setup_power_deferred_work(struct iwl_priv *priv)
  361. {
  362. INIT_DELAYED_WORK(&priv->set_power_save, iwl_bg_set_power_save);
  363. }
  364. EXPORT_SYMBOL(iwl_setup_power_deferred_work);
  365. void iwl_power_cancel_timeout(struct iwl_priv *priv)
  366. {
  367. cancel_delayed_work(&priv->set_power_save);
  368. }
  369. EXPORT_SYMBOL(iwl_power_cancel_timeout);