power.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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 - 2014 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 - 2014 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 <net/mac80211.h>
  67. #include "iwl-debug.h"
  68. #include "mvm.h"
  69. #include "iwl-modparams.h"
  70. #include "fw-api-power.h"
  71. #define POWER_KEEP_ALIVE_PERIOD_SEC 25
  72. int iwl_mvm_beacon_filter_send_cmd(struct iwl_mvm *mvm,
  73. struct iwl_beacon_filter_cmd *cmd)
  74. {
  75. int ret;
  76. ret = iwl_mvm_send_cmd_pdu(mvm, REPLY_BEACON_FILTERING_CMD, CMD_SYNC,
  77. sizeof(struct iwl_beacon_filter_cmd), cmd);
  78. if (!ret) {
  79. IWL_DEBUG_POWER(mvm, "ba_enable_beacon_abort is: %d\n",
  80. le32_to_cpu(cmd->ba_enable_beacon_abort));
  81. IWL_DEBUG_POWER(mvm, "ba_escape_timer is: %d\n",
  82. le32_to_cpu(cmd->ba_escape_timer));
  83. IWL_DEBUG_POWER(mvm, "bf_debug_flag is: %d\n",
  84. le32_to_cpu(cmd->bf_debug_flag));
  85. IWL_DEBUG_POWER(mvm, "bf_enable_beacon_filter is: %d\n",
  86. le32_to_cpu(cmd->bf_enable_beacon_filter));
  87. IWL_DEBUG_POWER(mvm, "bf_energy_delta is: %d\n",
  88. le32_to_cpu(cmd->bf_energy_delta));
  89. IWL_DEBUG_POWER(mvm, "bf_escape_timer is: %d\n",
  90. le32_to_cpu(cmd->bf_escape_timer));
  91. IWL_DEBUG_POWER(mvm, "bf_roaming_energy_delta is: %d\n",
  92. le32_to_cpu(cmd->bf_roaming_energy_delta));
  93. IWL_DEBUG_POWER(mvm, "bf_roaming_state is: %d\n",
  94. le32_to_cpu(cmd->bf_roaming_state));
  95. IWL_DEBUG_POWER(mvm, "bf_temp_threshold is: %d\n",
  96. le32_to_cpu(cmd->bf_temp_threshold));
  97. IWL_DEBUG_POWER(mvm, "bf_temp_fast_filter is: %d\n",
  98. le32_to_cpu(cmd->bf_temp_fast_filter));
  99. IWL_DEBUG_POWER(mvm, "bf_temp_slow_filter is: %d\n",
  100. le32_to_cpu(cmd->bf_temp_slow_filter));
  101. }
  102. return ret;
  103. }
  104. static
  105. void iwl_mvm_beacon_filter_set_cqm_params(struct iwl_mvm *mvm,
  106. struct ieee80211_vif *vif,
  107. struct iwl_beacon_filter_cmd *cmd)
  108. {
  109. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  110. if (vif->bss_conf.cqm_rssi_thold) {
  111. cmd->bf_energy_delta =
  112. cpu_to_le32(vif->bss_conf.cqm_rssi_hyst);
  113. /* fw uses an absolute value for this */
  114. cmd->bf_roaming_state =
  115. cpu_to_le32(-vif->bss_conf.cqm_rssi_thold);
  116. }
  117. cmd->ba_enable_beacon_abort = cpu_to_le32(mvmvif->bf_data.ba_enabled);
  118. }
  119. int iwl_mvm_update_beacon_abort(struct iwl_mvm *mvm,
  120. struct ieee80211_vif *vif, bool enable)
  121. {
  122. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  123. struct iwl_beacon_filter_cmd cmd = {
  124. IWL_BF_CMD_CONFIG_DEFAULTS,
  125. .bf_enable_beacon_filter = cpu_to_le32(1),
  126. .ba_enable_beacon_abort = cpu_to_le32(enable),
  127. };
  128. if (!mvmvif->bf_data.bf_enabled)
  129. return 0;
  130. if (mvm->cur_ucode == IWL_UCODE_WOWLAN)
  131. cmd.ba_escape_timer = cpu_to_le32(IWL_BA_ESCAPE_TIMER_D3);
  132. mvmvif->bf_data.ba_enabled = enable;
  133. iwl_mvm_beacon_filter_set_cqm_params(mvm, vif, &cmd);
  134. iwl_mvm_beacon_filter_debugfs_parameters(vif, &cmd);
  135. return iwl_mvm_beacon_filter_send_cmd(mvm, &cmd);
  136. }
  137. static void iwl_mvm_power_log(struct iwl_mvm *mvm,
  138. struct iwl_mac_power_cmd *cmd)
  139. {
  140. IWL_DEBUG_POWER(mvm,
  141. "Sending power table command on mac id 0x%X for power level %d, flags = 0x%X\n",
  142. cmd->id_and_color, iwlmvm_mod_params.power_scheme,
  143. le16_to_cpu(cmd->flags));
  144. IWL_DEBUG_POWER(mvm, "Keep alive = %u sec\n",
  145. le16_to_cpu(cmd->keep_alive_seconds));
  146. if (!(cmd->flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK))) {
  147. IWL_DEBUG_POWER(mvm, "Disable power management\n");
  148. return;
  149. }
  150. IWL_DEBUG_POWER(mvm, "Rx timeout = %u usec\n",
  151. le32_to_cpu(cmd->rx_data_timeout));
  152. IWL_DEBUG_POWER(mvm, "Tx timeout = %u usec\n",
  153. le32_to_cpu(cmd->tx_data_timeout));
  154. if (cmd->flags & cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK))
  155. IWL_DEBUG_POWER(mvm, "DTIM periods to skip = %u\n",
  156. cmd->skip_dtim_periods);
  157. if (cmd->flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
  158. IWL_DEBUG_POWER(mvm, "LP RX RSSI threshold = %u\n",
  159. cmd->lprx_rssi_threshold);
  160. if (cmd->flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK)) {
  161. IWL_DEBUG_POWER(mvm, "uAPSD enabled\n");
  162. IWL_DEBUG_POWER(mvm, "Rx timeout (uAPSD) = %u usec\n",
  163. le32_to_cpu(cmd->rx_data_timeout_uapsd));
  164. IWL_DEBUG_POWER(mvm, "Tx timeout (uAPSD) = %u usec\n",
  165. le32_to_cpu(cmd->tx_data_timeout_uapsd));
  166. IWL_DEBUG_POWER(mvm, "QNDP TID = %d\n", cmd->qndp_tid);
  167. IWL_DEBUG_POWER(mvm, "ACs flags = 0x%x\n", cmd->uapsd_ac_flags);
  168. IWL_DEBUG_POWER(mvm, "Max SP = %d\n", cmd->uapsd_max_sp);
  169. }
  170. }
  171. static void iwl_mvm_power_configure_uapsd(struct iwl_mvm *mvm,
  172. struct ieee80211_vif *vif,
  173. struct iwl_mac_power_cmd *cmd)
  174. {
  175. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  176. enum ieee80211_ac_numbers ac;
  177. bool tid_found = false;
  178. for (ac = IEEE80211_AC_VO; ac <= IEEE80211_AC_BK; ac++) {
  179. if (!mvmvif->queue_params[ac].uapsd)
  180. continue;
  181. if (mvm->cur_ucode != IWL_UCODE_WOWLAN)
  182. cmd->flags |=
  183. cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK);
  184. cmd->uapsd_ac_flags |= BIT(ac);
  185. /* QNDP TID - the highest TID with no admission control */
  186. if (!tid_found && !mvmvif->queue_params[ac].acm) {
  187. tid_found = true;
  188. switch (ac) {
  189. case IEEE80211_AC_VO:
  190. cmd->qndp_tid = 6;
  191. break;
  192. case IEEE80211_AC_VI:
  193. cmd->qndp_tid = 5;
  194. break;
  195. case IEEE80211_AC_BE:
  196. cmd->qndp_tid = 0;
  197. break;
  198. case IEEE80211_AC_BK:
  199. cmd->qndp_tid = 1;
  200. break;
  201. }
  202. }
  203. }
  204. if (!(cmd->flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK)))
  205. return;
  206. cmd->flags |= cpu_to_le16(POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK);
  207. if (cmd->uapsd_ac_flags == (BIT(IEEE80211_AC_VO) |
  208. BIT(IEEE80211_AC_VI) |
  209. BIT(IEEE80211_AC_BE) |
  210. BIT(IEEE80211_AC_BK))) {
  211. cmd->flags |= cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK);
  212. cmd->snooze_interval = cpu_to_le16(IWL_MVM_PS_SNOOZE_INTERVAL);
  213. cmd->snooze_window = (mvm->cur_ucode == IWL_UCODE_WOWLAN) ?
  214. cpu_to_le16(IWL_MVM_WOWLAN_PS_SNOOZE_WINDOW) :
  215. cpu_to_le16(IWL_MVM_PS_SNOOZE_WINDOW);
  216. }
  217. cmd->uapsd_max_sp = IWL_UAPSD_MAX_SP;
  218. if (mvm->cur_ucode == IWL_UCODE_WOWLAN || cmd->flags &
  219. cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)) {
  220. cmd->rx_data_timeout_uapsd =
  221. cpu_to_le32(IWL_MVM_WOWLAN_PS_RX_DATA_TIMEOUT);
  222. cmd->tx_data_timeout_uapsd =
  223. cpu_to_le32(IWL_MVM_WOWLAN_PS_TX_DATA_TIMEOUT);
  224. } else {
  225. cmd->rx_data_timeout_uapsd =
  226. cpu_to_le32(IWL_MVM_UAPSD_RX_DATA_TIMEOUT);
  227. cmd->tx_data_timeout_uapsd =
  228. cpu_to_le32(IWL_MVM_UAPSD_TX_DATA_TIMEOUT);
  229. }
  230. if (cmd->flags & cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)) {
  231. cmd->heavy_tx_thld_packets =
  232. IWL_MVM_PS_SNOOZE_HEAVY_TX_THLD_PACKETS;
  233. cmd->heavy_rx_thld_packets =
  234. IWL_MVM_PS_SNOOZE_HEAVY_RX_THLD_PACKETS;
  235. } else {
  236. cmd->heavy_tx_thld_packets =
  237. IWL_MVM_PS_HEAVY_TX_THLD_PACKETS;
  238. cmd->heavy_rx_thld_packets =
  239. IWL_MVM_PS_HEAVY_RX_THLD_PACKETS;
  240. }
  241. cmd->heavy_tx_thld_percentage =
  242. IWL_MVM_PS_HEAVY_TX_THLD_PERCENT;
  243. cmd->heavy_rx_thld_percentage =
  244. IWL_MVM_PS_HEAVY_RX_THLD_PERCENT;
  245. }
  246. static void iwl_mvm_power_build_cmd(struct iwl_mvm *mvm,
  247. struct ieee80211_vif *vif,
  248. struct iwl_mac_power_cmd *cmd)
  249. {
  250. struct ieee80211_hw *hw = mvm->hw;
  251. struct ieee80211_chanctx_conf *chanctx_conf;
  252. struct ieee80211_channel *chan;
  253. int dtimper, dtimper_msec;
  254. int keep_alive;
  255. bool radar_detect = false;
  256. struct iwl_mvm_vif *mvmvif __maybe_unused =
  257. iwl_mvm_vif_from_mac80211(vif);
  258. bool allow_uapsd = true;
  259. cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  260. mvmvif->color));
  261. dtimper = hw->conf.ps_dtim_period ?: 1;
  262. /*
  263. * Regardless of power management state the driver must set
  264. * keep alive period. FW will use it for sending keep alive NDPs
  265. * immediately after association. Check that keep alive period
  266. * is at least 3 * DTIM
  267. */
  268. dtimper_msec = dtimper * vif->bss_conf.beacon_int;
  269. keep_alive = max_t(int, 3 * dtimper_msec,
  270. MSEC_PER_SEC * POWER_KEEP_ALIVE_PERIOD_SEC);
  271. keep_alive = DIV_ROUND_UP(keep_alive, MSEC_PER_SEC);
  272. cmd->keep_alive_seconds = cpu_to_le16(keep_alive);
  273. if (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM ||
  274. mvm->ps_prevented)
  275. return;
  276. cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK);
  277. #ifdef CONFIG_IWLWIFI_DEBUGFS
  278. if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_DISABLE_POWER_OFF &&
  279. mvmvif->dbgfs_pm.disable_power_off)
  280. cmd->flags &= cpu_to_le16(~POWER_FLAGS_POWER_SAVE_ENA_MSK);
  281. #endif
  282. if (!vif->bss_conf.ps || mvmvif->pm_prevented)
  283. return;
  284. cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK);
  285. if (vif->bss_conf.beacon_rate &&
  286. (vif->bss_conf.beacon_rate->bitrate == 10 ||
  287. vif->bss_conf.beacon_rate->bitrate == 60)) {
  288. cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
  289. cmd->lprx_rssi_threshold = POWER_LPRX_RSSI_THRESHOLD;
  290. }
  291. /* Check if radar detection is required on current channel */
  292. rcu_read_lock();
  293. chanctx_conf = rcu_dereference(vif->chanctx_conf);
  294. WARN_ON(!chanctx_conf);
  295. if (chanctx_conf) {
  296. chan = chanctx_conf->def.chan;
  297. radar_detect = chan->flags & IEEE80211_CHAN_RADAR;
  298. }
  299. rcu_read_unlock();
  300. /* Check skip over DTIM conditions */
  301. if (!radar_detect && (dtimper <= 10) &&
  302. (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_LP ||
  303. mvm->cur_ucode == IWL_UCODE_WOWLAN)) {
  304. cmd->flags |= cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
  305. cmd->skip_dtim_periods = 3;
  306. }
  307. if (mvm->cur_ucode != IWL_UCODE_WOWLAN) {
  308. cmd->rx_data_timeout =
  309. cpu_to_le32(IWL_MVM_DEFAULT_PS_RX_DATA_TIMEOUT);
  310. cmd->tx_data_timeout =
  311. cpu_to_le32(IWL_MVM_DEFAULT_PS_TX_DATA_TIMEOUT);
  312. } else {
  313. cmd->rx_data_timeout =
  314. cpu_to_le32(IWL_MVM_WOWLAN_PS_RX_DATA_TIMEOUT);
  315. cmd->tx_data_timeout =
  316. cpu_to_le32(IWL_MVM_WOWLAN_PS_TX_DATA_TIMEOUT);
  317. }
  318. if (!memcmp(mvmvif->uapsd_misbehaving_bssid, vif->bss_conf.bssid,
  319. ETH_ALEN))
  320. allow_uapsd = false;
  321. if (vif->p2p &&
  322. !(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_P2P_PS_UAPSD))
  323. allow_uapsd = false;
  324. /*
  325. * Avoid using uAPSD if P2P client is associated to GO that uses
  326. * opportunistic power save. This is due to current FW limitation.
  327. */
  328. if (vif->p2p &&
  329. vif->bss_conf.p2p_noa_attr.oppps_ctwindow &
  330. IEEE80211_P2P_OPPPS_ENABLE_BIT)
  331. allow_uapsd = false;
  332. if (allow_uapsd)
  333. iwl_mvm_power_configure_uapsd(mvm, vif, cmd);
  334. #ifdef CONFIG_IWLWIFI_DEBUGFS
  335. if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_KEEP_ALIVE)
  336. cmd->keep_alive_seconds =
  337. cpu_to_le16(mvmvif->dbgfs_pm.keep_alive_seconds);
  338. if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_OVER_DTIM) {
  339. if (mvmvif->dbgfs_pm.skip_over_dtim)
  340. cmd->flags |=
  341. cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
  342. else
  343. cmd->flags &=
  344. cpu_to_le16(~POWER_FLAGS_SKIP_OVER_DTIM_MSK);
  345. }
  346. if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_RX_DATA_TIMEOUT)
  347. cmd->rx_data_timeout =
  348. cpu_to_le32(mvmvif->dbgfs_pm.rx_data_timeout);
  349. if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_TX_DATA_TIMEOUT)
  350. cmd->tx_data_timeout =
  351. cpu_to_le32(mvmvif->dbgfs_pm.tx_data_timeout);
  352. if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_DTIM_PERIODS)
  353. cmd->skip_dtim_periods = mvmvif->dbgfs_pm.skip_dtim_periods;
  354. if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_ENA) {
  355. if (mvmvif->dbgfs_pm.lprx_ena)
  356. cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
  357. else
  358. cmd->flags &= cpu_to_le16(~POWER_FLAGS_LPRX_ENA_MSK);
  359. }
  360. if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_RSSI_THRESHOLD)
  361. cmd->lprx_rssi_threshold = mvmvif->dbgfs_pm.lprx_rssi_threshold;
  362. if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SNOOZE_ENABLE) {
  363. if (mvmvif->dbgfs_pm.snooze_ena)
  364. cmd->flags |=
  365. cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK);
  366. else
  367. cmd->flags &=
  368. cpu_to_le16(~POWER_FLAGS_SNOOZE_ENA_MSK);
  369. }
  370. if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_UAPSD_MISBEHAVING) {
  371. u16 flag = POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK;
  372. if (mvmvif->dbgfs_pm.uapsd_misbehaving)
  373. cmd->flags |= cpu_to_le16(flag);
  374. else
  375. cmd->flags &= cpu_to_le16(flag);
  376. }
  377. #endif /* CONFIG_IWLWIFI_DEBUGFS */
  378. }
  379. static int iwl_mvm_power_mac_update_mode(struct iwl_mvm *mvm,
  380. struct ieee80211_vif *vif)
  381. {
  382. int ret;
  383. bool ba_enable;
  384. struct iwl_mac_power_cmd cmd = {};
  385. if (vif->type != NL80211_IFTYPE_STATION)
  386. return 0;
  387. if (vif->p2p &&
  388. !(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_P2P_PS))
  389. return 0;
  390. iwl_mvm_power_build_cmd(mvm, vif, &cmd);
  391. iwl_mvm_power_log(mvm, &cmd);
  392. ret = iwl_mvm_send_cmd_pdu(mvm, MAC_PM_POWER_TABLE, CMD_SYNC,
  393. sizeof(cmd), &cmd);
  394. if (ret)
  395. return ret;
  396. ba_enable = !!(cmd.flags &
  397. cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK));
  398. return iwl_mvm_update_beacon_abort(mvm, vif, ba_enable);
  399. }
  400. static int iwl_mvm_power_mac_disable(struct iwl_mvm *mvm,
  401. struct ieee80211_vif *vif)
  402. {
  403. struct iwl_mac_power_cmd cmd = {};
  404. struct iwl_mvm_vif *mvmvif __maybe_unused =
  405. iwl_mvm_vif_from_mac80211(vif);
  406. if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
  407. return 0;
  408. cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  409. mvmvif->color));
  410. if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
  411. cmd.flags |= cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK);
  412. #ifdef CONFIG_IWLWIFI_DEBUGFS
  413. if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_DISABLE_POWER_OFF &&
  414. mvmvif->dbgfs_pm.disable_power_off)
  415. cmd.flags &= cpu_to_le16(~POWER_FLAGS_POWER_SAVE_ENA_MSK);
  416. #endif
  417. iwl_mvm_power_log(mvm, &cmd);
  418. return iwl_mvm_send_cmd_pdu(mvm, MAC_PM_POWER_TABLE, CMD_ASYNC,
  419. sizeof(cmd), &cmd);
  420. }
  421. static int _iwl_mvm_power_update_device(struct iwl_mvm *mvm, bool force_disable)
  422. {
  423. struct iwl_device_power_cmd cmd = {
  424. .flags = cpu_to_le16(DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK),
  425. };
  426. if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_DEVICE_PS_CMD))
  427. return 0;
  428. if (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM ||
  429. force_disable)
  430. cmd.flags |= cpu_to_le16(DEVICE_POWER_FLAGS_CAM_MSK);
  431. #ifdef CONFIG_IWLWIFI_DEBUGFS
  432. if ((mvm->cur_ucode == IWL_UCODE_WOWLAN) ? mvm->disable_power_off_d3 :
  433. mvm->disable_power_off)
  434. cmd.flags &=
  435. cpu_to_le16(~DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK);
  436. #endif
  437. IWL_DEBUG_POWER(mvm,
  438. "Sending device power command with flags = 0x%X\n",
  439. cmd.flags);
  440. return iwl_mvm_send_cmd_pdu(mvm, POWER_TABLE_CMD, CMD_SYNC, sizeof(cmd),
  441. &cmd);
  442. }
  443. static int iwl_mvm_power_update_device(struct iwl_mvm *mvm)
  444. {
  445. return _iwl_mvm_power_update_device(mvm, false);
  446. }
  447. void iwl_mvm_power_vif_assoc(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  448. {
  449. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  450. if (memcmp(vif->bss_conf.bssid, mvmvif->uapsd_misbehaving_bssid,
  451. ETH_ALEN))
  452. memset(mvmvif->uapsd_misbehaving_bssid, 0, ETH_ALEN);
  453. }
  454. static void iwl_mvm_power_uapsd_misbehav_ap_iterator(void *_data, u8 *mac,
  455. struct ieee80211_vif *vif)
  456. {
  457. u8 *ap_sta_id = _data;
  458. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  459. /* The ap_sta_id is not expected to change during current association
  460. * so no explicit protection is needed
  461. */
  462. if (mvmvif->ap_sta_id == *ap_sta_id)
  463. memcpy(mvmvif->uapsd_misbehaving_bssid, vif->bss_conf.bssid,
  464. ETH_ALEN);
  465. }
  466. int iwl_mvm_power_uapsd_misbehaving_ap_notif(struct iwl_mvm *mvm,
  467. struct iwl_rx_cmd_buffer *rxb,
  468. struct iwl_device_cmd *cmd)
  469. {
  470. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  471. struct iwl_uapsd_misbehaving_ap_notif *notif = (void *)pkt->data;
  472. u8 ap_sta_id = le32_to_cpu(notif->sta_id);
  473. ieee80211_iterate_active_interfaces_atomic(
  474. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  475. iwl_mvm_power_uapsd_misbehav_ap_iterator, &ap_sta_id);
  476. return 0;
  477. }
  478. static void iwl_mvm_power_binding_iterator(void *_data, u8 *mac,
  479. struct ieee80211_vif *vif)
  480. {
  481. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  482. struct iwl_mvm *mvm = _data;
  483. int ret;
  484. mvmvif->pm_prevented = (mvm->bound_vif_cnt <= 1) ? false : true;
  485. ret = iwl_mvm_power_mac_update_mode(mvm, vif);
  486. WARN_ONCE(ret, "Failed to update power parameters on a specific vif\n");
  487. }
  488. static void _iwl_mvm_power_update_binding(struct iwl_mvm *mvm,
  489. struct ieee80211_vif *vif,
  490. bool assign)
  491. {
  492. if (vif->type == NL80211_IFTYPE_MONITOR) {
  493. int ret = _iwl_mvm_power_update_device(mvm, assign);
  494. mvm->ps_prevented = assign;
  495. WARN_ONCE(ret, "Failed to update power device state\n");
  496. }
  497. ieee80211_iterate_active_interfaces(mvm->hw,
  498. IEEE80211_IFACE_ITER_NORMAL,
  499. iwl_mvm_power_binding_iterator,
  500. mvm);
  501. }
  502. #ifdef CONFIG_IWLWIFI_DEBUGFS
  503. static int iwl_mvm_power_mac_dbgfs_read(struct iwl_mvm *mvm,
  504. struct ieee80211_vif *vif, char *buf,
  505. int bufsz)
  506. {
  507. struct iwl_mac_power_cmd cmd = {};
  508. int pos = 0;
  509. iwl_mvm_power_build_cmd(mvm, vif, &cmd);
  510. if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_DEVICE_PS_CMD))
  511. pos += scnprintf(buf+pos, bufsz-pos, "disable_power_off = %d\n",
  512. (cmd.flags &
  513. cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK)) ?
  514. 0 : 1);
  515. pos += scnprintf(buf+pos, bufsz-pos, "power_scheme = %d\n",
  516. iwlmvm_mod_params.power_scheme);
  517. pos += scnprintf(buf+pos, bufsz-pos, "flags = 0x%x\n",
  518. le16_to_cpu(cmd.flags));
  519. pos += scnprintf(buf+pos, bufsz-pos, "keep_alive = %d\n",
  520. le16_to_cpu(cmd.keep_alive_seconds));
  521. if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK)))
  522. return pos;
  523. pos += scnprintf(buf+pos, bufsz-pos, "skip_over_dtim = %d\n",
  524. (cmd.flags &
  525. cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK)) ? 1 : 0);
  526. pos += scnprintf(buf+pos, bufsz-pos, "skip_dtim_periods = %d\n",
  527. cmd.skip_dtim_periods);
  528. if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK))) {
  529. pos += scnprintf(buf+pos, bufsz-pos, "rx_data_timeout = %d\n",
  530. le32_to_cpu(cmd.rx_data_timeout));
  531. pos += scnprintf(buf+pos, bufsz-pos, "tx_data_timeout = %d\n",
  532. le32_to_cpu(cmd.tx_data_timeout));
  533. }
  534. if (cmd.flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
  535. pos += scnprintf(buf+pos, bufsz-pos,
  536. "lprx_rssi_threshold = %d\n",
  537. cmd.lprx_rssi_threshold);
  538. if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK)))
  539. return pos;
  540. pos += scnprintf(buf+pos, bufsz-pos, "rx_data_timeout_uapsd = %d\n",
  541. le32_to_cpu(cmd.rx_data_timeout_uapsd));
  542. pos += scnprintf(buf+pos, bufsz-pos, "tx_data_timeout_uapsd = %d\n",
  543. le32_to_cpu(cmd.tx_data_timeout_uapsd));
  544. pos += scnprintf(buf+pos, bufsz-pos, "qndp_tid = %d\n", cmd.qndp_tid);
  545. pos += scnprintf(buf+pos, bufsz-pos, "uapsd_ac_flags = 0x%x\n",
  546. cmd.uapsd_ac_flags);
  547. pos += scnprintf(buf+pos, bufsz-pos, "uapsd_max_sp = %d\n",
  548. cmd.uapsd_max_sp);
  549. pos += scnprintf(buf+pos, bufsz-pos, "heavy_tx_thld_packets = %d\n",
  550. cmd.heavy_tx_thld_packets);
  551. pos += scnprintf(buf+pos, bufsz-pos, "heavy_rx_thld_packets = %d\n",
  552. cmd.heavy_rx_thld_packets);
  553. pos += scnprintf(buf+pos, bufsz-pos, "heavy_tx_thld_percentage = %d\n",
  554. cmd.heavy_tx_thld_percentage);
  555. pos += scnprintf(buf+pos, bufsz-pos, "heavy_rx_thld_percentage = %d\n",
  556. cmd.heavy_rx_thld_percentage);
  557. pos += scnprintf(buf+pos, bufsz-pos, "uapsd_misbehaving_enable = %d\n",
  558. (cmd.flags &
  559. cpu_to_le16(POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK)) ?
  560. 1 : 0);
  561. if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)))
  562. return pos;
  563. pos += scnprintf(buf+pos, bufsz-pos, "snooze_interval = %d\n",
  564. cmd.snooze_interval);
  565. pos += scnprintf(buf+pos, bufsz-pos, "snooze_window = %d\n",
  566. cmd.snooze_window);
  567. return pos;
  568. }
  569. void
  570. iwl_mvm_beacon_filter_debugfs_parameters(struct ieee80211_vif *vif,
  571. struct iwl_beacon_filter_cmd *cmd)
  572. {
  573. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  574. struct iwl_dbgfs_bf *dbgfs_bf = &mvmvif->dbgfs_bf;
  575. if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ENERGY_DELTA)
  576. cmd->bf_energy_delta = cpu_to_le32(dbgfs_bf->bf_energy_delta);
  577. if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ROAMING_ENERGY_DELTA)
  578. cmd->bf_roaming_energy_delta =
  579. cpu_to_le32(dbgfs_bf->bf_roaming_energy_delta);
  580. if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ROAMING_STATE)
  581. cmd->bf_roaming_state = cpu_to_le32(dbgfs_bf->bf_roaming_state);
  582. if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_THRESHOLD)
  583. cmd->bf_temp_threshold =
  584. cpu_to_le32(dbgfs_bf->bf_temp_threshold);
  585. if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_FAST_FILTER)
  586. cmd->bf_temp_fast_filter =
  587. cpu_to_le32(dbgfs_bf->bf_temp_fast_filter);
  588. if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_SLOW_FILTER)
  589. cmd->bf_temp_slow_filter =
  590. cpu_to_le32(dbgfs_bf->bf_temp_slow_filter);
  591. if (dbgfs_bf->mask & MVM_DEBUGFS_BF_DEBUG_FLAG)
  592. cmd->bf_debug_flag = cpu_to_le32(dbgfs_bf->bf_debug_flag);
  593. if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ESCAPE_TIMER)
  594. cmd->bf_escape_timer = cpu_to_le32(dbgfs_bf->bf_escape_timer);
  595. if (dbgfs_bf->mask & MVM_DEBUGFS_BA_ESCAPE_TIMER)
  596. cmd->ba_escape_timer = cpu_to_le32(dbgfs_bf->ba_escape_timer);
  597. if (dbgfs_bf->mask & MVM_DEBUGFS_BA_ENABLE_BEACON_ABORT)
  598. cmd->ba_enable_beacon_abort =
  599. cpu_to_le32(dbgfs_bf->ba_enable_beacon_abort);
  600. }
  601. #endif
  602. int iwl_mvm_enable_beacon_filter(struct iwl_mvm *mvm,
  603. struct ieee80211_vif *vif)
  604. {
  605. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  606. struct iwl_beacon_filter_cmd cmd = {
  607. IWL_BF_CMD_CONFIG_DEFAULTS,
  608. .bf_enable_beacon_filter = cpu_to_le32(1),
  609. };
  610. int ret;
  611. if (mvmvif != mvm->bf_allowed_vif ||
  612. vif->type != NL80211_IFTYPE_STATION || vif->p2p)
  613. return 0;
  614. iwl_mvm_beacon_filter_set_cqm_params(mvm, vif, &cmd);
  615. iwl_mvm_beacon_filter_debugfs_parameters(vif, &cmd);
  616. ret = iwl_mvm_beacon_filter_send_cmd(mvm, &cmd);
  617. if (!ret)
  618. mvmvif->bf_data.bf_enabled = true;
  619. return ret;
  620. }
  621. int iwl_mvm_disable_beacon_filter(struct iwl_mvm *mvm,
  622. struct ieee80211_vif *vif)
  623. {
  624. struct iwl_beacon_filter_cmd cmd = {};
  625. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  626. int ret;
  627. if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BF_UPDATED) ||
  628. vif->type != NL80211_IFTYPE_STATION || vif->p2p)
  629. return 0;
  630. ret = iwl_mvm_beacon_filter_send_cmd(mvm, &cmd);
  631. if (!ret)
  632. mvmvif->bf_data.bf_enabled = false;
  633. return ret;
  634. }
  635. int iwl_mvm_update_beacon_filter(struct iwl_mvm *mvm,
  636. struct ieee80211_vif *vif)
  637. {
  638. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  639. if (!mvmvif->bf_data.bf_enabled)
  640. return 0;
  641. return iwl_mvm_enable_beacon_filter(mvm, vif);
  642. }
  643. const struct iwl_mvm_power_ops pm_mac_ops = {
  644. .power_update_mode = iwl_mvm_power_mac_update_mode,
  645. .power_update_device_mode = iwl_mvm_power_update_device,
  646. .power_disable = iwl_mvm_power_mac_disable,
  647. .power_update_binding = _iwl_mvm_power_update_binding,
  648. #ifdef CONFIG_IWLWIFI_DEBUGFS
  649. .power_dbgfs_read = iwl_mvm_power_mac_dbgfs_read,
  650. #endif
  651. };