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