blob: 506294fc2f878c037464112c307be2bb202deda6 [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
Johannes Berg8ca151b2013-01-24 14:25:36 +010010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
23 * USA
24 *
25 * The full GNU General Public License is included in this distribution
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020026 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010027 *
28 * Contact Information:
29 * Intel Linux Wireless <ilw@linux.intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 *
32 * BSD LICENSE
33 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020034 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +020035 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010036 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 *
42 * * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * * Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in
46 * the documentation and/or other materials provided with the
47 * distribution.
48 * * Neither the name Intel Corporation nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *
64 *****************************************************************************/
65
66#include <linux/kernel.h>
67#include <linux/module.h>
68#include <linux/slab.h>
Joe Perches93803b32015-03-02 19:54:49 -080069#include <linux/etherdevice.h>
Johannes Berg8ca151b2013-01-24 14:25:36 +010070
71#include <net/mac80211.h>
72
73#include "iwl-debug.h"
74#include "mvm.h"
75#include "iwl-modparams.h"
76#include "fw-api-power.h"
77
78#define POWER_KEEP_ALIVE_PERIOD_SEC 25
79
Emmanuel Grumbachd623d242014-01-26 12:58:24 +020080static
Alexander Bondare811ada2013-03-10 15:29:44 +020081int iwl_mvm_beacon_filter_send_cmd(struct iwl_mvm *mvm,
Eliad Peller3dd37d02014-01-07 14:00:24 +020082 struct iwl_beacon_filter_cmd *cmd,
83 u32 flags)
Alexander Bondar071d49902013-05-06 13:03:59 +030084{
Emmanuel Grumbachd623d242014-01-26 12:58:24 +020085 IWL_DEBUG_POWER(mvm, "ba_enable_beacon_abort is: %d\n",
86 le32_to_cpu(cmd->ba_enable_beacon_abort));
87 IWL_DEBUG_POWER(mvm, "ba_escape_timer is: %d\n",
88 le32_to_cpu(cmd->ba_escape_timer));
89 IWL_DEBUG_POWER(mvm, "bf_debug_flag is: %d\n",
90 le32_to_cpu(cmd->bf_debug_flag));
91 IWL_DEBUG_POWER(mvm, "bf_enable_beacon_filter is: %d\n",
92 le32_to_cpu(cmd->bf_enable_beacon_filter));
93 IWL_DEBUG_POWER(mvm, "bf_energy_delta is: %d\n",
94 le32_to_cpu(cmd->bf_energy_delta));
95 IWL_DEBUG_POWER(mvm, "bf_escape_timer is: %d\n",
96 le32_to_cpu(cmd->bf_escape_timer));
97 IWL_DEBUG_POWER(mvm, "bf_roaming_energy_delta is: %d\n",
98 le32_to_cpu(cmd->bf_roaming_energy_delta));
99 IWL_DEBUG_POWER(mvm, "bf_roaming_state is: %d\n",
100 le32_to_cpu(cmd->bf_roaming_state));
101 IWL_DEBUG_POWER(mvm, "bf_temp_threshold is: %d\n",
102 le32_to_cpu(cmd->bf_temp_threshold));
103 IWL_DEBUG_POWER(mvm, "bf_temp_fast_filter is: %d\n",
104 le32_to_cpu(cmd->bf_temp_fast_filter));
105 IWL_DEBUG_POWER(mvm, "bf_temp_slow_filter is: %d\n",
106 le32_to_cpu(cmd->bf_temp_slow_filter));
Alexander Bondar071d49902013-05-06 13:03:59 +0300107
Emmanuel Grumbachd623d242014-01-26 12:58:24 +0200108 return iwl_mvm_send_cmd_pdu(mvm, REPLY_BEACON_FILTERING_CMD, flags,
109 sizeof(struct iwl_beacon_filter_cmd), cmd);
Alexander Bondar071d49902013-05-06 13:03:59 +0300110}
111
Andrei Otcheretianskia20fd392013-07-21 17:23:59 +0300112static
113void iwl_mvm_beacon_filter_set_cqm_params(struct iwl_mvm *mvm,
114 struct ieee80211_vif *vif,
Gregory Greenmancdc306b2015-07-20 12:55:34 +0300115 struct iwl_beacon_filter_cmd *cmd,
116 bool d0i3)
Andrei Otcheretianskia20fd392013-07-21 17:23:59 +0300117{
118 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
119
Gregory Greenmancdc306b2015-07-20 12:55:34 +0300120 if (vif->bss_conf.cqm_rssi_thold && !d0i3) {
Andrei Otcheretianskia20fd392013-07-21 17:23:59 +0300121 cmd->bf_energy_delta =
122 cpu_to_le32(vif->bss_conf.cqm_rssi_hyst);
123 /* fw uses an absolute value for this */
124 cmd->bf_roaming_state =
125 cpu_to_le32(-vif->bss_conf.cqm_rssi_thold);
126 }
127 cmd->ba_enable_beacon_abort = cpu_to_le32(mvmvif->bf_data.ba_enabled);
128}
129
Alexander Bondar3f0b2b32013-03-05 14:08:23 +0200130static void iwl_mvm_power_log(struct iwl_mvm *mvm,
Alexander Bondare811ada2013-03-10 15:29:44 +0200131 struct iwl_mac_power_cmd *cmd)
Alexander Bondar3f0b2b32013-03-05 14:08:23 +0200132{
133 IWL_DEBUG_POWER(mvm,
Alexander Bondare811ada2013-03-10 15:29:44 +0200134 "Sending power table command on mac id 0x%X for power level %d, flags = 0x%X\n",
135 cmd->id_and_color, iwlmvm_mod_params.power_scheme,
Alexander Bondar3f0b2b32013-03-05 14:08:23 +0200136 le16_to_cpu(cmd->flags));
Alexander Bondare811ada2013-03-10 15:29:44 +0200137 IWL_DEBUG_POWER(mvm, "Keep alive = %u sec\n",
138 le16_to_cpu(cmd->keep_alive_seconds));
Alexander Bondar3f0b2b32013-03-05 14:08:23 +0200139
Alexander Bondare3c588e2013-04-07 14:08:59 +0300140 if (!(cmd->flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK))) {
141 IWL_DEBUG_POWER(mvm, "Disable power management\n");
142 return;
143 }
144
145 IWL_DEBUG_POWER(mvm, "Rx timeout = %u usec\n",
146 le32_to_cpu(cmd->rx_data_timeout));
147 IWL_DEBUG_POWER(mvm, "Tx timeout = %u usec\n",
148 le32_to_cpu(cmd->tx_data_timeout));
149 if (cmd->flags & cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK))
150 IWL_DEBUG_POWER(mvm, "DTIM periods to skip = %u\n",
151 cmd->skip_dtim_periods);
152 if (cmd->flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
153 IWL_DEBUG_POWER(mvm, "LP RX RSSI threshold = %u\n",
154 cmd->lprx_rssi_threshold);
155 if (cmd->flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK)) {
156 IWL_DEBUG_POWER(mvm, "uAPSD enabled\n");
157 IWL_DEBUG_POWER(mvm, "Rx timeout (uAPSD) = %u usec\n",
158 le32_to_cpu(cmd->rx_data_timeout_uapsd));
159 IWL_DEBUG_POWER(mvm, "Tx timeout (uAPSD) = %u usec\n",
160 le32_to_cpu(cmd->tx_data_timeout_uapsd));
161 IWL_DEBUG_POWER(mvm, "QNDP TID = %d\n", cmd->qndp_tid);
162 IWL_DEBUG_POWER(mvm, "ACs flags = 0x%x\n", cmd->uapsd_ac_flags);
163 IWL_DEBUG_POWER(mvm, "Max SP = %d\n", cmd->uapsd_max_sp);
Alexander Bondar3f0b2b32013-03-05 14:08:23 +0200164 }
165}
166
Alexander Bondar175a70b2013-04-14 20:59:37 +0300167static void iwl_mvm_power_configure_uapsd(struct iwl_mvm *mvm,
168 struct ieee80211_vif *vif,
169 struct iwl_mac_power_cmd *cmd)
170{
171 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
172 enum ieee80211_ac_numbers ac;
173 bool tid_found = false;
174
175 for (ac = IEEE80211_AC_VO; ac <= IEEE80211_AC_BK; ac++) {
176 if (!mvmvif->queue_params[ac].uapsd)
177 continue;
178
179 if (mvm->cur_ucode != IWL_UCODE_WOWLAN)
180 cmd->flags |=
181 cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK);
182
183 cmd->uapsd_ac_flags |= BIT(ac);
184
185 /* QNDP TID - the highest TID with no admission control */
186 if (!tid_found && !mvmvif->queue_params[ac].acm) {
187 tid_found = true;
188 switch (ac) {
189 case IEEE80211_AC_VO:
190 cmd->qndp_tid = 6;
191 break;
192 case IEEE80211_AC_VI:
193 cmd->qndp_tid = 5;
194 break;
195 case IEEE80211_AC_BE:
196 cmd->qndp_tid = 0;
197 break;
198 case IEEE80211_AC_BK:
199 cmd->qndp_tid = 1;
200 break;
201 }
202 }
203 }
204
Eliad Peller84fd7602014-07-30 15:42:19 +0300205 if (!(cmd->flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK))) {
206#ifdef CONFIG_IWLWIFI_DEBUGFS
207 /* set advanced pm flag with no uapsd ACs to enable ps-poll */
208 if (mvmvif->dbgfs_pm.use_ps_poll)
209 cmd->flags |=
210 cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK);
211#endif
Alexander Bondar175a70b2013-04-14 20:59:37 +0300212 return;
Eliad Peller84fd7602014-07-30 15:42:19 +0300213 }
Alexander Bondar175a70b2013-04-14 20:59:37 +0300214
215 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
228 cmd->uapsd_max_sp = IWL_UAPSD_MAX_SP;
229
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 Altmanc6e37a62014-04-01 12:08:13 +0300260static bool iwl_mvm_power_allow_uapsd(struct iwl_mvm *mvm,
261 struct ieee80211_vif *vif)
262{
263 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
264
265 if (!memcmp(mvmvif->uapsd_misbehaving_bssid, vif->bss_conf.bssid,
266 ETH_ALEN))
267 return false;
268
269 if (vif->p2p &&
270 !(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_P2P_PS_UAPSD))
271 return false;
272 /*
273 * Avoid using uAPSD if P2P client is associated to GO that uses
274 * opportunistic power save. This is due to current FW limitation.
275 */
276 if (vif->p2p &&
277 (vif->bss_conf.p2p_noa_attr.oppps_ctwindow &
278 IEEE80211_P2P_OPPPS_ENABLE_BIT))
279 return false;
280
Avri Altman05345282014-05-08 10:06:01 +0300281 /*
282 * Avoid using uAPSD if client is in DCM -
283 * low latency issue in Miracast
284 */
Arik Nemtsovcf7b4912014-05-15 11:44:40 +0300285 if (iwl_mvm_phy_ctx_count(mvm) >= 2)
Avri Altman05345282014-05-08 10:06:01 +0300286 return false;
287
Avri Altmanc6e37a62014-04-01 12:08:13 +0300288 return true;
289}
290
Avri Altman886ca9f2014-10-02 19:46:16 +0200291static int iwl_mvm_power_get_skip_over_dtim(int dtimper, int bi)
292{
293 int numerator;
294 int dtim_interval = dtimper * bi;
295
296 if (WARN_ON(!dtim_interval))
297 return 0;
298
299 if (dtimper == 1) {
300 if (bi > 100)
301 numerator = 408;
302 else
303 numerator = 510;
304 } else if (dtimper < 10) {
305 numerator = 612;
306 } else {
307 return 0;
308 }
309 return max(1, (numerator / dtim_interval));
310}
311
Avri Altman49c6d802014-08-27 19:58:56 +0300312static bool iwl_mvm_power_is_radar(struct ieee80211_vif *vif)
313{
314 struct ieee80211_chanctx_conf *chanctx_conf;
315 struct ieee80211_channel *chan;
316 bool radar_detect = false;
317
318 rcu_read_lock();
319 chanctx_conf = rcu_dereference(vif->chanctx_conf);
320 WARN_ON(!chanctx_conf);
321 if (chanctx_conf) {
322 chan = chanctx_conf->def.chan;
323 radar_detect = chan->flags & IEEE80211_CHAN_RADAR;
324 }
325 rcu_read_unlock();
326
327 return radar_detect;
328}
329
Alexander Bondare811ada2013-03-10 15:29:44 +0200330static void iwl_mvm_power_build_cmd(struct iwl_mvm *mvm,
331 struct ieee80211_vif *vif,
332 struct iwl_mac_power_cmd *cmd)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100333{
Avri Altman5387b342014-11-11 07:29:59 +0200334 int dtimper, bi;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100335 int keep_alive;
336 bool radar_detect = false;
Alexander Bondarb571a692013-05-21 14:49:09 +0300337 struct iwl_mvm_vif *mvmvif __maybe_unused =
338 iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100339
Alexander Bondare811ada2013-03-10 15:29:44 +0200340 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
341 mvmvif->color));
Emmanuel Grumbach717e2392014-07-31 14:39:40 +0300342 dtimper = vif->bss_conf.dtim_period;
Avri Altman886ca9f2014-10-02 19:46:16 +0200343 bi = vif->bss_conf.beacon_int;
Alexander Bondare811ada2013-03-10 15:29:44 +0200344
Alexander Bondare16cf7e2013-03-05 14:01:27 +0200345 /*
346 * Regardless of power management state the driver must set
347 * keep alive period. FW will use it for sending keep alive NDPs
Alexander Bondare811ada2013-03-10 15:29:44 +0200348 * immediately after association. Check that keep alive period
349 * is at least 3 * DTIM
Alexander Bondare16cf7e2013-03-05 14:01:27 +0200350 */
Avri Altman5387b342014-11-11 07:29:59 +0200351 keep_alive = DIV_ROUND_UP(ieee80211_tu_to_usec(3 * dtimper * bi),
352 USEC_PER_SEC);
353 keep_alive = max(keep_alive, POWER_KEEP_ALIVE_PERIOD_SEC);
Alexander Bondare811ada2013-03-10 15:29:44 +0200354 cmd->keep_alive_seconds = cpu_to_le16(keep_alive);
Alexander Bondare16cf7e2013-03-05 14:01:27 +0200355
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200356 if (mvm->ps_disabled)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100357 return;
358
Alexander Bondar9a613042013-03-05 12:54:00 +0200359 cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK);
360
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200361 if (!vif->bss_conf.ps || iwl_mvm_vif_low_latency(mvmvif) ||
Arik Nemtsovfcf23352015-02-22 19:15:04 +0200362 !mvmvif->pm_enabled)
Alexander Bondar9a613042013-03-05 12:54:00 +0200363 return;
364
365 cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100366
Alexander Bondarbd4ace22013-03-21 17:14:14 +0200367 if (vif->bss_conf.beacon_rate &&
368 (vif->bss_conf.beacon_rate->bitrate == 10 ||
369 vif->bss_conf.beacon_rate->bitrate == 60)) {
370 cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
Alexander Bondare811ada2013-03-10 15:29:44 +0200371 cmd->lprx_rssi_threshold = POWER_LPRX_RSSI_THRESHOLD;
Alexander Bondarbd4ace22013-03-21 17:14:14 +0200372 }
373
Johannes Berg8ca151b2013-01-24 14:25:36 +0100374 /* Check if radar detection is required on current channel */
Avri Altman49c6d802014-08-27 19:58:56 +0300375 radar_detect = iwl_mvm_power_is_radar(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100376
377 /* Check skip over DTIM conditions */
Avri Altman886ca9f2014-10-02 19:46:16 +0200378 if (!radar_detect && (dtimper < 10) &&
Alexander Bondaree1e8422013-04-23 13:52:10 +0300379 (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_LP ||
380 mvm->cur_ucode == IWL_UCODE_WOWLAN)) {
Avri Altman886ca9f2014-10-02 19:46:16 +0200381 cmd->skip_dtim_periods =
382 iwl_mvm_power_get_skip_over_dtim(dtimper, bi);
383 if (cmd->skip_dtim_periods)
384 cmd->flags |=
385 cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
Alexander Bondar5b1dbfc2013-04-23 13:32:35 +0300386 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100387
Alexander Bondaree1e8422013-04-23 13:52:10 +0300388 if (mvm->cur_ucode != IWL_UCODE_WOWLAN) {
Johannes Berg99545922013-06-14 13:36:21 +0200389 cmd->rx_data_timeout =
390 cpu_to_le32(IWL_MVM_DEFAULT_PS_RX_DATA_TIMEOUT);
391 cmd->tx_data_timeout =
392 cpu_to_le32(IWL_MVM_DEFAULT_PS_TX_DATA_TIMEOUT);
Alexander Bondaree1e8422013-04-23 13:52:10 +0300393 } else {
Johannes Berg99545922013-06-14 13:36:21 +0200394 cmd->rx_data_timeout =
395 cpu_to_le32(IWL_MVM_WOWLAN_PS_RX_DATA_TIMEOUT);
396 cmd->tx_data_timeout =
397 cpu_to_le32(IWL_MVM_WOWLAN_PS_TX_DATA_TIMEOUT);
Alexander Bondaree1e8422013-04-23 13:52:10 +0300398 }
Alexander Bondarb571a692013-05-21 14:49:09 +0300399
Avri Altmanc6e37a62014-04-01 12:08:13 +0300400 if (iwl_mvm_power_allow_uapsd(mvm, vif))
Alexander Bondar175a70b2013-04-14 20:59:37 +0300401 iwl_mvm_power_configure_uapsd(mvm, vif, cmd);
Alexander Bondare3c588e2013-04-07 14:08:59 +0300402
Alexander Bondarb571a692013-05-21 14:49:09 +0300403#ifdef CONFIG_IWLWIFI_DEBUGFS
404 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_KEEP_ALIVE)
Alexander Bondare811ada2013-03-10 15:29:44 +0200405 cmd->keep_alive_seconds =
406 cpu_to_le16(mvmvif->dbgfs_pm.keep_alive_seconds);
Alexander Bondarb571a692013-05-21 14:49:09 +0300407 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_OVER_DTIM) {
408 if (mvmvif->dbgfs_pm.skip_over_dtim)
409 cmd->flags |=
410 cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
411 else
412 cmd->flags &=
413 cpu_to_le16(~POWER_FLAGS_SKIP_OVER_DTIM_MSK);
414 }
415 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_RX_DATA_TIMEOUT)
416 cmd->rx_data_timeout =
417 cpu_to_le32(mvmvif->dbgfs_pm.rx_data_timeout);
418 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_TX_DATA_TIMEOUT)
419 cmd->tx_data_timeout =
420 cpu_to_le32(mvmvif->dbgfs_pm.tx_data_timeout);
421 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_DTIM_PERIODS)
Alexander Bondare811ada2013-03-10 15:29:44 +0200422 cmd->skip_dtim_periods = mvmvif->dbgfs_pm.skip_dtim_periods;
Alexander Bondarbd4ace22013-03-21 17:14:14 +0200423 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_ENA) {
424 if (mvmvif->dbgfs_pm.lprx_ena)
425 cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
426 else
427 cmd->flags &= cpu_to_le16(~POWER_FLAGS_LPRX_ENA_MSK);
428 }
429 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_RSSI_THRESHOLD)
Alexander Bondare811ada2013-03-10 15:29:44 +0200430 cmd->lprx_rssi_threshold = mvmvif->dbgfs_pm.lprx_rssi_threshold;
Alexander Bondar89716342013-08-04 17:52:23 +0300431 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SNOOZE_ENABLE) {
432 if (mvmvif->dbgfs_pm.snooze_ena)
433 cmd->flags |=
434 cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK);
435 else
436 cmd->flags &=
437 cpu_to_le16(~POWER_FLAGS_SNOOZE_ENA_MSK);
438 }
Alexander Bondare45a9412013-12-04 10:13:24 +0200439 if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_UAPSD_MISBEHAVING) {
440 u16 flag = POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK;
441 if (mvmvif->dbgfs_pm.uapsd_misbehaving)
442 cmd->flags |= cpu_to_le16(flag);
443 else
444 cmd->flags &= cpu_to_le16(flag);
445 }
Alexander Bondarb571a692013-05-21 14:49:09 +0300446#endif /* CONFIG_IWLWIFI_DEBUGFS */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100447}
448
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200449static int iwl_mvm_power_send_cmd(struct iwl_mvm *mvm,
Alexander Bondare811ada2013-03-10 15:29:44 +0200450 struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100451{
Alexander Bondare811ada2013-03-10 15:29:44 +0200452 struct iwl_mac_power_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100453
Alexander Bondarf4a3e2f2013-03-05 13:47:04 +0200454 iwl_mvm_power_build_cmd(mvm, vif, &cmd);
Alexander Bondar3f0b2b32013-03-05 14:08:23 +0200455 iwl_mvm_power_log(mvm, &cmd);
Emmanuel Grumbach474b50c2014-01-28 09:13:04 +0200456#ifdef CONFIG_IWLWIFI_DEBUGFS
457 memcpy(&iwl_mvm_vif_from_mac80211(vif)->mac_pwr_cmd, &cmd, sizeof(cmd));
458#endif
Johannes Berg8ca151b2013-01-24 14:25:36 +0100459
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300460 return iwl_mvm_send_cmd_pdu(mvm, MAC_PM_POWER_TABLE, 0,
Emmanuel Grumbach06280a22014-01-27 08:09:23 +0200461 sizeof(cmd), &cmd);
462}
463
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200464int iwl_mvm_power_update_device(struct iwl_mvm *mvm)
Alexander Bondar64b928c2013-09-03 14:18:03 +0300465{
466 struct iwl_device_power_cmd cmd = {
467 .flags = cpu_to_le16(DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK),
468 };
469
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200470 if (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM)
471 mvm->ps_disabled = true;
472
473 if (mvm->ps_disabled)
Alexander Bondar64b928c2013-09-03 14:18:03 +0300474 cmd.flags |= cpu_to_le16(DEVICE_POWER_FLAGS_CAM_MSK);
475
476#ifdef CONFIG_IWLWIFI_DEBUGFS
477 if ((mvm->cur_ucode == IWL_UCODE_WOWLAN) ? mvm->disable_power_off_d3 :
478 mvm->disable_power_off)
479 cmd.flags &=
480 cpu_to_le16(~DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK);
481#endif
482 IWL_DEBUG_POWER(mvm,
483 "Sending device power command with flags = 0x%X\n",
484 cmd.flags);
485
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300486 return iwl_mvm_send_cmd_pdu(mvm, POWER_TABLE_CMD, 0, sizeof(cmd),
Alexander Bondar64b928c2013-09-03 14:18:03 +0300487 &cmd);
488}
489
Alexander Bondar175a70b2013-04-14 20:59:37 +0300490void iwl_mvm_power_vif_assoc(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
491{
492 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
493
494 if (memcmp(vif->bss_conf.bssid, mvmvif->uapsd_misbehaving_bssid,
495 ETH_ALEN))
Joe Perches93803b32015-03-02 19:54:49 -0800496 eth_zero_addr(mvmvif->uapsd_misbehaving_bssid);
Alexander Bondar175a70b2013-04-14 20:59:37 +0300497}
498
499static void iwl_mvm_power_uapsd_misbehav_ap_iterator(void *_data, u8 *mac,
500 struct ieee80211_vif *vif)
501{
502 u8 *ap_sta_id = _data;
503 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
504
505 /* The ap_sta_id is not expected to change during current association
506 * so no explicit protection is needed
507 */
508 if (mvmvif->ap_sta_id == *ap_sta_id)
509 memcpy(mvmvif->uapsd_misbehaving_bssid, vif->bss_conf.bssid,
510 ETH_ALEN);
511}
512
Johannes Berg04168412015-06-23 21:22:09 +0200513void iwl_mvm_power_uapsd_misbehaving_ap_notif(struct iwl_mvm *mvm,
514 struct iwl_rx_cmd_buffer *rxb)
Alexander Bondar175a70b2013-04-14 20:59:37 +0300515{
516 struct iwl_rx_packet *pkt = rxb_addr(rxb);
517 struct iwl_uapsd_misbehaving_ap_notif *notif = (void *)pkt->data;
518 u8 ap_sta_id = le32_to_cpu(notif->sta_id);
519
520 ieee80211_iterate_active_interfaces_atomic(
521 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
522 iwl_mvm_power_uapsd_misbehav_ap_iterator, &ap_sta_id);
Alexander Bondar175a70b2013-04-14 20:59:37 +0300523}
524
Avri Altman19889022014-03-19 07:25:06 +0200525struct iwl_power_vifs {
Arik Nemtsovfa3d07e2014-05-15 18:59:32 +0300526 struct iwl_mvm *mvm;
Emmanuel Grumbach06280a22014-01-27 08:09:23 +0200527 struct ieee80211_vif *bf_vif;
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200528 struct ieee80211_vif *bss_vif;
Avri Altman8ea0c682014-02-20 13:28:57 +0200529 struct ieee80211_vif *p2p_vif;
Avri Altman19889022014-03-19 07:25:06 +0200530 struct ieee80211_vif *ap_vif;
531 struct ieee80211_vif *monitor_vif;
532 bool p2p_active;
533 bool bss_active;
534 bool ap_active;
535 bool monitor_active;
Emmanuel Grumbach06280a22014-01-27 08:09:23 +0200536};
537
Luciano Coelho128aa942014-08-08 18:36:22 +0300538static void iwl_mvm_power_disable_pm_iterator(void *_data, u8* mac,
539 struct ieee80211_vif *vif)
540{
541 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
542
543 mvmvif->pm_enabled = false;
544}
545
Luciano Coelho2533edc2014-08-08 19:50:46 +0300546static void iwl_mvm_power_ps_disabled_iterator(void *_data, u8* mac,
547 struct ieee80211_vif *vif)
548{
549 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
550 bool *disable_ps = _data;
551
552 if (mvmvif->phy_ctxt)
553 if (mvmvif->phy_ctxt->id < MAX_PHYS)
554 *disable_ps |= mvmvif->ps_disabled;
555}
556
Luciano Coelho128aa942014-08-08 18:36:22 +0300557static void iwl_mvm_power_get_vifs_iterator(void *_data, u8 *mac,
558 struct ieee80211_vif *vif)
Alexander Bondar1c2abf72013-08-27 20:31:48 +0300559{
Emmanuel Grumbach06280a22014-01-27 08:09:23 +0200560 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Avri Altman19889022014-03-19 07:25:06 +0200561 struct iwl_power_vifs *power_iterator = _data;
Alexander Bondar1c2abf72013-08-27 20:31:48 +0300562
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200563 switch (ieee80211_vif_type_p2p(vif)) {
564 case NL80211_IFTYPE_P2P_DEVICE:
565 break;
Emmanuel Grumbach06280a22014-01-27 08:09:23 +0200566
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200567 case NL80211_IFTYPE_P2P_GO:
568 case NL80211_IFTYPE_AP:
Avri Altman19889022014-03-19 07:25:06 +0200569 /* only a single MAC of the same type */
570 WARN_ON(power_iterator->ap_vif);
571 power_iterator->ap_vif = vif;
572 if (mvmvif->phy_ctxt)
573 if (mvmvif->phy_ctxt->id < MAX_PHYS)
574 power_iterator->ap_active = true;
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200575 break;
Emmanuel Grumbach06280a22014-01-27 08:09:23 +0200576
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200577 case NL80211_IFTYPE_MONITOR:
Avri Altman19889022014-03-19 07:25:06 +0200578 /* only a single MAC of the same type */
579 WARN_ON(power_iterator->monitor_vif);
580 power_iterator->monitor_vif = vif;
581 if (mvmvif->phy_ctxt)
582 if (mvmvif->phy_ctxt->id < MAX_PHYS)
583 power_iterator->monitor_active = true;
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200584 break;
585
586 case NL80211_IFTYPE_P2P_CLIENT:
Avri Altman19889022014-03-19 07:25:06 +0200587 /* only a single MAC of the same type */
Avri Altman8ea0c682014-02-20 13:28:57 +0200588 WARN_ON(power_iterator->p2p_vif);
589 power_iterator->p2p_vif = vif;
Avri Altman19889022014-03-19 07:25:06 +0200590 if (mvmvif->phy_ctxt)
591 if (mvmvif->phy_ctxt->id < MAX_PHYS)
592 power_iterator->p2p_active = true;
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200593 break;
594
595 case NL80211_IFTYPE_STATION:
Avri Altman19889022014-03-19 07:25:06 +0200596 /* only a single MAC of the same type */
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200597 WARN_ON(power_iterator->bss_vif);
598 power_iterator->bss_vif = vif;
Avri Altman19889022014-03-19 07:25:06 +0200599 if (mvmvif->phy_ctxt)
600 if (mvmvif->phy_ctxt->id < MAX_PHYS)
601 power_iterator->bss_active = true;
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200602
603 if (mvmvif->bf_data.bf_enabled &&
604 !WARN_ON(power_iterator->bf_vif))
605 power_iterator->bf_vif = vif;
Alexander Bondar7303dd7f2014-02-03 21:57:28 +0200606
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200607 break;
608
609 default:
610 break;
611 }
Alexander Bondar1c2abf72013-08-27 20:31:48 +0300612}
613
Luciano Coelhob1873302014-08-08 17:12:07 +0300614static void iwl_mvm_power_set_pm(struct iwl_mvm *mvm,
615 struct iwl_power_vifs *vifs)
Alexander Bondar1c2abf72013-08-27 20:31:48 +0300616{
Avri Altman19889022014-03-19 07:25:06 +0200617 struct iwl_mvm_vif *bss_mvmvif = NULL;
618 struct iwl_mvm_vif *p2p_mvmvif = NULL;
619 struct iwl_mvm_vif *ap_mvmvif = NULL;
620 bool client_same_channel = false;
621 bool ap_same_channel = false;
622
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200623 lockdep_assert_held(&mvm->mutex);
624
Luciano Coelho128aa942014-08-08 18:36:22 +0300625 /* set pm_enable to false */
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200626 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
Luciano Coelho128aa942014-08-08 18:36:22 +0300627 IEEE80211_IFACE_ITER_NORMAL,
628 iwl_mvm_power_disable_pm_iterator,
629 NULL);
Avri Altman19889022014-03-19 07:25:06 +0200630
631 if (vifs->bss_vif)
632 bss_mvmvif = iwl_mvm_vif_from_mac80211(vifs->bss_vif);
633
634 if (vifs->p2p_vif)
635 p2p_mvmvif = iwl_mvm_vif_from_mac80211(vifs->p2p_vif);
636
637 if (vifs->ap_vif)
638 ap_mvmvif = iwl_mvm_vif_from_mac80211(vifs->ap_vif);
639
Arik Nemtsovfcf23352015-02-22 19:15:04 +0200640 /* don't allow PM if any TDLS stations exist */
641 if (iwl_mvm_tdls_sta_count(mvm, NULL))
642 return;
643
Avri Altman19889022014-03-19 07:25:06 +0200644 /* enable PM on bss if bss stand alone */
Arik Nemtsov9474d442014-09-03 12:09:45 +0300645 if (vifs->bss_active && !vifs->p2p_active && !vifs->ap_active) {
Avri Altman19889022014-03-19 07:25:06 +0200646 bss_mvmvif->pm_enabled = true;
647 return;
648 }
649
650 /* enable PM on p2p if p2p stand alone */
Arik Nemtsov9474d442014-09-03 12:09:45 +0300651 if (vifs->p2p_active && !vifs->bss_active && !vifs->ap_active) {
Avri Altman19889022014-03-19 07:25:06 +0200652 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_P2P_PM)
653 p2p_mvmvif->pm_enabled = true;
654 return;
655 }
656
657 if (vifs->bss_active && vifs->p2p_active)
658 client_same_channel = (bss_mvmvif->phy_ctxt->id ==
659 p2p_mvmvif->phy_ctxt->id);
660 if (vifs->bss_active && vifs->ap_active)
661 ap_same_channel = (bss_mvmvif->phy_ctxt->id ==
662 ap_mvmvif->phy_ctxt->id);
663
Avri Altman63ef81c2014-04-02 07:54:07 +0300664 /* clients are not stand alone: enable PM if DCM */
665 if (!(client_same_channel || ap_same_channel) &&
Avri Altman19889022014-03-19 07:25:06 +0200666 (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BSS_P2P_PS_DCM)) {
Avri Altman63ef81c2014-04-02 07:54:07 +0300667 if (vifs->bss_active)
Avri Altman19889022014-03-19 07:25:06 +0200668 bss_mvmvif->pm_enabled = true;
Avri Altman63ef81c2014-04-02 07:54:07 +0300669 if (vifs->p2p_active &&
670 (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_P2P_PM))
671 p2p_mvmvif->pm_enabled = true;
672 return;
Avri Altman19889022014-03-19 07:25:06 +0200673 }
674
675 /*
676 * There is only one channel in the system and there are only
677 * bss and p2p clients that share it
678 */
679 if (client_same_channel && !vifs->ap_active &&
680 (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BSS_P2P_PS_SCM)) {
681 /* share same channel*/
682 bss_mvmvif->pm_enabled = true;
683 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_P2P_PM)
684 p2p_mvmvif->pm_enabled = true;
685 }
Emmanuel Grumbache5e7aa82014-01-27 16:57:33 +0200686}
687
Alexander Bondarb571a692013-05-21 14:49:09 +0300688#ifdef CONFIG_IWLWIFI_DEBUGFS
Emmanuel Grumbachc1cb92f2014-01-28 10:17:18 +0200689int iwl_mvm_power_mac_dbgfs_read(struct iwl_mvm *mvm,
690 struct ieee80211_vif *vif, char *buf,
691 int bufsz)
Alexander Bondare811ada2013-03-10 15:29:44 +0200692{
Emmanuel Grumbach474b50c2014-01-28 09:13:04 +0200693 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Alexander Bondare811ada2013-03-10 15:29:44 +0200694 struct iwl_mac_power_cmd cmd = {};
695 int pos = 0;
696
Emmanuel Grumbach474b50c2014-01-28 09:13:04 +0200697 mutex_lock(&mvm->mutex);
698 memcpy(&cmd, &mvmvif->mac_pwr_cmd, sizeof(cmd));
699 mutex_unlock(&mvm->mutex);
Alexander Bondare811ada2013-03-10 15:29:44 +0200700
Alexander Bondare811ada2013-03-10 15:29:44 +0200701 pos += scnprintf(buf+pos, bufsz-pos, "power_scheme = %d\n",
702 iwlmvm_mod_params.power_scheme);
703 pos += scnprintf(buf+pos, bufsz-pos, "flags = 0x%x\n",
704 le16_to_cpu(cmd.flags));
705 pos += scnprintf(buf+pos, bufsz-pos, "keep_alive = %d\n",
706 le16_to_cpu(cmd.keep_alive_seconds));
707
Alexander Bondar175a70b2013-04-14 20:59:37 +0300708 if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK)))
709 return pos;
710
711 pos += scnprintf(buf+pos, bufsz-pos, "skip_over_dtim = %d\n",
712 (cmd.flags &
713 cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK)) ? 1 : 0);
714 pos += scnprintf(buf+pos, bufsz-pos, "skip_dtim_periods = %d\n",
715 cmd.skip_dtim_periods);
716 if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK))) {
717 pos += scnprintf(buf+pos, bufsz-pos, "rx_data_timeout = %d\n",
718 le32_to_cpu(cmd.rx_data_timeout));
719 pos += scnprintf(buf+pos, bufsz-pos, "tx_data_timeout = %d\n",
720 le32_to_cpu(cmd.tx_data_timeout));
Alexander Bondare811ada2013-03-10 15:29:44 +0200721 }
Alexander Bondar175a70b2013-04-14 20:59:37 +0300722 if (cmd.flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
723 pos += scnprintf(buf+pos, bufsz-pos,
724 "lprx_rssi_threshold = %d\n",
725 cmd.lprx_rssi_threshold);
726
727 if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK)))
728 return pos;
729
730 pos += scnprintf(buf+pos, bufsz-pos, "rx_data_timeout_uapsd = %d\n",
731 le32_to_cpu(cmd.rx_data_timeout_uapsd));
732 pos += scnprintf(buf+pos, bufsz-pos, "tx_data_timeout_uapsd = %d\n",
733 le32_to_cpu(cmd.tx_data_timeout_uapsd));
734 pos += scnprintf(buf+pos, bufsz-pos, "qndp_tid = %d\n", cmd.qndp_tid);
735 pos += scnprintf(buf+pos, bufsz-pos, "uapsd_ac_flags = 0x%x\n",
736 cmd.uapsd_ac_flags);
737 pos += scnprintf(buf+pos, bufsz-pos, "uapsd_max_sp = %d\n",
738 cmd.uapsd_max_sp);
739 pos += scnprintf(buf+pos, bufsz-pos, "heavy_tx_thld_packets = %d\n",
740 cmd.heavy_tx_thld_packets);
741 pos += scnprintf(buf+pos, bufsz-pos, "heavy_rx_thld_packets = %d\n",
742 cmd.heavy_rx_thld_packets);
743 pos += scnprintf(buf+pos, bufsz-pos, "heavy_tx_thld_percentage = %d\n",
744 cmd.heavy_tx_thld_percentage);
745 pos += scnprintf(buf+pos, bufsz-pos, "heavy_rx_thld_percentage = %d\n",
746 cmd.heavy_rx_thld_percentage);
747 pos += scnprintf(buf+pos, bufsz-pos, "uapsd_misbehaving_enable = %d\n",
748 (cmd.flags &
749 cpu_to_le16(POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK)) ?
750 1 : 0);
751
752 if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)))
753 return pos;
754
755 pos += scnprintf(buf+pos, bufsz-pos, "snooze_interval = %d\n",
756 cmd.snooze_interval);
757 pos += scnprintf(buf+pos, bufsz-pos, "snooze_window = %d\n",
758 cmd.snooze_window);
759
Alexander Bondare811ada2013-03-10 15:29:44 +0200760 return pos;
761}
762
Alexander Bondarb571a692013-05-21 14:49:09 +0300763void
764iwl_mvm_beacon_filter_debugfs_parameters(struct ieee80211_vif *vif,
765 struct iwl_beacon_filter_cmd *cmd)
766{
767 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
768 struct iwl_dbgfs_bf *dbgfs_bf = &mvmvif->dbgfs_bf;
769
770 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ENERGY_DELTA)
Hila Gonen5dca7c22013-07-16 11:15:35 +0300771 cmd->bf_energy_delta = cpu_to_le32(dbgfs_bf->bf_energy_delta);
Alexander Bondarb571a692013-05-21 14:49:09 +0300772 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ROAMING_ENERGY_DELTA)
773 cmd->bf_roaming_energy_delta =
Hila Gonen5dca7c22013-07-16 11:15:35 +0300774 cpu_to_le32(dbgfs_bf->bf_roaming_energy_delta);
Alexander Bondarb571a692013-05-21 14:49:09 +0300775 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ROAMING_STATE)
Hila Gonen5dca7c22013-07-16 11:15:35 +0300776 cmd->bf_roaming_state = cpu_to_le32(dbgfs_bf->bf_roaming_state);
777 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_THRESHOLD)
778 cmd->bf_temp_threshold =
779 cpu_to_le32(dbgfs_bf->bf_temp_threshold);
780 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_FAST_FILTER)
781 cmd->bf_temp_fast_filter =
782 cpu_to_le32(dbgfs_bf->bf_temp_fast_filter);
783 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_SLOW_FILTER)
784 cmd->bf_temp_slow_filter =
785 cpu_to_le32(dbgfs_bf->bf_temp_slow_filter);
Alexander Bondarb571a692013-05-21 14:49:09 +0300786 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_DEBUG_FLAG)
Hila Gonen5dca7c22013-07-16 11:15:35 +0300787 cmd->bf_debug_flag = cpu_to_le32(dbgfs_bf->bf_debug_flag);
Alexander Bondarb571a692013-05-21 14:49:09 +0300788 if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ESCAPE_TIMER)
789 cmd->bf_escape_timer = cpu_to_le32(dbgfs_bf->bf_escape_timer);
790 if (dbgfs_bf->mask & MVM_DEBUGFS_BA_ESCAPE_TIMER)
791 cmd->ba_escape_timer = cpu_to_le32(dbgfs_bf->ba_escape_timer);
792 if (dbgfs_bf->mask & MVM_DEBUGFS_BA_ENABLE_BEACON_ABORT)
Hila Gonen5dca7c22013-07-16 11:15:35 +0300793 cmd->ba_enable_beacon_abort =
794 cpu_to_le32(dbgfs_bf->ba_enable_beacon_abort);
Alexander Bondarb571a692013-05-21 14:49:09 +0300795}
796#endif
797
Eliad Peller3dd37d02014-01-07 14:00:24 +0200798static int _iwl_mvm_enable_beacon_filter(struct iwl_mvm *mvm,
799 struct ieee80211_vif *vif,
800 struct iwl_beacon_filter_cmd *cmd,
801 u32 cmd_flags,
802 bool d0i3)
Hila Gonen7df15b12012-12-12 11:16:19 +0200803{
804 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Alexander Bondar071d49902013-05-06 13:03:59 +0300805 int ret;
Hila Gonen7df15b12012-12-12 11:16:19 +0200806
Avri Altmanfa7b2e72014-05-20 08:03:24 +0300807 if (mvmvif != mvm->bf_allowed_vif || !vif->bss_conf.dtim_period ||
Hila Gonen7df15b12012-12-12 11:16:19 +0200808 vif->type != NL80211_IFTYPE_STATION || vif->p2p)
809 return 0;
810
Gregory Greenmancdc306b2015-07-20 12:55:34 +0300811 iwl_mvm_beacon_filter_set_cqm_params(mvm, vif, cmd, d0i3);
Eliad Peller3dd37d02014-01-07 14:00:24 +0200812 if (!d0i3)
813 iwl_mvm_beacon_filter_debugfs_parameters(vif, cmd);
814 ret = iwl_mvm_beacon_filter_send_cmd(mvm, cmd, cmd_flags);
Alexander Bondar071d49902013-05-06 13:03:59 +0300815
Eliad Peller3dd37d02014-01-07 14:00:24 +0200816 /* don't change bf_enabled in case of temporary d0i3 configuration */
817 if (!ret && !d0i3)
Andrei Otcheretianskia20fd392013-07-21 17:23:59 +0300818 mvmvif->bf_data.bf_enabled = true;
Alexander Bondar071d49902013-05-06 13:03:59 +0300819
820 return ret;
Hila Gonen7df15b12012-12-12 11:16:19 +0200821}
822
Eliad Peller3dd37d02014-01-07 14:00:24 +0200823int iwl_mvm_enable_beacon_filter(struct iwl_mvm *mvm,
824 struct ieee80211_vif *vif,
825 u32 flags)
826{
827 struct iwl_beacon_filter_cmd cmd = {
828 IWL_BF_CMD_CONFIG_DEFAULTS,
829 .bf_enable_beacon_filter = cpu_to_le32(1),
830 };
831
832 return _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd, flags, false);
833}
834
Emmanuel Grumbach13b72322014-05-20 11:28:18 +0300835static int iwl_mvm_update_beacon_abort(struct iwl_mvm *mvm,
836 struct ieee80211_vif *vif,
837 bool enable)
Avri Altmanfa7b2e72014-05-20 08:03:24 +0300838{
839 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
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 if (!mvmvif->bf_data.bf_enabled)
846 return 0;
847
848 if (mvm->cur_ucode == IWL_UCODE_WOWLAN)
849 cmd.ba_escape_timer = cpu_to_le32(IWL_BA_ESCAPE_TIMER_D3);
850
851 mvmvif->bf_data.ba_enabled = enable;
852 return _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd, 0, false);
853}
854
Hila Gonen7df15b12012-12-12 11:16:19 +0200855int iwl_mvm_disable_beacon_filter(struct iwl_mvm *mvm,
Eliad Peller3dd37d02014-01-07 14:00:24 +0200856 struct ieee80211_vif *vif,
857 u32 flags)
Hila Gonen7df15b12012-12-12 11:16:19 +0200858{
Alexander Bondar071d49902013-05-06 13:03:59 +0300859 struct iwl_beacon_filter_cmd cmd = {};
860 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
861 int ret;
Hila Gonen7df15b12012-12-12 11:16:19 +0200862
Emmanuel Grumbach73e5f2c2014-03-30 08:57:30 +0300863 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
Hila Gonen7df15b12012-12-12 11:16:19 +0200864 return 0;
865
Eliad Peller3dd37d02014-01-07 14:00:24 +0200866 ret = iwl_mvm_beacon_filter_send_cmd(mvm, &cmd, flags);
Alexander Bondar071d49902013-05-06 13:03:59 +0300867
868 if (!ret)
Andrei Otcheretianskia20fd392013-07-21 17:23:59 +0300869 mvmvif->bf_data.bf_enabled = false;
Alexander Bondar071d49902013-05-06 13:03:59 +0300870
871 return ret;
Hila Gonen7df15b12012-12-12 11:16:19 +0200872}
Alexander Bondare811ada2013-03-10 15:29:44 +0200873
Luciano Coelho2533edc2014-08-08 19:50:46 +0300874static int iwl_mvm_power_set_ps(struct iwl_mvm *mvm)
Emmanuel Grumbach13b72322014-05-20 11:28:18 +0300875{
Luciano Coelho128aa942014-08-08 18:36:22 +0300876 bool disable_ps;
Emmanuel Grumbach13b72322014-05-20 11:28:18 +0300877 int ret;
878
Emmanuel Grumbach13b72322014-05-20 11:28:18 +0300879 /* disable PS if CAM */
Luciano Coelhob1873302014-08-08 17:12:07 +0300880 disable_ps = (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM);
Luciano Coelho2533edc2014-08-08 19:50:46 +0300881 /* ...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);
Luciano Coelhob1873302014-08-08 17:12:07 +0300886
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;
Emmanuel Grumbach13b72322014-05-20 11:28:18 +0300896 }
897 }
898
Luciano Coelho128aa942014-08-08 18:36:22 +0300899 return 0;
900}
901
Luciano Coelhoef9203d2014-08-08 19:18:35 +0300902static int iwl_mvm_power_set_ba(struct iwl_mvm *mvm,
903 struct iwl_power_vifs *vifs)
Luciano Coelho128aa942014-08-08 18:36:22 +0300904{
905 struct iwl_mvm_vif *mvmvif;
Luciano Coelhoef9203d2014-08-08 19:18:35 +0300906 bool ba_enable;
907
908 if (!vifs->bf_vif)
909 return 0;
910
911 mvmvif = iwl_mvm_vif_from_mac80211(vifs->bf_vif);
912
913 ba_enable = !(!mvmvif->pm_enabled || mvm->ps_disabled ||
914 !vifs->bf_vif->bss_conf.ps ||
915 iwl_mvm_vif_low_latency(mvmvif));
916
917 return iwl_mvm_update_beacon_abort(mvm, vifs->bf_vif, ba_enable);
918}
919
920int iwl_mvm_power_update_ps(struct iwl_mvm *mvm)
921{
Luciano Coelho128aa942014-08-08 18:36:22 +0300922 struct iwl_power_vifs vifs = {
923 .mvm = mvm,
924 };
Luciano Coelhoef9203d2014-08-08 19:18:35 +0300925 int ret;
926
927 lockdep_assert_held(&mvm->mutex);
928
929 /* get vifs info */
930 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
931 IEEE80211_IFACE_ITER_NORMAL,
932 iwl_mvm_power_get_vifs_iterator, &vifs);
933
Luciano Coelho2533edc2014-08-08 19:50:46 +0300934 ret = iwl_mvm_power_set_ps(mvm);
Luciano Coelhoef9203d2014-08-08 19:18:35 +0300935 if (ret)
936 return ret;
937
938 return iwl_mvm_power_set_ba(mvm, &vifs);
939}
940
941int iwl_mvm_power_update_mac(struct iwl_mvm *mvm)
942{
943 struct iwl_power_vifs vifs = {
944 .mvm = mvm,
945 };
Luciano Coelho128aa942014-08-08 18:36:22 +0300946 int ret;
947
948 lockdep_assert_held(&mvm->mutex);
949
950 /* get vifs info */
951 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
952 IEEE80211_IFACE_ITER_NORMAL,
953 iwl_mvm_power_get_vifs_iterator, &vifs);
954
955 iwl_mvm_power_set_pm(mvm, &vifs);
956
Luciano Coelho2533edc2014-08-08 19:50:46 +0300957 ret = iwl_mvm_power_set_ps(mvm);
Luciano Coelho128aa942014-08-08 18:36:22 +0300958 if (ret)
959 return ret;
960
Emmanuel Grumbach13b72322014-05-20 11:28:18 +0300961 if (vifs.bss_vif) {
962 ret = iwl_mvm_power_send_cmd(mvm, vifs.bss_vif);
963 if (ret)
964 return ret;
965 }
966
967 if (vifs.p2p_vif) {
968 ret = iwl_mvm_power_send_cmd(mvm, vifs.p2p_vif);
969 if (ret)
970 return ret;
971 }
972
Luciano Coelhoef9203d2014-08-08 19:18:35 +0300973 return iwl_mvm_power_set_ba(mvm, &vifs);
Emmanuel Grumbach13b72322014-05-20 11:28:18 +0300974}
975
Eliad Peller3dd37d02014-01-07 14:00:24 +0200976int iwl_mvm_update_d0i3_power_mode(struct iwl_mvm *mvm,
977 struct ieee80211_vif *vif,
978 bool enable, u32 flags)
979{
980 int ret;
981 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
982 struct iwl_mac_power_cmd cmd = {};
983
984 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
985 return 0;
986
987 if (!vif->bss_conf.assoc)
988 return 0;
989
990 iwl_mvm_power_build_cmd(mvm, vif, &cmd);
991 if (enable) {
Avri Altman49c6d802014-08-27 19:58:56 +0300992 /* configure skip over dtim up to 306TU - 314 msec */
Emmanuel Grumbach717e2392014-07-31 14:39:40 +0300993 int dtimper = vif->bss_conf.dtim_period ?: 1;
Avri Altman49c6d802014-08-27 19:58:56 +0300994 int dtimper_tu = dtimper * vif->bss_conf.beacon_int;
995 bool radar_detect = iwl_mvm_power_is_radar(vif);
Eliad Peller3dd37d02014-01-07 14:00:24 +0200996
Avri Altman49c6d802014-08-27 19:58:56 +0300997 if (WARN_ON(!dtimper_tu))
Eliad Peller3dd37d02014-01-07 14:00:24 +0200998 return 0;
999
Avri Altman49c6d802014-08-27 19:58:56 +03001000 /* Check skip over DTIM conditions */
1001 /* TODO: check that multicast wake lock is off */
1002 if (!radar_detect && (dtimper < 10)) {
1003 cmd.skip_dtim_periods = 306 / dtimper_tu;
1004 if (cmd.skip_dtim_periods)
1005 cmd.flags |= cpu_to_le16(
1006 POWER_FLAGS_SKIP_OVER_DTIM_MSK);
1007 }
Eliad Peller3dd37d02014-01-07 14:00:24 +02001008 }
1009 iwl_mvm_power_log(mvm, &cmd);
Emmanuel Grumbach474b50c2014-01-28 09:13:04 +02001010#ifdef CONFIG_IWLWIFI_DEBUGFS
1011 memcpy(&mvmvif->mac_pwr_cmd, &cmd, sizeof(cmd));
1012#endif
Eliad Peller3dd37d02014-01-07 14:00:24 +02001013 ret = iwl_mvm_send_cmd_pdu(mvm, MAC_PM_POWER_TABLE, flags,
1014 sizeof(cmd), &cmd);
1015 if (ret)
1016 return ret;
1017
1018 /* configure beacon filtering */
1019 if (mvmvif != mvm->bf_allowed_vif)
1020 return 0;
1021
1022 if (enable) {
1023 struct iwl_beacon_filter_cmd cmd_bf = {
1024 IWL_BF_CMD_CONFIG_D0I3,
1025 .bf_enable_beacon_filter = cpu_to_le32(1),
1026 };
1027 ret = _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd_bf,
1028 flags, true);
1029 } else {
1030 if (mvmvif->bf_data.bf_enabled)
1031 ret = iwl_mvm_enable_beacon_filter(mvm, vif, flags);
1032 else
1033 ret = iwl_mvm_disable_beacon_filter(mvm, vif, flags);
1034 }
1035
1036 return ret;
1037}