]> Pileus Git - ~andy/linux/blob - drivers/net/wireless/iwlwifi/mvm/power_legacy.c
{xfrm,pktgen} Fix compiling error when CONFIG_XFRM is not set
[~andy/linux] / drivers / net / wireless / iwlwifi / mvm / power_legacy.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 - 2013 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 - 2013 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 #include <linux/init.h>
68
69 #include <net/mac80211.h>
70
71 #include "iwl-debug.h"
72 #include "mvm.h"
73 #include "iwl-modparams.h"
74 #include "fw-api-power.h"
75
76 #define POWER_KEEP_ALIVE_PERIOD_SEC    25
77
78 static void iwl_mvm_power_log(struct iwl_mvm *mvm,
79                               struct iwl_powertable_cmd *cmd)
80 {
81         IWL_DEBUG_POWER(mvm,
82                         "Sending power table command for power level %d, flags = 0x%X\n",
83                         iwlmvm_mod_params.power_scheme,
84                         le16_to_cpu(cmd->flags));
85         IWL_DEBUG_POWER(mvm, "Keep alive = %u sec\n", cmd->keep_alive_seconds);
86
87         if (cmd->flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK)) {
88                 IWL_DEBUG_POWER(mvm, "Rx timeout = %u usec\n",
89                                 le32_to_cpu(cmd->rx_data_timeout));
90                 IWL_DEBUG_POWER(mvm, "Tx timeout = %u usec\n",
91                                 le32_to_cpu(cmd->tx_data_timeout));
92                 if (cmd->flags & cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK))
93                         IWL_DEBUG_POWER(mvm, "DTIM periods to skip = %u\n",
94                                         le32_to_cpu(cmd->skip_dtim_periods));
95                 if (cmd->flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
96                         IWL_DEBUG_POWER(mvm, "LP RX RSSI threshold = %u\n",
97                                         le32_to_cpu(cmd->lprx_rssi_threshold));
98         }
99 }
100
101 static void iwl_mvm_power_build_cmd(struct iwl_mvm *mvm,
102                                     struct ieee80211_vif *vif,
103                                     struct iwl_powertable_cmd *cmd)
104 {
105         struct ieee80211_hw *hw = mvm->hw;
106         struct ieee80211_chanctx_conf *chanctx_conf;
107         struct ieee80211_channel *chan;
108         int dtimper, dtimper_msec;
109         int keep_alive;
110         bool radar_detect = false;
111         struct iwl_mvm_vif *mvmvif __maybe_unused =
112                 iwl_mvm_vif_from_mac80211(vif);
113
114         /*
115          * Regardless of power management state the driver must set
116          * keep alive period. FW will use it for sending keep alive NDPs
117          * immediately after association.
118          */
119         cmd->keep_alive_seconds = POWER_KEEP_ALIVE_PERIOD_SEC;
120
121         if (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM)
122                 return;
123
124         cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK);
125         if (!vif->bss_conf.assoc)
126                 cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK);
127
128 #ifdef CONFIG_IWLWIFI_DEBUGFS
129         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_DISABLE_POWER_OFF &&
130             mvmvif->dbgfs_pm.disable_power_off)
131                 cmd->flags &= cpu_to_le16(~POWER_FLAGS_POWER_SAVE_ENA_MSK);
132 #endif
133         if (!vif->bss_conf.ps)
134                 return;
135
136         cmd->flags |= cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK);
137
138         if (vif->bss_conf.beacon_rate &&
139             (vif->bss_conf.beacon_rate->bitrate == 10 ||
140              vif->bss_conf.beacon_rate->bitrate == 60)) {
141                 cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
142                 cmd->lprx_rssi_threshold =
143                         cpu_to_le32(POWER_LPRX_RSSI_THRESHOLD);
144         }
145
146         dtimper = hw->conf.ps_dtim_period ?: 1;
147
148         /* Check if radar detection is required on current channel */
149         rcu_read_lock();
150         chanctx_conf = rcu_dereference(vif->chanctx_conf);
151         WARN_ON(!chanctx_conf);
152         if (chanctx_conf) {
153                 chan = chanctx_conf->def.chan;
154                 radar_detect = chan->flags & IEEE80211_CHAN_RADAR;
155         }
156         rcu_read_unlock();
157
158         /* Check skip over DTIM conditions */
159         if (!radar_detect && (dtimper <= 10) &&
160             (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_LP ||
161              mvm->cur_ucode == IWL_UCODE_WOWLAN)) {
162                 cmd->flags |= cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
163                 cmd->skip_dtim_periods = cpu_to_le32(3);
164         }
165
166         /* Check that keep alive period is at least 3 * DTIM */
167         dtimper_msec = dtimper * vif->bss_conf.beacon_int;
168         keep_alive = max_t(int, 3 * dtimper_msec,
169                            MSEC_PER_SEC * cmd->keep_alive_seconds);
170         keep_alive = DIV_ROUND_UP(keep_alive, MSEC_PER_SEC);
171         cmd->keep_alive_seconds = keep_alive;
172
173         if (mvm->cur_ucode != IWL_UCODE_WOWLAN) {
174                 cmd->rx_data_timeout = cpu_to_le32(100 * USEC_PER_MSEC);
175                 cmd->tx_data_timeout = cpu_to_le32(100 * USEC_PER_MSEC);
176         } else {
177                 cmd->rx_data_timeout = cpu_to_le32(10 * USEC_PER_MSEC);
178                 cmd->tx_data_timeout = cpu_to_le32(10 * USEC_PER_MSEC);
179         }
180
181 #ifdef CONFIG_IWLWIFI_DEBUGFS
182         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_KEEP_ALIVE)
183                 cmd->keep_alive_seconds = mvmvif->dbgfs_pm.keep_alive_seconds;
184         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_OVER_DTIM) {
185                 if (mvmvif->dbgfs_pm.skip_over_dtim)
186                         cmd->flags |=
187                                 cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK);
188                 else
189                         cmd->flags &=
190                                 cpu_to_le16(~POWER_FLAGS_SKIP_OVER_DTIM_MSK);
191         }
192         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_RX_DATA_TIMEOUT)
193                 cmd->rx_data_timeout =
194                         cpu_to_le32(mvmvif->dbgfs_pm.rx_data_timeout);
195         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_TX_DATA_TIMEOUT)
196                 cmd->tx_data_timeout =
197                         cpu_to_le32(mvmvif->dbgfs_pm.tx_data_timeout);
198         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_SKIP_DTIM_PERIODS)
199                 cmd->skip_dtim_periods =
200                         cpu_to_le32(mvmvif->dbgfs_pm.skip_dtim_periods);
201         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_ENA) {
202                 if (mvmvif->dbgfs_pm.lprx_ena)
203                         cmd->flags |= cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK);
204                 else
205                         cmd->flags &= cpu_to_le16(~POWER_FLAGS_LPRX_ENA_MSK);
206         }
207         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_LPRX_RSSI_THRESHOLD)
208                 cmd->lprx_rssi_threshold =
209                         cpu_to_le32(mvmvif->dbgfs_pm.lprx_rssi_threshold);
210 #endif /* CONFIG_IWLWIFI_DEBUGFS */
211 }
212
213 static int iwl_mvm_power_legacy_update_mode(struct iwl_mvm *mvm,
214                                             struct ieee80211_vif *vif)
215 {
216         int ret;
217         bool ba_enable;
218         struct iwl_powertable_cmd cmd = {};
219
220         if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
221                 return 0;
222
223         /*
224          * TODO: The following vif_count verification is temporary condition.
225          * Avoid power mode update if more than one interface is currently
226          * active. Remove this condition when FW will support power management
227          * on multiple MACs.
228          */
229         IWL_DEBUG_POWER(mvm, "Currently %d interfaces active\n",
230                         mvm->vif_count);
231         if (mvm->vif_count > 1)
232                 return 0;
233
234         iwl_mvm_power_build_cmd(mvm, vif, &cmd);
235         iwl_mvm_power_log(mvm, &cmd);
236
237         ret = iwl_mvm_send_cmd_pdu(mvm, POWER_TABLE_CMD, CMD_SYNC,
238                                    sizeof(cmd), &cmd);
239         if (ret)
240                 return ret;
241
242         ba_enable = !!(cmd.flags &
243                        cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK));
244
245         return iwl_mvm_update_beacon_abort(mvm, vif, ba_enable);
246 }
247
248 static int iwl_mvm_power_legacy_disable(struct iwl_mvm *mvm,
249                                         struct ieee80211_vif *vif)
250 {
251         struct iwl_powertable_cmd cmd = {};
252         struct iwl_mvm_vif *mvmvif __maybe_unused =
253                 iwl_mvm_vif_from_mac80211(vif);
254
255         if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
256                 return 0;
257
258         if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
259                 cmd.flags |= cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK);
260
261 #ifdef CONFIG_IWLWIFI_DEBUGFS
262         if (mvmvif->dbgfs_pm.mask & MVM_DEBUGFS_PM_DISABLE_POWER_OFF &&
263             mvmvif->dbgfs_pm.disable_power_off)
264                 cmd.flags &= cpu_to_le16(~POWER_FLAGS_POWER_SAVE_ENA_MSK);
265 #endif
266         iwl_mvm_power_log(mvm, &cmd);
267
268         return iwl_mvm_send_cmd_pdu(mvm, POWER_TABLE_CMD, CMD_ASYNC,
269                                     sizeof(cmd), &cmd);
270 }
271
272 #ifdef CONFIG_IWLWIFI_DEBUGFS
273 static int iwl_mvm_power_legacy_dbgfs_read(struct iwl_mvm *mvm,
274                                            struct ieee80211_vif *vif, char *buf,
275                                            int bufsz)
276 {
277         struct iwl_powertable_cmd cmd = {};
278         int pos = 0;
279
280         iwl_mvm_power_build_cmd(mvm, vif, &cmd);
281
282         pos += scnprintf(buf+pos, bufsz-pos, "disable_power_off = %d\n",
283                          (cmd.flags &
284                          cpu_to_le16(POWER_FLAGS_POWER_SAVE_ENA_MSK)) ?
285                          0 : 1);
286         pos += scnprintf(buf+pos, bufsz-pos, "skip_dtim_periods = %d\n",
287                          le32_to_cpu(cmd.skip_dtim_periods));
288         pos += scnprintf(buf+pos, bufsz-pos, "power_scheme = %d\n",
289                          iwlmvm_mod_params.power_scheme);
290         pos += scnprintf(buf+pos, bufsz-pos, "flags = 0x%x\n",
291                          le16_to_cpu(cmd.flags));
292         pos += scnprintf(buf+pos, bufsz-pos, "keep_alive = %d\n",
293                          cmd.keep_alive_seconds);
294
295         if (cmd.flags & cpu_to_le16(POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK)) {
296                 pos += scnprintf(buf+pos, bufsz-pos, "skip_over_dtim = %d\n",
297                                  (cmd.flags &
298                                  cpu_to_le16(POWER_FLAGS_SKIP_OVER_DTIM_MSK)) ?
299                                  1 : 0);
300                 pos += scnprintf(buf+pos, bufsz-pos, "rx_data_timeout = %d\n",
301                                  le32_to_cpu(cmd.rx_data_timeout));
302                 pos += scnprintf(buf+pos, bufsz-pos, "tx_data_timeout = %d\n",
303                                  le32_to_cpu(cmd.tx_data_timeout));
304                 if (cmd.flags & cpu_to_le16(POWER_FLAGS_LPRX_ENA_MSK))
305                         pos += scnprintf(buf+pos, bufsz-pos,
306                                          "lprx_rssi_threshold = %d\n",
307                                          le32_to_cpu(cmd.lprx_rssi_threshold));
308         }
309         return pos;
310 }
311 #endif
312
313 const struct iwl_mvm_power_ops pm_legacy_ops = {
314         .power_update_mode = iwl_mvm_power_legacy_update_mode,
315         .power_disable = iwl_mvm_power_legacy_disable,
316 #ifdef CONFIG_IWLWIFI_DEBUGFS
317         .power_dbgfs_read = iwl_mvm_power_legacy_dbgfs_read,
318 #endif
319 };