iwl-power.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. * This defines the old power levels. They are still used by default
  49. * (level 1) and for thermal throttle (levels 3 through 5)
  50. */
  51. struct il_power_vec_entry {
  52. struct il_powertable_cmd cmd;
  53. u8 no_dtim; /* number of skip dtim */
  54. };
  55. static void il_power_sleep_cam_cmd(struct il_priv *il,
  56. struct il_powertable_cmd *cmd)
  57. {
  58. memset(cmd, 0, sizeof(*cmd));
  59. if (il->power_data.pci_pm)
  60. cmd->flags |= IL_POWER_PCI_PM_MSK;
  61. IL_DEBUG_POWER(il, "Sleep command for CAM\n");
  62. }
  63. static int
  64. il_set_power(struct il_priv *il, struct il_powertable_cmd *cmd)
  65. {
  66. IL_DEBUG_POWER(il, "Sending power/sleep command\n");
  67. IL_DEBUG_POWER(il, "Flags value = 0x%08X\n", cmd->flags);
  68. IL_DEBUG_POWER(il, "Tx timeout = %u\n",
  69. le32_to_cpu(cmd->tx_data_timeout));
  70. IL_DEBUG_POWER(il, "Rx timeout = %u\n",
  71. le32_to_cpu(cmd->rx_data_timeout));
  72. IL_DEBUG_POWER(il,
  73. "Sleep interval vector = { %d , %d , %d , %d , %d }\n",
  74. le32_to_cpu(cmd->sleep_interval[0]),
  75. le32_to_cpu(cmd->sleep_interval[1]),
  76. le32_to_cpu(cmd->sleep_interval[2]),
  77. le32_to_cpu(cmd->sleep_interval[3]),
  78. le32_to_cpu(cmd->sleep_interval[4]));
  79. return il_send_cmd_pdu(il, POWER_TABLE_CMD,
  80. sizeof(struct il_powertable_cmd), cmd);
  81. }
  82. int
  83. il_power_set_mode(struct il_priv *il, struct il_powertable_cmd *cmd,
  84. bool force)
  85. {
  86. int ret;
  87. bool update_chains;
  88. lockdep_assert_held(&il->mutex);
  89. /* Don't update the RX chain when chain noise calibration is running */
  90. update_chains = il->chain_noise_data.state == IL_CHAIN_NOISE_DONE ||
  91. il->chain_noise_data.state == IL_CHAIN_NOISE_ALIVE;
  92. if (!memcmp(&il->power_data.sleep_cmd, cmd, sizeof(*cmd)) && !force)
  93. return 0;
  94. if (!il_is_ready_rf(il))
  95. return -EIO;
  96. /* scan complete use sleep_power_next, need to be updated */
  97. memcpy(&il->power_data.sleep_cmd_next, cmd, sizeof(*cmd));
  98. if (test_bit(STATUS_SCANNING, &il->status) && !force) {
  99. IL_DEBUG_INFO(il, "Defer power set mode while scanning\n");
  100. return 0;
  101. }
  102. if (cmd->flags & IL_POWER_DRIVER_ALLOW_SLEEP_MSK)
  103. set_bit(STATUS_POWER_PMI, &il->status);
  104. ret = il_set_power(il, cmd);
  105. if (!ret) {
  106. if (!(cmd->flags & IL_POWER_DRIVER_ALLOW_SLEEP_MSK))
  107. clear_bit(STATUS_POWER_PMI, &il->status);
  108. if (il->cfg->ops->lib->update_chain_flags && update_chains)
  109. il->cfg->ops->lib->update_chain_flags(il);
  110. else if (il->cfg->ops->lib->update_chain_flags)
  111. IL_DEBUG_POWER(il,
  112. "Cannot update the power, chain noise "
  113. "calibration running: %d\n",
  114. il->chain_noise_data.state);
  115. memcpy(&il->power_data.sleep_cmd, cmd, sizeof(*cmd));
  116. } else
  117. IL_ERR(il, "set power fail, ret = %d", ret);
  118. return ret;
  119. }
  120. int il_power_update_mode(struct il_priv *il, bool force)
  121. {
  122. struct il_powertable_cmd cmd;
  123. il_power_sleep_cam_cmd(il, &cmd);
  124. return il_power_set_mode(il, &cmd, force);
  125. }
  126. EXPORT_SYMBOL(il_power_update_mode);
  127. /* initialize to default */
  128. void il_power_initialize(struct il_priv *il)
  129. {
  130. u16 lctl = il_pcie_link_ctl(il);
  131. il->power_data.pci_pm = !(lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN);
  132. il->power_data.debug_sleep_level_override = -1;
  133. memset(&il->power_data.sleep_cmd, 0,
  134. sizeof(il->power_data.sleep_cmd));
  135. }
  136. EXPORT_SYMBOL(il_power_initialize);