power.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22. * USA
  23. *
  24. * The full GNU General Public License is included in this distribution
  25. * in the file called COPYING.
  26. *
  27. * Contact Information:
  28. * Intel Linux Wireless <ilw@linux.intel.com>
  29. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30. *
  31. * BSD LICENSE
  32. *
  33. * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. * * Neither the name Intel Corporation nor the names of its
  47. * contributors may be used to endorse or promote products derived
  48. * from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. *****************************************************************************/
  63. #include <linux/kernel.h>
  64. #include <linux/module.h>
  65. #include <linux/slab.h>
  66. #include <linux/init.h>
  67. #include <net/mac80211.h>
  68. #include "iwl-debug.h"
  69. #include "mvm.h"
  70. #include "iwl-modparams.h"
  71. #include "fw-api-power.h"
  72. #define POWER_KEEP_ALIVE_PERIOD_SEC 25
  73. static int iwl_mvm_beacon_filter_send_cmd(struct iwl_mvm *mvm,
  74. struct iwl_beacon_filter_cmd *cmd)
  75. {
  76. int ret;
  77. ret = iwl_mvm_send_cmd_pdu(mvm, REPLY_BEACON_FILTERING_CMD, CMD_SYNC,
  78. sizeof(struct iwl_beacon_filter_cmd), cmd);
  79. if (!ret) {
  80. IWL_DEBUG_POWER(mvm, "ba_enable_beacon_abort is: %d\n",
  81. cmd->ba_enable_beacon_abort);
  82. IWL_DEBUG_POWER(mvm, "ba_escape_timer is: %d\n",
  83. cmd->ba_escape_timer);
  84. IWL_DEBUG_POWER(mvm, "bf_debug_flag is: %d\n",
  85. cmd->bf_debug_flag);
  86. IWL_DEBUG_POWER(mvm, "bf_enable_beacon_filter is: %d\n",
  87. cmd->bf_enable_beacon_filter);
  88. IWL_DEBUG_POWER(mvm, "bf_energy_delta is: %d\n",
  89. cmd->bf_energy_delta);
  90. IWL_DEBUG_POWER(mvm, "bf_escape_timer is: %d\n",
  91. cmd->bf_escape_timer);
  92. IWL_DEBUG_POWER(mvm, "bf_roaming_energy_delta is: %d\n",
  93. cmd->bf_roaming_energy_delta);
  94. IWL_DEBUG_POWER(mvm, "bf_roaming_state is: %d\n",
  95. cmd->bf_roaming_state);
  96. IWL_DEBUG_POWER(mvm, "bf_temperature_delta is: %d\n",
  97. cmd->bf_temperature_delta);
  98. }
  99. return ret;
  100. }
  101. static int iwl_mvm_update_beacon_abort(struct iwl_mvm *mvm,
  102. struct ieee80211_vif *vif, bool enable)
  103. {
  104. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  105. struct iwl_beacon_filter_cmd cmd = {
  106. IWL_BF_CMD_CONFIG_DEFAULTS,
  107. .bf_enable_beacon_filter = 1,
  108. .ba_enable_beacon_abort = enable,
  109. };
  110. if (!mvmvif->bf_enabled)
  111. return 0;
  112. return iwl_mvm_beacon_filter_send_cmd(mvm, &cmd);
  113. }
  114. static void iwl_mvm_power_log(struct iwl_mvm *mvm,
  115. struct iwl_powertable_cmd *cmd)
  116. {
  117. IWL_DEBUG_POWER(mvm,
  118. "Sending power table command for power level %d, flags = 0x%X\n",
  119. iwlmvm_mod_params.power_scheme,
  120. le16_to_cpu(cmd->flags));
  121. IWL_DEBUG_POWER(mvm, "Keep alive = %u sec\n", cmd->keep_alive_seconds);
  122. if (cmd->flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK)) {
  123. IWL_DEBUG_POWER(mvm, "Rx timeout = %u usec\n",
  124. le32_to_cpu(cmd->rx_data_timeout));
  125. IWL_DEBUG_POWER(mvm, "Tx timeout = %u usec\n",
  126. le32_to_cpu(cmd->tx_data_timeout));
  127. IWL_DEBUG_POWER(mvm, "LP RX RSSI threshold = %u\n",
  128. cmd->lprx_rssi_threshold);
  129. if (cmd->flags & cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK))
  130. IWL_DEBUG_POWER(mvm, "DTIMs to skip = %u\n",
  131. le32_to_cpu(cmd->num_skip_dtim));
  132. }
  133. }
  134. void iwl_mvm_power_build_cmd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  135. struct iwl_powertable_cmd *cmd)
  136. {
  137. struct ieee80211_hw *hw = mvm->hw;
  138. struct ieee80211_chanctx_conf *chanctx_conf;
  139. struct ieee80211_channel *chan;
  140. int dtimper, dtimper_msec;
  141. int keep_alive;
  142. bool radar_detect = false;
  143. /*
  144. * Regardless of power management state the driver must set
  145. * keep alive period. FW will use it for sending keep alive NDPs
  146. * immediately after association.
  147. */
  148. cmd->keep_alive_seconds = POWER_KEEP_ALIVE_PERIOD_SEC;
  149. if (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM)
  150. return;
  151. cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK);
  152. if (!vif->bss_conf.ps)
  153. return;
  154. cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK);
  155. dtimper = hw->conf.ps_dtim_period ?: 1;
  156. /* Check if radar detection is required on current channel */
  157. rcu_read_lock();
  158. chanctx_conf = rcu_dereference(vif->chanctx_conf);
  159. WARN_ON(!chanctx_conf);
  160. if (chanctx_conf) {
  161. chan = chanctx_conf->def.chan;
  162. radar_detect = chan->flags & IEEE80211_CHAN_RADAR;
  163. }
  164. rcu_read_unlock();
  165. /* Check skip over DTIM conditions */
  166. if (!radar_detect && (dtimper <= 10) &&
  167. (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_LP ||
  168. mvm->cur_ucode == IWL_UCODE_WOWLAN)) {
  169. cmd->flags |= cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
  170. cmd->num_skip_dtim = cpu_to_le32(2);
  171. }
  172. /* Check that keep alive period is at least 3 * DTIM */
  173. dtimper_msec = dtimper * vif->bss_conf.beacon_int;
  174. keep_alive = max_t(int, 3 * dtimper_msec,
  175. MSEC_PER_SEC * cmd->keep_alive_seconds);
  176. keep_alive = DIV_ROUND_UP(keep_alive, MSEC_PER_SEC);
  177. cmd->keep_alive_seconds = keep_alive;
  178. if (mvm->cur_ucode != IWL_UCODE_WOWLAN) {
  179. cmd->rx_data_timeout = cpu_to_le32(100 * USEC_PER_MSEC);
  180. cmd->tx_data_timeout = cpu_to_le32(100 * USEC_PER_MSEC);
  181. } else {
  182. cmd->rx_data_timeout = cpu_to_le32(10 * USEC_PER_MSEC);
  183. cmd->tx_data_timeout = cpu_to_le32(10 * USEC_PER_MSEC);
  184. }
  185. }
  186. int iwl_mvm_power_update_mode(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  187. {
  188. int ret;
  189. bool ba_enable;
  190. struct iwl_powertable_cmd cmd = {};
  191. if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
  192. return 0;
  193. iwl_mvm_power_build_cmd(mvm, vif, &cmd);
  194. iwl_mvm_power_log(mvm, &cmd);
  195. ret = iwl_mvm_send_cmd_pdu(mvm, POWER_TABLE_CMD, CMD_SYNC,
  196. sizeof(cmd), &cmd);
  197. if (ret)
  198. return ret;
  199. ba_enable = !!(cmd.flags &
  200. cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK));
  201. return iwl_mvm_update_beacon_abort(mvm, vif, ba_enable);
  202. }
  203. int iwl_mvm_power_disable(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  204. {
  205. struct iwl_powertable_cmd cmd = {};
  206. if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
  207. return 0;
  208. if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
  209. cmd.flags |= cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK);
  210. iwl_mvm_power_log(mvm, &cmd);
  211. return iwl_mvm_send_cmd_pdu(mvm, POWER_TABLE_CMD, CMD_ASYNC,
  212. sizeof(cmd), &cmd);
  213. }
  214. int iwl_mvm_enable_beacon_filter(struct iwl_mvm *mvm,
  215. struct ieee80211_vif *vif)
  216. {
  217. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  218. struct iwl_beacon_filter_cmd cmd = {
  219. IWL_BF_CMD_CONFIG_DEFAULTS,
  220. .bf_enable_beacon_filter = 1,
  221. };
  222. int ret;
  223. if (mvmvif != mvm->bf_allowed_vif ||
  224. vif->type != NL80211_IFTYPE_STATION || vif->p2p)
  225. return 0;
  226. ret = iwl_mvm_beacon_filter_send_cmd(mvm, &cmd);
  227. if (!ret)
  228. mvmvif->bf_enabled = true;
  229. return ret;
  230. }
  231. int iwl_mvm_disable_beacon_filter(struct iwl_mvm *mvm,
  232. struct ieee80211_vif *vif)
  233. {
  234. struct iwl_beacon_filter_cmd cmd = {};
  235. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  236. int ret;
  237. if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
  238. return 0;
  239. ret = iwl_mvm_beacon_filter_send_cmd(mvm, &cmd);
  240. if (!ret)
  241. mvmvif->bf_enabled = false;
  242. return ret;
  243. }