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