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
iwl_mvm_beacon_filter_send_cmd(struct iwl_mvm * mvm,struct iwl_beacon_filter_cmd * cmd,u32 flags)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
iwl_mvm_beacon_filter_set_cqm_params(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_beacon_filter_cmd * cmd)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 {
132 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
133 
134 	if (vif->bss_conf.cqm_rssi_thold) {
135 		cmd->bf_energy_delta =
136 			cpu_to_le32(vif->bss_conf.cqm_rssi_hyst);
137 		/* fw uses an absolute value for this */
138 		cmd->bf_roaming_state =
139 			cpu_to_le32(-vif->bss_conf.cqm_rssi_thold);
140 	}
141 	cmd->ba_enable_beacon_abort = cpu_to_le32(mvmvif->bf_data.ba_enabled);
142 }
143 
iwl_mvm_power_log(struct iwl_mvm * mvm,struct iwl_mac_power_cmd * cmd)144 static void iwl_mvm_power_log(struct iwl_mvm *mvm,
145 			      struct iwl_mac_power_cmd *cmd)
146 {
147 	IWL_DEBUG_POWER(mvm,
148 			"Sending power table command on mac id 0x%X for power level %d, flags = 0x%X\n",
149 			cmd->id_and_color, iwlmvm_mod_params.power_scheme,
150 			le16_to_cpu(cmd->flags));
151 	IWL_DEBUG_POWER(mvm, "Keep alive = %u sec\n",
152 			le16_to_cpu(cmd->keep_alive_seconds));
153 
154 	if (!(cmd->flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK))) {
155 		IWL_DEBUG_POWER(mvm, "Disable power management\n");
156 		return;
157 	}
158 
159 	IWL_DEBUG_POWER(mvm, "Rx timeout = %u usec\n",
160 			le32_to_cpu(cmd->rx_data_timeout));
161 	IWL_DEBUG_POWER(mvm, "Tx timeout = %u usec\n",
162 			le32_to_cpu(cmd->tx_data_timeout));
163 	if (cmd->flags & cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK))
164 		IWL_DEBUG_POWER(mvm, "DTIM periods to skip = %u\n",
165 				cmd->skip_dtim_periods);
166 	if (cmd->flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
167 		IWL_DEBUG_POWER(mvm, "LP RX RSSI threshold = %u\n",
168 				cmd->lprx_rssi_threshold);
169 	if (cmd->flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK)) {
170 		IWL_DEBUG_POWER(mvm, "uAPSD enabled\n");
171 		IWL_DEBUG_POWER(mvm, "Rx timeout (uAPSD) = %u usec\n",
172 				le32_to_cpu(cmd->rx_data_timeout_uapsd));
173 		IWL_DEBUG_POWER(mvm, "Tx timeout (uAPSD) = %u usec\n",
174 				le32_to_cpu(cmd->tx_data_timeout_uapsd));
175 		IWL_DEBUG_POWER(mvm, "QNDP TID = %d\n", cmd->qndp_tid);
176 		IWL_DEBUG_POWER(mvm, "ACs flags = 0x%x\n", cmd->uapsd_ac_flags);
177 		IWL_DEBUG_POWER(mvm, "Max SP = %d\n", cmd->uapsd_max_sp);
178 	}
179 }
180 
iwl_mvm_power_configure_uapsd(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_mac_power_cmd * cmd)181 static void iwl_mvm_power_configure_uapsd(struct iwl_mvm *mvm,
182 					  struct ieee80211_vif *vif,
183 					  struct iwl_mac_power_cmd *cmd)
184 {
185 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
186 	enum ieee80211_ac_numbers ac;
187 	bool tid_found = false;
188 
189 #ifdef CONFIG_IWLWIFI_DEBUGFS
190 	/* set advanced pm flag with no uapsd ACs to enable ps-poll */
191 	if (mvmvif->dbgfs_pm.use_ps_poll) {
192 		cmd->flags |= cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK);
193 		return;
194 	}
195 #endif
196 
197 	for (ac = IEEE80211_AC_VO; ac <= IEEE80211_AC_BK; ac++) {
198 		if (!mvmvif->queue_params[ac].uapsd)
199 			continue;
200 
201 		if (!test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status))
202 			cmd->flags |=
203 				cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK);
204 
205 		cmd->uapsd_ac_flags |= BIT(ac);
206 
207 		/* QNDP TID - the highest TID with no admission control */
208 		if (!tid_found && !mvmvif->queue_params[ac].acm) {
209 			tid_found = true;
210 			switch (ac) {
211 			case IEEE80211_AC_VO:
212 				cmd->qndp_tid = 6;
213 				break;
214 			case IEEE80211_AC_VI:
215 				cmd->qndp_tid = 5;
216 				break;
217 			case IEEE80211_AC_BE:
218 				cmd->qndp_tid = 0;
219 				break;
220 			case IEEE80211_AC_BK:
221 				cmd->qndp_tid = 1;
222 				break;
223 			}
224 		}
225 	}
226 
227 	cmd->flags |= cpu_to_le16(POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK);
228 
229 	if (cmd->uapsd_ac_flags == (BIT(IEEE80211_AC_VO) |
230 				    BIT(IEEE80211_AC_VI) |
231 				    BIT(IEEE80211_AC_BE) |
232 				    BIT(IEEE80211_AC_BK))) {
233 		cmd->flags |= cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK);
234 		cmd->snooze_interval = cpu_to_le16(IWL_MVM_PS_SNOOZE_INTERVAL);
235 		cmd->snooze_window =
236 			test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status) ?
237 				cpu_to_le16(IWL_MVM_WOWLAN_PS_SNOOZE_WINDOW) :
238 				cpu_to_le16(IWL_MVM_PS_SNOOZE_WINDOW);
239 	}
240 
241 	cmd->uapsd_max_sp = mvm->hw->uapsd_max_sp_len;
242 
243 	if (test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status) ||
244 	    cmd->flags & cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)) {
245 		cmd->rx_data_timeout_uapsd =
246 			cpu_to_le32(IWL_MVM_WOWLAN_PS_RX_DATA_TIMEOUT);
247 		cmd->tx_data_timeout_uapsd =
248 			cpu_to_le32(IWL_MVM_WOWLAN_PS_TX_DATA_TIMEOUT);
249 	} else {
250 		cmd->rx_data_timeout_uapsd =
251 			cpu_to_le32(IWL_MVM_UAPSD_RX_DATA_TIMEOUT);
252 		cmd->tx_data_timeout_uapsd =
253 			cpu_to_le32(IWL_MVM_UAPSD_TX_DATA_TIMEOUT);
254 	}
255 
256 	if (cmd->flags & cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)) {
257 		cmd->heavy_tx_thld_packets =
258 			IWL_MVM_PS_SNOOZE_HEAVY_TX_THLD_PACKETS;
259 		cmd->heavy_rx_thld_packets =
260 			IWL_MVM_PS_SNOOZE_HEAVY_RX_THLD_PACKETS;
261 	} else {
262 		cmd->heavy_tx_thld_packets =
263 			IWL_MVM_PS_HEAVY_TX_THLD_PACKETS;
264 		cmd->heavy_rx_thld_packets =
265 			IWL_MVM_PS_HEAVY_RX_THLD_PACKETS;
266 	}
267 	cmd->heavy_tx_thld_percentage =
268 		IWL_MVM_PS_HEAVY_TX_THLD_PERCENT;
269 	cmd->heavy_rx_thld_percentage =
270 		IWL_MVM_PS_HEAVY_RX_THLD_PERCENT;
271 }
272 
iwl_mvm_p2p_standalone_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)273 static void iwl_mvm_p2p_standalone_iterator(void *_data, u8 *mac,
274 					    struct ieee80211_vif *vif)
275 {
276 	bool *is_p2p_standalone = _data;
277 
278 	switch (ieee80211_vif_type_p2p(vif)) {
279 	case NL80211_IFTYPE_P2P_GO:
280 	case NL80211_IFTYPE_AP:
281 		*is_p2p_standalone = false;
282 		break;
283 	case NL80211_IFTYPE_STATION:
284 		if (vif->bss_conf.assoc)
285 			*is_p2p_standalone = false;
286 		break;
287 
288 	default:
289 		break;
290 	}
291 }
292 
iwl_mvm_power_allow_uapsd(struct iwl_mvm * mvm,struct ieee80211_vif * vif)293 static bool iwl_mvm_power_allow_uapsd(struct iwl_mvm *mvm,
294 				       struct ieee80211_vif *vif)
295 {
296 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
297 
298 	if (!memcmp(mvmvif->uapsd_misbehaving_bssid, vif->bss_conf.bssid,
299 		    ETH_ALEN))
300 		return false;
301 
302 	/*
303 	 * Avoid using uAPSD if P2P client is associated to GO that uses
304 	 * opportunistic power save. This is due to current FW limitation.
305 	 */
306 	if (vif->p2p &&
307 	    (vif->bss_conf.p2p_noa_attr.oppps_ctwindow &
308 	    IEEE80211_P2P_OPPPS_ENABLE_BIT))
309 		return false;
310 
311 	/*
312 	 * Avoid using uAPSD if client is in DCM -
313 	 * low latency issue in Miracast
314 	 */
315 	if (iwl_mvm_phy_ctx_count(mvm) >= 2)
316 		return false;
317 
318 	if (vif->p2p) {
319 		/* Allow U-APSD only if p2p is stand alone */
320 		bool is_p2p_standalone = true;
321 
322 		if (!iwl_mvm_is_p2p_scm_uapsd_supported(mvm))
323 			return false;
324 
325 		ieee80211_iterate_active_interfaces_atomic(mvm->hw,
326 					IEEE80211_IFACE_ITER_NORMAL,
327 					iwl_mvm_p2p_standalone_iterator,
328 					&is_p2p_standalone);
329 
330 		if (!is_p2p_standalone)
331 			return false;
332 	}
333 
334 	return true;
335 }
336 
iwl_mvm_power_is_radar(struct ieee80211_vif * vif)337 static bool iwl_mvm_power_is_radar(struct ieee80211_vif *vif)
338 {
339 	struct ieee80211_chanctx_conf *chanctx_conf;
340 	struct ieee80211_channel *chan;
341 	bool radar_detect = false;
342 
343 	rcu_read_lock();
344 	chanctx_conf = rcu_dereference(vif->chanctx_conf);
345 	WARN_ON(!chanctx_conf);
346 	if (chanctx_conf) {
347 		chan = chanctx_conf->def.chan;
348 		radar_detect = chan->flags & IEEE80211_CHAN_RADAR;
349 	}
350 	rcu_read_unlock();
351 
352 	return radar_detect;
353 }
354 
iwl_mvm_power_config_skip_dtim(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_mac_power_cmd * cmd)355 static void iwl_mvm_power_config_skip_dtim(struct iwl_mvm *mvm,
356 					   struct ieee80211_vif *vif,
357 					   struct iwl_mac_power_cmd *cmd)
358 {
359 	int dtimper = vif->bss_conf.dtim_period ?: 1;
360 	int skip;
361 
362 	/* disable, in case we're supposed to override */
363 	cmd->skip_dtim_periods = 0;
364 	cmd->flags &= ~cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
365 
366 	if (iwl_mvm_power_is_radar(vif))
367 		return;
368 
369 	if (dtimper >= 10)
370 		return;
371 
372 	if (!test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status)) {
373 		if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_LP)
374 			return;
375 		skip = 2;
376 	} else {
377 		int dtimper_tu = dtimper * vif->bss_conf.beacon_int;
378 
379 		if (WARN_ON(!dtimper_tu))
380 			return;
381 		/* configure skip over dtim up to 306TU - 314 msec */
382 		skip = max_t(u8, 1, 306 / dtimper_tu);
383 	}
384 
385 	/* the firmware really expects "look at every X DTIMs", so add 1 */
386 	cmd->skip_dtim_periods = 1 + skip;
387 	cmd->flags |= cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
388 }
389 
iwl_mvm_power_build_cmd(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_mac_power_cmd * cmd)390 static void iwl_mvm_power_build_cmd(struct iwl_mvm *mvm,
391 				    struct ieee80211_vif *vif,
392 				    struct iwl_mac_power_cmd *cmd)
393 {
394 	int dtimper, bi;
395 	int keep_alive;
396 	struct iwl_mvm_vif *mvmvif __maybe_unused =
397 		iwl_mvm_vif_from_mac80211(vif);
398 
399 	cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
400 							    mvmvif->color));
401 	dtimper = vif->bss_conf.dtim_period;
402 	bi = vif->bss_conf.beacon_int;
403 
404 	/*
405 	 * Regardless of power management state the driver must set
406 	 * keep alive period. FW will use it for sending keep alive NDPs
407 	 * immediately after association. Check that keep alive period
408 	 * is at least 3 * DTIM
409 	 */
410 	keep_alive = DIV_ROUND_UP(ieee80211_tu_to_usec(3 * dtimper * bi),
411 				  USEC_PER_SEC);
412 	keep_alive = max(keep_alive, POWER_KEEP_ALIVE_PERIOD_SEC);
413 	cmd->keep_alive_seconds = cpu_to_le16(keep_alive);
414 
415 	if (mvm->ps_disabled)
416 		return;
417 
418 	cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK);
419 
420 	if (!vif->bss_conf.ps || !mvmvif->pm_enabled)
421 		return;
422 
423 	if (iwl_mvm_vif_low_latency(mvmvif) && vif->p2p &&
424 	    (!fw_has_capa(&mvm->fw->ucode_capa,
425 			 IWL_UCODE_TLV_CAPA_SHORT_PM_TIMEOUTS) ||
426 	     !IWL_MVM_P2P_LOWLATENCY_PS_ENABLE))
427 		return;
428 
429 	cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK);
430 
431 	if (vif->bss_conf.beacon_rate &&
432 	    (vif->bss_conf.beacon_rate->bitrate == 10 ||
433 	     vif->bss_conf.beacon_rate->bitrate == 60)) {
434 		cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
435 		cmd->lprx_rssi_threshold = POWER_LPRX_RSSI_THRESHOLD;
436 	}
437 
438 	iwl_mvm_power_config_skip_dtim(mvm, vif, cmd);
439 
440 	if (test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status)) {
441 		cmd->rx_data_timeout =
442 			cpu_to_le32(IWL_MVM_WOWLAN_PS_RX_DATA_TIMEOUT);
443 		cmd->tx_data_timeout =
444 			cpu_to_le32(IWL_MVM_WOWLAN_PS_TX_DATA_TIMEOUT);
445 	} else if (iwl_mvm_vif_low_latency(mvmvif) && vif->p2p &&
446 		   fw_has_capa(&mvm->fw->ucode_capa,
447 			       IWL_UCODE_TLV_CAPA_SHORT_PM_TIMEOUTS)) {
448 		cmd->tx_data_timeout =
449 			cpu_to_le32(IWL_MVM_SHORT_PS_TX_DATA_TIMEOUT);
450 		cmd->rx_data_timeout =
451 			cpu_to_le32(IWL_MVM_SHORT_PS_RX_DATA_TIMEOUT);
452 	} else {
453 		cmd->rx_data_timeout =
454 			cpu_to_le32(IWL_MVM_DEFAULT_PS_RX_DATA_TIMEOUT);
455 		cmd->tx_data_timeout =
456 			cpu_to_le32(IWL_MVM_DEFAULT_PS_TX_DATA_TIMEOUT);
457 	}
458 
459 	if (iwl_mvm_power_allow_uapsd(mvm, vif))
460 		iwl_mvm_power_configure_uapsd(mvm, vif, cmd);
461 
462 #ifdef CONFIG_IWLWIFI_DEBUGFS
463 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_KEEP_ALIVE)
464 		cmd->keep_alive_seconds =
465 			cpu_to_le16(mvmvif->dbgfs_pm.keep_alive_seconds);
466 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_OVER_DTIM) {
467 		if (mvmvif->dbgfs_pm.skip_over_dtim)
468 			cmd->flags |=
469 				cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
470 		else
471 			cmd->flags &=
472 				cpu_to_le16(~POWER_FLAGS_SKIP_OVER_DTIM_MSK);
473 	}
474 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_RX_DATA_TIMEOUT)
475 		cmd->rx_data_timeout =
476 			cpu_to_le32(mvmvif->dbgfs_pm.rx_data_timeout);
477 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_TX_DATA_TIMEOUT)
478 		cmd->tx_data_timeout =
479 			cpu_to_le32(mvmvif->dbgfs_pm.tx_data_timeout);
480 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_DTIM_PERIODS)
481 		cmd->skip_dtim_periods = mvmvif->dbgfs_pm.skip_dtim_periods;
482 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_ENA) {
483 		if (mvmvif->dbgfs_pm.lprx_ena)
484 			cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
485 		else
486 			cmd->flags &= cpu_to_le16(~POWER_FLAGS_LPRX_ENA_MSK);
487 	}
488 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_RSSI_THRESHOLD)
489 		cmd->lprx_rssi_threshold = mvmvif->dbgfs_pm.lprx_rssi_threshold;
490 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SNOOZE_ENABLE) {
491 		if (mvmvif->dbgfs_pm.snooze_ena)
492 			cmd->flags |=
493 				cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK);
494 		else
495 			cmd->flags &=
496 				cpu_to_le16(~POWER_FLAGS_SNOOZE_ENA_MSK);
497 	}
498 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_UAPSD_MISBEHAVING) {
499 		u16 flag = POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK;
500 		if (mvmvif->dbgfs_pm.uapsd_misbehaving)
501 			cmd->flags |= cpu_to_le16(flag);
502 		else
503 			cmd->flags &= cpu_to_le16(flag);
504 	}
505 #endif /* CONFIG_IWLWIFI_DEBUGFS */
506 }
507 
iwl_mvm_power_send_cmd(struct iwl_mvm * mvm,struct ieee80211_vif * vif)508 static int iwl_mvm_power_send_cmd(struct iwl_mvm *mvm,
509 					 struct ieee80211_vif *vif)
510 {
511 	struct iwl_mac_power_cmd cmd = {};
512 
513 	iwl_mvm_power_build_cmd(mvm, vif, &cmd);
514 	iwl_mvm_power_log(mvm, &cmd);
515 #ifdef CONFIG_IWLWIFI_DEBUGFS
516 	memcpy(&iwl_mvm_vif_from_mac80211(vif)->mac_pwr_cmd, &cmd, sizeof(cmd));
517 #endif
518 
519 	return iwl_mvm_send_cmd_pdu(mvm, MAC_PM_POWER_TABLE, 0,
520 				    sizeof(cmd), &cmd);
521 }
522 
iwl_mvm_power_update_device(struct iwl_mvm * mvm)523 int iwl_mvm_power_update_device(struct iwl_mvm *mvm)
524 {
525 	struct iwl_device_power_cmd cmd = {
526 		.flags = 0,
527 	};
528 
529 	if (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM)
530 		mvm->ps_disabled = true;
531 
532 	if (!mvm->ps_disabled)
533 		cmd.flags |= cpu_to_le16(DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK);
534 
535 #ifdef CONFIG_IWLWIFI_DEBUGFS
536 	if (test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status) ?
537 			mvm->disable_power_off_d3 : mvm->disable_power_off)
538 		cmd.flags &=
539 			cpu_to_le16(~DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK);
540 #endif
541 	if (mvm->ext_clock_valid)
542 		cmd.flags |= cpu_to_le16(DEVICE_POWER_FLAGS_32K_CLK_VALID_MSK);
543 
544 	IWL_DEBUG_POWER(mvm,
545 			"Sending device power command with flags = 0x%X\n",
546 			cmd.flags);
547 
548 	return iwl_mvm_send_cmd_pdu(mvm, POWER_TABLE_CMD, 0, sizeof(cmd),
549 				    &cmd);
550 }
551 
iwl_mvm_power_vif_assoc(struct iwl_mvm * mvm,struct ieee80211_vif * vif)552 void iwl_mvm_power_vif_assoc(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
553 {
554 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
555 
556 	if (memcmp(vif->bss_conf.bssid, mvmvif->uapsd_misbehaving_bssid,
557 		   ETH_ALEN))
558 		eth_zero_addr(mvmvif->uapsd_misbehaving_bssid);
559 }
560 
iwl_mvm_power_uapsd_misbehav_ap_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)561 static void iwl_mvm_power_uapsd_misbehav_ap_iterator(void *_data, u8 *mac,
562 						     struct ieee80211_vif *vif)
563 {
564 	u8 *ap_sta_id = _data;
565 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
566 
567 	/* The ap_sta_id is not expected to change during current association
568 	 * so no explicit protection is needed
569 	 */
570 	if (mvmvif->ap_sta_id == *ap_sta_id)
571 		memcpy(mvmvif->uapsd_misbehaving_bssid, vif->bss_conf.bssid,
572 		       ETH_ALEN);
573 }
574 
iwl_mvm_power_uapsd_misbehaving_ap_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)575 void iwl_mvm_power_uapsd_misbehaving_ap_notif(struct iwl_mvm *mvm,
576 					      struct iwl_rx_cmd_buffer *rxb)
577 {
578 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
579 	struct iwl_uapsd_misbehaving_ap_notif *notif = (void *)pkt->data;
580 	u8 ap_sta_id = le32_to_cpu(notif->sta_id);
581 
582 	ieee80211_iterate_active_interfaces_atomic(
583 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
584 		iwl_mvm_power_uapsd_misbehav_ap_iterator, &ap_sta_id);
585 }
586 
587 struct iwl_power_vifs {
588 	struct iwl_mvm *mvm;
589 	struct ieee80211_vif *bss_vif;
590 	struct ieee80211_vif *p2p_vif;
591 	struct ieee80211_vif *ap_vif;
592 	struct ieee80211_vif *monitor_vif;
593 	bool p2p_active;
594 	bool bss_active;
595 	bool ap_active;
596 	bool monitor_active;
597 };
598 
iwl_mvm_power_disable_pm_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)599 static void iwl_mvm_power_disable_pm_iterator(void *_data, u8* mac,
600 					      struct ieee80211_vif *vif)
601 {
602 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
603 
604 	mvmvif->pm_enabled = false;
605 }
606 
iwl_mvm_power_ps_disabled_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)607 static void iwl_mvm_power_ps_disabled_iterator(void *_data, u8* mac,
608 					       struct ieee80211_vif *vif)
609 {
610 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
611 	bool *disable_ps = _data;
612 
613 	if (mvmvif->phy_ctxt && mvmvif->phy_ctxt->id < NUM_PHY_CTX)
614 		*disable_ps |= mvmvif->ps_disabled;
615 }
616 
iwl_mvm_power_get_vifs_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)617 static void iwl_mvm_power_get_vifs_iterator(void *_data, u8 *mac,
618 					    struct ieee80211_vif *vif)
619 {
620 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
621 	struct iwl_power_vifs *power_iterator = _data;
622 	bool active = mvmvif->phy_ctxt && mvmvif->phy_ctxt->id < NUM_PHY_CTX;
623 
624 	switch (ieee80211_vif_type_p2p(vif)) {
625 	case NL80211_IFTYPE_P2P_DEVICE:
626 		break;
627 
628 	case NL80211_IFTYPE_P2P_GO:
629 	case NL80211_IFTYPE_AP:
630 		/* only a single MAC of the same type */
631 		WARN_ON(power_iterator->ap_vif);
632 		power_iterator->ap_vif = vif;
633 		if (active)
634 			power_iterator->ap_active = true;
635 		break;
636 
637 	case NL80211_IFTYPE_MONITOR:
638 		/* only a single MAC of the same type */
639 		WARN_ON(power_iterator->monitor_vif);
640 		power_iterator->monitor_vif = vif;
641 		if (active)
642 			power_iterator->monitor_active = true;
643 		break;
644 
645 	case NL80211_IFTYPE_P2P_CLIENT:
646 		/* only a single MAC of the same type */
647 		WARN_ON(power_iterator->p2p_vif);
648 		power_iterator->p2p_vif = vif;
649 		if (active)
650 			power_iterator->p2p_active = true;
651 		break;
652 
653 	case NL80211_IFTYPE_STATION:
654 		power_iterator->bss_vif = vif;
655 		if (active)
656 			power_iterator->bss_active = true;
657 		break;
658 
659 	default:
660 		break;
661 	}
662 }
663 
iwl_mvm_power_set_pm(struct iwl_mvm * mvm,struct iwl_power_vifs * vifs)664 static void iwl_mvm_power_set_pm(struct iwl_mvm *mvm,
665 				 struct iwl_power_vifs *vifs)
666 {
667 	struct iwl_mvm_vif *bss_mvmvif = NULL;
668 	struct iwl_mvm_vif *p2p_mvmvif = NULL;
669 	struct iwl_mvm_vif *ap_mvmvif = NULL;
670 	bool client_same_channel = false;
671 	bool ap_same_channel = false;
672 
673 	lockdep_assert_held(&mvm->mutex);
674 
675 	/* set pm_enable to false */
676 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
677 					IEEE80211_IFACE_ITER_NORMAL,
678 					iwl_mvm_power_disable_pm_iterator,
679 					NULL);
680 
681 	if (vifs->bss_vif)
682 		bss_mvmvif = iwl_mvm_vif_from_mac80211(vifs->bss_vif);
683 
684 	if (vifs->p2p_vif)
685 		p2p_mvmvif = iwl_mvm_vif_from_mac80211(vifs->p2p_vif);
686 
687 	if (vifs->ap_vif)
688 		ap_mvmvif = iwl_mvm_vif_from_mac80211(vifs->ap_vif);
689 
690 	/* don't allow PM if any TDLS stations exist */
691 	if (iwl_mvm_tdls_sta_count(mvm, NULL))
692 		return;
693 
694 	/* enable PM on bss if bss stand alone */
695 	if (vifs->bss_active && !vifs->p2p_active && !vifs->ap_active) {
696 		bss_mvmvif->pm_enabled = true;
697 		return;
698 	}
699 
700 	/* enable PM on p2p if p2p stand alone */
701 	if (vifs->p2p_active && !vifs->bss_active && !vifs->ap_active) {
702 		p2p_mvmvif->pm_enabled = true;
703 		return;
704 	}
705 
706 	if (vifs->bss_active && vifs->p2p_active)
707 		client_same_channel = (bss_mvmvif->phy_ctxt->id ==
708 				       p2p_mvmvif->phy_ctxt->id);
709 	if (vifs->bss_active && vifs->ap_active)
710 		ap_same_channel = (bss_mvmvif->phy_ctxt->id ==
711 				   ap_mvmvif->phy_ctxt->id);
712 
713 	/* clients are not stand alone: enable PM if DCM */
714 	if (!(client_same_channel || ap_same_channel)) {
715 		if (vifs->bss_active)
716 			bss_mvmvif->pm_enabled = true;
717 		if (vifs->p2p_active)
718 			p2p_mvmvif->pm_enabled = true;
719 		return;
720 	}
721 
722 	/*
723 	 * There is only one channel in the system and there are only
724 	 * bss and p2p clients that share it
725 	 */
726 	if (client_same_channel && !vifs->ap_active) {
727 		/* share same channel*/
728 		bss_mvmvif->pm_enabled = true;
729 		p2p_mvmvif->pm_enabled = true;
730 	}
731 }
732 
733 #ifdef CONFIG_IWLWIFI_DEBUGFS
iwl_mvm_power_mac_dbgfs_read(struct iwl_mvm * mvm,struct ieee80211_vif * vif,char * buf,int bufsz)734 int iwl_mvm_power_mac_dbgfs_read(struct iwl_mvm *mvm,
735 				 struct ieee80211_vif *vif, char *buf,
736 				 int bufsz)
737 {
738 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
739 	struct iwl_mac_power_cmd cmd = {};
740 	int pos = 0;
741 
742 	mutex_lock(&mvm->mutex);
743 	memcpy(&cmd, &mvmvif->mac_pwr_cmd, sizeof(cmd));
744 	mutex_unlock(&mvm->mutex);
745 
746 	pos += scnprintf(buf+pos, bufsz-pos, "power_scheme = %d\n",
747 			 iwlmvm_mod_params.power_scheme);
748 	pos += scnprintf(buf+pos, bufsz-pos, "flags = 0x%x\n",
749 			 le16_to_cpu(cmd.flags));
750 	pos += scnprintf(buf+pos, bufsz-pos, "keep_alive = %d\n",
751 			 le16_to_cpu(cmd.keep_alive_seconds));
752 
753 	if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK)))
754 		return pos;
755 
756 	pos += scnprintf(buf+pos, bufsz-pos, "skip_over_dtim = %d\n",
757 			 (cmd.flags &
758 			 cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK)) ? 1 : 0);
759 	pos += scnprintf(buf+pos, bufsz-pos, "skip_dtim_periods = %d\n",
760 			 cmd.skip_dtim_periods);
761 	if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK))) {
762 		pos += scnprintf(buf+pos, bufsz-pos, "rx_data_timeout = %d\n",
763 				 le32_to_cpu(cmd.rx_data_timeout));
764 		pos += scnprintf(buf+pos, bufsz-pos, "tx_data_timeout = %d\n",
765 				 le32_to_cpu(cmd.tx_data_timeout));
766 	}
767 	if (cmd.flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
768 		pos += scnprintf(buf+pos, bufsz-pos,
769 				 "lprx_rssi_threshold = %d\n",
770 				 cmd.lprx_rssi_threshold);
771 
772 	if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK)))
773 		return pos;
774 
775 	pos += scnprintf(buf+pos, bufsz-pos, "rx_data_timeout_uapsd = %d\n",
776 			 le32_to_cpu(cmd.rx_data_timeout_uapsd));
777 	pos += scnprintf(buf+pos, bufsz-pos, "tx_data_timeout_uapsd = %d\n",
778 			 le32_to_cpu(cmd.tx_data_timeout_uapsd));
779 	pos += scnprintf(buf+pos, bufsz-pos, "qndp_tid = %d\n", cmd.qndp_tid);
780 	pos += scnprintf(buf+pos, bufsz-pos, "uapsd_ac_flags = 0x%x\n",
781 			 cmd.uapsd_ac_flags);
782 	pos += scnprintf(buf+pos, bufsz-pos, "uapsd_max_sp = %d\n",
783 			 cmd.uapsd_max_sp);
784 	pos += scnprintf(buf+pos, bufsz-pos, "heavy_tx_thld_packets = %d\n",
785 			 cmd.heavy_tx_thld_packets);
786 	pos += scnprintf(buf+pos, bufsz-pos, "heavy_rx_thld_packets = %d\n",
787 			 cmd.heavy_rx_thld_packets);
788 	pos += scnprintf(buf+pos, bufsz-pos, "heavy_tx_thld_percentage = %d\n",
789 			 cmd.heavy_tx_thld_percentage);
790 	pos += scnprintf(buf+pos, bufsz-pos, "heavy_rx_thld_percentage = %d\n",
791 			 cmd.heavy_rx_thld_percentage);
792 	pos += scnprintf(buf+pos, bufsz-pos, "uapsd_misbehaving_enable = %d\n",
793 			 (cmd.flags &
794 			  cpu_to_le16(POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK)) ?
795 			 1 : 0);
796 
797 	if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)))
798 		return pos;
799 
800 	pos += scnprintf(buf+pos, bufsz-pos, "snooze_interval = %d\n",
801 			 cmd.snooze_interval);
802 	pos += scnprintf(buf+pos, bufsz-pos, "snooze_window = %d\n",
803 			 cmd.snooze_window);
804 
805 	return pos;
806 }
807 
808 void
iwl_mvm_beacon_filter_debugfs_parameters(struct ieee80211_vif * vif,struct iwl_beacon_filter_cmd * cmd)809 iwl_mvm_beacon_filter_debugfs_parameters(struct ieee80211_vif *vif,
810 					 struct iwl_beacon_filter_cmd *cmd)
811 {
812 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
813 	struct iwl_dbgfs_bf *dbgfs_bf = &mvmvif->dbgfs_bf;
814 
815 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ENERGY_DELTA)
816 		cmd->bf_energy_delta = cpu_to_le32(dbgfs_bf->bf_energy_delta);
817 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ROAMING_ENERGY_DELTA)
818 		cmd->bf_roaming_energy_delta =
819 				cpu_to_le32(dbgfs_bf->bf_roaming_energy_delta);
820 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ROAMING_STATE)
821 		cmd->bf_roaming_state = cpu_to_le32(dbgfs_bf->bf_roaming_state);
822 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_THRESHOLD)
823 		cmd->bf_temp_threshold =
824 				cpu_to_le32(dbgfs_bf->bf_temp_threshold);
825 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_FAST_FILTER)
826 		cmd->bf_temp_fast_filter =
827 				cpu_to_le32(dbgfs_bf->bf_temp_fast_filter);
828 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_SLOW_FILTER)
829 		cmd->bf_temp_slow_filter =
830 				cpu_to_le32(dbgfs_bf->bf_temp_slow_filter);
831 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_DEBUG_FLAG)
832 		cmd->bf_debug_flag = cpu_to_le32(dbgfs_bf->bf_debug_flag);
833 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ESCAPE_TIMER)
834 		cmd->bf_escape_timer = cpu_to_le32(dbgfs_bf->bf_escape_timer);
835 	if (dbgfs_bf->mask & MVM_DEBUGFS_BA_ESCAPE_TIMER)
836 		cmd->ba_escape_timer = cpu_to_le32(dbgfs_bf->ba_escape_timer);
837 	if (dbgfs_bf->mask & MVM_DEBUGFS_BA_ENABLE_BEACON_ABORT)
838 		cmd->ba_enable_beacon_abort =
839 				cpu_to_le32(dbgfs_bf->ba_enable_beacon_abort);
840 }
841 #endif
842 
_iwl_mvm_enable_beacon_filter(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_beacon_filter_cmd * cmd,u32 cmd_flags)843 static int _iwl_mvm_enable_beacon_filter(struct iwl_mvm *mvm,
844 					 struct ieee80211_vif *vif,
845 					 struct iwl_beacon_filter_cmd *cmd,
846 					 u32 cmd_flags)
847 {
848 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
849 	int ret;
850 
851 	if (mvmvif != mvm->bf_allowed_vif || !vif->bss_conf.dtim_period ||
852 	    vif->type != NL80211_IFTYPE_STATION || vif->p2p)
853 		return 0;
854 
855 	iwl_mvm_beacon_filter_set_cqm_params(mvm, vif, cmd);
856 	iwl_mvm_beacon_filter_debugfs_parameters(vif, cmd);
857 	ret = iwl_mvm_beacon_filter_send_cmd(mvm, cmd, cmd_flags);
858 
859 	if (!ret)
860 		mvmvif->bf_data.bf_enabled = true;
861 
862 	return ret;
863 }
864 
iwl_mvm_enable_beacon_filter(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 flags)865 int iwl_mvm_enable_beacon_filter(struct iwl_mvm *mvm,
866 				 struct ieee80211_vif *vif,
867 				 u32 flags)
868 {
869 	struct iwl_beacon_filter_cmd cmd = {
870 		IWL_BF_CMD_CONFIG_DEFAULTS,
871 		.bf_enable_beacon_filter = cpu_to_le32(1),
872 	};
873 
874 	return _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd, flags);
875 }
876 
_iwl_mvm_disable_beacon_filter(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 flags)877 static int _iwl_mvm_disable_beacon_filter(struct iwl_mvm *mvm,
878 					  struct ieee80211_vif *vif,
879 					  u32 flags)
880 {
881 	struct iwl_beacon_filter_cmd cmd = {};
882 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
883 	int ret;
884 
885 	if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
886 		return 0;
887 
888 	ret = iwl_mvm_beacon_filter_send_cmd(mvm, &cmd, flags);
889 
890 	if (!ret)
891 		mvmvif->bf_data.bf_enabled = false;
892 
893 	return ret;
894 }
895 
iwl_mvm_disable_beacon_filter(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 flags)896 int iwl_mvm_disable_beacon_filter(struct iwl_mvm *mvm,
897 				  struct ieee80211_vif *vif,
898 				  u32 flags)
899 {
900 	return _iwl_mvm_disable_beacon_filter(mvm, vif, flags);
901 }
902 
iwl_mvm_power_set_ps(struct iwl_mvm * mvm)903 static int iwl_mvm_power_set_ps(struct iwl_mvm *mvm)
904 {
905 	bool disable_ps;
906 	int ret;
907 
908 	/* disable PS if CAM */
909 	disable_ps = (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM);
910 	/* ...or if any of the vifs require PS to be off */
911 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
912 					IEEE80211_IFACE_ITER_NORMAL,
913 					iwl_mvm_power_ps_disabled_iterator,
914 					&disable_ps);
915 
916 	/* update device power state if it has changed */
917 	if (mvm->ps_disabled != disable_ps) {
918 		bool old_ps_disabled = mvm->ps_disabled;
919 
920 		mvm->ps_disabled = disable_ps;
921 		ret = iwl_mvm_power_update_device(mvm);
922 		if (ret) {
923 			mvm->ps_disabled = old_ps_disabled;
924 			return ret;
925 		}
926 	}
927 
928 	return 0;
929 }
930 
iwl_mvm_power_set_ba(struct iwl_mvm * mvm,struct ieee80211_vif * vif)931 static int iwl_mvm_power_set_ba(struct iwl_mvm *mvm,
932 				struct ieee80211_vif *vif)
933 {
934 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
935 	struct iwl_beacon_filter_cmd cmd = {
936 		IWL_BF_CMD_CONFIG_DEFAULTS,
937 		.bf_enable_beacon_filter = cpu_to_le32(1),
938 	};
939 
940 	if (!mvmvif->bf_data.bf_enabled)
941 		return 0;
942 
943 	if (test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status))
944 		cmd.ba_escape_timer = cpu_to_le32(IWL_BA_ESCAPE_TIMER_D3);
945 
946 	mvmvif->bf_data.ba_enabled = !(!mvmvif->pm_enabled ||
947 				       mvm->ps_disabled ||
948 				       !vif->bss_conf.ps ||
949 				       iwl_mvm_vif_low_latency(mvmvif));
950 
951 	return _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd, 0);
952 }
953 
iwl_mvm_power_update_ps(struct iwl_mvm * mvm)954 int iwl_mvm_power_update_ps(struct iwl_mvm *mvm)
955 {
956 	struct iwl_power_vifs vifs = {
957 		.mvm = mvm,
958 	};
959 	int ret;
960 
961 	lockdep_assert_held(&mvm->mutex);
962 
963 	/* get vifs info */
964 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
965 					IEEE80211_IFACE_ITER_NORMAL,
966 					iwl_mvm_power_get_vifs_iterator, &vifs);
967 
968 	ret = iwl_mvm_power_set_ps(mvm);
969 	if (ret)
970 		return ret;
971 
972 	if (vifs.bss_vif)
973 		return iwl_mvm_power_set_ba(mvm, vifs.bss_vif);
974 
975 	return 0;
976 }
977 
iwl_mvm_power_update_mac(struct iwl_mvm * mvm)978 int iwl_mvm_power_update_mac(struct iwl_mvm *mvm)
979 {
980 	struct iwl_power_vifs vifs = {
981 		.mvm = mvm,
982 	};
983 	int ret;
984 
985 	lockdep_assert_held(&mvm->mutex);
986 
987 	/* get vifs info */
988 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
989 					IEEE80211_IFACE_ITER_NORMAL,
990 					iwl_mvm_power_get_vifs_iterator, &vifs);
991 
992 	iwl_mvm_power_set_pm(mvm, &vifs);
993 
994 	ret = iwl_mvm_power_set_ps(mvm);
995 	if (ret)
996 		return ret;
997 
998 	if (vifs.bss_vif) {
999 		ret = iwl_mvm_power_send_cmd(mvm, vifs.bss_vif);
1000 		if (ret)
1001 			return ret;
1002 	}
1003 
1004 	if (vifs.p2p_vif) {
1005 		ret = iwl_mvm_power_send_cmd(mvm, vifs.p2p_vif);
1006 		if (ret)
1007 			return ret;
1008 	}
1009 
1010 	if (vifs.bss_vif)
1011 		return iwl_mvm_power_set_ba(mvm, vifs.bss_vif);
1012 
1013 	return 0;
1014 }
1015