c037b2ad5fa17fb637f43b6bc3737239029c57a0
[platform/kernel/linux-rpi.git] / drivers / net / wireless / intel / iwlwifi / mvm / debugfs.c
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2023 Intel Corporation
4  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5  * Copyright (C) 2016-2017 Intel Deutschland GmbH
6  */
7 #include <linux/vmalloc.h>
8 #include <linux/err.h>
9 #include <linux/ieee80211.h>
10 #include <linux/netdevice.h>
11 #include <linux/dmi.h>
12
13 #include "mvm.h"
14 #include "sta.h"
15 #include "iwl-io.h"
16 #include "debugfs.h"
17 #include "iwl-modparams.h"
18 #include "fw/error-dump.h"
19 #include "fw/api/phy-ctxt.h"
20
21 static ssize_t iwl_dbgfs_ctdp_budget_read(struct file *file,
22                                           char __user *user_buf,
23                                           size_t count, loff_t *ppos)
24 {
25         struct iwl_mvm *mvm = file->private_data;
26         char buf[16];
27         int pos, budget;
28
29         if (!iwl_mvm_is_ctdp_supported(mvm))
30                 return -EOPNOTSUPP;
31
32         if (!iwl_mvm_firmware_running(mvm) ||
33             mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
34                 return -EIO;
35
36         mutex_lock(&mvm->mutex);
37         budget = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_REPORT, 0);
38         mutex_unlock(&mvm->mutex);
39
40         if (budget < 0)
41                 return budget;
42
43         pos = scnprintf(buf, sizeof(buf), "%d\n", budget);
44
45         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
46 }
47
48 static ssize_t iwl_dbgfs_stop_ctdp_write(struct iwl_mvm *mvm, char *buf,
49                                          size_t count, loff_t *ppos)
50 {
51         int ret;
52
53         if (!iwl_mvm_is_ctdp_supported(mvm))
54                 return -EOPNOTSUPP;
55
56         if (!iwl_mvm_firmware_running(mvm) ||
57             mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
58                 return -EIO;
59
60         mutex_lock(&mvm->mutex);
61         ret = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_STOP, 0);
62         mutex_unlock(&mvm->mutex);
63
64         return ret ?: count;
65 }
66
67 static ssize_t iwl_dbgfs_force_ctkill_write(struct iwl_mvm *mvm, char *buf,
68                                             size_t count, loff_t *ppos)
69 {
70         if (!iwl_mvm_firmware_running(mvm) ||
71             mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
72                 return -EIO;
73
74         iwl_mvm_enter_ctkill(mvm);
75
76         return count;
77 }
78
79 static ssize_t iwl_dbgfs_tx_flush_write(struct iwl_mvm *mvm, char *buf,
80                                         size_t count, loff_t *ppos)
81 {
82         int ret;
83         u32 flush_arg;
84
85         if (!iwl_mvm_firmware_running(mvm) ||
86             mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
87                 return -EIO;
88
89         if (kstrtou32(buf, 0, &flush_arg))
90                 return -EINVAL;
91
92         if (iwl_mvm_has_new_tx_api(mvm)) {
93                 IWL_DEBUG_TX_QUEUES(mvm,
94                                     "FLUSHING all tids queues on sta_id = %d\n",
95                                     flush_arg);
96                 mutex_lock(&mvm->mutex);
97                 ret = iwl_mvm_flush_sta_tids(mvm, flush_arg, 0xFFFF)
98                         ? : count;
99                 mutex_unlock(&mvm->mutex);
100                 return ret;
101         }
102
103         IWL_DEBUG_TX_QUEUES(mvm, "FLUSHING queues mask to flush = 0x%x\n",
104                             flush_arg);
105
106         mutex_lock(&mvm->mutex);
107         ret =  iwl_mvm_flush_tx_path(mvm, flush_arg) ? : count;
108         mutex_unlock(&mvm->mutex);
109
110         return ret;
111 }
112
113 static ssize_t iwl_dbgfs_sta_drain_write(struct iwl_mvm *mvm, char *buf,
114                                          size_t count, loff_t *ppos)
115 {
116         struct iwl_mvm_sta *mvmsta;
117         int sta_id, drain, ret;
118
119         if (!iwl_mvm_firmware_running(mvm) ||
120             mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
121                 return -EIO;
122
123         if (sscanf(buf, "%d %d", &sta_id, &drain) != 2)
124                 return -EINVAL;
125         if (sta_id < 0 || sta_id >= mvm->fw->ucode_capa.num_stations)
126                 return -EINVAL;
127         if (drain < 0 || drain > 1)
128                 return -EINVAL;
129
130         mutex_lock(&mvm->mutex);
131
132         mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
133
134         if (!mvmsta)
135                 ret = -ENOENT;
136         else
137                 ret = iwl_mvm_drain_sta(mvm, mvmsta, drain) ? : count;
138
139         mutex_unlock(&mvm->mutex);
140
141         return ret;
142 }
143
144 static ssize_t iwl_dbgfs_sram_read(struct file *file, char __user *user_buf,
145                                    size_t count, loff_t *ppos)
146 {
147         struct iwl_mvm *mvm = file->private_data;
148         const struct fw_img *img;
149         unsigned int ofs, len;
150         size_t ret;
151         u8 *ptr;
152
153         if (!iwl_mvm_firmware_running(mvm))
154                 return -EINVAL;
155
156         /* default is to dump the entire data segment */
157         img = &mvm->fw->img[mvm->fwrt.cur_fw_img];
158         ofs = img->sec[IWL_UCODE_SECTION_DATA].offset;
159         len = img->sec[IWL_UCODE_SECTION_DATA].len;
160
161         if (mvm->dbgfs_sram_len) {
162                 ofs = mvm->dbgfs_sram_offset;
163                 len = mvm->dbgfs_sram_len;
164         }
165
166         ptr = kzalloc(len, GFP_KERNEL);
167         if (!ptr)
168                 return -ENOMEM;
169
170         iwl_trans_read_mem_bytes(mvm->trans, ofs, ptr, len);
171
172         ret = simple_read_from_buffer(user_buf, count, ppos, ptr, len);
173
174         kfree(ptr);
175
176         return ret;
177 }
178
179 static ssize_t iwl_dbgfs_sram_write(struct iwl_mvm *mvm, char *buf,
180                                     size_t count, loff_t *ppos)
181 {
182         const struct fw_img *img;
183         u32 offset, len;
184         u32 img_offset, img_len;
185
186         if (!iwl_mvm_firmware_running(mvm))
187                 return -EINVAL;
188
189         img = &mvm->fw->img[mvm->fwrt.cur_fw_img];
190         img_offset = img->sec[IWL_UCODE_SECTION_DATA].offset;
191         img_len = img->sec[IWL_UCODE_SECTION_DATA].len;
192
193         if (sscanf(buf, "%x,%x", &offset, &len) == 2) {
194                 if ((offset & 0x3) || (len & 0x3))
195                         return -EINVAL;
196
197                 if (offset + len > img_offset + img_len)
198                         return -EINVAL;
199
200                 mvm->dbgfs_sram_offset = offset;
201                 mvm->dbgfs_sram_len = len;
202         } else {
203                 mvm->dbgfs_sram_offset = 0;
204                 mvm->dbgfs_sram_len = 0;
205         }
206
207         return count;
208 }
209
210 static ssize_t iwl_dbgfs_set_nic_temperature_read(struct file *file,
211                                                   char __user *user_buf,
212                                                   size_t count, loff_t *ppos)
213 {
214         struct iwl_mvm *mvm = file->private_data;
215         char buf[16];
216         int pos;
217
218         if (!mvm->temperature_test)
219                 pos = scnprintf(buf, sizeof(buf), "disabled\n");
220         else
221                 pos = scnprintf(buf, sizeof(buf), "%d\n", mvm->temperature);
222
223         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
224 }
225
226 /*
227  * Set NIC Temperature
228  * Cause the driver to ignore the actual NIC temperature reported by the FW
229  * Enable: any value between IWL_MVM_DEBUG_SET_TEMPERATURE_MIN -
230  * IWL_MVM_DEBUG_SET_TEMPERATURE_MAX
231  * Disable: IWL_MVM_DEBUG_SET_TEMPERATURE_DISABLE
232  */
233 static ssize_t iwl_dbgfs_set_nic_temperature_write(struct iwl_mvm *mvm,
234                                                    char *buf, size_t count,
235                                                    loff_t *ppos)
236 {
237         int temperature;
238
239         if (!iwl_mvm_firmware_running(mvm) && !mvm->temperature_test)
240                 return -EIO;
241
242         if (kstrtoint(buf, 10, &temperature))
243                 return -EINVAL;
244         /* not a legal temperature */
245         if ((temperature > IWL_MVM_DEBUG_SET_TEMPERATURE_MAX &&
246              temperature != IWL_MVM_DEBUG_SET_TEMPERATURE_DISABLE) ||
247             temperature < IWL_MVM_DEBUG_SET_TEMPERATURE_MIN)
248                 return -EINVAL;
249
250         mutex_lock(&mvm->mutex);
251         if (temperature == IWL_MVM_DEBUG_SET_TEMPERATURE_DISABLE) {
252                 if (!mvm->temperature_test)
253                         goto out;
254
255                 mvm->temperature_test = false;
256                 /* Since we can't read the temp while awake, just set
257                  * it to zero until we get the next RX stats from the
258                  * firmware.
259                  */
260                 mvm->temperature = 0;
261         } else {
262                 mvm->temperature_test = true;
263                 mvm->temperature = temperature;
264         }
265         IWL_DEBUG_TEMP(mvm, "%sabling debug set temperature (temp = %d)\n",
266                        mvm->temperature_test ? "En" : "Dis",
267                        mvm->temperature);
268         /* handle the temperature change */
269         iwl_mvm_tt_handler(mvm);
270
271 out:
272         mutex_unlock(&mvm->mutex);
273
274         return count;
275 }
276
277 static ssize_t iwl_dbgfs_nic_temp_read(struct file *file,
278                                        char __user *user_buf,
279                                        size_t count, loff_t *ppos)
280 {
281         struct iwl_mvm *mvm = file->private_data;
282         char buf[16];
283         int pos, ret;
284         s32 temp;
285
286         if (!iwl_mvm_firmware_running(mvm))
287                 return -EIO;
288
289         mutex_lock(&mvm->mutex);
290         ret = iwl_mvm_get_temp(mvm, &temp);
291         mutex_unlock(&mvm->mutex);
292
293         if (ret)
294                 return -EIO;
295
296         pos = scnprintf(buf, sizeof(buf), "%d\n", temp);
297
298         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
299 }
300
301 #ifdef CONFIG_ACPI
302 static ssize_t iwl_dbgfs_sar_geo_profile_read(struct file *file,
303                                               char __user *user_buf,
304                                               size_t count, loff_t *ppos)
305 {
306         struct iwl_mvm *mvm = file->private_data;
307         char buf[256];
308         int pos = 0;
309         int bufsz = sizeof(buf);
310         int tbl_idx;
311
312         if (!iwl_mvm_firmware_running(mvm))
313                 return -EIO;
314
315         mutex_lock(&mvm->mutex);
316         tbl_idx = iwl_mvm_get_sar_geo_profile(mvm);
317         if (tbl_idx < 0) {
318                 mutex_unlock(&mvm->mutex);
319                 return tbl_idx;
320         }
321
322         if (!tbl_idx) {
323                 pos = scnprintf(buf, bufsz,
324                                 "SAR geographic profile disabled\n");
325         } else {
326                 pos += scnprintf(buf + pos, bufsz - pos,
327                                  "Use geographic profile %d\n", tbl_idx);
328                 pos += scnprintf(buf + pos, bufsz - pos,
329                                  "2.4GHz:\n\tChain A offset: %u dBm\n\tChain B offset: %u dBm\n\tmax tx power: %u dBm\n",
330                                  mvm->fwrt.geo_profiles[tbl_idx - 1].bands[0].chains[0],
331                                  mvm->fwrt.geo_profiles[tbl_idx - 1].bands[0].chains[1],
332                                  mvm->fwrt.geo_profiles[tbl_idx - 1].bands[0].max);
333                 pos += scnprintf(buf + pos, bufsz - pos,
334                                  "5.2GHz:\n\tChain A offset: %u dBm\n\tChain B offset: %u dBm\n\tmax tx power: %u dBm\n",
335                                  mvm->fwrt.geo_profiles[tbl_idx - 1].bands[1].chains[0],
336                                  mvm->fwrt.geo_profiles[tbl_idx - 1].bands[1].chains[1],
337                                  mvm->fwrt.geo_profiles[tbl_idx - 1].bands[1].max);
338         }
339         mutex_unlock(&mvm->mutex);
340
341         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
342 }
343
344 static ssize_t iwl_dbgfs_wifi_6e_enable_read(struct file *file,
345                                              char __user *user_buf,
346                                              size_t count, loff_t *ppos)
347 {
348         struct iwl_mvm *mvm = file->private_data;
349         int err, pos;
350         char buf[12];
351         u32 value;
352
353         err = iwl_acpi_get_dsm_u32(mvm->fwrt.dev, 0,
354                                    DSM_FUNC_ENABLE_6E,
355                                    &iwl_guid, &value);
356         if (err)
357                 return err;
358
359         pos = sprintf(buf, "0x%08x\n", value);
360
361         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
362 }
363 #endif
364
365 static ssize_t iwl_dbgfs_stations_read(struct file *file, char __user *user_buf,
366                                        size_t count, loff_t *ppos)
367 {
368         struct iwl_mvm *mvm = file->private_data;
369         struct ieee80211_sta *sta;
370         char buf[400];
371         int i, pos = 0, bufsz = sizeof(buf);
372
373         mutex_lock(&mvm->mutex);
374
375         for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
376                 pos += scnprintf(buf + pos, bufsz - pos, "%.2d: ", i);
377                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
378                                                 lockdep_is_held(&mvm->mutex));
379                 if (!sta)
380                         pos += scnprintf(buf + pos, bufsz - pos, "N/A\n");
381                 else if (IS_ERR(sta))
382                         pos += scnprintf(buf + pos, bufsz - pos, "%ld\n",
383                                          PTR_ERR(sta));
384                 else
385                         pos += scnprintf(buf + pos, bufsz - pos, "%pM\n",
386                                          sta->addr);
387         }
388
389         mutex_unlock(&mvm->mutex);
390
391         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
392 }
393
394 static ssize_t iwl_dbgfs_rs_data_read(struct ieee80211_link_sta *link_sta,
395                                       struct iwl_mvm_sta *mvmsta,
396                                       struct iwl_mvm *mvm,
397                                       struct iwl_mvm_link_sta *mvm_link_sta,
398                                       char __user *user_buf,
399                                       size_t count, loff_t *ppos)
400 {
401         struct iwl_lq_sta_rs_fw *lq_sta = &mvm_link_sta->lq_sta.rs_fw;
402         static const size_t bufsz = 2048;
403         char *buff;
404         int desc = 0;
405         ssize_t ret;
406
407         buff = kmalloc(bufsz, GFP_KERNEL);
408         if (!buff)
409                 return -ENOMEM;
410
411         desc += scnprintf(buff + desc, bufsz - desc, "sta_id %d\n",
412                           lq_sta->pers.sta_id);
413         desc += scnprintf(buff + desc, bufsz - desc,
414                           "fixed rate 0x%X\n",
415                           lq_sta->pers.dbg_fixed_rate);
416         desc += scnprintf(buff + desc, bufsz - desc,
417                           "A-MPDU size limit %d\n",
418                           lq_sta->pers.dbg_agg_frame_count_lim);
419         desc += scnprintf(buff + desc, bufsz - desc,
420                           "valid_tx_ant %s%s\n",
421                 (iwl_mvm_get_valid_tx_ant(mvm) & ANT_A) ? "ANT_A," : "",
422                 (iwl_mvm_get_valid_tx_ant(mvm) & ANT_B) ? "ANT_B," : "");
423         desc += scnprintf(buff + desc, bufsz - desc,
424                           "last tx rate=0x%X ",
425                           lq_sta->last_rate_n_flags);
426
427         desc += rs_pretty_print_rate(buff + desc, bufsz - desc,
428                                      lq_sta->last_rate_n_flags);
429         if (desc < bufsz - 1)
430                 buff[desc++] = '\n';
431
432         ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
433         kfree(buff);
434         return ret;
435 }
436
437 static ssize_t iwl_dbgfs_amsdu_len_write(struct ieee80211_link_sta *link_sta,
438                                          struct iwl_mvm_sta *mvmsta,
439                                          struct iwl_mvm *mvm,
440                                          struct iwl_mvm_link_sta *mvm_link_sta,
441                                          char *buf, size_t count,
442                                          loff_t *ppos)
443 {
444         int i;
445         u16 amsdu_len;
446
447         if (kstrtou16(buf, 0, &amsdu_len))
448                 return -EINVAL;
449
450         /* only change from debug set <-> debug unset */
451         if (amsdu_len && mvm_link_sta->orig_amsdu_len)
452                 return -EBUSY;
453
454         if (amsdu_len) {
455                 mvm_link_sta->orig_amsdu_len = link_sta->agg.max_amsdu_len;
456                 link_sta->agg.max_amsdu_len = amsdu_len;
457                 link_sta->agg.max_amsdu_len = amsdu_len;
458                 for (i = 0; i < ARRAY_SIZE(link_sta->agg.max_tid_amsdu_len); i++)
459                         link_sta->agg.max_tid_amsdu_len[i] = amsdu_len;
460         } else {
461                 link_sta->agg.max_amsdu_len = mvm_link_sta->orig_amsdu_len;
462                 mvm_link_sta->orig_amsdu_len = 0;
463         }
464
465         ieee80211_sta_recalc_aggregates(link_sta->sta);
466
467         return count;
468 }
469
470 static ssize_t iwl_dbgfs_amsdu_len_read(struct ieee80211_link_sta *link_sta,
471                                         struct iwl_mvm_sta *mvmsta,
472                                         struct iwl_mvm *mvm,
473                                         struct iwl_mvm_link_sta *mvm_link_sta,
474                                         char __user *user_buf,
475                                         size_t count, loff_t *ppos)
476 {
477         char buf[32];
478         int pos;
479
480         pos = scnprintf(buf, sizeof(buf), "current %d ",
481                         link_sta->agg.max_amsdu_len);
482         pos += scnprintf(buf + pos, sizeof(buf) - pos, "stored %d\n",
483                          mvm_link_sta->orig_amsdu_len);
484
485         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
486 }
487
488 static ssize_t iwl_dbgfs_disable_power_off_read(struct file *file,
489                                                 char __user *user_buf,
490                                                 size_t count, loff_t *ppos)
491 {
492         struct iwl_mvm *mvm = file->private_data;
493         char buf[64];
494         int bufsz = sizeof(buf);
495         int pos = 0;
496
497         pos += scnprintf(buf+pos, bufsz-pos, "disable_power_off_d0=%d\n",
498                          mvm->disable_power_off);
499         pos += scnprintf(buf+pos, bufsz-pos, "disable_power_off_d3=%d\n",
500                          mvm->disable_power_off_d3);
501
502         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
503 }
504
505 static ssize_t iwl_dbgfs_disable_power_off_write(struct iwl_mvm *mvm, char *buf,
506                                                  size_t count, loff_t *ppos)
507 {
508         int ret, val;
509
510         if (!iwl_mvm_firmware_running(mvm))
511                 return -EIO;
512
513         if (!strncmp("disable_power_off_d0=", buf, 21)) {
514                 if (sscanf(buf + 21, "%d", &val) != 1)
515                         return -EINVAL;
516                 mvm->disable_power_off = val;
517         } else if (!strncmp("disable_power_off_d3=", buf, 21)) {
518                 if (sscanf(buf + 21, "%d", &val) != 1)
519                         return -EINVAL;
520                 mvm->disable_power_off_d3 = val;
521         } else {
522                 return -EINVAL;
523         }
524
525         mutex_lock(&mvm->mutex);
526         ret = iwl_mvm_power_update_device(mvm);
527         mutex_unlock(&mvm->mutex);
528
529         return ret ?: count;
530 }
531
532 static
533 int iwl_mvm_coex_dump_mbox(struct iwl_bt_coex_profile_notif *notif, char *buf,
534                            int pos, int bufsz)
535 {
536         pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw0:\n");
537
538         BT_MBOX_PRINT(0, LE_SLAVE_LAT, false);
539         BT_MBOX_PRINT(0, LE_PROF1, false);
540         BT_MBOX_PRINT(0, LE_PROF2, false);
541         BT_MBOX_PRINT(0, LE_PROF_OTHER, false);
542         BT_MBOX_PRINT(0, CHL_SEQ_N, false);
543         BT_MBOX_PRINT(0, INBAND_S, false);
544         BT_MBOX_PRINT(0, LE_MIN_RSSI, false);
545         BT_MBOX_PRINT(0, LE_SCAN, false);
546         BT_MBOX_PRINT(0, LE_ADV, false);
547         BT_MBOX_PRINT(0, LE_MAX_TX_POWER, false);
548         BT_MBOX_PRINT(0, OPEN_CON_1, true);
549
550         pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw1:\n");
551
552         BT_MBOX_PRINT(1, BR_MAX_TX_POWER, false);
553         BT_MBOX_PRINT(1, IP_SR, false);
554         BT_MBOX_PRINT(1, LE_MSTR, false);
555         BT_MBOX_PRINT(1, AGGR_TRFC_LD, false);
556         BT_MBOX_PRINT(1, MSG_TYPE, false);
557         BT_MBOX_PRINT(1, SSN, true);
558
559         pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw2:\n");
560
561         BT_MBOX_PRINT(2, SNIFF_ACT, false);
562         BT_MBOX_PRINT(2, PAG, false);
563         BT_MBOX_PRINT(2, INQUIRY, false);
564         BT_MBOX_PRINT(2, CONN, false);
565         BT_MBOX_PRINT(2, SNIFF_INTERVAL, false);
566         BT_MBOX_PRINT(2, DISC, false);
567         BT_MBOX_PRINT(2, SCO_TX_ACT, false);
568         BT_MBOX_PRINT(2, SCO_RX_ACT, false);
569         BT_MBOX_PRINT(2, ESCO_RE_TX, false);
570         BT_MBOX_PRINT(2, SCO_DURATION, true);
571
572         pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw3:\n");
573
574         BT_MBOX_PRINT(3, SCO_STATE, false);
575         BT_MBOX_PRINT(3, SNIFF_STATE, false);
576         BT_MBOX_PRINT(3, A2DP_STATE, false);
577         BT_MBOX_PRINT(3, A2DP_SRC, false);
578         BT_MBOX_PRINT(3, ACL_STATE, false);
579         BT_MBOX_PRINT(3, MSTR_STATE, false);
580         BT_MBOX_PRINT(3, OBX_STATE, false);
581         BT_MBOX_PRINT(3, OPEN_CON_2, false);
582         BT_MBOX_PRINT(3, TRAFFIC_LOAD, false);
583         BT_MBOX_PRINT(3, CHL_SEQN_LSB, false);
584         BT_MBOX_PRINT(3, INBAND_P, false);
585         BT_MBOX_PRINT(3, MSG_TYPE_2, false);
586         BT_MBOX_PRINT(3, SSN_2, false);
587         BT_MBOX_PRINT(3, UPDATE_REQUEST, true);
588
589         return pos;
590 }
591
592 static ssize_t iwl_dbgfs_bt_notif_read(struct file *file, char __user *user_buf,
593                                        size_t count, loff_t *ppos)
594 {
595         struct iwl_mvm *mvm = file->private_data;
596         struct iwl_bt_coex_profile_notif *notif = &mvm->last_bt_notif;
597         char *buf;
598         int ret, pos = 0, bufsz = sizeof(char) * 1024;
599
600         buf = kmalloc(bufsz, GFP_KERNEL);
601         if (!buf)
602                 return -ENOMEM;
603
604         mutex_lock(&mvm->mutex);
605
606         pos += iwl_mvm_coex_dump_mbox(notif, buf, pos, bufsz);
607
608         pos += scnprintf(buf + pos, bufsz - pos, "bt_ci_compliance = %d\n",
609                          notif->bt_ci_compliance);
610         pos += scnprintf(buf + pos, bufsz - pos, "primary_ch_lut = %d\n",
611                          le32_to_cpu(notif->primary_ch_lut));
612         pos += scnprintf(buf + pos, bufsz - pos, "secondary_ch_lut = %d\n",
613                          le32_to_cpu(notif->secondary_ch_lut));
614         pos += scnprintf(buf + pos,
615                          bufsz - pos, "bt_activity_grading = %d\n",
616                          le32_to_cpu(notif->bt_activity_grading));
617         pos += scnprintf(buf + pos, bufsz - pos, "bt_rrc = %d\n",
618                          notif->rrc_status & 0xF);
619         pos += scnprintf(buf + pos, bufsz - pos, "bt_ttc = %d\n",
620                          notif->ttc_status & 0xF);
621
622         pos += scnprintf(buf + pos, bufsz - pos, "sync_sco = %d\n",
623                          IWL_MVM_BT_COEX_SYNC2SCO);
624         pos += scnprintf(buf + pos, bufsz - pos, "mplut = %d\n",
625                          IWL_MVM_BT_COEX_MPLUT);
626
627         mutex_unlock(&mvm->mutex);
628
629         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
630         kfree(buf);
631
632         return ret;
633 }
634 #undef BT_MBOX_PRINT
635
636 static ssize_t iwl_dbgfs_bt_cmd_read(struct file *file, char __user *user_buf,
637                                      size_t count, loff_t *ppos)
638 {
639         struct iwl_mvm *mvm = file->private_data;
640         struct iwl_bt_coex_ci_cmd *cmd = &mvm->last_bt_ci_cmd;
641         char buf[256];
642         int bufsz = sizeof(buf);
643         int pos = 0;
644
645         mutex_lock(&mvm->mutex);
646
647         pos += scnprintf(buf + pos, bufsz - pos, "Channel inhibition CMD\n");
648         pos += scnprintf(buf + pos, bufsz - pos,
649                          "\tPrimary Channel Bitmap 0x%016llx\n",
650                          le64_to_cpu(cmd->bt_primary_ci));
651         pos += scnprintf(buf + pos, bufsz - pos,
652                          "\tSecondary Channel Bitmap 0x%016llx\n",
653                          le64_to_cpu(cmd->bt_secondary_ci));
654
655         mutex_unlock(&mvm->mutex);
656
657         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
658 }
659
660 static ssize_t
661 iwl_dbgfs_bt_tx_prio_write(struct iwl_mvm *mvm, char *buf,
662                            size_t count, loff_t *ppos)
663 {
664         u32 bt_tx_prio;
665
666         if (sscanf(buf, "%u", &bt_tx_prio) != 1)
667                 return -EINVAL;
668         if (bt_tx_prio > 4)
669                 return -EINVAL;
670
671         mvm->bt_tx_prio = bt_tx_prio;
672
673         return count;
674 }
675
676 static ssize_t
677 iwl_dbgfs_bt_force_ant_write(struct iwl_mvm *mvm, char *buf,
678                              size_t count, loff_t *ppos)
679 {
680         static const char * const modes_str[BT_FORCE_ANT_MAX] = {
681                 [BT_FORCE_ANT_DIS] = "dis",
682                 [BT_FORCE_ANT_AUTO] = "auto",
683                 [BT_FORCE_ANT_BT] = "bt",
684                 [BT_FORCE_ANT_WIFI] = "wifi",
685         };
686         int ret, bt_force_ant_mode;
687
688         ret = match_string(modes_str, ARRAY_SIZE(modes_str), buf);
689         if (ret < 0)
690                 return ret;
691
692         bt_force_ant_mode = ret;
693         ret = 0;
694         mutex_lock(&mvm->mutex);
695         if (mvm->bt_force_ant_mode == bt_force_ant_mode)
696                 goto out;
697
698         mvm->bt_force_ant_mode = bt_force_ant_mode;
699         IWL_DEBUG_COEX(mvm, "Force mode: %s\n",
700                        modes_str[mvm->bt_force_ant_mode]);
701
702         if (iwl_mvm_firmware_running(mvm))
703                 ret = iwl_mvm_send_bt_init_conf(mvm);
704         else
705                 ret = 0;
706
707 out:
708         mutex_unlock(&mvm->mutex);
709         return ret ?: count;
710 }
711
712 static ssize_t iwl_dbgfs_fw_ver_read(struct file *file, char __user *user_buf,
713                                      size_t count, loff_t *ppos)
714 {
715         struct iwl_mvm *mvm = file->private_data;
716         char *buff, *pos, *endpos;
717         static const size_t bufsz = 1024;
718         int ret;
719
720         buff = kmalloc(bufsz, GFP_KERNEL);
721         if (!buff)
722                 return -ENOMEM;
723
724         pos = buff;
725         endpos = pos + bufsz;
726
727         pos += scnprintf(pos, endpos - pos, "FW prefix: %s\n",
728                          mvm->trans->cfg->fw_name_pre);
729         pos += scnprintf(pos, endpos - pos, "FW: %s\n",
730                          mvm->fwrt.fw->human_readable);
731         pos += scnprintf(pos, endpos - pos, "Device: %s\n",
732                          mvm->fwrt.trans->name);
733         pos += scnprintf(pos, endpos - pos, "Bus: %s\n",
734                          mvm->fwrt.dev->bus->name);
735
736         ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
737         kfree(buff);
738
739         return ret;
740 }
741
742 static ssize_t iwl_dbgfs_tas_get_status_read(struct file *file,
743                                              char __user *user_buf,
744                                              size_t count, loff_t *ppos)
745 {
746         struct iwl_mvm *mvm = file->private_data;
747         struct iwl_mvm_tas_status_resp tas_rsp;
748         struct iwl_mvm_tas_status_resp *rsp = &tas_rsp;
749         static const size_t bufsz = 1024;
750         char *buff, *pos, *endpos;
751         const char * const tas_dis_reason[TAS_DISABLED_REASON_MAX] = {
752                 [TAS_DISABLED_DUE_TO_BIOS] =
753                         "Due To BIOS",
754                 [TAS_DISABLED_DUE_TO_SAR_6DBM] =
755                         "Due To SAR Limit Less Than 6 dBm",
756                 [TAS_DISABLED_REASON_INVALID] =
757                         "N/A",
758         };
759         const char * const tas_current_status[TAS_DYNA_STATUS_MAX] = {
760                 [TAS_DYNA_INACTIVE] = "INACTIVE",
761                 [TAS_DYNA_INACTIVE_MVM_MODE] =
762                         "inactive due to mvm mode",
763                 [TAS_DYNA_INACTIVE_TRIGGER_MODE] =
764                         "inactive due to trigger mode",
765                 [TAS_DYNA_INACTIVE_BLOCK_LISTED] =
766                         "inactive due to block listed",
767                 [TAS_DYNA_INACTIVE_UHB_NON_US] =
768                         "inactive due to uhb non US",
769                 [TAS_DYNA_ACTIVE] = "ACTIVE",
770         };
771         struct iwl_host_cmd hcmd = {
772                 .id = WIDE_ID(DEBUG_GROUP, GET_TAS_STATUS),
773                 .flags = CMD_WANT_SKB,
774                 .len = { 0, },
775                 .data = { NULL, },
776         };
777         int ret, i, tmp;
778         bool tas_enabled = false;
779         unsigned long dyn_status;
780
781         if (!iwl_mvm_firmware_running(mvm))
782                 return -ENODEV;
783
784         mutex_lock(&mvm->mutex);
785         ret = iwl_mvm_send_cmd(mvm, &hcmd);
786         mutex_unlock(&mvm->mutex);
787         if (ret < 0)
788                 return ret;
789
790         buff = kzalloc(bufsz, GFP_KERNEL);
791         if (!buff)
792                 return -ENOMEM;
793         pos = buff;
794         endpos = pos + bufsz;
795
796         rsp = (void *)hcmd.resp_pkt->data;
797
798         pos += scnprintf(pos, endpos - pos, "TAS Conclusion:\n");
799         for (i = 0; i < rsp->in_dual_radio + 1; i++) {
800                 if (rsp->tas_status_mac[i].band != TAS_LMAC_BAND_INVALID &&
801                     rsp->tas_status_mac[i].dynamic_status & BIT(TAS_DYNA_ACTIVE)) {
802                         pos += scnprintf(pos, endpos - pos, "\tON for ");
803                         switch (rsp->tas_status_mac[i].band) {
804                         case TAS_LMAC_BAND_HB:
805                                 pos += scnprintf(pos, endpos - pos, "HB\n");
806                                 break;
807                         case TAS_LMAC_BAND_LB:
808                                 pos += scnprintf(pos, endpos - pos, "LB\n");
809                                 break;
810                         case TAS_LMAC_BAND_UHB:
811                                 pos += scnprintf(pos, endpos - pos, "UHB\n");
812                                 break;
813                         case TAS_LMAC_BAND_INVALID:
814                                 pos += scnprintf(pos, endpos - pos,
815                                                  "INVALID BAND\n");
816                                 break;
817                         default:
818                                 pos += scnprintf(pos, endpos - pos,
819                                                  "Unsupported band (%d)\n",
820                                                  rsp->tas_status_mac[i].band);
821                                 goto out;
822                         }
823                         tas_enabled = true;
824                 }
825         }
826         if (!tas_enabled)
827                 pos += scnprintf(pos, endpos - pos, "\tOFF\n");
828
829         pos += scnprintf(pos, endpos - pos, "TAS Report\n");
830         pos += scnprintf(pos, endpos - pos, "TAS FW version: %d\n",
831                          rsp->tas_fw_version);
832         pos += scnprintf(pos, endpos - pos, "Is UHB enabled for USA?: %s\n",
833                          rsp->is_uhb_for_usa_enable ? "True" : "False");
834         pos += scnprintf(pos, endpos - pos, "Current MCC: 0x%x\n",
835                          le16_to_cpu(rsp->curr_mcc));
836
837         pos += scnprintf(pos, endpos - pos, "Block list entries:");
838         for (i = 0; i < APCI_WTAS_BLACK_LIST_MAX; i++)
839                 pos += scnprintf(pos, endpos - pos, " 0x%x",
840                                  le16_to_cpu(rsp->block_list[i]));
841
842         pos += scnprintf(pos, endpos - pos, "\nOEM name: %s\n",
843                          dmi_get_system_info(DMI_SYS_VENDOR));
844         pos += scnprintf(pos, endpos - pos, "\tVendor In Approved List: %s\n",
845                          iwl_mvm_is_vendor_in_approved_list() ? "YES" : "NO");
846         pos += scnprintf(pos, endpos - pos,
847                          "\tDo TAS Support Dual Radio?: %s\n",
848                          rsp->in_dual_radio ? "TRUE" : "FALSE");
849
850         for (i = 0; i < rsp->in_dual_radio + 1; i++) {
851                 if (rsp->tas_status_mac[i].static_status == 0) {
852                         pos += scnprintf(pos, endpos - pos,
853                                          "Static status: disabled\n");
854                         pos += scnprintf(pos, endpos - pos,
855                                          "Static disabled reason: %s (0)\n",
856                                          tas_dis_reason[0]);
857                         goto out;
858                 }
859
860                 pos += scnprintf(pos, endpos - pos, "TAS status for ");
861                 switch (rsp->tas_status_mac[i].band) {
862                 case TAS_LMAC_BAND_HB:
863                         pos += scnprintf(pos, endpos - pos, "High band\n");
864                         break;
865                 case TAS_LMAC_BAND_LB:
866                         pos += scnprintf(pos, endpos - pos, "Low band\n");
867                         break;
868                 case TAS_LMAC_BAND_UHB:
869                         pos += scnprintf(pos, endpos - pos,
870                                          "Ultra high band\n");
871                         break;
872                 case TAS_LMAC_BAND_INVALID:
873                         pos += scnprintf(pos, endpos - pos,
874                                          "INVALID band\n");
875                         break;
876                 default:
877                         pos += scnprintf(pos, endpos - pos,
878                                          "Unsupported band (%d)\n",
879                                          rsp->tas_status_mac[i].band);
880                         goto out;
881                 }
882                 pos += scnprintf(pos, endpos - pos, "Static status: %sabled\n",
883                                  rsp->tas_status_mac[i].static_status ?
884                                  "En" : "Dis");
885                 pos += scnprintf(pos, endpos - pos,
886                                  "\tStatic Disabled Reason: ");
887                 if (rsp->tas_status_mac[i].static_dis_reason < TAS_DISABLED_REASON_MAX)
888                         pos += scnprintf(pos, endpos - pos, "%s (%d)\n",
889                                          tas_dis_reason[rsp->tas_status_mac[i].static_dis_reason],
890                                          rsp->tas_status_mac[i].static_dis_reason);
891                 else
892                         pos += scnprintf(pos, endpos - pos,
893                                          "unsupported value (%d)\n",
894                                          rsp->tas_status_mac[i].static_dis_reason);
895
896                 pos += scnprintf(pos, endpos - pos, "Dynamic status:\n");
897                 dyn_status = (rsp->tas_status_mac[i].dynamic_status);
898                 for_each_set_bit(tmp, &dyn_status, sizeof(dyn_status)) {
899                         if (tmp >= 0 && tmp < TAS_DYNA_STATUS_MAX)
900                                 pos += scnprintf(pos, endpos - pos,
901                                                  "\t%s (%d)\n",
902                                                  tas_current_status[tmp], tmp);
903                 }
904
905                 pos += scnprintf(pos, endpos - pos,
906                                  "Is near disconnection?: %s\n",
907                                  rsp->tas_status_mac[i].near_disconnection ?
908                                  "True" : "False");
909                 tmp = le16_to_cpu(rsp->tas_status_mac[i].max_reg_pwr_limit);
910                 pos += scnprintf(pos, endpos - pos,
911                                  "Max. regulatory pwr limit (dBm): %d.%03d\n",
912                                  tmp / 8, 125 * (tmp % 8));
913                 tmp = le16_to_cpu(rsp->tas_status_mac[i].sar_limit);
914                 pos += scnprintf(pos, endpos - pos,
915                                  "SAR limit (dBm): %d.%03d\n",
916                                  tmp / 8, 125 * (tmp % 8));
917         }
918
919 out:
920         ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
921         kfree(buff);
922         iwl_free_resp(&hcmd);
923         return ret;
924 }
925
926 static ssize_t iwl_dbgfs_phy_integration_ver_read(struct file *file,
927                                                   char __user *user_buf,
928                                                   size_t count, loff_t *ppos)
929 {
930         struct iwl_mvm *mvm = file->private_data;
931         char *buf;
932         size_t bufsz;
933         int pos;
934         ssize_t ret;
935
936         bufsz = mvm->fw->phy_integration_ver_len + 2;
937         buf = kmalloc(bufsz, GFP_KERNEL);
938         if (!buf)
939                 return -ENOMEM;
940
941         pos = scnprintf(buf, bufsz, "%.*s\n", mvm->fw->phy_integration_ver_len,
942                         mvm->fw->phy_integration_ver);
943
944         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
945
946         kfree(buf);
947         return ret;
948 }
949
950 #define PRINT_STATS_LE32(_struct, _memb)                                \
951                          pos += scnprintf(buf + pos, bufsz - pos,       \
952                                           fmt_table, #_memb,            \
953                                           le32_to_cpu(_struct->_memb))
954
955 static ssize_t iwl_dbgfs_fw_rx_stats_read(struct file *file,
956                                           char __user *user_buf, size_t count,
957                                           loff_t *ppos)
958 {
959         struct iwl_mvm *mvm = file->private_data;
960         static const char *fmt_table = "\t%-30s %10u\n";
961         static const char *fmt_header = "%-32s\n";
962         int pos = 0;
963         char *buf;
964         int ret;
965         size_t bufsz;
966
967         if (iwl_mvm_has_new_rx_stats_api(mvm))
968                 bufsz = ((sizeof(struct mvm_statistics_rx) /
969                           sizeof(__le32)) * 43) + (4 * 33) + 1;
970         else
971                 /* 43 = size of each data line; 33 = size of each header */
972                 bufsz = ((sizeof(struct mvm_statistics_rx_v3) /
973                           sizeof(__le32)) * 43) + (4 * 33) + 1;
974
975         buf = kzalloc(bufsz, GFP_KERNEL);
976         if (!buf)
977                 return -ENOMEM;
978
979         mutex_lock(&mvm->mutex);
980
981         if (iwl_mvm_firmware_running(mvm))
982                 iwl_mvm_request_statistics(mvm, false);
983
984         pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
985                          "Statistics_Rx - OFDM");
986         if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
987                 struct mvm_statistics_rx_phy_v2 *ofdm = &mvm->rx_stats_v3.ofdm;
988
989                 PRINT_STATS_LE32(ofdm, ina_cnt);
990                 PRINT_STATS_LE32(ofdm, fina_cnt);
991                 PRINT_STATS_LE32(ofdm, plcp_err);
992                 PRINT_STATS_LE32(ofdm, crc32_err);
993                 PRINT_STATS_LE32(ofdm, overrun_err);
994                 PRINT_STATS_LE32(ofdm, early_overrun_err);
995                 PRINT_STATS_LE32(ofdm, crc32_good);
996                 PRINT_STATS_LE32(ofdm, false_alarm_cnt);
997                 PRINT_STATS_LE32(ofdm, fina_sync_err_cnt);
998                 PRINT_STATS_LE32(ofdm, sfd_timeout);
999                 PRINT_STATS_LE32(ofdm, fina_timeout);
1000                 PRINT_STATS_LE32(ofdm, unresponded_rts);
1001                 PRINT_STATS_LE32(ofdm, rxe_frame_lmt_overrun);
1002                 PRINT_STATS_LE32(ofdm, sent_ack_cnt);
1003                 PRINT_STATS_LE32(ofdm, sent_cts_cnt);
1004                 PRINT_STATS_LE32(ofdm, sent_ba_rsp_cnt);
1005                 PRINT_STATS_LE32(ofdm, dsp_self_kill);
1006                 PRINT_STATS_LE32(ofdm, mh_format_err);
1007                 PRINT_STATS_LE32(ofdm, re_acq_main_rssi_sum);
1008                 PRINT_STATS_LE32(ofdm, reserved);
1009         } else {
1010                 struct mvm_statistics_rx_phy *ofdm = &mvm->rx_stats.ofdm;
1011
1012                 PRINT_STATS_LE32(ofdm, unresponded_rts);
1013                 PRINT_STATS_LE32(ofdm, rxe_frame_lmt_overrun);
1014                 PRINT_STATS_LE32(ofdm, sent_ba_rsp_cnt);
1015                 PRINT_STATS_LE32(ofdm, dsp_self_kill);
1016                 PRINT_STATS_LE32(ofdm, reserved);
1017         }
1018
1019         pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
1020                          "Statistics_Rx - CCK");
1021         if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
1022                 struct mvm_statistics_rx_phy_v2 *cck = &mvm->rx_stats_v3.cck;
1023
1024                 PRINT_STATS_LE32(cck, ina_cnt);
1025                 PRINT_STATS_LE32(cck, fina_cnt);
1026                 PRINT_STATS_LE32(cck, plcp_err);
1027                 PRINT_STATS_LE32(cck, crc32_err);
1028                 PRINT_STATS_LE32(cck, overrun_err);
1029                 PRINT_STATS_LE32(cck, early_overrun_err);
1030                 PRINT_STATS_LE32(cck, crc32_good);
1031                 PRINT_STATS_LE32(cck, false_alarm_cnt);
1032                 PRINT_STATS_LE32(cck, fina_sync_err_cnt);
1033                 PRINT_STATS_LE32(cck, sfd_timeout);
1034                 PRINT_STATS_LE32(cck, fina_timeout);
1035                 PRINT_STATS_LE32(cck, unresponded_rts);
1036                 PRINT_STATS_LE32(cck, rxe_frame_lmt_overrun);
1037                 PRINT_STATS_LE32(cck, sent_ack_cnt);
1038                 PRINT_STATS_LE32(cck, sent_cts_cnt);
1039                 PRINT_STATS_LE32(cck, sent_ba_rsp_cnt);
1040                 PRINT_STATS_LE32(cck, dsp_self_kill);
1041                 PRINT_STATS_LE32(cck, mh_format_err);
1042                 PRINT_STATS_LE32(cck, re_acq_main_rssi_sum);
1043                 PRINT_STATS_LE32(cck, reserved);
1044         } else {
1045                 struct mvm_statistics_rx_phy *cck = &mvm->rx_stats.cck;
1046
1047                 PRINT_STATS_LE32(cck, unresponded_rts);
1048                 PRINT_STATS_LE32(cck, rxe_frame_lmt_overrun);
1049                 PRINT_STATS_LE32(cck, sent_ba_rsp_cnt);
1050                 PRINT_STATS_LE32(cck, dsp_self_kill);
1051                 PRINT_STATS_LE32(cck, reserved);
1052         }
1053
1054         pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
1055                          "Statistics_Rx - GENERAL");
1056         if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
1057                 struct mvm_statistics_rx_non_phy_v3 *general =
1058                         &mvm->rx_stats_v3.general;
1059
1060                 PRINT_STATS_LE32(general, bogus_cts);
1061                 PRINT_STATS_LE32(general, bogus_ack);
1062                 PRINT_STATS_LE32(general, non_bssid_frames);
1063                 PRINT_STATS_LE32(general, filtered_frames);
1064                 PRINT_STATS_LE32(general, non_channel_beacons);
1065                 PRINT_STATS_LE32(general, channel_beacons);
1066                 PRINT_STATS_LE32(general, num_missed_bcon);
1067                 PRINT_STATS_LE32(general, adc_rx_saturation_time);
1068                 PRINT_STATS_LE32(general, ina_detection_search_time);
1069                 PRINT_STATS_LE32(general, beacon_silence_rssi_a);
1070                 PRINT_STATS_LE32(general, beacon_silence_rssi_b);
1071                 PRINT_STATS_LE32(general, beacon_silence_rssi_c);
1072                 PRINT_STATS_LE32(general, interference_data_flag);
1073                 PRINT_STATS_LE32(general, channel_load);
1074                 PRINT_STATS_LE32(general, dsp_false_alarms);
1075                 PRINT_STATS_LE32(general, beacon_rssi_a);
1076                 PRINT_STATS_LE32(general, beacon_rssi_b);
1077                 PRINT_STATS_LE32(general, beacon_rssi_c);
1078                 PRINT_STATS_LE32(general, beacon_energy_a);
1079                 PRINT_STATS_LE32(general, beacon_energy_b);
1080                 PRINT_STATS_LE32(general, beacon_energy_c);
1081                 PRINT_STATS_LE32(general, num_bt_kills);
1082                 PRINT_STATS_LE32(general, mac_id);
1083                 PRINT_STATS_LE32(general, directed_data_mpdu);
1084         } else {
1085                 struct mvm_statistics_rx_non_phy *general =
1086                         &mvm->rx_stats.general;
1087
1088                 PRINT_STATS_LE32(general, bogus_cts);
1089                 PRINT_STATS_LE32(general, bogus_ack);
1090                 PRINT_STATS_LE32(general, non_channel_beacons);
1091                 PRINT_STATS_LE32(general, channel_beacons);
1092                 PRINT_STATS_LE32(general, num_missed_bcon);
1093                 PRINT_STATS_LE32(general, adc_rx_saturation_time);
1094                 PRINT_STATS_LE32(general, ina_detection_search_time);
1095                 PRINT_STATS_LE32(general, beacon_silence_rssi_a);
1096                 PRINT_STATS_LE32(general, beacon_silence_rssi_b);
1097                 PRINT_STATS_LE32(general, beacon_silence_rssi_c);
1098                 PRINT_STATS_LE32(general, interference_data_flag);
1099                 PRINT_STATS_LE32(general, channel_load);
1100                 PRINT_STATS_LE32(general, beacon_rssi_a);
1101                 PRINT_STATS_LE32(general, beacon_rssi_b);
1102                 PRINT_STATS_LE32(general, beacon_rssi_c);
1103                 PRINT_STATS_LE32(general, beacon_energy_a);
1104                 PRINT_STATS_LE32(general, beacon_energy_b);
1105                 PRINT_STATS_LE32(general, beacon_energy_c);
1106                 PRINT_STATS_LE32(general, num_bt_kills);
1107                 PRINT_STATS_LE32(general, mac_id);
1108         }
1109
1110         pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
1111                          "Statistics_Rx - HT");
1112         if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
1113                 struct mvm_statistics_rx_ht_phy_v1 *ht =
1114                         &mvm->rx_stats_v3.ofdm_ht;
1115
1116                 PRINT_STATS_LE32(ht, plcp_err);
1117                 PRINT_STATS_LE32(ht, overrun_err);
1118                 PRINT_STATS_LE32(ht, early_overrun_err);
1119                 PRINT_STATS_LE32(ht, crc32_good);
1120                 PRINT_STATS_LE32(ht, crc32_err);
1121                 PRINT_STATS_LE32(ht, mh_format_err);
1122                 PRINT_STATS_LE32(ht, agg_crc32_good);
1123                 PRINT_STATS_LE32(ht, agg_mpdu_cnt);
1124                 PRINT_STATS_LE32(ht, agg_cnt);
1125                 PRINT_STATS_LE32(ht, unsupport_mcs);
1126         } else {
1127                 struct mvm_statistics_rx_ht_phy *ht =
1128                         &mvm->rx_stats.ofdm_ht;
1129
1130                 PRINT_STATS_LE32(ht, mh_format_err);
1131                 PRINT_STATS_LE32(ht, agg_mpdu_cnt);
1132                 PRINT_STATS_LE32(ht, agg_cnt);
1133                 PRINT_STATS_LE32(ht, unsupport_mcs);
1134         }
1135
1136         mutex_unlock(&mvm->mutex);
1137
1138         ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1139         kfree(buf);
1140
1141         return ret;
1142 }
1143 #undef PRINT_STAT_LE32
1144
1145 static ssize_t iwl_dbgfs_frame_stats_read(struct iwl_mvm *mvm,
1146                                           char __user *user_buf, size_t count,
1147                                           loff_t *ppos,
1148                                           struct iwl_mvm_frame_stats *stats)
1149 {
1150         char *buff, *pos, *endpos;
1151         int idx, i;
1152         int ret;
1153         static const size_t bufsz = 1024;
1154
1155         buff = kmalloc(bufsz, GFP_KERNEL);
1156         if (!buff)
1157                 return -ENOMEM;
1158
1159         spin_lock_bh(&mvm->drv_stats_lock);
1160
1161         pos = buff;
1162         endpos = pos + bufsz;
1163
1164         pos += scnprintf(pos, endpos - pos,
1165                          "Legacy/HT/VHT\t:\t%d/%d/%d\n",
1166                          stats->legacy_frames,
1167                          stats->ht_frames,
1168                          stats->vht_frames);
1169         pos += scnprintf(pos, endpos - pos, "20/40/80\t:\t%d/%d/%d\n",
1170                          stats->bw_20_frames,
1171                          stats->bw_40_frames,
1172                          stats->bw_80_frames);
1173         pos += scnprintf(pos, endpos - pos, "NGI/SGI\t\t:\t%d/%d\n",
1174                          stats->ngi_frames,
1175                          stats->sgi_frames);
1176         pos += scnprintf(pos, endpos - pos, "SISO/MIMO2\t:\t%d/%d\n",
1177                          stats->siso_frames,
1178                          stats->mimo2_frames);
1179         pos += scnprintf(pos, endpos - pos, "FAIL/SCSS\t:\t%d/%d\n",
1180                          stats->fail_frames,
1181                          stats->success_frames);
1182         pos += scnprintf(pos, endpos - pos, "MPDUs agg\t:\t%d\n",
1183                          stats->agg_frames);
1184         pos += scnprintf(pos, endpos - pos, "A-MPDUs\t\t:\t%d\n",
1185                          stats->ampdu_count);
1186         pos += scnprintf(pos, endpos - pos, "Avg MPDUs/A-MPDU:\t%d\n",
1187                          stats->ampdu_count > 0 ?
1188                          (stats->agg_frames / stats->ampdu_count) : 0);
1189
1190         pos += scnprintf(pos, endpos - pos, "Last Rates\n");
1191
1192         idx = stats->last_frame_idx - 1;
1193         for (i = 0; i < ARRAY_SIZE(stats->last_rates); i++) {
1194                 idx = (idx + 1) % ARRAY_SIZE(stats->last_rates);
1195                 if (stats->last_rates[idx] == 0)
1196                         continue;
1197                 pos += scnprintf(pos, endpos - pos, "Rate[%d]: ",
1198                                  (int)(ARRAY_SIZE(stats->last_rates) - i));
1199                 pos += rs_pretty_print_rate_v1(pos, endpos - pos,
1200                                                stats->last_rates[idx]);
1201                 if (pos < endpos - 1)
1202                         *pos++ = '\n';
1203         }
1204         spin_unlock_bh(&mvm->drv_stats_lock);
1205
1206         ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
1207         kfree(buff);
1208
1209         return ret;
1210 }
1211
1212 static ssize_t iwl_dbgfs_drv_rx_stats_read(struct file *file,
1213                                            char __user *user_buf, size_t count,
1214                                            loff_t *ppos)
1215 {
1216         struct iwl_mvm *mvm = file->private_data;
1217
1218         return iwl_dbgfs_frame_stats_read(mvm, user_buf, count, ppos,
1219                                           &mvm->drv_rx_stats);
1220 }
1221
1222 static ssize_t iwl_dbgfs_fw_restart_write(struct iwl_mvm *mvm, char *buf,
1223                                           size_t count, loff_t *ppos)
1224 {
1225         int __maybe_unused ret;
1226
1227         if (!iwl_mvm_firmware_running(mvm))
1228                 return -EIO;
1229
1230         mutex_lock(&mvm->mutex);
1231
1232         /* allow one more restart that we're provoking here */
1233         if (mvm->fw_restart >= 0)
1234                 mvm->fw_restart++;
1235
1236         if (count == 6 && !strcmp(buf, "nolog\n")) {
1237                 set_bit(IWL_MVM_STATUS_SUPPRESS_ERROR_LOG_ONCE, &mvm->status);
1238                 set_bit(STATUS_SUPPRESS_CMD_ERROR_ONCE, &mvm->trans->status);
1239         }
1240
1241         /* take the return value to make compiler happy - it will fail anyway */
1242         ret = iwl_mvm_send_cmd_pdu(mvm,
1243                                    WIDE_ID(LONG_GROUP, REPLY_ERROR),
1244                                    0, 0, NULL);
1245
1246         mutex_unlock(&mvm->mutex);
1247
1248         return count;
1249 }
1250
1251 static ssize_t iwl_dbgfs_fw_nmi_write(struct iwl_mvm *mvm, char *buf,
1252                                       size_t count, loff_t *ppos)
1253 {
1254         if (!iwl_mvm_firmware_running(mvm))
1255                 return -EIO;
1256
1257         if (count == 6 && !strcmp(buf, "nolog\n"))
1258                 set_bit(IWL_MVM_STATUS_SUPPRESS_ERROR_LOG_ONCE, &mvm->status);
1259
1260         iwl_force_nmi(mvm->trans);
1261
1262         return count;
1263 }
1264
1265 static ssize_t
1266 iwl_dbgfs_scan_ant_rxchain_read(struct file *file,
1267                                 char __user *user_buf,
1268                                 size_t count, loff_t *ppos)
1269 {
1270         struct iwl_mvm *mvm = file->private_data;
1271         int pos = 0;
1272         char buf[32];
1273         const size_t bufsz = sizeof(buf);
1274
1275         /* print which antennas were set for the scan command by the user */
1276         pos += scnprintf(buf + pos, bufsz - pos, "Antennas for scan: ");
1277         if (mvm->scan_rx_ant & ANT_A)
1278                 pos += scnprintf(buf + pos, bufsz - pos, "A");
1279         if (mvm->scan_rx_ant & ANT_B)
1280                 pos += scnprintf(buf + pos, bufsz - pos, "B");
1281         pos += scnprintf(buf + pos, bufsz - pos, " (%x)\n", mvm->scan_rx_ant);
1282
1283         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1284 }
1285
1286 static ssize_t
1287 iwl_dbgfs_scan_ant_rxchain_write(struct iwl_mvm *mvm, char *buf,
1288                                  size_t count, loff_t *ppos)
1289 {
1290         u8 scan_rx_ant;
1291
1292         if (!iwl_mvm_firmware_running(mvm))
1293                 return -EIO;
1294
1295         if (sscanf(buf, "%hhx", &scan_rx_ant) != 1)
1296                 return -EINVAL;
1297         if (scan_rx_ant > ANT_ABC)
1298                 return -EINVAL;
1299         if (scan_rx_ant & ~(iwl_mvm_get_valid_rx_ant(mvm)))
1300                 return -EINVAL;
1301
1302         if (mvm->scan_rx_ant != scan_rx_ant) {
1303                 mvm->scan_rx_ant = scan_rx_ant;
1304                 if (fw_has_capa(&mvm->fw->ucode_capa,
1305                                 IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1306                         iwl_mvm_config_scan(mvm);
1307         }
1308
1309         return count;
1310 }
1311
1312 static ssize_t iwl_dbgfs_indirection_tbl_write(struct iwl_mvm *mvm,
1313                                                char *buf, size_t count,
1314                                                loff_t *ppos)
1315 {
1316         struct iwl_rss_config_cmd cmd = {
1317                 .flags = cpu_to_le32(IWL_RSS_ENABLE),
1318                 .hash_mask = IWL_RSS_HASH_TYPE_IPV4_TCP |
1319                              IWL_RSS_HASH_TYPE_IPV4_UDP |
1320                              IWL_RSS_HASH_TYPE_IPV4_PAYLOAD |
1321                              IWL_RSS_HASH_TYPE_IPV6_TCP |
1322                              IWL_RSS_HASH_TYPE_IPV6_UDP |
1323                              IWL_RSS_HASH_TYPE_IPV6_PAYLOAD,
1324         };
1325         int ret, i, num_repeats, nbytes = count / 2;
1326
1327         ret = hex2bin(cmd.indirection_table, buf, nbytes);
1328         if (ret)
1329                 return ret;
1330
1331         /*
1332          * The input is the redirection table, partial or full.
1333          * Repeat the pattern if needed.
1334          * For example, input of 01020F will be repeated 42 times,
1335          * indirecting RSS hash results to queues 1, 2, 15 (skipping
1336          * queues 3 - 14).
1337          */
1338         num_repeats = ARRAY_SIZE(cmd.indirection_table) / nbytes;
1339         for (i = 1; i < num_repeats; i++)
1340                 memcpy(&cmd.indirection_table[i * nbytes],
1341                        cmd.indirection_table, nbytes);
1342         /* handle cut in the middle pattern for the last places */
1343         memcpy(&cmd.indirection_table[i * nbytes], cmd.indirection_table,
1344                ARRAY_SIZE(cmd.indirection_table) % nbytes);
1345
1346         netdev_rss_key_fill(cmd.secret_key, sizeof(cmd.secret_key));
1347
1348         mutex_lock(&mvm->mutex);
1349         if (iwl_mvm_firmware_running(mvm))
1350                 ret = iwl_mvm_send_cmd_pdu(mvm, RSS_CONFIG_CMD, 0,
1351                                            sizeof(cmd), &cmd);
1352         else
1353                 ret = 0;
1354         mutex_unlock(&mvm->mutex);
1355
1356         return ret ?: count;
1357 }
1358
1359 static ssize_t iwl_dbgfs_inject_packet_write(struct iwl_mvm *mvm,
1360                                              char *buf, size_t count,
1361                                              loff_t *ppos)
1362 {
1363         struct iwl_op_mode *opmode = container_of((void *)mvm,
1364                                                   struct iwl_op_mode,
1365                                                   op_mode_specific);
1366         struct iwl_rx_cmd_buffer rxb = {
1367                 ._rx_page_order = 0,
1368                 .truesize = 0, /* not used */
1369                 ._offset = 0,
1370         };
1371         struct iwl_rx_packet *pkt;
1372         int bin_len = count / 2;
1373         int ret = -EINVAL;
1374
1375         if (!iwl_mvm_firmware_running(mvm))
1376                 return -EIO;
1377
1378         /* supporting only MQ RX */
1379         if (!mvm->trans->trans_cfg->mq_rx_supported)
1380                 return -ENOTSUPP;
1381
1382         rxb._page = alloc_pages(GFP_ATOMIC, 0);
1383         if (!rxb._page)
1384                 return -ENOMEM;
1385         pkt = rxb_addr(&rxb);
1386
1387         ret = hex2bin(page_address(rxb._page), buf, bin_len);
1388         if (ret)
1389                 goto out;
1390
1391         /* avoid invalid memory access and malformed packet */
1392         if (bin_len < sizeof(*pkt) ||
1393             bin_len != sizeof(*pkt) + iwl_rx_packet_payload_len(pkt))
1394                 goto out;
1395
1396         local_bh_disable();
1397         iwl_mvm_rx_mq(opmode, NULL, &rxb);
1398         local_bh_enable();
1399         ret = 0;
1400
1401 out:
1402         iwl_free_rxb(&rxb);
1403
1404         return ret ?: count;
1405 }
1406
1407 static int _iwl_dbgfs_inject_beacon_ie(struct iwl_mvm *mvm, char *bin, int len)
1408 {
1409         struct ieee80211_vif *vif;
1410         struct iwl_mvm_vif *mvmvif;
1411         struct sk_buff *beacon;
1412         struct ieee80211_tx_info *info;
1413         struct iwl_mac_beacon_cmd beacon_cmd = {};
1414         unsigned int link_id;
1415         u8 rate;
1416         int i;
1417
1418         len /= 2;
1419
1420         /* Element len should be represented by u8 */
1421         if (len >= U8_MAX)
1422                 return -EINVAL;
1423
1424         if (!iwl_mvm_firmware_running(mvm))
1425                 return -EIO;
1426
1427         if (!iwl_mvm_has_new_tx_api(mvm) &&
1428             !fw_has_api(&mvm->fw->ucode_capa,
1429                         IWL_UCODE_TLV_API_NEW_BEACON_TEMPLATE))
1430                 return -EINVAL;
1431
1432         mutex_lock(&mvm->mutex);
1433
1434         for (i = 0; i < NUM_MAC_INDEX_DRIVER; i++) {
1435                 vif = iwl_mvm_rcu_dereference_vif_id(mvm, i, false);
1436                 if (!vif)
1437                         continue;
1438
1439                 if (vif->type == NL80211_IFTYPE_AP)
1440                         break;
1441         }
1442
1443         if (i == NUM_MAC_INDEX_DRIVER || !vif)
1444                 goto out_err;
1445
1446         mvm->hw->extra_beacon_tailroom = len;
1447
1448         beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL, 0);
1449         if (!beacon)
1450                 goto out_err;
1451
1452         if (len && hex2bin(skb_put_zero(beacon, len), bin, len)) {
1453                 dev_kfree_skb(beacon);
1454                 goto out_err;
1455         }
1456
1457         mvm->beacon_inject_active = true;
1458
1459         mvmvif = iwl_mvm_vif_from_mac80211(vif);
1460         info = IEEE80211_SKB_CB(beacon);
1461         rate = iwl_mvm_mac_ctxt_get_beacon_rate(mvm, info, vif);
1462
1463         for_each_mvm_vif_valid_link(mvmvif, link_id) {
1464                 beacon_cmd.flags =
1465                         cpu_to_le16(iwl_mvm_mac_ctxt_get_beacon_flags(mvm->fw,
1466                                                                       rate));
1467                 beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);
1468                 if (iwl_fw_lookup_cmd_ver(mvm->fw, BEACON_TEMPLATE_CMD, 0) > 12)
1469                         beacon_cmd.link_id =
1470                                 cpu_to_le32(mvmvif->link[link_id]->fw_link_id);
1471                 else
1472                         beacon_cmd.link_id = cpu_to_le32((u32)mvmvif->id);
1473
1474                 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1475                                          &beacon_cmd.tim_size,
1476                                          beacon->data, beacon->len);
1477
1478                 iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1479                                                  sizeof(beacon_cmd));
1480         }
1481         mutex_unlock(&mvm->mutex);
1482
1483         dev_kfree_skb(beacon);
1484
1485         return 0;
1486
1487 out_err:
1488         mutex_unlock(&mvm->mutex);
1489         return -EINVAL;
1490 }
1491
1492 static ssize_t iwl_dbgfs_inject_beacon_ie_write(struct iwl_mvm *mvm,
1493                                                 char *buf, size_t count,
1494                                                 loff_t *ppos)
1495 {
1496         int ret = _iwl_dbgfs_inject_beacon_ie(mvm, buf, count);
1497
1498         mvm->hw->extra_beacon_tailroom = 0;
1499         return ret ?: count;
1500 }
1501
1502 static ssize_t iwl_dbgfs_inject_beacon_ie_restore_write(struct iwl_mvm *mvm,
1503                                                         char *buf,
1504                                                         size_t count,
1505                                                         loff_t *ppos)
1506 {
1507         int ret = _iwl_dbgfs_inject_beacon_ie(mvm, NULL, 0);
1508
1509         mvm->hw->extra_beacon_tailroom = 0;
1510         mvm->beacon_inject_active = false;
1511         return ret ?: count;
1512 }
1513
1514 static ssize_t iwl_dbgfs_fw_dbg_conf_read(struct file *file,
1515                                           char __user *user_buf,
1516                                           size_t count, loff_t *ppos)
1517 {
1518         struct iwl_mvm *mvm = file->private_data;
1519         int conf;
1520         char buf[8];
1521         const size_t bufsz = sizeof(buf);
1522         int pos = 0;
1523
1524         mutex_lock(&mvm->mutex);
1525         conf = mvm->fwrt.dump.conf;
1526         mutex_unlock(&mvm->mutex);
1527
1528         pos += scnprintf(buf + pos, bufsz - pos, "%d\n", conf);
1529
1530         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1531 }
1532
1533 static ssize_t iwl_dbgfs_fw_dbg_conf_write(struct iwl_mvm *mvm,
1534                                            char *buf, size_t count,
1535                                            loff_t *ppos)
1536 {
1537         unsigned int conf_id;
1538         int ret;
1539
1540         if (!iwl_mvm_firmware_running(mvm))
1541                 return -EIO;
1542
1543         ret = kstrtouint(buf, 0, &conf_id);
1544         if (ret)
1545                 return ret;
1546
1547         if (WARN_ON(conf_id >= FW_DBG_CONF_MAX))
1548                 return -EINVAL;
1549
1550         mutex_lock(&mvm->mutex);
1551         ret = iwl_fw_start_dbg_conf(&mvm->fwrt, conf_id);
1552         mutex_unlock(&mvm->mutex);
1553
1554         return ret ?: count;
1555 }
1556
1557 static ssize_t iwl_dbgfs_fw_dbg_collect_write(struct iwl_mvm *mvm,
1558                                               char *buf, size_t count,
1559                                               loff_t *ppos)
1560 {
1561         if (count == 0)
1562                 return 0;
1563
1564         iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_USER_TRIGGER,
1565                                NULL);
1566
1567         iwl_fw_dbg_collect(&mvm->fwrt, FW_DBG_TRIGGER_USER, buf,
1568                            (count - 1), NULL);
1569
1570         return count;
1571 }
1572
1573 static ssize_t iwl_dbgfs_dbg_time_point_write(struct iwl_mvm *mvm,
1574                                               char *buf, size_t count,
1575                                               loff_t *ppos)
1576 {
1577         u32 timepoint;
1578
1579         if (kstrtou32(buf, 0, &timepoint))
1580                 return -EINVAL;
1581
1582         if (timepoint == IWL_FW_INI_TIME_POINT_INVALID ||
1583             timepoint >= IWL_FW_INI_TIME_POINT_NUM)
1584                 return -EINVAL;
1585
1586         iwl_dbg_tlv_time_point(&mvm->fwrt, timepoint, NULL);
1587
1588         return count;
1589 }
1590
1591 #define MVM_DEBUGFS_WRITE_FILE_OPS(name, bufsz) \
1592         _MVM_DEBUGFS_WRITE_FILE_OPS(name, bufsz, struct iwl_mvm)
1593 #define MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz) \
1594         _MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz, struct iwl_mvm)
1595 #define MVM_DEBUGFS_ADD_FILE_ALIAS(alias, name, parent, mode) do {      \
1596                 debugfs_create_file(alias, mode, parent, mvm,           \
1597                                     &iwl_dbgfs_##name##_ops);           \
1598         } while (0)
1599 #define MVM_DEBUGFS_ADD_FILE(name, parent, mode) \
1600         MVM_DEBUGFS_ADD_FILE_ALIAS(#name, name, parent, mode)
1601
1602 static ssize_t
1603 _iwl_dbgfs_link_sta_wrap_write(ssize_t (*real)(struct ieee80211_link_sta *,
1604                                                struct iwl_mvm_sta *,
1605                                                struct iwl_mvm *,
1606                                                struct iwl_mvm_link_sta *,
1607                                                char *,
1608                                                size_t, loff_t *),
1609                            struct file *file,
1610                            char *buf, size_t buf_size, loff_t *ppos)
1611 {
1612         struct ieee80211_link_sta *link_sta = file->private_data;
1613         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(link_sta->sta);
1614         struct iwl_mvm *mvm = iwl_mvm_vif_from_mac80211(mvmsta->vif)->mvm;
1615         struct iwl_mvm_link_sta *mvm_link_sta;
1616         ssize_t ret;
1617
1618         mutex_lock(&mvm->mutex);
1619
1620         mvm_link_sta = rcu_dereference_protected(mvmsta->link[link_sta->link_id],
1621                                                  lockdep_is_held(&mvm->mutex));
1622         if (WARN_ON(!mvm_link_sta)) {
1623                 mutex_unlock(&mvm->mutex);
1624                 return -ENODEV;
1625         }
1626
1627         ret = real(link_sta, mvmsta, mvm, mvm_link_sta, buf, buf_size, ppos);
1628
1629         mutex_unlock(&mvm->mutex);
1630
1631         return ret;
1632 }
1633
1634 static ssize_t
1635 _iwl_dbgfs_link_sta_wrap_read(ssize_t (*real)(struct ieee80211_link_sta *,
1636                                               struct iwl_mvm_sta *,
1637                                               struct iwl_mvm *,
1638                                               struct iwl_mvm_link_sta *,
1639                                               char __user *,
1640                                               size_t, loff_t *),
1641                            struct file *file,
1642                            char __user *user_buf, size_t count, loff_t *ppos)
1643 {
1644         struct ieee80211_link_sta *link_sta = file->private_data;
1645         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(link_sta->sta);
1646         struct iwl_mvm *mvm = iwl_mvm_vif_from_mac80211(mvmsta->vif)->mvm;
1647         struct iwl_mvm_link_sta *mvm_link_sta;
1648         ssize_t ret;
1649
1650         mutex_lock(&mvm->mutex);
1651
1652         mvm_link_sta = rcu_dereference_protected(mvmsta->link[link_sta->link_id],
1653                                                  lockdep_is_held(&mvm->mutex));
1654         if (WARN_ON(!mvm_link_sta)) {
1655                 mutex_unlock(&mvm->mutex);
1656                 return -ENODEV;
1657         }
1658
1659         ret = real(link_sta, mvmsta, mvm, mvm_link_sta, user_buf, count, ppos);
1660
1661         mutex_unlock(&mvm->mutex);
1662
1663         return ret;
1664 }
1665
1666 #define MVM_DEBUGFS_LINK_STA_WRITE_WRAPPER(name, buflen)                \
1667 static ssize_t _iwl_dbgfs_link_sta_##name##_write(struct file *file,    \
1668                                          const char __user *user_buf,   \
1669                                          size_t count, loff_t *ppos)    \
1670 {                                                                       \
1671         char buf[buflen] = {};                                          \
1672         size_t buf_size = min(count, sizeof(buf) -  1);                 \
1673                                                                         \
1674         if (copy_from_user(buf, user_buf, sizeof(buf)))                 \
1675                 return -EFAULT;                                         \
1676                                                                         \
1677         return _iwl_dbgfs_link_sta_wrap_write(iwl_dbgfs_##name##_write, \
1678                                               file,                     \
1679                                               buf, buf_size, ppos);     \
1680 }                                                                       \
1681
1682 #define MVM_DEBUGFS_LINK_STA_READ_WRAPPER(name)         \
1683 static ssize_t _iwl_dbgfs_link_sta_##name##_read(struct file *file,     \
1684                                          char __user *user_buf,         \
1685                                          size_t count, loff_t *ppos)    \
1686 {                                                                       \
1687         return _iwl_dbgfs_link_sta_wrap_read(iwl_dbgfs_##name##_read,   \
1688                                              file,                      \
1689                                              user_buf, count, ppos);    \
1690 }                                                                       \
1691
1692 #define MVM_DEBUGFS_WRITE_LINK_STA_FILE_OPS(name, bufsz)                \
1693 MVM_DEBUGFS_LINK_STA_WRITE_WRAPPER(name, bufsz)                         \
1694 static const struct file_operations iwl_dbgfs_link_sta_##name##_ops = { \
1695         .write = _iwl_dbgfs_link_sta_##name##_write,                    \
1696         .open = simple_open,                                            \
1697         .llseek = generic_file_llseek,                                  \
1698 }
1699
1700 #define MVM_DEBUGFS_READ_LINK_STA_FILE_OPS(name)                        \
1701 MVM_DEBUGFS_LINK_STA_READ_WRAPPER(name)                                 \
1702 static const struct file_operations iwl_dbgfs_link_sta_##name##_ops = { \
1703         .read = _iwl_dbgfs_link_sta_##name##_read,                      \
1704         .open = simple_open,                                            \
1705         .llseek = generic_file_llseek,                                  \
1706 }
1707
1708 #define MVM_DEBUGFS_READ_WRITE_LINK_STA_FILE_OPS(name, bufsz)           \
1709 MVM_DEBUGFS_LINK_STA_READ_WRAPPER(name)                                 \
1710 MVM_DEBUGFS_LINK_STA_WRITE_WRAPPER(name, bufsz)                         \
1711 static const struct file_operations iwl_dbgfs_link_sta_##name##_ops = { \
1712         .read = _iwl_dbgfs_link_sta_##name##_read,                      \
1713         .write = _iwl_dbgfs_link_sta_##name##_write,                    \
1714         .open = simple_open,                                            \
1715         .llseek = generic_file_llseek,                                  \
1716 }
1717
1718 #define MVM_DEBUGFS_ADD_LINK_STA_FILE_ALIAS(alias, name, parent, mode)  \
1719                 debugfs_create_file(alias, mode, parent, link_sta,      \
1720                                     &iwl_dbgfs_link_sta_##name##_ops)
1721 #define MVM_DEBUGFS_ADD_LINK_STA_FILE(name, parent, mode) \
1722         MVM_DEBUGFS_ADD_LINK_STA_FILE_ALIAS(#name, name, parent, mode)
1723
1724 static ssize_t
1725 iwl_dbgfs_prph_reg_read(struct file *file,
1726                         char __user *user_buf,
1727                         size_t count, loff_t *ppos)
1728 {
1729         struct iwl_mvm *mvm = file->private_data;
1730         int pos = 0;
1731         char buf[32];
1732         const size_t bufsz = sizeof(buf);
1733
1734         if (!mvm->dbgfs_prph_reg_addr)
1735                 return -EINVAL;
1736
1737         pos += scnprintf(buf + pos, bufsz - pos, "Reg 0x%x: (0x%x)\n",
1738                 mvm->dbgfs_prph_reg_addr,
1739                 iwl_read_prph(mvm->trans, mvm->dbgfs_prph_reg_addr));
1740
1741         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1742 }
1743
1744 static ssize_t
1745 iwl_dbgfs_prph_reg_write(struct iwl_mvm *mvm, char *buf,
1746                          size_t count, loff_t *ppos)
1747 {
1748         u8 args;
1749         u32 value;
1750
1751         args = sscanf(buf, "%i %i", &mvm->dbgfs_prph_reg_addr, &value);
1752         /* if we only want to set the reg address - nothing more to do */
1753         if (args == 1)
1754                 goto out;
1755
1756         /* otherwise, make sure we have both address and value */
1757         if (args != 2)
1758                 return -EINVAL;
1759
1760         iwl_write_prph(mvm->trans, mvm->dbgfs_prph_reg_addr, value);
1761
1762 out:
1763         return count;
1764 }
1765
1766 static ssize_t
1767 iwl_dbgfs_send_echo_cmd_write(struct iwl_mvm *mvm, char *buf,
1768                               size_t count, loff_t *ppos)
1769 {
1770         int ret;
1771
1772         if (!iwl_mvm_firmware_running(mvm))
1773                 return -EIO;
1774
1775         mutex_lock(&mvm->mutex);
1776         ret = iwl_mvm_send_cmd_pdu(mvm, ECHO_CMD, 0, 0, NULL);
1777         mutex_unlock(&mvm->mutex);
1778
1779         return ret ?: count;
1780 }
1781
1782 struct iwl_mvm_sniffer_apply {
1783         struct iwl_mvm *mvm;
1784         u8 *bssid;
1785         u16 aid;
1786 };
1787
1788 static bool iwl_mvm_sniffer_apply(struct iwl_notif_wait_data *notif_data,
1789                                   struct iwl_rx_packet *pkt, void *data)
1790 {
1791         struct iwl_mvm_sniffer_apply *apply = data;
1792
1793         apply->mvm->cur_aid = cpu_to_le16(apply->aid);
1794         memcpy(apply->mvm->cur_bssid, apply->bssid,
1795                sizeof(apply->mvm->cur_bssid));
1796
1797         return true;
1798 }
1799
1800 static ssize_t
1801 iwl_dbgfs_he_sniffer_params_write(struct iwl_mvm *mvm, char *buf,
1802                                   size_t count, loff_t *ppos)
1803 {
1804         struct iwl_notification_wait wait;
1805         struct iwl_he_monitor_cmd he_mon_cmd = {};
1806         struct iwl_mvm_sniffer_apply apply = {
1807                 .mvm = mvm,
1808         };
1809         u16 wait_cmds[] = {
1810                 WIDE_ID(DATA_PATH_GROUP, HE_AIR_SNIFFER_CONFIG_CMD),
1811         };
1812         u32 aid;
1813         int ret;
1814
1815         if (!iwl_mvm_firmware_running(mvm))
1816                 return -EIO;
1817
1818         ret = sscanf(buf, "%x %2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx", &aid,
1819                      &he_mon_cmd.bssid[0], &he_mon_cmd.bssid[1],
1820                      &he_mon_cmd.bssid[2], &he_mon_cmd.bssid[3],
1821                      &he_mon_cmd.bssid[4], &he_mon_cmd.bssid[5]);
1822         if (ret != 7)
1823                 return -EINVAL;
1824
1825         he_mon_cmd.aid = cpu_to_le16(aid);
1826
1827         apply.aid = aid;
1828         apply.bssid = (void *)he_mon_cmd.bssid;
1829
1830         mutex_lock(&mvm->mutex);
1831
1832         /*
1833          * Use the notification waiter to get our function triggered
1834          * in sequence with other RX. This ensures that frames we get
1835          * on the RX queue _before_ the new configuration is applied
1836          * still have mvm->cur_aid pointing to the old AID, and that
1837          * frames on the RX queue _after_ the firmware processed the
1838          * new configuration (and sent the response, synchronously)
1839          * get mvm->cur_aid correctly set to the new AID.
1840          */
1841         iwl_init_notification_wait(&mvm->notif_wait, &wait,
1842                                    wait_cmds, ARRAY_SIZE(wait_cmds),
1843                                    iwl_mvm_sniffer_apply, &apply);
1844
1845         ret = iwl_mvm_send_cmd_pdu(mvm,
1846                                    WIDE_ID(DATA_PATH_GROUP, HE_AIR_SNIFFER_CONFIG_CMD),
1847                                    0,
1848                                    sizeof(he_mon_cmd), &he_mon_cmd);
1849
1850         /* no need to really wait, we already did anyway */
1851         iwl_remove_notification(&mvm->notif_wait, &wait);
1852
1853         mutex_unlock(&mvm->mutex);
1854
1855         return ret ?: count;
1856 }
1857
1858 static ssize_t
1859 iwl_dbgfs_he_sniffer_params_read(struct file *file, char __user *user_buf,
1860                                  size_t count, loff_t *ppos)
1861 {
1862         struct iwl_mvm *mvm = file->private_data;
1863         u8 buf[32];
1864         int len;
1865
1866         len = scnprintf(buf, sizeof(buf),
1867                         "%d %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",
1868                         le16_to_cpu(mvm->cur_aid), mvm->cur_bssid[0],
1869                         mvm->cur_bssid[1], mvm->cur_bssid[2], mvm->cur_bssid[3],
1870                         mvm->cur_bssid[4], mvm->cur_bssid[5]);
1871
1872         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1873 }
1874
1875 static ssize_t
1876 iwl_dbgfs_uapsd_noagg_bssids_read(struct file *file, char __user *user_buf,
1877                                   size_t count, loff_t *ppos)
1878 {
1879         struct iwl_mvm *mvm = file->private_data;
1880         u8 buf[IWL_MVM_UAPSD_NOAGG_BSSIDS_NUM * ETH_ALEN * 3 + 1];
1881         unsigned int pos = 0;
1882         size_t bufsz = sizeof(buf);
1883         int i;
1884
1885         mutex_lock(&mvm->mutex);
1886
1887         for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++)
1888                 pos += scnprintf(buf + pos, bufsz - pos, "%pM\n",
1889                                  mvm->uapsd_noagg_bssids[i].addr);
1890
1891         mutex_unlock(&mvm->mutex);
1892
1893         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1894 }
1895
1896 static ssize_t
1897 iwl_dbgfs_ltr_config_write(struct iwl_mvm *mvm,
1898                            char *buf, size_t count, loff_t *ppos)
1899 {
1900         int ret;
1901         struct iwl_ltr_config_cmd ltr_config = {0};
1902
1903         if (!iwl_mvm_firmware_running(mvm))
1904                 return -EIO;
1905
1906         if (sscanf(buf, "%x,%x,%x,%x,%x,%x,%x",
1907                    &ltr_config.flags,
1908                    &ltr_config.static_long,
1909                    &ltr_config.static_short,
1910                    &ltr_config.ltr_cfg_values[0],
1911                    &ltr_config.ltr_cfg_values[1],
1912                    &ltr_config.ltr_cfg_values[2],
1913                    &ltr_config.ltr_cfg_values[3]) != 7) {
1914                 return -EINVAL;
1915         }
1916
1917         mutex_lock(&mvm->mutex);
1918         ret = iwl_mvm_send_cmd_pdu(mvm, LTR_CONFIG, 0, sizeof(ltr_config),
1919                                    &ltr_config);
1920         mutex_unlock(&mvm->mutex);
1921
1922         if (ret)
1923                 IWL_ERR(mvm, "failed to send ltr configuration cmd\n");
1924
1925         return ret ?: count;
1926 }
1927
1928 static ssize_t iwl_dbgfs_rfi_freq_table_write(struct iwl_mvm *mvm, char *buf,
1929                                               size_t count, loff_t *ppos)
1930 {
1931         int ret = 0;
1932         u16 op_id;
1933
1934         if (kstrtou16(buf, 10, &op_id))
1935                 return -EINVAL;
1936
1937         /* value zero triggers re-sending the default table to the device */
1938         if (!op_id) {
1939                 mutex_lock(&mvm->mutex);
1940                 ret = iwl_rfi_send_config_cmd(mvm, NULL);
1941                 mutex_unlock(&mvm->mutex);
1942         } else {
1943                 ret = -EOPNOTSUPP; /* in the future a new table will be added */
1944         }
1945
1946         return ret ?: count;
1947 }
1948
1949 /* The size computation is as follows:
1950  * each number needs at most 3 characters, number of rows is the size of
1951  * the table; So, need 5 chars for the "freq: " part and each tuple afterwards
1952  * needs 6 characters for numbers and 5 for the punctuation around.
1953  */
1954 #define IWL_RFI_BUF_SIZE (IWL_RFI_LUT_INSTALLED_SIZE *\
1955                                 (5 + IWL_RFI_LUT_ENTRY_CHANNELS_NUM * (6 + 5)))
1956
1957 static ssize_t iwl_dbgfs_rfi_freq_table_read(struct file *file,
1958                                              char __user *user_buf,
1959                                              size_t count, loff_t *ppos)
1960 {
1961         struct iwl_mvm *mvm = file->private_data;
1962         struct iwl_rfi_freq_table_resp_cmd *resp;
1963         u32 status;
1964         char buf[IWL_RFI_BUF_SIZE];
1965         int i, j, pos = 0;
1966
1967         resp = iwl_rfi_get_freq_table(mvm);
1968         if (IS_ERR(resp))
1969                 return PTR_ERR(resp);
1970
1971         status = le32_to_cpu(resp->status);
1972         if (status != RFI_FREQ_TABLE_OK) {
1973                 scnprintf(buf, IWL_RFI_BUF_SIZE, "status = %d\n", status);
1974                 goto out;
1975         }
1976
1977         for (i = 0; i < ARRAY_SIZE(resp->table); i++) {
1978                 pos += scnprintf(buf + pos, IWL_RFI_BUF_SIZE - pos, "%d: ",
1979                                  resp->table[i].freq);
1980
1981                 for (j = 0; j < ARRAY_SIZE(resp->table[i].channels); j++)
1982                         pos += scnprintf(buf + pos, IWL_RFI_BUF_SIZE - pos,
1983                                          "(%d, %d) ",
1984                                          resp->table[i].channels[j],
1985                                          resp->table[i].bands[j]);
1986                 pos += scnprintf(buf + pos, IWL_RFI_BUF_SIZE - pos, "\n");
1987         }
1988
1989 out:
1990         kfree(resp);
1991         return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1992 }
1993
1994 MVM_DEBUGFS_READ_WRITE_FILE_OPS(prph_reg, 64);
1995
1996 /* Device wide debugfs entries */
1997 MVM_DEBUGFS_READ_FILE_OPS(ctdp_budget);
1998 MVM_DEBUGFS_WRITE_FILE_OPS(stop_ctdp, 8);
1999 MVM_DEBUGFS_WRITE_FILE_OPS(force_ctkill, 8);
2000 MVM_DEBUGFS_WRITE_FILE_OPS(tx_flush, 16);
2001 MVM_DEBUGFS_WRITE_FILE_OPS(sta_drain, 8);
2002 MVM_DEBUGFS_WRITE_FILE_OPS(send_echo_cmd, 8);
2003 MVM_DEBUGFS_READ_WRITE_FILE_OPS(sram, 64);
2004 MVM_DEBUGFS_READ_WRITE_FILE_OPS(set_nic_temperature, 64);
2005 MVM_DEBUGFS_READ_FILE_OPS(nic_temp);
2006 MVM_DEBUGFS_READ_FILE_OPS(stations);
2007 MVM_DEBUGFS_READ_LINK_STA_FILE_OPS(rs_data);
2008 MVM_DEBUGFS_READ_FILE_OPS(bt_notif);
2009 MVM_DEBUGFS_READ_FILE_OPS(bt_cmd);
2010 MVM_DEBUGFS_READ_WRITE_FILE_OPS(disable_power_off, 64);
2011 MVM_DEBUGFS_READ_FILE_OPS(fw_rx_stats);
2012 MVM_DEBUGFS_READ_FILE_OPS(drv_rx_stats);
2013 MVM_DEBUGFS_READ_FILE_OPS(fw_ver);
2014 MVM_DEBUGFS_READ_FILE_OPS(phy_integration_ver);
2015 MVM_DEBUGFS_READ_FILE_OPS(tas_get_status);
2016 MVM_DEBUGFS_WRITE_FILE_OPS(fw_restart, 10);
2017 MVM_DEBUGFS_WRITE_FILE_OPS(fw_nmi, 10);
2018 MVM_DEBUGFS_WRITE_FILE_OPS(bt_tx_prio, 10);
2019 MVM_DEBUGFS_WRITE_FILE_OPS(bt_force_ant, 10);
2020 MVM_DEBUGFS_READ_WRITE_FILE_OPS(scan_ant_rxchain, 8);
2021 MVM_DEBUGFS_READ_WRITE_FILE_OPS(fw_dbg_conf, 8);
2022 MVM_DEBUGFS_WRITE_FILE_OPS(fw_dbg_collect, 64);
2023 MVM_DEBUGFS_WRITE_FILE_OPS(dbg_time_point, 64);
2024 MVM_DEBUGFS_WRITE_FILE_OPS(indirection_tbl,
2025                            (IWL_RSS_INDIRECTION_TABLE_SIZE * 2));
2026 MVM_DEBUGFS_WRITE_FILE_OPS(inject_packet, 512);
2027 MVM_DEBUGFS_WRITE_FILE_OPS(inject_beacon_ie, 512);
2028 MVM_DEBUGFS_WRITE_FILE_OPS(inject_beacon_ie_restore, 512);
2029
2030 MVM_DEBUGFS_READ_FILE_OPS(uapsd_noagg_bssids);
2031
2032 #ifdef CONFIG_ACPI
2033 MVM_DEBUGFS_READ_FILE_OPS(sar_geo_profile);
2034 MVM_DEBUGFS_READ_FILE_OPS(wifi_6e_enable);
2035 #endif
2036
2037 MVM_DEBUGFS_READ_WRITE_LINK_STA_FILE_OPS(amsdu_len, 16);
2038
2039 MVM_DEBUGFS_READ_WRITE_FILE_OPS(he_sniffer_params, 32);
2040
2041 MVM_DEBUGFS_WRITE_FILE_OPS(ltr_config, 512);
2042 MVM_DEBUGFS_READ_WRITE_FILE_OPS(rfi_freq_table, 16);
2043
2044 static ssize_t iwl_dbgfs_mem_read(struct file *file, char __user *user_buf,
2045                                   size_t count, loff_t *ppos)
2046 {
2047         struct iwl_mvm *mvm = file->private_data;
2048         struct iwl_dbg_mem_access_cmd cmd = {};
2049         struct iwl_dbg_mem_access_rsp *rsp;
2050         struct iwl_host_cmd hcmd = {
2051                 .flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
2052                 .data = { &cmd, },
2053                 .len = { sizeof(cmd) },
2054         };
2055         size_t delta;
2056         ssize_t ret, len;
2057
2058         if (!iwl_mvm_firmware_running(mvm))
2059                 return -EIO;
2060
2061         hcmd.id = WIDE_ID(DEBUG_GROUP, *ppos >> 24 ? UMAC_RD_WR : LMAC_RD_WR);
2062         cmd.op = cpu_to_le32(DEBUG_MEM_OP_READ);
2063
2064         /* Take care of alignment of both the position and the length */
2065         delta = *ppos & 0x3;
2066         cmd.addr = cpu_to_le32(*ppos - delta);
2067         cmd.len = cpu_to_le32(min(ALIGN(count + delta, 4) / 4,
2068                                   (size_t)DEBUG_MEM_MAX_SIZE_DWORDS));
2069
2070         mutex_lock(&mvm->mutex);
2071         ret = iwl_mvm_send_cmd(mvm, &hcmd);
2072         mutex_unlock(&mvm->mutex);
2073
2074         if (ret < 0)
2075                 return ret;
2076
2077         if (iwl_rx_packet_payload_len(hcmd.resp_pkt) < sizeof(*rsp)) {
2078                 ret = -EIO;
2079                 goto out;
2080         }
2081
2082         rsp = (void *)hcmd.resp_pkt->data;
2083         if (le32_to_cpu(rsp->status) != DEBUG_MEM_STATUS_SUCCESS) {
2084                 ret = -ENXIO;
2085                 goto out;
2086         }
2087
2088         len = min((size_t)le32_to_cpu(rsp->len) << 2,
2089                   iwl_rx_packet_payload_len(hcmd.resp_pkt) - sizeof(*rsp));
2090         len = min(len - delta, count);
2091         if (len < 0) {
2092                 ret = -EFAULT;
2093                 goto out;
2094         }
2095
2096         ret = len - copy_to_user(user_buf, (u8 *)rsp->data + delta, len);
2097         *ppos += ret;
2098
2099 out:
2100         iwl_free_resp(&hcmd);
2101         return ret;
2102 }
2103
2104 static ssize_t iwl_dbgfs_mem_write(struct file *file,
2105                                    const char __user *user_buf, size_t count,
2106                                    loff_t *ppos)
2107 {
2108         struct iwl_mvm *mvm = file->private_data;
2109         struct iwl_dbg_mem_access_cmd *cmd;
2110         struct iwl_dbg_mem_access_rsp *rsp;
2111         struct iwl_host_cmd hcmd = {};
2112         size_t cmd_size;
2113         size_t data_size;
2114         u32 op, len;
2115         ssize_t ret;
2116
2117         if (!iwl_mvm_firmware_running(mvm))
2118                 return -EIO;
2119
2120         hcmd.id = WIDE_ID(DEBUG_GROUP, *ppos >> 24 ? UMAC_RD_WR : LMAC_RD_WR);
2121
2122         if (*ppos & 0x3 || count < 4) {
2123                 op = DEBUG_MEM_OP_WRITE_BYTES;
2124                 len = min(count, (size_t)(4 - (*ppos & 0x3)));
2125                 data_size = len;
2126         } else {
2127                 op = DEBUG_MEM_OP_WRITE;
2128                 len = min(count >> 2, (size_t)DEBUG_MEM_MAX_SIZE_DWORDS);
2129                 data_size = len << 2;
2130         }
2131
2132         cmd_size = sizeof(*cmd) + ALIGN(data_size, 4);
2133         cmd = kzalloc(cmd_size, GFP_KERNEL);
2134         if (!cmd)
2135                 return -ENOMEM;
2136
2137         cmd->op = cpu_to_le32(op);
2138         cmd->len = cpu_to_le32(len);
2139         cmd->addr = cpu_to_le32(*ppos);
2140         if (copy_from_user((void *)cmd->data, user_buf, data_size)) {
2141                 kfree(cmd);
2142                 return -EFAULT;
2143         }
2144
2145         hcmd.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
2146         hcmd.data[0] = (void *)cmd;
2147         hcmd.len[0] = cmd_size;
2148
2149         mutex_lock(&mvm->mutex);
2150         ret = iwl_mvm_send_cmd(mvm, &hcmd);
2151         mutex_unlock(&mvm->mutex);
2152
2153         kfree(cmd);
2154
2155         if (ret < 0)
2156                 return ret;
2157
2158         if (iwl_rx_packet_payload_len(hcmd.resp_pkt) < sizeof(*rsp)) {
2159                 ret = -EIO;
2160                 goto out;
2161         }
2162
2163         rsp = (void *)hcmd.resp_pkt->data;
2164         if (rsp->status != DEBUG_MEM_STATUS_SUCCESS) {
2165                 ret = -ENXIO;
2166                 goto out;
2167         }
2168
2169         ret = data_size;
2170         *ppos += ret;
2171
2172 out:
2173         iwl_free_resp(&hcmd);
2174         return ret;
2175 }
2176
2177 static const struct file_operations iwl_dbgfs_mem_ops = {
2178         .read = iwl_dbgfs_mem_read,
2179         .write = iwl_dbgfs_mem_write,
2180         .open = simple_open,
2181         .llseek = default_llseek,
2182 };
2183
2184 void iwl_mvm_link_sta_add_debugfs(struct ieee80211_hw *hw,
2185                                   struct ieee80211_vif *vif,
2186                                   struct ieee80211_link_sta *link_sta,
2187                                   struct dentry *dir)
2188 {
2189         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2190
2191         if (iwl_mvm_has_tlc_offload(mvm)) {
2192                 MVM_DEBUGFS_ADD_LINK_STA_FILE(rs_data, dir, 0400);
2193         }
2194
2195         MVM_DEBUGFS_ADD_LINK_STA_FILE(amsdu_len, dir, 0600);
2196 }
2197
2198 void iwl_mvm_dbgfs_register(struct iwl_mvm *mvm)
2199 {
2200         struct dentry *bcast_dir __maybe_unused;
2201
2202         spin_lock_init(&mvm->drv_stats_lock);
2203
2204         MVM_DEBUGFS_ADD_FILE(tx_flush, mvm->debugfs_dir, 0200);
2205         MVM_DEBUGFS_ADD_FILE(sta_drain, mvm->debugfs_dir, 0200);
2206         MVM_DEBUGFS_ADD_FILE(sram, mvm->debugfs_dir, 0600);
2207         MVM_DEBUGFS_ADD_FILE(set_nic_temperature, mvm->debugfs_dir, 0600);
2208         MVM_DEBUGFS_ADD_FILE(nic_temp, mvm->debugfs_dir, 0400);
2209         MVM_DEBUGFS_ADD_FILE(ctdp_budget, mvm->debugfs_dir, 0400);
2210         MVM_DEBUGFS_ADD_FILE(stop_ctdp, mvm->debugfs_dir, 0200);
2211         MVM_DEBUGFS_ADD_FILE(force_ctkill, mvm->debugfs_dir, 0200);
2212         MVM_DEBUGFS_ADD_FILE(stations, mvm->debugfs_dir, 0400);
2213         MVM_DEBUGFS_ADD_FILE(bt_notif, mvm->debugfs_dir, 0400);
2214         MVM_DEBUGFS_ADD_FILE(bt_cmd, mvm->debugfs_dir, 0400);
2215         MVM_DEBUGFS_ADD_FILE(disable_power_off, mvm->debugfs_dir, 0600);
2216         MVM_DEBUGFS_ADD_FILE(fw_ver, mvm->debugfs_dir, 0400);
2217         MVM_DEBUGFS_ADD_FILE(fw_rx_stats, mvm->debugfs_dir, 0400);
2218         MVM_DEBUGFS_ADD_FILE(drv_rx_stats, mvm->debugfs_dir, 0400);
2219         MVM_DEBUGFS_ADD_FILE(fw_restart, mvm->debugfs_dir, 0200);
2220         MVM_DEBUGFS_ADD_FILE(fw_nmi, mvm->debugfs_dir, 0200);
2221         MVM_DEBUGFS_ADD_FILE(bt_tx_prio, mvm->debugfs_dir, 0200);
2222         MVM_DEBUGFS_ADD_FILE(bt_force_ant, mvm->debugfs_dir, 0200);
2223         MVM_DEBUGFS_ADD_FILE(scan_ant_rxchain, mvm->debugfs_dir, 0600);
2224         MVM_DEBUGFS_ADD_FILE(prph_reg, mvm->debugfs_dir, 0600);
2225         MVM_DEBUGFS_ADD_FILE(fw_dbg_conf, mvm->debugfs_dir, 0600);
2226         MVM_DEBUGFS_ADD_FILE(fw_dbg_collect, mvm->debugfs_dir, 0200);
2227         MVM_DEBUGFS_ADD_FILE(dbg_time_point, mvm->debugfs_dir, 0200);
2228         MVM_DEBUGFS_ADD_FILE(send_echo_cmd, mvm->debugfs_dir, 0200);
2229         MVM_DEBUGFS_ADD_FILE(indirection_tbl, mvm->debugfs_dir, 0200);
2230         MVM_DEBUGFS_ADD_FILE(inject_packet, mvm->debugfs_dir, 0200);
2231         MVM_DEBUGFS_ADD_FILE(inject_beacon_ie, mvm->debugfs_dir, 0200);
2232         MVM_DEBUGFS_ADD_FILE(inject_beacon_ie_restore, mvm->debugfs_dir, 0200);
2233         MVM_DEBUGFS_ADD_FILE(rfi_freq_table, mvm->debugfs_dir, 0600);
2234
2235         if (mvm->fw->phy_integration_ver)
2236                 MVM_DEBUGFS_ADD_FILE(phy_integration_ver, mvm->debugfs_dir, 0400);
2237         MVM_DEBUGFS_ADD_FILE(tas_get_status, mvm->debugfs_dir, 0400);
2238 #ifdef CONFIG_ACPI
2239         MVM_DEBUGFS_ADD_FILE(sar_geo_profile, mvm->debugfs_dir, 0400);
2240         MVM_DEBUGFS_ADD_FILE(wifi_6e_enable, mvm->debugfs_dir, 0400);
2241 #endif
2242         MVM_DEBUGFS_ADD_FILE(he_sniffer_params, mvm->debugfs_dir, 0600);
2243
2244         if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_SET_LTR_GEN2))
2245                 MVM_DEBUGFS_ADD_FILE(ltr_config, mvm->debugfs_dir, 0200);
2246
2247         debugfs_create_bool("enable_scan_iteration_notif", 0600,
2248                             mvm->debugfs_dir, &mvm->scan_iter_notif_enabled);
2249         debugfs_create_bool("drop_bcn_ap_mode", 0600, mvm->debugfs_dir,
2250                             &mvm->drop_bcn_ap_mode);
2251
2252         MVM_DEBUGFS_ADD_FILE(uapsd_noagg_bssids, mvm->debugfs_dir, S_IRUSR);
2253
2254 #ifdef CONFIG_PM_SLEEP
2255         MVM_DEBUGFS_ADD_FILE(d3_test, mvm->debugfs_dir, 0400);
2256         debugfs_create_bool("d3_wake_sysassert", 0600, mvm->debugfs_dir,
2257                             &mvm->d3_wake_sysassert);
2258         debugfs_create_u32("last_netdetect_scans", 0400, mvm->debugfs_dir,
2259                            &mvm->last_netdetect_scans);
2260 #endif
2261
2262         debugfs_create_u8("ps_disabled", 0400, mvm->debugfs_dir,
2263                           &mvm->ps_disabled);
2264         debugfs_create_blob("nvm_hw", 0400, mvm->debugfs_dir,
2265                             &mvm->nvm_hw_blob);
2266         debugfs_create_blob("nvm_sw", 0400, mvm->debugfs_dir,
2267                             &mvm->nvm_sw_blob);
2268         debugfs_create_blob("nvm_calib", 0400, mvm->debugfs_dir,
2269                             &mvm->nvm_calib_blob);
2270         debugfs_create_blob("nvm_prod", 0400, mvm->debugfs_dir,
2271                             &mvm->nvm_prod_blob);
2272         debugfs_create_blob("nvm_phy_sku", 0400, mvm->debugfs_dir,
2273                             &mvm->nvm_phy_sku_blob);
2274         debugfs_create_blob("nvm_reg", S_IRUSR,
2275                             mvm->debugfs_dir, &mvm->nvm_reg_blob);
2276
2277         debugfs_create_file("mem", 0600, mvm->debugfs_dir, mvm,
2278                             &iwl_dbgfs_mem_ops);
2279
2280         /*
2281          * Create a symlink with mac80211. It will be removed when mac80211
2282          * exists (before the opmode exists which removes the target.)
2283          */
2284         if (!IS_ERR(mvm->debugfs_dir)) {
2285                 char buf[100];
2286
2287                 snprintf(buf, 100, "../../%pd2", mvm->debugfs_dir->d_parent);
2288                 debugfs_create_symlink("iwlwifi", mvm->hw->wiphy->debugfsdir,
2289                                        buf);
2290         }
2291 }