Merge tag 'scsi-misc' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[linux-2.6-block.git] / drivers / net / wireless / ath / ath10k / debug.c
1 /*
2  * Copyright (c) 2005-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #include <linux/module.h>
19 #include <linux/debugfs.h>
20 #include <linux/vmalloc.h>
21 #include <linux/utsname.h>
22
23 #include "core.h"
24 #include "debug.h"
25 #include "hif.h"
26 #include "wmi-ops.h"
27
28 /* ms */
29 #define ATH10K_DEBUG_HTT_STATS_INTERVAL 1000
30
31 #define ATH10K_FW_CRASH_DUMP_VERSION 1
32
33 /**
34  * enum ath10k_fw_crash_dump_type - types of data in the dump file
35  * @ATH10K_FW_CRASH_DUMP_REGDUMP: Register crash dump in binary format
36  */
37 enum ath10k_fw_crash_dump_type {
38         ATH10K_FW_CRASH_DUMP_REGISTERS = 0,
39
40         ATH10K_FW_CRASH_DUMP_MAX,
41 };
42
43 struct ath10k_tlv_dump_data {
44         /* see ath10k_fw_crash_dump_type above */
45         __le32 type;
46
47         /* in bytes */
48         __le32 tlv_len;
49
50         /* pad to 32-bit boundaries as needed */
51         u8 tlv_data[];
52 } __packed;
53
54 struct ath10k_dump_file_data {
55         /* dump file information */
56
57         /* "ATH10K-FW-DUMP" */
58         char df_magic[16];
59
60         __le32 len;
61
62         /* file dump version */
63         __le32 version;
64
65         /* some info we can get from ath10k struct that might help */
66
67         u8 uuid[16];
68
69         __le32 chip_id;
70
71         /* 0 for now, in place for later hardware */
72         __le32 bus_type;
73
74         __le32 target_version;
75         __le32 fw_version_major;
76         __le32 fw_version_minor;
77         __le32 fw_version_release;
78         __le32 fw_version_build;
79         __le32 phy_capability;
80         __le32 hw_min_tx_power;
81         __le32 hw_max_tx_power;
82         __le32 ht_cap_info;
83         __le32 vht_cap_info;
84         __le32 num_rf_chains;
85
86         /* firmware version string */
87         char fw_ver[ETHTOOL_FWVERS_LEN];
88
89         /* Kernel related information */
90
91         /* time-of-day stamp */
92         __le64 tv_sec;
93
94         /* time-of-day stamp, nano-seconds */
95         __le64 tv_nsec;
96
97         /* LINUX_VERSION_CODE */
98         __le32 kernel_ver_code;
99
100         /* VERMAGIC_STRING */
101         char kernel_ver[64];
102
103         /* room for growth w/out changing binary format */
104         u8 unused[128];
105
106         /* struct ath10k_tlv_dump_data + more */
107         u8 data[0];
108 } __packed;
109
110 void ath10k_info(struct ath10k *ar, const char *fmt, ...)
111 {
112         struct va_format vaf = {
113                 .fmt = fmt,
114         };
115         va_list args;
116
117         va_start(args, fmt);
118         vaf.va = &args;
119         dev_info(ar->dev, "%pV", &vaf);
120         trace_ath10k_log_info(ar, &vaf);
121         va_end(args);
122 }
123 EXPORT_SYMBOL(ath10k_info);
124
125 void ath10k_print_driver_info(struct ath10k *ar)
126 {
127         ath10k_info(ar, "%s (0x%08x, 0x%08x) fw %s api %d htt %d.%d wmi %d cal %s max_sta %d\n",
128                     ar->hw_params.name,
129                     ar->target_version,
130                     ar->chip_id,
131                     ar->hw->wiphy->fw_version,
132                     ar->fw_api,
133                     ar->htt.target_version_major,
134                     ar->htt.target_version_minor,
135                     ar->wmi.op_version,
136                     ath10k_cal_mode_str(ar->cal_mode),
137                     ar->max_num_stations);
138         ath10k_info(ar, "debug %d debugfs %d tracing %d dfs %d testmode %d\n",
139                     config_enabled(CONFIG_ATH10K_DEBUG),
140                     config_enabled(CONFIG_ATH10K_DEBUGFS),
141                     config_enabled(CONFIG_ATH10K_TRACING),
142                     config_enabled(CONFIG_ATH10K_DFS_CERTIFIED),
143                     config_enabled(CONFIG_NL80211_TESTMODE));
144 }
145 EXPORT_SYMBOL(ath10k_print_driver_info);
146
147 void ath10k_err(struct ath10k *ar, const char *fmt, ...)
148 {
149         struct va_format vaf = {
150                 .fmt = fmt,
151         };
152         va_list args;
153
154         va_start(args, fmt);
155         vaf.va = &args;
156         dev_err(ar->dev, "%pV", &vaf);
157         trace_ath10k_log_err(ar, &vaf);
158         va_end(args);
159 }
160 EXPORT_SYMBOL(ath10k_err);
161
162 void ath10k_warn(struct ath10k *ar, const char *fmt, ...)
163 {
164         struct va_format vaf = {
165                 .fmt = fmt,
166         };
167         va_list args;
168
169         va_start(args, fmt);
170         vaf.va = &args;
171         dev_warn_ratelimited(ar->dev, "%pV", &vaf);
172         trace_ath10k_log_warn(ar, &vaf);
173
174         va_end(args);
175 }
176 EXPORT_SYMBOL(ath10k_warn);
177
178 #ifdef CONFIG_ATH10K_DEBUGFS
179
180 static ssize_t ath10k_read_wmi_services(struct file *file,
181                                         char __user *user_buf,
182                                         size_t count, loff_t *ppos)
183 {
184         struct ath10k *ar = file->private_data;
185         char *buf;
186         unsigned int len = 0, buf_len = 4096;
187         const char *name;
188         ssize_t ret_cnt;
189         bool enabled;
190         int i;
191
192         buf = kzalloc(buf_len, GFP_KERNEL);
193         if (!buf)
194                 return -ENOMEM;
195
196         mutex_lock(&ar->conf_mutex);
197
198         if (len > buf_len)
199                 len = buf_len;
200
201         spin_lock_bh(&ar->data_lock);
202         for (i = 0; i < WMI_SERVICE_MAX; i++) {
203                 enabled = test_bit(i, ar->wmi.svc_map);
204                 name = wmi_service_name(i);
205
206                 if (!name) {
207                         if (enabled)
208                                 len += scnprintf(buf + len, buf_len - len,
209                                                  "%-40s %s (bit %d)\n",
210                                                  "unknown", "enabled", i);
211
212                         continue;
213                 }
214
215                 len += scnprintf(buf + len, buf_len - len,
216                                  "%-40s %s\n",
217                                  name, enabled ? "enabled" : "-");
218         }
219         spin_unlock_bh(&ar->data_lock);
220
221         ret_cnt = simple_read_from_buffer(user_buf, count, ppos, buf, len);
222
223         mutex_unlock(&ar->conf_mutex);
224
225         kfree(buf);
226         return ret_cnt;
227 }
228
229 static const struct file_operations fops_wmi_services = {
230         .read = ath10k_read_wmi_services,
231         .open = simple_open,
232         .owner = THIS_MODULE,
233         .llseek = default_llseek,
234 };
235
236 static void ath10k_debug_fw_stats_pdevs_free(struct list_head *head)
237 {
238         struct ath10k_fw_stats_pdev *i, *tmp;
239
240         list_for_each_entry_safe(i, tmp, head, list) {
241                 list_del(&i->list);
242                 kfree(i);
243         }
244 }
245
246 static void ath10k_debug_fw_stats_peers_free(struct list_head *head)
247 {
248         struct ath10k_fw_stats_peer *i, *tmp;
249
250         list_for_each_entry_safe(i, tmp, head, list) {
251                 list_del(&i->list);
252                 kfree(i);
253         }
254 }
255
256 static void ath10k_debug_fw_stats_reset(struct ath10k *ar)
257 {
258         spin_lock_bh(&ar->data_lock);
259         ar->debug.fw_stats_done = false;
260         ath10k_debug_fw_stats_pdevs_free(&ar->debug.fw_stats.pdevs);
261         ath10k_debug_fw_stats_peers_free(&ar->debug.fw_stats.peers);
262         spin_unlock_bh(&ar->data_lock);
263 }
264
265 static size_t ath10k_debug_fw_stats_num_peers(struct list_head *head)
266 {
267         struct ath10k_fw_stats_peer *i;
268         size_t num = 0;
269
270         list_for_each_entry(i, head, list)
271                 ++num;
272
273         return num;
274 }
275
276 void ath10k_debug_fw_stats_process(struct ath10k *ar, struct sk_buff *skb)
277 {
278         struct ath10k_fw_stats stats = {};
279         bool is_start, is_started, is_end;
280         size_t num_peers;
281         int ret;
282
283         INIT_LIST_HEAD(&stats.pdevs);
284         INIT_LIST_HEAD(&stats.peers);
285
286         spin_lock_bh(&ar->data_lock);
287         ret = ath10k_wmi_pull_fw_stats(ar, skb, &stats);
288         if (ret) {
289                 ath10k_warn(ar, "failed to pull fw stats: %d\n", ret);
290                 goto unlock;
291         }
292
293         /* Stat data may exceed htc-wmi buffer limit. In such case firmware
294          * splits the stats data and delivers it in a ping-pong fashion of
295          * request cmd-update event.
296          *
297          * However there is no explicit end-of-data. Instead start-of-data is
298          * used as an implicit one. This works as follows:
299          *  a) discard stat update events until one with pdev stats is
300          *     delivered - this skips session started at end of (b)
301          *  b) consume stat update events until another one with pdev stats is
302          *     delivered which is treated as end-of-data and is itself discarded
303          */
304
305         if (ar->debug.fw_stats_done) {
306                 ath10k_warn(ar, "received unsolicited stats update event\n");
307                 goto free;
308         }
309
310         num_peers = ath10k_debug_fw_stats_num_peers(&ar->debug.fw_stats.peers);
311         is_start = (list_empty(&ar->debug.fw_stats.pdevs) &&
312                     !list_empty(&stats.pdevs));
313         is_end = (!list_empty(&ar->debug.fw_stats.pdevs) &&
314                   !list_empty(&stats.pdevs));
315
316         if (is_start)
317                 list_splice_tail_init(&stats.pdevs, &ar->debug.fw_stats.pdevs);
318
319         if (is_end)
320                 ar->debug.fw_stats_done = true;
321
322         is_started = !list_empty(&ar->debug.fw_stats.pdevs);
323
324         if (is_started && !is_end) {
325                 if (num_peers >= ATH10K_MAX_NUM_PEER_IDS) {
326                         /* Although this is unlikely impose a sane limit to
327                          * prevent firmware from DoS-ing the host.
328                          */
329                         ath10k_warn(ar, "dropping fw peer stats\n");
330                         goto free;
331                 }
332
333                 list_splice_tail_init(&stats.peers, &ar->debug.fw_stats.peers);
334         }
335
336         complete(&ar->debug.fw_stats_complete);
337
338 free:
339         /* In some cases lists have been spliced and cleared. Free up
340          * resources if that is not the case.
341          */
342         ath10k_debug_fw_stats_pdevs_free(&stats.pdevs);
343         ath10k_debug_fw_stats_peers_free(&stats.peers);
344
345 unlock:
346         spin_unlock_bh(&ar->data_lock);
347 }
348
349 static int ath10k_debug_fw_stats_request(struct ath10k *ar)
350 {
351         unsigned long timeout;
352         int ret;
353
354         lockdep_assert_held(&ar->conf_mutex);
355
356         timeout = jiffies + msecs_to_jiffies(1*HZ);
357
358         ath10k_debug_fw_stats_reset(ar);
359
360         for (;;) {
361                 if (time_after(jiffies, timeout))
362                         return -ETIMEDOUT;
363
364                 reinit_completion(&ar->debug.fw_stats_complete);
365
366                 ret = ath10k_wmi_request_stats(ar, WMI_REQUEST_PEER_STAT);
367                 if (ret) {
368                         ath10k_warn(ar, "could not request stats (%d)\n", ret);
369                         return ret;
370                 }
371
372                 ret = wait_for_completion_timeout(&ar->debug.fw_stats_complete,
373                                                   1*HZ);
374                 if (ret == 0)
375                         return -ETIMEDOUT;
376
377                 spin_lock_bh(&ar->data_lock);
378                 if (ar->debug.fw_stats_done) {
379                         spin_unlock_bh(&ar->data_lock);
380                         break;
381                 }
382                 spin_unlock_bh(&ar->data_lock);
383         }
384
385         return 0;
386 }
387
388 /* FIXME: How to calculate the buffer size sanely? */
389 #define ATH10K_FW_STATS_BUF_SIZE (1024*1024)
390
391 static void ath10k_fw_stats_fill(struct ath10k *ar,
392                                  struct ath10k_fw_stats *fw_stats,
393                                  char *buf)
394 {
395         unsigned int len = 0;
396         unsigned int buf_len = ATH10K_FW_STATS_BUF_SIZE;
397         const struct ath10k_fw_stats_pdev *pdev;
398         const struct ath10k_fw_stats_peer *peer;
399         size_t num_peers;
400
401         spin_lock_bh(&ar->data_lock);
402
403         pdev = list_first_entry_or_null(&fw_stats->pdevs,
404                                         struct ath10k_fw_stats_pdev, list);
405         if (!pdev) {
406                 ath10k_warn(ar, "failed to get pdev stats\n");
407                 goto unlock;
408         }
409
410         num_peers = ath10k_debug_fw_stats_num_peers(&fw_stats->peers);
411
412         len += scnprintf(buf + len, buf_len - len, "\n");
413         len += scnprintf(buf + len, buf_len - len, "%30s\n",
414                          "ath10k PDEV stats");
415         len += scnprintf(buf + len, buf_len - len, "%30s\n\n",
416                                  "=================");
417
418         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
419                          "Channel noise floor", pdev->ch_noise_floor);
420         len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
421                          "Channel TX power", pdev->chan_tx_power);
422         len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
423                          "TX frame count", pdev->tx_frame_count);
424         len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
425                          "RX frame count", pdev->rx_frame_count);
426         len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
427                          "RX clear count", pdev->rx_clear_count);
428         len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
429                          "Cycle count", pdev->cycle_count);
430         len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
431                          "PHY error count", pdev->phy_err_count);
432         len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
433                          "RTS bad count", pdev->rts_bad);
434         len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
435                          "RTS good count", pdev->rts_good);
436         len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
437                          "FCS bad count", pdev->fcs_bad);
438         len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
439                          "No beacon count", pdev->no_beacons);
440         len += scnprintf(buf + len, buf_len - len, "%30s %10u\n",
441                          "MIB int count", pdev->mib_int_count);
442
443         len += scnprintf(buf + len, buf_len - len, "\n");
444         len += scnprintf(buf + len, buf_len - len, "%30s\n",
445                          "ath10k PDEV TX stats");
446         len += scnprintf(buf + len, buf_len - len, "%30s\n\n",
447                                  "=================");
448
449         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
450                          "HTT cookies queued", pdev->comp_queued);
451         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
452                          "HTT cookies disp.", pdev->comp_delivered);
453         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
454                          "MSDU queued", pdev->msdu_enqued);
455         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
456                          "MPDU queued", pdev->mpdu_enqued);
457         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
458                          "MSDUs dropped", pdev->wmm_drop);
459         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
460                          "Local enqued", pdev->local_enqued);
461         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
462                          "Local freed", pdev->local_freed);
463         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
464                          "HW queued", pdev->hw_queued);
465         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
466                          "PPDUs reaped", pdev->hw_reaped);
467         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
468                          "Num underruns", pdev->underrun);
469         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
470                          "PPDUs cleaned", pdev->tx_abort);
471         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
472                          "MPDUs requed", pdev->mpdus_requed);
473         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
474                          "Excessive retries", pdev->tx_ko);
475         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
476                          "HW rate", pdev->data_rc);
477         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
478                          "Sched self tiggers", pdev->self_triggers);
479         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
480                          "Dropped due to SW retries",
481                          pdev->sw_retry_failure);
482         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
483                          "Illegal rate phy errors",
484                          pdev->illgl_rate_phy_err);
485         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
486                          "Pdev continous xretry", pdev->pdev_cont_xretry);
487         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
488                          "TX timeout", pdev->pdev_tx_timeout);
489         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
490                          "PDEV resets", pdev->pdev_resets);
491         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
492                          "PHY underrun", pdev->phy_underrun);
493         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
494                          "MPDU is more than txop limit", pdev->txop_ovf);
495
496         len += scnprintf(buf + len, buf_len - len, "\n");
497         len += scnprintf(buf + len, buf_len - len, "%30s\n",
498                          "ath10k PDEV RX stats");
499         len += scnprintf(buf + len, buf_len - len, "%30s\n\n",
500                                  "=================");
501
502         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
503                          "Mid PPDU route change",
504                          pdev->mid_ppdu_route_change);
505         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
506                          "Tot. number of statuses", pdev->status_rcvd);
507         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
508                          "Extra frags on rings 0", pdev->r0_frags);
509         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
510                          "Extra frags on rings 1", pdev->r1_frags);
511         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
512                          "Extra frags on rings 2", pdev->r2_frags);
513         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
514                          "Extra frags on rings 3", pdev->r3_frags);
515         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
516                          "MSDUs delivered to HTT", pdev->htt_msdus);
517         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
518                          "MPDUs delivered to HTT", pdev->htt_mpdus);
519         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
520                          "MSDUs delivered to stack", pdev->loc_msdus);
521         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
522                          "MPDUs delivered to stack", pdev->loc_mpdus);
523         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
524                          "Oversized AMSUs", pdev->oversize_amsdu);
525         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
526                          "PHY errors", pdev->phy_errs);
527         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
528                          "PHY errors drops", pdev->phy_err_drop);
529         len += scnprintf(buf + len, buf_len - len, "%30s %10d\n",
530                          "MPDU errors (FCS, MIC, ENC)", pdev->mpdu_errs);
531
532         len += scnprintf(buf + len, buf_len - len, "\n");
533         len += scnprintf(buf + len, buf_len - len, "%30s (%zu)\n",
534                          "ath10k PEER stats", num_peers);
535         len += scnprintf(buf + len, buf_len - len, "%30s\n\n",
536                                  "=================");
537
538         list_for_each_entry(peer, &fw_stats->peers, list) {
539                 len += scnprintf(buf + len, buf_len - len, "%30s %pM\n",
540                                  "Peer MAC address", peer->peer_macaddr);
541                 len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
542                                  "Peer RSSI", peer->peer_rssi);
543                 len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
544                                  "Peer TX rate", peer->peer_tx_rate);
545                 len += scnprintf(buf + len, buf_len - len, "%30s %u\n",
546                                  "Peer RX rate", peer->peer_rx_rate);
547                 len += scnprintf(buf + len, buf_len - len, "\n");
548         }
549
550 unlock:
551         spin_unlock_bh(&ar->data_lock);
552
553         if (len >= buf_len)
554                 buf[len - 1] = 0;
555         else
556                 buf[len] = 0;
557 }
558
559 static int ath10k_fw_stats_open(struct inode *inode, struct file *file)
560 {
561         struct ath10k *ar = inode->i_private;
562         void *buf = NULL;
563         int ret;
564
565         mutex_lock(&ar->conf_mutex);
566
567         if (ar->state != ATH10K_STATE_ON) {
568                 ret = -ENETDOWN;
569                 goto err_unlock;
570         }
571
572         buf = vmalloc(ATH10K_FW_STATS_BUF_SIZE);
573         if (!buf) {
574                 ret = -ENOMEM;
575                 goto err_unlock;
576         }
577
578         ret = ath10k_debug_fw_stats_request(ar);
579         if (ret) {
580                 ath10k_warn(ar, "failed to request fw stats: %d\n", ret);
581                 goto err_free;
582         }
583
584         ath10k_fw_stats_fill(ar, &ar->debug.fw_stats, buf);
585         file->private_data = buf;
586
587         mutex_unlock(&ar->conf_mutex);
588         return 0;
589
590 err_free:
591         vfree(buf);
592
593 err_unlock:
594         mutex_unlock(&ar->conf_mutex);
595         return ret;
596 }
597
598 static int ath10k_fw_stats_release(struct inode *inode, struct file *file)
599 {
600         vfree(file->private_data);
601
602         return 0;
603 }
604
605 static ssize_t ath10k_fw_stats_read(struct file *file, char __user *user_buf,
606                                     size_t count, loff_t *ppos)
607 {
608         const char *buf = file->private_data;
609         unsigned int len = strlen(buf);
610
611         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
612 }
613
614 static const struct file_operations fops_fw_stats = {
615         .open = ath10k_fw_stats_open,
616         .release = ath10k_fw_stats_release,
617         .read = ath10k_fw_stats_read,
618         .owner = THIS_MODULE,
619         .llseek = default_llseek,
620 };
621
622 static ssize_t ath10k_debug_fw_reset_stats_read(struct file *file,
623                                                 char __user *user_buf,
624                                                 size_t count, loff_t *ppos)
625 {
626         struct ath10k *ar = file->private_data;
627         int ret, len, buf_len;
628         char *buf;
629
630         buf_len = 500;
631         buf = kmalloc(buf_len, GFP_KERNEL);
632         if (!buf)
633                 return -ENOMEM;
634
635         spin_lock_bh(&ar->data_lock);
636
637         len = 0;
638         len += scnprintf(buf + len, buf_len - len,
639                          "fw_crash_counter\t\t%d\n", ar->stats.fw_crash_counter);
640         len += scnprintf(buf + len, buf_len - len,
641                          "fw_warm_reset_counter\t\t%d\n",
642                          ar->stats.fw_warm_reset_counter);
643         len += scnprintf(buf + len, buf_len - len,
644                          "fw_cold_reset_counter\t\t%d\n",
645                          ar->stats.fw_cold_reset_counter);
646
647         spin_unlock_bh(&ar->data_lock);
648
649         ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
650
651         kfree(buf);
652
653         return ret;
654 }
655
656 static const struct file_operations fops_fw_reset_stats = {
657         .open = simple_open,
658         .read = ath10k_debug_fw_reset_stats_read,
659         .owner = THIS_MODULE,
660         .llseek = default_llseek,
661 };
662
663 /* This is a clean assert crash in firmware. */
664 static int ath10k_debug_fw_assert(struct ath10k *ar)
665 {
666         struct wmi_vdev_install_key_cmd *cmd;
667         struct sk_buff *skb;
668
669         skb = ath10k_wmi_alloc_skb(ar, sizeof(*cmd) + 16);
670         if (!skb)
671                 return -ENOMEM;
672
673         cmd = (struct wmi_vdev_install_key_cmd *)skb->data;
674         memset(cmd, 0, sizeof(*cmd));
675
676         /* big enough number so that firmware asserts */
677         cmd->vdev_id = __cpu_to_le32(0x7ffe);
678
679         return ath10k_wmi_cmd_send(ar, skb,
680                                    ar->wmi.cmd->vdev_install_key_cmdid);
681 }
682
683 static ssize_t ath10k_read_simulate_fw_crash(struct file *file,
684                                              char __user *user_buf,
685                                              size_t count, loff_t *ppos)
686 {
687         const char buf[] =
688                 "To simulate firmware crash write one of the keywords to this file:\n"
689                 "`soft` - this will send WMI_FORCE_FW_HANG_ASSERT to firmware if FW supports that command.\n"
690                 "`hard` - this will send to firmware command with illegal parameters causing firmware crash.\n"
691                 "`assert` - this will send special illegal parameter to firmware to cause assert failure and crash.\n"
692                 "`hw-restart` - this will simply queue hw restart without fw/hw actually crashing.\n";
693
694         return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
695 }
696
697 /* Simulate firmware crash:
698  * 'soft': Call wmi command causing firmware hang. This firmware hang is
699  * recoverable by warm firmware reset.
700  * 'hard': Force firmware crash by setting any vdev parameter for not allowed
701  * vdev id. This is hard firmware crash because it is recoverable only by cold
702  * firmware reset.
703  */
704 static ssize_t ath10k_write_simulate_fw_crash(struct file *file,
705                                               const char __user *user_buf,
706                                               size_t count, loff_t *ppos)
707 {
708         struct ath10k *ar = file->private_data;
709         char buf[32];
710         int ret;
711
712         mutex_lock(&ar->conf_mutex);
713
714         simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, user_buf, count);
715
716         /* make sure that buf is null terminated */
717         buf[sizeof(buf) - 1] = 0;
718
719         if (ar->state != ATH10K_STATE_ON &&
720             ar->state != ATH10K_STATE_RESTARTED) {
721                 ret = -ENETDOWN;
722                 goto exit;
723         }
724
725         /* drop the possible '\n' from the end */
726         if (buf[count - 1] == '\n') {
727                 buf[count - 1] = 0;
728                 count--;
729         }
730
731         if (!strcmp(buf, "soft")) {
732                 ath10k_info(ar, "simulating soft firmware crash\n");
733                 ret = ath10k_wmi_force_fw_hang(ar, WMI_FORCE_FW_HANG_ASSERT, 0);
734         } else if (!strcmp(buf, "hard")) {
735                 ath10k_info(ar, "simulating hard firmware crash\n");
736                 /* 0x7fff is vdev id, and it is always out of range for all
737                  * firmware variants in order to force a firmware crash.
738                  */
739                 ret = ath10k_wmi_vdev_set_param(ar, 0x7fff,
740                                                 ar->wmi.vdev_param->rts_threshold,
741                                                 0);
742         } else if (!strcmp(buf, "assert")) {
743                 ath10k_info(ar, "simulating firmware assert crash\n");
744                 ret = ath10k_debug_fw_assert(ar);
745         } else if (!strcmp(buf, "hw-restart")) {
746                 ath10k_info(ar, "user requested hw restart\n");
747                 queue_work(ar->workqueue, &ar->restart_work);
748                 ret = 0;
749         } else {
750                 ret = -EINVAL;
751                 goto exit;
752         }
753
754         if (ret) {
755                 ath10k_warn(ar, "failed to simulate firmware crash: %d\n", ret);
756                 goto exit;
757         }
758
759         ret = count;
760
761 exit:
762         mutex_unlock(&ar->conf_mutex);
763         return ret;
764 }
765
766 static const struct file_operations fops_simulate_fw_crash = {
767         .read = ath10k_read_simulate_fw_crash,
768         .write = ath10k_write_simulate_fw_crash,
769         .open = simple_open,
770         .owner = THIS_MODULE,
771         .llseek = default_llseek,
772 };
773
774 static ssize_t ath10k_read_chip_id(struct file *file, char __user *user_buf,
775                                    size_t count, loff_t *ppos)
776 {
777         struct ath10k *ar = file->private_data;
778         unsigned int len;
779         char buf[50];
780
781         len = scnprintf(buf, sizeof(buf), "0x%08x\n", ar->chip_id);
782
783         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
784 }
785
786 static const struct file_operations fops_chip_id = {
787         .read = ath10k_read_chip_id,
788         .open = simple_open,
789         .owner = THIS_MODULE,
790         .llseek = default_llseek,
791 };
792
793 struct ath10k_fw_crash_data *
794 ath10k_debug_get_new_fw_crash_data(struct ath10k *ar)
795 {
796         struct ath10k_fw_crash_data *crash_data = ar->debug.fw_crash_data;
797
798         lockdep_assert_held(&ar->data_lock);
799
800         crash_data->crashed_since_read = true;
801         uuid_le_gen(&crash_data->uuid);
802         getnstimeofday(&crash_data->timestamp);
803
804         return crash_data;
805 }
806 EXPORT_SYMBOL(ath10k_debug_get_new_fw_crash_data);
807
808 static struct ath10k_dump_file_data *ath10k_build_dump_file(struct ath10k *ar)
809 {
810         struct ath10k_fw_crash_data *crash_data = ar->debug.fw_crash_data;
811         struct ath10k_dump_file_data *dump_data;
812         struct ath10k_tlv_dump_data *dump_tlv;
813         int hdr_len = sizeof(*dump_data);
814         unsigned int len, sofar = 0;
815         unsigned char *buf;
816
817         len = hdr_len;
818         len += sizeof(*dump_tlv) + sizeof(crash_data->registers);
819
820         sofar += hdr_len;
821
822         /* This is going to get big when we start dumping FW RAM and such,
823          * so go ahead and use vmalloc.
824          */
825         buf = vzalloc(len);
826         if (!buf)
827                 return NULL;
828
829         spin_lock_bh(&ar->data_lock);
830
831         if (!crash_data->crashed_since_read) {
832                 spin_unlock_bh(&ar->data_lock);
833                 vfree(buf);
834                 return NULL;
835         }
836
837         dump_data = (struct ath10k_dump_file_data *)(buf);
838         strlcpy(dump_data->df_magic, "ATH10K-FW-DUMP",
839                 sizeof(dump_data->df_magic));
840         dump_data->len = cpu_to_le32(len);
841
842         dump_data->version = cpu_to_le32(ATH10K_FW_CRASH_DUMP_VERSION);
843
844         memcpy(dump_data->uuid, &crash_data->uuid, sizeof(dump_data->uuid));
845         dump_data->chip_id = cpu_to_le32(ar->chip_id);
846         dump_data->bus_type = cpu_to_le32(0);
847         dump_data->target_version = cpu_to_le32(ar->target_version);
848         dump_data->fw_version_major = cpu_to_le32(ar->fw_version_major);
849         dump_data->fw_version_minor = cpu_to_le32(ar->fw_version_minor);
850         dump_data->fw_version_release = cpu_to_le32(ar->fw_version_release);
851         dump_data->fw_version_build = cpu_to_le32(ar->fw_version_build);
852         dump_data->phy_capability = cpu_to_le32(ar->phy_capability);
853         dump_data->hw_min_tx_power = cpu_to_le32(ar->hw_min_tx_power);
854         dump_data->hw_max_tx_power = cpu_to_le32(ar->hw_max_tx_power);
855         dump_data->ht_cap_info = cpu_to_le32(ar->ht_cap_info);
856         dump_data->vht_cap_info = cpu_to_le32(ar->vht_cap_info);
857         dump_data->num_rf_chains = cpu_to_le32(ar->num_rf_chains);
858
859         strlcpy(dump_data->fw_ver, ar->hw->wiphy->fw_version,
860                 sizeof(dump_data->fw_ver));
861
862         dump_data->kernel_ver_code = 0;
863         strlcpy(dump_data->kernel_ver, init_utsname()->release,
864                 sizeof(dump_data->kernel_ver));
865
866         dump_data->tv_sec = cpu_to_le64(crash_data->timestamp.tv_sec);
867         dump_data->tv_nsec = cpu_to_le64(crash_data->timestamp.tv_nsec);
868
869         /* Gather crash-dump */
870         dump_tlv = (struct ath10k_tlv_dump_data *)(buf + sofar);
871         dump_tlv->type = cpu_to_le32(ATH10K_FW_CRASH_DUMP_REGISTERS);
872         dump_tlv->tlv_len = cpu_to_le32(sizeof(crash_data->registers));
873         memcpy(dump_tlv->tlv_data, &crash_data->registers,
874                sizeof(crash_data->registers));
875         sofar += sizeof(*dump_tlv) + sizeof(crash_data->registers);
876
877         ar->debug.fw_crash_data->crashed_since_read = false;
878
879         spin_unlock_bh(&ar->data_lock);
880
881         return dump_data;
882 }
883
884 static int ath10k_fw_crash_dump_open(struct inode *inode, struct file *file)
885 {
886         struct ath10k *ar = inode->i_private;
887         struct ath10k_dump_file_data *dump;
888
889         dump = ath10k_build_dump_file(ar);
890         if (!dump)
891                 return -ENODATA;
892
893         file->private_data = dump;
894
895         return 0;
896 }
897
898 static ssize_t ath10k_fw_crash_dump_read(struct file *file,
899                                          char __user *user_buf,
900                                          size_t count, loff_t *ppos)
901 {
902         struct ath10k_dump_file_data *dump_file = file->private_data;
903
904         return simple_read_from_buffer(user_buf, count, ppos,
905                                        dump_file,
906                                        le32_to_cpu(dump_file->len));
907 }
908
909 static int ath10k_fw_crash_dump_release(struct inode *inode,
910                                         struct file *file)
911 {
912         vfree(file->private_data);
913
914         return 0;
915 }
916
917 static const struct file_operations fops_fw_crash_dump = {
918         .open = ath10k_fw_crash_dump_open,
919         .read = ath10k_fw_crash_dump_read,
920         .release = ath10k_fw_crash_dump_release,
921         .owner = THIS_MODULE,
922         .llseek = default_llseek,
923 };
924
925 static ssize_t ath10k_reg_addr_read(struct file *file,
926                                     char __user *user_buf,
927                                     size_t count, loff_t *ppos)
928 {
929         struct ath10k *ar = file->private_data;
930         u8 buf[32];
931         unsigned int len = 0;
932         u32 reg_addr;
933
934         mutex_lock(&ar->conf_mutex);
935         reg_addr = ar->debug.reg_addr;
936         mutex_unlock(&ar->conf_mutex);
937
938         len += scnprintf(buf + len, sizeof(buf) - len, "0x%x\n", reg_addr);
939
940         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
941 }
942
943 static ssize_t ath10k_reg_addr_write(struct file *file,
944                                      const char __user *user_buf,
945                                      size_t count, loff_t *ppos)
946 {
947         struct ath10k *ar = file->private_data;
948         u32 reg_addr;
949         int ret;
950
951         ret = kstrtou32_from_user(user_buf, count, 0, &reg_addr);
952         if (ret)
953                 return ret;
954
955         if (!IS_ALIGNED(reg_addr, 4))
956                 return -EFAULT;
957
958         mutex_lock(&ar->conf_mutex);
959         ar->debug.reg_addr = reg_addr;
960         mutex_unlock(&ar->conf_mutex);
961
962         return count;
963 }
964
965 static const struct file_operations fops_reg_addr = {
966         .read = ath10k_reg_addr_read,
967         .write = ath10k_reg_addr_write,
968         .open = simple_open,
969         .owner = THIS_MODULE,
970         .llseek = default_llseek,
971 };
972
973 static ssize_t ath10k_reg_value_read(struct file *file,
974                                      char __user *user_buf,
975                                      size_t count, loff_t *ppos)
976 {
977         struct ath10k *ar = file->private_data;
978         u8 buf[48];
979         unsigned int len;
980         u32 reg_addr, reg_val;
981         int ret;
982
983         mutex_lock(&ar->conf_mutex);
984
985         if (ar->state != ATH10K_STATE_ON &&
986             ar->state != ATH10K_STATE_UTF) {
987                 ret = -ENETDOWN;
988                 goto exit;
989         }
990
991         reg_addr = ar->debug.reg_addr;
992
993         reg_val = ath10k_hif_read32(ar, reg_addr);
994         len = scnprintf(buf, sizeof(buf), "0x%08x:0x%08x\n", reg_addr, reg_val);
995
996         ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
997
998 exit:
999         mutex_unlock(&ar->conf_mutex);
1000
1001         return ret;
1002 }
1003
1004 static ssize_t ath10k_reg_value_write(struct file *file,
1005                                       const char __user *user_buf,
1006                                       size_t count, loff_t *ppos)
1007 {
1008         struct ath10k *ar = file->private_data;
1009         u32 reg_addr, reg_val;
1010         int ret;
1011
1012         mutex_lock(&ar->conf_mutex);
1013
1014         if (ar->state != ATH10K_STATE_ON &&
1015             ar->state != ATH10K_STATE_UTF) {
1016                 ret = -ENETDOWN;
1017                 goto exit;
1018         }
1019
1020         reg_addr = ar->debug.reg_addr;
1021
1022         ret = kstrtou32_from_user(user_buf, count, 0, &reg_val);
1023         if (ret)
1024                 goto exit;
1025
1026         ath10k_hif_write32(ar, reg_addr, reg_val);
1027
1028         ret = count;
1029
1030 exit:
1031         mutex_unlock(&ar->conf_mutex);
1032
1033         return ret;
1034 }
1035
1036 static const struct file_operations fops_reg_value = {
1037         .read = ath10k_reg_value_read,
1038         .write = ath10k_reg_value_write,
1039         .open = simple_open,
1040         .owner = THIS_MODULE,
1041         .llseek = default_llseek,
1042 };
1043
1044 static ssize_t ath10k_mem_value_read(struct file *file,
1045                                      char __user *user_buf,
1046                                      size_t count, loff_t *ppos)
1047 {
1048         struct ath10k *ar = file->private_data;
1049         u8 *buf;
1050         int ret;
1051
1052         if (*ppos < 0)
1053                 return -EINVAL;
1054
1055         if (!count)
1056                 return 0;
1057
1058         mutex_lock(&ar->conf_mutex);
1059
1060         buf = vmalloc(count);
1061         if (!buf) {
1062                 ret = -ENOMEM;
1063                 goto exit;
1064         }
1065
1066         if (ar->state != ATH10K_STATE_ON &&
1067             ar->state != ATH10K_STATE_UTF) {
1068                 ret = -ENETDOWN;
1069                 goto exit;
1070         }
1071
1072         ret = ath10k_hif_diag_read(ar, *ppos, buf, count);
1073         if (ret) {
1074                 ath10k_warn(ar, "failed to read address 0x%08x via diagnose window fnrom debugfs: %d\n",
1075                             (u32)(*ppos), ret);
1076                 goto exit;
1077         }
1078
1079         ret = copy_to_user(user_buf, buf, count);
1080         if (ret) {
1081                 ret = -EFAULT;
1082                 goto exit;
1083         }
1084
1085         count -= ret;
1086         *ppos += count;
1087         ret = count;
1088
1089 exit:
1090         vfree(buf);
1091         mutex_unlock(&ar->conf_mutex);
1092
1093         return ret;
1094 }
1095
1096 static ssize_t ath10k_mem_value_write(struct file *file,
1097                                       const char __user *user_buf,
1098                                       size_t count, loff_t *ppos)
1099 {
1100         struct ath10k *ar = file->private_data;
1101         u8 *buf;
1102         int ret;
1103
1104         if (*ppos < 0)
1105                 return -EINVAL;
1106
1107         if (!count)
1108                 return 0;
1109
1110         mutex_lock(&ar->conf_mutex);
1111
1112         buf = vmalloc(count);
1113         if (!buf) {
1114                 ret = -ENOMEM;
1115                 goto exit;
1116         }
1117
1118         if (ar->state != ATH10K_STATE_ON &&
1119             ar->state != ATH10K_STATE_UTF) {
1120                 ret = -ENETDOWN;
1121                 goto exit;
1122         }
1123
1124         ret = copy_from_user(buf, user_buf, count);
1125         if (ret) {
1126                 ret = -EFAULT;
1127                 goto exit;
1128         }
1129
1130         ret = ath10k_hif_diag_write(ar, *ppos, buf, count);
1131         if (ret) {
1132                 ath10k_warn(ar, "failed to write address 0x%08x via diagnose window from debugfs: %d\n",
1133                             (u32)(*ppos), ret);
1134                 goto exit;
1135         }
1136
1137         *ppos += count;
1138         ret = count;
1139
1140 exit:
1141         vfree(buf);
1142         mutex_unlock(&ar->conf_mutex);
1143
1144         return ret;
1145 }
1146
1147 static const struct file_operations fops_mem_value = {
1148         .read = ath10k_mem_value_read,
1149         .write = ath10k_mem_value_write,
1150         .open = simple_open,
1151         .owner = THIS_MODULE,
1152         .llseek = default_llseek,
1153 };
1154
1155 static int ath10k_debug_htt_stats_req(struct ath10k *ar)
1156 {
1157         u64 cookie;
1158         int ret;
1159
1160         lockdep_assert_held(&ar->conf_mutex);
1161
1162         if (ar->debug.htt_stats_mask == 0)
1163                 /* htt stats are disabled */
1164                 return 0;
1165
1166         if (ar->state != ATH10K_STATE_ON)
1167                 return 0;
1168
1169         cookie = get_jiffies_64();
1170
1171         ret = ath10k_htt_h2t_stats_req(&ar->htt, ar->debug.htt_stats_mask,
1172                                        cookie);
1173         if (ret) {
1174                 ath10k_warn(ar, "failed to send htt stats request: %d\n", ret);
1175                 return ret;
1176         }
1177
1178         queue_delayed_work(ar->workqueue, &ar->debug.htt_stats_dwork,
1179                            msecs_to_jiffies(ATH10K_DEBUG_HTT_STATS_INTERVAL));
1180
1181         return 0;
1182 }
1183
1184 static void ath10k_debug_htt_stats_dwork(struct work_struct *work)
1185 {
1186         struct ath10k *ar = container_of(work, struct ath10k,
1187                                          debug.htt_stats_dwork.work);
1188
1189         mutex_lock(&ar->conf_mutex);
1190
1191         ath10k_debug_htt_stats_req(ar);
1192
1193         mutex_unlock(&ar->conf_mutex);
1194 }
1195
1196 static ssize_t ath10k_read_htt_stats_mask(struct file *file,
1197                                           char __user *user_buf,
1198                                           size_t count, loff_t *ppos)
1199 {
1200         struct ath10k *ar = file->private_data;
1201         char buf[32];
1202         unsigned int len;
1203
1204         len = scnprintf(buf, sizeof(buf), "%lu\n", ar->debug.htt_stats_mask);
1205
1206         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1207 }
1208
1209 static ssize_t ath10k_write_htt_stats_mask(struct file *file,
1210                                            const char __user *user_buf,
1211                                            size_t count, loff_t *ppos)
1212 {
1213         struct ath10k *ar = file->private_data;
1214         unsigned long mask;
1215         int ret;
1216
1217         ret = kstrtoul_from_user(user_buf, count, 0, &mask);
1218         if (ret)
1219                 return ret;
1220
1221         /* max 8 bit masks (for now) */
1222         if (mask > 0xff)
1223                 return -E2BIG;
1224
1225         mutex_lock(&ar->conf_mutex);
1226
1227         ar->debug.htt_stats_mask = mask;
1228
1229         ret = ath10k_debug_htt_stats_req(ar);
1230         if (ret)
1231                 goto out;
1232
1233         ret = count;
1234
1235 out:
1236         mutex_unlock(&ar->conf_mutex);
1237
1238         return ret;
1239 }
1240
1241 static const struct file_operations fops_htt_stats_mask = {
1242         .read = ath10k_read_htt_stats_mask,
1243         .write = ath10k_write_htt_stats_mask,
1244         .open = simple_open,
1245         .owner = THIS_MODULE,
1246         .llseek = default_llseek,
1247 };
1248
1249 static ssize_t ath10k_read_htt_max_amsdu_ampdu(struct file *file,
1250                                                char __user *user_buf,
1251                                                size_t count, loff_t *ppos)
1252 {
1253         struct ath10k *ar = file->private_data;
1254         char buf[64];
1255         u8 amsdu = 3, ampdu = 64;
1256         unsigned int len;
1257
1258         mutex_lock(&ar->conf_mutex);
1259
1260         if (ar->debug.htt_max_amsdu)
1261                 amsdu = ar->debug.htt_max_amsdu;
1262
1263         if (ar->debug.htt_max_ampdu)
1264                 ampdu = ar->debug.htt_max_ampdu;
1265
1266         mutex_unlock(&ar->conf_mutex);
1267
1268         len = scnprintf(buf, sizeof(buf), "%u %u\n", amsdu, ampdu);
1269
1270         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1271 }
1272
1273 static ssize_t ath10k_write_htt_max_amsdu_ampdu(struct file *file,
1274                                                 const char __user *user_buf,
1275                                                 size_t count, loff_t *ppos)
1276 {
1277         struct ath10k *ar = file->private_data;
1278         int res;
1279         char buf[64];
1280         unsigned int amsdu, ampdu;
1281
1282         simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, user_buf, count);
1283
1284         /* make sure that buf is null terminated */
1285         buf[sizeof(buf) - 1] = 0;
1286
1287         res = sscanf(buf, "%u %u", &amsdu, &ampdu);
1288
1289         if (res != 2)
1290                 return -EINVAL;
1291
1292         mutex_lock(&ar->conf_mutex);
1293
1294         res = ath10k_htt_h2t_aggr_cfg_msg(&ar->htt, ampdu, amsdu);
1295         if (res)
1296                 goto out;
1297
1298         res = count;
1299         ar->debug.htt_max_amsdu = amsdu;
1300         ar->debug.htt_max_ampdu = ampdu;
1301
1302 out:
1303         mutex_unlock(&ar->conf_mutex);
1304         return res;
1305 }
1306
1307 static const struct file_operations fops_htt_max_amsdu_ampdu = {
1308         .read = ath10k_read_htt_max_amsdu_ampdu,
1309         .write = ath10k_write_htt_max_amsdu_ampdu,
1310         .open = simple_open,
1311         .owner = THIS_MODULE,
1312         .llseek = default_llseek,
1313 };
1314
1315 static ssize_t ath10k_read_fw_dbglog(struct file *file,
1316                                      char __user *user_buf,
1317                                      size_t count, loff_t *ppos)
1318 {
1319         struct ath10k *ar = file->private_data;
1320         unsigned int len;
1321         char buf[64];
1322
1323         len = scnprintf(buf, sizeof(buf), "0x%08x %u\n",
1324                         ar->debug.fw_dbglog_mask, ar->debug.fw_dbglog_level);
1325
1326         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1327 }
1328
1329 static ssize_t ath10k_write_fw_dbglog(struct file *file,
1330                                       const char __user *user_buf,
1331                                       size_t count, loff_t *ppos)
1332 {
1333         struct ath10k *ar = file->private_data;
1334         int ret;
1335         char buf[64];
1336         unsigned int log_level, mask;
1337
1338         simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, user_buf, count);
1339
1340         /* make sure that buf is null terminated */
1341         buf[sizeof(buf) - 1] = 0;
1342
1343         ret = sscanf(buf, "%x %u", &mask, &log_level);
1344
1345         if (!ret)
1346                 return -EINVAL;
1347
1348         if (ret == 1)
1349                 /* default if user did not specify */
1350                 log_level = ATH10K_DBGLOG_LEVEL_WARN;
1351
1352         mutex_lock(&ar->conf_mutex);
1353
1354         ar->debug.fw_dbglog_mask = mask;
1355         ar->debug.fw_dbglog_level = log_level;
1356
1357         if (ar->state == ATH10K_STATE_ON) {
1358                 ret = ath10k_wmi_dbglog_cfg(ar, ar->debug.fw_dbglog_mask,
1359                                             ar->debug.fw_dbglog_level);
1360                 if (ret) {
1361                         ath10k_warn(ar, "dbglog cfg failed from debugfs: %d\n",
1362                                     ret);
1363                         goto exit;
1364                 }
1365         }
1366
1367         ret = count;
1368
1369 exit:
1370         mutex_unlock(&ar->conf_mutex);
1371
1372         return ret;
1373 }
1374
1375 /* TODO:  Would be nice to always support ethtool stats, would need to
1376  * move the stats storage out of ath10k_debug, or always have ath10k_debug
1377  * struct available..
1378  */
1379
1380 /* This generally cooresponds to the debugfs fw_stats file */
1381 static const char ath10k_gstrings_stats[][ETH_GSTRING_LEN] = {
1382         "tx_pkts_nic",
1383         "tx_bytes_nic",
1384         "rx_pkts_nic",
1385         "rx_bytes_nic",
1386         "d_noise_floor",
1387         "d_cycle_count",
1388         "d_phy_error",
1389         "d_rts_bad",
1390         "d_rts_good",
1391         "d_tx_power", /* in .5 dbM I think */
1392         "d_rx_crc_err", /* fcs_bad */
1393         "d_no_beacon",
1394         "d_tx_mpdus_queued",
1395         "d_tx_msdu_queued",
1396         "d_tx_msdu_dropped",
1397         "d_local_enqued",
1398         "d_local_freed",
1399         "d_tx_ppdu_hw_queued",
1400         "d_tx_ppdu_reaped",
1401         "d_tx_fifo_underrun",
1402         "d_tx_ppdu_abort",
1403         "d_tx_mpdu_requed",
1404         "d_tx_excessive_retries",
1405         "d_tx_hw_rate",
1406         "d_tx_dropped_sw_retries",
1407         "d_tx_illegal_rate",
1408         "d_tx_continuous_xretries",
1409         "d_tx_timeout",
1410         "d_tx_mpdu_txop_limit",
1411         "d_pdev_resets",
1412         "d_rx_mid_ppdu_route_change",
1413         "d_rx_status",
1414         "d_rx_extra_frags_ring0",
1415         "d_rx_extra_frags_ring1",
1416         "d_rx_extra_frags_ring2",
1417         "d_rx_extra_frags_ring3",
1418         "d_rx_msdu_htt",
1419         "d_rx_mpdu_htt",
1420         "d_rx_msdu_stack",
1421         "d_rx_mpdu_stack",
1422         "d_rx_phy_err",
1423         "d_rx_phy_err_drops",
1424         "d_rx_mpdu_errors", /* FCS, MIC, ENC */
1425         "d_fw_crash_count",
1426         "d_fw_warm_reset_count",
1427         "d_fw_cold_reset_count",
1428 };
1429
1430 #define ATH10K_SSTATS_LEN ARRAY_SIZE(ath10k_gstrings_stats)
1431
1432 void ath10k_debug_get_et_strings(struct ieee80211_hw *hw,
1433                                  struct ieee80211_vif *vif,
1434                                  u32 sset, u8 *data)
1435 {
1436         if (sset == ETH_SS_STATS)
1437                 memcpy(data, *ath10k_gstrings_stats,
1438                        sizeof(ath10k_gstrings_stats));
1439 }
1440
1441 int ath10k_debug_get_et_sset_count(struct ieee80211_hw *hw,
1442                                    struct ieee80211_vif *vif, int sset)
1443 {
1444         if (sset == ETH_SS_STATS)
1445                 return ATH10K_SSTATS_LEN;
1446
1447         return 0;
1448 }
1449
1450 void ath10k_debug_get_et_stats(struct ieee80211_hw *hw,
1451                                struct ieee80211_vif *vif,
1452                                struct ethtool_stats *stats, u64 *data)
1453 {
1454         struct ath10k *ar = hw->priv;
1455         static const struct ath10k_fw_stats_pdev zero_stats = {};
1456         const struct ath10k_fw_stats_pdev *pdev_stats;
1457         int i = 0, ret;
1458
1459         mutex_lock(&ar->conf_mutex);
1460
1461         if (ar->state == ATH10K_STATE_ON) {
1462                 ret = ath10k_debug_fw_stats_request(ar);
1463                 if (ret) {
1464                         /* just print a warning and try to use older results */
1465                         ath10k_warn(ar,
1466                                     "failed to get fw stats for ethtool: %d\n",
1467                                     ret);
1468                 }
1469         }
1470
1471         pdev_stats = list_first_entry_or_null(&ar->debug.fw_stats.pdevs,
1472                                               struct ath10k_fw_stats_pdev,
1473                                               list);
1474         if (!pdev_stats) {
1475                 /* no results available so just return zeroes */
1476                 pdev_stats = &zero_stats;
1477         }
1478
1479         spin_lock_bh(&ar->data_lock);
1480
1481         data[i++] = pdev_stats->hw_reaped; /* ppdu reaped */
1482         data[i++] = 0; /* tx bytes */
1483         data[i++] = pdev_stats->htt_mpdus;
1484         data[i++] = 0; /* rx bytes */
1485         data[i++] = pdev_stats->ch_noise_floor;
1486         data[i++] = pdev_stats->cycle_count;
1487         data[i++] = pdev_stats->phy_err_count;
1488         data[i++] = pdev_stats->rts_bad;
1489         data[i++] = pdev_stats->rts_good;
1490         data[i++] = pdev_stats->chan_tx_power;
1491         data[i++] = pdev_stats->fcs_bad;
1492         data[i++] = pdev_stats->no_beacons;
1493         data[i++] = pdev_stats->mpdu_enqued;
1494         data[i++] = pdev_stats->msdu_enqued;
1495         data[i++] = pdev_stats->wmm_drop;
1496         data[i++] = pdev_stats->local_enqued;
1497         data[i++] = pdev_stats->local_freed;
1498         data[i++] = pdev_stats->hw_queued;
1499         data[i++] = pdev_stats->hw_reaped;
1500         data[i++] = pdev_stats->underrun;
1501         data[i++] = pdev_stats->tx_abort;
1502         data[i++] = pdev_stats->mpdus_requed;
1503         data[i++] = pdev_stats->tx_ko;
1504         data[i++] = pdev_stats->data_rc;
1505         data[i++] = pdev_stats->sw_retry_failure;
1506         data[i++] = pdev_stats->illgl_rate_phy_err;
1507         data[i++] = pdev_stats->pdev_cont_xretry;
1508         data[i++] = pdev_stats->pdev_tx_timeout;
1509         data[i++] = pdev_stats->txop_ovf;
1510         data[i++] = pdev_stats->pdev_resets;
1511         data[i++] = pdev_stats->mid_ppdu_route_change;
1512         data[i++] = pdev_stats->status_rcvd;
1513         data[i++] = pdev_stats->r0_frags;
1514         data[i++] = pdev_stats->r1_frags;
1515         data[i++] = pdev_stats->r2_frags;
1516         data[i++] = pdev_stats->r3_frags;
1517         data[i++] = pdev_stats->htt_msdus;
1518         data[i++] = pdev_stats->htt_mpdus;
1519         data[i++] = pdev_stats->loc_msdus;
1520         data[i++] = pdev_stats->loc_mpdus;
1521         data[i++] = pdev_stats->phy_errs;
1522         data[i++] = pdev_stats->phy_err_drop;
1523         data[i++] = pdev_stats->mpdu_errs;
1524         data[i++] = ar->stats.fw_crash_counter;
1525         data[i++] = ar->stats.fw_warm_reset_counter;
1526         data[i++] = ar->stats.fw_cold_reset_counter;
1527
1528         spin_unlock_bh(&ar->data_lock);
1529
1530         mutex_unlock(&ar->conf_mutex);
1531
1532         WARN_ON(i != ATH10K_SSTATS_LEN);
1533 }
1534
1535 static const struct file_operations fops_fw_dbglog = {
1536         .read = ath10k_read_fw_dbglog,
1537         .write = ath10k_write_fw_dbglog,
1538         .open = simple_open,
1539         .owner = THIS_MODULE,
1540         .llseek = default_llseek,
1541 };
1542
1543 static int ath10k_debug_cal_data_open(struct inode *inode, struct file *file)
1544 {
1545         struct ath10k *ar = inode->i_private;
1546         void *buf;
1547         u32 hi_addr;
1548         __le32 addr;
1549         int ret;
1550
1551         mutex_lock(&ar->conf_mutex);
1552
1553         if (ar->state != ATH10K_STATE_ON &&
1554             ar->state != ATH10K_STATE_UTF) {
1555                 ret = -ENETDOWN;
1556                 goto err;
1557         }
1558
1559         buf = vmalloc(QCA988X_CAL_DATA_LEN);
1560         if (!buf) {
1561                 ret = -ENOMEM;
1562                 goto err;
1563         }
1564
1565         hi_addr = host_interest_item_address(HI_ITEM(hi_board_data));
1566
1567         ret = ath10k_hif_diag_read(ar, hi_addr, &addr, sizeof(addr));
1568         if (ret) {
1569                 ath10k_warn(ar, "failed to read hi_board_data address: %d\n", ret);
1570                 goto err_vfree;
1571         }
1572
1573         ret = ath10k_hif_diag_read(ar, le32_to_cpu(addr), buf,
1574                                    QCA988X_CAL_DATA_LEN);
1575         if (ret) {
1576                 ath10k_warn(ar, "failed to read calibration data: %d\n", ret);
1577                 goto err_vfree;
1578         }
1579
1580         file->private_data = buf;
1581
1582         mutex_unlock(&ar->conf_mutex);
1583
1584         return 0;
1585
1586 err_vfree:
1587         vfree(buf);
1588
1589 err:
1590         mutex_unlock(&ar->conf_mutex);
1591
1592         return ret;
1593 }
1594
1595 static ssize_t ath10k_debug_cal_data_read(struct file *file,
1596                                           char __user *user_buf,
1597                                           size_t count, loff_t *ppos)
1598 {
1599         void *buf = file->private_data;
1600
1601         return simple_read_from_buffer(user_buf, count, ppos,
1602                                        buf, QCA988X_CAL_DATA_LEN);
1603 }
1604
1605 static int ath10k_debug_cal_data_release(struct inode *inode,
1606                                          struct file *file)
1607 {
1608         vfree(file->private_data);
1609
1610         return 0;
1611 }
1612
1613 static const struct file_operations fops_cal_data = {
1614         .open = ath10k_debug_cal_data_open,
1615         .read = ath10k_debug_cal_data_read,
1616         .release = ath10k_debug_cal_data_release,
1617         .owner = THIS_MODULE,
1618         .llseek = default_llseek,
1619 };
1620
1621 static ssize_t ath10k_read_nf_cal_period(struct file *file,
1622                                          char __user *user_buf,
1623                                          size_t count, loff_t *ppos)
1624 {
1625         struct ath10k *ar = file->private_data;
1626         unsigned int len;
1627         char buf[32];
1628
1629         len = scnprintf(buf, sizeof(buf), "%d\n",
1630                         ar->debug.nf_cal_period);
1631
1632         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1633 }
1634
1635 static ssize_t ath10k_write_nf_cal_period(struct file *file,
1636                                           const char __user *user_buf,
1637                                           size_t count, loff_t *ppos)
1638 {
1639         struct ath10k *ar = file->private_data;
1640         unsigned long period;
1641         int ret;
1642
1643         ret = kstrtoul_from_user(user_buf, count, 0, &period);
1644         if (ret)
1645                 return ret;
1646
1647         if (period > WMI_PDEV_PARAM_CAL_PERIOD_MAX)
1648                 return -EINVAL;
1649
1650         /* there's no way to switch back to the firmware default */
1651         if (period == 0)
1652                 return -EINVAL;
1653
1654         mutex_lock(&ar->conf_mutex);
1655
1656         ar->debug.nf_cal_period = period;
1657
1658         if (ar->state != ATH10K_STATE_ON) {
1659                 /* firmware is not running, nothing else to do */
1660                 ret = count;
1661                 goto exit;
1662         }
1663
1664         ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->cal_period,
1665                                         ar->debug.nf_cal_period);
1666         if (ret) {
1667                 ath10k_warn(ar, "cal period cfg failed from debugfs: %d\n",
1668                             ret);
1669                 goto exit;
1670         }
1671
1672         ret = count;
1673
1674 exit:
1675         mutex_unlock(&ar->conf_mutex);
1676
1677         return ret;
1678 }
1679
1680 static const struct file_operations fops_nf_cal_period = {
1681         .read = ath10k_read_nf_cal_period,
1682         .write = ath10k_write_nf_cal_period,
1683         .open = simple_open,
1684         .owner = THIS_MODULE,
1685         .llseek = default_llseek,
1686 };
1687
1688 int ath10k_debug_start(struct ath10k *ar)
1689 {
1690         int ret;
1691
1692         lockdep_assert_held(&ar->conf_mutex);
1693
1694         ret = ath10k_debug_htt_stats_req(ar);
1695         if (ret)
1696                 /* continue normally anyway, this isn't serious */
1697                 ath10k_warn(ar, "failed to start htt stats workqueue: %d\n",
1698                             ret);
1699
1700         if (ar->debug.fw_dbglog_mask) {
1701                 ret = ath10k_wmi_dbglog_cfg(ar, ar->debug.fw_dbglog_mask,
1702                                             ATH10K_DBGLOG_LEVEL_WARN);
1703                 if (ret)
1704                         /* not serious */
1705                         ath10k_warn(ar, "failed to enable dbglog during start: %d",
1706                                     ret);
1707         }
1708
1709         if (ar->debug.pktlog_filter) {
1710                 ret = ath10k_wmi_pdev_pktlog_enable(ar,
1711                                                     ar->debug.pktlog_filter);
1712                 if (ret)
1713                         /* not serious */
1714                         ath10k_warn(ar,
1715                                     "failed to enable pktlog filter %x: %d\n",
1716                                     ar->debug.pktlog_filter, ret);
1717         } else {
1718                 ret = ath10k_wmi_pdev_pktlog_disable(ar);
1719                 if (ret)
1720                         /* not serious */
1721                         ath10k_warn(ar, "failed to disable pktlog: %d\n", ret);
1722         }
1723
1724         if (ar->debug.nf_cal_period) {
1725                 ret = ath10k_wmi_pdev_set_param(ar,
1726                                                 ar->wmi.pdev_param->cal_period,
1727                                                 ar->debug.nf_cal_period);
1728                 if (ret)
1729                         /* not serious */
1730                         ath10k_warn(ar, "cal period cfg failed from debug start: %d\n",
1731                                     ret);
1732         }
1733
1734         return ret;
1735 }
1736
1737 void ath10k_debug_stop(struct ath10k *ar)
1738 {
1739         lockdep_assert_held(&ar->conf_mutex);
1740
1741         /* Must not use _sync to avoid deadlock, we do that in
1742          * ath10k_debug_destroy(). The check for htt_stats_mask is to avoid
1743          * warning from del_timer(). */
1744         if (ar->debug.htt_stats_mask != 0)
1745                 cancel_delayed_work(&ar->debug.htt_stats_dwork);
1746
1747         ar->debug.htt_max_amsdu = 0;
1748         ar->debug.htt_max_ampdu = 0;
1749
1750         ath10k_wmi_pdev_pktlog_disable(ar);
1751 }
1752
1753 static ssize_t ath10k_write_simulate_radar(struct file *file,
1754                                            const char __user *user_buf,
1755                                            size_t count, loff_t *ppos)
1756 {
1757         struct ath10k *ar = file->private_data;
1758
1759         ieee80211_radar_detected(ar->hw);
1760
1761         return count;
1762 }
1763
1764 static const struct file_operations fops_simulate_radar = {
1765         .write = ath10k_write_simulate_radar,
1766         .open = simple_open,
1767         .owner = THIS_MODULE,
1768         .llseek = default_llseek,
1769 };
1770
1771 #define ATH10K_DFS_STAT(s, p) (\
1772         len += scnprintf(buf + len, size - len, "%-28s : %10u\n", s, \
1773                          ar->debug.dfs_stats.p))
1774
1775 #define ATH10K_DFS_POOL_STAT(s, p) (\
1776         len += scnprintf(buf + len, size - len, "%-28s : %10u\n", s, \
1777                          ar->debug.dfs_pool_stats.p))
1778
1779 static ssize_t ath10k_read_dfs_stats(struct file *file, char __user *user_buf,
1780                                      size_t count, loff_t *ppos)
1781 {
1782         int retval = 0, len = 0;
1783         const int size = 8000;
1784         struct ath10k *ar = file->private_data;
1785         char *buf;
1786
1787         buf = kzalloc(size, GFP_KERNEL);
1788         if (buf == NULL)
1789                 return -ENOMEM;
1790
1791         if (!ar->dfs_detector) {
1792                 len += scnprintf(buf + len, size - len, "DFS not enabled\n");
1793                 goto exit;
1794         }
1795
1796         ar->debug.dfs_pool_stats =
1797                         ar->dfs_detector->get_stats(ar->dfs_detector);
1798
1799         len += scnprintf(buf + len, size - len, "Pulse detector statistics:\n");
1800
1801         ATH10K_DFS_STAT("reported phy errors", phy_errors);
1802         ATH10K_DFS_STAT("pulse events reported", pulses_total);
1803         ATH10K_DFS_STAT("DFS pulses detected", pulses_detected);
1804         ATH10K_DFS_STAT("DFS pulses discarded", pulses_discarded);
1805         ATH10K_DFS_STAT("Radars detected", radar_detected);
1806
1807         len += scnprintf(buf + len, size - len, "Global Pool statistics:\n");
1808         ATH10K_DFS_POOL_STAT("Pool references", pool_reference);
1809         ATH10K_DFS_POOL_STAT("Pulses allocated", pulse_allocated);
1810         ATH10K_DFS_POOL_STAT("Pulses alloc error", pulse_alloc_error);
1811         ATH10K_DFS_POOL_STAT("Pulses in use", pulse_used);
1812         ATH10K_DFS_POOL_STAT("Seqs. allocated", pseq_allocated);
1813         ATH10K_DFS_POOL_STAT("Seqs. alloc error", pseq_alloc_error);
1814         ATH10K_DFS_POOL_STAT("Seqs. in use", pseq_used);
1815
1816 exit:
1817         if (len > size)
1818                 len = size;
1819
1820         retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
1821         kfree(buf);
1822
1823         return retval;
1824 }
1825
1826 static const struct file_operations fops_dfs_stats = {
1827         .read = ath10k_read_dfs_stats,
1828         .open = simple_open,
1829         .owner = THIS_MODULE,
1830         .llseek = default_llseek,
1831 };
1832
1833 static ssize_t ath10k_write_pktlog_filter(struct file *file,
1834                                           const char __user *ubuf,
1835                                           size_t count, loff_t *ppos)
1836 {
1837         struct ath10k *ar = file->private_data;
1838         u32 filter;
1839         int ret;
1840
1841         if (kstrtouint_from_user(ubuf, count, 0, &filter))
1842                 return -EINVAL;
1843
1844         mutex_lock(&ar->conf_mutex);
1845
1846         if (ar->state != ATH10K_STATE_ON) {
1847                 ar->debug.pktlog_filter = filter;
1848                 ret = count;
1849                 goto out;
1850         }
1851
1852         if (filter && (filter != ar->debug.pktlog_filter)) {
1853                 ret = ath10k_wmi_pdev_pktlog_enable(ar, filter);
1854                 if (ret) {
1855                         ath10k_warn(ar, "failed to enable pktlog filter %x: %d\n",
1856                                     ar->debug.pktlog_filter, ret);
1857                         goto out;
1858                 }
1859         } else {
1860                 ret = ath10k_wmi_pdev_pktlog_disable(ar);
1861                 if (ret) {
1862                         ath10k_warn(ar, "failed to disable pktlog: %d\n", ret);
1863                         goto out;
1864                 }
1865         }
1866
1867         ar->debug.pktlog_filter = filter;
1868         ret = count;
1869
1870 out:
1871         mutex_unlock(&ar->conf_mutex);
1872         return ret;
1873 }
1874
1875 static ssize_t ath10k_read_pktlog_filter(struct file *file, char __user *ubuf,
1876                                          size_t count, loff_t *ppos)
1877 {
1878         char buf[32];
1879         struct ath10k *ar = file->private_data;
1880         int len = 0;
1881
1882         mutex_lock(&ar->conf_mutex);
1883         len = scnprintf(buf, sizeof(buf) - len, "%08x\n",
1884                         ar->debug.pktlog_filter);
1885         mutex_unlock(&ar->conf_mutex);
1886
1887         return simple_read_from_buffer(ubuf, count, ppos, buf, len);
1888 }
1889
1890 static const struct file_operations fops_pktlog_filter = {
1891         .read = ath10k_read_pktlog_filter,
1892         .write = ath10k_write_pktlog_filter,
1893         .open = simple_open
1894 };
1895
1896 int ath10k_debug_create(struct ath10k *ar)
1897 {
1898         ar->debug.fw_crash_data = vzalloc(sizeof(*ar->debug.fw_crash_data));
1899         if (!ar->debug.fw_crash_data)
1900                 return -ENOMEM;
1901
1902         INIT_LIST_HEAD(&ar->debug.fw_stats.pdevs);
1903         INIT_LIST_HEAD(&ar->debug.fw_stats.peers);
1904
1905         return 0;
1906 }
1907
1908 void ath10k_debug_destroy(struct ath10k *ar)
1909 {
1910         vfree(ar->debug.fw_crash_data);
1911         ar->debug.fw_crash_data = NULL;
1912
1913         ath10k_debug_fw_stats_reset(ar);
1914 }
1915
1916 int ath10k_debug_register(struct ath10k *ar)
1917 {
1918         ar->debug.debugfs_phy = debugfs_create_dir("ath10k",
1919                                                    ar->hw->wiphy->debugfsdir);
1920         if (IS_ERR_OR_NULL(ar->debug.debugfs_phy)) {
1921                 if (IS_ERR(ar->debug.debugfs_phy))
1922                         return PTR_ERR(ar->debug.debugfs_phy);
1923
1924                 return -ENOMEM;
1925         }
1926
1927         INIT_DELAYED_WORK(&ar->debug.htt_stats_dwork,
1928                           ath10k_debug_htt_stats_dwork);
1929
1930         init_completion(&ar->debug.fw_stats_complete);
1931
1932         debugfs_create_file("fw_stats", S_IRUSR, ar->debug.debugfs_phy, ar,
1933                             &fops_fw_stats);
1934
1935         debugfs_create_file("fw_reset_stats", S_IRUSR, ar->debug.debugfs_phy,
1936                             ar, &fops_fw_reset_stats);
1937
1938         debugfs_create_file("wmi_services", S_IRUSR, ar->debug.debugfs_phy, ar,
1939                             &fops_wmi_services);
1940
1941         debugfs_create_file("simulate_fw_crash", S_IRUSR, ar->debug.debugfs_phy,
1942                             ar, &fops_simulate_fw_crash);
1943
1944         debugfs_create_file("fw_crash_dump", S_IRUSR, ar->debug.debugfs_phy,
1945                             ar, &fops_fw_crash_dump);
1946
1947         debugfs_create_file("reg_addr", S_IRUSR | S_IWUSR,
1948                             ar->debug.debugfs_phy, ar, &fops_reg_addr);
1949
1950         debugfs_create_file("reg_value", S_IRUSR | S_IWUSR,
1951                             ar->debug.debugfs_phy, ar, &fops_reg_value);
1952
1953         debugfs_create_file("mem_value", S_IRUSR | S_IWUSR,
1954                             ar->debug.debugfs_phy, ar, &fops_mem_value);
1955
1956         debugfs_create_file("chip_id", S_IRUSR, ar->debug.debugfs_phy,
1957                             ar, &fops_chip_id);
1958
1959         debugfs_create_file("htt_stats_mask", S_IRUSR, ar->debug.debugfs_phy,
1960                             ar, &fops_htt_stats_mask);
1961
1962         debugfs_create_file("htt_max_amsdu_ampdu", S_IRUSR | S_IWUSR,
1963                             ar->debug.debugfs_phy, ar,
1964                             &fops_htt_max_amsdu_ampdu);
1965
1966         debugfs_create_file("fw_dbglog", S_IRUSR, ar->debug.debugfs_phy,
1967                             ar, &fops_fw_dbglog);
1968
1969         debugfs_create_file("cal_data", S_IRUSR, ar->debug.debugfs_phy,
1970                             ar, &fops_cal_data);
1971
1972         debugfs_create_file("nf_cal_period", S_IRUSR | S_IWUSR,
1973                             ar->debug.debugfs_phy, ar, &fops_nf_cal_period);
1974
1975         if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
1976                 debugfs_create_file("dfs_simulate_radar", S_IWUSR,
1977                                     ar->debug.debugfs_phy, ar,
1978                                     &fops_simulate_radar);
1979
1980                 debugfs_create_bool("dfs_block_radar_events", S_IWUSR,
1981                                     ar->debug.debugfs_phy,
1982                                     &ar->dfs_block_radar_events);
1983
1984                 debugfs_create_file("dfs_stats", S_IRUSR,
1985                                     ar->debug.debugfs_phy, ar,
1986                                     &fops_dfs_stats);
1987         }
1988
1989         debugfs_create_file("pktlog_filter", S_IRUGO | S_IWUSR,
1990                             ar->debug.debugfs_phy, ar, &fops_pktlog_filter);
1991
1992         return 0;
1993 }
1994
1995 void ath10k_debug_unregister(struct ath10k *ar)
1996 {
1997         cancel_delayed_work_sync(&ar->debug.htt_stats_dwork);
1998 }
1999
2000 #endif /* CONFIG_ATH10K_DEBUGFS */
2001
2002 #ifdef CONFIG_ATH10K_DEBUG
2003 void ath10k_dbg(struct ath10k *ar, enum ath10k_debug_mask mask,
2004                 const char *fmt, ...)
2005 {
2006         struct va_format vaf;
2007         va_list args;
2008
2009         va_start(args, fmt);
2010
2011         vaf.fmt = fmt;
2012         vaf.va = &args;
2013
2014         if (ath10k_debug_mask & mask)
2015                 dev_printk(KERN_DEBUG, ar->dev, "%pV", &vaf);
2016
2017         trace_ath10k_log_dbg(ar, mask, &vaf);
2018
2019         va_end(args);
2020 }
2021 EXPORT_SYMBOL(ath10k_dbg);
2022
2023 void ath10k_dbg_dump(struct ath10k *ar,
2024                      enum ath10k_debug_mask mask,
2025                      const char *msg, const char *prefix,
2026                      const void *buf, size_t len)
2027 {
2028         char linebuf[256];
2029         unsigned int linebuflen;
2030         const void *ptr;
2031
2032         if (ath10k_debug_mask & mask) {
2033                 if (msg)
2034                         ath10k_dbg(ar, mask, "%s\n", msg);
2035
2036                 for (ptr = buf; (ptr - buf) < len; ptr += 16) {
2037                         linebuflen = 0;
2038                         linebuflen += scnprintf(linebuf + linebuflen,
2039                                                 sizeof(linebuf) - linebuflen,
2040                                                 "%s%08x: ",
2041                                                 (prefix ? prefix : ""),
2042                                                 (unsigned int)(ptr - buf));
2043                         hex_dump_to_buffer(ptr, len - (ptr - buf), 16, 1,
2044                                            linebuf + linebuflen,
2045                                            sizeof(linebuf) - linebuflen, true);
2046                         dev_printk(KERN_DEBUG, ar->dev, "%s\n", linebuf);
2047                 }
2048         }
2049
2050         /* tracing code doesn't like null strings :/ */
2051         trace_ath10k_log_dbg_dump(ar, msg ? msg : "", prefix ? prefix : "",
2052                                   buf, len);
2053 }
2054 EXPORT_SYMBOL(ath10k_dbg_dump);
2055
2056 #endif /* CONFIG_ATH10K_DEBUG */