iwl-power.c 14 KB

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