blob: e684811f8e8b3156b4f392d1fc5624a299d22465 [file] [log] [blame]
Johannes Berg8ca151b2013-01-24 14:25:36 +01001/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +02008 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +02009 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
Sara Sharon0db056d2015-12-29 11:07:15 +020010 * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of version 2 of the GNU General Public License as
14 * published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24 * USA
25 *
26 * The full GNU General Public License is included in this distribution
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020027 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010028 *
29 * Contact Information:
Emmanuel Grumbachcb2f8272015-11-17 15:39:56 +020030 * Intel Linux Wireless <linuxwifi@intel.com>
Johannes Berg8ca151b2013-01-24 14:25:36 +010031 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
32 *
33 * BSD LICENSE
34 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020035 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +020036 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
Sara Sharon0db056d2015-12-29 11:07:15 +020037 * Copyright(c) 2015 - 2016 Intel Deutschland GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010038 * All rights reserved.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 *
44 * * Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * * Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in
48 * the documentation and/or other materials provided with the
49 * distribution.
50 * * Neither the name Intel Corporation nor the names of its
51 * contributors may be used to endorse or promote products derived
52 * from this software without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 *
66 *****************************************************************************/
67
68#include <linux/kernel.h>
69#include <linux/module.h>
70#include <linux/slab.h>
Joe Perches93803b32015-03-02 19:54:49 -080071#include <linux/etherdevice.h>
Johannes Berg8ca151b2013-01-24 14:25:36 +010072
73#include <net/mac80211.h>
74
75#include "iwl-debug.h"
76#include "mvm.h"
77#include "iwl-modparams.h"
78#include "fw-api-power.h"
79
80#define POWER_KEEP_ALIVE_PERIOD_SEC 25
81
Emmanuel Grumbachd623d242014-01-26 12:58:24 +020082static
Alexander Bondare811ada2013-03-10 15:29:44 +020083int iwl_mvm_beacon_filter_send_cmd(struct iwl_mvm *mvm,
Eliad Peller3dd37d02014-01-07 14:00:24 +020084 struct iwl_beacon_filter_cmd *cmd,
85 u32 flags)
Alexander Bondar071d49902013-05-06 13:03:59 +030086{
Emmanuel Grumbachd623d242014-01-26 12:58:24 +020087 IWL_DEBUG_POWER(mvm, "ba_enable_beacon_abort is: %d\n",
88 le32_to_cpu(cmd->ba_enable_beacon_abort));
89 IWL_DEBUG_POWER(mvm, "ba_escape_timer is: %d\n",
90 le32_to_cpu(cmd->ba_escape_timer));
91 IWL_DEBUG_POWER(mvm, "bf_debug_flag is: %d\n",
92 le32_to_cpu(cmd->bf_debug_flag));
93 IWL_DEBUG_POWER(mvm, "bf_enable_beacon_filter is: %d\n",
94 le32_to_cpu(cmd->bf_enable_beacon_filter));
95 IWL_DEBUG_POWER(mvm, "bf_energy_delta is: %d\n",
96 le32_to_cpu(cmd->bf_energy_delta));
97 IWL_DEBUG_POWER(mvm, "bf_escape_timer is: %d\n",
98 le32_to_cpu(cmd->bf_escape_timer));
99 IWL_DEBUG_POWER(mvm, "bf_roaming_energy_delta is: %d\n",
100 le32_to_cpu(cmd->bf_roaming_energy_delta));
101 IWL_DEBUG_POWER(mvm, "bf_roaming_state is: %d\n",
102 le32_to_cpu(cmd->bf_roaming_state));
103 IWL_DEBUG_POWER(mvm, "bf_temp_threshold is: %d\n",
104 le32_to_cpu(cmd->bf_temp_threshold));
105 IWL_DEBUG_POWER(mvm, "bf_temp_fast_filter is: %d\n",
106 le32_to_cpu(cmd->bf_temp_fast_filter));
107 IWL_DEBUG_POWER(mvm, "bf_temp_slow_filter is: %d\n",
108 le32_to_cpu(cmd->bf_temp_slow_filter));
Alexander Bondar071d49902013-05-06 13:03:59 +0300109
Emmanuel Grumbachd623d242014-01-26 12:58:24 +0200110 return iwl_mvm_send_cmd_pdu(mvm, REPLY_BEACON_FILTERING_CMD, flags,
111 sizeof(struct iwl_beacon_filter_cmd), cmd);
Alexander Bondar071d49902013-05-06 13:03:59 +0300112}
113
Andrei Otcheretianskia20fd392013-07-21 17:23:59 +0300114static
115void iwl_mvm_beacon_filter_set_cqm_params(struct iwl_mvm *mvm,
116 struct ieee80211_vif *vif,
Gregory Greenmancdc306b2015-07-20 12:55:34 +0300117 struct iwl_beacon_filter_cmd *cmd,
118 bool d0i3)
Andrei Otcheretianskia20fd392013-07-21 17:23:59 +0300119{
120 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
121
Gregory Greenmancdc306b2015-07-20 12:55:34 +0300122 if (vif->bss_conf.cqm_rssi_thold && !d0i3) {
Andrei Otcheretianskia20fd392013-07-21 17:23:59 +0300123 cmd->bf_energy_delta =
124 cpu_to_le32(vif->bss_conf.cqm_rssi_hyst);
125 /* fw uses an absolute value for this */
126 cmd->bf_roaming_state =
127 cpu_to_le32(-vif->bss_conf.cqm_rssi_thold);
128 }
129 cmd->ba_enable_beacon_abort = cpu_to_le32(mvmvif->bf_data.ba_enabled);
130}
131
Alexander Bondar3f0b2b32013-03-05 14:08:23 +0200132static void iwl_mvm_power_log(struct iwl_mvm *mvm,
Alexander Bondare811ada2013-03-10 15:29:44 +0200133 struct iwl_mac_power_cmd *cmd)
Alexander Bondar3f0b2b32013-03-05 14:08:23 +0200134{
135 IWL_DEBUG_POWER(mvm,
Alexander Bondare811ada2013-03-10 15:29:44 +0200136 "Sending power table command on mac id 0x%X for power level %d, flags = 0x%X\n",
137 cmd->id_and_color, iwlmvm_mod_params.power_scheme,
Alexander Bondar3f0b2b32013-03-05 14:08:23 +0200138 le16_to_cpu(cmd->flags));
Alexander Bondare811ada2013-03-10 15:29:44 +0200139 IWL_DEBUG_POWER(mvm, "Keep alive = %u sec\n",
140 le16_to_cpu(cmd->keep_alive_seconds));
Alexander Bondar3f0b2b32013-03-05 14:08:23 +0200141
Alexander Bondare3c588e2013-04-07 14:08:59 +0300142 if (!(cmd->flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK))) {
143 IWL_DEBUG_POWER(mvm, "Disable power management\n");
144 return;
145 }
146
147 IWL_DEBUG_POWER(mvm, "Rx timeout = %u usec\n",
148 le32_to_cpu(cmd->rx_data_timeout));
149 IWL_DEBUG_POWER(mvm, "Tx timeout = %u usec\n",
150 le32_to_cpu(cmd->tx_data_timeout));
151 if (cmd->flags & cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK))
152 IWL_DEBUG_POWER(mvm, "DTIM periods to skip = %u\n",
153 cmd->skip_dtim_periods);
154 if (cmd->flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
155 IWL_DEBUG_POWER(mvm, "LP RX RSSI threshold = %u\n",
156 cmd->lprx_rssi_threshold);
157 if (cmd->flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK)) {
158 IWL_DEBUG_POWER(mvm, "uAPSD enabled\n");
159 IWL_DEBUG_POWER(mvm, "Rx timeout (uAPSD) = %u usec\n",
160 le32_to_cpu(cmd->rx_data_timeout_uapsd));
161 IWL_DEBUG_POWER(mvm, "Tx timeout (uAPSD) = %u usec\n",
162 le32_to_cpu(cmd->tx_data_timeout_uapsd));
163 IWL_DEBUG_POWER(mvm, "QNDP TID = %d\n", cmd->qndp_tid);
164 IWL_DEBUG_POWER(mvm, "ACs flags = 0x%x\n", cmd->uapsd_ac_flags);
165 IWL_DEBUG_POWER(mvm, "Max SP = %d\n", cmd->uapsd_max_sp);
Alexander Bondar3f0b2b32013-03-05 14:08:23 +0200166 }
167}
168
Alexander Bondar175a70b2013-04-14 20:59:37 +0300169static void iwl_mvm_power_configure_uapsd(struct iwl_mvm *mvm,
170 struct ieee80211_vif *vif,
171 struct iwl_mac_power_cmd *cmd)
172{
173 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
174 enum ieee80211_ac_numbers ac;
175 bool tid_found = false;
176
Emmanuel Grumbach143b0b22016-10-26 14:21:27 +0300177#ifdef CONFIG_IWLWIFI_DEBUGFS
178 /* set advanced pm flag with no uapsd ACs to enable ps-poll */
179 if (mvmvif->dbgfs_pm.use_ps_poll) {
180 cmd->flags |= cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK);
181 return;
182 }
183#endif
184
Alexander Bondar175a70b2013-04-14 20:59:37 +0300185 for (ac = IEEE80211_AC_VO; ac <= IEEE80211_AC_BK; ac++) {
186 if (!mvmvif->queue_params[ac].uapsd)
187 continue;
188
189 if (mvm->cur_ucode != IWL_UCODE_WOWLAN)
190 cmd->flags |=
191 cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK);
192
193 cmd->uapsd_ac_flags |= BIT(ac);
194
195 /* QNDP TID - the highest TID with no admission control */
196 if (!tid_found && !mvmvif->queue_params[ac].acm) {
197 tid_found = true;
198 switch (ac) {
199 case IEEE80211_AC_VO:
200 cmd->qndp_tid = 6;
201 break;
202 case IEEE80211_AC_VI:
203 cmd->qndp_tid = 5;
204 break;
205 case IEEE80211_AC_BE:
206 cmd->qndp_tid = 0;
207 break;
208 case IEEE80211_AC_BK:
209 cmd->qndp_tid = 1;
210 break;
211 }
212 }
213 }
214
Alexander Bondar175a70b2013-04-14 20:59:37 +0300215 cmd->flags |= cpu_to_le16(POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK);
216
217 if (cmd->uapsd_ac_flags == (BIT(IEEE80211_AC_VO) |
218 BIT(IEEE80211_AC_VI) |
219 BIT(IEEE80211_AC_BE) |
220 BIT(IEEE80211_AC_BK))) {
221 cmd->flags |= cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK);
222 cmd->snooze_interval = cpu_to_le16(IWL_MVM_PS_SNOOZE_INTERVAL);
223 cmd->snooze_window = (mvm->cur_ucode == IWL_UCODE_WOWLAN) ?
224 cpu_to_le16(IWL_MVM_WOWLAN_PS_SNOOZE_WINDOW) :
225 cpu_to_le16(IWL_MVM_PS_SNOOZE_WINDOW);
226 }
227
Emmanuel Grumbach6e2611f2016-03-15 11:12:20 +0200228 cmd->uapsd_max_sp = mvm->hw->uapsd_max_sp_len;
Alexander Bondar175a70b2013-04-14 20:59:37 +0300229
230 if (mvm->cur_ucode == IWL_UCODE_WOWLAN || cmd->flags &
231 cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)) {
232 cmd->rx_data_timeout_uapsd =
233 cpu_to_le32(IWL_MVM_WOWLAN_PS_RX_DATA_TIMEOUT);
234 cmd->tx_data_timeout_uapsd =
235 cpu_to_le32(IWL_MVM_WOWLAN_PS_TX_DATA_TIMEOUT);
236 } else {
237 cmd->rx_data_timeout_uapsd =
238 cpu_to_le32(IWL_MVM_UAPSD_RX_DATA_TIMEOUT);
239 cmd->tx_data_timeout_uapsd =
240 cpu_to_le32(IWL_MVM_UAPSD_TX_DATA_TIMEOUT);
241 }
242
243 if (cmd->flags & cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)) {
244 cmd->heavy_tx_thld_packets =
245 IWL_MVM_PS_SNOOZE_HEAVY_TX_THLD_PACKETS;
246 cmd->heavy_rx_thld_packets =
247 IWL_MVM_PS_SNOOZE_HEAVY_RX_THLD_PACKETS;
248 } else {
249 cmd->heavy_tx_thld_packets =
250 IWL_MVM_PS_HEAVY_TX_THLD_PACKETS;
251 cmd->heavy_rx_thld_packets =
252 IWL_MVM_PS_HEAVY_RX_THLD_PACKETS;
253 }
254 cmd->heavy_tx_thld_percentage =
255 IWL_MVM_PS_HEAVY_TX_THLD_PERCENT;
256 cmd->heavy_rx_thld_percentage =
257 IWL_MVM_PS_HEAVY_RX_THLD_PERCENT;
258}
259
Avri Altmanee95ed32015-11-25 13:17:10 +0200260static void iwl_mvm_p2p_standalone_iterator(void *_data, u8 *mac,
261 struct ieee80211_vif *vif)
262{
263 bool *is_p2p_standalone = _data;
264
265 switch (ieee80211_vif_type_p2p(vif)) {
266 case NL80211_IFTYPE_P2P_GO:
267 case NL80211_IFTYPE_AP:
268 *is_p2p_standalone = false;
269 break;
270 case NL80211_IFTYPE_STATION:
271 if (vif->bss_conf.assoc)
272 *is_p2p_standalone = false;
273 break;
274
275 default:
276 break;
277 }
278}
279
Avri Altmanc6e37a62014-04-01 12:08:13 +0300280static bool iwl_mvm_power_allow_uapsd(struct iwl_mvm *mvm,
281 struct ieee80211_vif *vif)
282{
283 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
284
285 if (!memcmp(mvmvif->uapsd_misbehaving_bssid, vif->bss_conf.bssid,
286 ETH_ALEN))
287 return false;
288
Avri Altmanc6e37a62014-04-01 12:08:13 +0300289 /*
290 * Avoid using uAPSD if P2P client is associated to GO that uses
291 * opportunistic power save. This is due to current FW limitation.
292 */
293 if (vif->p2p &&
294 (vif->bss_conf.p2p_noa_attr.oppps_ctwindow &
295 IEEE80211_P2P_OPPPS_ENABLE_BIT))
296 return false;
297
Avri Altman05345282014-05-08 10:06:01 +0300298 /*
299 * Avoid using uAPSD if client is in DCM -
300 * low latency issue in Miracast
301 */
Arik Nemtsovcf7b4912014-05-15 11:44:40 +0300302 if (iwl_mvm_phy_ctx_count(mvm) >= 2)
Avri Altman05345282014-05-08 10:06:01 +0300303 return false;
304
Avri Altmanee95ed32015-11-25 13:17:10 +0200305 if (vif->p2p) {
306 /* Allow U-APSD only if p2p is stand alone */
307 bool is_p2p_standalone = true;
308
Avraham Sternc5241b02016-04-20 09:29:18 +0300309 if (!iwl_mvm_is_p2p_scm_uapsd_supported(mvm))
Avri Altmanee95ed32015-11-25 13:17:10 +0200310 return false;
311
312 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
313 IEEE80211_IFACE_ITER_NORMAL,
314 iwl_mvm_p2p_standalone_iterator,
315 &is_p2p_standalone);
316
317 if (!is_p2p_standalone)
318 return false;
319 }
320
Avri Altmanc6e37a62014-04-01 12:08:13 +0300321 return true;
322}
323
Avri Altman49c6d802014-08-27 19:58:56 +0300324static bool iwl_mvm_power_is_radar(struct ieee80211_vif *vif)
325{
326 struct ieee80211_chanctx_conf *chanctx_conf;
327 struct ieee80211_channel *chan;
328 bool radar_detect = false;
329
330 rcu_read_lock();
331 chanctx_conf = rcu_dereference(vif->chanctx_conf);
332 WARN_ON(!chanctx_conf);
333 if (chanctx_conf) {
334 chan = chanctx_conf->def.chan;
335 radar_detect = chan->flags & IEEE80211_CHAN_RADAR;
336 }
337 rcu_read_unlock();
338
339 return radar_detect;
340}
341
Johannes Berga6449122015-09-01 14:15:17 +0200342static void iwl_mvm_power_config_skip_dtim(struct iwl_mvm *mvm,
343 struct ieee80211_vif *vif,
344 struct iwl_mac_power_cmd *cmd,
345 bool host_awake)
346{
347 int dtimper = vif->bss_conf.dtim_period ?: 1;
348 int skip;
349
350 /* disable, in case we're supposed to override */
351 cmd->skip_dtim_periods = 0;
352 cmd->flags &= ~cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
353
354 if (iwl_mvm_power_is_radar(vif))
355 return;
356
357 if (dtimper >= 10)
358 return;
359
360 /* TODO: check that multicast wake lock is off */
361
362 if (host_awake) {
363 if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_LP)
364 return;
365 skip = 2;
366 } else {
367 int dtimper_tu = dtimper * vif->bss_conf.beacon_int;
368
369 if (WARN_ON(!dtimper_tu))
370 return;
371 /* configure skip over dtim up to 306TU - 314 msec */
372 skip = max_t(u8, 1, 306 / dtimper_tu);
373 }
374
375 /* the firmware really expects "look at every X DTIMs", so add 1 */
376 cmd->skip_dtim_periods = 1 + skip;
377 cmd->flags |= cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
378}
379
Alexander Bondare811ada2013-03-10 15:29:44 +0200380static void iwl_mvm_power_build_cmd(struct iwl_mvm *mvm,
381 struct ieee80211_vif *vif,
Johannes Berg9645edb2015-10-06 13:28:48 +0200382 struct iwl_mac_power_cmd *cmd,
383 bool host_awake)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100384{
Avri Altman5387b342014-11-11 07:29:59 +0200385 int dtimper, bi;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100386 int keep_alive;
Alexander Bondarb571a692013-05-21 14:49:09 +0300387 struct iwl_mvm_vif *mvmvif __maybe_unused =
388 iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100389
Alexander Bondare811ada2013-03-10 15:29:44 +0200390 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
391 mvmvif->color));
Emmanuel Grumbach717e2392014-07-31 14:39:40 +0300392 dtimper = vif->bss_conf.dtim_period;
Avri Altman886ca9f2014-10-02 19:46:16 +0200393 bi = vif->bss_conf.beacon_int;
Alexander Bondare811ada2013-03-10 15:29:44 +0200394
Alexander Bondare16cf7e2013-03-05 14:01:27 +0200395 /*
396 * Regardless of power management state the driver must set
397 * keep alive period. FW will use it for sending keep alive NDPs
Alexander Bondare811ada2013-03-10 15:29:44 +0200398 * immediately after association. Check that keep alive period
399 * is at least 3 * DTIM
Alexander Bondare16cf7e2013-03-05 14:01:27 +0200400 */
Avri Altman5387b342014-11-11 07:29:59 +0200401 keep_alive = DIV_ROUND_UP(ieee80211_tu_to_usec(3 * dtimper * bi),
402 USEC_PER_SEC);
403 keep_alive = max(keep_alive, POWER_KEEP_ALIVE_PERIOD_SEC);
Alexander Bondare811ada2013-03-10 15:29:44 +0200404 cmd->keep_alive_seconds = cpu_to_le16(keep_alive);
Alexander Bondare16cf7e2013-03-05 14:01:27 +0200405
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200406 if (mvm->ps_disabled)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100407 return;
408
Alexander Bondar9a613042013-03-05 12:54:00 +0200409 cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK);
410
Johannes Bergb08dbed2015-10-06 14:07:44 +0200411 if (!vif->bss_conf.ps || !mvmvif->pm_enabled)
412 return;
413
414 if (iwl_mvm_vif_low_latency(mvmvif) && vif->p2p &&
415 (!fw_has_capa(&mvm->fw->ucode_capa,
416 IWL_UCODE_TLV_CAPA_SHORT_PM_TIMEOUTS) ||
417 !IWL_MVM_P2P_LOWLATENCY_PS_ENABLE))
Alexander Bondar9a613042013-03-05 12:54:00 +0200418 return;
419
420 cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100421
Alexander Bondarbd4ace22013-03-21 17:14:14 +0200422 if (vif->bss_conf.beacon_rate &&
423 (vif->bss_conf.beacon_rate->bitrate == 10 ||
424 vif->bss_conf.beacon_rate->bitrate == 60)) {
425 cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
Alexander Bondare811ada2013-03-10 15:29:44 +0200426 cmd->lprx_rssi_threshold = POWER_LPRX_RSSI_THRESHOLD;
Alexander Bondarbd4ace22013-03-21 17:14:14 +0200427 }
428
Johannes Berg9645edb2015-10-06 13:28:48 +0200429 iwl_mvm_power_config_skip_dtim(mvm, vif, cmd, host_awake);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100430
Johannes Bergb08dbed2015-10-06 14:07:44 +0200431 if (!host_awake) {
Johannes Berg99545922013-06-14 13:36:21 +0200432 cmd->rx_data_timeout =
433 cpu_to_le32(IWL_MVM_WOWLAN_PS_RX_DATA_TIMEOUT);
434 cmd->tx_data_timeout =
435 cpu_to_le32(IWL_MVM_WOWLAN_PS_TX_DATA_TIMEOUT);
Johannes Bergb08dbed2015-10-06 14:07:44 +0200436 } else if (iwl_mvm_vif_low_latency(mvmvif) && vif->p2p &&
437 fw_has_capa(&mvm->fw->ucode_capa,
438 IWL_UCODE_TLV_CAPA_SHORT_PM_TIMEOUTS)) {
439 cmd->tx_data_timeout =
440 cpu_to_le32(IWL_MVM_SHORT_PS_TX_DATA_TIMEOUT);
441 cmd->rx_data_timeout =
442 cpu_to_le32(IWL_MVM_SHORT_PS_RX_DATA_TIMEOUT);
443 } else {
444 cmd->rx_data_timeout =
445 cpu_to_le32(IWL_MVM_DEFAULT_PS_RX_DATA_TIMEOUT);
446 cmd->tx_data_timeout =
447 cpu_to_le32(IWL_MVM_DEFAULT_PS_TX_DATA_TIMEOUT);
Alexander Bondaree1e8422013-04-23 13:52:10 +0300448 }
Alexander Bondarb571a692013-05-21 14:49:09 +0300449
Avri Altmanc6e37a62014-04-01 12:08:13 +0300450 if (iwl_mvm_power_allow_uapsd(mvm, vif))
Alexander Bondar175a70b2013-04-14 20:59:37 +0300451 iwl_mvm_power_configure_uapsd(mvm, vif, cmd);
Alexander Bondare3c588e2013-04-07 14:08:59 +0300452
Alexander Bondarb571a692013-05-21 14:49:09 +0300453#ifdef CONFIG_IWLWIFI_DEBUGFS
454 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_KEEP_ALIVE)
Alexander Bondare811ada2013-03-10 15:29:44 +0200455 cmd->keep_alive_seconds =
456 cpu_to_le16(mvmvif->dbgfs_pm.keep_alive_seconds);
Alexander Bondarb571a692013-05-21 14:49:09 +0300457 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_OVER_DTIM) {
458 if (mvmvif->dbgfs_pm.skip_over_dtim)
459 cmd->flags |=
460 cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
461 else
462 cmd->flags &=
463 cpu_to_le16(~POWER_FLAGS_SKIP_OVER_DTIM_MSK);
464 }
465 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_RX_DATA_TIMEOUT)
466 cmd->rx_data_timeout =
467 cpu_to_le32(mvmvif->dbgfs_pm.rx_data_timeout);
468 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_TX_DATA_TIMEOUT)
469 cmd->tx_data_timeout =
470 cpu_to_le32(mvmvif->dbgfs_pm.tx_data_timeout);
471 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_DTIM_PERIODS)
Alexander Bondare811ada2013-03-10 15:29:44 +0200472 cmd->skip_dtim_periods = mvmvif->dbgfs_pm.skip_dtim_periods;
Alexander Bondarbd4ace22013-03-21 17:14:14 +0200473 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_ENA) {
474 if (mvmvif->dbgfs_pm.lprx_ena)
475 cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
476 else
477 cmd->flags &= cpu_to_le16(~POWER_FLAGS_LPRX_ENA_MSK);
478 }
479 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_RSSI_THRESHOLD)
Alexander Bondare811ada2013-03-10 15:29:44 +0200480 cmd->lprx_rssi_threshold = mvmvif->dbgfs_pm.lprx_rssi_threshold;
Alexander Bondar89716342013-08-04 17:52:23 +0300481 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SNOOZE_ENABLE) {
482 if (mvmvif->dbgfs_pm.snooze_ena)
483 cmd->flags |=
484 cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK);
485 else
486 cmd->flags &=
487 cpu_to_le16(~POWER_FLAGS_SNOOZE_ENA_MSK);
488 }
Alexander Bondare45a9412013-12-04 10:13:24 +0200489 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_UAPSD_MISBEHAVING) {
490 u16 flag = POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK;
491 if (mvmvif->dbgfs_pm.uapsd_misbehaving)
492 cmd->flags |= cpu_to_le16(flag);
493 else
494 cmd->flags &= cpu_to_le16(flag);
495 }
Alexander Bondarb571a692013-05-21 14:49:09 +0300496#endif /* CONFIG_IWLWIFI_DEBUGFS */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100497}
498
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200499static int iwl_mvm_power_send_cmd(struct iwl_mvm *mvm,
Alexander Bondare811ada2013-03-10 15:29:44 +0200500 struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100501{
Alexander Bondare811ada2013-03-10 15:29:44 +0200502 struct iwl_mac_power_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100503
Johannes Berg9645edb2015-10-06 13:28:48 +0200504 iwl_mvm_power_build_cmd(mvm, vif, &cmd,
505 mvm->cur_ucode != IWL_UCODE_WOWLAN);
Alexander Bondar3f0b2b32013-03-05 14:08:23 +0200506 iwl_mvm_power_log(mvm, &cmd);
Emmanuel Grumbach474b50c2014-01-28 09:13:04 +0200507#ifdef CONFIG_IWLWIFI_DEBUGFS
508 memcpy(&iwl_mvm_vif_from_mac80211(vif)->mac_pwr_cmd, &cmd, sizeof(cmd));
509#endif
Johannes Berg8ca151b2013-01-24 14:25:36 +0100510
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300511 return iwl_mvm_send_cmd_pdu(mvm, MAC_PM_POWER_TABLE, 0,
Emmanuel Grumbach06280a22014-01-27 08:09:23 +0200512 sizeof(cmd), &cmd);
513}
514
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200515int iwl_mvm_power_update_device(struct iwl_mvm *mvm)
Alexander Bondar64b928c2013-09-03 14:18:03 +0300516{
517 struct iwl_device_power_cmd cmd = {
Johannes Bergceef91c2015-09-04 11:27:25 +0200518 .flags = 0,
Alexander Bondar64b928c2013-09-03 14:18:03 +0300519 };
520
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200521 if (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM)
522 mvm->ps_disabled = true;
523
Johannes Bergceef91c2015-09-04 11:27:25 +0200524 if (!mvm->ps_disabled)
525 cmd.flags |= cpu_to_le16(DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK);
Alexander Bondar64b928c2013-09-03 14:18:03 +0300526
527#ifdef CONFIG_IWLWIFI_DEBUGFS
528 if ((mvm->cur_ucode == IWL_UCODE_WOWLAN) ? mvm->disable_power_off_d3 :
529 mvm->disable_power_off)
530 cmd.flags &=
531 cpu_to_le16(~DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK);
532#endif
533 IWL_DEBUG_POWER(mvm,
534 "Sending device power command with flags = 0x%X\n",
535 cmd.flags);
536
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300537 return iwl_mvm_send_cmd_pdu(mvm, POWER_TABLE_CMD, 0, sizeof(cmd),
Alexander Bondar64b928c2013-09-03 14:18:03 +0300538 &cmd);
539}
540
Alexander Bondar175a70b2013-04-14 20:59:37 +0300541void iwl_mvm_power_vif_assoc(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
542{
543 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
544
545 if (memcmp(vif->bss_conf.bssid, mvmvif->uapsd_misbehaving_bssid,
546 ETH_ALEN))
Joe Perches93803b32015-03-02 19:54:49 -0800547 eth_zero_addr(mvmvif->uapsd_misbehaving_bssid);
Alexander Bondar175a70b2013-04-14 20:59:37 +0300548}
549
550static void iwl_mvm_power_uapsd_misbehav_ap_iterator(void *_data, u8 *mac,
551 struct ieee80211_vif *vif)
552{
553 u8 *ap_sta_id = _data;
554 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
555
556 /* The ap_sta_id is not expected to change during current association
557 * so no explicit protection is needed
558 */
559 if (mvmvif->ap_sta_id == *ap_sta_id)
560 memcpy(mvmvif->uapsd_misbehaving_bssid, vif->bss_conf.bssid,
561 ETH_ALEN);
562}
563
Johannes Berg04168412015-06-23 21:22:09 +0200564void iwl_mvm_power_uapsd_misbehaving_ap_notif(struct iwl_mvm *mvm,
565 struct iwl_rx_cmd_buffer *rxb)
Alexander Bondar175a70b2013-04-14 20:59:37 +0300566{
567 struct iwl_rx_packet *pkt = rxb_addr(rxb);
568 struct iwl_uapsd_misbehaving_ap_notif *notif = (void *)pkt->data;
569 u8 ap_sta_id = le32_to_cpu(notif->sta_id);
570
571 ieee80211_iterate_active_interfaces_atomic(
572 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
573 iwl_mvm_power_uapsd_misbehav_ap_iterator, &ap_sta_id);
Alexander Bondar175a70b2013-04-14 20:59:37 +0300574}
575
Avri Altman19889022014-03-19 07:25:06 +0200576struct iwl_power_vifs {
Arik Nemtsovfa3d07e2014-05-15 18:59:32 +0300577 struct iwl_mvm *mvm;
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200578 struct ieee80211_vif *bss_vif;
Avri Altman8ea0c682014-02-20 13:28:57 +0200579 struct ieee80211_vif *p2p_vif;
Avri Altman19889022014-03-19 07:25:06 +0200580 struct ieee80211_vif *ap_vif;
581 struct ieee80211_vif *monitor_vif;
582 bool p2p_active;
583 bool bss_active;
584 bool ap_active;
585 bool monitor_active;
Emmanuel Grumbach06280a22014-01-27 08:09:23 +0200586};
587
Luciano Coelho128aa942014-08-08 18:36:22 +0300588static void iwl_mvm_power_disable_pm_iterator(void *_data, u8* mac,
589 struct ieee80211_vif *vif)
590{
591 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
592
593 mvmvif->pm_enabled = false;
594}
595
Luciano Coelho2533edc2014-08-08 19:50:46 +0300596static void iwl_mvm_power_ps_disabled_iterator(void *_data, u8* mac,
597 struct ieee80211_vif *vif)
598{
599 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
600 bool *disable_ps = _data;
601
Sara Sharon4f23f202016-11-20 13:50:39 +0200602 if (mvmvif->phy_ctxt && mvmvif->phy_ctxt->id < NUM_PHY_CTX)
603 *disable_ps |= mvmvif->ps_disabled;
Luciano Coelho2533edc2014-08-08 19:50:46 +0300604}
605
Luciano Coelho128aa942014-08-08 18:36:22 +0300606static void iwl_mvm_power_get_vifs_iterator(void *_data, u8 *mac,
607 struct ieee80211_vif *vif)
Alexander Bondar1c2abf72013-08-27 20:31:48 +0300608{
Emmanuel Grumbach06280a22014-01-27 08:09:23 +0200609 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Avri Altman19889022014-03-19 07:25:06 +0200610 struct iwl_power_vifs *power_iterator = _data;
Sara Sharon4f23f202016-11-20 13:50:39 +0200611 bool active = mvmvif->phy_ctxt && mvmvif->phy_ctxt->id < NUM_PHY_CTX;
Alexander Bondar1c2abf72013-08-27 20:31:48 +0300612
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200613 switch (ieee80211_vif_type_p2p(vif)) {
614 case NL80211_IFTYPE_P2P_DEVICE:
615 break;
Emmanuel Grumbach06280a22014-01-27 08:09:23 +0200616
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200617 case NL80211_IFTYPE_P2P_GO:
618 case NL80211_IFTYPE_AP:
Avri Altman19889022014-03-19 07:25:06 +0200619 /* only a single MAC of the same type */
620 WARN_ON(power_iterator->ap_vif);
621 power_iterator->ap_vif = vif;
Sara Sharon4f23f202016-11-20 13:50:39 +0200622 if (active)
623 power_iterator->ap_active = true;
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200624 break;
Emmanuel Grumbach06280a22014-01-27 08:09:23 +0200625
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200626 case NL80211_IFTYPE_MONITOR:
Avri Altman19889022014-03-19 07:25:06 +0200627 /* only a single MAC of the same type */
628 WARN_ON(power_iterator->monitor_vif);
629 power_iterator->monitor_vif = vif;
Sara Sharon4f23f202016-11-20 13:50:39 +0200630 if (active)
631 power_iterator->monitor_active = true;
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200632 break;
633
634 case NL80211_IFTYPE_P2P_CLIENT:
Avri Altman19889022014-03-19 07:25:06 +0200635 /* only a single MAC of the same type */
Avri Altman8ea0c682014-02-20 13:28:57 +0200636 WARN_ON(power_iterator->p2p_vif);
637 power_iterator->p2p_vif = vif;
Sara Sharon4f23f202016-11-20 13:50:39 +0200638 if (active)
639 power_iterator->p2p_active = true;
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200640 break;
641
642 case NL80211_IFTYPE_STATION:
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200643 power_iterator->bss_vif = vif;
Sara Sharon4f23f202016-11-20 13:50:39 +0200644 if (active)
645 power_iterator->bss_active = true;
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200646 break;
647
648 default:
649 break;
650 }
Alexander Bondar1c2abf72013-08-27 20:31:48 +0300651}
652
Luciano Coelhob1873302014-08-08 17:12:07 +0300653static void iwl_mvm_power_set_pm(struct iwl_mvm *mvm,
654 struct iwl_power_vifs *vifs)
Alexander Bondar1c2abf72013-08-27 20:31:48 +0300655{
Avri Altman19889022014-03-19 07:25:06 +0200656 struct iwl_mvm_vif *bss_mvmvif = NULL;
657 struct iwl_mvm_vif *p2p_mvmvif = NULL;
658 struct iwl_mvm_vif *ap_mvmvif = NULL;
659 bool client_same_channel = false;
660 bool ap_same_channel = false;
661
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200662 lockdep_assert_held(&mvm->mutex);
663
Luciano Coelho128aa942014-08-08 18:36:22 +0300664 /* set pm_enable to false */
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200665 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
Luciano Coelho128aa942014-08-08 18:36:22 +0300666 IEEE80211_IFACE_ITER_NORMAL,
667 iwl_mvm_power_disable_pm_iterator,
668 NULL);
Avri Altman19889022014-03-19 07:25:06 +0200669
670 if (vifs->bss_vif)
671 bss_mvmvif = iwl_mvm_vif_from_mac80211(vifs->bss_vif);
672
673 if (vifs->p2p_vif)
674 p2p_mvmvif = iwl_mvm_vif_from_mac80211(vifs->p2p_vif);
675
676 if (vifs->ap_vif)
677 ap_mvmvif = iwl_mvm_vif_from_mac80211(vifs->ap_vif);
678
Arik Nemtsovfcf23352015-02-22 19:15:04 +0200679 /* don't allow PM if any TDLS stations exist */
680 if (iwl_mvm_tdls_sta_count(mvm, NULL))
681 return;
682
Avri Altman19889022014-03-19 07:25:06 +0200683 /* enable PM on bss if bss stand alone */
Arik Nemtsov9474d442014-09-03 12:09:45 +0300684 if (vifs->bss_active && !vifs->p2p_active && !vifs->ap_active) {
Avri Altman19889022014-03-19 07:25:06 +0200685 bss_mvmvif->pm_enabled = true;
686 return;
687 }
688
689 /* enable PM on p2p if p2p stand alone */
Arik Nemtsov9474d442014-09-03 12:09:45 +0300690 if (vifs->p2p_active && !vifs->bss_active && !vifs->ap_active) {
Luca Coelho4b87e5a2016-09-12 16:03:30 +0300691 p2p_mvmvif->pm_enabled = true;
Avri Altman19889022014-03-19 07:25:06 +0200692 return;
693 }
694
695 if (vifs->bss_active && vifs->p2p_active)
696 client_same_channel = (bss_mvmvif->phy_ctxt->id ==
697 p2p_mvmvif->phy_ctxt->id);
698 if (vifs->bss_active && vifs->ap_active)
699 ap_same_channel = (bss_mvmvif->phy_ctxt->id ==
700 ap_mvmvif->phy_ctxt->id);
701
Avri Altman63ef81c2014-04-02 07:54:07 +0300702 /* clients are not stand alone: enable PM if DCM */
Luca Coelho4b87e5a2016-09-12 16:03:30 +0300703 if (!(client_same_channel || ap_same_channel)) {
Avri Altman63ef81c2014-04-02 07:54:07 +0300704 if (vifs->bss_active)
Avri Altman19889022014-03-19 07:25:06 +0200705 bss_mvmvif->pm_enabled = true;
Luca Coelho4b87e5a2016-09-12 16:03:30 +0300706 if (vifs->p2p_active)
Avri Altman63ef81c2014-04-02 07:54:07 +0300707 p2p_mvmvif->pm_enabled = true;
708 return;
Avri Altman19889022014-03-19 07:25:06 +0200709 }
710
711 /*
712 * There is only one channel in the system and there are only
713 * bss and p2p clients that share it
714 */
Luca Coelho4b87e5a2016-09-12 16:03:30 +0300715 if (client_same_channel && !vifs->ap_active) {
Avri Altman19889022014-03-19 07:25:06 +0200716 /* share same channel*/
717 bss_mvmvif->pm_enabled = true;
Luca Coelho4b87e5a2016-09-12 16:03:30 +0300718 p2p_mvmvif->pm_enabled = true;
Avri Altman19889022014-03-19 07:25:06 +0200719 }
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200720}
721
Alexander Bondarb571a692013-05-21 14:49:09 +0300722#ifdef CONFIG_IWLWIFI_DEBUGFS
Emmanuel Grumbachc1cb92f2014-01-28 10:17:18 +0200723int iwl_mvm_power_mac_dbgfs_read(struct iwl_mvm *mvm,
724 struct ieee80211_vif *vif, char *buf,
725 int bufsz)
Alexander Bondare811ada2013-03-10 15:29:44 +0200726{
Emmanuel Grumbach474b50c2014-01-28 09:13:04 +0200727 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Alexander Bondare811ada2013-03-10 15:29:44 +0200728 struct iwl_mac_power_cmd cmd = {};
729 int pos = 0;
730
Emmanuel Grumbach474b50c2014-01-28 09:13:04 +0200731 mutex_lock(&mvm->mutex);
732 memcpy(&cmd, &mvmvif->mac_pwr_cmd, sizeof(cmd));
733 mutex_unlock(&mvm->mutex);
Alexander Bondare811ada2013-03-10 15:29:44 +0200734
Alexander Bondare811ada2013-03-10 15:29:44 +0200735 pos += scnprintf(buf+pos, bufsz-pos, "power_scheme = %d\n",
736 iwlmvm_mod_params.power_scheme);
737 pos += scnprintf(buf+pos, bufsz-pos, "flags = 0x%x\n",
738 le16_to_cpu(cmd.flags));
739 pos += scnprintf(buf+pos, bufsz-pos, "keep_alive = %d\n",
740 le16_to_cpu(cmd.keep_alive_seconds));
741
Alexander Bondar175a70b2013-04-14 20:59:37 +0300742 if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK)))
743 return pos;
744
745 pos += scnprintf(buf+pos, bufsz-pos, "skip_over_dtim = %d\n",
746 (cmd.flags &
747 cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK)) ? 1 : 0);
748 pos += scnprintf(buf+pos, bufsz-pos, "skip_dtim_periods = %d\n",
749 cmd.skip_dtim_periods);
750 if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK))) {
751 pos += scnprintf(buf+pos, bufsz-pos, "rx_data_timeout = %d\n",
752 le32_to_cpu(cmd.rx_data_timeout));
753 pos += scnprintf(buf+pos, bufsz-pos, "tx_data_timeout = %d\n",
754 le32_to_cpu(cmd.tx_data_timeout));
Alexander Bondare811ada2013-03-10 15:29:44 +0200755 }
Alexander Bondar175a70b2013-04-14 20:59:37 +0300756 if (cmd.flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
757 pos += scnprintf(buf+pos, bufsz-pos,
758 "lprx_rssi_threshold = %d\n",
759 cmd.lprx_rssi_threshold);
760
761 if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK)))
762 return pos;
763
764 pos += scnprintf(buf+pos, bufsz-pos, "rx_data_timeout_uapsd = %d\n",
765 le32_to_cpu(cmd.rx_data_timeout_uapsd));
766 pos += scnprintf(buf+pos, bufsz-pos, "tx_data_timeout_uapsd = %d\n",
767 le32_to_cpu(cmd.tx_data_timeout_uapsd));
768 pos += scnprintf(buf+pos, bufsz-pos, "qndp_tid = %d\n", cmd.qndp_tid);
769 pos += scnprintf(buf+pos, bufsz-pos, "uapsd_ac_flags = 0x%x\n",
770 cmd.uapsd_ac_flags);
771 pos += scnprintf(buf+pos, bufsz-pos, "uapsd_max_sp = %d\n",
772 cmd.uapsd_max_sp);
773 pos += scnprintf(buf+pos, bufsz-pos, "heavy_tx_thld_packets = %d\n",
774 cmd.heavy_tx_thld_packets);
775 pos += scnprintf(buf+pos, bufsz-pos, "heavy_rx_thld_packets = %d\n",
776 cmd.heavy_rx_thld_packets);
777 pos += scnprintf(buf+pos, bufsz-pos, "heavy_tx_thld_percentage = %d\n",
778 cmd.heavy_tx_thld_percentage);
779 pos += scnprintf(buf+pos, bufsz-pos, "heavy_rx_thld_percentage = %d\n",
780 cmd.heavy_rx_thld_percentage);
781 pos += scnprintf(buf+pos, bufsz-pos, "uapsd_misbehaving_enable = %d\n",
782 (cmd.flags &
783 cpu_to_le16(POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK)) ?
784 1 : 0);
785
786 if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)))
787 return pos;
788
789 pos += scnprintf(buf+pos, bufsz-pos, "snooze_interval = %d\n",
790 cmd.snooze_interval);
791 pos += scnprintf(buf+pos, bufsz-pos, "snooze_window = %d\n",
792 cmd.snooze_window);
793
Alexander Bondare811ada2013-03-10 15:29:44 +0200794 return pos;
795}
796
Alexander Bondarb571a692013-05-21 14:49:09 +0300797void
798iwl_mvm_beacon_filter_debugfs_parameters(struct ieee80211_vif *vif,
799 struct iwl_beacon_filter_cmd *cmd)
800{
801 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
802 struct iwl_dbgfs_bf *dbgfs_bf = &mvmvif->dbgfs_bf;
803
804 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ENERGY_DELTA)
Hila Gonen5dca7c22013-07-16 11:15:35 +0300805 cmd->bf_energy_delta = cpu_to_le32(dbgfs_bf->bf_energy_delta);
Alexander Bondarb571a692013-05-21 14:49:09 +0300806 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ROAMING_ENERGY_DELTA)
807 cmd->bf_roaming_energy_delta =
Hila Gonen5dca7c22013-07-16 11:15:35 +0300808 cpu_to_le32(dbgfs_bf->bf_roaming_energy_delta);
Alexander Bondarb571a692013-05-21 14:49:09 +0300809 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ROAMING_STATE)
Hila Gonen5dca7c22013-07-16 11:15:35 +0300810 cmd->bf_roaming_state = cpu_to_le32(dbgfs_bf->bf_roaming_state);
811 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_THRESHOLD)
812 cmd->bf_temp_threshold =
813 cpu_to_le32(dbgfs_bf->bf_temp_threshold);
814 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_FAST_FILTER)
815 cmd->bf_temp_fast_filter =
816 cpu_to_le32(dbgfs_bf->bf_temp_fast_filter);
817 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_SLOW_FILTER)
818 cmd->bf_temp_slow_filter =
819 cpu_to_le32(dbgfs_bf->bf_temp_slow_filter);
Alexander Bondarb571a692013-05-21 14:49:09 +0300820 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_DEBUG_FLAG)
Hila Gonen5dca7c22013-07-16 11:15:35 +0300821 cmd->bf_debug_flag = cpu_to_le32(dbgfs_bf->bf_debug_flag);
Alexander Bondarb571a692013-05-21 14:49:09 +0300822 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ESCAPE_TIMER)
823 cmd->bf_escape_timer = cpu_to_le32(dbgfs_bf->bf_escape_timer);
824 if (dbgfs_bf->mask & MVM_DEBUGFS_BA_ESCAPE_TIMER)
825 cmd->ba_escape_timer = cpu_to_le32(dbgfs_bf->ba_escape_timer);
826 if (dbgfs_bf->mask & MVM_DEBUGFS_BA_ENABLE_BEACON_ABORT)
Hila Gonen5dca7c22013-07-16 11:15:35 +0300827 cmd->ba_enable_beacon_abort =
828 cpu_to_le32(dbgfs_bf->ba_enable_beacon_abort);
Alexander Bondarb571a692013-05-21 14:49:09 +0300829}
830#endif
831
Eliad Peller3dd37d02014-01-07 14:00:24 +0200832static int _iwl_mvm_enable_beacon_filter(struct iwl_mvm *mvm,
833 struct ieee80211_vif *vif,
834 struct iwl_beacon_filter_cmd *cmd,
835 u32 cmd_flags,
836 bool d0i3)
Hila Gonen7df15b12012-12-12 11:16:19 +0200837{
838 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Alexander Bondar071d49902013-05-06 13:03:59 +0300839 int ret;
Hila Gonen7df15b12012-12-12 11:16:19 +0200840
Avri Altmanfa7b2e72014-05-20 08:03:24 +0300841 if (mvmvif != mvm->bf_allowed_vif || !vif->bss_conf.dtim_period ||
Hila Gonen7df15b12012-12-12 11:16:19 +0200842 vif->type != NL80211_IFTYPE_STATION || vif->p2p)
843 return 0;
844
Gregory Greenmancdc306b2015-07-20 12:55:34 +0300845 iwl_mvm_beacon_filter_set_cqm_params(mvm, vif, cmd, d0i3);
Eliad Peller3dd37d02014-01-07 14:00:24 +0200846 if (!d0i3)
847 iwl_mvm_beacon_filter_debugfs_parameters(vif, cmd);
848 ret = iwl_mvm_beacon_filter_send_cmd(mvm, cmd, cmd_flags);
Alexander Bondar071d49902013-05-06 13:03:59 +0300849
Eliad Peller3dd37d02014-01-07 14:00:24 +0200850 /* don't change bf_enabled in case of temporary d0i3 configuration */
851 if (!ret && !d0i3)
Andrei Otcheretianskia20fd392013-07-21 17:23:59 +0300852 mvmvif->bf_data.bf_enabled = true;
Alexander Bondar071d49902013-05-06 13:03:59 +0300853
854 return ret;
Hila Gonen7df15b12012-12-12 11:16:19 +0200855}
856
Eliad Peller3dd37d02014-01-07 14:00:24 +0200857int iwl_mvm_enable_beacon_filter(struct iwl_mvm *mvm,
858 struct ieee80211_vif *vif,
859 u32 flags)
860{
861 struct iwl_beacon_filter_cmd cmd = {
862 IWL_BF_CMD_CONFIG_DEFAULTS,
863 .bf_enable_beacon_filter = cpu_to_le32(1),
864 };
865
866 return _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd, flags, false);
867}
868
Sara Sharon0db056d2015-12-29 11:07:15 +0200869static int _iwl_mvm_disable_beacon_filter(struct iwl_mvm *mvm,
870 struct ieee80211_vif *vif,
871 u32 flags, bool d0i3)
Hila Gonen7df15b12012-12-12 11:16:19 +0200872{
Alexander Bondar071d49902013-05-06 13:03:59 +0300873 struct iwl_beacon_filter_cmd cmd = {};
874 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
875 int ret;
Hila Gonen7df15b12012-12-12 11:16:19 +0200876
Emmanuel Grumbach73e5f2c2014-03-30 08:57:30 +0300877 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
Hila Gonen7df15b12012-12-12 11:16:19 +0200878 return 0;
879
Eliad Peller3dd37d02014-01-07 14:00:24 +0200880 ret = iwl_mvm_beacon_filter_send_cmd(mvm, &cmd, flags);
Alexander Bondar071d49902013-05-06 13:03:59 +0300881
Sara Sharon0db056d2015-12-29 11:07:15 +0200882 /* don't change bf_enabled in case of temporary d0i3 configuration */
883 if (!ret && !d0i3)
Andrei Otcheretianskia20fd392013-07-21 17:23:59 +0300884 mvmvif->bf_data.bf_enabled = false;
Alexander Bondar071d49902013-05-06 13:03:59 +0300885
886 return ret;
Hila Gonen7df15b12012-12-12 11:16:19 +0200887}
Alexander Bondare811ada2013-03-10 15:29:44 +0200888
Sara Sharon0db056d2015-12-29 11:07:15 +0200889int iwl_mvm_disable_beacon_filter(struct iwl_mvm *mvm,
890 struct ieee80211_vif *vif,
891 u32 flags)
892{
893 return _iwl_mvm_disable_beacon_filter(mvm, vif, flags, false);
894}
895
Luciano Coelho2533edc2014-08-08 19:50:46 +0300896static int iwl_mvm_power_set_ps(struct iwl_mvm *mvm)
Emmanuel Grumbach13b72322014-05-20 11:28:18 +0300897{
Luciano Coelho128aa942014-08-08 18:36:22 +0300898 bool disable_ps;
Emmanuel Grumbach13b72322014-05-20 11:28:18 +0300899 int ret;
900
Emmanuel Grumbach13b72322014-05-20 11:28:18 +0300901 /* disable PS if CAM */
Luciano Coelhob1873302014-08-08 17:12:07 +0300902 disable_ps = (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM);
Luciano Coelho2533edc2014-08-08 19:50:46 +0300903 /* ...or if any of the vifs require PS to be off */
904 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
905 IEEE80211_IFACE_ITER_NORMAL,
906 iwl_mvm_power_ps_disabled_iterator,
907 &disable_ps);
Luciano Coelhob1873302014-08-08 17:12:07 +0300908
909 /* update device power state if it has changed */
910 if (mvm->ps_disabled != disable_ps) {
911 bool old_ps_disabled = mvm->ps_disabled;
912
913 mvm->ps_disabled = disable_ps;
914 ret = iwl_mvm_power_update_device(mvm);
915 if (ret) {
916 mvm->ps_disabled = old_ps_disabled;
917 return ret;
Emmanuel Grumbach13b72322014-05-20 11:28:18 +0300918 }
919 }
920
Luciano Coelho128aa942014-08-08 18:36:22 +0300921 return 0;
922}
923
Luciano Coelhoef9203d2014-08-08 19:18:35 +0300924static int iwl_mvm_power_set_ba(struct iwl_mvm *mvm,
Avri Altman88121822016-01-24 15:18:59 +0200925 struct ieee80211_vif *vif)
Luciano Coelho128aa942014-08-08 18:36:22 +0300926{
Avri Altman88121822016-01-24 15:18:59 +0200927 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Avri Altman416ec4f2016-01-24 15:56:11 +0200928 struct iwl_beacon_filter_cmd cmd = {
929 IWL_BF_CMD_CONFIG_DEFAULTS,
930 .bf_enable_beacon_filter = cpu_to_le32(1),
931 };
Luciano Coelhoef9203d2014-08-08 19:18:35 +0300932
Avri Altman88121822016-01-24 15:18:59 +0200933 if (!mvmvif->bf_data.bf_enabled)
Luciano Coelhoef9203d2014-08-08 19:18:35 +0300934 return 0;
935
Avri Altman416ec4f2016-01-24 15:56:11 +0200936 if (mvm->cur_ucode == IWL_UCODE_WOWLAN)
937 cmd.ba_escape_timer = cpu_to_le32(IWL_BA_ESCAPE_TIMER_D3);
Luciano Coelhoef9203d2014-08-08 19:18:35 +0300938
Avri Altman416ec4f2016-01-24 15:56:11 +0200939 mvmvif->bf_data.ba_enabled = !(!mvmvif->pm_enabled ||
940 mvm->ps_disabled ||
941 !vif->bss_conf.ps ||
942 iwl_mvm_vif_low_latency(mvmvif));
943
944 return _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd, 0, false);
Luciano Coelhoef9203d2014-08-08 19:18:35 +0300945}
946
947int iwl_mvm_power_update_ps(struct iwl_mvm *mvm)
948{
Luciano Coelho128aa942014-08-08 18:36:22 +0300949 struct iwl_power_vifs vifs = {
950 .mvm = mvm,
951 };
Luciano Coelhoef9203d2014-08-08 19:18:35 +0300952 int ret;
953
954 lockdep_assert_held(&mvm->mutex);
955
956 /* get vifs info */
957 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
958 IEEE80211_IFACE_ITER_NORMAL,
959 iwl_mvm_power_get_vifs_iterator, &vifs);
960
Luciano Coelho2533edc2014-08-08 19:50:46 +0300961 ret = iwl_mvm_power_set_ps(mvm);
Luciano Coelhoef9203d2014-08-08 19:18:35 +0300962 if (ret)
963 return ret;
964
Avri Altman88121822016-01-24 15:18:59 +0200965 if (vifs.bss_vif)
966 return iwl_mvm_power_set_ba(mvm, vifs.bss_vif);
967
968 return 0;
Luciano Coelhoef9203d2014-08-08 19:18:35 +0300969}
970
971int iwl_mvm_power_update_mac(struct iwl_mvm *mvm)
972{
973 struct iwl_power_vifs vifs = {
974 .mvm = mvm,
975 };
Luciano Coelho128aa942014-08-08 18:36:22 +0300976 int ret;
977
978 lockdep_assert_held(&mvm->mutex);
979
980 /* get vifs info */
981 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
982 IEEE80211_IFACE_ITER_NORMAL,
983 iwl_mvm_power_get_vifs_iterator, &vifs);
984
985 iwl_mvm_power_set_pm(mvm, &vifs);
986
Luciano Coelho2533edc2014-08-08 19:50:46 +0300987 ret = iwl_mvm_power_set_ps(mvm);
Luciano Coelho128aa942014-08-08 18:36:22 +0300988 if (ret)
989 return ret;
990
Emmanuel Grumbach13b72322014-05-20 11:28:18 +0300991 if (vifs.bss_vif) {
992 ret = iwl_mvm_power_send_cmd(mvm, vifs.bss_vif);
993 if (ret)
994 return ret;
995 }
996
997 if (vifs.p2p_vif) {
998 ret = iwl_mvm_power_send_cmd(mvm, vifs.p2p_vif);
999 if (ret)
1000 return ret;
1001 }
1002
Avri Altman88121822016-01-24 15:18:59 +02001003 if (vifs.bss_vif)
1004 return iwl_mvm_power_set_ba(mvm, vifs.bss_vif);
1005
1006 return 0;
Emmanuel Grumbach13b72322014-05-20 11:28:18 +03001007}
1008
Eliad Peller3dd37d02014-01-07 14:00:24 +02001009int iwl_mvm_update_d0i3_power_mode(struct iwl_mvm *mvm,
1010 struct ieee80211_vif *vif,
1011 bool enable, u32 flags)
1012{
1013 int ret;
1014 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1015 struct iwl_mac_power_cmd cmd = {};
1016
1017 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
1018 return 0;
1019
1020 if (!vif->bss_conf.assoc)
1021 return 0;
1022
Johannes Berg9645edb2015-10-06 13:28:48 +02001023 iwl_mvm_power_build_cmd(mvm, vif, &cmd, !enable);
Eliad Peller3dd37d02014-01-07 14:00:24 +02001024
Eliad Peller3dd37d02014-01-07 14:00:24 +02001025 iwl_mvm_power_log(mvm, &cmd);
Emmanuel Grumbach474b50c2014-01-28 09:13:04 +02001026#ifdef CONFIG_IWLWIFI_DEBUGFS
1027 memcpy(&mvmvif->mac_pwr_cmd, &cmd, sizeof(cmd));
1028#endif
Eliad Peller3dd37d02014-01-07 14:00:24 +02001029 ret = iwl_mvm_send_cmd_pdu(mvm, MAC_PM_POWER_TABLE, flags,
1030 sizeof(cmd), &cmd);
1031 if (ret)
1032 return ret;
1033
1034 /* configure beacon filtering */
1035 if (mvmvif != mvm->bf_allowed_vif)
1036 return 0;
1037
1038 if (enable) {
1039 struct iwl_beacon_filter_cmd cmd_bf = {
1040 IWL_BF_CMD_CONFIG_D0I3,
1041 .bf_enable_beacon_filter = cpu_to_le32(1),
1042 };
Sara Sharon0db056d2015-12-29 11:07:15 +02001043 /*
1044 * When beacon storing is supported - disable beacon filtering
1045 * altogether - the latest beacon will be sent when exiting d0i3
1046 */
1047 if (fw_has_capa(&mvm->fw->ucode_capa,
1048 IWL_UCODE_TLV_CAPA_BEACON_STORING))
1049 ret = _iwl_mvm_disable_beacon_filter(mvm, vif, flags,
1050 true);
1051 else
1052 ret = _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd_bf,
1053 flags, true);
Eliad Peller3dd37d02014-01-07 14:00:24 +02001054 } else {
1055 if (mvmvif->bf_data.bf_enabled)
1056 ret = iwl_mvm_enable_beacon_filter(mvm, vif, flags);
1057 else
1058 ret = iwl_mvm_disable_beacon_filter(mvm, vif, flags);
1059 }
1060
1061 return ret;
1062}