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