ab92a634aded47ab272c4b2b9e801f246317d888
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / iwlwifi / mvm / power.c
1 /******************************************************************************
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  *
8  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22  * USA
23  *
24  * The full GNU General Public License is included in this distribution
25  * in the file called COPYING.
26  *
27  * Contact Information:
28  *  Intel Linux Wireless <ilw@linux.intel.com>
29  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30  *
31  * BSD LICENSE
32  *
33  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  *
40  *  * Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  *  * Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in
44  *    the documentation and/or other materials provided with the
45  *    distribution.
46  *  * Neither the name Intel Corporation nor the names of its
47  *    contributors may be used to endorse or promote products derived
48  *    from this software without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61  *
62  *****************************************************************************/
63
64 #include <linux/kernel.h>
65 #include <linux/module.h>
66 #include <linux/slab.h>
67
68 #include <net/mac80211.h>
69
70 #include "iwl-debug.h"
71 #include "mvm.h"
72 #include "iwl-modparams.h"
73 #include "fw-api-power.h"
74
75 #define POWER_KEEP_ALIVE_PERIOD_SEC    25
76
77 static
78 int iwl_mvm_beacon_filter_send_cmd(struct iwl_mvm *mvm,
79                                    struct iwl_beacon_filter_cmd *cmd,
80                                    u32 flags)
81 {
82         IWL_DEBUG_POWER(mvm, "ba_enable_beacon_abort is: %d\n",
83                         le32_to_cpu(cmd->ba_enable_beacon_abort));
84         IWL_DEBUG_POWER(mvm, "ba_escape_timer is: %d\n",
85                         le32_to_cpu(cmd->ba_escape_timer));
86         IWL_DEBUG_POWER(mvm, "bf_debug_flag is: %d\n",
87                         le32_to_cpu(cmd->bf_debug_flag));
88         IWL_DEBUG_POWER(mvm, "bf_enable_beacon_filter is: %d\n",
89                         le32_to_cpu(cmd->bf_enable_beacon_filter));
90         IWL_DEBUG_POWER(mvm, "bf_energy_delta is: %d\n",
91                         le32_to_cpu(cmd->bf_energy_delta));
92         IWL_DEBUG_POWER(mvm, "bf_escape_timer is: %d\n",
93                         le32_to_cpu(cmd->bf_escape_timer));
94         IWL_DEBUG_POWER(mvm, "bf_roaming_energy_delta is: %d\n",
95                         le32_to_cpu(cmd->bf_roaming_energy_delta));
96         IWL_DEBUG_POWER(mvm, "bf_roaming_state is: %d\n",
97                         le32_to_cpu(cmd->bf_roaming_state));
98         IWL_DEBUG_POWER(mvm, "bf_temp_threshold is: %d\n",
99                         le32_to_cpu(cmd->bf_temp_threshold));
100         IWL_DEBUG_POWER(mvm, "bf_temp_fast_filter is: %d\n",
101                         le32_to_cpu(cmd->bf_temp_fast_filter));
102         IWL_DEBUG_POWER(mvm, "bf_temp_slow_filter is: %d\n",
103                         le32_to_cpu(cmd->bf_temp_slow_filter));
104
105         return iwl_mvm_send_cmd_pdu(mvm, REPLY_BEACON_FILTERING_CMD, flags,
106                                     sizeof(struct iwl_beacon_filter_cmd), cmd);
107 }
108
109 static
110 void iwl_mvm_beacon_filter_set_cqm_params(struct iwl_mvm *mvm,
111                                           struct ieee80211_vif *vif,
112                                           struct iwl_beacon_filter_cmd *cmd)
113 {
114         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
115
116         if (vif->bss_conf.cqm_rssi_thold) {
117                 cmd->bf_energy_delta =
118                         cpu_to_le32(vif->bss_conf.cqm_rssi_hyst);
119                 /* fw uses an absolute value for this */
120                 cmd->bf_roaming_state =
121                         cpu_to_le32(-vif->bss_conf.cqm_rssi_thold);
122         }
123         cmd->ba_enable_beacon_abort = cpu_to_le32(mvmvif->bf_data.ba_enabled);
124 }
125
126 int iwl_mvm_update_beacon_abort(struct iwl_mvm *mvm,
127                                 struct ieee80211_vif *vif, bool enable)
128 {
129         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
130         struct iwl_beacon_filter_cmd cmd = {
131                 IWL_BF_CMD_CONFIG_DEFAULTS,
132                 .bf_enable_beacon_filter = cpu_to_le32(1),
133                 .ba_enable_beacon_abort = cpu_to_le32(enable),
134         };
135
136         if (!mvmvif->bf_data.bf_enabled)
137                 return 0;
138
139         if (mvm->cur_ucode == IWL_UCODE_WOWLAN)
140                 cmd.ba_escape_timer = cpu_to_le32(IWL_BA_ESCAPE_TIMER_D3);
141
142         mvmvif->bf_data.ba_enabled = enable;
143         iwl_mvm_beacon_filter_set_cqm_params(mvm, vif, &cmd);
144         iwl_mvm_beacon_filter_debugfs_parameters(vif, &cmd);
145         return iwl_mvm_beacon_filter_send_cmd(mvm, &cmd, CMD_SYNC);
146 }
147
148 static void iwl_mvm_power_log(struct iwl_mvm *mvm,
149                               struct iwl_mac_power_cmd *cmd)
150 {
151         IWL_DEBUG_POWER(mvm,
152                         "Sending power table command on mac id 0x%X for power level %d, flags = 0x%X\n",
153                         cmd->id_and_color, iwlmvm_mod_params.power_scheme,
154                         le16_to_cpu(cmd->flags));
155         IWL_DEBUG_POWER(mvm, "Keep alive = %u sec\n",
156                         le16_to_cpu(cmd->keep_alive_seconds));
157
158         if (!(cmd->flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK))) {
159                 IWL_DEBUG_POWER(mvm, "Disable power management\n");
160                 return;
161         }
162
163         IWL_DEBUG_POWER(mvm, "Rx timeout = %u usec\n",
164                         le32_to_cpu(cmd->rx_data_timeout));
165         IWL_DEBUG_POWER(mvm, "Tx timeout = %u usec\n",
166                         le32_to_cpu(cmd->tx_data_timeout));
167         if (cmd->flags & cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK))
168                 IWL_DEBUG_POWER(mvm, "DTIM periods to skip = %u\n",
169                                 cmd->skip_dtim_periods);
170         if (cmd->flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
171                 IWL_DEBUG_POWER(mvm, "LP RX RSSI threshold = %u\n",
172                                 cmd->lprx_rssi_threshold);
173         if (cmd->flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK)) {
174                 IWL_DEBUG_POWER(mvm, "uAPSD enabled\n");
175                 IWL_DEBUG_POWER(mvm, "Rx timeout (uAPSD) = %u usec\n",
176                                 le32_to_cpu(cmd->rx_data_timeout_uapsd));
177                 IWL_DEBUG_POWER(mvm, "Tx timeout (uAPSD) = %u usec\n",
178                                 le32_to_cpu(cmd->tx_data_timeout_uapsd));
179                 IWL_DEBUG_POWER(mvm, "QNDP TID = %d\n", cmd->qndp_tid);
180                 IWL_DEBUG_POWER(mvm, "ACs flags = 0x%x\n", cmd->uapsd_ac_flags);
181                 IWL_DEBUG_POWER(mvm, "Max SP = %d\n", cmd->uapsd_max_sp);
182         }
183 }
184
185 static void iwl_mvm_power_configure_uapsd(struct iwl_mvm *mvm,
186                                           struct ieee80211_vif *vif,
187                                           struct iwl_mac_power_cmd *cmd)
188 {
189         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
190         enum ieee80211_ac_numbers ac;
191         bool tid_found = false;
192
193         for (ac = IEEE80211_AC_VO; ac <= IEEE80211_AC_BK; ac++) {
194                 if (!mvmvif->queue_params[ac].uapsd)
195                         continue;
196
197                 if (mvm->cur_ucode != IWL_UCODE_WOWLAN)
198                         cmd->flags |=
199                                 cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK);
200
201                 cmd->uapsd_ac_flags |= BIT(ac);
202
203                 /* QNDP TID - the highest TID with no admission control */
204                 if (!tid_found && !mvmvif->queue_params[ac].acm) {
205                         tid_found = true;
206                         switch (ac) {
207                         case IEEE80211_AC_VO:
208                                 cmd->qndp_tid = 6;
209                                 break;
210                         case IEEE80211_AC_VI:
211                                 cmd->qndp_tid = 5;
212                                 break;
213                         case IEEE80211_AC_BE:
214                                 cmd->qndp_tid = 0;
215                                 break;
216                         case IEEE80211_AC_BK:
217                                 cmd->qndp_tid = 1;
218                                 break;
219                         }
220                 }
221         }
222
223         if (!(cmd->flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK)))
224                 return;
225
226         cmd->flags |= cpu_to_le16(POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK);
227
228         if (cmd->uapsd_ac_flags == (BIT(IEEE80211_AC_VO) |
229                                     BIT(IEEE80211_AC_VI) |
230                                     BIT(IEEE80211_AC_BE) |
231                                     BIT(IEEE80211_AC_BK))) {
232                 cmd->flags |= cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK);
233                 cmd->snooze_interval = cpu_to_le16(IWL_MVM_PS_SNOOZE_INTERVAL);
234                 cmd->snooze_window = (mvm->cur_ucode == IWL_UCODE_WOWLAN) ?
235                         cpu_to_le16(IWL_MVM_WOWLAN_PS_SNOOZE_WINDOW) :
236                         cpu_to_le16(IWL_MVM_PS_SNOOZE_WINDOW);
237         }
238
239         cmd->uapsd_max_sp = IWL_UAPSD_MAX_SP;
240
241         if (mvm->cur_ucode == IWL_UCODE_WOWLAN || cmd->flags &
242             cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)) {
243                 cmd->rx_data_timeout_uapsd =
244                         cpu_to_le32(IWL_MVM_WOWLAN_PS_RX_DATA_TIMEOUT);
245                 cmd->tx_data_timeout_uapsd =
246                         cpu_to_le32(IWL_MVM_WOWLAN_PS_TX_DATA_TIMEOUT);
247         } else {
248                 cmd->rx_data_timeout_uapsd =
249                         cpu_to_le32(IWL_MVM_UAPSD_RX_DATA_TIMEOUT);
250                 cmd->tx_data_timeout_uapsd =
251                         cpu_to_le32(IWL_MVM_UAPSD_TX_DATA_TIMEOUT);
252         }
253
254         if (cmd->flags & cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)) {
255                 cmd->heavy_tx_thld_packets =
256                         IWL_MVM_PS_SNOOZE_HEAVY_TX_THLD_PACKETS;
257                 cmd->heavy_rx_thld_packets =
258                         IWL_MVM_PS_SNOOZE_HEAVY_RX_THLD_PACKETS;
259         } else {
260                 cmd->heavy_tx_thld_packets =
261                         IWL_MVM_PS_HEAVY_TX_THLD_PACKETS;
262                 cmd->heavy_rx_thld_packets =
263                         IWL_MVM_PS_HEAVY_RX_THLD_PACKETS;
264         }
265         cmd->heavy_tx_thld_percentage =
266                 IWL_MVM_PS_HEAVY_TX_THLD_PERCENT;
267         cmd->heavy_rx_thld_percentage =
268                 IWL_MVM_PS_HEAVY_RX_THLD_PERCENT;
269 }
270
271 static void iwl_mvm_power_build_cmd(struct iwl_mvm *mvm,
272                                     struct ieee80211_vif *vif,
273                                     struct iwl_mac_power_cmd *cmd)
274 {
275         struct ieee80211_hw *hw = mvm->hw;
276         struct ieee80211_chanctx_conf *chanctx_conf;
277         struct ieee80211_channel *chan;
278         int dtimper, dtimper_msec;
279         int keep_alive;
280         bool radar_detect = false;
281         struct iwl_mvm_vif *mvmvif __maybe_unused =
282                 iwl_mvm_vif_from_mac80211(vif);
283         bool allow_uapsd = true;
284
285         cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
286                                                             mvmvif->color));
287         dtimper = hw->conf.ps_dtim_period ?: 1;
288
289         /*
290          * Regardless of power management state the driver must set
291          * keep alive period. FW will use it for sending keep alive NDPs
292          * immediately after association. Check that keep alive period
293          * is at least 3 * DTIM
294          */
295         dtimper_msec = dtimper * vif->bss_conf.beacon_int;
296         keep_alive = max_t(int, 3 * dtimper_msec,
297                            MSEC_PER_SEC * POWER_KEEP_ALIVE_PERIOD_SEC);
298         keep_alive = DIV_ROUND_UP(keep_alive, MSEC_PER_SEC);
299         cmd->keep_alive_seconds = cpu_to_le16(keep_alive);
300
301         if (mvm->ps_disabled)
302                 return;
303
304         cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK);
305
306 #ifdef CONFIG_IWLWIFI_DEBUGFS
307         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_DISABLE_POWER_OFF &&
308             mvmvif->dbgfs_pm.disable_power_off)
309                 cmd->flags &= cpu_to_le16(~POWER_FLAGS_POWER_SAVE_ENA_MSK);
310 #endif
311         if (!vif->bss_conf.ps || iwl_mvm_vif_low_latency(mvmvif) ||
312             !mvmvif->pm_enabled)
313                 return;
314
315         cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK);
316
317         if (vif->bss_conf.beacon_rate &&
318             (vif->bss_conf.beacon_rate->bitrate == 10 ||
319              vif->bss_conf.beacon_rate->bitrate == 60)) {
320                 cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
321                 cmd->lprx_rssi_threshold = POWER_LPRX_RSSI_THRESHOLD;
322         }
323
324         /* Check if radar detection is required on current channel */
325         rcu_read_lock();
326         chanctx_conf = rcu_dereference(vif->chanctx_conf);
327         WARN_ON(!chanctx_conf);
328         if (chanctx_conf) {
329                 chan = chanctx_conf->def.chan;
330                 radar_detect = chan->flags & IEEE80211_CHAN_RADAR;
331         }
332         rcu_read_unlock();
333
334         /* Check skip over DTIM conditions */
335         if (!radar_detect && (dtimper <= 10) &&
336             (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_LP ||
337              mvm->cur_ucode == IWL_UCODE_WOWLAN)) {
338                 cmd->flags |= cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
339                 cmd->skip_dtim_periods = 3;
340         }
341
342         if (mvm->cur_ucode != IWL_UCODE_WOWLAN) {
343                 cmd->rx_data_timeout =
344                         cpu_to_le32(IWL_MVM_DEFAULT_PS_RX_DATA_TIMEOUT);
345                 cmd->tx_data_timeout =
346                         cpu_to_le32(IWL_MVM_DEFAULT_PS_TX_DATA_TIMEOUT);
347         } else {
348                 cmd->rx_data_timeout =
349                         cpu_to_le32(IWL_MVM_WOWLAN_PS_RX_DATA_TIMEOUT);
350                 cmd->tx_data_timeout =
351                         cpu_to_le32(IWL_MVM_WOWLAN_PS_TX_DATA_TIMEOUT);
352         }
353
354         if (!memcmp(mvmvif->uapsd_misbehaving_bssid, vif->bss_conf.bssid,
355                     ETH_ALEN))
356                 allow_uapsd = false;
357
358         if (vif->p2p &&
359             !(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_P2P_PS_UAPSD))
360                 allow_uapsd = false;
361         /*
362          * Avoid using uAPSD if P2P client is associated to GO that uses
363          * opportunistic power save. This is due to current FW limitation.
364          */
365         if (vif->p2p &&
366             vif->bss_conf.p2p_noa_attr.oppps_ctwindow &
367             IEEE80211_P2P_OPPPS_ENABLE_BIT)
368                 allow_uapsd = false;
369
370         if (allow_uapsd)
371                 iwl_mvm_power_configure_uapsd(mvm, vif, cmd);
372
373 #ifdef CONFIG_IWLWIFI_DEBUGFS
374         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_KEEP_ALIVE)
375                 cmd->keep_alive_seconds =
376                         cpu_to_le16(mvmvif->dbgfs_pm.keep_alive_seconds);
377         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_OVER_DTIM) {
378                 if (mvmvif->dbgfs_pm.skip_over_dtim)
379                         cmd->flags |=
380                                 cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
381                 else
382                         cmd->flags &=
383                                 cpu_to_le16(~POWER_FLAGS_SKIP_OVER_DTIM_MSK);
384         }
385         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_RX_DATA_TIMEOUT)
386                 cmd->rx_data_timeout =
387                         cpu_to_le32(mvmvif->dbgfs_pm.rx_data_timeout);
388         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_TX_DATA_TIMEOUT)
389                 cmd->tx_data_timeout =
390                         cpu_to_le32(mvmvif->dbgfs_pm.tx_data_timeout);
391         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_DTIM_PERIODS)
392                 cmd->skip_dtim_periods = mvmvif->dbgfs_pm.skip_dtim_periods;
393         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_ENA) {
394                 if (mvmvif->dbgfs_pm.lprx_ena)
395                         cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
396                 else
397                         cmd->flags &= cpu_to_le16(~POWER_FLAGS_LPRX_ENA_MSK);
398         }
399         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_RSSI_THRESHOLD)
400                 cmd->lprx_rssi_threshold = mvmvif->dbgfs_pm.lprx_rssi_threshold;
401         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SNOOZE_ENABLE) {
402                 if (mvmvif->dbgfs_pm.snooze_ena)
403                         cmd->flags |=
404                                 cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK);
405                 else
406                         cmd->flags &=
407                                 cpu_to_le16(~POWER_FLAGS_SNOOZE_ENA_MSK);
408         }
409         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_UAPSD_MISBEHAVING) {
410                 u16 flag = POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK;
411                 if (mvmvif->dbgfs_pm.uapsd_misbehaving)
412                         cmd->flags |= cpu_to_le16(flag);
413                 else
414                         cmd->flags &= cpu_to_le16(flag);
415         }
416 #endif /* CONFIG_IWLWIFI_DEBUGFS */
417 }
418
419 static int iwl_mvm_power_send_cmd(struct iwl_mvm *mvm,
420                                          struct ieee80211_vif *vif)
421 {
422         struct iwl_mac_power_cmd cmd = {};
423
424         iwl_mvm_power_build_cmd(mvm, vif, &cmd);
425         iwl_mvm_power_log(mvm, &cmd);
426 #ifdef CONFIG_IWLWIFI_DEBUGFS
427         memcpy(&iwl_mvm_vif_from_mac80211(vif)->mac_pwr_cmd, &cmd, sizeof(cmd));
428 #endif
429
430         return iwl_mvm_send_cmd_pdu(mvm, MAC_PM_POWER_TABLE, CMD_SYNC,
431                                     sizeof(cmd), &cmd);
432 }
433
434 int iwl_mvm_power_update_device(struct iwl_mvm *mvm)
435 {
436         struct iwl_device_power_cmd cmd = {
437                 .flags = cpu_to_le16(DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK),
438         };
439
440         if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_DEVICE_PS_CMD))
441                 return 0;
442
443         if (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM)
444                 mvm->ps_disabled = true;
445
446         if (mvm->ps_disabled)
447                 cmd.flags |= cpu_to_le16(DEVICE_POWER_FLAGS_CAM_MSK);
448
449 #ifdef CONFIG_IWLWIFI_DEBUGFS
450         if ((mvm->cur_ucode == IWL_UCODE_WOWLAN) ? mvm->disable_power_off_d3 :
451             mvm->disable_power_off)
452                 cmd.flags &=
453                         cpu_to_le16(~DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK);
454 #endif
455         IWL_DEBUG_POWER(mvm,
456                         "Sending device power command with flags = 0x%X\n",
457                         cmd.flags);
458
459         return iwl_mvm_send_cmd_pdu(mvm, POWER_TABLE_CMD, CMD_SYNC, sizeof(cmd),
460                                     &cmd);
461 }
462
463 void iwl_mvm_power_vif_assoc(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
464 {
465         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
466
467         if (memcmp(vif->bss_conf.bssid, mvmvif->uapsd_misbehaving_bssid,
468                    ETH_ALEN))
469                 memset(mvmvif->uapsd_misbehaving_bssid, 0, ETH_ALEN);
470 }
471
472 static void iwl_mvm_power_uapsd_misbehav_ap_iterator(void *_data, u8 *mac,
473                                                      struct ieee80211_vif *vif)
474 {
475         u8 *ap_sta_id = _data;
476         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
477
478         /* The ap_sta_id is not expected to change during current association
479          * so no explicit protection is needed
480          */
481         if (mvmvif->ap_sta_id == *ap_sta_id)
482                 memcpy(mvmvif->uapsd_misbehaving_bssid, vif->bss_conf.bssid,
483                        ETH_ALEN);
484 }
485
486 int iwl_mvm_power_uapsd_misbehaving_ap_notif(struct iwl_mvm *mvm,
487                                              struct iwl_rx_cmd_buffer *rxb,
488                                              struct iwl_device_cmd *cmd)
489 {
490         struct iwl_rx_packet *pkt = rxb_addr(rxb);
491         struct iwl_uapsd_misbehaving_ap_notif *notif = (void *)pkt->data;
492         u8 ap_sta_id = le32_to_cpu(notif->sta_id);
493
494         ieee80211_iterate_active_interfaces_atomic(
495                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
496                 iwl_mvm_power_uapsd_misbehav_ap_iterator, &ap_sta_id);
497
498         return 0;
499 }
500
501 struct iwl_power_vifs {
502         struct ieee80211_vif *bf_vif;
503         struct ieee80211_vif *bss_vif;
504         struct ieee80211_vif *p2p_vif;
505         struct ieee80211_vif *ap_vif;
506         struct ieee80211_vif *monitor_vif;
507         bool p2p_active;
508         bool bss_active;
509         bool ap_active;
510         bool monitor_active;
511 };
512
513 static void iwl_mvm_power_iterator(void *_data, u8 *mac,
514                                    struct ieee80211_vif *vif)
515 {
516         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
517         struct iwl_power_vifs *power_iterator = _data;
518
519         mvmvif->pm_enabled = false;
520         switch (ieee80211_vif_type_p2p(vif)) {
521         case NL80211_IFTYPE_P2P_DEVICE:
522                 break;
523
524         case NL80211_IFTYPE_P2P_GO:
525         case NL80211_IFTYPE_AP:
526                 /* only a single MAC of the same type */
527                 WARN_ON(power_iterator->ap_vif);
528                 power_iterator->ap_vif = vif;
529                 if (mvmvif->phy_ctxt)
530                         if (mvmvif->phy_ctxt->id < MAX_PHYS)
531                                 power_iterator->ap_active = true;
532                 break;
533
534         case NL80211_IFTYPE_MONITOR:
535                 /* only a single MAC of the same type */
536                 WARN_ON(power_iterator->monitor_vif);
537                 power_iterator->monitor_vif = vif;
538                 if (mvmvif->phy_ctxt)
539                         if (mvmvif->phy_ctxt->id < MAX_PHYS)
540                                 power_iterator->monitor_active = true;
541                 break;
542
543         case NL80211_IFTYPE_P2P_CLIENT:
544                 /* only a single MAC of the same type */
545                 WARN_ON(power_iterator->p2p_vif);
546                 power_iterator->p2p_vif = vif;
547                 if (mvmvif->phy_ctxt)
548                         if (mvmvif->phy_ctxt->id < MAX_PHYS)
549                                 power_iterator->p2p_active = true;
550                 break;
551
552         case NL80211_IFTYPE_STATION:
553                 /* only a single MAC of the same type */
554                 WARN_ON(power_iterator->bss_vif);
555                 power_iterator->bss_vif = vif;
556                 if (mvmvif->phy_ctxt)
557                         if (mvmvif->phy_ctxt->id < MAX_PHYS)
558                                 power_iterator->bss_active = true;
559
560                 if (mvmvif->bf_data.bf_enabled &&
561                     !WARN_ON(power_iterator->bf_vif))
562                         power_iterator->bf_vif = vif;
563
564                 break;
565
566         default:
567                 break;
568         }
569 }
570
571 static void
572 iwl_mvm_power_set_pm(struct iwl_mvm *mvm,
573                                     struct iwl_power_vifs *vifs)
574 {
575         struct iwl_mvm_vif *bss_mvmvif = NULL;
576         struct iwl_mvm_vif *p2p_mvmvif = NULL;
577         struct iwl_mvm_vif *ap_mvmvif = NULL;
578         bool client_same_channel = false;
579         bool ap_same_channel = false;
580
581         lockdep_assert_held(&mvm->mutex);
582
583         /* get vifs info + set pm_enable to false */
584         ieee80211_iterate_active_interfaces_atomic(mvm->hw,
585                                             IEEE80211_IFACE_ITER_NORMAL,
586                                             iwl_mvm_power_iterator, vifs);
587
588         if (vifs->bss_vif)
589                 bss_mvmvif = iwl_mvm_vif_from_mac80211(vifs->bss_vif);
590
591         if (vifs->p2p_vif)
592                 p2p_mvmvif = iwl_mvm_vif_from_mac80211(vifs->p2p_vif);
593
594         if (vifs->ap_vif)
595                 ap_mvmvif = iwl_mvm_vif_from_mac80211(vifs->ap_vif);
596
597         /* enable PM on bss if bss stand alone */
598         if (vifs->bss_active && !vifs->p2p_active && !vifs->ap_active) {
599                 bss_mvmvif->pm_enabled = true;
600                 return;
601         }
602
603         /* enable PM on p2p if p2p stand alone */
604         if (vifs->p2p_active && !vifs->bss_active && !vifs->ap_active) {
605                 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_P2P_PM)
606                         p2p_mvmvif->pm_enabled = true;
607                 return;
608         }
609
610         if (vifs->bss_active && vifs->p2p_active)
611                 client_same_channel = (bss_mvmvif->phy_ctxt->id ==
612                                        p2p_mvmvif->phy_ctxt->id);
613         if (vifs->bss_active && vifs->ap_active)
614                 ap_same_channel = (bss_mvmvif->phy_ctxt->id ==
615                                    ap_mvmvif->phy_ctxt->id);
616
617         /* bss is not stand alone: enable PM if alone on its channel */
618         if (vifs->bss_active && !(client_same_channel || ap_same_channel) &&
619             (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BSS_P2P_PS_DCM)) {
620                         bss_mvmvif->pm_enabled = true;
621                         return;
622         }
623
624         /*
625          * There is only one channel in the system and there are only
626          * bss and p2p clients that share it
627          */
628         if (client_same_channel && !vifs->ap_active &&
629             (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BSS_P2P_PS_SCM)) {
630                 /* share same channel*/
631                 bss_mvmvif->pm_enabled = true;
632                 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_P2P_PM)
633                         p2p_mvmvif->pm_enabled = true;
634         }
635 }
636
637 int iwl_mvm_power_update_mac(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
638 {
639         struct iwl_mvm_vif *mvmvif;
640         struct iwl_power_vifs vifs = {};
641         bool ba_enable;
642         int ret;
643
644         lockdep_assert_held(&mvm->mutex);
645
646         iwl_mvm_power_set_pm(mvm, &vifs);
647
648         /* disable PS if CAM */
649         if (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM) {
650                 mvm->ps_disabled = true;
651         } else {
652         /* don't update device power state unless we add / remove monitor */
653                 if (vifs.monitor_vif) {
654                         if (vifs.monitor_active)
655                                 mvm->ps_disabled = true;
656                         ret = iwl_mvm_power_update_device(mvm);
657                         if (ret)
658                                 return ret;
659                 }
660         }
661
662         if (vifs.bss_vif) {
663                 ret = iwl_mvm_power_send_cmd(mvm, vifs.bss_vif);
664                 if (ret)
665                         return ret;
666         }
667
668         if (vifs.p2p_vif) {
669                 ret = iwl_mvm_power_send_cmd(mvm, vifs.p2p_vif);
670                 if (ret)
671                         return ret;
672         }
673
674         if (!vifs.bf_vif)
675                 return 0;
676
677         vif = vifs.bf_vif;
678         mvmvif = iwl_mvm_vif_from_mac80211(vif);
679
680         ba_enable = !(!mvmvif->pm_enabled || mvm->ps_disabled ||
681                       !vif->bss_conf.ps || iwl_mvm_vif_low_latency(mvmvif));
682
683         return iwl_mvm_update_beacon_abort(mvm, vifs.bf_vif, ba_enable);
684 }
685
686 #ifdef CONFIG_IWLWIFI_DEBUGFS
687 int iwl_mvm_power_mac_dbgfs_read(struct iwl_mvm *mvm,
688                                  struct ieee80211_vif *vif, char *buf,
689                                  int bufsz)
690 {
691         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
692         struct iwl_mac_power_cmd cmd = {};
693         int pos = 0;
694
695         mutex_lock(&mvm->mutex);
696         memcpy(&cmd, &mvmvif->mac_pwr_cmd, sizeof(cmd));
697         mutex_unlock(&mvm->mutex);
698
699         if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_DEVICE_PS_CMD))
700                 pos += scnprintf(buf+pos, bufsz-pos, "disable_power_off = %d\n",
701                                  (cmd.flags &
702                                  cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK)) ?
703                                  0 : 1);
704         pos += scnprintf(buf+pos, bufsz-pos, "power_scheme = %d\n",
705                          iwlmvm_mod_params.power_scheme);
706         pos += scnprintf(buf+pos, bufsz-pos, "flags = 0x%x\n",
707                          le16_to_cpu(cmd.flags));
708         pos += scnprintf(buf+pos, bufsz-pos, "keep_alive = %d\n",
709                          le16_to_cpu(cmd.keep_alive_seconds));
710
711         if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK)))
712                 return pos;
713
714         pos += scnprintf(buf+pos, bufsz-pos, "skip_over_dtim = %d\n",
715                          (cmd.flags &
716                          cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK)) ? 1 : 0);
717         pos += scnprintf(buf+pos, bufsz-pos, "skip_dtim_periods = %d\n",
718                          cmd.skip_dtim_periods);
719         if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK))) {
720                 pos += scnprintf(buf+pos, bufsz-pos, "rx_data_timeout = %d\n",
721                                  le32_to_cpu(cmd.rx_data_timeout));
722                 pos += scnprintf(buf+pos, bufsz-pos, "tx_data_timeout = %d\n",
723                                  le32_to_cpu(cmd.tx_data_timeout));
724         }
725         if (cmd.flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
726                 pos += scnprintf(buf+pos, bufsz-pos,
727                                  "lprx_rssi_threshold = %d\n",
728                                  cmd.lprx_rssi_threshold);
729
730         if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK)))
731                 return pos;
732
733         pos += scnprintf(buf+pos, bufsz-pos, "rx_data_timeout_uapsd = %d\n",
734                          le32_to_cpu(cmd.rx_data_timeout_uapsd));
735         pos += scnprintf(buf+pos, bufsz-pos, "tx_data_timeout_uapsd = %d\n",
736                          le32_to_cpu(cmd.tx_data_timeout_uapsd));
737         pos += scnprintf(buf+pos, bufsz-pos, "qndp_tid = %d\n", cmd.qndp_tid);
738         pos += scnprintf(buf+pos, bufsz-pos, "uapsd_ac_flags = 0x%x\n",
739                          cmd.uapsd_ac_flags);
740         pos += scnprintf(buf+pos, bufsz-pos, "uapsd_max_sp = %d\n",
741                          cmd.uapsd_max_sp);
742         pos += scnprintf(buf+pos, bufsz-pos, "heavy_tx_thld_packets = %d\n",
743                          cmd.heavy_tx_thld_packets);
744         pos += scnprintf(buf+pos, bufsz-pos, "heavy_rx_thld_packets = %d\n",
745                          cmd.heavy_rx_thld_packets);
746         pos += scnprintf(buf+pos, bufsz-pos, "heavy_tx_thld_percentage = %d\n",
747                          cmd.heavy_tx_thld_percentage);
748         pos += scnprintf(buf+pos, bufsz-pos, "heavy_rx_thld_percentage = %d\n",
749                          cmd.heavy_rx_thld_percentage);
750         pos += scnprintf(buf+pos, bufsz-pos, "uapsd_misbehaving_enable = %d\n",
751                          (cmd.flags &
752                           cpu_to_le16(POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK)) ?
753                          1 : 0);
754
755         if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)))
756                 return pos;
757
758         pos += scnprintf(buf+pos, bufsz-pos, "snooze_interval = %d\n",
759                          cmd.snooze_interval);
760         pos += scnprintf(buf+pos, bufsz-pos, "snooze_window = %d\n",
761                          cmd.snooze_window);
762
763         return pos;
764 }
765
766 void
767 iwl_mvm_beacon_filter_debugfs_parameters(struct ieee80211_vif *vif,
768                                          struct iwl_beacon_filter_cmd *cmd)
769 {
770         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
771         struct iwl_dbgfs_bf *dbgfs_bf = &mvmvif->dbgfs_bf;
772
773         if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ENERGY_DELTA)
774                 cmd->bf_energy_delta = cpu_to_le32(dbgfs_bf->bf_energy_delta);
775         if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ROAMING_ENERGY_DELTA)
776                 cmd->bf_roaming_energy_delta =
777                                 cpu_to_le32(dbgfs_bf->bf_roaming_energy_delta);
778         if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ROAMING_STATE)
779                 cmd->bf_roaming_state = cpu_to_le32(dbgfs_bf->bf_roaming_state);
780         if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_THRESHOLD)
781                 cmd->bf_temp_threshold =
782                                 cpu_to_le32(dbgfs_bf->bf_temp_threshold);
783         if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_FAST_FILTER)
784                 cmd->bf_temp_fast_filter =
785                                 cpu_to_le32(dbgfs_bf->bf_temp_fast_filter);
786         if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_SLOW_FILTER)
787                 cmd->bf_temp_slow_filter =
788                                 cpu_to_le32(dbgfs_bf->bf_temp_slow_filter);
789         if (dbgfs_bf->mask & MVM_DEBUGFS_BF_DEBUG_FLAG)
790                 cmd->bf_debug_flag = cpu_to_le32(dbgfs_bf->bf_debug_flag);
791         if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ESCAPE_TIMER)
792                 cmd->bf_escape_timer = cpu_to_le32(dbgfs_bf->bf_escape_timer);
793         if (dbgfs_bf->mask & MVM_DEBUGFS_BA_ESCAPE_TIMER)
794                 cmd->ba_escape_timer = cpu_to_le32(dbgfs_bf->ba_escape_timer);
795         if (dbgfs_bf->mask & MVM_DEBUGFS_BA_ENABLE_BEACON_ABORT)
796                 cmd->ba_enable_beacon_abort =
797                                 cpu_to_le32(dbgfs_bf->ba_enable_beacon_abort);
798 }
799 #endif
800
801 static int _iwl_mvm_enable_beacon_filter(struct iwl_mvm *mvm,
802                                          struct ieee80211_vif *vif,
803                                          struct iwl_beacon_filter_cmd *cmd,
804                                          u32 cmd_flags,
805                                          bool d0i3)
806 {
807         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
808         int ret;
809
810         if (mvmvif != mvm->bf_allowed_vif ||
811             vif->type != NL80211_IFTYPE_STATION || vif->p2p)
812                 return 0;
813
814         iwl_mvm_beacon_filter_set_cqm_params(mvm, vif, cmd);
815         if (!d0i3)
816                 iwl_mvm_beacon_filter_debugfs_parameters(vif, cmd);
817         ret = iwl_mvm_beacon_filter_send_cmd(mvm, cmd, cmd_flags);
818
819         /* don't change bf_enabled in case of temporary d0i3 configuration */
820         if (!ret && !d0i3)
821                 mvmvif->bf_data.bf_enabled = true;
822
823         return ret;
824 }
825
826 int iwl_mvm_enable_beacon_filter(struct iwl_mvm *mvm,
827                                  struct ieee80211_vif *vif,
828                                  u32 flags)
829 {
830         struct iwl_beacon_filter_cmd cmd = {
831                 IWL_BF_CMD_CONFIG_DEFAULTS,
832                 .bf_enable_beacon_filter = cpu_to_le32(1),
833         };
834
835         return _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd, flags, false);
836 }
837
838 int iwl_mvm_disable_beacon_filter(struct iwl_mvm *mvm,
839                                   struct ieee80211_vif *vif,
840                                   u32 flags)
841 {
842         struct iwl_beacon_filter_cmd cmd = {};
843         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
844         int ret;
845
846         if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BF_UPDATED) ||
847             vif->type != NL80211_IFTYPE_STATION || vif->p2p)
848                 return 0;
849
850         ret = iwl_mvm_beacon_filter_send_cmd(mvm, &cmd, flags);
851
852         if (!ret)
853                 mvmvif->bf_data.bf_enabled = false;
854
855         return ret;
856 }
857
858 int iwl_mvm_update_d0i3_power_mode(struct iwl_mvm *mvm,
859                                    struct ieee80211_vif *vif,
860                                    bool enable, u32 flags)
861 {
862         int ret;
863         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
864         struct iwl_mac_power_cmd cmd = {};
865
866         if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
867                 return 0;
868
869         if (!vif->bss_conf.assoc)
870                 return 0;
871
872         iwl_mvm_power_build_cmd(mvm, vif, &cmd);
873         if (enable) {
874                 /* configure skip over dtim up to 300 msec */
875                 int dtimper = mvm->hw->conf.ps_dtim_period ?: 1;
876                 int dtimper_msec = dtimper * vif->bss_conf.beacon_int;
877
878                 if (WARN_ON(!dtimper_msec))
879                         return 0;
880
881                 cmd.flags |=
882                         cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
883                 cmd.skip_dtim_periods = 300 / dtimper_msec;
884         }
885         iwl_mvm_power_log(mvm, &cmd);
886 #ifdef CONFIG_IWLWIFI_DEBUGFS
887         memcpy(&mvmvif->mac_pwr_cmd, &cmd, sizeof(cmd));
888 #endif
889         ret = iwl_mvm_send_cmd_pdu(mvm, MAC_PM_POWER_TABLE, flags,
890                                    sizeof(cmd), &cmd);
891         if (ret)
892                 return ret;
893
894         /* configure beacon filtering */
895         if (mvmvif != mvm->bf_allowed_vif)
896                 return 0;
897
898         if (enable) {
899                 struct iwl_beacon_filter_cmd cmd_bf = {
900                         IWL_BF_CMD_CONFIG_D0I3,
901                         .bf_enable_beacon_filter = cpu_to_le32(1),
902                 };
903                 ret = _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd_bf,
904                                                     flags, true);
905         } else {
906                 if (mvmvif->bf_data.bf_enabled)
907                         ret = iwl_mvm_enable_beacon_filter(mvm, vif, flags);
908                 else
909                         ret = iwl_mvm_disable_beacon_filter(mvm, vif, flags);
910         }
911
912         return ret;
913 }
914
915 int iwl_mvm_update_beacon_filter(struct iwl_mvm *mvm,
916                                  struct ieee80211_vif *vif,
917                                  bool force,
918                                  u32 flags)
919 {
920         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
921
922         if (mvmvif != mvm->bf_allowed_vif)
923                 return 0;
924
925         if (!mvmvif->bf_data.bf_enabled) {
926                 /* disable beacon filtering explicitly if force is true */
927                 if (force)
928                         return iwl_mvm_disable_beacon_filter(mvm, vif, flags);
929                 return 0;
930         }
931
932         return iwl_mvm_enable_beacon_filter(mvm, vif, flags);
933 }