Merge remote-tracking branches 'asoc/fix/adsp', 'asoc/fix/arizona', 'asoc/fix/atmel...
[linux-drm-fsl-dcu.git] / drivers / net / wireless / iwlwifi / mvm / debugfs.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 #include "mvm.h"
64 #include "sta.h"
65 #include "iwl-io.h"
66
67 struct iwl_dbgfs_mvm_ctx {
68         struct iwl_mvm *mvm;
69         struct ieee80211_vif *vif;
70 };
71
72 static ssize_t iwl_dbgfs_tx_flush_write(struct file *file,
73                                         const char __user *user_buf,
74                                         size_t count, loff_t *ppos)
75 {
76         struct iwl_mvm *mvm = file->private_data;
77
78         char buf[16];
79         int buf_size, ret;
80         u32 scd_q_msk;
81
82         if (!mvm->ucode_loaded || mvm->cur_ucode != IWL_UCODE_REGULAR)
83                 return -EIO;
84
85         memset(buf, 0, sizeof(buf));
86         buf_size = min(count, sizeof(buf) - 1);
87         if (copy_from_user(buf, user_buf, buf_size))
88                 return -EFAULT;
89
90         if (sscanf(buf, "%x", &scd_q_msk) != 1)
91                 return -EINVAL;
92
93         IWL_ERR(mvm, "FLUSHING queues: scd_q_msk = 0x%x\n", scd_q_msk);
94
95         mutex_lock(&mvm->mutex);
96         ret =  iwl_mvm_flush_tx_path(mvm, scd_q_msk, true) ? : count;
97         mutex_unlock(&mvm->mutex);
98
99         return ret;
100 }
101
102 static ssize_t iwl_dbgfs_sta_drain_write(struct file *file,
103                                          const char __user *user_buf,
104                                          size_t count, loff_t *ppos)
105 {
106         struct iwl_mvm *mvm = file->private_data;
107         struct ieee80211_sta *sta;
108
109         char buf[8];
110         int buf_size, sta_id, drain, ret;
111
112         if (!mvm->ucode_loaded || mvm->cur_ucode != IWL_UCODE_REGULAR)
113                 return -EIO;
114
115         memset(buf, 0, sizeof(buf));
116         buf_size = min(count, sizeof(buf) - 1);
117         if (copy_from_user(buf, user_buf, buf_size))
118                 return -EFAULT;
119
120         if (sscanf(buf, "%d %d", &sta_id, &drain) != 2)
121                 return -EINVAL;
122         if (sta_id < 0 || sta_id >= IWL_MVM_STATION_COUNT)
123                 return -EINVAL;
124         if (drain < 0 || drain > 1)
125                 return -EINVAL;
126
127         mutex_lock(&mvm->mutex);
128
129         sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
130                                         lockdep_is_held(&mvm->mutex));
131         if (IS_ERR_OR_NULL(sta))
132                 ret = -ENOENT;
133         else
134                 ret = iwl_mvm_drain_sta(mvm, (void *)sta->drv_priv, drain) ? :
135                         count;
136
137         mutex_unlock(&mvm->mutex);
138
139         return ret;
140 }
141
142 static ssize_t iwl_dbgfs_sram_read(struct file *file, char __user *user_buf,
143                                    size_t count, loff_t *ppos)
144 {
145         struct iwl_mvm *mvm = file->private_data;
146         const struct fw_img *img;
147         int ofs, len, pos = 0;
148         size_t bufsz, ret;
149         char *buf;
150         u8 *ptr;
151
152         if (!mvm->ucode_loaded)
153                 return -EINVAL;
154
155         /* default is to dump the entire data segment */
156         if (!mvm->dbgfs_sram_offset && !mvm->dbgfs_sram_len) {
157                 img = &mvm->fw->img[mvm->cur_ucode];
158                 ofs = img->sec[IWL_UCODE_SECTION_DATA].offset;
159                 len = img->sec[IWL_UCODE_SECTION_DATA].len;
160         } else {
161                 ofs = mvm->dbgfs_sram_offset;
162                 len = mvm->dbgfs_sram_len;
163         }
164
165         bufsz = len * 4 + 256;
166         buf = kzalloc(bufsz, GFP_KERNEL);
167         if (!buf)
168                 return -ENOMEM;
169
170         ptr = kzalloc(len, GFP_KERNEL);
171         if (!ptr) {
172                 kfree(buf);
173                 return -ENOMEM;
174         }
175
176         pos += scnprintf(buf + pos, bufsz - pos, "sram_len: 0x%x\n", len);
177         pos += scnprintf(buf + pos, bufsz - pos, "sram_offset: 0x%x\n", ofs);
178
179         iwl_trans_read_mem_bytes(mvm->trans, ofs, ptr, len);
180         for (ofs = 0; ofs < len; ofs += 16) {
181                 pos += scnprintf(buf + pos, bufsz - pos, "0x%.4x ", ofs);
182                 hex_dump_to_buffer(ptr + ofs, 16, 16, 1, buf + pos,
183                                    bufsz - pos, false);
184                 pos += strlen(buf + pos);
185                 if (bufsz - pos > 0)
186                         buf[pos++] = '\n';
187         }
188
189         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
190
191         kfree(buf);
192         kfree(ptr);
193
194         return ret;
195 }
196
197 static ssize_t iwl_dbgfs_sram_write(struct file *file,
198                                     const char __user *user_buf, size_t count,
199                                     loff_t *ppos)
200 {
201         struct iwl_mvm *mvm = file->private_data;
202         char buf[64];
203         int buf_size;
204         u32 offset, len;
205
206         memset(buf, 0, sizeof(buf));
207         buf_size = min(count, sizeof(buf) -  1);
208         if (copy_from_user(buf, user_buf, buf_size))
209                 return -EFAULT;
210
211         if (sscanf(buf, "%x,%x", &offset, &len) == 2) {
212                 if ((offset & 0x3) || (len & 0x3))
213                         return -EINVAL;
214                 mvm->dbgfs_sram_offset = offset;
215                 mvm->dbgfs_sram_len = len;
216         } else {
217                 mvm->dbgfs_sram_offset = 0;
218                 mvm->dbgfs_sram_len = 0;
219         }
220
221         return count;
222 }
223
224 static ssize_t iwl_dbgfs_stations_read(struct file *file, char __user *user_buf,
225                                        size_t count, loff_t *ppos)
226 {
227         struct iwl_mvm *mvm = file->private_data;
228         struct ieee80211_sta *sta;
229         char buf[400];
230         int i, pos = 0, bufsz = sizeof(buf);
231
232         mutex_lock(&mvm->mutex);
233
234         for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
235                 pos += scnprintf(buf + pos, bufsz - pos, "%.2d: ", i);
236                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
237                                                 lockdep_is_held(&mvm->mutex));
238                 if (!sta)
239                         pos += scnprintf(buf + pos, bufsz - pos, "N/A\n");
240                 else if (IS_ERR(sta))
241                         pos += scnprintf(buf + pos, bufsz - pos, "%ld\n",
242                                          PTR_ERR(sta));
243                 else
244                         pos += scnprintf(buf + pos, bufsz - pos, "%pM\n",
245                                          sta->addr);
246         }
247
248         mutex_unlock(&mvm->mutex);
249
250         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
251 }
252
253 static ssize_t iwl_dbgfs_disable_power_off_read(struct file *file,
254                                                 char __user *user_buf,
255                                                 size_t count, loff_t *ppos)
256 {
257         struct iwl_mvm *mvm = file->private_data;
258         char buf[64];
259         int bufsz = sizeof(buf);
260         int pos = 0;
261
262         pos += scnprintf(buf+pos, bufsz-pos, "disable_power_off_d0=%d\n",
263                          mvm->disable_power_off);
264         pos += scnprintf(buf+pos, bufsz-pos, "disable_power_off_d3=%d\n",
265                          mvm->disable_power_off_d3);
266
267         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
268 }
269
270 static ssize_t iwl_dbgfs_disable_power_off_write(struct file *file,
271                                                  const char __user *user_buf,
272                                                  size_t count, loff_t *ppos)
273 {
274         struct iwl_mvm *mvm = file->private_data;
275         char buf[64] = {};
276         int ret;
277         int val;
278
279         if (!mvm->ucode_loaded)
280                 return -EIO;
281
282         count = min_t(size_t, count, sizeof(buf) - 1);
283         if (copy_from_user(buf, user_buf, count))
284                 return -EFAULT;
285
286         if (!strncmp("disable_power_off_d0=", buf, 21)) {
287                 if (sscanf(buf + 21, "%d", &val) != 1)
288                         return -EINVAL;
289                 mvm->disable_power_off = val;
290         } else if (!strncmp("disable_power_off_d3=", buf, 21)) {
291                 if (sscanf(buf + 21, "%d", &val) != 1)
292                         return -EINVAL;
293                 mvm->disable_power_off_d3 = val;
294         } else {
295                 return -EINVAL;
296         }
297
298         mutex_lock(&mvm->mutex);
299         ret = iwl_mvm_power_update_device_mode(mvm);
300         mutex_unlock(&mvm->mutex);
301
302         return ret ?: count;
303 }
304
305 static void iwl_dbgfs_update_pm(struct iwl_mvm *mvm,
306                                  struct ieee80211_vif *vif,
307                                  enum iwl_dbgfs_pm_mask param, int val)
308 {
309         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
310         struct iwl_dbgfs_pm *dbgfs_pm = &mvmvif->dbgfs_pm;
311
312         dbgfs_pm->mask |= param;
313
314         switch (param) {
315         case MVM_DEBUGFS_PM_KEEP_ALIVE: {
316                 struct ieee80211_hw *hw = mvm->hw;
317                 int dtimper = hw->conf.ps_dtim_period ?: 1;
318                 int dtimper_msec = dtimper * vif->bss_conf.beacon_int;
319
320                 IWL_DEBUG_POWER(mvm, "debugfs: set keep_alive= %d sec\n", val);
321                 if (val * MSEC_PER_SEC < 3 * dtimper_msec) {
322                         IWL_WARN(mvm,
323                                  "debugfs: keep alive period (%ld msec) is less than minimum required (%d msec)\n",
324                                  val * MSEC_PER_SEC, 3 * dtimper_msec);
325                 }
326                 dbgfs_pm->keep_alive_seconds = val;
327                 break;
328         }
329         case MVM_DEBUGFS_PM_SKIP_OVER_DTIM:
330                 IWL_DEBUG_POWER(mvm, "skip_over_dtim %s\n",
331                                 val ? "enabled" : "disabled");
332                 dbgfs_pm->skip_over_dtim = val;
333                 break;
334         case MVM_DEBUGFS_PM_SKIP_DTIM_PERIODS:
335                 IWL_DEBUG_POWER(mvm, "skip_dtim_periods=%d\n", val);
336                 dbgfs_pm->skip_dtim_periods = val;
337                 break;
338         case MVM_DEBUGFS_PM_RX_DATA_TIMEOUT:
339                 IWL_DEBUG_POWER(mvm, "rx_data_timeout=%d\n", val);
340                 dbgfs_pm->rx_data_timeout = val;
341                 break;
342         case MVM_DEBUGFS_PM_TX_DATA_TIMEOUT:
343                 IWL_DEBUG_POWER(mvm, "tx_data_timeout=%d\n", val);
344                 dbgfs_pm->tx_data_timeout = val;
345                 break;
346         case MVM_DEBUGFS_PM_DISABLE_POWER_OFF:
347                 IWL_DEBUG_POWER(mvm, "disable_power_off=%d\n", val);
348                 dbgfs_pm->disable_power_off = val;
349                 break;
350         case MVM_DEBUGFS_PM_LPRX_ENA:
351                 IWL_DEBUG_POWER(mvm, "lprx %s\n", val ? "enabled" : "disabled");
352                 dbgfs_pm->lprx_ena = val;
353                 break;
354         case MVM_DEBUGFS_PM_LPRX_RSSI_THRESHOLD:
355                 IWL_DEBUG_POWER(mvm, "lprx_rssi_threshold=%d\n", val);
356                 dbgfs_pm->lprx_rssi_threshold = val;
357                 break;
358         case MVM_DEBUGFS_PM_SNOOZE_ENABLE:
359                 IWL_DEBUG_POWER(mvm, "snooze_enable=%d\n", val);
360                 dbgfs_pm->snooze_ena = val;
361                 break;
362         }
363 }
364
365 static ssize_t iwl_dbgfs_pm_params_write(struct file *file,
366                                          const char __user *user_buf,
367                                          size_t count, loff_t *ppos)
368 {
369         struct ieee80211_vif *vif = file->private_data;
370         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
371         struct iwl_mvm *mvm = mvmvif->dbgfs_data;
372         enum iwl_dbgfs_pm_mask param;
373         char buf[32] = {};
374         int val;
375         int ret;
376
377         count = min_t(size_t, count, sizeof(buf) - 1);
378         if (copy_from_user(buf, user_buf, count))
379                 return -EFAULT;
380
381         if (!strncmp("keep_alive=", buf, 11)) {
382                 if (sscanf(buf + 11, "%d", &val) != 1)
383                         return -EINVAL;
384                 param = MVM_DEBUGFS_PM_KEEP_ALIVE;
385         } else if (!strncmp("skip_over_dtim=", buf, 15)) {
386                 if (sscanf(buf + 15, "%d", &val) != 1)
387                         return -EINVAL;
388                 param = MVM_DEBUGFS_PM_SKIP_OVER_DTIM;
389         } else if (!strncmp("skip_dtim_periods=", buf, 18)) {
390                 if (sscanf(buf + 18, "%d", &val) != 1)
391                         return -EINVAL;
392                 param = MVM_DEBUGFS_PM_SKIP_DTIM_PERIODS;
393         } else if (!strncmp("rx_data_timeout=", buf, 16)) {
394                 if (sscanf(buf + 16, "%d", &val) != 1)
395                         return -EINVAL;
396                 param = MVM_DEBUGFS_PM_RX_DATA_TIMEOUT;
397         } else if (!strncmp("tx_data_timeout=", buf, 16)) {
398                 if (sscanf(buf + 16, "%d", &val) != 1)
399                         return -EINVAL;
400                 param = MVM_DEBUGFS_PM_TX_DATA_TIMEOUT;
401         } else if (!strncmp("disable_power_off=", buf, 18) &&
402                    !(mvm->fw->ucode_capa.flags &
403                      IWL_UCODE_TLV_FLAGS_DEVICE_PS_CMD)) {
404                 if (sscanf(buf + 18, "%d", &val) != 1)
405                         return -EINVAL;
406                 param = MVM_DEBUGFS_PM_DISABLE_POWER_OFF;
407         } else if (!strncmp("lprx=", buf, 5)) {
408                 if (sscanf(buf + 5, "%d", &val) != 1)
409                         return -EINVAL;
410                 param = MVM_DEBUGFS_PM_LPRX_ENA;
411         } else if (!strncmp("lprx_rssi_threshold=", buf, 20)) {
412                 if (sscanf(buf + 20, "%d", &val) != 1)
413                         return -EINVAL;
414                 if (val > POWER_LPRX_RSSI_THRESHOLD_MAX || val <
415                     POWER_LPRX_RSSI_THRESHOLD_MIN)
416                         return -EINVAL;
417                 param = MVM_DEBUGFS_PM_LPRX_RSSI_THRESHOLD;
418         } else if (!strncmp("snooze_enable=", buf, 14)) {
419                 if (sscanf(buf + 14, "%d", &val) != 1)
420                         return -EINVAL;
421                 param = MVM_DEBUGFS_PM_SNOOZE_ENABLE;
422         } else {
423                 return -EINVAL;
424         }
425
426         mutex_lock(&mvm->mutex);
427         iwl_dbgfs_update_pm(mvm, vif, param, val);
428         ret = iwl_mvm_power_update_mode(mvm, vif);
429         mutex_unlock(&mvm->mutex);
430
431         return ret ?: count;
432 }
433
434 static ssize_t iwl_dbgfs_pm_params_read(struct file *file,
435                                         char __user *user_buf,
436                                         size_t count, loff_t *ppos)
437 {
438         struct ieee80211_vif *vif = file->private_data;
439         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
440         struct iwl_mvm *mvm = mvmvif->dbgfs_data;
441         char buf[512];
442         int bufsz = sizeof(buf);
443         int pos;
444
445         pos = iwl_mvm_power_dbgfs_read(mvm, vif, buf, bufsz);
446
447         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
448 }
449
450 static ssize_t iwl_dbgfs_mac_params_read(struct file *file,
451                                          char __user *user_buf,
452                                          size_t count, loff_t *ppos)
453 {
454         struct ieee80211_vif *vif = file->private_data;
455         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
456         struct iwl_mvm *mvm = mvmvif->dbgfs_data;
457         u8 ap_sta_id;
458         struct ieee80211_chanctx_conf *chanctx_conf;
459         char buf[512];
460         int bufsz = sizeof(buf);
461         int pos = 0;
462         int i;
463
464         mutex_lock(&mvm->mutex);
465
466         ap_sta_id = mvmvif->ap_sta_id;
467
468         pos += scnprintf(buf+pos, bufsz-pos, "mac id/color: %d / %d\n",
469                          mvmvif->id, mvmvif->color);
470         pos += scnprintf(buf+pos, bufsz-pos, "bssid: %pM\n",
471                          vif->bss_conf.bssid);
472         pos += scnprintf(buf+pos, bufsz-pos, "QoS:\n");
473         for (i = 0; i < ARRAY_SIZE(mvmvif->queue_params); i++) {
474                 pos += scnprintf(buf+pos, bufsz-pos,
475                                  "\t%d: txop:%d - cw_min:%d - cw_max = %d - aifs = %d upasd = %d\n",
476                                  i, mvmvif->queue_params[i].txop,
477                                  mvmvif->queue_params[i].cw_min,
478                                  mvmvif->queue_params[i].cw_max,
479                                  mvmvif->queue_params[i].aifs,
480                                  mvmvif->queue_params[i].uapsd);
481         }
482
483         if (vif->type == NL80211_IFTYPE_STATION &&
484             ap_sta_id != IWL_MVM_STATION_COUNT) {
485                 struct ieee80211_sta *sta;
486                 struct iwl_mvm_sta *mvm_sta;
487
488                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[ap_sta_id],
489                                                 lockdep_is_held(&mvm->mutex));
490                 mvm_sta = (void *)sta->drv_priv;
491                 pos += scnprintf(buf+pos, bufsz-pos,
492                                  "ap_sta_id %d - reduced Tx power %d\n",
493                                  ap_sta_id, mvm_sta->bt_reduced_txpower);
494         }
495
496         rcu_read_lock();
497         chanctx_conf = rcu_dereference(vif->chanctx_conf);
498         if (chanctx_conf) {
499                 pos += scnprintf(buf+pos, bufsz-pos,
500                                  "idle rx chains %d, active rx chains: %d\n",
501                                  chanctx_conf->rx_chains_static,
502                                  chanctx_conf->rx_chains_dynamic);
503         }
504         rcu_read_unlock();
505
506         mutex_unlock(&mvm->mutex);
507
508         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
509 }
510
511 #define BT_MBOX_MSG(_notif, _num, _field)                                    \
512         ((le32_to_cpu((_notif)->mbox_msg[(_num)]) & BT_MBOX##_num##_##_field)\
513         >> BT_MBOX##_num##_##_field##_POS)
514
515
516 #define BT_MBOX_PRINT(_num, _field, _end)                                   \
517                         pos += scnprintf(buf + pos, bufsz - pos,            \
518                                          "\t%s: %d%s",                      \
519                                          #_field,                           \
520                                          BT_MBOX_MSG(notif, _num, _field),  \
521                                          true ? "\n" : ", ");
522
523 static ssize_t iwl_dbgfs_bt_notif_read(struct file *file, char __user *user_buf,
524                                        size_t count, loff_t *ppos)
525 {
526         struct iwl_mvm *mvm = file->private_data;
527         struct iwl_bt_coex_profile_notif *notif = &mvm->last_bt_notif;
528         char *buf;
529         int ret, pos = 0, bufsz = sizeof(char) * 1024;
530
531         buf = kmalloc(bufsz, GFP_KERNEL);
532         if (!buf)
533                 return -ENOMEM;
534
535         mutex_lock(&mvm->mutex);
536
537         pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw0:\n");
538
539         BT_MBOX_PRINT(0, LE_SLAVE_LAT, false);
540         BT_MBOX_PRINT(0, LE_PROF1, false);
541         BT_MBOX_PRINT(0, LE_PROF2, false);
542         BT_MBOX_PRINT(0, LE_PROF_OTHER, false);
543         BT_MBOX_PRINT(0, CHL_SEQ_N, false);
544         BT_MBOX_PRINT(0, INBAND_S, false);
545         BT_MBOX_PRINT(0, LE_MIN_RSSI, false);
546         BT_MBOX_PRINT(0, LE_SCAN, false);
547         BT_MBOX_PRINT(0, LE_ADV, false);
548         BT_MBOX_PRINT(0, LE_MAX_TX_POWER, false);
549         BT_MBOX_PRINT(0, OPEN_CON_1, true);
550
551         pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw1:\n");
552
553         BT_MBOX_PRINT(1, BR_MAX_TX_POWER, false);
554         BT_MBOX_PRINT(1, IP_SR, false);
555         BT_MBOX_PRINT(1, LE_MSTR, false);
556         BT_MBOX_PRINT(1, AGGR_TRFC_LD, false);
557         BT_MBOX_PRINT(1, MSG_TYPE, false);
558         BT_MBOX_PRINT(1, SSN, true);
559
560         pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw2:\n");
561
562         BT_MBOX_PRINT(2, SNIFF_ACT, false);
563         BT_MBOX_PRINT(2, PAG, false);
564         BT_MBOX_PRINT(2, INQUIRY, false);
565         BT_MBOX_PRINT(2, CONN, false);
566         BT_MBOX_PRINT(2, SNIFF_INTERVAL, false);
567         BT_MBOX_PRINT(2, DISC, false);
568         BT_MBOX_PRINT(2, SCO_TX_ACT, false);
569         BT_MBOX_PRINT(2, SCO_RX_ACT, false);
570         BT_MBOX_PRINT(2, ESCO_RE_TX, false);
571         BT_MBOX_PRINT(2, SCO_DURATION, true);
572
573         pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw3:\n");
574
575         BT_MBOX_PRINT(3, SCO_STATE, false);
576         BT_MBOX_PRINT(3, SNIFF_STATE, false);
577         BT_MBOX_PRINT(3, A2DP_STATE, false);
578         BT_MBOX_PRINT(3, ACL_STATE, false);
579         BT_MBOX_PRINT(3, MSTR_STATE, false);
580         BT_MBOX_PRINT(3, OBX_STATE, false);
581         BT_MBOX_PRINT(3, OPEN_CON_2, false);
582         BT_MBOX_PRINT(3, TRAFFIC_LOAD, false);
583         BT_MBOX_PRINT(3, CHL_SEQN_LSB, false);
584         BT_MBOX_PRINT(3, INBAND_P, false);
585         BT_MBOX_PRINT(3, MSG_TYPE_2, false);
586         BT_MBOX_PRINT(3, SSN_2, false);
587         BT_MBOX_PRINT(3, UPDATE_REQUEST, true);
588
589         pos += scnprintf(buf+pos, bufsz-pos, "bt_status = %d\n",
590                          notif->bt_status);
591         pos += scnprintf(buf+pos, bufsz-pos, "bt_open_conn = %d\n",
592                          notif->bt_open_conn);
593         pos += scnprintf(buf+pos, bufsz-pos, "bt_traffic_load = %d\n",
594                          notif->bt_traffic_load);
595         pos += scnprintf(buf+pos, bufsz-pos, "bt_agg_traffic_load = %d\n",
596                          notif->bt_agg_traffic_load);
597         pos += scnprintf(buf+pos, bufsz-pos, "bt_ci_compliance = %d\n",
598                          notif->bt_ci_compliance);
599         pos += scnprintf(buf+pos, bufsz-pos, "primary_ch_lut = %d\n",
600                          le32_to_cpu(notif->primary_ch_lut));
601         pos += scnprintf(buf+pos, bufsz-pos, "secondary_ch_lut = %d\n",
602                          le32_to_cpu(notif->secondary_ch_lut));
603         pos += scnprintf(buf+pos, bufsz-pos, "bt_activity_grading = %d\n",
604                          le32_to_cpu(notif->bt_activity_grading));
605
606         mutex_unlock(&mvm->mutex);
607
608         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
609         kfree(buf);
610
611         return ret;
612 }
613 #undef BT_MBOX_PRINT
614
615 static ssize_t iwl_dbgfs_bt_cmd_read(struct file *file, char __user *user_buf,
616                                      size_t count, loff_t *ppos)
617 {
618         struct iwl_mvm *mvm = file->private_data;
619         struct iwl_bt_coex_ci_cmd *cmd = &mvm->last_bt_ci_cmd;
620         char buf[256];
621         int bufsz = sizeof(buf);
622         int pos = 0;
623
624         mutex_lock(&mvm->mutex);
625
626         pos += scnprintf(buf+pos, bufsz-pos, "Channel inhibition CMD\n");
627         pos += scnprintf(buf+pos, bufsz-pos,
628                        "\tPrimary Channel Bitmap 0x%016llx Fat: %d\n",
629                        le64_to_cpu(cmd->bt_primary_ci),
630                        !!cmd->co_run_bw_primary);
631         pos += scnprintf(buf+pos, bufsz-pos,
632                        "\tSecondary Channel Bitmap 0x%016llx Fat: %d\n",
633                        le64_to_cpu(cmd->bt_secondary_ci),
634                        !!cmd->co_run_bw_secondary);
635
636         pos += scnprintf(buf+pos, bufsz-pos, "BT Configuration CMD\n");
637         pos += scnprintf(buf+pos, bufsz-pos, "\tACK Kill Mask 0x%08x\n",
638                          iwl_bt_ack_kill_msk[mvm->bt_kill_msk]);
639         pos += scnprintf(buf+pos, bufsz-pos, "\tCTS Kill Mask 0x%08x\n",
640                          iwl_bt_cts_kill_msk[mvm->bt_kill_msk]);
641
642         mutex_unlock(&mvm->mutex);
643
644         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
645 }
646
647 #define PRINT_STATS_LE32(_str, _val)                                    \
648                          pos += scnprintf(buf + pos, bufsz - pos,       \
649                                           fmt_table, _str,              \
650                                           le32_to_cpu(_val))
651
652 static ssize_t iwl_dbgfs_fw_rx_stats_read(struct file *file,
653                                           char __user *user_buf, size_t count,
654                                           loff_t *ppos)
655 {
656         struct iwl_mvm *mvm = file->private_data;
657         static const char *fmt_table = "\t%-30s %10u\n";
658         static const char *fmt_header = "%-32s\n";
659         int pos = 0;
660         char *buf;
661         int ret;
662         /* 43 is the size of each data line, 33 is the size of each header */
663         size_t bufsz =
664                 ((sizeof(struct mvm_statistics_rx) / sizeof(__le32)) * 43) +
665                 (4 * 33) + 1;
666
667         struct mvm_statistics_rx_phy *ofdm;
668         struct mvm_statistics_rx_phy *cck;
669         struct mvm_statistics_rx_non_phy *general;
670         struct mvm_statistics_rx_ht_phy *ht;
671
672         buf = kzalloc(bufsz, GFP_KERNEL);
673         if (!buf)
674                 return -ENOMEM;
675
676         mutex_lock(&mvm->mutex);
677
678         ofdm = &mvm->rx_stats.ofdm;
679         cck = &mvm->rx_stats.cck;
680         general = &mvm->rx_stats.general;
681         ht = &mvm->rx_stats.ofdm_ht;
682
683         pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
684                          "Statistics_Rx - OFDM");
685         PRINT_STATS_LE32("ina_cnt", ofdm->ina_cnt);
686         PRINT_STATS_LE32("fina_cnt", ofdm->fina_cnt);
687         PRINT_STATS_LE32("plcp_err", ofdm->plcp_err);
688         PRINT_STATS_LE32("crc32_err", ofdm->crc32_err);
689         PRINT_STATS_LE32("overrun_err", ofdm->overrun_err);
690         PRINT_STATS_LE32("early_overrun_err", ofdm->early_overrun_err);
691         PRINT_STATS_LE32("crc32_good", ofdm->crc32_good);
692         PRINT_STATS_LE32("false_alarm_cnt", ofdm->false_alarm_cnt);
693         PRINT_STATS_LE32("fina_sync_err_cnt", ofdm->fina_sync_err_cnt);
694         PRINT_STATS_LE32("sfd_timeout", ofdm->sfd_timeout);
695         PRINT_STATS_LE32("fina_timeout", ofdm->fina_timeout);
696         PRINT_STATS_LE32("unresponded_rts", ofdm->unresponded_rts);
697         PRINT_STATS_LE32("rxe_frame_lmt_overrun",
698                          ofdm->rxe_frame_limit_overrun);
699         PRINT_STATS_LE32("sent_ack_cnt", ofdm->sent_ack_cnt);
700         PRINT_STATS_LE32("sent_cts_cnt", ofdm->sent_cts_cnt);
701         PRINT_STATS_LE32("sent_ba_rsp_cnt", ofdm->sent_ba_rsp_cnt);
702         PRINT_STATS_LE32("dsp_self_kill", ofdm->dsp_self_kill);
703         PRINT_STATS_LE32("mh_format_err", ofdm->mh_format_err);
704         PRINT_STATS_LE32("re_acq_main_rssi_sum", ofdm->re_acq_main_rssi_sum);
705         PRINT_STATS_LE32("reserved", ofdm->reserved);
706
707         pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
708                          "Statistics_Rx - CCK");
709         PRINT_STATS_LE32("ina_cnt", cck->ina_cnt);
710         PRINT_STATS_LE32("fina_cnt", cck->fina_cnt);
711         PRINT_STATS_LE32("plcp_err", cck->plcp_err);
712         PRINT_STATS_LE32("crc32_err", cck->crc32_err);
713         PRINT_STATS_LE32("overrun_err", cck->overrun_err);
714         PRINT_STATS_LE32("early_overrun_err", cck->early_overrun_err);
715         PRINT_STATS_LE32("crc32_good", cck->crc32_good);
716         PRINT_STATS_LE32("false_alarm_cnt", cck->false_alarm_cnt);
717         PRINT_STATS_LE32("fina_sync_err_cnt", cck->fina_sync_err_cnt);
718         PRINT_STATS_LE32("sfd_timeout", cck->sfd_timeout);
719         PRINT_STATS_LE32("fina_timeout", cck->fina_timeout);
720         PRINT_STATS_LE32("unresponded_rts", cck->unresponded_rts);
721         PRINT_STATS_LE32("rxe_frame_lmt_overrun",
722                          cck->rxe_frame_limit_overrun);
723         PRINT_STATS_LE32("sent_ack_cnt", cck->sent_ack_cnt);
724         PRINT_STATS_LE32("sent_cts_cnt", cck->sent_cts_cnt);
725         PRINT_STATS_LE32("sent_ba_rsp_cnt", cck->sent_ba_rsp_cnt);
726         PRINT_STATS_LE32("dsp_self_kill", cck->dsp_self_kill);
727         PRINT_STATS_LE32("mh_format_err", cck->mh_format_err);
728         PRINT_STATS_LE32("re_acq_main_rssi_sum", cck->re_acq_main_rssi_sum);
729         PRINT_STATS_LE32("reserved", cck->reserved);
730
731         pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
732                          "Statistics_Rx - GENERAL");
733         PRINT_STATS_LE32("bogus_cts", general->bogus_cts);
734         PRINT_STATS_LE32("bogus_ack", general->bogus_ack);
735         PRINT_STATS_LE32("non_bssid_frames", general->non_bssid_frames);
736         PRINT_STATS_LE32("filtered_frames", general->filtered_frames);
737         PRINT_STATS_LE32("non_channel_beacons", general->non_channel_beacons);
738         PRINT_STATS_LE32("channel_beacons", general->channel_beacons);
739         PRINT_STATS_LE32("num_missed_bcon", general->num_missed_bcon);
740         PRINT_STATS_LE32("adc_rx_saturation_time",
741                          general->adc_rx_saturation_time);
742         PRINT_STATS_LE32("ina_detection_search_time",
743                          general->ina_detection_search_time);
744         PRINT_STATS_LE32("beacon_silence_rssi_a",
745                          general->beacon_silence_rssi_a);
746         PRINT_STATS_LE32("beacon_silence_rssi_b",
747                          general->beacon_silence_rssi_b);
748         PRINT_STATS_LE32("beacon_silence_rssi_c",
749                          general->beacon_silence_rssi_c);
750         PRINT_STATS_LE32("interference_data_flag",
751                          general->interference_data_flag);
752         PRINT_STATS_LE32("channel_load", general->channel_load);
753         PRINT_STATS_LE32("dsp_false_alarms", general->dsp_false_alarms);
754         PRINT_STATS_LE32("beacon_rssi_a", general->beacon_rssi_a);
755         PRINT_STATS_LE32("beacon_rssi_b", general->beacon_rssi_b);
756         PRINT_STATS_LE32("beacon_rssi_c", general->beacon_rssi_c);
757         PRINT_STATS_LE32("beacon_energy_a", general->beacon_energy_a);
758         PRINT_STATS_LE32("beacon_energy_b", general->beacon_energy_b);
759         PRINT_STATS_LE32("beacon_energy_c", general->beacon_energy_c);
760         PRINT_STATS_LE32("num_bt_kills", general->num_bt_kills);
761         PRINT_STATS_LE32("mac_id", general->mac_id);
762         PRINT_STATS_LE32("directed_data_mpdu", general->directed_data_mpdu);
763
764         pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
765                          "Statistics_Rx - HT");
766         PRINT_STATS_LE32("plcp_err", ht->plcp_err);
767         PRINT_STATS_LE32("overrun_err", ht->overrun_err);
768         PRINT_STATS_LE32("early_overrun_err", ht->early_overrun_err);
769         PRINT_STATS_LE32("crc32_good", ht->crc32_good);
770         PRINT_STATS_LE32("crc32_err", ht->crc32_err);
771         PRINT_STATS_LE32("mh_format_err", ht->mh_format_err);
772         PRINT_STATS_LE32("agg_crc32_good", ht->agg_crc32_good);
773         PRINT_STATS_LE32("agg_mpdu_cnt", ht->agg_mpdu_cnt);
774         PRINT_STATS_LE32("agg_cnt", ht->agg_cnt);
775         PRINT_STATS_LE32("unsupport_mcs", ht->unsupport_mcs);
776
777         mutex_unlock(&mvm->mutex);
778
779         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
780         kfree(buf);
781
782         return ret;
783 }
784 #undef PRINT_STAT_LE32
785
786 static ssize_t iwl_dbgfs_fw_restart_write(struct file *file,
787                                           const char __user *user_buf,
788                                           size_t count, loff_t *ppos)
789 {
790         struct iwl_mvm *mvm = file->private_data;
791         int ret;
792
793         mutex_lock(&mvm->mutex);
794
795         /* allow one more restart that we're provoking here */
796         if (mvm->restart_fw >= 0)
797                 mvm->restart_fw++;
798
799         /* take the return value to make compiler happy - it will fail anyway */
800         ret = iwl_mvm_send_cmd_pdu(mvm, REPLY_ERROR, CMD_SYNC, 0, NULL);
801
802         mutex_unlock(&mvm->mutex);
803
804         return count;
805 }
806
807 static ssize_t
808 iwl_dbgfs_scan_ant_rxchain_read(struct file *file,
809                                 char __user *user_buf,
810                                 size_t count, loff_t *ppos)
811 {
812         struct iwl_mvm *mvm = file->private_data;
813         int pos = 0;
814         char buf[32];
815         const size_t bufsz = sizeof(buf);
816
817         /* print which antennas were set for the scan command by the user */
818         pos += scnprintf(buf + pos, bufsz - pos, "Antennas for scan: ");
819         if (mvm->scan_rx_ant & ANT_A)
820                 pos += scnprintf(buf + pos, bufsz - pos, "A");
821         if (mvm->scan_rx_ant & ANT_B)
822                 pos += scnprintf(buf + pos, bufsz - pos, "B");
823         if (mvm->scan_rx_ant & ANT_C)
824                 pos += scnprintf(buf + pos, bufsz - pos, "C");
825         pos += scnprintf(buf + pos, bufsz - pos, " (%hhx)\n", mvm->scan_rx_ant);
826
827         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
828 }
829
830 static ssize_t
831 iwl_dbgfs_scan_ant_rxchain_write(struct file *file,
832                                  const char __user *user_buf,
833                                  size_t count, loff_t *ppos)
834 {
835         struct iwl_mvm *mvm = file->private_data;
836         char buf[8];
837         int buf_size;
838         u8 scan_rx_ant;
839
840         memset(buf, 0, sizeof(buf));
841         buf_size = min(count, sizeof(buf) - 1);
842
843         /* get the argument from the user and check if it is valid */
844         if (copy_from_user(buf, user_buf, buf_size))
845                 return -EFAULT;
846         if (sscanf(buf, "%hhx", &scan_rx_ant) != 1)
847                 return -EINVAL;
848         if (scan_rx_ant > ANT_ABC)
849                 return -EINVAL;
850         if (scan_rx_ant & ~iwl_fw_valid_rx_ant(mvm->fw))
851                 return -EINVAL;
852
853         /* change the rx antennas for scan command */
854         mvm->scan_rx_ant = scan_rx_ant;
855
856         return count;
857 }
858
859
860 static void iwl_dbgfs_update_bf(struct ieee80211_vif *vif,
861                                 enum iwl_dbgfs_bf_mask param, int value)
862 {
863         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
864         struct iwl_dbgfs_bf *dbgfs_bf = &mvmvif->dbgfs_bf;
865
866         dbgfs_bf->mask |= param;
867
868         switch (param) {
869         case MVM_DEBUGFS_BF_ENERGY_DELTA:
870                 dbgfs_bf->bf_energy_delta = value;
871                 break;
872         case MVM_DEBUGFS_BF_ROAMING_ENERGY_DELTA:
873                 dbgfs_bf->bf_roaming_energy_delta = value;
874                 break;
875         case MVM_DEBUGFS_BF_ROAMING_STATE:
876                 dbgfs_bf->bf_roaming_state = value;
877                 break;
878         case MVM_DEBUGFS_BF_TEMP_THRESHOLD:
879                 dbgfs_bf->bf_temp_threshold = value;
880                 break;
881         case MVM_DEBUGFS_BF_TEMP_FAST_FILTER:
882                 dbgfs_bf->bf_temp_fast_filter = value;
883                 break;
884         case MVM_DEBUGFS_BF_TEMP_SLOW_FILTER:
885                 dbgfs_bf->bf_temp_slow_filter = value;
886                 break;
887         case MVM_DEBUGFS_BF_ENABLE_BEACON_FILTER:
888                 dbgfs_bf->bf_enable_beacon_filter = value;
889                 break;
890         case MVM_DEBUGFS_BF_DEBUG_FLAG:
891                 dbgfs_bf->bf_debug_flag = value;
892                 break;
893         case MVM_DEBUGFS_BF_ESCAPE_TIMER:
894                 dbgfs_bf->bf_escape_timer = value;
895                 break;
896         case MVM_DEBUGFS_BA_ENABLE_BEACON_ABORT:
897                 dbgfs_bf->ba_enable_beacon_abort = value;
898                 break;
899         case MVM_DEBUGFS_BA_ESCAPE_TIMER:
900                 dbgfs_bf->ba_escape_timer = value;
901                 break;
902         }
903 }
904
905 static ssize_t iwl_dbgfs_bf_params_write(struct file *file,
906                                          const char __user *user_buf,
907                                          size_t count, loff_t *ppos)
908 {
909         struct ieee80211_vif *vif = file->private_data;
910         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
911         struct iwl_mvm *mvm = mvmvif->dbgfs_data;
912         enum iwl_dbgfs_bf_mask param;
913         char buf[256];
914         int buf_size;
915         int value;
916         int ret = 0;
917
918         memset(buf, 0, sizeof(buf));
919         buf_size = min(count, sizeof(buf) - 1);
920         if (copy_from_user(buf, user_buf, buf_size))
921                 return -EFAULT;
922
923         if (!strncmp("bf_energy_delta=", buf, 16)) {
924                 if (sscanf(buf+16, "%d", &value) != 1)
925                         return -EINVAL;
926                 if (value < IWL_BF_ENERGY_DELTA_MIN ||
927                     value > IWL_BF_ENERGY_DELTA_MAX)
928                         return -EINVAL;
929                 param = MVM_DEBUGFS_BF_ENERGY_DELTA;
930         } else if (!strncmp("bf_roaming_energy_delta=", buf, 24)) {
931                 if (sscanf(buf+24, "%d", &value) != 1)
932                         return -EINVAL;
933                 if (value < IWL_BF_ROAMING_ENERGY_DELTA_MIN ||
934                     value > IWL_BF_ROAMING_ENERGY_DELTA_MAX)
935                         return -EINVAL;
936                 param = MVM_DEBUGFS_BF_ROAMING_ENERGY_DELTA;
937         } else if (!strncmp("bf_roaming_state=", buf, 17)) {
938                 if (sscanf(buf+17, "%d", &value) != 1)
939                         return -EINVAL;
940                 if (value < IWL_BF_ROAMING_STATE_MIN ||
941                     value > IWL_BF_ROAMING_STATE_MAX)
942                         return -EINVAL;
943                 param = MVM_DEBUGFS_BF_ROAMING_STATE;
944         } else if (!strncmp("bf_temp_threshold=", buf, 18)) {
945                 if (sscanf(buf+18, "%d", &value) != 1)
946                         return -EINVAL;
947                 if (value < IWL_BF_TEMP_THRESHOLD_MIN ||
948                     value > IWL_BF_TEMP_THRESHOLD_MAX)
949                         return -EINVAL;
950                 param = MVM_DEBUGFS_BF_TEMP_THRESHOLD;
951         } else if (!strncmp("bf_temp_fast_filter=", buf, 20)) {
952                 if (sscanf(buf+20, "%d", &value) != 1)
953                         return -EINVAL;
954                 if (value < IWL_BF_TEMP_FAST_FILTER_MIN ||
955                     value > IWL_BF_TEMP_FAST_FILTER_MAX)
956                         return -EINVAL;
957                 param = MVM_DEBUGFS_BF_TEMP_FAST_FILTER;
958         } else if (!strncmp("bf_temp_slow_filter=", buf, 20)) {
959                 if (sscanf(buf+20, "%d", &value) != 1)
960                         return -EINVAL;
961                 if (value < IWL_BF_TEMP_SLOW_FILTER_MIN ||
962                     value > IWL_BF_TEMP_SLOW_FILTER_MAX)
963                         return -EINVAL;
964                 param = MVM_DEBUGFS_BF_TEMP_SLOW_FILTER;
965         } else if (!strncmp("bf_enable_beacon_filter=", buf, 24)) {
966                 if (sscanf(buf+24, "%d", &value) != 1)
967                         return -EINVAL;
968                 if (value < 0 || value > 1)
969                         return -EINVAL;
970                 param = MVM_DEBUGFS_BF_ENABLE_BEACON_FILTER;
971         } else if (!strncmp("bf_debug_flag=", buf, 14)) {
972                 if (sscanf(buf+14, "%d", &value) != 1)
973                         return -EINVAL;
974                 if (value < 0 || value > 1)
975                         return -EINVAL;
976                 param = MVM_DEBUGFS_BF_DEBUG_FLAG;
977         } else if (!strncmp("bf_escape_timer=", buf, 16)) {
978                 if (sscanf(buf+16, "%d", &value) != 1)
979                         return -EINVAL;
980                 if (value < IWL_BF_ESCAPE_TIMER_MIN ||
981                     value > IWL_BF_ESCAPE_TIMER_MAX)
982                         return -EINVAL;
983                 param = MVM_DEBUGFS_BF_ESCAPE_TIMER;
984         } else if (!strncmp("ba_escape_timer=", buf, 16)) {
985                 if (sscanf(buf+16, "%d", &value) != 1)
986                         return -EINVAL;
987                 if (value < IWL_BA_ESCAPE_TIMER_MIN ||
988                     value > IWL_BA_ESCAPE_TIMER_MAX)
989                         return -EINVAL;
990                 param = MVM_DEBUGFS_BA_ESCAPE_TIMER;
991         } else if (!strncmp("ba_enable_beacon_abort=", buf, 23)) {
992                 if (sscanf(buf+23, "%d", &value) != 1)
993                         return -EINVAL;
994                 if (value < 0 || value > 1)
995                         return -EINVAL;
996                 param = MVM_DEBUGFS_BA_ENABLE_BEACON_ABORT;
997         } else {
998                 return -EINVAL;
999         }
1000
1001         mutex_lock(&mvm->mutex);
1002         iwl_dbgfs_update_bf(vif, param, value);
1003         if (param == MVM_DEBUGFS_BF_ENABLE_BEACON_FILTER && !value) {
1004                 ret = iwl_mvm_disable_beacon_filter(mvm, vif);
1005         } else {
1006                 ret = iwl_mvm_enable_beacon_filter(mvm, vif);
1007         }
1008         mutex_unlock(&mvm->mutex);
1009
1010         return ret ?: count;
1011 }
1012
1013 static ssize_t iwl_dbgfs_bf_params_read(struct file *file,
1014                                         char __user *user_buf,
1015                                         size_t count, loff_t *ppos)
1016 {
1017         struct ieee80211_vif *vif = file->private_data;
1018         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1019         char buf[256];
1020         int pos = 0;
1021         const size_t bufsz = sizeof(buf);
1022         struct iwl_beacon_filter_cmd cmd = {
1023                 IWL_BF_CMD_CONFIG_DEFAULTS,
1024                 .bf_enable_beacon_filter =
1025                         cpu_to_le32(IWL_BF_ENABLE_BEACON_FILTER_DEFAULT),
1026                 .ba_enable_beacon_abort =
1027                         cpu_to_le32(IWL_BA_ENABLE_BEACON_ABORT_DEFAULT),
1028         };
1029
1030         iwl_mvm_beacon_filter_debugfs_parameters(vif, &cmd);
1031         if (mvmvif->bf_data.bf_enabled)
1032                 cmd.bf_enable_beacon_filter = cpu_to_le32(1);
1033         else
1034                 cmd.bf_enable_beacon_filter = 0;
1035
1036         pos += scnprintf(buf+pos, bufsz-pos, "bf_energy_delta = %d\n",
1037                          le32_to_cpu(cmd.bf_energy_delta));
1038         pos += scnprintf(buf+pos, bufsz-pos, "bf_roaming_energy_delta = %d\n",
1039                          le32_to_cpu(cmd.bf_roaming_energy_delta));
1040         pos += scnprintf(buf+pos, bufsz-pos, "bf_roaming_state = %d\n",
1041                          le32_to_cpu(cmd.bf_roaming_state));
1042         pos += scnprintf(buf+pos, bufsz-pos, "bf_temp_threshold = %d\n",
1043                          le32_to_cpu(cmd.bf_temp_threshold));
1044         pos += scnprintf(buf+pos, bufsz-pos, "bf_temp_fast_filter = %d\n",
1045                          le32_to_cpu(cmd.bf_temp_fast_filter));
1046         pos += scnprintf(buf+pos, bufsz-pos, "bf_temp_slow_filter = %d\n",
1047                          le32_to_cpu(cmd.bf_temp_slow_filter));
1048         pos += scnprintf(buf+pos, bufsz-pos, "bf_enable_beacon_filter = %d\n",
1049                          le32_to_cpu(cmd.bf_enable_beacon_filter));
1050         pos += scnprintf(buf+pos, bufsz-pos, "bf_debug_flag = %d\n",
1051                          le32_to_cpu(cmd.bf_debug_flag));
1052         pos += scnprintf(buf+pos, bufsz-pos, "bf_escape_timer = %d\n",
1053                          le32_to_cpu(cmd.bf_escape_timer));
1054         pos += scnprintf(buf+pos, bufsz-pos, "ba_escape_timer = %d\n",
1055                          le32_to_cpu(cmd.ba_escape_timer));
1056         pos += scnprintf(buf+pos, bufsz-pos, "ba_enable_beacon_abort = %d\n",
1057                          le32_to_cpu(cmd.ba_enable_beacon_abort));
1058
1059         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1060 }
1061
1062 #ifdef CONFIG_PM_SLEEP
1063 static ssize_t iwl_dbgfs_d3_sram_write(struct file *file,
1064                                        const char __user *user_buf,
1065                                        size_t count, loff_t *ppos)
1066 {
1067         struct iwl_mvm *mvm = file->private_data;
1068         char buf[8] = {};
1069         int store;
1070
1071         count = min_t(size_t, count, sizeof(buf) - 1);
1072         if (copy_from_user(buf, user_buf, count))
1073                 return -EFAULT;
1074
1075         if (sscanf(buf, "%d", &store) != 1)
1076                 return -EINVAL;
1077
1078         mvm->store_d3_resume_sram = store;
1079
1080         return count;
1081 }
1082
1083 static ssize_t iwl_dbgfs_d3_sram_read(struct file *file, char __user *user_buf,
1084                                       size_t count, loff_t *ppos)
1085 {
1086         struct iwl_mvm *mvm = file->private_data;
1087         const struct fw_img *img;
1088         int ofs, len, pos = 0;
1089         size_t bufsz, ret;
1090         char *buf;
1091         u8 *ptr = mvm->d3_resume_sram;
1092
1093         img = &mvm->fw->img[IWL_UCODE_WOWLAN];
1094         len = img->sec[IWL_UCODE_SECTION_DATA].len;
1095
1096         bufsz = len * 4 + 256;
1097         buf = kzalloc(bufsz, GFP_KERNEL);
1098         if (!buf)
1099                 return -ENOMEM;
1100
1101         pos += scnprintf(buf, bufsz, "D3 SRAM capture: %sabled\n",
1102                          mvm->store_d3_resume_sram ? "en" : "dis");
1103
1104         if (ptr) {
1105                 for (ofs = 0; ofs < len; ofs += 16) {
1106                         pos += scnprintf(buf + pos, bufsz - pos,
1107                                          "0x%.4x ", ofs);
1108                         hex_dump_to_buffer(ptr + ofs, 16, 16, 1, buf + pos,
1109                                            bufsz - pos, false);
1110                         pos += strlen(buf + pos);
1111                         if (bufsz - pos > 0)
1112                                 buf[pos++] = '\n';
1113                 }
1114         } else {
1115                 pos += scnprintf(buf + pos, bufsz - pos,
1116                                  "(no data captured)\n");
1117         }
1118
1119         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1120
1121         kfree(buf);
1122
1123         return ret;
1124 }
1125 #endif
1126
1127 #define MVM_DEBUGFS_READ_FILE_OPS(name)                                 \
1128 static const struct file_operations iwl_dbgfs_##name##_ops = {  \
1129         .read = iwl_dbgfs_##name##_read,                                \
1130         .open = simple_open,                                            \
1131         .llseek = generic_file_llseek,                                  \
1132 }
1133
1134 #define MVM_DEBUGFS_READ_WRITE_FILE_OPS(name)                           \
1135 static const struct file_operations iwl_dbgfs_##name##_ops = {  \
1136         .write = iwl_dbgfs_##name##_write,                              \
1137         .read = iwl_dbgfs_##name##_read,                                \
1138         .open = simple_open,                                            \
1139         .llseek = generic_file_llseek,                                  \
1140 };
1141
1142 #define MVM_DEBUGFS_WRITE_FILE_OPS(name)                                \
1143 static const struct file_operations iwl_dbgfs_##name##_ops = {  \
1144         .write = iwl_dbgfs_##name##_write,                              \
1145         .open = simple_open,                                            \
1146         .llseek = generic_file_llseek,                                  \
1147 };
1148
1149 #define MVM_DEBUGFS_ADD_FILE(name, parent, mode) do {                   \
1150                 if (!debugfs_create_file(#name, mode, parent, mvm,      \
1151                                          &iwl_dbgfs_##name##_ops))      \
1152                         goto err;                                       \
1153         } while (0)
1154
1155 #define MVM_DEBUGFS_ADD_FILE_VIF(name, parent, mode) do {               \
1156                 if (!debugfs_create_file(#name, mode, parent, vif,      \
1157                                          &iwl_dbgfs_##name##_ops))      \
1158                         goto err;                                       \
1159         } while (0)
1160
1161 /* Device wide debugfs entries */
1162 MVM_DEBUGFS_WRITE_FILE_OPS(tx_flush);
1163 MVM_DEBUGFS_WRITE_FILE_OPS(sta_drain);
1164 MVM_DEBUGFS_READ_WRITE_FILE_OPS(sram);
1165 MVM_DEBUGFS_READ_FILE_OPS(stations);
1166 MVM_DEBUGFS_READ_FILE_OPS(bt_notif);
1167 MVM_DEBUGFS_READ_FILE_OPS(bt_cmd);
1168 MVM_DEBUGFS_READ_WRITE_FILE_OPS(disable_power_off);
1169 MVM_DEBUGFS_READ_FILE_OPS(fw_rx_stats);
1170 MVM_DEBUGFS_WRITE_FILE_OPS(fw_restart);
1171 MVM_DEBUGFS_READ_WRITE_FILE_OPS(scan_ant_rxchain);
1172
1173 #ifdef CONFIG_PM_SLEEP
1174 MVM_DEBUGFS_READ_WRITE_FILE_OPS(d3_sram);
1175 #endif
1176
1177 /* Interface specific debugfs entries */
1178 MVM_DEBUGFS_READ_FILE_OPS(mac_params);
1179 MVM_DEBUGFS_READ_WRITE_FILE_OPS(pm_params);
1180 MVM_DEBUGFS_READ_WRITE_FILE_OPS(bf_params);
1181
1182 int iwl_mvm_dbgfs_register(struct iwl_mvm *mvm, struct dentry *dbgfs_dir)
1183 {
1184         char buf[100];
1185
1186         mvm->debugfs_dir = dbgfs_dir;
1187
1188         MVM_DEBUGFS_ADD_FILE(tx_flush, mvm->debugfs_dir, S_IWUSR);
1189         MVM_DEBUGFS_ADD_FILE(sta_drain, mvm->debugfs_dir, S_IWUSR);
1190         MVM_DEBUGFS_ADD_FILE(sram, mvm->debugfs_dir, S_IWUSR | S_IRUSR);
1191         MVM_DEBUGFS_ADD_FILE(stations, dbgfs_dir, S_IRUSR);
1192         MVM_DEBUGFS_ADD_FILE(bt_notif, dbgfs_dir, S_IRUSR);
1193         MVM_DEBUGFS_ADD_FILE(bt_cmd, dbgfs_dir, S_IRUSR);
1194         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_DEVICE_PS_CMD)
1195                 MVM_DEBUGFS_ADD_FILE(disable_power_off, mvm->debugfs_dir,
1196                                      S_IRUSR | S_IWUSR);
1197         MVM_DEBUGFS_ADD_FILE(fw_rx_stats, mvm->debugfs_dir, S_IRUSR);
1198         MVM_DEBUGFS_ADD_FILE(fw_restart, mvm->debugfs_dir, S_IWUSR);
1199         MVM_DEBUGFS_ADD_FILE(scan_ant_rxchain, mvm->debugfs_dir,
1200                              S_IWUSR | S_IRUSR);
1201 #ifdef CONFIG_PM_SLEEP
1202         MVM_DEBUGFS_ADD_FILE(d3_sram, mvm->debugfs_dir, S_IRUSR | S_IWUSR);
1203         MVM_DEBUGFS_ADD_FILE(d3_test, mvm->debugfs_dir, S_IRUSR);
1204         if (!debugfs_create_bool("d3_wake_sysassert", S_IRUSR | S_IWUSR,
1205                                  mvm->debugfs_dir, &mvm->d3_wake_sysassert))
1206                 goto err;
1207 #endif
1208
1209         /*
1210          * Create a symlink with mac80211. It will be removed when mac80211
1211          * exists (before the opmode exists which removes the target.)
1212          */
1213         snprintf(buf, 100, "../../%s/%s",
1214                  dbgfs_dir->d_parent->d_parent->d_name.name,
1215                  dbgfs_dir->d_parent->d_name.name);
1216         if (!debugfs_create_symlink("iwlwifi", mvm->hw->wiphy->debugfsdir, buf))
1217                 goto err;
1218
1219         return 0;
1220 err:
1221         IWL_ERR(mvm, "Can't create the mvm debugfs directory\n");
1222         return -ENOMEM;
1223 }
1224
1225 void iwl_mvm_vif_dbgfs_register(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1226 {
1227         struct dentry *dbgfs_dir = vif->debugfs_dir;
1228         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1229         char buf[100];
1230
1231         /*
1232          * Check if debugfs directory already exist before creating it.
1233          * This may happen when, for example, resetting hw or suspend-resume
1234          */
1235         if (!dbgfs_dir || mvmvif->dbgfs_dir)
1236                 return;
1237
1238         mvmvif->dbgfs_dir = debugfs_create_dir("iwlmvm", dbgfs_dir);
1239         mvmvif->dbgfs_data = mvm;
1240
1241         if (!mvmvif->dbgfs_dir) {
1242                 IWL_ERR(mvm, "Failed to create debugfs directory under %s\n",
1243                         dbgfs_dir->d_name.name);
1244                 return;
1245         }
1246
1247         if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM &&
1248             vif->type == NL80211_IFTYPE_STATION && !vif->p2p)
1249                 MVM_DEBUGFS_ADD_FILE_VIF(pm_params, mvmvif->dbgfs_dir, S_IWUSR |
1250                                          S_IRUSR);
1251
1252         MVM_DEBUGFS_ADD_FILE_VIF(mac_params, mvmvif->dbgfs_dir,
1253                                  S_IRUSR);
1254
1255         if (vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
1256             mvmvif == mvm->bf_allowed_vif)
1257                 MVM_DEBUGFS_ADD_FILE_VIF(bf_params, mvmvif->dbgfs_dir,
1258                                          S_IRUSR | S_IWUSR);
1259
1260         /*
1261          * Create symlink for convenience pointing to interface specific
1262          * debugfs entries for the driver. For example, under
1263          * /sys/kernel/debug/iwlwifi/0000\:02\:00.0/iwlmvm/
1264          * find
1265          * netdev:wlan0 -> ../../../ieee80211/phy0/netdev:wlan0/iwlmvm/
1266          */
1267         snprintf(buf, 100, "../../../%s/%s/%s/%s",
1268                  dbgfs_dir->d_parent->d_parent->d_name.name,
1269                  dbgfs_dir->d_parent->d_name.name,
1270                  dbgfs_dir->d_name.name,
1271                  mvmvif->dbgfs_dir->d_name.name);
1272
1273         mvmvif->dbgfs_slink = debugfs_create_symlink(dbgfs_dir->d_name.name,
1274                                                      mvm->debugfs_dir, buf);
1275         if (!mvmvif->dbgfs_slink)
1276                 IWL_ERR(mvm, "Can't create debugfs symbolic link under %s\n",
1277                         dbgfs_dir->d_name.name);
1278         return;
1279 err:
1280         IWL_ERR(mvm, "Can't create debugfs entity\n");
1281 }
1282
1283 void iwl_mvm_vif_dbgfs_clean(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1284 {
1285         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1286
1287         debugfs_remove(mvmvif->dbgfs_slink);
1288         mvmvif->dbgfs_slink = NULL;
1289
1290         debugfs_remove_recursive(mvmvif->dbgfs_dir);
1291         mvmvif->dbgfs_dir = NULL;
1292 }