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
10 * Copyright(c) 2015 Intel Deutschland GmbH
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of version 2 of the GNU General Public License as
14 * published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
26 * The full GNU General Public License is included in this distribution
27 * in the file called COPYING.
29 * Contact Information:
30 * Intel Linux Wireless <ilw@linux.intel.com>
31 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
35 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
36 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
37 * Copyright(c) 2015 Intel Deutschland GmbH
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
44 * * Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * * Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in
48 * the documentation and/or other materials provided with the
50 * * Neither the name Intel Corporation nor the names of its
51 * contributors may be used to endorse or promote products derived
52 * from this software without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66 *****************************************************************************/
68 #include <linux/kernel.h>
69 #include <linux/module.h>
70 #include <linux/slab.h>
71 #include <linux/etherdevice.h>
73 #include <net/mac80211.h>
75 #include "iwl-debug.h"
77 #include "iwl-modparams.h"
78 #include "fw-api-power.h"
80 #define POWER_KEEP_ALIVE_PERIOD_SEC 25
83 int iwl_mvm_beacon_filter_send_cmd(struct iwl_mvm *mvm,
84 struct iwl_beacon_filter_cmd *cmd,
87 IWL_DEBUG_POWER(mvm, "ba_enable_beacon_abort is: %d\n",
88 le32_to_cpu(cmd->ba_enable_beacon_abort));
89 IWL_DEBUG_POWER(mvm, "ba_escape_timer is: %d\n",
90 le32_to_cpu(cmd->ba_escape_timer));
91 IWL_DEBUG_POWER(mvm, "bf_debug_flag is: %d\n",
92 le32_to_cpu(cmd->bf_debug_flag));
93 IWL_DEBUG_POWER(mvm, "bf_enable_beacon_filter is: %d\n",
94 le32_to_cpu(cmd->bf_enable_beacon_filter));
95 IWL_DEBUG_POWER(mvm, "bf_energy_delta is: %d\n",
96 le32_to_cpu(cmd->bf_energy_delta));
97 IWL_DEBUG_POWER(mvm, "bf_escape_timer is: %d\n",
98 le32_to_cpu(cmd->bf_escape_timer));
99 IWL_DEBUG_POWER(mvm, "bf_roaming_energy_delta is: %d\n",
100 le32_to_cpu(cmd->bf_roaming_energy_delta));
101 IWL_DEBUG_POWER(mvm, "bf_roaming_state is: %d\n",
102 le32_to_cpu(cmd->bf_roaming_state));
103 IWL_DEBUG_POWER(mvm, "bf_temp_threshold is: %d\n",
104 le32_to_cpu(cmd->bf_temp_threshold));
105 IWL_DEBUG_POWER(mvm, "bf_temp_fast_filter is: %d\n",
106 le32_to_cpu(cmd->bf_temp_fast_filter));
107 IWL_DEBUG_POWER(mvm, "bf_temp_slow_filter is: %d\n",
108 le32_to_cpu(cmd->bf_temp_slow_filter));
110 return iwl_mvm_send_cmd_pdu(mvm, REPLY_BEACON_FILTERING_CMD, flags,
111 sizeof(struct iwl_beacon_filter_cmd), cmd);
115 void iwl_mvm_beacon_filter_set_cqm_params(struct iwl_mvm *mvm,
116 struct ieee80211_vif *vif,
117 struct iwl_beacon_filter_cmd *cmd,
120 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
122 if (vif->bss_conf.cqm_rssi_thold && !d0i3) {
123 cmd->bf_energy_delta =
124 cpu_to_le32(vif->bss_conf.cqm_rssi_hyst);
125 /* fw uses an absolute value for this */
126 cmd->bf_roaming_state =
127 cpu_to_le32(-vif->bss_conf.cqm_rssi_thold);
129 cmd->ba_enable_beacon_abort = cpu_to_le32(mvmvif->bf_data.ba_enabled);
132 static void iwl_mvm_power_log(struct iwl_mvm *mvm,
133 struct iwl_mac_power_cmd *cmd)
136 "Sending power table command on mac id 0x%X for power level %d, flags = 0x%X\n",
137 cmd->id_and_color, iwlmvm_mod_params.power_scheme,
138 le16_to_cpu(cmd->flags));
139 IWL_DEBUG_POWER(mvm, "Keep alive = %u sec\n",
140 le16_to_cpu(cmd->keep_alive_seconds));
142 if (!(cmd->flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK))) {
143 IWL_DEBUG_POWER(mvm, "Disable power management\n");
147 IWL_DEBUG_POWER(mvm, "Rx timeout = %u usec\n",
148 le32_to_cpu(cmd->rx_data_timeout));
149 IWL_DEBUG_POWER(mvm, "Tx timeout = %u usec\n",
150 le32_to_cpu(cmd->tx_data_timeout));
151 if (cmd->flags & cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK))
152 IWL_DEBUG_POWER(mvm, "DTIM periods to skip = %u\n",
153 cmd->skip_dtim_periods);
154 if (cmd->flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
155 IWL_DEBUG_POWER(mvm, "LP RX RSSI threshold = %u\n",
156 cmd->lprx_rssi_threshold);
157 if (cmd->flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK)) {
158 IWL_DEBUG_POWER(mvm, "uAPSD enabled\n");
159 IWL_DEBUG_POWER(mvm, "Rx timeout (uAPSD) = %u usec\n",
160 le32_to_cpu(cmd->rx_data_timeout_uapsd));
161 IWL_DEBUG_POWER(mvm, "Tx timeout (uAPSD) = %u usec\n",
162 le32_to_cpu(cmd->tx_data_timeout_uapsd));
163 IWL_DEBUG_POWER(mvm, "QNDP TID = %d\n", cmd->qndp_tid);
164 IWL_DEBUG_POWER(mvm, "ACs flags = 0x%x\n", cmd->uapsd_ac_flags);
165 IWL_DEBUG_POWER(mvm, "Max SP = %d\n", cmd->uapsd_max_sp);
169 static void iwl_mvm_power_configure_uapsd(struct iwl_mvm *mvm,
170 struct ieee80211_vif *vif,
171 struct iwl_mac_power_cmd *cmd)
173 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
174 enum ieee80211_ac_numbers ac;
175 bool tid_found = false;
177 for (ac = IEEE80211_AC_VO; ac <= IEEE80211_AC_BK; ac++) {
178 if (!mvmvif->queue_params[ac].uapsd)
181 if (mvm->cur_ucode != IWL_UCODE_WOWLAN)
183 cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK);
185 cmd->uapsd_ac_flags |= BIT(ac);
187 /* QNDP TID - the highest TID with no admission control */
188 if (!tid_found && !mvmvif->queue_params[ac].acm) {
191 case IEEE80211_AC_VO:
194 case IEEE80211_AC_VI:
197 case IEEE80211_AC_BE:
200 case IEEE80211_AC_BK:
207 if (!(cmd->flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK))) {
208 #ifdef CONFIG_IWLWIFI_DEBUGFS
209 /* set advanced pm flag with no uapsd ACs to enable ps-poll */
210 if (mvmvif->dbgfs_pm.use_ps_poll)
212 cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK);
217 cmd->flags |= cpu_to_le16(POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK);
219 if (cmd->uapsd_ac_flags == (BIT(IEEE80211_AC_VO) |
220 BIT(IEEE80211_AC_VI) |
221 BIT(IEEE80211_AC_BE) |
222 BIT(IEEE80211_AC_BK))) {
223 cmd->flags |= cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK);
224 cmd->snooze_interval = cpu_to_le16(IWL_MVM_PS_SNOOZE_INTERVAL);
225 cmd->snooze_window = (mvm->cur_ucode == IWL_UCODE_WOWLAN) ?
226 cpu_to_le16(IWL_MVM_WOWLAN_PS_SNOOZE_WINDOW) :
227 cpu_to_le16(IWL_MVM_PS_SNOOZE_WINDOW);
230 cmd->uapsd_max_sp = IWL_UAPSD_MAX_SP;
232 if (mvm->cur_ucode == IWL_UCODE_WOWLAN || cmd->flags &
233 cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)) {
234 cmd->rx_data_timeout_uapsd =
235 cpu_to_le32(IWL_MVM_WOWLAN_PS_RX_DATA_TIMEOUT);
236 cmd->tx_data_timeout_uapsd =
237 cpu_to_le32(IWL_MVM_WOWLAN_PS_TX_DATA_TIMEOUT);
239 cmd->rx_data_timeout_uapsd =
240 cpu_to_le32(IWL_MVM_UAPSD_RX_DATA_TIMEOUT);
241 cmd->tx_data_timeout_uapsd =
242 cpu_to_le32(IWL_MVM_UAPSD_TX_DATA_TIMEOUT);
245 if (cmd->flags & cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)) {
246 cmd->heavy_tx_thld_packets =
247 IWL_MVM_PS_SNOOZE_HEAVY_TX_THLD_PACKETS;
248 cmd->heavy_rx_thld_packets =
249 IWL_MVM_PS_SNOOZE_HEAVY_RX_THLD_PACKETS;
251 cmd->heavy_tx_thld_packets =
252 IWL_MVM_PS_HEAVY_TX_THLD_PACKETS;
253 cmd->heavy_rx_thld_packets =
254 IWL_MVM_PS_HEAVY_RX_THLD_PACKETS;
256 cmd->heavy_tx_thld_percentage =
257 IWL_MVM_PS_HEAVY_TX_THLD_PERCENT;
258 cmd->heavy_rx_thld_percentage =
259 IWL_MVM_PS_HEAVY_RX_THLD_PERCENT;
262 static bool iwl_mvm_power_allow_uapsd(struct iwl_mvm *mvm,
263 struct ieee80211_vif *vif)
265 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
267 if (!memcmp(mvmvif->uapsd_misbehaving_bssid, vif->bss_conf.bssid,
272 !(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_P2P_PS_UAPSD))
275 * Avoid using uAPSD if P2P client is associated to GO that uses
276 * opportunistic power save. This is due to current FW limitation.
279 (vif->bss_conf.p2p_noa_attr.oppps_ctwindow &
280 IEEE80211_P2P_OPPPS_ENABLE_BIT))
284 * Avoid using uAPSD if client is in DCM -
285 * low latency issue in Miracast
287 if (iwl_mvm_phy_ctx_count(mvm) >= 2)
293 static bool iwl_mvm_power_is_radar(struct ieee80211_vif *vif)
295 struct ieee80211_chanctx_conf *chanctx_conf;
296 struct ieee80211_channel *chan;
297 bool radar_detect = false;
300 chanctx_conf = rcu_dereference(vif->chanctx_conf);
301 WARN_ON(!chanctx_conf);
303 chan = chanctx_conf->def.chan;
304 radar_detect = chan->flags & IEEE80211_CHAN_RADAR;
311 static void iwl_mvm_power_config_skip_dtim(struct iwl_mvm *mvm,
312 struct ieee80211_vif *vif,
313 struct iwl_mac_power_cmd *cmd,
316 int dtimper = vif->bss_conf.dtim_period ?: 1;
319 /* disable, in case we're supposed to override */
320 cmd->skip_dtim_periods = 0;
321 cmd->flags &= ~cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
323 if (iwl_mvm_power_is_radar(vif))
329 /* TODO: check that multicast wake lock is off */
332 if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_LP)
336 int dtimper_tu = dtimper * vif->bss_conf.beacon_int;
338 if (WARN_ON(!dtimper_tu))
340 /* configure skip over dtim up to 306TU - 314 msec */
341 skip = max_t(u8, 1, 306 / dtimper_tu);
344 /* the firmware really expects "look at every X DTIMs", so add 1 */
345 cmd->skip_dtim_periods = 1 + skip;
346 cmd->flags |= cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
349 static void iwl_mvm_power_build_cmd(struct iwl_mvm *mvm,
350 struct ieee80211_vif *vif,
351 struct iwl_mac_power_cmd *cmd)
355 struct iwl_mvm_vif *mvmvif __maybe_unused =
356 iwl_mvm_vif_from_mac80211(vif);
358 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
360 dtimper = vif->bss_conf.dtim_period;
361 bi = vif->bss_conf.beacon_int;
364 * Regardless of power management state the driver must set
365 * keep alive period. FW will use it for sending keep alive NDPs
366 * immediately after association. Check that keep alive period
367 * is at least 3 * DTIM
369 keep_alive = DIV_ROUND_UP(ieee80211_tu_to_usec(3 * dtimper * bi),
371 keep_alive = max(keep_alive, POWER_KEEP_ALIVE_PERIOD_SEC);
372 cmd->keep_alive_seconds = cpu_to_le16(keep_alive);
374 if (mvm->ps_disabled)
377 cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK);
379 if (!vif->bss_conf.ps || !mvmvif->pm_enabled ||
380 (iwl_mvm_vif_low_latency(mvmvif) && vif->p2p))
383 cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK);
385 if (vif->bss_conf.beacon_rate &&
386 (vif->bss_conf.beacon_rate->bitrate == 10 ||
387 vif->bss_conf.beacon_rate->bitrate == 60)) {
388 cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
389 cmd->lprx_rssi_threshold = POWER_LPRX_RSSI_THRESHOLD;
392 iwl_mvm_power_config_skip_dtim(mvm, vif, cmd,
393 mvm->cur_ucode != IWL_UCODE_WOWLAN);
395 if (mvm->cur_ucode != IWL_UCODE_WOWLAN) {
396 cmd->rx_data_timeout =
397 cpu_to_le32(IWL_MVM_DEFAULT_PS_RX_DATA_TIMEOUT);
398 cmd->tx_data_timeout =
399 cpu_to_le32(IWL_MVM_DEFAULT_PS_TX_DATA_TIMEOUT);
401 cmd->rx_data_timeout =
402 cpu_to_le32(IWL_MVM_WOWLAN_PS_RX_DATA_TIMEOUT);
403 cmd->tx_data_timeout =
404 cpu_to_le32(IWL_MVM_WOWLAN_PS_TX_DATA_TIMEOUT);
407 if (iwl_mvm_power_allow_uapsd(mvm, vif))
408 iwl_mvm_power_configure_uapsd(mvm, vif, cmd);
410 #ifdef CONFIG_IWLWIFI_DEBUGFS
411 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_KEEP_ALIVE)
412 cmd->keep_alive_seconds =
413 cpu_to_le16(mvmvif->dbgfs_pm.keep_alive_seconds);
414 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_OVER_DTIM) {
415 if (mvmvif->dbgfs_pm.skip_over_dtim)
417 cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
420 cpu_to_le16(~POWER_FLAGS_SKIP_OVER_DTIM_MSK);
422 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_RX_DATA_TIMEOUT)
423 cmd->rx_data_timeout =
424 cpu_to_le32(mvmvif->dbgfs_pm.rx_data_timeout);
425 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_TX_DATA_TIMEOUT)
426 cmd->tx_data_timeout =
427 cpu_to_le32(mvmvif->dbgfs_pm.tx_data_timeout);
428 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_DTIM_PERIODS)
429 cmd->skip_dtim_periods = mvmvif->dbgfs_pm.skip_dtim_periods;
430 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_ENA) {
431 if (mvmvif->dbgfs_pm.lprx_ena)
432 cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
434 cmd->flags &= cpu_to_le16(~POWER_FLAGS_LPRX_ENA_MSK);
436 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_RSSI_THRESHOLD)
437 cmd->lprx_rssi_threshold = mvmvif->dbgfs_pm.lprx_rssi_threshold;
438 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SNOOZE_ENABLE) {
439 if (mvmvif->dbgfs_pm.snooze_ena)
441 cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK);
444 cpu_to_le16(~POWER_FLAGS_SNOOZE_ENA_MSK);
446 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_UAPSD_MISBEHAVING) {
447 u16 flag = POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK;
448 if (mvmvif->dbgfs_pm.uapsd_misbehaving)
449 cmd->flags |= cpu_to_le16(flag);
451 cmd->flags &= cpu_to_le16(flag);
453 #endif /* CONFIG_IWLWIFI_DEBUGFS */
456 static int iwl_mvm_power_send_cmd(struct iwl_mvm *mvm,
457 struct ieee80211_vif *vif)
459 struct iwl_mac_power_cmd cmd = {};
461 iwl_mvm_power_build_cmd(mvm, vif, &cmd);
462 iwl_mvm_power_log(mvm, &cmd);
463 #ifdef CONFIG_IWLWIFI_DEBUGFS
464 memcpy(&iwl_mvm_vif_from_mac80211(vif)->mac_pwr_cmd, &cmd, sizeof(cmd));
467 return iwl_mvm_send_cmd_pdu(mvm, MAC_PM_POWER_TABLE, 0,
471 int iwl_mvm_power_update_device(struct iwl_mvm *mvm)
473 struct iwl_device_power_cmd cmd = {
474 .flags = cpu_to_le16(DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK),
477 if (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM)
478 mvm->ps_disabled = true;
480 if (mvm->ps_disabled)
481 cmd.flags |= cpu_to_le16(DEVICE_POWER_FLAGS_CAM_MSK);
483 #ifdef CONFIG_IWLWIFI_DEBUGFS
484 if ((mvm->cur_ucode == IWL_UCODE_WOWLAN) ? mvm->disable_power_off_d3 :
485 mvm->disable_power_off)
487 cpu_to_le16(~DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK);
490 "Sending device power command with flags = 0x%X\n",
493 return iwl_mvm_send_cmd_pdu(mvm, POWER_TABLE_CMD, 0, sizeof(cmd),
497 void iwl_mvm_power_vif_assoc(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
499 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
501 if (memcmp(vif->bss_conf.bssid, mvmvif->uapsd_misbehaving_bssid,
503 eth_zero_addr(mvmvif->uapsd_misbehaving_bssid);
506 static void iwl_mvm_power_uapsd_misbehav_ap_iterator(void *_data, u8 *mac,
507 struct ieee80211_vif *vif)
509 u8 *ap_sta_id = _data;
510 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
512 /* The ap_sta_id is not expected to change during current association
513 * so no explicit protection is needed
515 if (mvmvif->ap_sta_id == *ap_sta_id)
516 memcpy(mvmvif->uapsd_misbehaving_bssid, vif->bss_conf.bssid,
520 void iwl_mvm_power_uapsd_misbehaving_ap_notif(struct iwl_mvm *mvm,
521 struct iwl_rx_cmd_buffer *rxb)
523 struct iwl_rx_packet *pkt = rxb_addr(rxb);
524 struct iwl_uapsd_misbehaving_ap_notif *notif = (void *)pkt->data;
525 u8 ap_sta_id = le32_to_cpu(notif->sta_id);
527 ieee80211_iterate_active_interfaces_atomic(
528 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
529 iwl_mvm_power_uapsd_misbehav_ap_iterator, &ap_sta_id);
532 struct iwl_power_vifs {
534 struct ieee80211_vif *bf_vif;
535 struct ieee80211_vif *bss_vif;
536 struct ieee80211_vif *p2p_vif;
537 struct ieee80211_vif *ap_vif;
538 struct ieee80211_vif *monitor_vif;
545 static void iwl_mvm_power_disable_pm_iterator(void *_data, u8* mac,
546 struct ieee80211_vif *vif)
548 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
550 mvmvif->pm_enabled = false;
553 static void iwl_mvm_power_ps_disabled_iterator(void *_data, u8* mac,
554 struct ieee80211_vif *vif)
556 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
557 bool *disable_ps = _data;
559 if (mvmvif->phy_ctxt)
560 if (mvmvif->phy_ctxt->id < MAX_PHYS)
561 *disable_ps |= mvmvif->ps_disabled;
564 static void iwl_mvm_power_get_vifs_iterator(void *_data, u8 *mac,
565 struct ieee80211_vif *vif)
567 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
568 struct iwl_power_vifs *power_iterator = _data;
570 switch (ieee80211_vif_type_p2p(vif)) {
571 case NL80211_IFTYPE_P2P_DEVICE:
574 case NL80211_IFTYPE_P2P_GO:
575 case NL80211_IFTYPE_AP:
576 /* only a single MAC of the same type */
577 WARN_ON(power_iterator->ap_vif);
578 power_iterator->ap_vif = vif;
579 if (mvmvif->phy_ctxt)
580 if (mvmvif->phy_ctxt->id < MAX_PHYS)
581 power_iterator->ap_active = true;
584 case NL80211_IFTYPE_MONITOR:
585 /* only a single MAC of the same type */
586 WARN_ON(power_iterator->monitor_vif);
587 power_iterator->monitor_vif = vif;
588 if (mvmvif->phy_ctxt)
589 if (mvmvif->phy_ctxt->id < MAX_PHYS)
590 power_iterator->monitor_active = true;
593 case NL80211_IFTYPE_P2P_CLIENT:
594 /* only a single MAC of the same type */
595 WARN_ON(power_iterator->p2p_vif);
596 power_iterator->p2p_vif = vif;
597 if (mvmvif->phy_ctxt)
598 if (mvmvif->phy_ctxt->id < MAX_PHYS)
599 power_iterator->p2p_active = true;
602 case NL80211_IFTYPE_STATION:
603 /* only a single MAC of the same type */
604 WARN_ON(power_iterator->bss_vif);
605 power_iterator->bss_vif = vif;
606 if (mvmvif->phy_ctxt)
607 if (mvmvif->phy_ctxt->id < MAX_PHYS)
608 power_iterator->bss_active = true;
610 if (mvmvif->bf_data.bf_enabled &&
611 !WARN_ON(power_iterator->bf_vif))
612 power_iterator->bf_vif = vif;
621 static void iwl_mvm_power_set_pm(struct iwl_mvm *mvm,
622 struct iwl_power_vifs *vifs)
624 struct iwl_mvm_vif *bss_mvmvif = NULL;
625 struct iwl_mvm_vif *p2p_mvmvif = NULL;
626 struct iwl_mvm_vif *ap_mvmvif = NULL;
627 bool client_same_channel = false;
628 bool ap_same_channel = false;
630 lockdep_assert_held(&mvm->mutex);
632 /* set pm_enable to false */
633 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
634 IEEE80211_IFACE_ITER_NORMAL,
635 iwl_mvm_power_disable_pm_iterator,
639 bss_mvmvif = iwl_mvm_vif_from_mac80211(vifs->bss_vif);
642 p2p_mvmvif = iwl_mvm_vif_from_mac80211(vifs->p2p_vif);
645 ap_mvmvif = iwl_mvm_vif_from_mac80211(vifs->ap_vif);
647 /* don't allow PM if any TDLS stations exist */
648 if (iwl_mvm_tdls_sta_count(mvm, NULL))
651 /* enable PM on bss if bss stand alone */
652 if (vifs->bss_active && !vifs->p2p_active && !vifs->ap_active) {
653 bss_mvmvif->pm_enabled = true;
657 /* enable PM on p2p if p2p stand alone */
658 if (vifs->p2p_active && !vifs->bss_active && !vifs->ap_active) {
659 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_P2P_PM)
660 p2p_mvmvif->pm_enabled = true;
664 if (vifs->bss_active && vifs->p2p_active)
665 client_same_channel = (bss_mvmvif->phy_ctxt->id ==
666 p2p_mvmvif->phy_ctxt->id);
667 if (vifs->bss_active && vifs->ap_active)
668 ap_same_channel = (bss_mvmvif->phy_ctxt->id ==
669 ap_mvmvif->phy_ctxt->id);
671 /* clients are not stand alone: enable PM if DCM */
672 if (!(client_same_channel || ap_same_channel) &&
673 (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BSS_P2P_PS_DCM)) {
674 if (vifs->bss_active)
675 bss_mvmvif->pm_enabled = true;
676 if (vifs->p2p_active &&
677 (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_P2P_PM))
678 p2p_mvmvif->pm_enabled = true;
683 * There is only one channel in the system and there are only
684 * bss and p2p clients that share it
686 if (client_same_channel && !vifs->ap_active &&
687 (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BSS_P2P_PS_SCM)) {
688 /* share same channel*/
689 bss_mvmvif->pm_enabled = true;
690 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_P2P_PM)
691 p2p_mvmvif->pm_enabled = true;
695 #ifdef CONFIG_IWLWIFI_DEBUGFS
696 int iwl_mvm_power_mac_dbgfs_read(struct iwl_mvm *mvm,
697 struct ieee80211_vif *vif, char *buf,
700 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
701 struct iwl_mac_power_cmd cmd = {};
704 mutex_lock(&mvm->mutex);
705 memcpy(&cmd, &mvmvif->mac_pwr_cmd, sizeof(cmd));
706 mutex_unlock(&mvm->mutex);
708 pos += scnprintf(buf+pos, bufsz-pos, "power_scheme = %d\n",
709 iwlmvm_mod_params.power_scheme);
710 pos += scnprintf(buf+pos, bufsz-pos, "flags = 0x%x\n",
711 le16_to_cpu(cmd.flags));
712 pos += scnprintf(buf+pos, bufsz-pos, "keep_alive = %d\n",
713 le16_to_cpu(cmd.keep_alive_seconds));
715 if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK)))
718 pos += scnprintf(buf+pos, bufsz-pos, "skip_over_dtim = %d\n",
720 cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK)) ? 1 : 0);
721 pos += scnprintf(buf+pos, bufsz-pos, "skip_dtim_periods = %d\n",
722 cmd.skip_dtim_periods);
723 if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK))) {
724 pos += scnprintf(buf+pos, bufsz-pos, "rx_data_timeout = %d\n",
725 le32_to_cpu(cmd.rx_data_timeout));
726 pos += scnprintf(buf+pos, bufsz-pos, "tx_data_timeout = %d\n",
727 le32_to_cpu(cmd.tx_data_timeout));
729 if (cmd.flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
730 pos += scnprintf(buf+pos, bufsz-pos,
731 "lprx_rssi_threshold = %d\n",
732 cmd.lprx_rssi_threshold);
734 if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK)))
737 pos += scnprintf(buf+pos, bufsz-pos, "rx_data_timeout_uapsd = %d\n",
738 le32_to_cpu(cmd.rx_data_timeout_uapsd));
739 pos += scnprintf(buf+pos, bufsz-pos, "tx_data_timeout_uapsd = %d\n",
740 le32_to_cpu(cmd.tx_data_timeout_uapsd));
741 pos += scnprintf(buf+pos, bufsz-pos, "qndp_tid = %d\n", cmd.qndp_tid);
742 pos += scnprintf(buf+pos, bufsz-pos, "uapsd_ac_flags = 0x%x\n",
744 pos += scnprintf(buf+pos, bufsz-pos, "uapsd_max_sp = %d\n",
746 pos += scnprintf(buf+pos, bufsz-pos, "heavy_tx_thld_packets = %d\n",
747 cmd.heavy_tx_thld_packets);
748 pos += scnprintf(buf+pos, bufsz-pos, "heavy_rx_thld_packets = %d\n",
749 cmd.heavy_rx_thld_packets);
750 pos += scnprintf(buf+pos, bufsz-pos, "heavy_tx_thld_percentage = %d\n",
751 cmd.heavy_tx_thld_percentage);
752 pos += scnprintf(buf+pos, bufsz-pos, "heavy_rx_thld_percentage = %d\n",
753 cmd.heavy_rx_thld_percentage);
754 pos += scnprintf(buf+pos, bufsz-pos, "uapsd_misbehaving_enable = %d\n",
756 cpu_to_le16(POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK)) ?
759 if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)))
762 pos += scnprintf(buf+pos, bufsz-pos, "snooze_interval = %d\n",
763 cmd.snooze_interval);
764 pos += scnprintf(buf+pos, bufsz-pos, "snooze_window = %d\n",
771 iwl_mvm_beacon_filter_debugfs_parameters(struct ieee80211_vif *vif,
772 struct iwl_beacon_filter_cmd *cmd)
774 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
775 struct iwl_dbgfs_bf *dbgfs_bf = &mvmvif->dbgfs_bf;
777 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ENERGY_DELTA)
778 cmd->bf_energy_delta = cpu_to_le32(dbgfs_bf->bf_energy_delta);
779 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ROAMING_ENERGY_DELTA)
780 cmd->bf_roaming_energy_delta =
781 cpu_to_le32(dbgfs_bf->bf_roaming_energy_delta);
782 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ROAMING_STATE)
783 cmd->bf_roaming_state = cpu_to_le32(dbgfs_bf->bf_roaming_state);
784 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_THRESHOLD)
785 cmd->bf_temp_threshold =
786 cpu_to_le32(dbgfs_bf->bf_temp_threshold);
787 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_FAST_FILTER)
788 cmd->bf_temp_fast_filter =
789 cpu_to_le32(dbgfs_bf->bf_temp_fast_filter);
790 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_SLOW_FILTER)
791 cmd->bf_temp_slow_filter =
792 cpu_to_le32(dbgfs_bf->bf_temp_slow_filter);
793 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_DEBUG_FLAG)
794 cmd->bf_debug_flag = cpu_to_le32(dbgfs_bf->bf_debug_flag);
795 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ESCAPE_TIMER)
796 cmd->bf_escape_timer = cpu_to_le32(dbgfs_bf->bf_escape_timer);
797 if (dbgfs_bf->mask & MVM_DEBUGFS_BA_ESCAPE_TIMER)
798 cmd->ba_escape_timer = cpu_to_le32(dbgfs_bf->ba_escape_timer);
799 if (dbgfs_bf->mask & MVM_DEBUGFS_BA_ENABLE_BEACON_ABORT)
800 cmd->ba_enable_beacon_abort =
801 cpu_to_le32(dbgfs_bf->ba_enable_beacon_abort);
805 static int _iwl_mvm_enable_beacon_filter(struct iwl_mvm *mvm,
806 struct ieee80211_vif *vif,
807 struct iwl_beacon_filter_cmd *cmd,
811 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
814 if (mvmvif != mvm->bf_allowed_vif || !vif->bss_conf.dtim_period ||
815 vif->type != NL80211_IFTYPE_STATION || vif->p2p)
818 iwl_mvm_beacon_filter_set_cqm_params(mvm, vif, cmd, d0i3);
820 iwl_mvm_beacon_filter_debugfs_parameters(vif, cmd);
821 ret = iwl_mvm_beacon_filter_send_cmd(mvm, cmd, cmd_flags);
823 /* don't change bf_enabled in case of temporary d0i3 configuration */
825 mvmvif->bf_data.bf_enabled = true;
830 int iwl_mvm_enable_beacon_filter(struct iwl_mvm *mvm,
831 struct ieee80211_vif *vif,
834 struct iwl_beacon_filter_cmd cmd = {
835 IWL_BF_CMD_CONFIG_DEFAULTS,
836 .bf_enable_beacon_filter = cpu_to_le32(1),
839 return _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd, flags, false);
842 static int iwl_mvm_update_beacon_abort(struct iwl_mvm *mvm,
843 struct ieee80211_vif *vif,
846 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
847 struct iwl_beacon_filter_cmd cmd = {
848 IWL_BF_CMD_CONFIG_DEFAULTS,
849 .bf_enable_beacon_filter = cpu_to_le32(1),
852 if (!mvmvif->bf_data.bf_enabled)
855 if (mvm->cur_ucode == IWL_UCODE_WOWLAN)
856 cmd.ba_escape_timer = cpu_to_le32(IWL_BA_ESCAPE_TIMER_D3);
858 mvmvif->bf_data.ba_enabled = enable;
859 return _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd, 0, false);
862 int iwl_mvm_disable_beacon_filter(struct iwl_mvm *mvm,
863 struct ieee80211_vif *vif,
866 struct iwl_beacon_filter_cmd cmd = {};
867 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
870 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
873 ret = iwl_mvm_beacon_filter_send_cmd(mvm, &cmd, flags);
876 mvmvif->bf_data.bf_enabled = false;
881 static int iwl_mvm_power_set_ps(struct iwl_mvm *mvm)
886 /* disable PS if CAM */
887 disable_ps = (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM);
888 /* ...or if any of the vifs require PS to be off */
889 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
890 IEEE80211_IFACE_ITER_NORMAL,
891 iwl_mvm_power_ps_disabled_iterator,
894 /* update device power state if it has changed */
895 if (mvm->ps_disabled != disable_ps) {
896 bool old_ps_disabled = mvm->ps_disabled;
898 mvm->ps_disabled = disable_ps;
899 ret = iwl_mvm_power_update_device(mvm);
901 mvm->ps_disabled = old_ps_disabled;
909 static int iwl_mvm_power_set_ba(struct iwl_mvm *mvm,
910 struct iwl_power_vifs *vifs)
912 struct iwl_mvm_vif *mvmvif;
918 mvmvif = iwl_mvm_vif_from_mac80211(vifs->bf_vif);
920 ba_enable = !(!mvmvif->pm_enabled || mvm->ps_disabled ||
921 !vifs->bf_vif->bss_conf.ps ||
922 iwl_mvm_vif_low_latency(mvmvif));
924 return iwl_mvm_update_beacon_abort(mvm, vifs->bf_vif, ba_enable);
927 int iwl_mvm_power_update_ps(struct iwl_mvm *mvm)
929 struct iwl_power_vifs vifs = {
934 lockdep_assert_held(&mvm->mutex);
937 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
938 IEEE80211_IFACE_ITER_NORMAL,
939 iwl_mvm_power_get_vifs_iterator, &vifs);
941 ret = iwl_mvm_power_set_ps(mvm);
945 return iwl_mvm_power_set_ba(mvm, &vifs);
948 int iwl_mvm_power_update_mac(struct iwl_mvm *mvm)
950 struct iwl_power_vifs vifs = {
955 lockdep_assert_held(&mvm->mutex);
958 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
959 IEEE80211_IFACE_ITER_NORMAL,
960 iwl_mvm_power_get_vifs_iterator, &vifs);
962 iwl_mvm_power_set_pm(mvm, &vifs);
964 ret = iwl_mvm_power_set_ps(mvm);
969 ret = iwl_mvm_power_send_cmd(mvm, vifs.bss_vif);
975 ret = iwl_mvm_power_send_cmd(mvm, vifs.p2p_vif);
980 return iwl_mvm_power_set_ba(mvm, &vifs);
983 int iwl_mvm_update_d0i3_power_mode(struct iwl_mvm *mvm,
984 struct ieee80211_vif *vif,
985 bool enable, u32 flags)
988 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
989 struct iwl_mac_power_cmd cmd = {};
991 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
994 if (!vif->bss_conf.assoc)
997 iwl_mvm_power_build_cmd(mvm, vif, &cmd);
999 /* when enabling D0i3, override the skip-over-dtim configuration */
1001 iwl_mvm_power_config_skip_dtim(mvm, vif, &cmd, false);
1003 iwl_mvm_power_log(mvm, &cmd);
1004 #ifdef CONFIG_IWLWIFI_DEBUGFS
1005 memcpy(&mvmvif->mac_pwr_cmd, &cmd, sizeof(cmd));
1007 ret = iwl_mvm_send_cmd_pdu(mvm, MAC_PM_POWER_TABLE, flags,
1012 /* configure beacon filtering */
1013 if (mvmvif != mvm->bf_allowed_vif)
1017 struct iwl_beacon_filter_cmd cmd_bf = {
1018 IWL_BF_CMD_CONFIG_D0I3,
1019 .bf_enable_beacon_filter = cpu_to_le32(1),
1021 ret = _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd_bf,
1024 if (mvmvif->bf_data.bf_enabled)
1025 ret = iwl_mvm_enable_beacon_filter(mvm, vif, flags);
1027 ret = iwl_mvm_disable_beacon_filter(mvm, vif, flags);