Merge branch 'stable-3.16' of git://git.infradead.org/users/pcmoore/selinux into...
[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 static void iwl_mvm_power_log(struct iwl_mvm *mvm,
127                               struct iwl_mac_power_cmd *cmd)
128 {
129         IWL_DEBUG_POWER(mvm,
130                         "Sending power table command on mac id 0x%X for power level %d, flags = 0x%X\n",
131                         cmd->id_and_color, iwlmvm_mod_params.power_scheme,
132                         le16_to_cpu(cmd->flags));
133         IWL_DEBUG_POWER(mvm, "Keep alive = %u sec\n",
134                         le16_to_cpu(cmd->keep_alive_seconds));
135
136         if (!(cmd->flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK))) {
137                 IWL_DEBUG_POWER(mvm, "Disable power management\n");
138                 return;
139         }
140
141         IWL_DEBUG_POWER(mvm, "Rx timeout = %u usec\n",
142                         le32_to_cpu(cmd->rx_data_timeout));
143         IWL_DEBUG_POWER(mvm, "Tx timeout = %u usec\n",
144                         le32_to_cpu(cmd->tx_data_timeout));
145         if (cmd->flags & cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK))
146                 IWL_DEBUG_POWER(mvm, "DTIM periods to skip = %u\n",
147                                 cmd->skip_dtim_periods);
148         if (cmd->flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
149                 IWL_DEBUG_POWER(mvm, "LP RX RSSI threshold = %u\n",
150                                 cmd->lprx_rssi_threshold);
151         if (cmd->flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK)) {
152                 IWL_DEBUG_POWER(mvm, "uAPSD enabled\n");
153                 IWL_DEBUG_POWER(mvm, "Rx timeout (uAPSD) = %u usec\n",
154                                 le32_to_cpu(cmd->rx_data_timeout_uapsd));
155                 IWL_DEBUG_POWER(mvm, "Tx timeout (uAPSD) = %u usec\n",
156                                 le32_to_cpu(cmd->tx_data_timeout_uapsd));
157                 IWL_DEBUG_POWER(mvm, "QNDP TID = %d\n", cmd->qndp_tid);
158                 IWL_DEBUG_POWER(mvm, "ACs flags = 0x%x\n", cmd->uapsd_ac_flags);
159                 IWL_DEBUG_POWER(mvm, "Max SP = %d\n", cmd->uapsd_max_sp);
160         }
161 }
162
163 static void iwl_mvm_power_configure_uapsd(struct iwl_mvm *mvm,
164                                           struct ieee80211_vif *vif,
165                                           struct iwl_mac_power_cmd *cmd)
166 {
167         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
168         enum ieee80211_ac_numbers ac;
169         bool tid_found = false;
170
171         for (ac = IEEE80211_AC_VO; ac <= IEEE80211_AC_BK; ac++) {
172                 if (!mvmvif->queue_params[ac].uapsd)
173                         continue;
174
175                 if (mvm->cur_ucode != IWL_UCODE_WOWLAN)
176                         cmd->flags |=
177                                 cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK);
178
179                 cmd->uapsd_ac_flags |= BIT(ac);
180
181                 /* QNDP TID - the highest TID with no admission control */
182                 if (!tid_found && !mvmvif->queue_params[ac].acm) {
183                         tid_found = true;
184                         switch (ac) {
185                         case IEEE80211_AC_VO:
186                                 cmd->qndp_tid = 6;
187                                 break;
188                         case IEEE80211_AC_VI:
189                                 cmd->qndp_tid = 5;
190                                 break;
191                         case IEEE80211_AC_BE:
192                                 cmd->qndp_tid = 0;
193                                 break;
194                         case IEEE80211_AC_BK:
195                                 cmd->qndp_tid = 1;
196                                 break;
197                         }
198                 }
199         }
200
201         if (!(cmd->flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK)))
202                 return;
203
204         cmd->flags |= cpu_to_le16(POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK);
205
206         if (cmd->uapsd_ac_flags == (BIT(IEEE80211_AC_VO) |
207                                     BIT(IEEE80211_AC_VI) |
208                                     BIT(IEEE80211_AC_BE) |
209                                     BIT(IEEE80211_AC_BK))) {
210                 cmd->flags |= cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK);
211                 cmd->snooze_interval = cpu_to_le16(IWL_MVM_PS_SNOOZE_INTERVAL);
212                 cmd->snooze_window = (mvm->cur_ucode == IWL_UCODE_WOWLAN) ?
213                         cpu_to_le16(IWL_MVM_WOWLAN_PS_SNOOZE_WINDOW) :
214                         cpu_to_le16(IWL_MVM_PS_SNOOZE_WINDOW);
215         }
216
217         cmd->uapsd_max_sp = IWL_UAPSD_MAX_SP;
218
219         if (mvm->cur_ucode == IWL_UCODE_WOWLAN || cmd->flags &
220             cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)) {
221                 cmd->rx_data_timeout_uapsd =
222                         cpu_to_le32(IWL_MVM_WOWLAN_PS_RX_DATA_TIMEOUT);
223                 cmd->tx_data_timeout_uapsd =
224                         cpu_to_le32(IWL_MVM_WOWLAN_PS_TX_DATA_TIMEOUT);
225         } else {
226                 cmd->rx_data_timeout_uapsd =
227                         cpu_to_le32(IWL_MVM_UAPSD_RX_DATA_TIMEOUT);
228                 cmd->tx_data_timeout_uapsd =
229                         cpu_to_le32(IWL_MVM_UAPSD_TX_DATA_TIMEOUT);
230         }
231
232         if (cmd->flags & cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)) {
233                 cmd->heavy_tx_thld_packets =
234                         IWL_MVM_PS_SNOOZE_HEAVY_TX_THLD_PACKETS;
235                 cmd->heavy_rx_thld_packets =
236                         IWL_MVM_PS_SNOOZE_HEAVY_RX_THLD_PACKETS;
237         } else {
238                 cmd->heavy_tx_thld_packets =
239                         IWL_MVM_PS_HEAVY_TX_THLD_PACKETS;
240                 cmd->heavy_rx_thld_packets =
241                         IWL_MVM_PS_HEAVY_RX_THLD_PACKETS;
242         }
243         cmd->heavy_tx_thld_percentage =
244                 IWL_MVM_PS_HEAVY_TX_THLD_PERCENT;
245         cmd->heavy_rx_thld_percentage =
246                 IWL_MVM_PS_HEAVY_RX_THLD_PERCENT;
247 }
248
249 static void iwl_mvm_binding_iterator(void *_data, u8 *mac,
250                                       struct ieee80211_vif *vif)
251 {
252         unsigned long *data = _data;
253         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
254
255         if (!mvmvif->phy_ctxt)
256                 return;
257
258         if (vif->type == NL80211_IFTYPE_STATION ||
259             vif->type == NL80211_IFTYPE_AP)
260                 __set_bit(mvmvif->phy_ctxt->id, data);
261 }
262
263 static bool iwl_mvm_power_allow_uapsd(struct iwl_mvm *mvm,
264                                        struct ieee80211_vif *vif)
265 {
266         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
267         unsigned long phy_ctxt_counter = 0;
268
269         ieee80211_iterate_active_interfaces_atomic(mvm->hw,
270                                                    IEEE80211_IFACE_ITER_NORMAL,
271                                                    iwl_mvm_binding_iterator,
272                                                    &phy_ctxt_counter);
273
274         if (!memcmp(mvmvif->uapsd_misbehaving_bssid, vif->bss_conf.bssid,
275                     ETH_ALEN))
276                 return false;
277
278         if (vif->p2p &&
279             !(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_P2P_PS_UAPSD))
280                 return false;
281         /*
282          * Avoid using uAPSD if P2P client is associated to GO that uses
283          * opportunistic power save. This is due to current FW limitation.
284          */
285         if (vif->p2p &&
286             (vif->bss_conf.p2p_noa_attr.oppps_ctwindow &
287             IEEE80211_P2P_OPPPS_ENABLE_BIT))
288                 return false;
289
290         /*
291          * Avoid using uAPSD if client is in DCM -
292          * low latency issue in Miracast
293          */
294         if (hweight8(phy_ctxt_counter) >= 2)
295                 return false;
296
297         return true;
298 }
299
300 static void iwl_mvm_power_build_cmd(struct iwl_mvm *mvm,
301                                     struct ieee80211_vif *vif,
302                                     struct iwl_mac_power_cmd *cmd)
303 {
304         struct ieee80211_hw *hw = mvm->hw;
305         struct ieee80211_chanctx_conf *chanctx_conf;
306         struct ieee80211_channel *chan;
307         int dtimper, dtimper_msec;
308         int keep_alive;
309         bool radar_detect = false;
310         struct iwl_mvm_vif *mvmvif __maybe_unused =
311                 iwl_mvm_vif_from_mac80211(vif);
312
313         cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
314                                                             mvmvif->color));
315         dtimper = hw->conf.ps_dtim_period ?: 1;
316
317         /*
318          * Regardless of power management state the driver must set
319          * keep alive period. FW will use it for sending keep alive NDPs
320          * immediately after association. Check that keep alive period
321          * is at least 3 * DTIM
322          */
323         dtimper_msec = dtimper * vif->bss_conf.beacon_int;
324         keep_alive = max_t(int, 3 * dtimper_msec,
325                            MSEC_PER_SEC * POWER_KEEP_ALIVE_PERIOD_SEC);
326         keep_alive = DIV_ROUND_UP(keep_alive, MSEC_PER_SEC);
327         cmd->keep_alive_seconds = cpu_to_le16(keep_alive);
328
329         if (mvm->ps_disabled)
330                 return;
331
332         cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK);
333
334         if (!vif->bss_conf.ps || iwl_mvm_vif_low_latency(mvmvif) ||
335             !mvmvif->pm_enabled)
336                 return;
337
338         cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK);
339
340         if (vif->bss_conf.beacon_rate &&
341             (vif->bss_conf.beacon_rate->bitrate == 10 ||
342              vif->bss_conf.beacon_rate->bitrate == 60)) {
343                 cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
344                 cmd->lprx_rssi_threshold = POWER_LPRX_RSSI_THRESHOLD;
345         }
346
347         /* Check if radar detection is required on current channel */
348         rcu_read_lock();
349         chanctx_conf = rcu_dereference(vif->chanctx_conf);
350         WARN_ON(!chanctx_conf);
351         if (chanctx_conf) {
352                 chan = chanctx_conf->def.chan;
353                 radar_detect = chan->flags & IEEE80211_CHAN_RADAR;
354         }
355         rcu_read_unlock();
356
357         /* Check skip over DTIM conditions */
358         if (!radar_detect && (dtimper <= 10) &&
359             (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_LP ||
360              mvm->cur_ucode == IWL_UCODE_WOWLAN)) {
361                 cmd->flags |= cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
362                 cmd->skip_dtim_periods = 3;
363         }
364
365         if (mvm->cur_ucode != IWL_UCODE_WOWLAN) {
366                 cmd->rx_data_timeout =
367                         cpu_to_le32(IWL_MVM_DEFAULT_PS_RX_DATA_TIMEOUT);
368                 cmd->tx_data_timeout =
369                         cpu_to_le32(IWL_MVM_DEFAULT_PS_TX_DATA_TIMEOUT);
370         } else {
371                 cmd->rx_data_timeout =
372                         cpu_to_le32(IWL_MVM_WOWLAN_PS_RX_DATA_TIMEOUT);
373                 cmd->tx_data_timeout =
374                         cpu_to_le32(IWL_MVM_WOWLAN_PS_TX_DATA_TIMEOUT);
375         }
376
377         if (iwl_mvm_power_allow_uapsd(mvm, vif))
378                 iwl_mvm_power_configure_uapsd(mvm, vif, cmd);
379
380 #ifdef CONFIG_IWLWIFI_DEBUGFS
381         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_KEEP_ALIVE)
382                 cmd->keep_alive_seconds =
383                         cpu_to_le16(mvmvif->dbgfs_pm.keep_alive_seconds);
384         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_OVER_DTIM) {
385                 if (mvmvif->dbgfs_pm.skip_over_dtim)
386                         cmd->flags |=
387                                 cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
388                 else
389                         cmd->flags &=
390                                 cpu_to_le16(~POWER_FLAGS_SKIP_OVER_DTIM_MSK);
391         }
392         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_RX_DATA_TIMEOUT)
393                 cmd->rx_data_timeout =
394                         cpu_to_le32(mvmvif->dbgfs_pm.rx_data_timeout);
395         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_TX_DATA_TIMEOUT)
396                 cmd->tx_data_timeout =
397                         cpu_to_le32(mvmvif->dbgfs_pm.tx_data_timeout);
398         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_DTIM_PERIODS)
399                 cmd->skip_dtim_periods = mvmvif->dbgfs_pm.skip_dtim_periods;
400         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_ENA) {
401                 if (mvmvif->dbgfs_pm.lprx_ena)
402                         cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
403                 else
404                         cmd->flags &= cpu_to_le16(~POWER_FLAGS_LPRX_ENA_MSK);
405         }
406         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_RSSI_THRESHOLD)
407                 cmd->lprx_rssi_threshold = mvmvif->dbgfs_pm.lprx_rssi_threshold;
408         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SNOOZE_ENABLE) {
409                 if (mvmvif->dbgfs_pm.snooze_ena)
410                         cmd->flags |=
411                                 cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK);
412                 else
413                         cmd->flags &=
414                                 cpu_to_le16(~POWER_FLAGS_SNOOZE_ENA_MSK);
415         }
416         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_UAPSD_MISBEHAVING) {
417                 u16 flag = POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK;
418                 if (mvmvif->dbgfs_pm.uapsd_misbehaving)
419                         cmd->flags |= cpu_to_le16(flag);
420                 else
421                         cmd->flags &= cpu_to_le16(flag);
422         }
423 #endif /* CONFIG_IWLWIFI_DEBUGFS */
424 }
425
426 static int iwl_mvm_power_send_cmd(struct iwl_mvm *mvm,
427                                          struct ieee80211_vif *vif)
428 {
429         struct iwl_mac_power_cmd cmd = {};
430
431         iwl_mvm_power_build_cmd(mvm, vif, &cmd);
432         iwl_mvm_power_log(mvm, &cmd);
433 #ifdef CONFIG_IWLWIFI_DEBUGFS
434         memcpy(&iwl_mvm_vif_from_mac80211(vif)->mac_pwr_cmd, &cmd, sizeof(cmd));
435 #endif
436
437         return iwl_mvm_send_cmd_pdu(mvm, MAC_PM_POWER_TABLE, 0,
438                                     sizeof(cmd), &cmd);
439 }
440
441 int iwl_mvm_power_update_device(struct iwl_mvm *mvm)
442 {
443         struct iwl_device_power_cmd cmd = {
444                 .flags = cpu_to_le16(DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK),
445         };
446
447         if (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM)
448                 mvm->ps_disabled = true;
449
450         if (mvm->ps_disabled)
451                 cmd.flags |= cpu_to_le16(DEVICE_POWER_FLAGS_CAM_MSK);
452
453 #ifdef CONFIG_IWLWIFI_DEBUGFS
454         if ((mvm->cur_ucode == IWL_UCODE_WOWLAN) ? mvm->disable_power_off_d3 :
455             mvm->disable_power_off)
456                 cmd.flags &=
457                         cpu_to_le16(~DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK);
458 #endif
459         IWL_DEBUG_POWER(mvm,
460                         "Sending device power command with flags = 0x%X\n",
461                         cmd.flags);
462
463         return iwl_mvm_send_cmd_pdu(mvm, POWER_TABLE_CMD, 0, sizeof(cmd),
464                                     &cmd);
465 }
466
467 void iwl_mvm_power_vif_assoc(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
468 {
469         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
470
471         if (memcmp(vif->bss_conf.bssid, mvmvif->uapsd_misbehaving_bssid,
472                    ETH_ALEN))
473                 memset(mvmvif->uapsd_misbehaving_bssid, 0, ETH_ALEN);
474 }
475
476 static void iwl_mvm_power_uapsd_misbehav_ap_iterator(void *_data, u8 *mac,
477                                                      struct ieee80211_vif *vif)
478 {
479         u8 *ap_sta_id = _data;
480         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
481
482         /* The ap_sta_id is not expected to change during current association
483          * so no explicit protection is needed
484          */
485         if (mvmvif->ap_sta_id == *ap_sta_id)
486                 memcpy(mvmvif->uapsd_misbehaving_bssid, vif->bss_conf.bssid,
487                        ETH_ALEN);
488 }
489
490 int iwl_mvm_power_uapsd_misbehaving_ap_notif(struct iwl_mvm *mvm,
491                                              struct iwl_rx_cmd_buffer *rxb,
492                                              struct iwl_device_cmd *cmd)
493 {
494         struct iwl_rx_packet *pkt = rxb_addr(rxb);
495         struct iwl_uapsd_misbehaving_ap_notif *notif = (void *)pkt->data;
496         u8 ap_sta_id = le32_to_cpu(notif->sta_id);
497
498         ieee80211_iterate_active_interfaces_atomic(
499                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
500                 iwl_mvm_power_uapsd_misbehav_ap_iterator, &ap_sta_id);
501
502         return 0;
503 }
504
505 struct iwl_power_vifs {
506         struct ieee80211_vif *bf_vif;
507         struct ieee80211_vif *bss_vif;
508         struct ieee80211_vif *p2p_vif;
509         struct ieee80211_vif *ap_vif;
510         struct ieee80211_vif *monitor_vif;
511         bool p2p_active;
512         bool bss_active;
513         bool ap_active;
514         bool monitor_active;
515 };
516
517 static void iwl_mvm_power_iterator(void *_data, u8 *mac,
518                                    struct ieee80211_vif *vif)
519 {
520         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
521         struct iwl_power_vifs *power_iterator = _data;
522
523         mvmvif->pm_enabled = false;
524         switch (ieee80211_vif_type_p2p(vif)) {
525         case NL80211_IFTYPE_P2P_DEVICE:
526                 break;
527
528         case NL80211_IFTYPE_P2P_GO:
529         case NL80211_IFTYPE_AP:
530                 /* only a single MAC of the same type */
531                 WARN_ON(power_iterator->ap_vif);
532                 power_iterator->ap_vif = vif;
533                 if (mvmvif->phy_ctxt)
534                         if (mvmvif->phy_ctxt->id < MAX_PHYS)
535                                 power_iterator->ap_active = true;
536                 break;
537
538         case NL80211_IFTYPE_MONITOR:
539                 /* only a single MAC of the same type */
540                 WARN_ON(power_iterator->monitor_vif);
541                 power_iterator->monitor_vif = vif;
542                 if (mvmvif->phy_ctxt)
543                         if (mvmvif->phy_ctxt->id < MAX_PHYS)
544                                 power_iterator->monitor_active = true;
545                 break;
546
547         case NL80211_IFTYPE_P2P_CLIENT:
548                 /* only a single MAC of the same type */
549                 WARN_ON(power_iterator->p2p_vif);
550                 power_iterator->p2p_vif = vif;
551                 if (mvmvif->phy_ctxt)
552                         if (mvmvif->phy_ctxt->id < MAX_PHYS)
553                                 power_iterator->p2p_active = true;
554                 break;
555
556         case NL80211_IFTYPE_STATION:
557                 /* only a single MAC of the same type */
558                 WARN_ON(power_iterator->bss_vif);
559                 power_iterator->bss_vif = vif;
560                 if (mvmvif->phy_ctxt)
561                         if (mvmvif->phy_ctxt->id < MAX_PHYS)
562                                 power_iterator->bss_active = true;
563
564                 if (mvmvif->bf_data.bf_enabled &&
565                     !WARN_ON(power_iterator->bf_vif))
566                         power_iterator->bf_vif = vif;
567
568                 break;
569
570         default:
571                 break;
572         }
573 }
574
575 static void
576 iwl_mvm_power_set_pm(struct iwl_mvm *mvm,
577                                     struct iwl_power_vifs *vifs)
578 {
579         struct iwl_mvm_vif *bss_mvmvif = NULL;
580         struct iwl_mvm_vif *p2p_mvmvif = NULL;
581         struct iwl_mvm_vif *ap_mvmvif = NULL;
582         bool client_same_channel = false;
583         bool ap_same_channel = false;
584
585         lockdep_assert_held(&mvm->mutex);
586
587         /* get vifs info + set pm_enable to false */
588         ieee80211_iterate_active_interfaces_atomic(mvm->hw,
589                                             IEEE80211_IFACE_ITER_NORMAL,
590                                             iwl_mvm_power_iterator, vifs);
591
592         if (vifs->bss_vif)
593                 bss_mvmvif = iwl_mvm_vif_from_mac80211(vifs->bss_vif);
594
595         if (vifs->p2p_vif)
596                 p2p_mvmvif = iwl_mvm_vif_from_mac80211(vifs->p2p_vif);
597
598         if (vifs->ap_vif)
599                 ap_mvmvif = iwl_mvm_vif_from_mac80211(vifs->ap_vif);
600
601         /* enable PM on bss if bss stand alone */
602         if (vifs->bss_active && !vifs->p2p_active && !vifs->ap_active) {
603                 bss_mvmvif->pm_enabled = true;
604                 return;
605         }
606
607         /* enable PM on p2p if p2p stand alone */
608         if (vifs->p2p_active && !vifs->bss_active && !vifs->ap_active) {
609                 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_P2P_PM)
610                         p2p_mvmvif->pm_enabled = true;
611                 return;
612         }
613
614         if (vifs->bss_active && vifs->p2p_active)
615                 client_same_channel = (bss_mvmvif->phy_ctxt->id ==
616                                        p2p_mvmvif->phy_ctxt->id);
617         if (vifs->bss_active && vifs->ap_active)
618                 ap_same_channel = (bss_mvmvif->phy_ctxt->id ==
619                                    ap_mvmvif->phy_ctxt->id);
620
621         /* clients are not stand alone: enable PM if DCM */
622         if (!(client_same_channel || ap_same_channel) &&
623             (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BSS_P2P_PS_DCM)) {
624                 if (vifs->bss_active)
625                         bss_mvmvif->pm_enabled = true;
626                 if (vifs->p2p_active &&
627                     (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_P2P_PM))
628                         p2p_mvmvif->pm_enabled = true;
629                 return;
630         }
631
632         /*
633          * There is only one channel in the system and there are only
634          * bss and p2p clients that share it
635          */
636         if (client_same_channel && !vifs->ap_active &&
637             (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BSS_P2P_PS_SCM)) {
638                 /* share same channel*/
639                 bss_mvmvif->pm_enabled = true;
640                 if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_P2P_PM)
641                         p2p_mvmvif->pm_enabled = true;
642         }
643 }
644
645 #ifdef CONFIG_IWLWIFI_DEBUGFS
646 int iwl_mvm_power_mac_dbgfs_read(struct iwl_mvm *mvm,
647                                  struct ieee80211_vif *vif, char *buf,
648                                  int bufsz)
649 {
650         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
651         struct iwl_mac_power_cmd cmd = {};
652         int pos = 0;
653
654         mutex_lock(&mvm->mutex);
655         memcpy(&cmd, &mvmvif->mac_pwr_cmd, sizeof(cmd));
656         mutex_unlock(&mvm->mutex);
657
658         pos += scnprintf(buf+pos, bufsz-pos, "power_scheme = %d\n",
659                          iwlmvm_mod_params.power_scheme);
660         pos += scnprintf(buf+pos, bufsz-pos, "flags = 0x%x\n",
661                          le16_to_cpu(cmd.flags));
662         pos += scnprintf(buf+pos, bufsz-pos, "keep_alive = %d\n",
663                          le16_to_cpu(cmd.keep_alive_seconds));
664
665         if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK)))
666                 return pos;
667
668         pos += scnprintf(buf+pos, bufsz-pos, "skip_over_dtim = %d\n",
669                          (cmd.flags &
670                          cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK)) ? 1 : 0);
671         pos += scnprintf(buf+pos, bufsz-pos, "skip_dtim_periods = %d\n",
672                          cmd.skip_dtim_periods);
673         if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK))) {
674                 pos += scnprintf(buf+pos, bufsz-pos, "rx_data_timeout = %d\n",
675                                  le32_to_cpu(cmd.rx_data_timeout));
676                 pos += scnprintf(buf+pos, bufsz-pos, "tx_data_timeout = %d\n",
677                                  le32_to_cpu(cmd.tx_data_timeout));
678         }
679         if (cmd.flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
680                 pos += scnprintf(buf+pos, bufsz-pos,
681                                  "lprx_rssi_threshold = %d\n",
682                                  cmd.lprx_rssi_threshold);
683
684         if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_ADVANCE_PM_ENA_MSK)))
685                 return pos;
686
687         pos += scnprintf(buf+pos, bufsz-pos, "rx_data_timeout_uapsd = %d\n",
688                          le32_to_cpu(cmd.rx_data_timeout_uapsd));
689         pos += scnprintf(buf+pos, bufsz-pos, "tx_data_timeout_uapsd = %d\n",
690                          le32_to_cpu(cmd.tx_data_timeout_uapsd));
691         pos += scnprintf(buf+pos, bufsz-pos, "qndp_tid = %d\n", cmd.qndp_tid);
692         pos += scnprintf(buf+pos, bufsz-pos, "uapsd_ac_flags = 0x%x\n",
693                          cmd.uapsd_ac_flags);
694         pos += scnprintf(buf+pos, bufsz-pos, "uapsd_max_sp = %d\n",
695                          cmd.uapsd_max_sp);
696         pos += scnprintf(buf+pos, bufsz-pos, "heavy_tx_thld_packets = %d\n",
697                          cmd.heavy_tx_thld_packets);
698         pos += scnprintf(buf+pos, bufsz-pos, "heavy_rx_thld_packets = %d\n",
699                          cmd.heavy_rx_thld_packets);
700         pos += scnprintf(buf+pos, bufsz-pos, "heavy_tx_thld_percentage = %d\n",
701                          cmd.heavy_tx_thld_percentage);
702         pos += scnprintf(buf+pos, bufsz-pos, "heavy_rx_thld_percentage = %d\n",
703                          cmd.heavy_rx_thld_percentage);
704         pos += scnprintf(buf+pos, bufsz-pos, "uapsd_misbehaving_enable = %d\n",
705                          (cmd.flags &
706                           cpu_to_le16(POWER_FLAGS_UAPSD_MISBEHAVING_ENA_MSK)) ?
707                          1 : 0);
708
709         if (!(cmd.flags & cpu_to_le16(POWER_FLAGS_SNOOZE_ENA_MSK)))
710                 return pos;
711
712         pos += scnprintf(buf+pos, bufsz-pos, "snooze_interval = %d\n",
713                          cmd.snooze_interval);
714         pos += scnprintf(buf+pos, bufsz-pos, "snooze_window = %d\n",
715                          cmd.snooze_window);
716
717         return pos;
718 }
719
720 void
721 iwl_mvm_beacon_filter_debugfs_parameters(struct ieee80211_vif *vif,
722                                          struct iwl_beacon_filter_cmd *cmd)
723 {
724         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
725         struct iwl_dbgfs_bf *dbgfs_bf = &mvmvif->dbgfs_bf;
726
727         if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ENERGY_DELTA)
728                 cmd->bf_energy_delta = cpu_to_le32(dbgfs_bf->bf_energy_delta);
729         if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ROAMING_ENERGY_DELTA)
730                 cmd->bf_roaming_energy_delta =
731                                 cpu_to_le32(dbgfs_bf->bf_roaming_energy_delta);
732         if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ROAMING_STATE)
733                 cmd->bf_roaming_state = cpu_to_le32(dbgfs_bf->bf_roaming_state);
734         if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_THRESHOLD)
735                 cmd->bf_temp_threshold =
736                                 cpu_to_le32(dbgfs_bf->bf_temp_threshold);
737         if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_FAST_FILTER)
738                 cmd->bf_temp_fast_filter =
739                                 cpu_to_le32(dbgfs_bf->bf_temp_fast_filter);
740         if (dbgfs_bf->mask & MVM_DEBUGFS_BF_TEMP_SLOW_FILTER)
741                 cmd->bf_temp_slow_filter =
742                                 cpu_to_le32(dbgfs_bf->bf_temp_slow_filter);
743         if (dbgfs_bf->mask & MVM_DEBUGFS_BF_DEBUG_FLAG)
744                 cmd->bf_debug_flag = cpu_to_le32(dbgfs_bf->bf_debug_flag);
745         if (dbgfs_bf->mask & MVM_DEBUGFS_BF_ESCAPE_TIMER)
746                 cmd->bf_escape_timer = cpu_to_le32(dbgfs_bf->bf_escape_timer);
747         if (dbgfs_bf->mask & MVM_DEBUGFS_BA_ESCAPE_TIMER)
748                 cmd->ba_escape_timer = cpu_to_le32(dbgfs_bf->ba_escape_timer);
749         if (dbgfs_bf->mask & MVM_DEBUGFS_BA_ENABLE_BEACON_ABORT)
750                 cmd->ba_enable_beacon_abort =
751                                 cpu_to_le32(dbgfs_bf->ba_enable_beacon_abort);
752 }
753 #endif
754
755 static int _iwl_mvm_enable_beacon_filter(struct iwl_mvm *mvm,
756                                          struct ieee80211_vif *vif,
757                                          struct iwl_beacon_filter_cmd *cmd,
758                                          u32 cmd_flags,
759                                          bool d0i3)
760 {
761         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
762         int ret;
763
764         if (mvmvif != mvm->bf_allowed_vif || !vif->bss_conf.dtim_period ||
765             vif->type != NL80211_IFTYPE_STATION || vif->p2p)
766                 return 0;
767
768         iwl_mvm_beacon_filter_set_cqm_params(mvm, vif, cmd);
769         if (!d0i3)
770                 iwl_mvm_beacon_filter_debugfs_parameters(vif, cmd);
771         ret = iwl_mvm_beacon_filter_send_cmd(mvm, cmd, cmd_flags);
772
773         /* don't change bf_enabled in case of temporary d0i3 configuration */
774         if (!ret && !d0i3)
775                 mvmvif->bf_data.bf_enabled = true;
776
777         return ret;
778 }
779
780 int iwl_mvm_enable_beacon_filter(struct iwl_mvm *mvm,
781                                  struct ieee80211_vif *vif,
782                                  u32 flags)
783 {
784         struct iwl_beacon_filter_cmd cmd = {
785                 IWL_BF_CMD_CONFIG_DEFAULTS,
786                 .bf_enable_beacon_filter = cpu_to_le32(1),
787         };
788
789         return _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd, flags, false);
790 }
791
792 static int iwl_mvm_update_beacon_abort(struct iwl_mvm *mvm,
793                                        struct ieee80211_vif *vif,
794                                        bool enable)
795 {
796         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
797         struct iwl_beacon_filter_cmd cmd = {
798                 IWL_BF_CMD_CONFIG_DEFAULTS,
799                 .bf_enable_beacon_filter = cpu_to_le32(1),
800         };
801
802         if (!mvmvif->bf_data.bf_enabled)
803                 return 0;
804
805         if (mvm->cur_ucode == IWL_UCODE_WOWLAN)
806                 cmd.ba_escape_timer = cpu_to_le32(IWL_BA_ESCAPE_TIMER_D3);
807
808         mvmvif->bf_data.ba_enabled = enable;
809         return _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd, 0, false);
810 }
811
812 int iwl_mvm_disable_beacon_filter(struct iwl_mvm *mvm,
813                                   struct ieee80211_vif *vif,
814                                   u32 flags)
815 {
816         struct iwl_beacon_filter_cmd cmd = {};
817         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
818         int ret;
819
820         if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
821                 return 0;
822
823         ret = iwl_mvm_beacon_filter_send_cmd(mvm, &cmd, flags);
824
825         if (!ret)
826                 mvmvif->bf_data.bf_enabled = false;
827
828         return ret;
829 }
830
831 int iwl_mvm_power_update_mac(struct iwl_mvm *mvm)
832 {
833         struct iwl_mvm_vif *mvmvif;
834         struct iwl_power_vifs vifs = {};
835         bool ba_enable;
836         int ret;
837
838         lockdep_assert_held(&mvm->mutex);
839
840         iwl_mvm_power_set_pm(mvm, &vifs);
841
842         /* disable PS if CAM */
843         if (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM) {
844                 mvm->ps_disabled = true;
845         } else {
846         /* don't update device power state unless we add / remove monitor */
847                 if (vifs.monitor_vif) {
848                         if (vifs.monitor_active)
849                                 mvm->ps_disabled = true;
850                         ret = iwl_mvm_power_update_device(mvm);
851                         if (ret)
852                                 return ret;
853                 }
854         }
855
856         if (vifs.bss_vif) {
857                 ret = iwl_mvm_power_send_cmd(mvm, vifs.bss_vif);
858                 if (ret)
859                         return ret;
860         }
861
862         if (vifs.p2p_vif) {
863                 ret = iwl_mvm_power_send_cmd(mvm, vifs.p2p_vif);
864                 if (ret)
865                         return ret;
866         }
867
868         if (!vifs.bf_vif)
869                 return 0;
870
871         mvmvif = iwl_mvm_vif_from_mac80211(vifs.bf_vif);
872
873         ba_enable = !(!mvmvif->pm_enabled || mvm->ps_disabled ||
874                       !vifs.bf_vif->bss_conf.ps ||
875                       iwl_mvm_vif_low_latency(mvmvif));
876
877         return iwl_mvm_update_beacon_abort(mvm, vifs.bf_vif, ba_enable);
878 }
879
880 int iwl_mvm_update_d0i3_power_mode(struct iwl_mvm *mvm,
881                                    struct ieee80211_vif *vif,
882                                    bool enable, u32 flags)
883 {
884         int ret;
885         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
886         struct iwl_mac_power_cmd cmd = {};
887
888         if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
889                 return 0;
890
891         if (!vif->bss_conf.assoc)
892                 return 0;
893
894         iwl_mvm_power_build_cmd(mvm, vif, &cmd);
895         if (enable) {
896                 /* configure skip over dtim up to 300 msec */
897                 int dtimper = mvm->hw->conf.ps_dtim_period ?: 1;
898                 int dtimper_msec = dtimper * vif->bss_conf.beacon_int;
899
900                 if (WARN_ON(!dtimper_msec))
901                         return 0;
902
903                 cmd.skip_dtim_periods = 300 / dtimper_msec;
904                 if (cmd.skip_dtim_periods)
905                         cmd.flags |=
906                                 cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
907         }
908         iwl_mvm_power_log(mvm, &cmd);
909 #ifdef CONFIG_IWLWIFI_DEBUGFS
910         memcpy(&mvmvif->mac_pwr_cmd, &cmd, sizeof(cmd));
911 #endif
912         ret = iwl_mvm_send_cmd_pdu(mvm, MAC_PM_POWER_TABLE, flags,
913                                    sizeof(cmd), &cmd);
914         if (ret)
915                 return ret;
916
917         /* configure beacon filtering */
918         if (mvmvif != mvm->bf_allowed_vif)
919                 return 0;
920
921         if (enable) {
922                 struct iwl_beacon_filter_cmd cmd_bf = {
923                         IWL_BF_CMD_CONFIG_D0I3,
924                         .bf_enable_beacon_filter = cpu_to_le32(1),
925                 };
926                 ret = _iwl_mvm_enable_beacon_filter(mvm, vif, &cmd_bf,
927                                                     flags, true);
928         } else {
929                 if (mvmvif->bf_data.bf_enabled)
930                         ret = iwl_mvm_enable_beacon_filter(mvm, vif, flags);
931                 else
932                         ret = iwl_mvm_disable_beacon_filter(mvm, vif, flags);
933         }
934
935         return ret;
936 }