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  * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
10  * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
11  *
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.
15  *
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.
20  *
21  * The full GNU General Public License is included in this distribution
22  * in the file called COPYING.
23  *
24  * Contact Information:
25  *  Intel Linux Wireless <linuxwifi@intel.com>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *
28  * BSD LICENSE
29  *
30  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
31  * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
32  * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
33  * All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  *
39  *  * Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  *  * Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in
43  *    the documentation and/or other materials provided with the
44  *    distribution.
45  *  * Neither the name Intel Corporation nor the names of its
46  *    contributors may be used to endorse or promote products derived
47  *    from this software without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
50  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
51  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
52  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
53  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
55  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
59  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60  *
61  *****************************************************************************/
62 
63 #include <linux/kernel.h>
64 #include <linux/module.h>
65 #include <linux/slab.h>
66 #include <linux/etherdevice.h>
67 
68 #include <net/mac80211.h>
69 
70 #include "iwl-debug.h"
71 #include "mvm.h"
72 #include "iwl-modparams.h"
73 #include "fw/api/power.h"
74 
75 #define POWER_KEEP_ALIVE_PERIOD_SEC    25
76 
77 static
78 int iwl_mvm_beacon_filter_send_cmd(struct iwl_mvm *mvm,
79 				   struct iwl_beacon_filter_cmd *cmd,
80 				   u32 flags)
81 {
82 	IWL_DEBUG_POWER(mvm, "ba_enable_beacon_abort is: %d\n",
83 			le32_to_cpu(cmd->ba_enable_beacon_abort));
84 	IWL_DEBUG_POWER(mvm, "ba_escape_timer is: %d\n",
85 			le32_to_cpu(cmd->ba_escape_timer));
86 	IWL_DEBUG_POWER(mvm, "bf_debug_flag is: %d\n",
87 			le32_to_cpu(cmd->bf_debug_flag));
88 	IWL_DEBUG_POWER(mvm, "bf_enable_beacon_filter is: %d\n",
89 			le32_to_cpu(cmd->bf_enable_beacon_filter));
90 	IWL_DEBUG_POWER(mvm, "bf_energy_delta is: %d\n",
91 			le32_to_cpu(cmd->bf_energy_delta));
92 	IWL_DEBUG_POWER(mvm, "bf_escape_timer is: %d\n",
93 			le32_to_cpu(cmd->bf_escape_timer));
94 	IWL_DEBUG_POWER(mvm, "bf_roaming_energy_delta is: %d\n",
95 			le32_to_cpu(cmd->bf_roaming_energy_delta));
96 	IWL_DEBUG_POWER(mvm, "bf_roaming_state is: %d\n",
97 			le32_to_cpu(cmd->bf_roaming_state));
98 	IWL_DEBUG_POWER(mvm, "bf_temp_threshold is: %d\n",
99 			le32_to_cpu(cmd->bf_temp_threshold));
100 	IWL_DEBUG_POWER(mvm, "bf_temp_fast_filter is: %d\n",
101 			le32_to_cpu(cmd->bf_temp_fast_filter));
102 	IWL_DEBUG_POWER(mvm, "bf_temp_slow_filter is: %d\n",
103 			le32_to_cpu(cmd->bf_temp_slow_filter));
104 
105 	return iwl_mvm_send_cmd_pdu(mvm, REPLY_BEACON_FILTERING_CMD, flags,
106 				    sizeof(struct iwl_beacon_filter_cmd), cmd);
107 }
108 
109 static
110 void iwl_mvm_beacon_filter_set_cqm_params(struct iwl_mvm *mvm,
111 					  struct ieee80211_vif *vif,
112 					  struct iwl_beacon_filter_cmd *cmd,
113 					  bool d0i3)
114 {
115 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
116 
117 	if (vif->bss_conf.cqm_rssi_thold && !d0i3) {
118 		cmd->bf_energy_delta =
119 			cpu_to_le32(vif->bss_conf.cqm_rssi_hyst);
120 		/* fw uses an absolute value for this */
121 		cmd->bf_roaming_state =
122 			cpu_to_le32(-vif->bss_conf.cqm_rssi_thold);
123 	}
124 	cmd->ba_enable_beacon_abort = cpu_to_le32(mvmvif->bf_data.ba_enabled);
125 }
126 
127 static void iwl_mvm_power_log(struct iwl_mvm *mvm,
128 			      struct iwl_mac_power_cmd *cmd)
129 {
130 	IWL_DEBUG_POWER(mvm,
131 			"Sending power table command on mac id 0x%X for power level %d, flags = 0x%X\n",
132 			cmd->id_and_color, iwlmvm_mod_params.power_scheme,
133 			le16_to_cpu(cmd->flags));
134 	IWL_DEBUG_POWER(mvm, "Keep alive = %u sec\n",
135 			le16_to_cpu(cmd->keep_alive_seconds));
136 
137 	if (!(cmd->flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK))) {
138 		IWL_DEBUG_POWER(mvm, "Disable power management\n");
139 		return;
140 	}
141 
142 	IWL_DEBUG_POWER(mvm, "Rx timeout = %u usec\n",
143 			le32_to_cpu(cmd->rx_data_timeout));
144 	IWL_DEBUG_POWER(mvm, "Tx timeout = %u usec\n",
145 			le32_to_cpu(cmd->tx_data_timeout));
146 	if (cmd->flags & cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK))
147 		IWL_DEBUG_POWER(mvm, "DTIM periods to skip = %u\n",
148 				cmd->skip_dtim_periods);
149 	if (cmd->flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
150 		IWL_DEBUG_POWER(mvm, "LP RX RSSI threshold = %u\n",
151 				cmd->lprx_rssi_threshold);
152 	if (cmd->flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK)) {
153 		IWL_DEBUG_POWER(mvm, "uAPSD enabled\n");
154 		IWL_DEBUG_POWER(mvm, "Rx timeout (uAPSD) = %u usec\n",
155 				le32_to_cpu(cmd->rx_data_timeout_uapsd));
156 		IWL_DEBUG_POWER(mvm, "Tx timeout (uAPSD) = %u usec\n",
157 				le32_to_cpu(cmd->tx_data_timeout_uapsd));
158 		IWL_DEBUG_POWER(mvm, "QNDP TID = %d\n", cmd->qndp_tid);
159 		IWL_DEBUG_POWER(mvm, "ACs flags = 0x%x\n", cmd->uapsd_ac_flags);
160 		IWL_DEBUG_POWER(mvm, "Max SP = %d\n", cmd->uapsd_max_sp);
161 	}
162 }
163 
164 static void iwl_mvm_power_configure_uapsd(struct iwl_mvm *mvm,
165 					  struct ieee80211_vif *vif,
166 					  struct iwl_mac_power_cmd *cmd)
167 {
168 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
169 	enum ieee80211_ac_numbers ac;
170 	bool tid_found = false;
171 
172 #ifdef CONFIG_IWLWIFI_DEBUGFS
173 	/* set advanced pm flag with no uapsd ACs to enable ps-poll */
174 	if (mvmvif->dbgfs_pm.use_ps_poll) {
175 		cmd->flags |= cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK);
176 		return;
177 	}
178 #endif
179 
180 	for (ac = IEEE80211_AC_VO; ac <= IEEE80211_AC_BK; ac++) {
181 		if (!mvmvif->queue_params[ac].uapsd)
182 			continue;
183 
184 		if (mvm->fwrt.cur_fw_img != IWL_UCODE_WOWLAN)
185 			cmd->flags |=
186 				cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK);
187 
188 		cmd->uapsd_ac_flags |= BIT(ac);
189 
190 		/* QNDP TID - the highest TID with no admission control */
191 		if (!tid_found && !mvmvif->queue_params[ac].acm) {
192 			tid_found = true;
193 			switch (ac) {
194 			case IEEE80211_AC_VO:
195 				cmd->qndp_tid = 6;
196 				break;
197 			case IEEE80211_AC_VI:
198 				cmd->qndp_tid = 5;
199 				break;
200 			case IEEE80211_AC_BE:
201 				cmd->qndp_tid = 0;
202 				break;
203 			case IEEE80211_AC_BK:
204 				cmd->qndp_tid = 1;
205 				break;
206 			}
207 		}
208 	}
209 
210 	cmd->flags |= cpu_to_le16(POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK);
211 
212 	if (cmd->uapsd_ac_flags == (BIT(IEEE80211_AC_VO) |
213 				    BIT(IEEE80211_AC_VI) |
214 				    BIT(IEEE80211_AC_BE) |
215 				    BIT(IEEE80211_AC_BK))) {
216 		cmd->flags |= cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK);
217 		cmd->snooze_interval = cpu_to_le16(IWL_MVM_PS_SNOOZE_INTERVAL);
218 		cmd->snooze_window =
219 			(mvm->fwrt.cur_fw_img == IWL_UCODE_WOWLAN) ?
220 				cpu_to_le16(IWL_MVM_WOWLAN_PS_SNOOZE_WINDOW) :
221 				cpu_to_le16(IWL_MVM_PS_SNOOZE_WINDOW);
222 	}
223 
224 	cmd->uapsd_max_sp = mvm->hw->uapsd_max_sp_len;
225 
226 	if (mvm->fwrt.cur_fw_img == IWL_UCODE_WOWLAN || cmd->flags &
227 	    cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)) {
228 		cmd->rx_data_timeout_uapsd =
229 			cpu_to_le32(IWL_MVM_WOWLAN_PS_RX_DATA_TIMEOUT);
230 		cmd->tx_data_timeout_uapsd =
231 			cpu_to_le32(IWL_MVM_WOWLAN_PS_TX_DATA_TIMEOUT);
232 	} else {
233 		cmd->rx_data_timeout_uapsd =
234 			cpu_to_le32(IWL_MVM_UAPSD_RX_DATA_TIMEOUT);
235 		cmd->tx_data_timeout_uapsd =
236 			cpu_to_le32(IWL_MVM_UAPSD_TX_DATA_TIMEOUT);
237 	}
238 
239 	if (cmd->flags & cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)) {
240 		cmd->heavy_tx_thld_packets =
241 			IWL_MVM_PS_SNOOZE_HEAVY_TX_THLD_PACKETS;
242 		cmd->heavy_rx_thld_packets =
243 			IWL_MVM_PS_SNOOZE_HEAVY_RX_THLD_PACKETS;
244 	} else {
245 		cmd->heavy_tx_thld_packets =
246 			IWL_MVM_PS_HEAVY_TX_THLD_PACKETS;
247 		cmd->heavy_rx_thld_packets =
248 			IWL_MVM_PS_HEAVY_RX_THLD_PACKETS;
249 	}
250 	cmd->heavy_tx_thld_percentage =
251 		IWL_MVM_PS_HEAVY_TX_THLD_PERCENT;
252 	cmd->heavy_rx_thld_percentage =
253 		IWL_MVM_PS_HEAVY_RX_THLD_PERCENT;
254 }
255 
256 static void iwl_mvm_p2p_standalone_iterator(void *_data, u8 *mac,
257 					    struct ieee80211_vif *vif)
258 {
259 	bool *is_p2p_standalone = _data;
260 
261 	switch (ieee80211_vif_type_p2p(vif)) {
262 	case NL80211_IFTYPE_P2P_GO:
263 	case NL80211_IFTYPE_AP:
264 		*is_p2p_standalone = false;
265 		break;
266 	case NL80211_IFTYPE_STATION:
267 		if (vif->bss_conf.assoc)
268 			*is_p2p_standalone = false;
269 		break;
270 
271 	default:
272 		break;
273 	}
274 }
275 
276 static bool iwl_mvm_power_allow_uapsd(struct iwl_mvm *mvm,
277 				       struct ieee80211_vif *vif)
278 {
279 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
280 
281 	if (!memcmp(mvmvif->uapsd_misbehaving_bssid, vif->bss_conf.bssid,
282 		    ETH_ALEN))
283 		return false;
284 
285 	/*
286 	 * Avoid using uAPSD if P2P client is associated to GO that uses
287 	 * opportunistic power save. This is due to current FW limitation.
288 	 */
289 	if (vif->p2p &&
290 	    (vif->bss_conf.p2p_noa_attr.oppps_ctwindow &
291 	    IEEE80211_P2P_OPPPS_ENABLE_BIT))
292 		return false;
293 
294 	/*
295 	 * Avoid using uAPSD if client is in DCM -
296 	 * low latency issue in Miracast
297 	 */
298 	if (iwl_mvm_phy_ctx_count(mvm) >= 2)
299 		return false;
300 
301 	if (vif->p2p) {
302 		/* Allow U-APSD only if p2p is stand alone */
303 		bool is_p2p_standalone = true;
304 
305 		if (!iwl_mvm_is_p2p_scm_uapsd_supported(mvm))
306 			return false;
307 
308 		ieee80211_iterate_active_interfaces_atomic(mvm->hw,
309 					IEEE80211_IFACE_ITER_NORMAL,
310 					iwl_mvm_p2p_standalone_iterator,
311 					&is_p2p_standalone);
312 
313 		if (!is_p2p_standalone)
314 			return false;
315 	}
316 
317 	return true;
318 }
319 
320 static bool iwl_mvm_power_is_radar(struct ieee80211_vif *vif)
321 {
322 	struct ieee80211_chanctx_conf *chanctx_conf;
323 	struct ieee80211_channel *chan;
324 	bool radar_detect = false;
325 
326 	rcu_read_lock();
327 	chanctx_conf = rcu_dereference(vif->chanctx_conf);
328 	WARN_ON(!chanctx_conf);
329 	if (chanctx_conf) {
330 		chan = chanctx_conf->def.chan;
331 		radar_detect = chan->flags & IEEE80211_CHAN_RADAR;
332 	}
333 	rcu_read_unlock();
334 
335 	return radar_detect;
336 }
337 
338 static void iwl_mvm_power_config_skip_dtim(struct iwl_mvm *mvm,
339 					   struct ieee80211_vif *vif,
340 					   struct iwl_mac_power_cmd *cmd,
341 					   bool host_awake)
342 {
343 	int dtimper = vif->bss_conf.dtim_period ?: 1;
344 	int skip;
345 
346 	/* disable, in case we're supposed to override */
347 	cmd->skip_dtim_periods = 0;
348 	cmd->flags &= ~cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
349 
350 	if (iwl_mvm_power_is_radar(vif))
351 		return;
352 
353 	if (dtimper >= 10)
354 		return;
355 
356 	/* TODO: check that multicast wake lock is off */
357 
358 	if (host_awake) {
359 		if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_LP)
360 			return;
361 		skip = 2;
362 	} else {
363 		int dtimper_tu = dtimper * vif->bss_conf.beacon_int;
364 
365 		if (WARN_ON(!dtimper_tu))
366 			return;
367 		/* configure skip over dtim up to 306TU - 314 msec */
368 		skip = max_t(u8, 1, 306 / dtimper_tu);
369 	}
370 
371 	/* the firmware really expects "look at every X DTIMs", so add 1 */
372 	cmd->skip_dtim_periods = 1 + skip;
373 	cmd->flags |= cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
374 }
375 
376 static void iwl_mvm_power_build_cmd(struct iwl_mvm *mvm,
377 				    struct ieee80211_vif *vif,
378 				    struct iwl_mac_power_cmd *cmd,
379 				    bool host_awake)
380 {
381 	int dtimper, bi;
382 	int keep_alive;
383 	struct iwl_mvm_vif *mvmvif __maybe_unused =
384 		iwl_mvm_vif_from_mac80211(vif);
385 
386 	cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
387 							    mvmvif->color));
388 	dtimper = vif->bss_conf.dtim_period;
389 	bi = vif->bss_conf.beacon_int;
390 
391 	/*
392 	 * Regardless of power management state the driver must set
393 	 * keep alive period. FW will use it for sending keep alive NDPs
394 	 * immediately after association. Check that keep alive period
395 	 * is at least 3 * DTIM
396 	 */
397 	keep_alive = DIV_ROUND_UP(ieee80211_tu_to_usec(3 * dtimper * bi),
398 				  USEC_PER_SEC);
399 	keep_alive = max(keep_alive, POWER_KEEP_ALIVE_PERIOD_SEC);
400 	cmd->keep_alive_seconds = cpu_to_le16(keep_alive);
401 
402 	if (mvm->ps_disabled)
403 		return;
404 
405 	cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK);
406 
407 	if (!vif->bss_conf.ps || !mvmvif->pm_enabled)
408 		return;
409 
410 	if (iwl_mvm_vif_low_latency(mvmvif) && vif->p2p &&
411 	    (!fw_has_capa(&mvm->fw->ucode_capa,
412 			 IWL_UCODE_TLV_CAPA_SHORT_PM_TIMEOUTS) ||
413 	     !IWL_MVM_P2P_LOWLATENCY_PS_ENABLE))
414 		return;
415 
416 	cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK);
417 
418 	if (vif->bss_conf.beacon_rate &&
419 	    (vif->bss_conf.beacon_rate->bitrate == 10 ||
420 	     vif->bss_conf.beacon_rate->bitrate == 60)) {
421 		cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
422 		cmd->lprx_rssi_threshold = POWER_LPRX_RSSI_THRESHOLD;
423 	}
424 
425 	iwl_mvm_power_config_skip_dtim(mvm, vif, cmd, host_awake);
426 
427 	if (!host_awake) {
428 		cmd->rx_data_timeout =
429 			cpu_to_le32(IWL_MVM_WOWLAN_PS_RX_DATA_TIMEOUT);
430 		cmd->tx_data_timeout =
431 			cpu_to_le32(IWL_MVM_WOWLAN_PS_TX_DATA_TIMEOUT);
432 	} else if (iwl_mvm_vif_low_latency(mvmvif) && vif->p2p &&
433 		   fw_has_capa(&mvm->fw->ucode_capa,
434 			       IWL_UCODE_TLV_CAPA_SHORT_PM_TIMEOUTS)) {
435 		cmd->tx_data_timeout =
436 			cpu_to_le32(IWL_MVM_SHORT_PS_TX_DATA_TIMEOUT);
437 		cmd->rx_data_timeout =
438 			cpu_to_le32(IWL_MVM_SHORT_PS_RX_DATA_TIMEOUT);
439 	} else {
440 		cmd->rx_data_timeout =
441 			cpu_to_le32(IWL_MVM_DEFAULT_PS_RX_DATA_TIMEOUT);
442 		cmd->tx_data_timeout =
443 			cpu_to_le32(IWL_MVM_DEFAULT_PS_TX_DATA_TIMEOUT);
444 	}
445 
446 	if (iwl_mvm_power_allow_uapsd(mvm, vif))
447 		iwl_mvm_power_configure_uapsd(mvm, vif, cmd);
448 
449 #ifdef CONFIG_IWLWIFI_DEBUGFS
450 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_KEEP_ALIVE)
451 		cmd->keep_alive_seconds =
452 			cpu_to_le16(mvmvif->dbgfs_pm.keep_alive_seconds);
453 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_OVER_DTIM) {
454 		if (mvmvif->dbgfs_pm.skip_over_dtim)
455 			cmd->flags |=
456 				cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
457 		else
458 			cmd->flags &=
459 				cpu_to_le16(~POWER_FLAGS_SKIP_OVER_DTIM_MSK);
460 	}
461 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_RX_DATA_TIMEOUT)
462 		cmd->rx_data_timeout =
463 			cpu_to_le32(mvmvif->dbgfs_pm.rx_data_timeout);
464 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_TX_DATA_TIMEOUT)
465 		cmd->tx_data_timeout =
466 			cpu_to_le32(mvmvif->dbgfs_pm.tx_data_timeout);
467 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_DTIM_PERIODS)
468 		cmd->skip_dtim_periods = mvmvif->dbgfs_pm.skip_dtim_periods;
469 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_ENA) {
470 		if (mvmvif->dbgfs_pm.lprx_ena)
471 			cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
472 		else
473 			cmd->flags &= cpu_to_le16(~POWER_FLAGS_LPRX_ENA_MSK);
474 	}
475 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_RSSI_THRESHOLD)
476 		cmd->lprx_rssi_threshold = mvmvif->dbgfs_pm.lprx_rssi_threshold;
477 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SNOOZE_ENABLE) {
478 		if (mvmvif->dbgfs_pm.snooze_ena)
479 			cmd->flags |=
480 				cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK);
481 		else
482 			cmd->flags &=
483 				cpu_to_le16(~POWER_FLAGS_SNOOZE_ENA_MSK);
484 	}
485 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_UAPSD_MISBEHAVING) {
486 		u16 flag = POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK;
487 		if (mvmvif->dbgfs_pm.uapsd_misbehaving)
488 			cmd->flags |= cpu_to_le16(flag);
489 		else
490 			cmd->flags &= cpu_to_le16(flag);
491 	}
492 #endif /* CONFIG_IWLWIFI_DEBUGFS */
493 }
494 
495 static int iwl_mvm_power_send_cmd(struct iwl_mvm *mvm,
496 					 struct ieee80211_vif *vif)
497 {
498 	struct iwl_mac_power_cmd cmd = {};
499 
500 	iwl_mvm_power_build_cmd(mvm, vif, &cmd,
501 				mvm->fwrt.cur_fw_img != IWL_UCODE_WOWLAN);
502 	iwl_mvm_power_log(mvm, &cmd);
503 #ifdef CONFIG_IWLWIFI_DEBUGFS
504 	memcpy(&iwl_mvm_vif_from_mac80211(vif)->mac_pwr_cmd, &cmd, sizeof(cmd));
505 #endif
506 
507 	return iwl_mvm_send_cmd_pdu(mvm, MAC_PM_POWER_TABLE, 0,
508 				    sizeof(cmd), &cmd);
509 }
510 
511 int iwl_mvm_power_update_device(struct iwl_mvm *mvm)
512 {
513 	struct iwl_device_power_cmd cmd = {
514 		.flags = 0,
515 	};
516 
517 	if (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM)
518 		mvm->ps_disabled = true;
519 
520 	if (!mvm->ps_disabled)
521 		cmd.flags |= cpu_to_le16(DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK);
522 
523 #ifdef CONFIG_IWLWIFI_DEBUGFS
524 	if ((mvm->fwrt.cur_fw_img == IWL_UCODE_WOWLAN) ?
525 			mvm->disable_power_off_d3 : mvm->disable_power_off)
526 		cmd.flags &=
527 			cpu_to_le16(~DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK);
528 #endif
529 	IWL_DEBUG_POWER(mvm,
530 			"Sending device power command with flags = 0x%X\n",
531 			cmd.flags);
532 
533 	return iwl_mvm_send_cmd_pdu(mvm, POWER_TABLE_CMD, 0, sizeof(cmd),
534 				    &cmd);
535 }
536 
537 void iwl_mvm_power_vif_assoc(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
538 {
539 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
540 
541 	if (memcmp(vif->bss_conf.bssid, mvmvif->uapsd_misbehaving_bssid,
542 		   ETH_ALEN))
543 		eth_zero_addr(mvmvif->uapsd_misbehaving_bssid);
544 }
545 
546 static void iwl_mvm_power_uapsd_misbehav_ap_iterator(void *_data, u8 *mac,
547 						     struct ieee80211_vif *vif)
548 {
549 	u8 *ap_sta_id = _data;
550 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
551 
552 	/* The ap_sta_id is not expected to change during current association
553 	 * so no explicit protection is needed
554 	 */
555 	if (mvmvif->ap_sta_id == *ap_sta_id)
556 		memcpy(mvmvif->uapsd_misbehaving_bssid, vif->bss_conf.bssid,
557 		       ETH_ALEN);
558 }
559 
560 void iwl_mvm_power_uapsd_misbehaving_ap_notif(struct iwl_mvm *mvm,
561 					      struct iwl_rx_cmd_buffer *rxb)
562 {
563 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
564 	struct iwl_uapsd_misbehaving_ap_notif *notif = (void *)pkt->data;
565 	u8 ap_sta_id = le32_to_cpu(notif->sta_id);
566 
567 	ieee80211_iterate_active_interfaces_atomic(
568 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
569 		iwl_mvm_power_uapsd_misbehav_ap_iterator, &ap_sta_id);
570 }
571 
572 struct iwl_power_vifs {
573 	struct iwl_mvm *mvm;
574 	struct ieee80211_vif *bss_vif;
575 	struct ieee80211_vif *p2p_vif;
576 	struct ieee80211_vif *ap_vif;
577 	struct ieee80211_vif *monitor_vif;
578 	bool p2p_active;
579 	bool bss_active;
580 	bool ap_active;
581 	bool monitor_active;
582 };
583 
584 static void iwl_mvm_power_disable_pm_iterator(void *_data, u8* mac,
585 					      struct ieee80211_vif *vif)
586 {
587 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
588 
589 	mvmvif->pm_enabled = false;
590 }
591 
592 static void iwl_mvm_power_ps_disabled_iterator(void *_data, u8* mac,
593 					       struct ieee80211_vif *vif)
594 {
595 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
596 	bool *disable_ps = _data;
597 
598 	if (mvmvif->phy_ctxt && mvmvif->phy_ctxt->id < NUM_PHY_CTX)
599 		*disable_ps |= mvmvif->ps_disabled;
600 }
601 
602 static void iwl_mvm_power_get_vifs_iterator(void *_data, u8 *mac,
603 					    struct ieee80211_vif *vif)
604 {
605 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
606 	struct iwl_power_vifs *power_iterator = _data;
607 	bool active = mvmvif->phy_ctxt && mvmvif->phy_ctxt->id < NUM_PHY_CTX;
608 
609 	switch (ieee80211_vif_type_p2p(vif)) {
610 	case NL80211_IFTYPE_P2P_DEVICE:
611 		break;
612 
613 	case NL80211_IFTYPE_P2P_GO:
614 	case NL80211_IFTYPE_AP:
615 		/* only a single MAC of the same type */
616 		WARN_ON(power_iterator->ap_vif);
617 		power_iterator->ap_vif = vif;
618 		if (active)
619 			power_iterator->ap_active = true;
620 		break;
621 
622 	case NL80211_IFTYPE_MONITOR:
623 		/* only a single MAC of the same type */
624 		WARN_ON(power_iterator->monitor_vif);
625 		power_iterator->monitor_vif = vif;
626 		if (active)
627 			power_iterator->monitor_active = true;
628 		break;
629 
630 	case NL80211_IFTYPE_P2P_CLIENT:
631 		/* only a single MAC of the same type */
632 		WARN_ON(power_iterator->p2p_vif);
633 		power_iterator->p2p_vif = vif;
634 		if (active)
635 			power_iterator->p2p_active = true;
636 		break;
637 
638 	case NL80211_IFTYPE_STATION:
639 		power_iterator->bss_vif = vif;
640 		if (active)
641 			power_iterator->bss_active = true;
642 		break;
643 
644 	default:
645 		break;
646 	}
647 }
648 
649 static void iwl_mvm_power_set_pm(struct iwl_mvm *mvm,
650 				 struct iwl_power_vifs *vifs)
651 {
652 	struct iwl_mvm_vif *bss_mvmvif = NULL;
653 	struct iwl_mvm_vif *p2p_mvmvif = NULL;
654 	struct iwl_mvm_vif *ap_mvmvif = NULL;
655 	bool client_same_channel = false;
656 	bool ap_same_channel = false;
657 
658 	lockdep_assert_held(&mvm->mutex);
659 
660 	/* set pm_enable to false */
661 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
662 					IEEE80211_IFACE_ITER_NORMAL,
663 					iwl_mvm_power_disable_pm_iterator,
664 					NULL);
665 
666 	if (vifs->bss_vif)
667 		bss_mvmvif = iwl_mvm_vif_from_mac80211(vifs->bss_vif);
668 
669 	if (vifs->p2p_vif)
670 		p2p_mvmvif = iwl_mvm_vif_from_mac80211(vifs->p2p_vif);
671 
672 	if (vifs->ap_vif)
673 		ap_mvmvif = iwl_mvm_vif_from_mac80211(vifs->ap_vif);
674 
675 	/* don't allow PM if any TDLS stations exist */
676 	if (iwl_mvm_tdls_sta_count(mvm, NULL))
677 		return;
678 
679 	/* enable PM on bss if bss stand alone */
680 	if (vifs->bss_active && !vifs->p2p_active && !vifs->ap_active) {
681 		bss_mvmvif->pm_enabled = true;
682 		return;
683 	}
684 
685 	/* enable PM on p2p if p2p stand alone */
686 	if (vifs->p2p_active && !vifs->bss_active && !vifs->ap_active) {
687 		p2p_mvmvif->pm_enabled = true;
688 		return;
689 	}
690 
691 	if (vifs->bss_active && vifs->p2p_active)
692 		client_same_channel = (bss_mvmvif->phy_ctxt->id ==
693 				       p2p_mvmvif->phy_ctxt->id);
694 	if (vifs->bss_active && vifs->ap_active)
695 		ap_same_channel = (bss_mvmvif->phy_ctxt->id ==
696 				   ap_mvmvif->phy_ctxt->id);
697 
698 	/* clients are not stand alone: enable PM if DCM */
699 	if (!(client_same_channel || ap_same_channel)) {
700 		if (vifs->bss_active)
701 			bss_mvmvif->pm_enabled = true;
702 		if (vifs->p2p_active)
703 			p2p_mvmvif->pm_enabled = true;
704 		return;
705 	}
706 
707 	/*
708 	 * There is only one channel in the system and there are only
709 	 * bss and p2p clients that share it
710 	 */
711 	if (client_same_channel && !vifs->ap_active) {
712 		/* share same channel*/
713 		bss_mvmvif->pm_enabled = true;
714 		p2p_mvmvif->pm_enabled = true;
715 	}
716 }
717 
718 #ifdef CONFIG_IWLWIFI_DEBUGFS
719 int iwl_mvm_power_mac_dbgfs_read(struct iwl_mvm *mvm,
720 				 struct ieee80211_vif *vif, char *buf,
721 				 int bufsz)
722 {
723 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
724 	struct iwl_mac_power_cmd cmd = {};
725 	int pos = 0;
726 
727 	mutex_lock(&mvm->mutex);
728 	memcpy(&cmd, &mvmvif->mac_pwr_cmd, sizeof(cmd));
729 	mutex_unlock(&mvm->mutex);
730 
731 	pos += scnprintf(buf+pos, bufsz-pos, "power_scheme = %d\n",
732 			 iwlmvm_mod_params.power_scheme);
733 	pos += scnprintf(buf+pos, bufsz-pos, "flags = 0x%x\n",
734 			 le16_to_cpu(cmd.flags));
735 	pos += scnprintf(buf+pos, bufsz-pos, "keep_alive = %d\n",
736 			 le16_to_cpu(cmd.keep_alive_seconds));
737 
738 	if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK)))
739 		return pos;
740 
741 	pos += scnprintf(buf+pos, bufsz-pos, "skip_over_dtim = %d\n",
742 			 (cmd.flags &
743 			 cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK)) ? 1 : 0);
744 	pos += scnprintf(buf+pos, bufsz-pos, "skip_dtim_periods = %d\n",
745 			 cmd.skip_dtim_periods);
746 	if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK))) {
747 		pos += scnprintf(buf+pos, bufsz-pos, "rx_data_timeout = %d\n",
748 				 le32_to_cpu(cmd.rx_data_timeout));
749 		pos += scnprintf(buf+pos, bufsz-pos, "tx_data_timeout = %d\n",
750 				 le32_to_cpu(cmd.tx_data_timeout));
751 	}
752 	if (cmd.flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
753 		pos += scnprintf(buf+pos, bufsz-pos,
754 				 "lprx_rssi_threshold = %d\n",
755 				 cmd.lprx_rssi_threshold);
756 
757 	if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK)))
758 		return pos;
759 
760 	pos += scnprintf(buf+pos, bufsz-pos, "rx_data_timeout_uapsd = %d\n",
761 			 le32_to_cpu(cmd.rx_data_timeout_uapsd));
762 	pos += scnprintf(buf+pos, bufsz-pos, "tx_data_timeout_uapsd = %d\n",
763 			 le32_to_cpu(cmd.tx_data_timeout_uapsd));
764 	pos += scnprintf(buf+pos, bufsz-pos, "qndp_tid = %d\n", cmd.qndp_tid);
765 	pos += scnprintf(buf+pos, bufsz-pos, "uapsd_ac_flags = 0x%x\n",
766 			 cmd.uapsd_ac_flags);
767 	pos += scnprintf(buf+pos, bufsz-pos, "uapsd_max_sp = %d\n",
768 			 cmd.uapsd_max_sp);
769 	pos += scnprintf(buf+pos, bufsz-pos, "heavy_tx_thld_packets = %d\n",
770 			 cmd.heavy_tx_thld_packets);
771 	pos += scnprintf(buf+pos, bufsz-pos, "heavy_rx_thld_packets = %d\n",
772 			 cmd.heavy_rx_thld_packets);
773 	pos += scnprintf(buf+pos, bufsz-pos, "heavy_tx_thld_percentage = %d\n",
774 			 cmd.heavy_tx_thld_percentage);
775 	pos += scnprintf(buf+pos, bufsz-pos, "heavy_rx_thld_percentage = %d\n",
776 			 cmd.heavy_rx_thld_percentage);
777 	pos += scnprintf(buf+pos, bufsz-pos, "uapsd_misbehaving_enable = %d\n",
778 			 (cmd.flags &
779 			  cpu_to_le16(POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK)) ?
780 			 1 : 0);
781 
782 	if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)))
783 		return pos;
784 
785 	pos += scnprintf(buf+pos, bufsz-pos, "snooze_interval = %d\n",
786 			 cmd.snooze_interval);
787 	pos += scnprintf(buf+pos, bufsz-pos, "snooze_window = %d\n",
788 			 cmd.snooze_window);
789 
790 	return pos;
791 }
792 
793 void
794 iwl_mvm_beacon_filter_debugfs_parameters(struct ieee80211_vif *vif,
795 					 struct iwl_beacon_filter_cmd *cmd)
796 {
797 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
798 	struct iwl_dbgfs_bf *dbgfs_bf = &mvmvif->dbgfs_bf;
799 
800 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ENERGY_DELTA)
801 		cmd->bf_energy_delta = cpu_to_le32(dbgfs_bf->bf_energy_delta);
802 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ROAMING_ENERGY_DELTA)
803 		cmd->bf_roaming_energy_delta =
804 				cpu_to_le32(dbgfs_bf->bf_roaming_energy_delta);
805 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ROAMING_STATE)
806 		cmd->bf_roaming_state = cpu_to_le32(dbgfs_bf->bf_roaming_state);
807 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_THRESHOLD)
808 		cmd->bf_temp_threshold =
809 				cpu_to_le32(dbgfs_bf->bf_temp_threshold);
810 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_FAST_FILTER)
811 		cmd->bf_temp_fast_filter =
812 				cpu_to_le32(dbgfs_bf->bf_temp_fast_filter);
813 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_SLOW_FILTER)
814 		cmd->bf_temp_slow_filter =
815 				cpu_to_le32(dbgfs_bf->bf_temp_slow_filter);
816 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_DEBUG_FLAG)
817 		cmd->bf_debug_flag = cpu_to_le32(dbgfs_bf->bf_debug_flag);
818 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ESCAPE_TIMER)
819 		cmd->bf_escape_timer = cpu_to_le32(dbgfs_bf->bf_escape_timer);
820 	if (dbgfs_bf->mask & MVM_DEBUGFS_BA_ESCAPE_TIMER)
821 		cmd->ba_escape_timer = cpu_to_le32(dbgfs_bf->ba_escape_timer);
822 	if (dbgfs_bf->mask & MVM_DEBUGFS_BA_ENABLE_BEACON_ABORT)
823 		cmd->ba_enable_beacon_abort =
824 				cpu_to_le32(dbgfs_bf->ba_enable_beacon_abort);
825 }
826 #endif
827 
828 static int _iwl_mvm_enable_beacon_filter(struct iwl_mvm *mvm,
829 					 struct ieee80211_vif *vif,
830 					 struct iwl_beacon_filter_cmd *cmd,
831 					 u32 cmd_flags,
832 					 bool d0i3)
833 {
834 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
835 	int ret;
836 
837 	if (mvmvif != mvm->bf_allowed_vif || !vif->bss_conf.dtim_period ||
838 	    vif->type != NL80211_IFTYPE_STATION || vif->p2p)
839 		return 0;
840 
841 	iwl_mvm_beacon_filter_set_cqm_params(mvm, vif, cmd, d0i3);
842 	if (!d0i3)
843 		iwl_mvm_beacon_filter_debugfs_parameters(vif, cmd);
844 	ret = iwl_mvm_beacon_filter_send_cmd(mvm, cmd, cmd_flags);
845 
846 	/* don't change bf_enabled in case of temporary d0i3 configuration */
847 	if (!ret && !d0i3)
848 		mvmvif->bf_data.bf_enabled = true;
849 
850 	return ret;
851 }
852 
853 int iwl_mvm_enable_beacon_filter(struct iwl_mvm *mvm,
854 				 struct ieee80211_vif *vif,
855 				 u32 flags)
856 {
857 	struct iwl_beacon_filter_cmd cmd = {
858 		IWL_BF_CMD_CONFIG_DEFAULTS,
859 		.bf_enable_beacon_filter = cpu_to_le32(1),
860 	};
861 
862 	return _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd, flags, false);
863 }
864 
865 static int _iwl_mvm_disable_beacon_filter(struct iwl_mvm *mvm,
866 					  struct ieee80211_vif *vif,
867 					  u32 flags, bool d0i3)
868 {
869 	struct iwl_beacon_filter_cmd cmd = {};
870 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
871 	int ret;
872 
873 	if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
874 		return 0;
875 
876 	ret = iwl_mvm_beacon_filter_send_cmd(mvm, &cmd, flags);
877 
878 	/* don't change bf_enabled in case of temporary d0i3 configuration */
879 	if (!ret && !d0i3)
880 		mvmvif->bf_data.bf_enabled = false;
881 
882 	return ret;
883 }
884 
885 int iwl_mvm_disable_beacon_filter(struct iwl_mvm *mvm,
886 				  struct ieee80211_vif *vif,
887 				  u32 flags)
888 {
889 	return _iwl_mvm_disable_beacon_filter(mvm, vif, flags, false);
890 }
891 
892 static int iwl_mvm_power_set_ps(struct iwl_mvm *mvm)
893 {
894 	bool disable_ps;
895 	int ret;
896 
897 	/* disable PS if CAM */
898 	disable_ps = (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM);
899 	/* ...or if any of the vifs require PS to be off */
900 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
901 					IEEE80211_IFACE_ITER_NORMAL,
902 					iwl_mvm_power_ps_disabled_iterator,
903 					&disable_ps);
904 
905 	/* update device power state if it has changed */
906 	if (mvm->ps_disabled != disable_ps) {
907 		bool old_ps_disabled = mvm->ps_disabled;
908 
909 		mvm->ps_disabled = disable_ps;
910 		ret = iwl_mvm_power_update_device(mvm);
911 		if (ret) {
912 			mvm->ps_disabled = old_ps_disabled;
913 			return ret;
914 		}
915 	}
916 
917 	return 0;
918 }
919 
920 static int iwl_mvm_power_set_ba(struct iwl_mvm *mvm,
921 				struct ieee80211_vif *vif)
922 {
923 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
924 	struct iwl_beacon_filter_cmd cmd = {
925 		IWL_BF_CMD_CONFIG_DEFAULTS,
926 		.bf_enable_beacon_filter = cpu_to_le32(1),
927 	};
928 
929 	if (!mvmvif->bf_data.bf_enabled)
930 		return 0;
931 
932 	if (mvm->fwrt.cur_fw_img == IWL_UCODE_WOWLAN)
933 		cmd.ba_escape_timer = cpu_to_le32(IWL_BA_ESCAPE_TIMER_D3);
934 
935 	mvmvif->bf_data.ba_enabled = !(!mvmvif->pm_enabled ||
936 				       mvm->ps_disabled ||
937 				       !vif->bss_conf.ps ||
938 				       iwl_mvm_vif_low_latency(mvmvif));
939 
940 	return _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd, 0, false);
941 }
942 
943 int iwl_mvm_power_update_ps(struct iwl_mvm *mvm)
944 {
945 	struct iwl_power_vifs vifs = {
946 		.mvm = mvm,
947 	};
948 	int ret;
949 
950 	lockdep_assert_held(&mvm->mutex);
951 
952 	/* get vifs info */
953 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
954 					IEEE80211_IFACE_ITER_NORMAL,
955 					iwl_mvm_power_get_vifs_iterator, &vifs);
956 
957 	ret = iwl_mvm_power_set_ps(mvm);
958 	if (ret)
959 		return ret;
960 
961 	if (vifs.bss_vif)
962 		return iwl_mvm_power_set_ba(mvm, vifs.bss_vif);
963 
964 	return 0;
965 }
966 
967 int iwl_mvm_power_update_mac(struct iwl_mvm *mvm)
968 {
969 	struct iwl_power_vifs vifs = {
970 		.mvm = mvm,
971 	};
972 	int ret;
973 
974 	lockdep_assert_held(&mvm->mutex);
975 
976 	/* get vifs info */
977 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
978 					IEEE80211_IFACE_ITER_NORMAL,
979 					iwl_mvm_power_get_vifs_iterator, &vifs);
980 
981 	iwl_mvm_power_set_pm(mvm, &vifs);
982 
983 	ret = iwl_mvm_power_set_ps(mvm);
984 	if (ret)
985 		return ret;
986 
987 	if (vifs.bss_vif) {
988 		ret = iwl_mvm_power_send_cmd(mvm, vifs.bss_vif);
989 		if (ret)
990 			return ret;
991 	}
992 
993 	if (vifs.p2p_vif) {
994 		ret = iwl_mvm_power_send_cmd(mvm, vifs.p2p_vif);
995 		if (ret)
996 			return ret;
997 	}
998 
999 	if (vifs.bss_vif)
1000 		return iwl_mvm_power_set_ba(mvm, vifs.bss_vif);
1001 
1002 	return 0;
1003 }
1004 
1005 int iwl_mvm_update_d0i3_power_mode(struct iwl_mvm *mvm,
1006 				   struct ieee80211_vif *vif,
1007 				   bool enable, u32 flags)
1008 {
1009 	int ret;
1010 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1011 	struct iwl_mac_power_cmd cmd = {};
1012 
1013 	if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
1014 		return 0;
1015 
1016 	if (!vif->bss_conf.assoc)
1017 		return 0;
1018 
1019 	iwl_mvm_power_build_cmd(mvm, vif, &cmd, !enable);
1020 
1021 	iwl_mvm_power_log(mvm, &cmd);
1022 #ifdef CONFIG_IWLWIFI_DEBUGFS
1023 	memcpy(&mvmvif->mac_pwr_cmd, &cmd, sizeof(cmd));
1024 #endif
1025 	ret = iwl_mvm_send_cmd_pdu(mvm, MAC_PM_POWER_TABLE, flags,
1026 				   sizeof(cmd), &cmd);
1027 	if (ret)
1028 		return ret;
1029 
1030 	/* configure beacon filtering */
1031 	if (mvmvif != mvm->bf_allowed_vif)
1032 		return 0;
1033 
1034 	if (enable) {
1035 		struct iwl_beacon_filter_cmd cmd_bf = {
1036 			IWL_BF_CMD_CONFIG_D0I3,
1037 			.bf_enable_beacon_filter = cpu_to_le32(1),
1038 		};
1039 		/*
1040 		 * When beacon storing is supported - disable beacon filtering
1041 		 * altogether - the latest beacon will be sent when exiting d0i3
1042 		 */
1043 		if (fw_has_capa(&mvm->fw->ucode_capa,
1044 				IWL_UCODE_TLV_CAPA_BEACON_STORING))
1045 			ret = _iwl_mvm_disable_beacon_filter(mvm, vif, flags,
1046 							     true);
1047 		else
1048 			ret = _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd_bf,
1049 							    flags, true);
1050 	} else {
1051 		if (mvmvif->bf_data.bf_enabled)
1052 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, flags);
1053 		else
1054 			ret = iwl_mvm_disable_beacon_filter(mvm, vif, flags);
1055 	}
1056 
1057 	return ret;
1058 }
1059