1 /******************************************************************************
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.
8 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
25 * The full GNU General Public License is included in this distribution
26 * in the file called COPYING.
28 * Contact Information:
29 * Intel Linux Wireless <ilw@linux.intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
34 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
35 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
42 * * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * * Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in
46 * the documentation and/or other materials provided with the
48 * * Neither the name Intel Corporation nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 *****************************************************************************/
66 #include <linux/kernel.h>
67 #include <linux/module.h>
68 #include <linux/slab.h>
69 #include <linux/etherdevice.h>
71 #include <net/mac80211.h>
73 #include "iwl-debug.h"
75 #include "iwl-modparams.h"
76 #include "fw-api-power.h"
78 #define POWER_KEEP_ALIVE_PERIOD_SEC 25
81 int iwl_mvm_beacon_filter_send_cmd(struct iwl_mvm *mvm,
82 struct iwl_beacon_filter_cmd *cmd,
85 IWL_DEBUG_POWER(mvm, "ba_enable_beacon_abort is: %d\n",
86 le32_to_cpu(cmd->ba_enable_beacon_abort));
87 IWL_DEBUG_POWER(mvm, "ba_escape_timer is: %d\n",
88 le32_to_cpu(cmd->ba_escape_timer));
89 IWL_DEBUG_POWER(mvm, "bf_debug_flag is: %d\n",
90 le32_to_cpu(cmd->bf_debug_flag));
91 IWL_DEBUG_POWER(mvm, "bf_enable_beacon_filter is: %d\n",
92 le32_to_cpu(cmd->bf_enable_beacon_filter));
93 IWL_DEBUG_POWER(mvm, "bf_energy_delta is: %d\n",
94 le32_to_cpu(cmd->bf_energy_delta));
95 IWL_DEBUG_POWER(mvm, "bf_escape_timer is: %d\n",
96 le32_to_cpu(cmd->bf_escape_timer));
97 IWL_DEBUG_POWER(mvm, "bf_roaming_energy_delta is: %d\n",
98 le32_to_cpu(cmd->bf_roaming_energy_delta));
99 IWL_DEBUG_POWER(mvm, "bf_roaming_state is: %d\n",
100 le32_to_cpu(cmd->bf_roaming_state));
101 IWL_DEBUG_POWER(mvm, "bf_temp_threshold is: %d\n",
102 le32_to_cpu(cmd->bf_temp_threshold));
103 IWL_DEBUG_POWER(mvm, "bf_temp_fast_filter is: %d\n",
104 le32_to_cpu(cmd->bf_temp_fast_filter));
105 IWL_DEBUG_POWER(mvm, "bf_temp_slow_filter is: %d\n",
106 le32_to_cpu(cmd->bf_temp_slow_filter));
108 return iwl_mvm_send_cmd_pdu(mvm, REPLY_BEACON_FILTERING_CMD, flags,
109 sizeof(struct iwl_beacon_filter_cmd), cmd);
113 void iwl_mvm_beacon_filter_set_cqm_params(struct iwl_mvm *mvm,
114 struct ieee80211_vif *vif,
115 struct iwl_beacon_filter_cmd *cmd,
118 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
120 if (vif->bss_conf.cqm_rssi_thold && !d0i3) {
121 cmd->bf_energy_delta =
122 cpu_to_le32(vif->bss_conf.cqm_rssi_hyst);
123 /* fw uses an absolute value for this */
124 cmd->bf_roaming_state =
125 cpu_to_le32(-vif->bss_conf.cqm_rssi_thold);
127 cmd->ba_enable_beacon_abort = cpu_to_le32(mvmvif->bf_data.ba_enabled);
130 static void iwl_mvm_power_log(struct iwl_mvm *mvm,
131 struct iwl_mac_power_cmd *cmd)
134 "Sending power table command on mac id 0x%X for power level %d, flags = 0x%X\n",
135 cmd->id_and_color, iwlmvm_mod_params.power_scheme,
136 le16_to_cpu(cmd->flags));
137 IWL_DEBUG_POWER(mvm, "Keep alive = %u sec\n",
138 le16_to_cpu(cmd->keep_alive_seconds));
140 if (!(cmd->flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK))) {
141 IWL_DEBUG_POWER(mvm, "Disable power management\n");
145 IWL_DEBUG_POWER(mvm, "Rx timeout = %u usec\n",
146 le32_to_cpu(cmd->rx_data_timeout));
147 IWL_DEBUG_POWER(mvm, "Tx timeout = %u usec\n",
148 le32_to_cpu(cmd->tx_data_timeout));
149 if (cmd->flags & cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK))
150 IWL_DEBUG_POWER(mvm, "DTIM periods to skip = %u\n",
151 cmd->skip_dtim_periods);
152 if (cmd->flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
153 IWL_DEBUG_POWER(mvm, "LP RX RSSI threshold = %u\n",
154 cmd->lprx_rssi_threshold);
155 if (cmd->flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK)) {
156 IWL_DEBUG_POWER(mvm, "uAPSD enabled\n");
157 IWL_DEBUG_POWER(mvm, "Rx timeout (uAPSD) = %u usec\n",
158 le32_to_cpu(cmd->rx_data_timeout_uapsd));
159 IWL_DEBUG_POWER(mvm, "Tx timeout (uAPSD) = %u usec\n",
160 le32_to_cpu(cmd->tx_data_timeout_uapsd));
161 IWL_DEBUG_POWER(mvm, "QNDP TID = %d\n", cmd->qndp_tid);
162 IWL_DEBUG_POWER(mvm, "ACs flags = 0x%x\n", cmd->uapsd_ac_flags);
163 IWL_DEBUG_POWER(mvm, "Max SP = %d\n", cmd->uapsd_max_sp);
167 static void iwl_mvm_power_configure_uapsd(struct iwl_mvm *mvm,
168 struct ieee80211_vif *vif,
169 struct iwl_mac_power_cmd *cmd)
171 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
172 enum ieee80211_ac_numbers ac;
173 bool tid_found = false;
175 for (ac = IEEE80211_AC_VO; ac <= IEEE80211_AC_BK; ac++) {
176 if (!mvmvif->queue_params[ac].uapsd)
179 if (mvm->cur_ucode != IWL_UCODE_WOWLAN)
181 cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK);
183 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) {
189 case IEEE80211_AC_VO:
192 case IEEE80211_AC_VI:
195 case IEEE80211_AC_BE:
198 case IEEE80211_AC_BK:
205 if (!(cmd->flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK))) {
206 #ifdef CONFIG_IWLWIFI_DEBUGFS
207 /* set advanced pm flag with no uapsd ACs to enable ps-poll */
208 if (mvmvif->dbgfs_pm.use_ps_poll)
210 cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK);
215 cmd->flags |= cpu_to_le16(POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK);
217 if (cmd->uapsd_ac_flags == (BIT(IEEE80211_AC_VO) |
218 BIT(IEEE80211_AC_VI) |
219 BIT(IEEE80211_AC_BE) |
220 BIT(IEEE80211_AC_BK))) {
221 cmd->flags |= cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK);
222 cmd->snooze_interval = cpu_to_le16(IWL_MVM_PS_SNOOZE_INTERVAL);
223 cmd->snooze_window = (mvm->cur_ucode == IWL_UCODE_WOWLAN) ?
224 cpu_to_le16(IWL_MVM_WOWLAN_PS_SNOOZE_WINDOW) :
225 cpu_to_le16(IWL_MVM_PS_SNOOZE_WINDOW);
228 cmd->uapsd_max_sp = IWL_UAPSD_MAX_SP;
230 if (mvm->cur_ucode == IWL_UCODE_WOWLAN || cmd->flags &
231 cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)) {
232 cmd->rx_data_timeout_uapsd =
233 cpu_to_le32(IWL_MVM_WOWLAN_PS_RX_DATA_TIMEOUT);
234 cmd->tx_data_timeout_uapsd =
235 cpu_to_le32(IWL_MVM_WOWLAN_PS_TX_DATA_TIMEOUT);
237 cmd->rx_data_timeout_uapsd =
238 cpu_to_le32(IWL_MVM_UAPSD_RX_DATA_TIMEOUT);
239 cmd->tx_data_timeout_uapsd =
240 cpu_to_le32(IWL_MVM_UAPSD_TX_DATA_TIMEOUT);
243 if (cmd->flags & cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)) {
244 cmd->heavy_tx_thld_packets =
245 IWL_MVM_PS_SNOOZE_HEAVY_TX_THLD_PACKETS;
246 cmd->heavy_rx_thld_packets =
247 IWL_MVM_PS_SNOOZE_HEAVY_RX_THLD_PACKETS;
249 cmd->heavy_tx_thld_packets =
250 IWL_MVM_PS_HEAVY_TX_THLD_PACKETS;
251 cmd->heavy_rx_thld_packets =
252 IWL_MVM_PS_HEAVY_RX_THLD_PACKETS;
254 cmd->heavy_tx_thld_percentage =
255 IWL_MVM_PS_HEAVY_TX_THLD_PERCENT;
256 cmd->heavy_rx_thld_percentage =
257 IWL_MVM_PS_HEAVY_RX_THLD_PERCENT;
260 static bool iwl_mvm_power_allow_uapsd(struct iwl_mvm *mvm,
261 struct ieee80211_vif *vif)
263 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
265 if (!memcmp(mvmvif->uapsd_misbehaving_bssid, vif->bss_conf.bssid,
270 !(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_P2P_PS_UAPSD))
273 * Avoid using uAPSD if P2P client is associated to GO that uses
274 * opportunistic power save. This is due to current FW limitation.
277 (vif->bss_conf.p2p_noa_attr.oppps_ctwindow &
278 IEEE80211_P2P_OPPPS_ENABLE_BIT))
282 * Avoid using uAPSD if client is in DCM -
283 * low latency issue in Miracast
285 if (iwl_mvm_phy_ctx_count(mvm) >= 2)
291 static bool iwl_mvm_power_is_radar(struct ieee80211_vif *vif)
293 struct ieee80211_chanctx_conf *chanctx_conf;
294 struct ieee80211_channel *chan;
295 bool radar_detect = false;
298 chanctx_conf = rcu_dereference(vif->chanctx_conf);
299 WARN_ON(!chanctx_conf);
301 chan = chanctx_conf->def.chan;
302 radar_detect = chan->flags & IEEE80211_CHAN_RADAR;
309 static void iwl_mvm_power_build_cmd(struct iwl_mvm *mvm,
310 struct ieee80211_vif *vif,
311 struct iwl_mac_power_cmd *cmd)
315 bool radar_detect = false;
316 struct iwl_mvm_vif *mvmvif __maybe_unused =
317 iwl_mvm_vif_from_mac80211(vif);
319 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
321 dtimper = vif->bss_conf.dtim_period;
322 bi = vif->bss_conf.beacon_int;
325 * Regardless of power management state the driver must set
326 * keep alive period. FW will use it for sending keep alive NDPs
327 * immediately after association. Check that keep alive period
328 * is at least 3 * DTIM
330 keep_alive = DIV_ROUND_UP(ieee80211_tu_to_usec(3 * dtimper * bi),
332 keep_alive = max(keep_alive, POWER_KEEP_ALIVE_PERIOD_SEC);
333 cmd->keep_alive_seconds = cpu_to_le16(keep_alive);
335 if (mvm->ps_disabled)
338 cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK);
340 if (!vif->bss_conf.ps || !mvmvif->pm_enabled ||
341 (iwl_mvm_vif_low_latency(mvmvif) && vif->p2p))
344 cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK);
346 if (vif->bss_conf.beacon_rate &&
347 (vif->bss_conf.beacon_rate->bitrate == 10 ||
348 vif->bss_conf.beacon_rate->bitrate == 60)) {
349 cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
350 cmd->lprx_rssi_threshold = POWER_LPRX_RSSI_THRESHOLD;
353 /* Check if radar detection is required on current channel */
354 radar_detect = iwl_mvm_power_is_radar(vif);
356 /* Check skip over DTIM conditions */
357 if (!radar_detect && (dtimper < 10) &&
358 (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_LP ||
359 mvm->cur_ucode == IWL_UCODE_WOWLAN)) {
360 cmd->flags |= cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
361 cmd->skip_dtim_periods = 3;
364 if (mvm->cur_ucode != IWL_UCODE_WOWLAN) {
365 cmd->rx_data_timeout =
366 cpu_to_le32(IWL_MVM_DEFAULT_PS_RX_DATA_TIMEOUT);
367 cmd->tx_data_timeout =
368 cpu_to_le32(IWL_MVM_DEFAULT_PS_TX_DATA_TIMEOUT);
370 cmd->rx_data_timeout =
371 cpu_to_le32(IWL_MVM_WOWLAN_PS_RX_DATA_TIMEOUT);
372 cmd->tx_data_timeout =
373 cpu_to_le32(IWL_MVM_WOWLAN_PS_TX_DATA_TIMEOUT);
376 if (iwl_mvm_power_allow_uapsd(mvm, vif))
377 iwl_mvm_power_configure_uapsd(mvm, vif, cmd);
379 #ifdef CONFIG_IWLWIFI_DEBUGFS
380 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_KEEP_ALIVE)
381 cmd->keep_alive_seconds =
382 cpu_to_le16(mvmvif->dbgfs_pm.keep_alive_seconds);
383 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_OVER_DTIM) {
384 if (mvmvif->dbgfs_pm.skip_over_dtim)
386 cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
389 cpu_to_le16(~POWER_FLAGS_SKIP_OVER_DTIM_MSK);
391 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_RX_DATA_TIMEOUT)
392 cmd->rx_data_timeout =
393 cpu_to_le32(mvmvif->dbgfs_pm.rx_data_timeout);
394 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_TX_DATA_TIMEOUT)
395 cmd->tx_data_timeout =
396 cpu_to_le32(mvmvif->dbgfs_pm.tx_data_timeout);
397 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_DTIM_PERIODS)
398 cmd->skip_dtim_periods = mvmvif->dbgfs_pm.skip_dtim_periods;
399 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_ENA) {
400 if (mvmvif->dbgfs_pm.lprx_ena)
401 cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
403 cmd->flags &= cpu_to_le16(~POWER_FLAGS_LPRX_ENA_MSK);
405 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_RSSI_THRESHOLD)
406 cmd->lprx_rssi_threshold = mvmvif->dbgfs_pm.lprx_rssi_threshold;
407 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SNOOZE_ENABLE) {
408 if (mvmvif->dbgfs_pm.snooze_ena)
410 cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK);
413 cpu_to_le16(~POWER_FLAGS_SNOOZE_ENA_MSK);
415 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_UAPSD_MISBEHAVING) {
416 u16 flag = POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK;
417 if (mvmvif->dbgfs_pm.uapsd_misbehaving)
418 cmd->flags |= cpu_to_le16(flag);
420 cmd->flags &= cpu_to_le16(flag);
422 #endif /* CONFIG_IWLWIFI_DEBUGFS */
425 static int iwl_mvm_power_send_cmd(struct iwl_mvm *mvm,
426 struct ieee80211_vif *vif)
428 struct iwl_mac_power_cmd cmd = {};
430 iwl_mvm_power_build_cmd(mvm, vif, &cmd);
431 iwl_mvm_power_log(mvm, &cmd);
432 #ifdef CONFIG_IWLWIFI_DEBUGFS
433 memcpy(&iwl_mvm_vif_from_mac80211(vif)->mac_pwr_cmd, &cmd, sizeof(cmd));
436 return iwl_mvm_send_cmd_pdu(mvm, MAC_PM_POWER_TABLE, 0,
440 int iwl_mvm_power_update_device(struct iwl_mvm *mvm)
442 struct iwl_device_power_cmd cmd = {
443 .flags = cpu_to_le16(DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK),
446 if (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM)
447 mvm->ps_disabled = true;
449 if (mvm->ps_disabled)
450 cmd.flags |= cpu_to_le16(DEVICE_POWER_FLAGS_CAM_MSK);
452 #ifdef CONFIG_IWLWIFI_DEBUGFS
453 if ((mvm->cur_ucode == IWL_UCODE_WOWLAN) ? mvm->disable_power_off_d3 :
454 mvm->disable_power_off)
456 cpu_to_le16(~DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK);
459 "Sending device power command with flags = 0x%X\n",
462 return iwl_mvm_send_cmd_pdu(mvm, POWER_TABLE_CMD, 0, sizeof(cmd),
466 void iwl_mvm_power_vif_assoc(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
468 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
470 if (memcmp(vif->bss_conf.bssid, mvmvif->uapsd_misbehaving_bssid,
472 eth_zero_addr(mvmvif->uapsd_misbehaving_bssid);
475 static void iwl_mvm_power_uapsd_misbehav_ap_iterator(void *_data, u8 *mac,
476 struct ieee80211_vif *vif)
478 u8 *ap_sta_id = _data;
479 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
481 /* The ap_sta_id is not expected to change during current association
482 * so no explicit protection is needed
484 if (mvmvif->ap_sta_id == *ap_sta_id)
485 memcpy(mvmvif->uapsd_misbehaving_bssid, vif->bss_conf.bssid,
489 void iwl_mvm_power_uapsd_misbehaving_ap_notif(struct iwl_mvm *mvm,
490 struct iwl_rx_cmd_buffer *rxb)
492 struct iwl_rx_packet *pkt = rxb_addr(rxb);
493 struct iwl_uapsd_misbehaving_ap_notif *notif = (void *)pkt->data;
494 u8 ap_sta_id = le32_to_cpu(notif->sta_id);
496 ieee80211_iterate_active_interfaces_atomic(
497 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
498 iwl_mvm_power_uapsd_misbehav_ap_iterator, &ap_sta_id);
501 struct iwl_power_vifs {
503 struct ieee80211_vif *bf_vif;
504 struct ieee80211_vif *bss_vif;
505 struct ieee80211_vif *p2p_vif;
506 struct ieee80211_vif *ap_vif;
507 struct ieee80211_vif *monitor_vif;
514 static void iwl_mvm_power_disable_pm_iterator(void *_data, u8* mac,
515 struct ieee80211_vif *vif)
517 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
519 mvmvif->pm_enabled = false;
522 static void iwl_mvm_power_ps_disabled_iterator(void *_data, u8* mac,
523 struct ieee80211_vif *vif)
525 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
526 bool *disable_ps = _data;
528 if (mvmvif->phy_ctxt)
529 if (mvmvif->phy_ctxt->id < MAX_PHYS)
530 *disable_ps |= mvmvif->ps_disabled;
533 static void iwl_mvm_power_get_vifs_iterator(void *_data, u8 *mac,
534 struct ieee80211_vif *vif)
536 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
537 struct iwl_power_vifs *power_iterator = _data;
539 switch (ieee80211_vif_type_p2p(vif)) {
540 case NL80211_IFTYPE_P2P_DEVICE:
543 case NL80211_IFTYPE_P2P_GO:
544 case NL80211_IFTYPE_AP:
545 /* only a single MAC of the same type */
546 WARN_ON(power_iterator->ap_vif);
547 power_iterator->ap_vif = vif;
548 if (mvmvif->phy_ctxt)
549 if (mvmvif->phy_ctxt->id < MAX_PHYS)
550 power_iterator->ap_active = true;
553 case NL80211_IFTYPE_MONITOR:
554 /* only a single MAC of the same type */
555 WARN_ON(power_iterator->monitor_vif);
556 power_iterator->monitor_vif = vif;
557 if (mvmvif->phy_ctxt)
558 if (mvmvif->phy_ctxt->id < MAX_PHYS)
559 power_iterator->monitor_active = true;
562 case NL80211_IFTYPE_P2P_CLIENT:
563 /* only a single MAC of the same type */
564 WARN_ON(power_iterator->p2p_vif);
565 power_iterator->p2p_vif = vif;
566 if (mvmvif->phy_ctxt)
567 if (mvmvif->phy_ctxt->id < MAX_PHYS)
568 power_iterator->p2p_active = true;
571 case NL80211_IFTYPE_STATION:
572 /* only a single MAC of the same type */
573 WARN_ON(power_iterator->bss_vif);
574 power_iterator->bss_vif = vif;
575 if (mvmvif->phy_ctxt)
576 if (mvmvif->phy_ctxt->id < MAX_PHYS)
577 power_iterator->bss_active = true;
579 if (mvmvif->bf_data.bf_enabled &&
580 !WARN_ON(power_iterator->bf_vif))
581 power_iterator->bf_vif = vif;
590 static void iwl_mvm_power_set_pm(struct iwl_mvm *mvm,
591 struct iwl_power_vifs *vifs)
593 struct iwl_mvm_vif *bss_mvmvif = NULL;
594 struct iwl_mvm_vif *p2p_mvmvif = NULL;
595 struct iwl_mvm_vif *ap_mvmvif = NULL;
596 bool client_same_channel = false;
597 bool ap_same_channel = false;
599 lockdep_assert_held(&mvm->mutex);
601 /* set pm_enable to false */
602 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
603 IEEE80211_IFACE_ITER_NORMAL,
604 iwl_mvm_power_disable_pm_iterator,
608 bss_mvmvif = iwl_mvm_vif_from_mac80211(vifs->bss_vif);
611 p2p_mvmvif = iwl_mvm_vif_from_mac80211(vifs->p2p_vif);
614 ap_mvmvif = iwl_mvm_vif_from_mac80211(vifs->ap_vif);
616 /* don't allow PM if any TDLS stations exist */
617 if (iwl_mvm_tdls_sta_count(mvm, NULL))
620 /* enable PM on bss if bss stand alone */
621 if (vifs->bss_active && !vifs->p2p_active && !vifs->ap_active) {
622 bss_mvmvif->pm_enabled = true;
626 /* enable PM on p2p if p2p stand alone */
627 if (vifs->p2p_active && !vifs->bss_active && !vifs->ap_active) {
628 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_P2P_PM)
629 p2p_mvmvif->pm_enabled = true;
633 if (vifs->bss_active && vifs->p2p_active)
634 client_same_channel = (bss_mvmvif->phy_ctxt->id ==
635 p2p_mvmvif->phy_ctxt->id);
636 if (vifs->bss_active && vifs->ap_active)
637 ap_same_channel = (bss_mvmvif->phy_ctxt->id ==
638 ap_mvmvif->phy_ctxt->id);
640 /* clients are not stand alone: enable PM if DCM */
641 if (!(client_same_channel || ap_same_channel) &&
642 (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BSS_P2P_PS_DCM)) {
643 if (vifs->bss_active)
644 bss_mvmvif->pm_enabled = true;
645 if (vifs->p2p_active &&
646 (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_P2P_PM))
647 p2p_mvmvif->pm_enabled = true;
652 * There is only one channel in the system and there are only
653 * bss and p2p clients that share it
655 if (client_same_channel && !vifs->ap_active &&
656 (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BSS_P2P_PS_SCM)) {
657 /* share same channel*/
658 bss_mvmvif->pm_enabled = true;
659 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_P2P_PM)
660 p2p_mvmvif->pm_enabled = true;
664 #ifdef CONFIG_IWLWIFI_DEBUGFS
665 int iwl_mvm_power_mac_dbgfs_read(struct iwl_mvm *mvm,
666 struct ieee80211_vif *vif, char *buf,
669 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
670 struct iwl_mac_power_cmd cmd = {};
673 mutex_lock(&mvm->mutex);
674 memcpy(&cmd, &mvmvif->mac_pwr_cmd, sizeof(cmd));
675 mutex_unlock(&mvm->mutex);
677 pos += scnprintf(buf+pos, bufsz-pos, "power_scheme = %d\n",
678 iwlmvm_mod_params.power_scheme);
679 pos += scnprintf(buf+pos, bufsz-pos, "flags = 0x%x\n",
680 le16_to_cpu(cmd.flags));
681 pos += scnprintf(buf+pos, bufsz-pos, "keep_alive = %d\n",
682 le16_to_cpu(cmd.keep_alive_seconds));
684 if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK)))
687 pos += scnprintf(buf+pos, bufsz-pos, "skip_over_dtim = %d\n",
689 cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK)) ? 1 : 0);
690 pos += scnprintf(buf+pos, bufsz-pos, "skip_dtim_periods = %d\n",
691 cmd.skip_dtim_periods);
692 if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK))) {
693 pos += scnprintf(buf+pos, bufsz-pos, "rx_data_timeout = %d\n",
694 le32_to_cpu(cmd.rx_data_timeout));
695 pos += scnprintf(buf+pos, bufsz-pos, "tx_data_timeout = %d\n",
696 le32_to_cpu(cmd.tx_data_timeout));
698 if (cmd.flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
699 pos += scnprintf(buf+pos, bufsz-pos,
700 "lprx_rssi_threshold = %d\n",
701 cmd.lprx_rssi_threshold);
703 if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK)))
706 pos += scnprintf(buf+pos, bufsz-pos, "rx_data_timeout_uapsd = %d\n",
707 le32_to_cpu(cmd.rx_data_timeout_uapsd));
708 pos += scnprintf(buf+pos, bufsz-pos, "tx_data_timeout_uapsd = %d\n",
709 le32_to_cpu(cmd.tx_data_timeout_uapsd));
710 pos += scnprintf(buf+pos, bufsz-pos, "qndp_tid = %d\n", cmd.qndp_tid);
711 pos += scnprintf(buf+pos, bufsz-pos, "uapsd_ac_flags = 0x%x\n",
713 pos += scnprintf(buf+pos, bufsz-pos, "uapsd_max_sp = %d\n",
715 pos += scnprintf(buf+pos, bufsz-pos, "heavy_tx_thld_packets = %d\n",
716 cmd.heavy_tx_thld_packets);
717 pos += scnprintf(buf+pos, bufsz-pos, "heavy_rx_thld_packets = %d\n",
718 cmd.heavy_rx_thld_packets);
719 pos += scnprintf(buf+pos, bufsz-pos, "heavy_tx_thld_percentage = %d\n",
720 cmd.heavy_tx_thld_percentage);
721 pos += scnprintf(buf+pos, bufsz-pos, "heavy_rx_thld_percentage = %d\n",
722 cmd.heavy_rx_thld_percentage);
723 pos += scnprintf(buf+pos, bufsz-pos, "uapsd_misbehaving_enable = %d\n",
725 cpu_to_le16(POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK)) ?
728 if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)))
731 pos += scnprintf(buf+pos, bufsz-pos, "snooze_interval = %d\n",
732 cmd.snooze_interval);
733 pos += scnprintf(buf+pos, bufsz-pos, "snooze_window = %d\n",
740 iwl_mvm_beacon_filter_debugfs_parameters(struct ieee80211_vif *vif,
741 struct iwl_beacon_filter_cmd *cmd)
743 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
744 struct iwl_dbgfs_bf *dbgfs_bf = &mvmvif->dbgfs_bf;
746 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ENERGY_DELTA)
747 cmd->bf_energy_delta = cpu_to_le32(dbgfs_bf->bf_energy_delta);
748 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ROAMING_ENERGY_DELTA)
749 cmd->bf_roaming_energy_delta =
750 cpu_to_le32(dbgfs_bf->bf_roaming_energy_delta);
751 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ROAMING_STATE)
752 cmd->bf_roaming_state = cpu_to_le32(dbgfs_bf->bf_roaming_state);
753 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_THRESHOLD)
754 cmd->bf_temp_threshold =
755 cpu_to_le32(dbgfs_bf->bf_temp_threshold);
756 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_FAST_FILTER)
757 cmd->bf_temp_fast_filter =
758 cpu_to_le32(dbgfs_bf->bf_temp_fast_filter);
759 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_SLOW_FILTER)
760 cmd->bf_temp_slow_filter =
761 cpu_to_le32(dbgfs_bf->bf_temp_slow_filter);
762 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_DEBUG_FLAG)
763 cmd->bf_debug_flag = cpu_to_le32(dbgfs_bf->bf_debug_flag);
764 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ESCAPE_TIMER)
765 cmd->bf_escape_timer = cpu_to_le32(dbgfs_bf->bf_escape_timer);
766 if (dbgfs_bf->mask & MVM_DEBUGFS_BA_ESCAPE_TIMER)
767 cmd->ba_escape_timer = cpu_to_le32(dbgfs_bf->ba_escape_timer);
768 if (dbgfs_bf->mask & MVM_DEBUGFS_BA_ENABLE_BEACON_ABORT)
769 cmd->ba_enable_beacon_abort =
770 cpu_to_le32(dbgfs_bf->ba_enable_beacon_abort);
774 static int _iwl_mvm_enable_beacon_filter(struct iwl_mvm *mvm,
775 struct ieee80211_vif *vif,
776 struct iwl_beacon_filter_cmd *cmd,
780 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
783 if (mvmvif != mvm->bf_allowed_vif || !vif->bss_conf.dtim_period ||
784 vif->type != NL80211_IFTYPE_STATION || vif->p2p)
787 iwl_mvm_beacon_filter_set_cqm_params(mvm, vif, cmd, d0i3);
789 iwl_mvm_beacon_filter_debugfs_parameters(vif, cmd);
790 ret = iwl_mvm_beacon_filter_send_cmd(mvm, cmd, cmd_flags);
792 /* don't change bf_enabled in case of temporary d0i3 configuration */
794 mvmvif->bf_data.bf_enabled = true;
799 int iwl_mvm_enable_beacon_filter(struct iwl_mvm *mvm,
800 struct ieee80211_vif *vif,
803 struct iwl_beacon_filter_cmd cmd = {
804 IWL_BF_CMD_CONFIG_DEFAULTS,
805 .bf_enable_beacon_filter = cpu_to_le32(1),
808 return _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd, flags, false);
811 static int iwl_mvm_update_beacon_abort(struct iwl_mvm *mvm,
812 struct ieee80211_vif *vif,
815 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
816 struct iwl_beacon_filter_cmd cmd = {
817 IWL_BF_CMD_CONFIG_DEFAULTS,
818 .bf_enable_beacon_filter = cpu_to_le32(1),
821 if (!mvmvif->bf_data.bf_enabled)
824 if (mvm->cur_ucode == IWL_UCODE_WOWLAN)
825 cmd.ba_escape_timer = cpu_to_le32(IWL_BA_ESCAPE_TIMER_D3);
827 mvmvif->bf_data.ba_enabled = enable;
828 return _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd, 0, false);
831 int iwl_mvm_disable_beacon_filter(struct iwl_mvm *mvm,
832 struct ieee80211_vif *vif,
835 struct iwl_beacon_filter_cmd cmd = {};
836 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
839 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
842 ret = iwl_mvm_beacon_filter_send_cmd(mvm, &cmd, flags);
845 mvmvif->bf_data.bf_enabled = false;
850 static int iwl_mvm_power_set_ps(struct iwl_mvm *mvm)
855 /* disable PS if CAM */
856 disable_ps = (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM);
857 /* ...or if any of the vifs require PS to be off */
858 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
859 IEEE80211_IFACE_ITER_NORMAL,
860 iwl_mvm_power_ps_disabled_iterator,
863 /* update device power state if it has changed */
864 if (mvm->ps_disabled != disable_ps) {
865 bool old_ps_disabled = mvm->ps_disabled;
867 mvm->ps_disabled = disable_ps;
868 ret = iwl_mvm_power_update_device(mvm);
870 mvm->ps_disabled = old_ps_disabled;
878 static int iwl_mvm_power_set_ba(struct iwl_mvm *mvm,
879 struct iwl_power_vifs *vifs)
881 struct iwl_mvm_vif *mvmvif;
887 mvmvif = iwl_mvm_vif_from_mac80211(vifs->bf_vif);
889 ba_enable = !(!mvmvif->pm_enabled || mvm->ps_disabled ||
890 !vifs->bf_vif->bss_conf.ps ||
891 iwl_mvm_vif_low_latency(mvmvif));
893 return iwl_mvm_update_beacon_abort(mvm, vifs->bf_vif, ba_enable);
896 int iwl_mvm_power_update_ps(struct iwl_mvm *mvm)
898 struct iwl_power_vifs vifs = {
903 lockdep_assert_held(&mvm->mutex);
906 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
907 IEEE80211_IFACE_ITER_NORMAL,
908 iwl_mvm_power_get_vifs_iterator, &vifs);
910 ret = iwl_mvm_power_set_ps(mvm);
914 return iwl_mvm_power_set_ba(mvm, &vifs);
917 int iwl_mvm_power_update_mac(struct iwl_mvm *mvm)
919 struct iwl_power_vifs vifs = {
924 lockdep_assert_held(&mvm->mutex);
927 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
928 IEEE80211_IFACE_ITER_NORMAL,
929 iwl_mvm_power_get_vifs_iterator, &vifs);
931 iwl_mvm_power_set_pm(mvm, &vifs);
933 ret = iwl_mvm_power_set_ps(mvm);
938 ret = iwl_mvm_power_send_cmd(mvm, vifs.bss_vif);
944 ret = iwl_mvm_power_send_cmd(mvm, vifs.p2p_vif);
949 return iwl_mvm_power_set_ba(mvm, &vifs);
952 int iwl_mvm_update_d0i3_power_mode(struct iwl_mvm *mvm,
953 struct ieee80211_vif *vif,
954 bool enable, u32 flags)
957 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
958 struct iwl_mac_power_cmd cmd = {};
960 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
963 if (!vif->bss_conf.assoc)
966 iwl_mvm_power_build_cmd(mvm, vif, &cmd);
968 /* configure skip over dtim up to 306TU - 314 msec */
969 int dtimper = vif->bss_conf.dtim_period ?: 1;
970 int dtimper_tu = dtimper * vif->bss_conf.beacon_int;
971 bool radar_detect = iwl_mvm_power_is_radar(vif);
973 if (WARN_ON(!dtimper_tu))
976 /* Check skip over DTIM conditions */
977 /* TODO: check that multicast wake lock is off */
978 if (!radar_detect && (dtimper < 10)) {
979 cmd.skip_dtim_periods = 306 / dtimper_tu;
980 if (cmd.skip_dtim_periods)
981 cmd.flags |= cpu_to_le16(
982 POWER_FLAGS_SKIP_OVER_DTIM_MSK);
985 iwl_mvm_power_log(mvm, &cmd);
986 #ifdef CONFIG_IWLWIFI_DEBUGFS
987 memcpy(&mvmvif->mac_pwr_cmd, &cmd, sizeof(cmd));
989 ret = iwl_mvm_send_cmd_pdu(mvm, MAC_PM_POWER_TABLE, flags,
994 /* configure beacon filtering */
995 if (mvmvif != mvm->bf_allowed_vif)
999 struct iwl_beacon_filter_cmd cmd_bf = {
1000 IWL_BF_CMD_CONFIG_D0I3,
1001 .bf_enable_beacon_filter = cpu_to_le32(1),
1003 ret = _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd_bf,
1006 if (mvmvif->bf_data.bf_enabled)
1007 ret = iwl_mvm_enable_beacon_filter(mvm, vif, flags);
1009 ret = iwl_mvm_disable_beacon_filter(mvm, vif, flags);