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