Linux Audio

Check our new training course

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