power_legacy.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 void iwl_mvm_power_log(struct iwl_mvm *mvm,
  74. struct iwl_powertable_cmd *cmd)
  75. {
  76. IWL_DEBUG_POWER(mvm,
  77. "Sending power table command for power level %d, flags = 0x%X\n",
  78. iwlmvm_mod_params.power_scheme,
  79. le16_to_cpu(cmd->flags));
  80. IWL_DEBUG_POWER(mvm, "Keep alive = %u sec\n", cmd->keep_alive_seconds);
  81. if (cmd->flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK)) {
  82. IWL_DEBUG_POWER(mvm, "Rx timeout = %u usec\n",
  83. le32_to_cpu(cmd->rx_data_timeout));
  84. IWL_DEBUG_POWER(mvm, "Tx timeout = %u usec\n",
  85. le32_to_cpu(cmd->tx_data_timeout));
  86. if (cmd->flags & cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK))
  87. IWL_DEBUG_POWER(mvm, "DTIM periods to skip = %u\n",
  88. le32_to_cpu(cmd->skip_dtim_periods));
  89. if (cmd->flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
  90. IWL_DEBUG_POWER(mvm, "LP RX RSSI threshold = %u\n",
  91. le32_to_cpu(cmd->lprx_rssi_threshold));
  92. }
  93. }
  94. static void iwl_mvm_power_build_cmd(struct iwl_mvm *mvm,
  95. struct ieee80211_vif *vif,
  96. struct iwl_powertable_cmd *cmd)
  97. {
  98. struct ieee80211_hw *hw = mvm->hw;
  99. struct ieee80211_chanctx_conf *chanctx_conf;
  100. struct ieee80211_channel *chan;
  101. int dtimper, dtimper_msec;
  102. int keep_alive;
  103. bool radar_detect = false;
  104. struct iwl_mvm_vif *mvmvif __maybe_unused =
  105. iwl_mvm_vif_from_mac80211(vif);
  106. /*
  107. * Regardless of power management state the driver must set
  108. * keep alive period. FW will use it for sending keep alive NDPs
  109. * immediately after association.
  110. */
  111. cmd->keep_alive_seconds = POWER_KEEP_ALIVE_PERIOD_SEC;
  112. if (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM)
  113. return;
  114. cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK);
  115. if (!vif->bss_conf.assoc)
  116. cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK);
  117. #ifdef CONFIG_IWLWIFI_DEBUGFS
  118. if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_DISABLE_POWER_OFF &&
  119. mvmvif->dbgfs_pm.disable_power_off)
  120. cmd->flags &= cpu_to_le16(~POWER_FLAGS_POWER_SAVE_ENA_MSK);
  121. #endif
  122. if (!vif->bss_conf.ps)
  123. return;
  124. cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK);
  125. if (vif->bss_conf.beacon_rate &&
  126. (vif->bss_conf.beacon_rate->bitrate == 10 ||
  127. vif->bss_conf.beacon_rate->bitrate == 60)) {
  128. cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
  129. cmd->lprx_rssi_threshold =
  130. cpu_to_le32(POWER_LPRX_RSSI_THRESHOLD);
  131. }
  132. dtimper = hw->conf.ps_dtim_period ?: 1;
  133. /* Check if radar detection is required on current channel */
  134. rcu_read_lock();
  135. chanctx_conf = rcu_dereference(vif->chanctx_conf);
  136. WARN_ON(!chanctx_conf);
  137. if (chanctx_conf) {
  138. chan = chanctx_conf->def.chan;
  139. radar_detect = chan->flags & IEEE80211_CHAN_RADAR;
  140. }
  141. rcu_read_unlock();
  142. /* Check skip over DTIM conditions */
  143. if (!radar_detect && (dtimper <= 10) &&
  144. (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_LP ||
  145. mvm->cur_ucode == IWL_UCODE_WOWLAN)) {
  146. cmd->flags |= cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
  147. cmd->skip_dtim_periods = cpu_to_le32(3);
  148. }
  149. /* Check that keep alive period is at least 3 * DTIM */
  150. dtimper_msec = dtimper * vif->bss_conf.beacon_int;
  151. keep_alive = max_t(int, 3 * dtimper_msec,
  152. MSEC_PER_SEC * cmd->keep_alive_seconds);
  153. keep_alive = DIV_ROUND_UP(keep_alive, MSEC_PER_SEC);
  154. cmd->keep_alive_seconds = keep_alive;
  155. if (mvm->cur_ucode != IWL_UCODE_WOWLAN) {
  156. cmd->rx_data_timeout = cpu_to_le32(100 * USEC_PER_MSEC);
  157. cmd->tx_data_timeout = cpu_to_le32(100 * USEC_PER_MSEC);
  158. } else {
  159. cmd->rx_data_timeout = cpu_to_le32(10 * USEC_PER_MSEC);
  160. cmd->tx_data_timeout = cpu_to_le32(10 * USEC_PER_MSEC);
  161. }
  162. #ifdef CONFIG_IWLWIFI_DEBUGFS
  163. if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_KEEP_ALIVE)
  164. cmd->keep_alive_seconds = mvmvif->dbgfs_pm.keep_alive_seconds;
  165. if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_OVER_DTIM) {
  166. if (mvmvif->dbgfs_pm.skip_over_dtim)
  167. cmd->flags |=
  168. cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
  169. else
  170. cmd->flags &=
  171. cpu_to_le16(~POWER_FLAGS_SKIP_OVER_DTIM_MSK);
  172. }
  173. if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_RX_DATA_TIMEOUT)
  174. cmd->rx_data_timeout =
  175. cpu_to_le32(mvmvif->dbgfs_pm.rx_data_timeout);
  176. if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_TX_DATA_TIMEOUT)
  177. cmd->tx_data_timeout =
  178. cpu_to_le32(mvmvif->dbgfs_pm.tx_data_timeout);
  179. if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_DTIM_PERIODS)
  180. cmd->skip_dtim_periods =
  181. cpu_to_le32(mvmvif->dbgfs_pm.skip_dtim_periods);
  182. if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_ENA) {
  183. if (mvmvif->dbgfs_pm.lprx_ena)
  184. cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
  185. else
  186. cmd->flags &= cpu_to_le16(~POWER_FLAGS_LPRX_ENA_MSK);
  187. }
  188. if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_RSSI_THRESHOLD)
  189. cmd->lprx_rssi_threshold =
  190. cpu_to_le32(mvmvif->dbgfs_pm.lprx_rssi_threshold);
  191. #endif /* CONFIG_IWLWIFI_DEBUGFS */
  192. }
  193. static int iwl_mvm_power_legacy_update_mode(struct iwl_mvm *mvm,
  194. struct ieee80211_vif *vif)
  195. {
  196. int ret;
  197. bool ba_enable;
  198. struct iwl_powertable_cmd cmd = {};
  199. if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
  200. return 0;
  201. /*
  202. * TODO: The following vif_count verification is temporary condition.
  203. * Avoid power mode update if more than one interface is currently
  204. * active. Remove this condition when FW will support power management
  205. * on multiple MACs.
  206. */
  207. IWL_DEBUG_POWER(mvm, "Currently %d interfaces active\n",
  208. mvm->vif_count);
  209. if (mvm->vif_count > 1)
  210. return 0;
  211. iwl_mvm_power_build_cmd(mvm, vif, &cmd);
  212. iwl_mvm_power_log(mvm, &cmd);
  213. ret = iwl_mvm_send_cmd_pdu(mvm, POWER_TABLE_CMD, CMD_SYNC,
  214. sizeof(cmd), &cmd);
  215. if (ret)
  216. return ret;
  217. ba_enable = !!(cmd.flags &
  218. cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK));
  219. return iwl_mvm_update_beacon_abort(mvm, vif, ba_enable);
  220. }
  221. static int iwl_mvm_power_legacy_disable(struct iwl_mvm *mvm,
  222. struct ieee80211_vif *vif)
  223. {
  224. struct iwl_powertable_cmd cmd = {};
  225. struct iwl_mvm_vif *mvmvif __maybe_unused =
  226. iwl_mvm_vif_from_mac80211(vif);
  227. if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
  228. return 0;
  229. if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
  230. cmd.flags |= cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK);
  231. #ifdef CONFIG_IWLWIFI_DEBUGFS
  232. if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_DISABLE_POWER_OFF &&
  233. mvmvif->dbgfs_pm.disable_power_off)
  234. cmd.flags &= cpu_to_le16(~POWER_FLAGS_POWER_SAVE_ENA_MSK);
  235. #endif
  236. iwl_mvm_power_log(mvm, &cmd);
  237. return iwl_mvm_send_cmd_pdu(mvm, POWER_TABLE_CMD, CMD_ASYNC,
  238. sizeof(cmd), &cmd);
  239. }
  240. #ifdef CONFIG_IWLWIFI_DEBUGFS
  241. static int iwl_mvm_power_legacy_dbgfs_read(struct iwl_mvm *mvm,
  242. struct ieee80211_vif *vif, char *buf,
  243. int bufsz)
  244. {
  245. struct iwl_powertable_cmd cmd = {};
  246. int pos = 0;
  247. iwl_mvm_power_build_cmd(mvm, vif, &cmd);
  248. pos += scnprintf(buf+pos, bufsz-pos, "disable_power_off = %d\n",
  249. (cmd.flags &
  250. cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK)) ?
  251. 0 : 1);
  252. pos += scnprintf(buf+pos, bufsz-pos, "skip_dtim_periods = %d\n",
  253. le32_to_cpu(cmd.skip_dtim_periods));
  254. pos += scnprintf(buf+pos, bufsz-pos, "power_scheme = %d\n",
  255. iwlmvm_mod_params.power_scheme);
  256. pos += scnprintf(buf+pos, bufsz-pos, "flags = 0x%x\n",
  257. le16_to_cpu(cmd.flags));
  258. pos += scnprintf(buf+pos, bufsz-pos, "keep_alive = %d\n",
  259. cmd.keep_alive_seconds);
  260. if (cmd.flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK)) {
  261. pos += scnprintf(buf+pos, bufsz-pos, "skip_over_dtim = %d\n",
  262. (cmd.flags &
  263. cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK)) ?
  264. 1 : 0);
  265. pos += scnprintf(buf+pos, bufsz-pos, "rx_data_timeout = %d\n",
  266. le32_to_cpu(cmd.rx_data_timeout));
  267. pos += scnprintf(buf+pos, bufsz-pos, "tx_data_timeout = %d\n",
  268. le32_to_cpu(cmd.tx_data_timeout));
  269. if (cmd.flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
  270. pos += scnprintf(buf+pos, bufsz-pos,
  271. "lprx_rssi_threshold = %d\n",
  272. le32_to_cpu(cmd.lprx_rssi_threshold));
  273. }
  274. return pos;
  275. }
  276. #endif
  277. const struct iwl_mvm_power_ops pm_legacy_ops = {
  278. .power_update_mode = iwl_mvm_power_legacy_update_mode,
  279. .power_disable = iwl_mvm_power_legacy_disable,
  280. #ifdef CONFIG_IWLWIFI_DEBUGFS
  281. .power_dbgfs_read = iwl_mvm_power_legacy_dbgfs_read,
  282. #endif
  283. };