iwl-power.c 12 KB

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