ASoC: wm8776: replace codec to component
[linux-block.git] / drivers / net / wireless / intel / 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 - 2014 Intel Corporation. All rights reserved.
9  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of version 2 of the GNU General Public License as
14  * published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24  * USA
25  *
26  * The full GNU General Public License is included in this distribution
27  * in the file called COPYING.
28  *
29  * Contact Information:
30  *  Intel Linux Wireless <linuxwifi@intel.com>
31  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
32  *
33  * BSD LICENSE
34  *
35  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
36  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
37  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
38  * All rights reserved.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  *
44  *  * Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  *  * Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in
48  *    the documentation and/or other materials provided with the
49  *    distribution.
50  *  * Neither the name Intel Corporation nor the names of its
51  *    contributors may be used to endorse or promote products derived
52  *    from this software without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65  *
66  *****************************************************************************/
67 #include <linux/vmalloc.h>
68 #include <linux/ieee80211.h>
69 #include <linux/netdevice.h>
70
71 #include "mvm.h"
72 #include "sta.h"
73 #include "iwl-io.h"
74 #include "debugfs.h"
75 #include "fw/error-dump.h"
76
77 static ssize_t iwl_dbgfs_ctdp_budget_read(struct file *file,
78                                           char __user *user_buf,
79                                           size_t count, loff_t *ppos)
80 {
81         struct iwl_mvm *mvm = file->private_data;
82         char buf[16];
83         int pos, budget;
84
85         if (!iwl_mvm_is_ctdp_supported(mvm))
86                 return -EOPNOTSUPP;
87
88         if (!iwl_mvm_firmware_running(mvm) ||
89             mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
90                 return -EIO;
91
92         mutex_lock(&mvm->mutex);
93         budget = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_REPORT, 0);
94         mutex_unlock(&mvm->mutex);
95
96         if (budget < 0)
97                 return budget;
98
99         pos = scnprintf(buf, sizeof(buf), "%d\n", budget);
100
101         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
102 }
103
104 static ssize_t iwl_dbgfs_stop_ctdp_write(struct iwl_mvm *mvm, char *buf,
105                                          size_t count, loff_t *ppos)
106 {
107         int ret;
108
109         if (!iwl_mvm_is_ctdp_supported(mvm))
110                 return -EOPNOTSUPP;
111
112         if (!iwl_mvm_firmware_running(mvm) ||
113             mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
114                 return -EIO;
115
116         mutex_lock(&mvm->mutex);
117         ret = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_STOP, 0);
118         mutex_unlock(&mvm->mutex);
119
120         return ret ?: count;
121 }
122
123 static ssize_t iwl_dbgfs_force_ctkill_write(struct iwl_mvm *mvm, char *buf,
124                                             size_t count, loff_t *ppos)
125 {
126         if (!iwl_mvm_firmware_running(mvm) ||
127             mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
128                 return -EIO;
129
130         iwl_mvm_enter_ctkill(mvm);
131
132         return count;
133 }
134
135 static ssize_t iwl_dbgfs_tx_flush_write(struct iwl_mvm *mvm, char *buf,
136                                         size_t count, loff_t *ppos)
137 {
138         int ret;
139         u32 flush_arg;
140
141         if (!iwl_mvm_firmware_running(mvm) ||
142             mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
143                 return -EIO;
144
145         if (kstrtou32(buf, 0, &flush_arg))
146                 return -EINVAL;
147
148         if (iwl_mvm_has_new_tx_api(mvm)) {
149                 IWL_DEBUG_TX_QUEUES(mvm,
150                                     "FLUSHING all tids queues on sta_id = %d\n",
151                                     flush_arg);
152                 mutex_lock(&mvm->mutex);
153                 ret = iwl_mvm_flush_sta_tids(mvm, flush_arg, 0xFF, 0) ? : count;
154                 mutex_unlock(&mvm->mutex);
155                 return ret;
156         }
157
158         IWL_DEBUG_TX_QUEUES(mvm, "FLUSHING queues mask to flush = 0x%x\n",
159                             flush_arg);
160
161         mutex_lock(&mvm->mutex);
162         ret =  iwl_mvm_flush_tx_path(mvm, flush_arg, 0) ? : count;
163         mutex_unlock(&mvm->mutex);
164
165         return ret;
166 }
167
168 static ssize_t iwl_dbgfs_sta_drain_write(struct iwl_mvm *mvm, char *buf,
169                                          size_t count, loff_t *ppos)
170 {
171         struct iwl_mvm_sta *mvmsta;
172         int sta_id, drain, ret;
173
174         if (!iwl_mvm_firmware_running(mvm) ||
175             mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
176                 return -EIO;
177
178         if (sscanf(buf, "%d %d", &sta_id, &drain) != 2)
179                 return -EINVAL;
180         if (sta_id < 0 || sta_id >= IWL_MVM_STATION_COUNT)
181                 return -EINVAL;
182         if (drain < 0 || drain > 1)
183                 return -EINVAL;
184
185         mutex_lock(&mvm->mutex);
186
187         mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
188
189         if (!mvmsta)
190                 ret = -ENOENT;
191         else
192                 ret = iwl_mvm_drain_sta(mvm, mvmsta, drain) ? : count;
193
194         mutex_unlock(&mvm->mutex);
195
196         return ret;
197 }
198
199 static ssize_t iwl_dbgfs_sram_read(struct file *file, char __user *user_buf,
200                                    size_t count, loff_t *ppos)
201 {
202         struct iwl_mvm *mvm = file->private_data;
203         const struct fw_img *img;
204         unsigned int ofs, len;
205         size_t ret;
206         u8 *ptr;
207
208         if (!iwl_mvm_firmware_running(mvm))
209                 return -EINVAL;
210
211         /* default is to dump the entire data segment */
212         img = &mvm->fw->img[mvm->fwrt.cur_fw_img];
213         ofs = img->sec[IWL_UCODE_SECTION_DATA].offset;
214         len = img->sec[IWL_UCODE_SECTION_DATA].len;
215
216         if (mvm->dbgfs_sram_len) {
217                 ofs = mvm->dbgfs_sram_offset;
218                 len = mvm->dbgfs_sram_len;
219         }
220
221         ptr = kzalloc(len, GFP_KERNEL);
222         if (!ptr)
223                 return -ENOMEM;
224
225         iwl_trans_read_mem_bytes(mvm->trans, ofs, ptr, len);
226
227         ret = simple_read_from_buffer(user_buf, count, ppos, ptr, len);
228
229         kfree(ptr);
230
231         return ret;
232 }
233
234 static ssize_t iwl_dbgfs_sram_write(struct iwl_mvm *mvm, char *buf,
235                                     size_t count, loff_t *ppos)
236 {
237         const struct fw_img *img;
238         u32 offset, len;
239         u32 img_offset, img_len;
240
241         if (!iwl_mvm_firmware_running(mvm))
242                 return -EINVAL;
243
244         img = &mvm->fw->img[mvm->fwrt.cur_fw_img];
245         img_offset = img->sec[IWL_UCODE_SECTION_DATA].offset;
246         img_len = img->sec[IWL_UCODE_SECTION_DATA].len;
247
248         if (sscanf(buf, "%x,%x", &offset, &len) == 2) {
249                 if ((offset & 0x3) || (len & 0x3))
250                         return -EINVAL;
251
252                 if (offset + len > img_offset + img_len)
253                         return -EINVAL;
254
255                 mvm->dbgfs_sram_offset = offset;
256                 mvm->dbgfs_sram_len = len;
257         } else {
258                 mvm->dbgfs_sram_offset = 0;
259                 mvm->dbgfs_sram_len = 0;
260         }
261
262         return count;
263 }
264
265 static ssize_t iwl_dbgfs_set_nic_temperature_read(struct file *file,
266                                                   char __user *user_buf,
267                                                   size_t count, loff_t *ppos)
268 {
269         struct iwl_mvm *mvm = file->private_data;
270         char buf[16];
271         int pos;
272
273         if (!mvm->temperature_test)
274                 pos = scnprintf(buf , sizeof(buf), "disabled\n");
275         else
276                 pos = scnprintf(buf , sizeof(buf), "%d\n", mvm->temperature);
277
278         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
279 }
280
281 /*
282  * Set NIC Temperature
283  * Cause the driver to ignore the actual NIC temperature reported by the FW
284  * Enable: any value between IWL_MVM_DEBUG_SET_TEMPERATURE_MIN -
285  * IWL_MVM_DEBUG_SET_TEMPERATURE_MAX
286  * Disable: IWL_MVM_DEBUG_SET_TEMPERATURE_DISABLE
287  */
288 static ssize_t iwl_dbgfs_set_nic_temperature_write(struct iwl_mvm *mvm,
289                                                    char *buf, size_t count,
290                                                    loff_t *ppos)
291 {
292         int temperature;
293
294         if (!iwl_mvm_firmware_running(mvm) && !mvm->temperature_test)
295                 return -EIO;
296
297         if (kstrtoint(buf, 10, &temperature))
298                 return -EINVAL;
299         /* not a legal temperature */
300         if ((temperature > IWL_MVM_DEBUG_SET_TEMPERATURE_MAX &&
301              temperature != IWL_MVM_DEBUG_SET_TEMPERATURE_DISABLE) ||
302             temperature < IWL_MVM_DEBUG_SET_TEMPERATURE_MIN)
303                 return -EINVAL;
304
305         mutex_lock(&mvm->mutex);
306         if (temperature == IWL_MVM_DEBUG_SET_TEMPERATURE_DISABLE) {
307                 if (!mvm->temperature_test)
308                         goto out;
309
310                 mvm->temperature_test = false;
311                 /* Since we can't read the temp while awake, just set
312                  * it to zero until we get the next RX stats from the
313                  * firmware.
314                  */
315                 mvm->temperature = 0;
316         } else {
317                 mvm->temperature_test = true;
318                 mvm->temperature = temperature;
319         }
320         IWL_DEBUG_TEMP(mvm, "%sabling debug set temperature (temp = %d)\n",
321                        mvm->temperature_test ? "En" : "Dis" ,
322                        mvm->temperature);
323         /* handle the temperature change */
324         iwl_mvm_tt_handler(mvm);
325
326 out:
327         mutex_unlock(&mvm->mutex);
328
329         return count;
330 }
331
332 static ssize_t iwl_dbgfs_nic_temp_read(struct file *file,
333                                        char __user *user_buf,
334                                        size_t count, loff_t *ppos)
335 {
336         struct iwl_mvm *mvm = file->private_data;
337         char buf[16];
338         int pos, ret;
339         s32 temp;
340
341         if (!iwl_mvm_firmware_running(mvm))
342                 return -EIO;
343
344         mutex_lock(&mvm->mutex);
345         ret = iwl_mvm_get_temp(mvm, &temp);
346         mutex_unlock(&mvm->mutex);
347
348         if (ret)
349                 return -EIO;
350
351         pos = scnprintf(buf , sizeof(buf), "%d\n", temp);
352
353         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
354 }
355
356 #ifdef CONFIG_ACPI
357 static ssize_t iwl_dbgfs_sar_geo_profile_read(struct file *file,
358                                               char __user *user_buf,
359                                               size_t count, loff_t *ppos)
360 {
361         struct iwl_mvm *mvm = file->private_data;
362         char buf[256];
363         int pos = 0;
364         int bufsz = sizeof(buf);
365         int tbl_idx;
366         u8 *value;
367
368         if (!iwl_mvm_firmware_running(mvm))
369                 return -EIO;
370
371         mutex_lock(&mvm->mutex);
372         tbl_idx = iwl_mvm_get_sar_geo_profile(mvm);
373         if (tbl_idx < 0) {
374                 mutex_unlock(&mvm->mutex);
375                 return tbl_idx;
376         }
377
378         if (!tbl_idx) {
379                 pos = scnprintf(buf, bufsz,
380                                 "SAR geographic profile disabled\n");
381         } else {
382                 value = &mvm->geo_profiles[tbl_idx - 1].values[0];
383
384                 pos += scnprintf(buf + pos, bufsz - pos,
385                                  "Use geographic profile %d\n", tbl_idx);
386                 pos += scnprintf(buf + pos, bufsz - pos,
387                                  "2.4GHz:\n\tChain A offset: %hhd dBm\n\tChain B offset: %hhd dBm\n\tmax tx power: %hhd dBm\n",
388                                  value[1], value[2], value[0]);
389                 pos += scnprintf(buf + pos, bufsz - pos,
390                                  "5.2GHz:\n\tChain A offset: %hhd dBm\n\tChain B offset: %hhd dBm\n\tmax tx power: %hhd dBm\n",
391                                  value[4], value[5], value[3]);
392         }
393         mutex_unlock(&mvm->mutex);
394
395         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
396 }
397 #endif
398
399 static ssize_t iwl_dbgfs_stations_read(struct file *file, char __user *user_buf,
400                                        size_t count, loff_t *ppos)
401 {
402         struct iwl_mvm *mvm = file->private_data;
403         struct ieee80211_sta *sta;
404         char buf[400];
405         int i, pos = 0, bufsz = sizeof(buf);
406
407         mutex_lock(&mvm->mutex);
408
409         for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
410                 pos += scnprintf(buf + pos, bufsz - pos, "%.2d: ", i);
411                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
412                                                 lockdep_is_held(&mvm->mutex));
413                 if (!sta)
414                         pos += scnprintf(buf + pos, bufsz - pos, "N/A\n");
415                 else if (IS_ERR(sta))
416                         pos += scnprintf(buf + pos, bufsz - pos, "%ld\n",
417                                          PTR_ERR(sta));
418                 else
419                         pos += scnprintf(buf + pos, bufsz - pos, "%pM\n",
420                                          sta->addr);
421         }
422
423         mutex_unlock(&mvm->mutex);
424
425         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
426 }
427
428 static ssize_t iwl_dbgfs_rs_data_read(struct file *file, char __user *user_buf,
429                                       size_t count, loff_t *ppos)
430 {
431         struct ieee80211_sta *sta = file->private_data;
432         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
433         struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->lq_sta.rs_fw;
434         struct iwl_mvm *mvm = lq_sta->pers.drv;
435         static const size_t bufsz = 2048;
436         char *buff;
437         int desc = 0;
438         ssize_t ret;
439
440         buff = kmalloc(bufsz, GFP_KERNEL);
441         if (!buff)
442                 return -ENOMEM;
443
444         mutex_lock(&mvm->mutex);
445
446         desc += scnprintf(buff + desc, bufsz - desc, "sta_id %d\n",
447                           lq_sta->pers.sta_id);
448         desc += scnprintf(buff + desc, bufsz - desc,
449                           "fixed rate 0x%X\n",
450                           lq_sta->pers.dbg_fixed_rate);
451         desc += scnprintf(buff + desc, bufsz - desc,
452                           "A-MPDU size limit %d\n",
453                           lq_sta->pers.dbg_agg_frame_count_lim);
454         desc += scnprintf(buff + desc, bufsz - desc,
455                           "valid_tx_ant %s%s%s\n",
456                 (iwl_mvm_get_valid_tx_ant(mvm) & ANT_A) ? "ANT_A," : "",
457                 (iwl_mvm_get_valid_tx_ant(mvm) & ANT_B) ? "ANT_B," : "",
458                 (iwl_mvm_get_valid_tx_ant(mvm) & ANT_C) ? "ANT_C" : "");
459         desc += scnprintf(buff + desc, bufsz - desc,
460                           "last tx rate=0x%X ",
461                           lq_sta->last_rate_n_flags);
462
463         desc += rs_pretty_print_rate(buff + desc, bufsz - desc,
464                                      lq_sta->last_rate_n_flags);
465         mutex_unlock(&mvm->mutex);
466
467         ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
468         kfree(buff);
469         return ret;
470 }
471
472 static ssize_t iwl_dbgfs_disable_power_off_read(struct file *file,
473                                                 char __user *user_buf,
474                                                 size_t count, loff_t *ppos)
475 {
476         struct iwl_mvm *mvm = file->private_data;
477         char buf[64];
478         int bufsz = sizeof(buf);
479         int pos = 0;
480
481         pos += scnprintf(buf+pos, bufsz-pos, "disable_power_off_d0=%d\n",
482                          mvm->disable_power_off);
483         pos += scnprintf(buf+pos, bufsz-pos, "disable_power_off_d3=%d\n",
484                          mvm->disable_power_off_d3);
485
486         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
487 }
488
489 static ssize_t iwl_dbgfs_disable_power_off_write(struct iwl_mvm *mvm, char *buf,
490                                                  size_t count, loff_t *ppos)
491 {
492         int ret, val;
493
494         if (!iwl_mvm_firmware_running(mvm))
495                 return -EIO;
496
497         if (!strncmp("disable_power_off_d0=", buf, 21)) {
498                 if (sscanf(buf + 21, "%d", &val) != 1)
499                         return -EINVAL;
500                 mvm->disable_power_off = val;
501         } else if (!strncmp("disable_power_off_d3=", buf, 21)) {
502                 if (sscanf(buf + 21, "%d", &val) != 1)
503                         return -EINVAL;
504                 mvm->disable_power_off_d3 = val;
505         } else {
506                 return -EINVAL;
507         }
508
509         mutex_lock(&mvm->mutex);
510         ret = iwl_mvm_power_update_device(mvm);
511         mutex_unlock(&mvm->mutex);
512
513         return ret ?: count;
514 }
515
516 static
517 int iwl_mvm_coex_dump_mbox(struct iwl_bt_coex_profile_notif *notif, char *buf,
518                            int pos, int bufsz)
519 {
520         pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw0:\n");
521
522         BT_MBOX_PRINT(0, LE_SLAVE_LAT, false);
523         BT_MBOX_PRINT(0, LE_PROF1, false);
524         BT_MBOX_PRINT(0, LE_PROF2, false);
525         BT_MBOX_PRINT(0, LE_PROF_OTHER, false);
526         BT_MBOX_PRINT(0, CHL_SEQ_N, false);
527         BT_MBOX_PRINT(0, INBAND_S, false);
528         BT_MBOX_PRINT(0, LE_MIN_RSSI, false);
529         BT_MBOX_PRINT(0, LE_SCAN, false);
530         BT_MBOX_PRINT(0, LE_ADV, false);
531         BT_MBOX_PRINT(0, LE_MAX_TX_POWER, false);
532         BT_MBOX_PRINT(0, OPEN_CON_1, true);
533
534         pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw1:\n");
535
536         BT_MBOX_PRINT(1, BR_MAX_TX_POWER, false);
537         BT_MBOX_PRINT(1, IP_SR, false);
538         BT_MBOX_PRINT(1, LE_MSTR, false);
539         BT_MBOX_PRINT(1, AGGR_TRFC_LD, false);
540         BT_MBOX_PRINT(1, MSG_TYPE, false);
541         BT_MBOX_PRINT(1, SSN, true);
542
543         pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw2:\n");
544
545         BT_MBOX_PRINT(2, SNIFF_ACT, false);
546         BT_MBOX_PRINT(2, PAG, false);
547         BT_MBOX_PRINT(2, INQUIRY, false);
548         BT_MBOX_PRINT(2, CONN, false);
549         BT_MBOX_PRINT(2, SNIFF_INTERVAL, false);
550         BT_MBOX_PRINT(2, DISC, false);
551         BT_MBOX_PRINT(2, SCO_TX_ACT, false);
552         BT_MBOX_PRINT(2, SCO_RX_ACT, false);
553         BT_MBOX_PRINT(2, ESCO_RE_TX, false);
554         BT_MBOX_PRINT(2, SCO_DURATION, true);
555
556         pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw3:\n");
557
558         BT_MBOX_PRINT(3, SCO_STATE, false);
559         BT_MBOX_PRINT(3, SNIFF_STATE, false);
560         BT_MBOX_PRINT(3, A2DP_STATE, false);
561         BT_MBOX_PRINT(3, A2DP_SRC, false);
562         BT_MBOX_PRINT(3, ACL_STATE, false);
563         BT_MBOX_PRINT(3, MSTR_STATE, false);
564         BT_MBOX_PRINT(3, OBX_STATE, false);
565         BT_MBOX_PRINT(3, OPEN_CON_2, false);
566         BT_MBOX_PRINT(3, TRAFFIC_LOAD, false);
567         BT_MBOX_PRINT(3, CHL_SEQN_LSB, false);
568         BT_MBOX_PRINT(3, INBAND_P, false);
569         BT_MBOX_PRINT(3, MSG_TYPE_2, false);
570         BT_MBOX_PRINT(3, SSN_2, false);
571         BT_MBOX_PRINT(3, UPDATE_REQUEST, true);
572
573         return pos;
574 }
575
576 static ssize_t iwl_dbgfs_bt_notif_read(struct file *file, char __user *user_buf,
577                                        size_t count, loff_t *ppos)
578 {
579         struct iwl_mvm *mvm = file->private_data;
580         struct iwl_bt_coex_profile_notif *notif = &mvm->last_bt_notif;
581         char *buf;
582         int ret, pos = 0, bufsz = sizeof(char) * 1024;
583
584         buf = kmalloc(bufsz, GFP_KERNEL);
585         if (!buf)
586                 return -ENOMEM;
587
588         mutex_lock(&mvm->mutex);
589
590         pos += iwl_mvm_coex_dump_mbox(notif, buf, pos, bufsz);
591
592         pos += scnprintf(buf + pos, bufsz - pos, "bt_ci_compliance = %d\n",
593                          notif->bt_ci_compliance);
594         pos += scnprintf(buf + pos, bufsz - pos, "primary_ch_lut = %d\n",
595                          le32_to_cpu(notif->primary_ch_lut));
596         pos += scnprintf(buf + pos, bufsz - pos, "secondary_ch_lut = %d\n",
597                          le32_to_cpu(notif->secondary_ch_lut));
598         pos += scnprintf(buf + pos,
599                          bufsz - pos, "bt_activity_grading = %d\n",
600                          le32_to_cpu(notif->bt_activity_grading));
601         pos += scnprintf(buf + pos, bufsz - pos, "bt_rrc = %d\n",
602                          notif->rrc_status & 0xF);
603         pos += scnprintf(buf + pos, bufsz - pos, "bt_ttc = %d\n",
604                          notif->ttc_status & 0xF);
605
606         pos += scnprintf(buf + pos, bufsz - pos, "sync_sco = %d\n",
607                          IWL_MVM_BT_COEX_SYNC2SCO);
608         pos += scnprintf(buf + pos, bufsz - pos, "mplut = %d\n",
609                          IWL_MVM_BT_COEX_MPLUT);
610
611         mutex_unlock(&mvm->mutex);
612
613         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
614         kfree(buf);
615
616         return ret;
617 }
618 #undef BT_MBOX_PRINT
619
620 static ssize_t iwl_dbgfs_bt_cmd_read(struct file *file, char __user *user_buf,
621                                      size_t count, loff_t *ppos)
622 {
623         struct iwl_mvm *mvm = file->private_data;
624         struct iwl_bt_coex_ci_cmd *cmd = &mvm->last_bt_ci_cmd;
625         char buf[256];
626         int bufsz = sizeof(buf);
627         int pos = 0;
628
629         mutex_lock(&mvm->mutex);
630
631         pos += scnprintf(buf + pos, bufsz - pos, "Channel inhibition CMD\n");
632         pos += scnprintf(buf + pos, bufsz - pos,
633                          "\tPrimary Channel Bitmap 0x%016llx\n",
634                          le64_to_cpu(cmd->bt_primary_ci));
635         pos += scnprintf(buf + pos, bufsz - pos,
636                          "\tSecondary Channel Bitmap 0x%016llx\n",
637                          le64_to_cpu(cmd->bt_secondary_ci));
638
639         mutex_unlock(&mvm->mutex);
640
641         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
642 }
643
644 static ssize_t
645 iwl_dbgfs_bt_tx_prio_write(struct iwl_mvm *mvm, char *buf,
646                            size_t count, loff_t *ppos)
647 {
648         u32 bt_tx_prio;
649
650         if (sscanf(buf, "%u", &bt_tx_prio) != 1)
651                 return -EINVAL;
652         if (bt_tx_prio > 4)
653                 return -EINVAL;
654
655         mvm->bt_tx_prio = bt_tx_prio;
656
657         return count;
658 }
659
660 static ssize_t
661 iwl_dbgfs_bt_force_ant_write(struct iwl_mvm *mvm, char *buf,
662                              size_t count, loff_t *ppos)
663 {
664         static const char * const modes_str[BT_FORCE_ANT_MAX] = {
665                 [BT_FORCE_ANT_DIS] = "dis",
666                 [BT_FORCE_ANT_AUTO] = "auto",
667                 [BT_FORCE_ANT_BT] = "bt",
668                 [BT_FORCE_ANT_WIFI] = "wifi",
669         };
670         int ret, bt_force_ant_mode;
671
672         for (bt_force_ant_mode = 0;
673              bt_force_ant_mode < ARRAY_SIZE(modes_str);
674              bt_force_ant_mode++) {
675                 if (!strcmp(buf, modes_str[bt_force_ant_mode]))
676                         break;
677         }
678
679         if (bt_force_ant_mode >= ARRAY_SIZE(modes_str))
680                 return -EINVAL;
681
682         ret = 0;
683         mutex_lock(&mvm->mutex);
684         if (mvm->bt_force_ant_mode == bt_force_ant_mode)
685                 goto out;
686
687         mvm->bt_force_ant_mode = bt_force_ant_mode;
688         IWL_DEBUG_COEX(mvm, "Force mode: %s\n",
689                        modes_str[mvm->bt_force_ant_mode]);
690
691         if (iwl_mvm_firmware_running(mvm))
692                 ret = iwl_mvm_send_bt_init_conf(mvm);
693         else
694                 ret = 0;
695
696 out:
697         mutex_unlock(&mvm->mutex);
698         return ret ?: count;
699 }
700
701 static ssize_t iwl_dbgfs_fw_ver_read(struct file *file, char __user *user_buf,
702                                      size_t count, loff_t *ppos)
703 {
704         struct iwl_mvm *mvm = file->private_data;
705         char *buff, *pos, *endpos;
706         static const size_t bufsz = 1024;
707         int ret;
708
709         buff = kmalloc(bufsz, GFP_KERNEL);
710         if (!buff)
711                 return -ENOMEM;
712
713         pos = buff;
714         endpos = pos + bufsz;
715
716         pos += scnprintf(pos, endpos - pos, "FW prefix: %s\n",
717                          mvm->trans->cfg->fw_name_pre);
718         pos += scnprintf(pos, endpos - pos, "FW: %s\n",
719                          mvm->fwrt.fw->human_readable);
720         pos += scnprintf(pos, endpos - pos, "Device: %s\n",
721                          mvm->fwrt.trans->cfg->name);
722         pos += scnprintf(pos, endpos - pos, "Bus: %s\n",
723                          mvm->fwrt.dev->bus->name);
724
725         ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
726         kfree(buff);
727
728         return ret;
729 }
730
731 #define PRINT_STATS_LE32(_struct, _memb)                                \
732                          pos += scnprintf(buf + pos, bufsz - pos,       \
733                                           fmt_table, #_memb,            \
734                                           le32_to_cpu(_struct->_memb))
735
736 static ssize_t iwl_dbgfs_fw_rx_stats_read(struct file *file,
737                                           char __user *user_buf, size_t count,
738                                           loff_t *ppos)
739 {
740         struct iwl_mvm *mvm = file->private_data;
741         static const char *fmt_table = "\t%-30s %10u\n";
742         static const char *fmt_header = "%-32s\n";
743         int pos = 0;
744         char *buf;
745         int ret;
746         size_t bufsz;
747
748         if (iwl_mvm_has_new_rx_stats_api(mvm))
749                 bufsz = ((sizeof(struct mvm_statistics_rx) /
750                           sizeof(__le32)) * 43) + (4 * 33) + 1;
751         else
752                 /* 43 = size of each data line; 33 = size of each header */
753                 bufsz = ((sizeof(struct mvm_statistics_rx_v3) /
754                           sizeof(__le32)) * 43) + (4 * 33) + 1;
755
756         buf = kzalloc(bufsz, GFP_KERNEL);
757         if (!buf)
758                 return -ENOMEM;
759
760         mutex_lock(&mvm->mutex);
761
762         if (iwl_mvm_firmware_running(mvm))
763                 iwl_mvm_request_statistics(mvm, false);
764
765         pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
766                          "Statistics_Rx - OFDM");
767         if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
768                 struct mvm_statistics_rx_phy_v2 *ofdm = &mvm->rx_stats_v3.ofdm;
769
770                 PRINT_STATS_LE32(ofdm, ina_cnt);
771                 PRINT_STATS_LE32(ofdm, fina_cnt);
772                 PRINT_STATS_LE32(ofdm, plcp_err);
773                 PRINT_STATS_LE32(ofdm, crc32_err);
774                 PRINT_STATS_LE32(ofdm, overrun_err);
775                 PRINT_STATS_LE32(ofdm, early_overrun_err);
776                 PRINT_STATS_LE32(ofdm, crc32_good);
777                 PRINT_STATS_LE32(ofdm, false_alarm_cnt);
778                 PRINT_STATS_LE32(ofdm, fina_sync_err_cnt);
779                 PRINT_STATS_LE32(ofdm, sfd_timeout);
780                 PRINT_STATS_LE32(ofdm, fina_timeout);
781                 PRINT_STATS_LE32(ofdm, unresponded_rts);
782                 PRINT_STATS_LE32(ofdm, rxe_frame_lmt_overrun);
783                 PRINT_STATS_LE32(ofdm, sent_ack_cnt);
784                 PRINT_STATS_LE32(ofdm, sent_cts_cnt);
785                 PRINT_STATS_LE32(ofdm, sent_ba_rsp_cnt);
786                 PRINT_STATS_LE32(ofdm, dsp_self_kill);
787                 PRINT_STATS_LE32(ofdm, mh_format_err);
788                 PRINT_STATS_LE32(ofdm, re_acq_main_rssi_sum);
789                 PRINT_STATS_LE32(ofdm, reserved);
790         } else {
791                 struct mvm_statistics_rx_phy *ofdm = &mvm->rx_stats.ofdm;
792
793                 PRINT_STATS_LE32(ofdm, unresponded_rts);
794                 PRINT_STATS_LE32(ofdm, rxe_frame_lmt_overrun);
795                 PRINT_STATS_LE32(ofdm, sent_ba_rsp_cnt);
796                 PRINT_STATS_LE32(ofdm, dsp_self_kill);
797                 PRINT_STATS_LE32(ofdm, reserved);
798         }
799
800         pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
801                          "Statistics_Rx - CCK");
802         if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
803                 struct mvm_statistics_rx_phy_v2 *cck = &mvm->rx_stats_v3.cck;
804
805                 PRINT_STATS_LE32(cck, ina_cnt);
806                 PRINT_STATS_LE32(cck, fina_cnt);
807                 PRINT_STATS_LE32(cck, plcp_err);
808                 PRINT_STATS_LE32(cck, crc32_err);
809                 PRINT_STATS_LE32(cck, overrun_err);
810                 PRINT_STATS_LE32(cck, early_overrun_err);
811                 PRINT_STATS_LE32(cck, crc32_good);
812                 PRINT_STATS_LE32(cck, false_alarm_cnt);
813                 PRINT_STATS_LE32(cck, fina_sync_err_cnt);
814                 PRINT_STATS_LE32(cck, sfd_timeout);
815                 PRINT_STATS_LE32(cck, fina_timeout);
816                 PRINT_STATS_LE32(cck, unresponded_rts);
817                 PRINT_STATS_LE32(cck, rxe_frame_lmt_overrun);
818                 PRINT_STATS_LE32(cck, sent_ack_cnt);
819                 PRINT_STATS_LE32(cck, sent_cts_cnt);
820                 PRINT_STATS_LE32(cck, sent_ba_rsp_cnt);
821                 PRINT_STATS_LE32(cck, dsp_self_kill);
822                 PRINT_STATS_LE32(cck, mh_format_err);
823                 PRINT_STATS_LE32(cck, re_acq_main_rssi_sum);
824                 PRINT_STATS_LE32(cck, reserved);
825         } else {
826                 struct mvm_statistics_rx_phy *cck = &mvm->rx_stats.cck;
827
828                 PRINT_STATS_LE32(cck, unresponded_rts);
829                 PRINT_STATS_LE32(cck, rxe_frame_lmt_overrun);
830                 PRINT_STATS_LE32(cck, sent_ba_rsp_cnt);
831                 PRINT_STATS_LE32(cck, dsp_self_kill);
832                 PRINT_STATS_LE32(cck, reserved);
833         }
834
835         pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
836                          "Statistics_Rx - GENERAL");
837         if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
838                 struct mvm_statistics_rx_non_phy_v3 *general =
839                         &mvm->rx_stats_v3.general;
840
841                 PRINT_STATS_LE32(general, bogus_cts);
842                 PRINT_STATS_LE32(general, bogus_ack);
843                 PRINT_STATS_LE32(general, non_bssid_frames);
844                 PRINT_STATS_LE32(general, filtered_frames);
845                 PRINT_STATS_LE32(general, non_channel_beacons);
846                 PRINT_STATS_LE32(general, channel_beacons);
847                 PRINT_STATS_LE32(general, num_missed_bcon);
848                 PRINT_STATS_LE32(general, adc_rx_saturation_time);
849                 PRINT_STATS_LE32(general, ina_detection_search_time);
850                 PRINT_STATS_LE32(general, beacon_silence_rssi_a);
851                 PRINT_STATS_LE32(general, beacon_silence_rssi_b);
852                 PRINT_STATS_LE32(general, beacon_silence_rssi_c);
853                 PRINT_STATS_LE32(general, interference_data_flag);
854                 PRINT_STATS_LE32(general, channel_load);
855                 PRINT_STATS_LE32(general, dsp_false_alarms);
856                 PRINT_STATS_LE32(general, beacon_rssi_a);
857                 PRINT_STATS_LE32(general, beacon_rssi_b);
858                 PRINT_STATS_LE32(general, beacon_rssi_c);
859                 PRINT_STATS_LE32(general, beacon_energy_a);
860                 PRINT_STATS_LE32(general, beacon_energy_b);
861                 PRINT_STATS_LE32(general, beacon_energy_c);
862                 PRINT_STATS_LE32(general, num_bt_kills);
863                 PRINT_STATS_LE32(general, mac_id);
864                 PRINT_STATS_LE32(general, directed_data_mpdu);
865         } else {
866                 struct mvm_statistics_rx_non_phy *general =
867                         &mvm->rx_stats.general;
868
869                 PRINT_STATS_LE32(general, bogus_cts);
870                 PRINT_STATS_LE32(general, bogus_ack);
871                 PRINT_STATS_LE32(general, non_channel_beacons);
872                 PRINT_STATS_LE32(general, channel_beacons);
873                 PRINT_STATS_LE32(general, num_missed_bcon);
874                 PRINT_STATS_LE32(general, adc_rx_saturation_time);
875                 PRINT_STATS_LE32(general, ina_detection_search_time);
876                 PRINT_STATS_LE32(general, beacon_silence_rssi_a);
877                 PRINT_STATS_LE32(general, beacon_silence_rssi_b);
878                 PRINT_STATS_LE32(general, beacon_silence_rssi_c);
879                 PRINT_STATS_LE32(general, interference_data_flag);
880                 PRINT_STATS_LE32(general, channel_load);
881                 PRINT_STATS_LE32(general, beacon_rssi_a);
882                 PRINT_STATS_LE32(general, beacon_rssi_b);
883                 PRINT_STATS_LE32(general, beacon_rssi_c);
884                 PRINT_STATS_LE32(general, beacon_energy_a);
885                 PRINT_STATS_LE32(general, beacon_energy_b);
886                 PRINT_STATS_LE32(general, beacon_energy_c);
887                 PRINT_STATS_LE32(general, num_bt_kills);
888                 PRINT_STATS_LE32(general, mac_id);
889         }
890
891         pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
892                          "Statistics_Rx - HT");
893         if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
894                 struct mvm_statistics_rx_ht_phy_v1 *ht =
895                         &mvm->rx_stats_v3.ofdm_ht;
896
897                 PRINT_STATS_LE32(ht, plcp_err);
898                 PRINT_STATS_LE32(ht, overrun_err);
899                 PRINT_STATS_LE32(ht, early_overrun_err);
900                 PRINT_STATS_LE32(ht, crc32_good);
901                 PRINT_STATS_LE32(ht, crc32_err);
902                 PRINT_STATS_LE32(ht, mh_format_err);
903                 PRINT_STATS_LE32(ht, agg_crc32_good);
904                 PRINT_STATS_LE32(ht, agg_mpdu_cnt);
905                 PRINT_STATS_LE32(ht, agg_cnt);
906                 PRINT_STATS_LE32(ht, unsupport_mcs);
907         } else {
908                 struct mvm_statistics_rx_ht_phy *ht =
909                         &mvm->rx_stats.ofdm_ht;
910
911                 PRINT_STATS_LE32(ht, mh_format_err);
912                 PRINT_STATS_LE32(ht, agg_mpdu_cnt);
913                 PRINT_STATS_LE32(ht, agg_cnt);
914                 PRINT_STATS_LE32(ht, unsupport_mcs);
915         }
916
917         mutex_unlock(&mvm->mutex);
918
919         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
920         kfree(buf);
921
922         return ret;
923 }
924 #undef PRINT_STAT_LE32
925
926 static ssize_t iwl_dbgfs_frame_stats_read(struct iwl_mvm *mvm,
927                                           char __user *user_buf, size_t count,
928                                           loff_t *ppos,
929                                           struct iwl_mvm_frame_stats *stats)
930 {
931         char *buff, *pos, *endpos;
932         int idx, i;
933         int ret;
934         static const size_t bufsz = 1024;
935
936         buff = kmalloc(bufsz, GFP_KERNEL);
937         if (!buff)
938                 return -ENOMEM;
939
940         spin_lock_bh(&mvm->drv_stats_lock);
941
942         pos = buff;
943         endpos = pos + bufsz;
944
945         pos += scnprintf(pos, endpos - pos,
946                          "Legacy/HT/VHT\t:\t%d/%d/%d\n",
947                          stats->legacy_frames,
948                          stats->ht_frames,
949                          stats->vht_frames);
950         pos += scnprintf(pos, endpos - pos, "20/40/80\t:\t%d/%d/%d\n",
951                          stats->bw_20_frames,
952                          stats->bw_40_frames,
953                          stats->bw_80_frames);
954         pos += scnprintf(pos, endpos - pos, "NGI/SGI\t\t:\t%d/%d\n",
955                          stats->ngi_frames,
956                          stats->sgi_frames);
957         pos += scnprintf(pos, endpos - pos, "SISO/MIMO2\t:\t%d/%d\n",
958                          stats->siso_frames,
959                          stats->mimo2_frames);
960         pos += scnprintf(pos, endpos - pos, "FAIL/SCSS\t:\t%d/%d\n",
961                          stats->fail_frames,
962                          stats->success_frames);
963         pos += scnprintf(pos, endpos - pos, "MPDUs agg\t:\t%d\n",
964                          stats->agg_frames);
965         pos += scnprintf(pos, endpos - pos, "A-MPDUs\t\t:\t%d\n",
966                          stats->ampdu_count);
967         pos += scnprintf(pos, endpos - pos, "Avg MPDUs/A-MPDU:\t%d\n",
968                          stats->ampdu_count > 0 ?
969                          (stats->agg_frames / stats->ampdu_count) : 0);
970
971         pos += scnprintf(pos, endpos - pos, "Last Rates\n");
972
973         idx = stats->last_frame_idx - 1;
974         for (i = 0; i < ARRAY_SIZE(stats->last_rates); i++) {
975                 idx = (idx + 1) % ARRAY_SIZE(stats->last_rates);
976                 if (stats->last_rates[idx] == 0)
977                         continue;
978                 pos += scnprintf(pos, endpos - pos, "Rate[%d]: ",
979                                  (int)(ARRAY_SIZE(stats->last_rates) - i));
980                 pos += rs_pretty_print_rate(pos, endpos - pos,
981                                             stats->last_rates[idx]);
982         }
983         spin_unlock_bh(&mvm->drv_stats_lock);
984
985         ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
986         kfree(buff);
987
988         return ret;
989 }
990
991 static ssize_t iwl_dbgfs_drv_rx_stats_read(struct file *file,
992                                            char __user *user_buf, size_t count,
993                                            loff_t *ppos)
994 {
995         struct iwl_mvm *mvm = file->private_data;
996
997         return iwl_dbgfs_frame_stats_read(mvm, user_buf, count, ppos,
998                                           &mvm->drv_rx_stats);
999 }
1000
1001 static ssize_t iwl_dbgfs_fw_restart_write(struct iwl_mvm *mvm, char *buf,
1002                                           size_t count, loff_t *ppos)
1003 {
1004         int __maybe_unused ret;
1005
1006         if (!iwl_mvm_firmware_running(mvm))
1007                 return -EIO;
1008
1009         mutex_lock(&mvm->mutex);
1010
1011         /* allow one more restart that we're provoking here */
1012         if (mvm->fw_restart >= 0)
1013                 mvm->fw_restart++;
1014
1015         /* take the return value to make compiler happy - it will fail anyway */
1016         ret = iwl_mvm_send_cmd_pdu(mvm, REPLY_ERROR, 0, 0, NULL);
1017
1018         mutex_unlock(&mvm->mutex);
1019
1020         return count;
1021 }
1022
1023 static ssize_t iwl_dbgfs_fw_nmi_write(struct iwl_mvm *mvm, char *buf,
1024                                       size_t count, loff_t *ppos)
1025 {
1026         int ret;
1027
1028         if (!iwl_mvm_firmware_running(mvm))
1029                 return -EIO;
1030
1031         ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_NMI);
1032         if (ret)
1033                 return ret;
1034
1035         iwl_force_nmi(mvm->trans);
1036
1037         iwl_mvm_unref(mvm, IWL_MVM_REF_NMI);
1038
1039         return count;
1040 }
1041
1042 static ssize_t
1043 iwl_dbgfs_scan_ant_rxchain_read(struct file *file,
1044                                 char __user *user_buf,
1045                                 size_t count, loff_t *ppos)
1046 {
1047         struct iwl_mvm *mvm = file->private_data;
1048         int pos = 0;
1049         char buf[32];
1050         const size_t bufsz = sizeof(buf);
1051
1052         /* print which antennas were set for the scan command by the user */
1053         pos += scnprintf(buf + pos, bufsz - pos, "Antennas for scan: ");
1054         if (mvm->scan_rx_ant & ANT_A)
1055                 pos += scnprintf(buf + pos, bufsz - pos, "A");
1056         if (mvm->scan_rx_ant & ANT_B)
1057                 pos += scnprintf(buf + pos, bufsz - pos, "B");
1058         if (mvm->scan_rx_ant & ANT_C)
1059                 pos += scnprintf(buf + pos, bufsz - pos, "C");
1060         pos += scnprintf(buf + pos, bufsz - pos, " (%hhx)\n", mvm->scan_rx_ant);
1061
1062         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1063 }
1064
1065 static ssize_t
1066 iwl_dbgfs_scan_ant_rxchain_write(struct iwl_mvm *mvm, char *buf,
1067                                  size_t count, loff_t *ppos)
1068 {
1069         u8 scan_rx_ant;
1070
1071         if (!iwl_mvm_firmware_running(mvm))
1072                 return -EIO;
1073
1074         if (sscanf(buf, "%hhx", &scan_rx_ant) != 1)
1075                 return -EINVAL;
1076         if (scan_rx_ant > ANT_ABC)
1077                 return -EINVAL;
1078         if (scan_rx_ant & ~(iwl_mvm_get_valid_rx_ant(mvm)))
1079                 return -EINVAL;
1080
1081         if (mvm->scan_rx_ant != scan_rx_ant) {
1082                 mvm->scan_rx_ant = scan_rx_ant;
1083                 if (fw_has_capa(&mvm->fw->ucode_capa,
1084                                 IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1085                         iwl_mvm_config_scan(mvm);
1086         }
1087
1088         return count;
1089 }
1090
1091 static ssize_t iwl_dbgfs_indirection_tbl_write(struct iwl_mvm *mvm,
1092                                                char *buf, size_t count,
1093                                                loff_t *ppos)
1094 {
1095         struct iwl_rss_config_cmd cmd = {
1096                 .flags = cpu_to_le32(IWL_RSS_ENABLE),
1097                 .hash_mask = IWL_RSS_HASH_TYPE_IPV4_TCP |
1098                              IWL_RSS_HASH_TYPE_IPV4_UDP |
1099                              IWL_RSS_HASH_TYPE_IPV4_PAYLOAD |
1100                              IWL_RSS_HASH_TYPE_IPV6_TCP |
1101                              IWL_RSS_HASH_TYPE_IPV6_UDP |
1102                              IWL_RSS_HASH_TYPE_IPV6_PAYLOAD,
1103         };
1104         int ret, i, num_repeats, nbytes = count / 2;
1105
1106         ret = hex2bin(cmd.indirection_table, buf, nbytes);
1107         if (ret)
1108                 return ret;
1109
1110         /*
1111          * The input is the redirection table, partial or full.
1112          * Repeat the pattern if needed.
1113          * For example, input of 01020F will be repeated 42 times,
1114          * indirecting RSS hash results to queues 1, 2, 15 (skipping
1115          * queues 3 - 14).
1116          */
1117         num_repeats = ARRAY_SIZE(cmd.indirection_table) / nbytes;
1118         for (i = 1; i < num_repeats; i++)
1119                 memcpy(&cmd.indirection_table[i * nbytes],
1120                        cmd.indirection_table, nbytes);
1121         /* handle cut in the middle pattern for the last places */
1122         memcpy(&cmd.indirection_table[i * nbytes], cmd.indirection_table,
1123                ARRAY_SIZE(cmd.indirection_table) % nbytes);
1124
1125         netdev_rss_key_fill(cmd.secret_key, sizeof(cmd.secret_key));
1126
1127         mutex_lock(&mvm->mutex);
1128         if (iwl_mvm_firmware_running(mvm))
1129                 ret = iwl_mvm_send_cmd_pdu(mvm, RSS_CONFIG_CMD, 0,
1130                                            sizeof(cmd), &cmd);
1131         else
1132                 ret = 0;
1133         mutex_unlock(&mvm->mutex);
1134
1135         return ret ?: count;
1136 }
1137
1138 static ssize_t iwl_dbgfs_inject_packet_write(struct iwl_mvm *mvm,
1139                                              char *buf, size_t count,
1140                                              loff_t *ppos)
1141 {
1142         struct iwl_rx_cmd_buffer rxb = {
1143                 ._rx_page_order = 0,
1144                 .truesize = 0, /* not used */
1145                 ._offset = 0,
1146         };
1147         struct iwl_rx_packet *pkt;
1148         struct iwl_rx_mpdu_desc *desc;
1149         int bin_len = count / 2;
1150         int ret = -EINVAL;
1151
1152         if (!iwl_mvm_firmware_running(mvm))
1153                 return -EIO;
1154
1155         /* supporting only 9000 descriptor */
1156         if (!mvm->trans->cfg->mq_rx_supported)
1157                 return -ENOTSUPP;
1158
1159         rxb._page = alloc_pages(GFP_ATOMIC, 0);
1160         if (!rxb._page)
1161                 return -ENOMEM;
1162         pkt = rxb_addr(&rxb);
1163
1164         ret = hex2bin(page_address(rxb._page), buf, bin_len);
1165         if (ret)
1166                 goto out;
1167
1168         /* avoid invalid memory access */
1169         if (bin_len < sizeof(*pkt) + sizeof(*desc))
1170                 goto out;
1171
1172         /* check this is RX packet */
1173         if (WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd) !=
1174             WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD))
1175                 goto out;
1176
1177         /* check the length in metadata matches actual received length */
1178         desc = (void *)pkt->data;
1179         if (le16_to_cpu(desc->mpdu_len) !=
1180             (bin_len - sizeof(*desc) - sizeof(*pkt)))
1181                 goto out;
1182
1183         local_bh_disable();
1184         iwl_mvm_rx_mpdu_mq(mvm, NULL, &rxb, 0);
1185         local_bh_enable();
1186         ret = 0;
1187
1188 out:
1189         iwl_free_rxb(&rxb);
1190
1191         return ret ?: count;
1192 }
1193
1194 static ssize_t iwl_dbgfs_fw_dbg_conf_read(struct file *file,
1195                                           char __user *user_buf,
1196                                           size_t count, loff_t *ppos)
1197 {
1198         struct iwl_mvm *mvm = file->private_data;
1199         int conf;
1200         char buf[8];
1201         const size_t bufsz = sizeof(buf);
1202         int pos = 0;
1203
1204         mutex_lock(&mvm->mutex);
1205         conf = mvm->fwrt.dump.conf;
1206         mutex_unlock(&mvm->mutex);
1207
1208         pos += scnprintf(buf + pos, bufsz - pos, "%d\n", conf);
1209
1210         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1211 }
1212
1213 /*
1214  * Enable / Disable continuous recording.
1215  * Cause the FW to start continuous recording, by sending the relevant hcmd.
1216  * Enable: input of every integer larger than 0, ENABLE_CONT_RECORDING.
1217  * Disable: for 0 as input, DISABLE_CONT_RECORDING.
1218  */
1219 static ssize_t iwl_dbgfs_cont_recording_write(struct iwl_mvm *mvm,
1220                                               char *buf, size_t count,
1221                                               loff_t *ppos)
1222 {
1223         struct iwl_trans *trans = mvm->trans;
1224         const struct iwl_fw_dbg_dest_tlv_v1 *dest = trans->dbg_dest_tlv;
1225         struct iwl_continuous_record_cmd cont_rec = {};
1226         int ret, rec_mode;
1227
1228         if (!iwl_mvm_firmware_running(mvm))
1229                 return -EIO;
1230
1231         if (!dest)
1232                 return -EOPNOTSUPP;
1233
1234         if (dest->monitor_mode != SMEM_MODE ||
1235             trans->cfg->device_family < IWL_DEVICE_FAMILY_8000)
1236                 return -EOPNOTSUPP;
1237
1238         ret = kstrtoint(buf, 0, &rec_mode);
1239         if (ret)
1240                 return ret;
1241
1242         cont_rec.record_mode.enable_recording = rec_mode ?
1243                 cpu_to_le16(ENABLE_CONT_RECORDING) :
1244                 cpu_to_le16(DISABLE_CONT_RECORDING);
1245
1246         mutex_lock(&mvm->mutex);
1247         ret = iwl_mvm_send_cmd_pdu(mvm, LDBG_CONFIG_CMD, 0,
1248                                    sizeof(cont_rec), &cont_rec);
1249         mutex_unlock(&mvm->mutex);
1250
1251         return ret ?: count;
1252 }
1253
1254 static ssize_t iwl_dbgfs_fw_dbg_conf_write(struct iwl_mvm *mvm,
1255                                            char *buf, size_t count,
1256                                            loff_t *ppos)
1257 {
1258         unsigned int conf_id;
1259         int ret;
1260
1261         if (!iwl_mvm_firmware_running(mvm))
1262                 return -EIO;
1263
1264         ret = kstrtouint(buf, 0, &conf_id);
1265         if (ret)
1266                 return ret;
1267
1268         if (WARN_ON(conf_id >= FW_DBG_CONF_MAX))
1269                 return -EINVAL;
1270
1271         mutex_lock(&mvm->mutex);
1272         ret = iwl_fw_start_dbg_conf(&mvm->fwrt, conf_id);
1273         mutex_unlock(&mvm->mutex);
1274
1275         return ret ?: count;
1276 }
1277
1278 static ssize_t iwl_dbgfs_fw_dbg_collect_write(struct iwl_mvm *mvm,
1279                                               char *buf, size_t count,
1280                                               loff_t *ppos)
1281 {
1282         int ret;
1283
1284         if (!iwl_mvm_firmware_running(mvm))
1285                 return -EIO;
1286
1287         ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PRPH_WRITE);
1288         if (ret)
1289                 return ret;
1290         if (count == 0)
1291                 return 0;
1292
1293         iwl_fw_dbg_collect(&mvm->fwrt, FW_DBG_TRIGGER_USER, buf,
1294                            (count - 1), NULL);
1295
1296         iwl_mvm_unref(mvm, IWL_MVM_REF_PRPH_WRITE);
1297
1298         return count;
1299 }
1300
1301 static ssize_t iwl_dbgfs_max_amsdu_len_write(struct iwl_mvm *mvm,
1302                                              char *buf, size_t count,
1303                                              loff_t *ppos)
1304 {
1305         unsigned int max_amsdu_len;
1306         int ret;
1307
1308         ret = kstrtouint(buf, 0, &max_amsdu_len);
1309         if (ret)
1310                 return ret;
1311
1312         if (max_amsdu_len > IEEE80211_MAX_MPDU_LEN_VHT_11454)
1313                 return -EINVAL;
1314         mvm->max_amsdu_len = max_amsdu_len;
1315
1316         return count;
1317 }
1318
1319 #define ADD_TEXT(...) pos += scnprintf(buf + pos, bufsz - pos, __VA_ARGS__)
1320 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1321 static ssize_t iwl_dbgfs_bcast_filters_read(struct file *file,
1322                                             char __user *user_buf,
1323                                             size_t count, loff_t *ppos)
1324 {
1325         struct iwl_mvm *mvm = file->private_data;
1326         struct iwl_bcast_filter_cmd cmd;
1327         const struct iwl_fw_bcast_filter *filter;
1328         char *buf;
1329         int bufsz = 1024;
1330         int i, j, pos = 0;
1331         ssize_t ret;
1332
1333         buf = kzalloc(bufsz, GFP_KERNEL);
1334         if (!buf)
1335                 return -ENOMEM;
1336
1337         mutex_lock(&mvm->mutex);
1338         if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd)) {
1339                 ADD_TEXT("None\n");
1340                 mutex_unlock(&mvm->mutex);
1341                 goto out;
1342         }
1343         mutex_unlock(&mvm->mutex);
1344
1345         for (i = 0; cmd.filters[i].attrs[0].mask; i++) {
1346                 filter = &cmd.filters[i];
1347
1348                 ADD_TEXT("Filter [%d]:\n", i);
1349                 ADD_TEXT("\tDiscard=%d\n", filter->discard);
1350                 ADD_TEXT("\tFrame Type: %s\n",
1351                          filter->frame_type ? "IPv4" : "Generic");
1352
1353                 for (j = 0; j < ARRAY_SIZE(filter->attrs); j++) {
1354                         const struct iwl_fw_bcast_filter_attr *attr;
1355
1356                         attr = &filter->attrs[j];
1357                         if (!attr->mask)
1358                                 break;
1359
1360                         ADD_TEXT("\tAttr [%d]: offset=%d (from %s), mask=0x%x, value=0x%x reserved=0x%x\n",
1361                                  j, attr->offset,
1362                                  attr->offset_type ? "IP End" :
1363                                                      "Payload Start",
1364                                  be32_to_cpu(attr->mask),
1365                                  be32_to_cpu(attr->val),
1366                                  le16_to_cpu(attr->reserved1));
1367                 }
1368         }
1369 out:
1370         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1371         kfree(buf);
1372         return ret;
1373 }
1374
1375 static ssize_t iwl_dbgfs_bcast_filters_write(struct iwl_mvm *mvm, char *buf,
1376                                              size_t count, loff_t *ppos)
1377 {
1378         int pos, next_pos;
1379         struct iwl_fw_bcast_filter filter = {};
1380         struct iwl_bcast_filter_cmd cmd;
1381         u32 filter_id, attr_id, mask, value;
1382         int err = 0;
1383
1384         if (sscanf(buf, "%d %hhi %hhi %n", &filter_id, &filter.discard,
1385                    &filter.frame_type, &pos) != 3)
1386                 return -EINVAL;
1387
1388         if (filter_id >= ARRAY_SIZE(mvm->dbgfs_bcast_filtering.cmd.filters) ||
1389             filter.frame_type > BCAST_FILTER_FRAME_TYPE_IPV4)
1390                 return -EINVAL;
1391
1392         for (attr_id = 0; attr_id < ARRAY_SIZE(filter.attrs);
1393              attr_id++) {
1394                 struct iwl_fw_bcast_filter_attr *attr =
1395                                 &filter.attrs[attr_id];
1396
1397                 if (pos >= count)
1398                         break;
1399
1400                 if (sscanf(&buf[pos], "%hhi %hhi %i %i %n",
1401                            &attr->offset, &attr->offset_type,
1402                            &mask, &value, &next_pos) != 4)
1403                         return -EINVAL;
1404
1405                 attr->mask = cpu_to_be32(mask);
1406                 attr->val = cpu_to_be32(value);
1407                 if (mask)
1408                         filter.num_attrs++;
1409
1410                 pos += next_pos;
1411         }
1412
1413         mutex_lock(&mvm->mutex);
1414         memcpy(&mvm->dbgfs_bcast_filtering.cmd.filters[filter_id],
1415                &filter, sizeof(filter));
1416
1417         /* send updated bcast filtering configuration */
1418         if (iwl_mvm_firmware_running(mvm) &&
1419             mvm->dbgfs_bcast_filtering.override &&
1420             iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
1421                 err = iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, 0,
1422                                            sizeof(cmd), &cmd);
1423         mutex_unlock(&mvm->mutex);
1424
1425         return err ?: count;
1426 }
1427
1428 static ssize_t iwl_dbgfs_bcast_filters_macs_read(struct file *file,
1429                                                  char __user *user_buf,
1430                                                  size_t count, loff_t *ppos)
1431 {
1432         struct iwl_mvm *mvm = file->private_data;
1433         struct iwl_bcast_filter_cmd cmd;
1434         char *buf;
1435         int bufsz = 1024;
1436         int i, pos = 0;
1437         ssize_t ret;
1438
1439         buf = kzalloc(bufsz, GFP_KERNEL);
1440         if (!buf)
1441                 return -ENOMEM;
1442
1443         mutex_lock(&mvm->mutex);
1444         if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd)) {
1445                 ADD_TEXT("None\n");
1446                 mutex_unlock(&mvm->mutex);
1447                 goto out;
1448         }
1449         mutex_unlock(&mvm->mutex);
1450
1451         for (i = 0; i < ARRAY_SIZE(cmd.macs); i++) {
1452                 const struct iwl_fw_bcast_mac *mac = &cmd.macs[i];
1453
1454                 ADD_TEXT("Mac [%d]: discard=%d attached_filters=0x%x\n",
1455                          i, mac->default_discard, mac->attached_filters);
1456         }
1457 out:
1458         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1459         kfree(buf);
1460         return ret;
1461 }
1462
1463 static ssize_t iwl_dbgfs_bcast_filters_macs_write(struct iwl_mvm *mvm,
1464                                                   char *buf, size_t count,
1465                                                   loff_t *ppos)
1466 {
1467         struct iwl_bcast_filter_cmd cmd;
1468         struct iwl_fw_bcast_mac mac = {};
1469         u32 mac_id, attached_filters;
1470         int err = 0;
1471
1472         if (!mvm->bcast_filters)
1473                 return -ENOENT;
1474
1475         if (sscanf(buf, "%d %hhi %i", &mac_id, &mac.default_discard,
1476                    &attached_filters) != 3)
1477                 return -EINVAL;
1478
1479         if (mac_id >= ARRAY_SIZE(cmd.macs) ||
1480             mac.default_discard > 1 ||
1481             attached_filters >= BIT(ARRAY_SIZE(cmd.filters)))
1482                 return -EINVAL;
1483
1484         mac.attached_filters = cpu_to_le16(attached_filters);
1485
1486         mutex_lock(&mvm->mutex);
1487         memcpy(&mvm->dbgfs_bcast_filtering.cmd.macs[mac_id],
1488                &mac, sizeof(mac));
1489
1490         /* send updated bcast filtering configuration */
1491         if (iwl_mvm_firmware_running(mvm) &&
1492             mvm->dbgfs_bcast_filtering.override &&
1493             iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
1494                 err = iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, 0,
1495                                            sizeof(cmd), &cmd);
1496         mutex_unlock(&mvm->mutex);
1497
1498         return err ?: count;
1499 }
1500 #endif
1501
1502 #ifdef CONFIG_PM_SLEEP
1503 static ssize_t iwl_dbgfs_d3_sram_write(struct iwl_mvm *mvm, char *buf,
1504                                        size_t count, loff_t *ppos)
1505 {
1506         int store;
1507
1508         if (sscanf(buf, "%d", &store) != 1)
1509                 return -EINVAL;
1510
1511         mvm->store_d3_resume_sram = store;
1512
1513         return count;
1514 }
1515
1516 static ssize_t iwl_dbgfs_d3_sram_read(struct file *file, char __user *user_buf,
1517                                       size_t count, loff_t *ppos)
1518 {
1519         struct iwl_mvm *mvm = file->private_data;
1520         const struct fw_img *img;
1521         int ofs, len, pos = 0;
1522         size_t bufsz, ret;
1523         char *buf;
1524         u8 *ptr = mvm->d3_resume_sram;
1525
1526         img = &mvm->fw->img[IWL_UCODE_WOWLAN];
1527         len = img->sec[IWL_UCODE_SECTION_DATA].len;
1528
1529         bufsz = len * 4 + 256;
1530         buf = kzalloc(bufsz, GFP_KERNEL);
1531         if (!buf)
1532                 return -ENOMEM;
1533
1534         pos += scnprintf(buf, bufsz, "D3 SRAM capture: %sabled\n",
1535                          mvm->store_d3_resume_sram ? "en" : "dis");
1536
1537         if (ptr) {
1538                 for (ofs = 0; ofs < len; ofs += 16) {
1539                         pos += scnprintf(buf + pos, bufsz - pos,
1540                                          "0x%.4x %16ph\n", ofs, ptr + ofs);
1541                 }
1542         } else {
1543                 pos += scnprintf(buf + pos, bufsz - pos,
1544                                  "(no data captured)\n");
1545         }
1546
1547         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1548
1549         kfree(buf);
1550
1551         return ret;
1552 }
1553 #endif
1554
1555 #define PRINT_MVM_REF(ref) do {                                         \
1556         if (mvm->refs[ref])                                             \
1557                 pos += scnprintf(buf + pos, bufsz - pos,                \
1558                                  "\t(0x%lx): %d %s\n",                  \
1559                                  BIT(ref), mvm->refs[ref], #ref);       \
1560 } while (0)
1561
1562 static ssize_t iwl_dbgfs_d0i3_refs_read(struct file *file,
1563                                         char __user *user_buf,
1564                                         size_t count, loff_t *ppos)
1565 {
1566         struct iwl_mvm *mvm = file->private_data;
1567         int i, pos = 0;
1568         char buf[256];
1569         const size_t bufsz = sizeof(buf);
1570         u32 refs = 0;
1571
1572         for (i = 0; i < IWL_MVM_REF_COUNT; i++)
1573                 if (mvm->refs[i])
1574                         refs |= BIT(i);
1575
1576         pos += scnprintf(buf + pos, bufsz - pos, "taken mvm refs: 0x%x\n",
1577                          refs);
1578
1579         PRINT_MVM_REF(IWL_MVM_REF_UCODE_DOWN);
1580         PRINT_MVM_REF(IWL_MVM_REF_SCAN);
1581         PRINT_MVM_REF(IWL_MVM_REF_ROC);
1582         PRINT_MVM_REF(IWL_MVM_REF_ROC_AUX);
1583         PRINT_MVM_REF(IWL_MVM_REF_P2P_CLIENT);
1584         PRINT_MVM_REF(IWL_MVM_REF_AP_IBSS);
1585         PRINT_MVM_REF(IWL_MVM_REF_USER);
1586         PRINT_MVM_REF(IWL_MVM_REF_TX);
1587         PRINT_MVM_REF(IWL_MVM_REF_TX_AGG);
1588         PRINT_MVM_REF(IWL_MVM_REF_ADD_IF);
1589         PRINT_MVM_REF(IWL_MVM_REF_START_AP);
1590         PRINT_MVM_REF(IWL_MVM_REF_BSS_CHANGED);
1591         PRINT_MVM_REF(IWL_MVM_REF_PREPARE_TX);
1592         PRINT_MVM_REF(IWL_MVM_REF_PROTECT_TDLS);
1593         PRINT_MVM_REF(IWL_MVM_REF_CHECK_CTKILL);
1594         PRINT_MVM_REF(IWL_MVM_REF_PRPH_READ);
1595         PRINT_MVM_REF(IWL_MVM_REF_PRPH_WRITE);
1596         PRINT_MVM_REF(IWL_MVM_REF_NMI);
1597         PRINT_MVM_REF(IWL_MVM_REF_TM_CMD);
1598         PRINT_MVM_REF(IWL_MVM_REF_EXIT_WORK);
1599         PRINT_MVM_REF(IWL_MVM_REF_PROTECT_CSA);
1600         PRINT_MVM_REF(IWL_MVM_REF_FW_DBG_COLLECT);
1601         PRINT_MVM_REF(IWL_MVM_REF_INIT_UCODE);
1602         PRINT_MVM_REF(IWL_MVM_REF_SENDING_CMD);
1603         PRINT_MVM_REF(IWL_MVM_REF_RX);
1604
1605         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1606 }
1607
1608 static ssize_t iwl_dbgfs_d0i3_refs_write(struct iwl_mvm *mvm, char *buf,
1609                                          size_t count, loff_t *ppos)
1610 {
1611         unsigned long value;
1612         int ret;
1613         bool taken;
1614
1615         ret = kstrtoul(buf, 10, &value);
1616         if (ret < 0)
1617                 return ret;
1618
1619         mutex_lock(&mvm->mutex);
1620
1621         taken = mvm->refs[IWL_MVM_REF_USER];
1622         if (value == 1 && !taken)
1623                 iwl_mvm_ref(mvm, IWL_MVM_REF_USER);
1624         else if (value == 0 && taken)
1625                 iwl_mvm_unref(mvm, IWL_MVM_REF_USER);
1626         else
1627                 ret = -EINVAL;
1628
1629         mutex_unlock(&mvm->mutex);
1630
1631         if (ret < 0)
1632                 return ret;
1633         return count;
1634 }
1635
1636 #define MVM_DEBUGFS_WRITE_FILE_OPS(name, bufsz) \
1637         _MVM_DEBUGFS_WRITE_FILE_OPS(name, bufsz, struct iwl_mvm)
1638 #define MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz) \
1639         _MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz, struct iwl_mvm)
1640 #define MVM_DEBUGFS_ADD_FILE_ALIAS(alias, name, parent, mode) do {      \
1641                 if (!debugfs_create_file(alias, mode, parent, mvm,      \
1642                                          &iwl_dbgfs_##name##_ops))      \
1643                         goto err;                                       \
1644         } while (0)
1645 #define MVM_DEBUGFS_ADD_FILE(name, parent, mode) \
1646         MVM_DEBUGFS_ADD_FILE_ALIAS(#name, name, parent, mode)
1647
1648 #define MVM_DEBUGFS_WRITE_STA_FILE_OPS(name, bufsz) \
1649         _MVM_DEBUGFS_WRITE_FILE_OPS(name, bufsz, struct ieee80211_sta)
1650 #define MVM_DEBUGFS_READ_WRITE_STA_FILE_OPS(name, bufsz) \
1651         _MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz, struct ieee80211_sta)
1652
1653 #define MVM_DEBUGFS_ADD_STA_FILE_ALIAS(alias, name, parent, mode) do {  \
1654                 if (!debugfs_create_file(alias, mode, parent, sta,      \
1655                                          &iwl_dbgfs_##name##_ops))      \
1656                         goto err;                                       \
1657         } while (0)
1658 #define MVM_DEBUGFS_ADD_STA_FILE(name, parent, mode) \
1659         MVM_DEBUGFS_ADD_STA_FILE_ALIAS(#name, name, parent, mode)
1660
1661 static ssize_t
1662 iwl_dbgfs_prph_reg_read(struct file *file,
1663                         char __user *user_buf,
1664                         size_t count, loff_t *ppos)
1665 {
1666         struct iwl_mvm *mvm = file->private_data;
1667         int pos = 0;
1668         char buf[32];
1669         const size_t bufsz = sizeof(buf);
1670         int ret;
1671
1672         if (!mvm->dbgfs_prph_reg_addr)
1673                 return -EINVAL;
1674
1675         ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PRPH_READ);
1676         if (ret)
1677                 return ret;
1678
1679         pos += scnprintf(buf + pos, bufsz - pos, "Reg 0x%x: (0x%x)\n",
1680                 mvm->dbgfs_prph_reg_addr,
1681                 iwl_read_prph(mvm->trans, mvm->dbgfs_prph_reg_addr));
1682
1683         iwl_mvm_unref(mvm, IWL_MVM_REF_PRPH_READ);
1684
1685         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1686 }
1687
1688 static ssize_t
1689 iwl_dbgfs_prph_reg_write(struct iwl_mvm *mvm, char *buf,
1690                          size_t count, loff_t *ppos)
1691 {
1692         u8 args;
1693         u32 value;
1694         int ret;
1695
1696         args = sscanf(buf, "%i %i", &mvm->dbgfs_prph_reg_addr, &value);
1697         /* if we only want to set the reg address - nothing more to do */
1698         if (args == 1)
1699                 goto out;
1700
1701         /* otherwise, make sure we have both address and value */
1702         if (args != 2)
1703                 return -EINVAL;
1704
1705         ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PRPH_WRITE);
1706         if (ret)
1707                 return ret;
1708
1709         iwl_write_prph(mvm->trans, mvm->dbgfs_prph_reg_addr, value);
1710
1711         iwl_mvm_unref(mvm, IWL_MVM_REF_PRPH_WRITE);
1712 out:
1713         return count;
1714 }
1715
1716 static ssize_t
1717 iwl_dbgfs_send_echo_cmd_write(struct iwl_mvm *mvm, char *buf,
1718                               size_t count, loff_t *ppos)
1719 {
1720         int ret;
1721
1722         if (!iwl_mvm_firmware_running(mvm))
1723                 return -EIO;
1724
1725         mutex_lock(&mvm->mutex);
1726         ret = iwl_mvm_send_cmd_pdu(mvm, ECHO_CMD, 0, 0, NULL);
1727         mutex_unlock(&mvm->mutex);
1728
1729         return ret ?: count;
1730 }
1731
1732 MVM_DEBUGFS_READ_WRITE_FILE_OPS(prph_reg, 64);
1733
1734 /* Device wide debugfs entries */
1735 MVM_DEBUGFS_READ_FILE_OPS(ctdp_budget);
1736 MVM_DEBUGFS_WRITE_FILE_OPS(stop_ctdp, 8);
1737 MVM_DEBUGFS_WRITE_FILE_OPS(force_ctkill, 8);
1738 MVM_DEBUGFS_WRITE_FILE_OPS(tx_flush, 16);
1739 MVM_DEBUGFS_WRITE_FILE_OPS(sta_drain, 8);
1740 MVM_DEBUGFS_WRITE_FILE_OPS(send_echo_cmd, 8);
1741 MVM_DEBUGFS_READ_WRITE_FILE_OPS(sram, 64);
1742 MVM_DEBUGFS_READ_WRITE_FILE_OPS(set_nic_temperature, 64);
1743 MVM_DEBUGFS_READ_FILE_OPS(nic_temp);
1744 MVM_DEBUGFS_READ_FILE_OPS(stations);
1745 MVM_DEBUGFS_READ_FILE_OPS(rs_data);
1746 MVM_DEBUGFS_READ_FILE_OPS(bt_notif);
1747 MVM_DEBUGFS_READ_FILE_OPS(bt_cmd);
1748 MVM_DEBUGFS_READ_WRITE_FILE_OPS(disable_power_off, 64);
1749 MVM_DEBUGFS_READ_FILE_OPS(fw_rx_stats);
1750 MVM_DEBUGFS_READ_FILE_OPS(drv_rx_stats);
1751 MVM_DEBUGFS_READ_FILE_OPS(fw_ver);
1752 MVM_DEBUGFS_WRITE_FILE_OPS(fw_restart, 10);
1753 MVM_DEBUGFS_WRITE_FILE_OPS(fw_nmi, 10);
1754 MVM_DEBUGFS_WRITE_FILE_OPS(bt_tx_prio, 10);
1755 MVM_DEBUGFS_WRITE_FILE_OPS(bt_force_ant, 10);
1756 MVM_DEBUGFS_READ_WRITE_FILE_OPS(scan_ant_rxchain, 8);
1757 MVM_DEBUGFS_READ_WRITE_FILE_OPS(d0i3_refs, 8);
1758 MVM_DEBUGFS_READ_WRITE_FILE_OPS(fw_dbg_conf, 8);
1759 MVM_DEBUGFS_WRITE_FILE_OPS(fw_dbg_collect, 64);
1760 MVM_DEBUGFS_WRITE_FILE_OPS(cont_recording, 8);
1761 MVM_DEBUGFS_WRITE_FILE_OPS(max_amsdu_len, 8);
1762 MVM_DEBUGFS_WRITE_FILE_OPS(indirection_tbl,
1763                            (IWL_RSS_INDIRECTION_TABLE_SIZE * 2));
1764 MVM_DEBUGFS_WRITE_FILE_OPS(inject_packet, 512);
1765
1766 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1767 MVM_DEBUGFS_READ_WRITE_FILE_OPS(bcast_filters, 256);
1768 MVM_DEBUGFS_READ_WRITE_FILE_OPS(bcast_filters_macs, 256);
1769 #endif
1770
1771 #ifdef CONFIG_PM_SLEEP
1772 MVM_DEBUGFS_READ_WRITE_FILE_OPS(d3_sram, 8);
1773 #endif
1774 #ifdef CONFIG_ACPI
1775 MVM_DEBUGFS_READ_FILE_OPS(sar_geo_profile);
1776 #endif
1777
1778 static ssize_t iwl_dbgfs_mem_read(struct file *file, char __user *user_buf,
1779                                   size_t count, loff_t *ppos)
1780 {
1781         struct iwl_mvm *mvm = file->private_data;
1782         struct iwl_dbg_mem_access_cmd cmd = {};
1783         struct iwl_dbg_mem_access_rsp *rsp;
1784         struct iwl_host_cmd hcmd = {
1785                 .flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
1786                 .data = { &cmd, },
1787                 .len = { sizeof(cmd) },
1788         };
1789         size_t delta;
1790         ssize_t ret, len;
1791
1792         if (!iwl_mvm_firmware_running(mvm))
1793                 return -EIO;
1794
1795         hcmd.id = iwl_cmd_id(*ppos >> 24 ? UMAC_RD_WR : LMAC_RD_WR,
1796                              DEBUG_GROUP, 0);
1797         cmd.op = cpu_to_le32(DEBUG_MEM_OP_READ);
1798
1799         /* Take care of alignment of both the position and the length */
1800         delta = *ppos & 0x3;
1801         cmd.addr = cpu_to_le32(*ppos - delta);
1802         cmd.len = cpu_to_le32(min(ALIGN(count + delta, 4) / 4,
1803                                   (size_t)DEBUG_MEM_MAX_SIZE_DWORDS));
1804
1805         mutex_lock(&mvm->mutex);
1806         ret = iwl_mvm_send_cmd(mvm, &hcmd);
1807         mutex_unlock(&mvm->mutex);
1808
1809         if (ret < 0)
1810                 return ret;
1811
1812         rsp = (void *)hcmd.resp_pkt->data;
1813         if (le32_to_cpu(rsp->status) != DEBUG_MEM_STATUS_SUCCESS) {
1814                 ret = -ENXIO;
1815                 goto out;
1816         }
1817
1818         len = min((size_t)le32_to_cpu(rsp->len) << 2,
1819                   iwl_rx_packet_payload_len(hcmd.resp_pkt) - sizeof(*rsp));
1820         len = min(len - delta, count);
1821         if (len < 0) {
1822                 ret = -EFAULT;
1823                 goto out;
1824         }
1825
1826         ret = len - copy_to_user(user_buf, (void *)rsp->data + delta, len);
1827         *ppos += ret;
1828
1829 out:
1830         iwl_free_resp(&hcmd);
1831         return ret;
1832 }
1833
1834 static ssize_t iwl_dbgfs_mem_write(struct file *file,
1835                                    const char __user *user_buf, size_t count,
1836                                    loff_t *ppos)
1837 {
1838         struct iwl_mvm *mvm = file->private_data;
1839         struct iwl_dbg_mem_access_cmd *cmd;
1840         struct iwl_dbg_mem_access_rsp *rsp;
1841         struct iwl_host_cmd hcmd = {};
1842         size_t cmd_size;
1843         size_t data_size;
1844         u32 op, len;
1845         ssize_t ret;
1846
1847         if (!iwl_mvm_firmware_running(mvm))
1848                 return -EIO;
1849
1850         hcmd.id = iwl_cmd_id(*ppos >> 24 ? UMAC_RD_WR : LMAC_RD_WR,
1851                              DEBUG_GROUP, 0);
1852
1853         if (*ppos & 0x3 || count < 4) {
1854                 op = DEBUG_MEM_OP_WRITE_BYTES;
1855                 len = min(count, (size_t)(4 - (*ppos & 0x3)));
1856                 data_size = len;
1857         } else {
1858                 op = DEBUG_MEM_OP_WRITE;
1859                 len = min(count >> 2, (size_t)DEBUG_MEM_MAX_SIZE_DWORDS);
1860                 data_size = len << 2;
1861         }
1862
1863         cmd_size = sizeof(*cmd) + ALIGN(data_size, 4);
1864         cmd = kzalloc(cmd_size, GFP_KERNEL);
1865         if (!cmd)
1866                 return -ENOMEM;
1867
1868         cmd->op = cpu_to_le32(op);
1869         cmd->len = cpu_to_le32(len);
1870         cmd->addr = cpu_to_le32(*ppos);
1871         if (copy_from_user((void *)cmd->data, user_buf, data_size)) {
1872                 kfree(cmd);
1873                 return -EFAULT;
1874         }
1875
1876         hcmd.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
1877         hcmd.data[0] = (void *)cmd;
1878         hcmd.len[0] = cmd_size;
1879
1880         mutex_lock(&mvm->mutex);
1881         ret = iwl_mvm_send_cmd(mvm, &hcmd);
1882         mutex_unlock(&mvm->mutex);
1883
1884         kfree(cmd);
1885
1886         if (ret < 0)
1887                 return ret;
1888
1889         rsp = (void *)hcmd.resp_pkt->data;
1890         if (rsp->status != DEBUG_MEM_STATUS_SUCCESS) {
1891                 ret = -ENXIO;
1892                 goto out;
1893         }
1894
1895         ret = data_size;
1896         *ppos += ret;
1897
1898 out:
1899         iwl_free_resp(&hcmd);
1900         return ret;
1901 }
1902
1903 static const struct file_operations iwl_dbgfs_mem_ops = {
1904         .read = iwl_dbgfs_mem_read,
1905         .write = iwl_dbgfs_mem_write,
1906         .open = simple_open,
1907         .llseek = default_llseek,
1908 };
1909
1910 void iwl_mvm_sta_add_debugfs(struct ieee80211_hw *hw,
1911                              struct ieee80211_vif *vif,
1912                              struct ieee80211_sta *sta,
1913                              struct dentry *dir)
1914 {
1915         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1916
1917         if (iwl_mvm_has_tlc_offload(mvm))
1918                 MVM_DEBUGFS_ADD_STA_FILE(rs_data, dir, S_IRUSR);
1919
1920         return;
1921 err:
1922         IWL_ERR(mvm, "Can't create the mvm station debugfs entry\n");
1923 }
1924
1925 int iwl_mvm_dbgfs_register(struct iwl_mvm *mvm, struct dentry *dbgfs_dir)
1926 {
1927         struct dentry *bcast_dir __maybe_unused;
1928         char buf[100];
1929
1930         spin_lock_init(&mvm->drv_stats_lock);
1931
1932         mvm->debugfs_dir = dbgfs_dir;
1933
1934         MVM_DEBUGFS_ADD_FILE(tx_flush, mvm->debugfs_dir, S_IWUSR);
1935         MVM_DEBUGFS_ADD_FILE(sta_drain, mvm->debugfs_dir, S_IWUSR);
1936         MVM_DEBUGFS_ADD_FILE(sram, mvm->debugfs_dir, S_IWUSR | S_IRUSR);
1937         MVM_DEBUGFS_ADD_FILE(set_nic_temperature, mvm->debugfs_dir,
1938                              S_IWUSR | S_IRUSR);
1939         MVM_DEBUGFS_ADD_FILE(nic_temp, dbgfs_dir, S_IRUSR);
1940         MVM_DEBUGFS_ADD_FILE(ctdp_budget, dbgfs_dir, S_IRUSR);
1941         MVM_DEBUGFS_ADD_FILE(stop_ctdp, dbgfs_dir, S_IWUSR);
1942         MVM_DEBUGFS_ADD_FILE(force_ctkill, dbgfs_dir, S_IWUSR);
1943         MVM_DEBUGFS_ADD_FILE(stations, dbgfs_dir, S_IRUSR);
1944         MVM_DEBUGFS_ADD_FILE(bt_notif, dbgfs_dir, S_IRUSR);
1945         MVM_DEBUGFS_ADD_FILE(bt_cmd, dbgfs_dir, S_IRUSR);
1946         MVM_DEBUGFS_ADD_FILE(disable_power_off, mvm->debugfs_dir,
1947                              S_IRUSR | S_IWUSR);
1948         MVM_DEBUGFS_ADD_FILE(fw_ver, mvm->debugfs_dir, S_IRUSR);
1949         MVM_DEBUGFS_ADD_FILE(fw_rx_stats, mvm->debugfs_dir, S_IRUSR);
1950         MVM_DEBUGFS_ADD_FILE(drv_rx_stats, mvm->debugfs_dir, S_IRUSR);
1951         MVM_DEBUGFS_ADD_FILE(fw_restart, mvm->debugfs_dir, S_IWUSR);
1952         MVM_DEBUGFS_ADD_FILE(fw_nmi, mvm->debugfs_dir, S_IWUSR);
1953         MVM_DEBUGFS_ADD_FILE(bt_tx_prio, mvm->debugfs_dir, S_IWUSR);
1954         MVM_DEBUGFS_ADD_FILE(bt_force_ant, mvm->debugfs_dir, S_IWUSR);
1955         MVM_DEBUGFS_ADD_FILE(scan_ant_rxchain, mvm->debugfs_dir,
1956                              S_IWUSR | S_IRUSR);
1957         MVM_DEBUGFS_ADD_FILE(prph_reg, mvm->debugfs_dir, S_IWUSR | S_IRUSR);
1958         MVM_DEBUGFS_ADD_FILE(d0i3_refs, mvm->debugfs_dir, S_IRUSR | S_IWUSR);
1959         MVM_DEBUGFS_ADD_FILE(fw_dbg_conf, mvm->debugfs_dir, S_IRUSR | S_IWUSR);
1960         MVM_DEBUGFS_ADD_FILE(fw_dbg_collect, mvm->debugfs_dir, S_IWUSR);
1961         MVM_DEBUGFS_ADD_FILE(max_amsdu_len, mvm->debugfs_dir, S_IWUSR);
1962         MVM_DEBUGFS_ADD_FILE(send_echo_cmd, mvm->debugfs_dir, S_IWUSR);
1963         MVM_DEBUGFS_ADD_FILE(cont_recording, mvm->debugfs_dir, S_IWUSR);
1964         MVM_DEBUGFS_ADD_FILE(indirection_tbl, mvm->debugfs_dir, S_IWUSR);
1965         MVM_DEBUGFS_ADD_FILE(inject_packet, mvm->debugfs_dir, S_IWUSR);
1966 #ifdef CONFIG_ACPI
1967         MVM_DEBUGFS_ADD_FILE(sar_geo_profile, dbgfs_dir, S_IRUSR);
1968 #endif
1969
1970         if (!debugfs_create_bool("enable_scan_iteration_notif",
1971                                  S_IRUSR | S_IWUSR,
1972                                  mvm->debugfs_dir,
1973                                  &mvm->scan_iter_notif_enabled))
1974                 goto err;
1975         if (!debugfs_create_bool("drop_bcn_ap_mode", S_IRUSR | S_IWUSR,
1976                                  mvm->debugfs_dir, &mvm->drop_bcn_ap_mode))
1977                 goto err;
1978
1979 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1980         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BCAST_FILTERING) {
1981                 bcast_dir = debugfs_create_dir("bcast_filtering",
1982                                                mvm->debugfs_dir);
1983                 if (!bcast_dir)
1984                         goto err;
1985
1986                 if (!debugfs_create_bool("override", S_IRUSR | S_IWUSR,
1987                                 bcast_dir,
1988                                 &mvm->dbgfs_bcast_filtering.override))
1989                         goto err;
1990
1991                 MVM_DEBUGFS_ADD_FILE_ALIAS("filters", bcast_filters,
1992                                            bcast_dir, S_IWUSR | S_IRUSR);
1993                 MVM_DEBUGFS_ADD_FILE_ALIAS("macs", bcast_filters_macs,
1994                                            bcast_dir, S_IWUSR | S_IRUSR);
1995         }
1996 #endif
1997
1998 #ifdef CONFIG_PM_SLEEP
1999         MVM_DEBUGFS_ADD_FILE(d3_sram, mvm->debugfs_dir, S_IRUSR | S_IWUSR);
2000         MVM_DEBUGFS_ADD_FILE(d3_test, mvm->debugfs_dir, S_IRUSR);
2001         if (!debugfs_create_bool("d3_wake_sysassert", S_IRUSR | S_IWUSR,
2002                                  mvm->debugfs_dir, &mvm->d3_wake_sysassert))
2003                 goto err;
2004         if (!debugfs_create_u32("last_netdetect_scans", S_IRUSR,
2005                                 mvm->debugfs_dir, &mvm->last_netdetect_scans))
2006                 goto err;
2007 #endif
2008
2009         if (!debugfs_create_u8("ps_disabled", S_IRUSR,
2010                                mvm->debugfs_dir, &mvm->ps_disabled))
2011                 goto err;
2012         if (!debugfs_create_blob("nvm_hw", S_IRUSR,
2013                                   mvm->debugfs_dir, &mvm->nvm_hw_blob))
2014                 goto err;
2015         if (!debugfs_create_blob("nvm_sw", S_IRUSR,
2016                                   mvm->debugfs_dir, &mvm->nvm_sw_blob))
2017                 goto err;
2018         if (!debugfs_create_blob("nvm_calib", S_IRUSR,
2019                                   mvm->debugfs_dir, &mvm->nvm_calib_blob))
2020                 goto err;
2021         if (!debugfs_create_blob("nvm_prod", S_IRUSR,
2022                                   mvm->debugfs_dir, &mvm->nvm_prod_blob))
2023                 goto err;
2024         if (!debugfs_create_blob("nvm_phy_sku", S_IRUSR,
2025                                  mvm->debugfs_dir, &mvm->nvm_phy_sku_blob))
2026                 goto err;
2027
2028         debugfs_create_file("mem", S_IRUSR | S_IWUSR, dbgfs_dir, mvm,
2029                             &iwl_dbgfs_mem_ops);
2030
2031         /*
2032          * Create a symlink with mac80211. It will be removed when mac80211
2033          * exists (before the opmode exists which removes the target.)
2034          */
2035         snprintf(buf, 100, "../../%pd2", dbgfs_dir->d_parent);
2036         if (!debugfs_create_symlink("iwlwifi", mvm->hw->wiphy->debugfsdir, buf))
2037                 goto err;
2038
2039         return 0;
2040 err:
2041         IWL_ERR(mvm, "Can't create the mvm debugfs directory\n");
2042         return -ENOMEM;
2043 }