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 
21 static
22 int 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 
69 static
70 void 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 
86 static 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 
123 static 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 
215 static 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 
235 static 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 
279 static 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 
299 out:
300 	rcu_read_unlock();
301 	return radar_detect;
302 }
303 
304 static 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 306TU - 314 msec */
331 		skip = max_t(u8, 1, 306 / dtimper_tu);
332 	}
333 
334 	/* the firmware really expects "look at every X DTIMs", so add 1 */
335 	cmd->skip_dtim_periods = 1 + skip;
336 	cmd->flags |= cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
337 }
338 
339 static void iwl_mvm_power_build_cmd(struct iwl_mvm *mvm,
340 				    struct ieee80211_vif *vif,
341 				    struct iwl_mac_power_cmd *cmd)
342 {
343 	int dtimper, bi;
344 	int keep_alive;
345 	struct iwl_mvm_vif *mvmvif __maybe_unused =
346 		iwl_mvm_vif_from_mac80211(vif);
347 
348 	cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
349 							    mvmvif->color));
350 	dtimper = vif->bss_conf.dtim_period;
351 	bi = vif->bss_conf.beacon_int;
352 
353 	/*
354 	 * Regardless of power management state the driver must set
355 	 * keep alive period. FW will use it for sending keep alive NDPs
356 	 * immediately after association. Check that keep alive period
357 	 * is at least 3 * DTIM
358 	 */
359 	keep_alive = DIV_ROUND_UP(ieee80211_tu_to_usec(3 * dtimper * bi),
360 				  USEC_PER_SEC);
361 	keep_alive = max(keep_alive, POWER_KEEP_ALIVE_PERIOD_SEC);
362 	cmd->keep_alive_seconds = cpu_to_le16(keep_alive);
363 
364 	if (mvm->ps_disabled)
365 		return;
366 
367 	cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK);
368 
369 	if (!vif->cfg.ps || !mvmvif->pm_enabled)
370 		return;
371 
372 	if (iwl_mvm_vif_low_latency(mvmvif) && vif->p2p &&
373 	    (!fw_has_capa(&mvm->fw->ucode_capa,
374 			 IWL_UCODE_TLV_CAPA_SHORT_PM_TIMEOUTS) ||
375 	     !IWL_MVM_P2P_LOWLATENCY_PS_ENABLE))
376 		return;
377 
378 	cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK);
379 
380 	if (vif->bss_conf.beacon_rate &&
381 	    (vif->bss_conf.beacon_rate->bitrate == 10 ||
382 	     vif->bss_conf.beacon_rate->bitrate == 60)) {
383 		cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
384 		cmd->lprx_rssi_threshold = POWER_LPRX_RSSI_THRESHOLD;
385 	}
386 
387 	iwl_mvm_power_config_skip_dtim(mvm, vif, cmd);
388 
389 	if (test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status)) {
390 		cmd->rx_data_timeout =
391 			cpu_to_le32(IWL_MVM_WOWLAN_PS_RX_DATA_TIMEOUT);
392 		cmd->tx_data_timeout =
393 			cpu_to_le32(IWL_MVM_WOWLAN_PS_TX_DATA_TIMEOUT);
394 	} else if (iwl_mvm_vif_low_latency(mvmvif) && vif->p2p &&
395 		   fw_has_capa(&mvm->fw->ucode_capa,
396 			       IWL_UCODE_TLV_CAPA_SHORT_PM_TIMEOUTS)) {
397 		cmd->tx_data_timeout =
398 			cpu_to_le32(IWL_MVM_SHORT_PS_TX_DATA_TIMEOUT);
399 		cmd->rx_data_timeout =
400 			cpu_to_le32(IWL_MVM_SHORT_PS_RX_DATA_TIMEOUT);
401 	} else {
402 		cmd->rx_data_timeout =
403 			cpu_to_le32(IWL_MVM_DEFAULT_PS_RX_DATA_TIMEOUT);
404 		cmd->tx_data_timeout =
405 			cpu_to_le32(IWL_MVM_DEFAULT_PS_TX_DATA_TIMEOUT);
406 	}
407 
408 	if (iwl_mvm_power_allow_uapsd(mvm, vif))
409 		iwl_mvm_power_configure_uapsd(mvm, vif, cmd);
410 
411 #ifdef CONFIG_IWLWIFI_DEBUGFS
412 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_KEEP_ALIVE)
413 		cmd->keep_alive_seconds =
414 			cpu_to_le16(mvmvif->dbgfs_pm.keep_alive_seconds);
415 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_OVER_DTIM) {
416 		if (mvmvif->dbgfs_pm.skip_over_dtim)
417 			cmd->flags |=
418 				cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
419 		else
420 			cmd->flags &=
421 				cpu_to_le16(~POWER_FLAGS_SKIP_OVER_DTIM_MSK);
422 	}
423 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_RX_DATA_TIMEOUT)
424 		cmd->rx_data_timeout =
425 			cpu_to_le32(mvmvif->dbgfs_pm.rx_data_timeout);
426 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_TX_DATA_TIMEOUT)
427 		cmd->tx_data_timeout =
428 			cpu_to_le32(mvmvif->dbgfs_pm.tx_data_timeout);
429 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_DTIM_PERIODS)
430 		cmd->skip_dtim_periods = mvmvif->dbgfs_pm.skip_dtim_periods;
431 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_ENA) {
432 		if (mvmvif->dbgfs_pm.lprx_ena)
433 			cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
434 		else
435 			cmd->flags &= cpu_to_le16(~POWER_FLAGS_LPRX_ENA_MSK);
436 	}
437 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_RSSI_THRESHOLD)
438 		cmd->lprx_rssi_threshold = mvmvif->dbgfs_pm.lprx_rssi_threshold;
439 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SNOOZE_ENABLE) {
440 		if (mvmvif->dbgfs_pm.snooze_ena)
441 			cmd->flags |=
442 				cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK);
443 		else
444 			cmd->flags &=
445 				cpu_to_le16(~POWER_FLAGS_SNOOZE_ENA_MSK);
446 	}
447 	if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_UAPSD_MISBEHAVING) {
448 		u16 flag = POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK;
449 		if (mvmvif->dbgfs_pm.uapsd_misbehaving)
450 			cmd->flags |= cpu_to_le16(flag);
451 		else
452 			cmd->flags &= cpu_to_le16(flag);
453 	}
454 #endif /* CONFIG_IWLWIFI_DEBUGFS */
455 }
456 
457 static int iwl_mvm_power_send_cmd(struct iwl_mvm *mvm,
458 					 struct ieee80211_vif *vif)
459 {
460 	struct iwl_mac_power_cmd cmd = {};
461 
462 	iwl_mvm_power_build_cmd(mvm, vif, &cmd);
463 	iwl_mvm_power_log(mvm, &cmd);
464 #ifdef CONFIG_IWLWIFI_DEBUGFS
465 	memcpy(&iwl_mvm_vif_from_mac80211(vif)->mac_pwr_cmd, &cmd, sizeof(cmd));
466 #endif
467 
468 	return iwl_mvm_send_cmd_pdu(mvm, MAC_PM_POWER_TABLE, 0,
469 				    sizeof(cmd), &cmd);
470 }
471 
472 int iwl_mvm_power_update_device(struct iwl_mvm *mvm)
473 {
474 	struct iwl_device_power_cmd cmd = {
475 		.flags = 0,
476 	};
477 
478 	if (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM)
479 		mvm->ps_disabled = true;
480 
481 	if (!mvm->ps_disabled)
482 		cmd.flags |= cpu_to_le16(DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK);
483 
484 #ifdef CONFIG_IWLWIFI_DEBUGFS
485 	if (test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status) ?
486 			mvm->disable_power_off_d3 : mvm->disable_power_off)
487 		cmd.flags &=
488 			cpu_to_le16(~DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK);
489 #endif
490 	if (mvm->ext_clock_valid)
491 		cmd.flags |= cpu_to_le16(DEVICE_POWER_FLAGS_32K_CLK_VALID_MSK);
492 
493 	IWL_DEBUG_POWER(mvm,
494 			"Sending device power command with flags = 0x%X\n",
495 			cmd.flags);
496 
497 	return iwl_mvm_send_cmd_pdu(mvm, POWER_TABLE_CMD, 0, sizeof(cmd),
498 				    &cmd);
499 }
500 
501 void iwl_mvm_power_vif_assoc(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
502 {
503 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
504 
505 	if (memcmp(vif->bss_conf.bssid, mvmvif->uapsd_misbehaving_bssid,
506 		   ETH_ALEN))
507 		eth_zero_addr(mvmvif->uapsd_misbehaving_bssid);
508 }
509 
510 static void iwl_mvm_power_uapsd_misbehav_ap_iterator(void *_data, u8 *mac,
511 						     struct ieee80211_vif *vif)
512 {
513 	u8 *ap_sta_id = _data;
514 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
515 
516 	/* The ap_sta_id is not expected to change during current association
517 	 * so no explicit protection is needed
518 	 */
519 	if (mvmvif->deflink.ap_sta_id == *ap_sta_id)
520 		memcpy(mvmvif->uapsd_misbehaving_bssid,
521 		       vif->bss_conf.bssid,
522 		       ETH_ALEN);
523 }
524 
525 void iwl_mvm_power_uapsd_misbehaving_ap_notif(struct iwl_mvm *mvm,
526 					      struct iwl_rx_cmd_buffer *rxb)
527 {
528 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
529 	struct iwl_uapsd_misbehaving_ap_notif *notif = (void *)pkt->data;
530 	u8 ap_sta_id = le32_to_cpu(notif->sta_id);
531 
532 	ieee80211_iterate_active_interfaces_atomic(
533 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
534 		iwl_mvm_power_uapsd_misbehav_ap_iterator, &ap_sta_id);
535 }
536 
537 struct iwl_power_vifs {
538 	struct iwl_mvm *mvm;
539 	struct ieee80211_vif *bss_vif;
540 	struct ieee80211_vif *p2p_vif;
541 	struct ieee80211_vif *ap_vif;
542 	struct ieee80211_vif *monitor_vif;
543 	bool p2p_active;
544 	bool bss_active;
545 	bool ap_active;
546 	bool monitor_active;
547 };
548 
549 static void iwl_mvm_power_disable_pm_iterator(void *_data, u8* mac,
550 					      struct ieee80211_vif *vif)
551 {
552 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
553 
554 	mvmvif->pm_enabled = false;
555 }
556 
557 static void iwl_mvm_power_ps_disabled_iterator(void *_data, u8* mac,
558 					       struct ieee80211_vif *vif)
559 {
560 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
561 	bool *disable_ps = _data;
562 
563 	if (iwl_mvm_vif_is_active(mvmvif))
564 		*disable_ps |= mvmvif->ps_disabled;
565 }
566 
567 static void iwl_mvm_power_get_vifs_iterator(void *_data, u8 *mac,
568 					    struct ieee80211_vif *vif)
569 {
570 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
571 	struct iwl_power_vifs *power_iterator = _data;
572 	bool active;
573 
574 	if (!mvmvif->uploaded)
575 		return;
576 
577 	active = iwl_mvm_vif_is_active(mvmvif);
578 
579 	switch (ieee80211_vif_type_p2p(vif)) {
580 	case NL80211_IFTYPE_P2P_DEVICE:
581 		break;
582 
583 	case NL80211_IFTYPE_P2P_GO:
584 	case NL80211_IFTYPE_AP:
585 		/* only a single MAC of the same type */
586 		WARN_ON(power_iterator->ap_vif);
587 		power_iterator->ap_vif = vif;
588 		if (active)
589 			power_iterator->ap_active = true;
590 		break;
591 
592 	case NL80211_IFTYPE_MONITOR:
593 		/* only a single MAC of the same type */
594 		WARN_ON(power_iterator->monitor_vif);
595 		power_iterator->monitor_vif = vif;
596 		if (active)
597 			power_iterator->monitor_active = true;
598 		break;
599 
600 	case NL80211_IFTYPE_P2P_CLIENT:
601 		/* only a single MAC of the same type */
602 		WARN_ON(power_iterator->p2p_vif);
603 		power_iterator->p2p_vif = vif;
604 		if (active)
605 			power_iterator->p2p_active = true;
606 		break;
607 
608 	case NL80211_IFTYPE_STATION:
609 		power_iterator->bss_vif = vif;
610 		if (active)
611 			power_iterator->bss_active = true;
612 		break;
613 
614 	default:
615 		break;
616 	}
617 }
618 
619 static void iwl_mvm_power_set_pm(struct iwl_mvm *mvm,
620 				 struct iwl_power_vifs *vifs)
621 {
622 	struct iwl_mvm_vif *bss_mvmvif = NULL;
623 	struct iwl_mvm_vif *p2p_mvmvif = NULL;
624 	struct iwl_mvm_vif *ap_mvmvif = NULL;
625 	bool client_same_channel = false;
626 	bool ap_same_channel = false;
627 
628 	lockdep_assert_held(&mvm->mutex);
629 
630 	/* set pm_enable to false */
631 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
632 					IEEE80211_IFACE_ITER_NORMAL,
633 					iwl_mvm_power_disable_pm_iterator,
634 					NULL);
635 
636 	if (vifs->bss_vif)
637 		bss_mvmvif = iwl_mvm_vif_from_mac80211(vifs->bss_vif);
638 
639 	if (vifs->p2p_vif)
640 		p2p_mvmvif = iwl_mvm_vif_from_mac80211(vifs->p2p_vif);
641 
642 	if (vifs->ap_vif)
643 		ap_mvmvif = iwl_mvm_vif_from_mac80211(vifs->ap_vif);
644 
645 	/* don't allow PM if any TDLS stations exist */
646 	if (iwl_mvm_tdls_sta_count(mvm, NULL))
647 		return;
648 
649 	/* enable PM on bss if bss stand alone */
650 	if (vifs->bss_active && !vifs->p2p_active && !vifs->ap_active) {
651 		bss_mvmvif->pm_enabled = true;
652 		return;
653 	}
654 
655 	/* enable PM on p2p if p2p stand alone */
656 	if (vifs->p2p_active && !vifs->bss_active && !vifs->ap_active) {
657 		p2p_mvmvif->pm_enabled = true;
658 		return;
659 	}
660 
661 	if (vifs->bss_active && vifs->p2p_active)
662 		client_same_channel =
663 			iwl_mvm_have_links_same_channel(bss_mvmvif, p2p_mvmvif);
664 
665 	if (vifs->bss_active && vifs->ap_active)
666 		ap_same_channel =
667 			iwl_mvm_have_links_same_channel(bss_mvmvif, ap_mvmvif);
668 
669 	/* clients are not stand alone: enable PM if DCM */
670 	if (!(client_same_channel || ap_same_channel)) {
671 		if (vifs->bss_active)
672 			bss_mvmvif->pm_enabled = true;
673 		if (vifs->p2p_active)
674 			p2p_mvmvif->pm_enabled = true;
675 		return;
676 	}
677 
678 	/*
679 	 * There is only one channel in the system and there are only
680 	 * bss and p2p clients that share it
681 	 */
682 	if (client_same_channel && !vifs->ap_active) {
683 		/* share same channel*/
684 		bss_mvmvif->pm_enabled = true;
685 		p2p_mvmvif->pm_enabled = true;
686 	}
687 }
688 
689 #ifdef CONFIG_IWLWIFI_DEBUGFS
690 int iwl_mvm_power_mac_dbgfs_read(struct iwl_mvm *mvm,
691 				 struct ieee80211_vif *vif, char *buf,
692 				 int bufsz)
693 {
694 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
695 	struct iwl_mac_power_cmd cmd = {};
696 	int pos = 0;
697 
698 	mutex_lock(&mvm->mutex);
699 	memcpy(&cmd, &mvmvif->mac_pwr_cmd, sizeof(cmd));
700 	mutex_unlock(&mvm->mutex);
701 
702 	pos += scnprintf(buf+pos, bufsz-pos, "power_scheme = %d\n",
703 			 iwlmvm_mod_params.power_scheme);
704 	pos += scnprintf(buf+pos, bufsz-pos, "flags = 0x%x\n",
705 			 le16_to_cpu(cmd.flags));
706 	pos += scnprintf(buf+pos, bufsz-pos, "keep_alive = %d\n",
707 			 le16_to_cpu(cmd.keep_alive_seconds));
708 
709 	if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK)))
710 		return pos;
711 
712 	pos += scnprintf(buf+pos, bufsz-pos, "skip_over_dtim = %d\n",
713 			 (cmd.flags &
714 			 cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK)) ? 1 : 0);
715 	pos += scnprintf(buf+pos, bufsz-pos, "skip_dtim_periods = %d\n",
716 			 cmd.skip_dtim_periods);
717 	if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK))) {
718 		pos += scnprintf(buf+pos, bufsz-pos, "rx_data_timeout = %d\n",
719 				 le32_to_cpu(cmd.rx_data_timeout));
720 		pos += scnprintf(buf+pos, bufsz-pos, "tx_data_timeout = %d\n",
721 				 le32_to_cpu(cmd.tx_data_timeout));
722 	}
723 	if (cmd.flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
724 		pos += scnprintf(buf+pos, bufsz-pos,
725 				 "lprx_rssi_threshold = %d\n",
726 				 cmd.lprx_rssi_threshold);
727 
728 	if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK)))
729 		return pos;
730 
731 	pos += scnprintf(buf+pos, bufsz-pos, "rx_data_timeout_uapsd = %d\n",
732 			 le32_to_cpu(cmd.rx_data_timeout_uapsd));
733 	pos += scnprintf(buf+pos, bufsz-pos, "tx_data_timeout_uapsd = %d\n",
734 			 le32_to_cpu(cmd.tx_data_timeout_uapsd));
735 	pos += scnprintf(buf+pos, bufsz-pos, "qndp_tid = %d\n", cmd.qndp_tid);
736 	pos += scnprintf(buf+pos, bufsz-pos, "uapsd_ac_flags = 0x%x\n",
737 			 cmd.uapsd_ac_flags);
738 	pos += scnprintf(buf+pos, bufsz-pos, "uapsd_max_sp = %d\n",
739 			 cmd.uapsd_max_sp);
740 	pos += scnprintf(buf+pos, bufsz-pos, "heavy_tx_thld_packets = %d\n",
741 			 cmd.heavy_tx_thld_packets);
742 	pos += scnprintf(buf+pos, bufsz-pos, "heavy_rx_thld_packets = %d\n",
743 			 cmd.heavy_rx_thld_packets);
744 	pos += scnprintf(buf+pos, bufsz-pos, "heavy_tx_thld_percentage = %d\n",
745 			 cmd.heavy_tx_thld_percentage);
746 	pos += scnprintf(buf+pos, bufsz-pos, "heavy_rx_thld_percentage = %d\n",
747 			 cmd.heavy_rx_thld_percentage);
748 	pos += scnprintf(buf+pos, bufsz-pos, "uapsd_misbehaving_enable = %d\n",
749 			 (cmd.flags &
750 			  cpu_to_le16(POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK)) ?
751 			 1 : 0);
752 
753 	if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)))
754 		return pos;
755 
756 	pos += scnprintf(buf+pos, bufsz-pos, "snooze_interval = %d\n",
757 			 cmd.snooze_interval);
758 	pos += scnprintf(buf+pos, bufsz-pos, "snooze_window = %d\n",
759 			 cmd.snooze_window);
760 
761 	return pos;
762 }
763 
764 void
765 iwl_mvm_beacon_filter_debugfs_parameters(struct ieee80211_vif *vif,
766 					 struct iwl_beacon_filter_cmd *cmd)
767 {
768 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
769 	struct iwl_dbgfs_bf *dbgfs_bf = &mvmvif->dbgfs_bf;
770 
771 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ENERGY_DELTA)
772 		cmd->bf_energy_delta = cpu_to_le32(dbgfs_bf->bf_energy_delta);
773 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ROAMING_ENERGY_DELTA)
774 		cmd->bf_roaming_energy_delta =
775 				cpu_to_le32(dbgfs_bf->bf_roaming_energy_delta);
776 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ROAMING_STATE)
777 		cmd->bf_roaming_state = cpu_to_le32(dbgfs_bf->bf_roaming_state);
778 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_THRESHOLD)
779 		cmd->bf_temp_threshold =
780 				cpu_to_le32(dbgfs_bf->bf_temp_threshold);
781 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_FAST_FILTER)
782 		cmd->bf_temp_fast_filter =
783 				cpu_to_le32(dbgfs_bf->bf_temp_fast_filter);
784 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_SLOW_FILTER)
785 		cmd->bf_temp_slow_filter =
786 				cpu_to_le32(dbgfs_bf->bf_temp_slow_filter);
787 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_DEBUG_FLAG)
788 		cmd->bf_debug_flag = cpu_to_le32(dbgfs_bf->bf_debug_flag);
789 	if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ESCAPE_TIMER)
790 		cmd->bf_escape_timer = cpu_to_le32(dbgfs_bf->bf_escape_timer);
791 	if (dbgfs_bf->mask & MVM_DEBUGFS_BA_ESCAPE_TIMER)
792 		cmd->ba_escape_timer = cpu_to_le32(dbgfs_bf->ba_escape_timer);
793 	if (dbgfs_bf->mask & MVM_DEBUGFS_BA_ENABLE_BEACON_ABORT)
794 		cmd->ba_enable_beacon_abort =
795 				cpu_to_le32(dbgfs_bf->ba_enable_beacon_abort);
796 }
797 #endif
798 
799 static int _iwl_mvm_enable_beacon_filter(struct iwl_mvm *mvm,
800 					 struct ieee80211_vif *vif,
801 					 struct iwl_beacon_filter_cmd *cmd,
802 					 u32 cmd_flags)
803 {
804 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
805 	int ret;
806 
807 	if (mvmvif != mvm->bf_allowed_vif || !vif->bss_conf.dtim_period ||
808 	    vif->type != NL80211_IFTYPE_STATION || vif->p2p)
809 		return 0;
810 
811 	iwl_mvm_beacon_filter_set_cqm_params(mvm, vif, cmd);
812 	iwl_mvm_beacon_filter_debugfs_parameters(vif, cmd);
813 	ret = iwl_mvm_beacon_filter_send_cmd(mvm, cmd, cmd_flags);
814 
815 	if (!ret)
816 		mvmvif->bf_data.bf_enabled = true;
817 
818 	return ret;
819 }
820 
821 int iwl_mvm_enable_beacon_filter(struct iwl_mvm *mvm,
822 				 struct ieee80211_vif *vif,
823 				 u32 flags)
824 {
825 	struct iwl_beacon_filter_cmd cmd = {
826 		IWL_BF_CMD_CONFIG_DEFAULTS,
827 		.bf_enable_beacon_filter = cpu_to_le32(1),
828 	};
829 
830 	return _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd, flags);
831 }
832 
833 static int _iwl_mvm_disable_beacon_filter(struct iwl_mvm *mvm,
834 					  struct ieee80211_vif *vif,
835 					  u32 flags)
836 {
837 	struct iwl_beacon_filter_cmd cmd = {};
838 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
839 	int ret;
840 
841 	if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
842 		return 0;
843 
844 	ret = iwl_mvm_beacon_filter_send_cmd(mvm, &cmd, flags);
845 
846 	if (!ret)
847 		mvmvif->bf_data.bf_enabled = false;
848 
849 	return ret;
850 }
851 
852 int iwl_mvm_disable_beacon_filter(struct iwl_mvm *mvm,
853 				  struct ieee80211_vif *vif,
854 				  u32 flags)
855 {
856 	return _iwl_mvm_disable_beacon_filter(mvm, vif, flags);
857 }
858 
859 static int iwl_mvm_power_set_ps(struct iwl_mvm *mvm)
860 {
861 	bool disable_ps;
862 	int ret;
863 
864 	/* disable PS if CAM */
865 	disable_ps = (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM);
866 	/* ...or if any of the vifs require PS to be off */
867 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
868 					IEEE80211_IFACE_ITER_NORMAL,
869 					iwl_mvm_power_ps_disabled_iterator,
870 					&disable_ps);
871 
872 	/* update device power state if it has changed */
873 	if (mvm->ps_disabled != disable_ps) {
874 		bool old_ps_disabled = mvm->ps_disabled;
875 
876 		mvm->ps_disabled = disable_ps;
877 		ret = iwl_mvm_power_update_device(mvm);
878 		if (ret) {
879 			mvm->ps_disabled = old_ps_disabled;
880 			return ret;
881 		}
882 	}
883 
884 	return 0;
885 }
886 
887 static int iwl_mvm_power_set_ba(struct iwl_mvm *mvm,
888 				struct ieee80211_vif *vif)
889 {
890 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
891 	struct iwl_beacon_filter_cmd cmd = {
892 		IWL_BF_CMD_CONFIG_DEFAULTS,
893 		.bf_enable_beacon_filter = cpu_to_le32(1),
894 	};
895 
896 	if (!mvmvif->bf_data.bf_enabled)
897 		return 0;
898 
899 	if (test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status))
900 		cmd.ba_escape_timer = cpu_to_le32(IWL_BA_ESCAPE_TIMER_D3);
901 
902 	mvmvif->bf_data.ba_enabled = !(!mvmvif->pm_enabled ||
903 				       mvm->ps_disabled ||
904 				       !vif->cfg.ps ||
905 				       iwl_mvm_vif_low_latency(mvmvif));
906 
907 	return _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd, 0);
908 }
909 
910 int iwl_mvm_power_update_ps(struct iwl_mvm *mvm)
911 {
912 	struct iwl_power_vifs vifs = {
913 		.mvm = mvm,
914 	};
915 	int ret;
916 
917 	lockdep_assert_held(&mvm->mutex);
918 
919 	/* get vifs info */
920 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
921 					IEEE80211_IFACE_ITER_NORMAL,
922 					iwl_mvm_power_get_vifs_iterator, &vifs);
923 
924 	ret = iwl_mvm_power_set_ps(mvm);
925 	if (ret)
926 		return ret;
927 
928 	if (vifs.bss_vif)
929 		return iwl_mvm_power_set_ba(mvm, vifs.bss_vif);
930 
931 	return 0;
932 }
933 
934 int iwl_mvm_power_update_mac(struct iwl_mvm *mvm)
935 {
936 	struct iwl_power_vifs vifs = {
937 		.mvm = mvm,
938 	};
939 	int ret;
940 
941 	lockdep_assert_held(&mvm->mutex);
942 
943 	/* get vifs info */
944 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
945 					IEEE80211_IFACE_ITER_NORMAL,
946 					iwl_mvm_power_get_vifs_iterator, &vifs);
947 
948 	iwl_mvm_power_set_pm(mvm, &vifs);
949 
950 	ret = iwl_mvm_power_set_ps(mvm);
951 	if (ret)
952 		return ret;
953 
954 	if (vifs.bss_vif) {
955 		ret = iwl_mvm_power_send_cmd(mvm, vifs.bss_vif);
956 		if (ret)
957 			return ret;
958 	}
959 
960 	if (vifs.p2p_vif) {
961 		ret = iwl_mvm_power_send_cmd(mvm, vifs.p2p_vif);
962 		if (ret)
963 			return ret;
964 	}
965 
966 	if (vifs.bss_vif)
967 		return iwl_mvm_power_set_ba(mvm, vifs.bss_vif);
968 
969 	return 0;
970 }
971