1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2020 The Linux Foundation. All rights reserved.
4  */
5 
6 #include <linux/vmalloc.h>
7 
8 #include "debugfs.h"
9 
10 #include "core.h"
11 #include "debug.h"
12 #include "wmi.h"
13 #include "hal_rx.h"
14 #include "dp_tx.h"
15 #include "debugfs_htt_stats.h"
16 #include "peer.h"
17 
18 static const char *htt_bp_umac_ring[HTT_SW_UMAC_RING_IDX_MAX] = {
19 	"REO2SW1_RING",
20 	"REO2SW2_RING",
21 	"REO2SW3_RING",
22 	"REO2SW4_RING",
23 	"WBM2REO_LINK_RING",
24 	"REO2TCL_RING",
25 	"REO2FW_RING",
26 	"RELEASE_RING",
27 	"PPE_RELEASE_RING",
28 	"TCL2TQM_RING",
29 	"TQM_RELEASE_RING",
30 	"REO_RELEASE_RING",
31 	"WBM2SW0_RELEASE_RING",
32 	"WBM2SW1_RELEASE_RING",
33 	"WBM2SW2_RELEASE_RING",
34 	"WBM2SW3_RELEASE_RING",
35 	"REO_CMD_RING",
36 	"REO_STATUS_RING",
37 };
38 
39 static const char *htt_bp_lmac_ring[HTT_SW_LMAC_RING_IDX_MAX] = {
40 	"FW2RXDMA_BUF_RING",
41 	"FW2RXDMA_STATUS_RING",
42 	"FW2RXDMA_LINK_RING",
43 	"SW2RXDMA_BUF_RING",
44 	"WBM2RXDMA_LINK_RING",
45 	"RXDMA2FW_RING",
46 	"RXDMA2SW_RING",
47 	"RXDMA2RELEASE_RING",
48 	"RXDMA2REO_RING",
49 	"MONITOR_STATUS_RING",
50 	"MONITOR_BUF_RING",
51 	"MONITOR_DESC_RING",
52 	"MONITOR_DEST_RING",
53 };
54 
55 static void ath11k_fw_stats_pdevs_free(struct list_head *head)
56 {
57 	struct ath11k_fw_stats_pdev *i, *tmp;
58 
59 	list_for_each_entry_safe(i, tmp, head, list) {
60 		list_del(&i->list);
61 		kfree(i);
62 	}
63 }
64 
65 static void ath11k_fw_stats_vdevs_free(struct list_head *head)
66 {
67 	struct ath11k_fw_stats_vdev *i, *tmp;
68 
69 	list_for_each_entry_safe(i, tmp, head, list) {
70 		list_del(&i->list);
71 		kfree(i);
72 	}
73 }
74 
75 static void ath11k_fw_stats_bcn_free(struct list_head *head)
76 {
77 	struct ath11k_fw_stats_bcn *i, *tmp;
78 
79 	list_for_each_entry_safe(i, tmp, head, list) {
80 		list_del(&i->list);
81 		kfree(i);
82 	}
83 }
84 
85 static void ath11k_debugfs_fw_stats_reset(struct ath11k *ar)
86 {
87 	spin_lock_bh(&ar->data_lock);
88 	ar->debug.fw_stats_done = false;
89 	ath11k_fw_stats_pdevs_free(&ar->debug.fw_stats.pdevs);
90 	ath11k_fw_stats_vdevs_free(&ar->debug.fw_stats.vdevs);
91 	spin_unlock_bh(&ar->data_lock);
92 }
93 
94 void ath11k_debugfs_fw_stats_process(struct ath11k_base *ab, struct sk_buff *skb)
95 {
96 	struct ath11k_fw_stats stats = {};
97 	struct ath11k *ar;
98 	struct ath11k_pdev *pdev;
99 	bool is_end;
100 	static unsigned int num_vdev, num_bcn;
101 	size_t total_vdevs_started = 0;
102 	int i, ret;
103 
104 	INIT_LIST_HEAD(&stats.pdevs);
105 	INIT_LIST_HEAD(&stats.vdevs);
106 	INIT_LIST_HEAD(&stats.bcn);
107 
108 	ret = ath11k_wmi_pull_fw_stats(ab, skb, &stats);
109 	if (ret) {
110 		ath11k_warn(ab, "failed to pull fw stats: %d\n", ret);
111 		goto free;
112 	}
113 
114 	rcu_read_lock();
115 	ar = ath11k_mac_get_ar_by_pdev_id(ab, stats.pdev_id);
116 	if (!ar) {
117 		rcu_read_unlock();
118 		ath11k_warn(ab, "failed to get ar for pdev_id %d: %d\n",
119 			    stats.pdev_id, ret);
120 		goto free;
121 	}
122 
123 	spin_lock_bh(&ar->data_lock);
124 
125 	if (stats.stats_id == WMI_REQUEST_PDEV_STAT) {
126 		list_splice_tail_init(&stats.pdevs, &ar->debug.fw_stats.pdevs);
127 		ar->debug.fw_stats_done = true;
128 		goto complete;
129 	}
130 
131 	if (stats.stats_id == WMI_REQUEST_RSSI_PER_CHAIN_STAT) {
132 		ar->debug.fw_stats_done = true;
133 		goto complete;
134 	}
135 
136 	if (stats.stats_id == WMI_REQUEST_VDEV_STAT) {
137 		if (list_empty(&stats.vdevs)) {
138 			ath11k_warn(ab, "empty vdev stats");
139 			goto complete;
140 		}
141 		/* FW sends all the active VDEV stats irrespective of PDEV,
142 		 * hence limit until the count of all VDEVs started
143 		 */
144 		for (i = 0; i < ab->num_radios; i++) {
145 			pdev = rcu_dereference(ab->pdevs_active[i]);
146 			if (pdev && pdev->ar)
147 				total_vdevs_started += ar->num_started_vdevs;
148 		}
149 
150 		is_end = ((++num_vdev) == total_vdevs_started);
151 
152 		list_splice_tail_init(&stats.vdevs,
153 				      &ar->debug.fw_stats.vdevs);
154 
155 		if (is_end) {
156 			ar->debug.fw_stats_done = true;
157 			num_vdev = 0;
158 		}
159 		goto complete;
160 	}
161 
162 	if (stats.stats_id == WMI_REQUEST_BCN_STAT) {
163 		if (list_empty(&stats.bcn)) {
164 			ath11k_warn(ab, "empty bcn stats");
165 			goto complete;
166 		}
167 		/* Mark end until we reached the count of all started VDEVs
168 		 * within the PDEV
169 		 */
170 		is_end = ((++num_bcn) == ar->num_started_vdevs);
171 
172 		list_splice_tail_init(&stats.bcn,
173 				      &ar->debug.fw_stats.bcn);
174 
175 		if (is_end) {
176 			ar->debug.fw_stats_done = true;
177 			num_bcn = 0;
178 		}
179 	}
180 complete:
181 	complete(&ar->debug.fw_stats_complete);
182 	rcu_read_unlock();
183 	spin_unlock_bh(&ar->data_lock);
184 
185 free:
186 	ath11k_fw_stats_pdevs_free(&stats.pdevs);
187 	ath11k_fw_stats_vdevs_free(&stats.vdevs);
188 	ath11k_fw_stats_bcn_free(&stats.bcn);
189 }
190 
191 static int ath11k_debugfs_fw_stats_request(struct ath11k *ar,
192 					   struct stats_request_params *req_param)
193 {
194 	struct ath11k_base *ab = ar->ab;
195 	unsigned long timeout, time_left;
196 	int ret;
197 
198 	lockdep_assert_held(&ar->conf_mutex);
199 
200 	/* FW stats can get split when exceeding the stats data buffer limit.
201 	 * In that case, since there is no end marking for the back-to-back
202 	 * received 'update stats' event, we keep a 3 seconds timeout in case,
203 	 * fw_stats_done is not marked yet
204 	 */
205 	timeout = jiffies + msecs_to_jiffies(3 * 1000);
206 
207 	ath11k_debugfs_fw_stats_reset(ar);
208 
209 	reinit_completion(&ar->debug.fw_stats_complete);
210 
211 	ret = ath11k_wmi_send_stats_request_cmd(ar, req_param);
212 
213 	if (ret) {
214 		ath11k_warn(ab, "could not request fw stats (%d)\n",
215 			    ret);
216 		return ret;
217 	}
218 
219 	time_left =
220 	wait_for_completion_timeout(&ar->debug.fw_stats_complete,
221 				    1 * HZ);
222 	if (!time_left)
223 		return -ETIMEDOUT;
224 
225 	for (;;) {
226 		if (time_after(jiffies, timeout))
227 			break;
228 
229 		spin_lock_bh(&ar->data_lock);
230 		if (ar->debug.fw_stats_done) {
231 			spin_unlock_bh(&ar->data_lock);
232 			break;
233 		}
234 		spin_unlock_bh(&ar->data_lock);
235 	}
236 	return 0;
237 }
238 
239 int ath11k_debugfs_get_fw_stats(struct ath11k *ar, u32 pdev_id,
240 				u32 vdev_id, u32 stats_id)
241 {
242 	struct ath11k_base *ab = ar->ab;
243 	struct stats_request_params req_param;
244 	int ret;
245 
246 	mutex_lock(&ar->conf_mutex);
247 
248 	if (ar->state != ATH11K_STATE_ON) {
249 		ret = -ENETDOWN;
250 		goto err_unlock;
251 	}
252 
253 	req_param.pdev_id = pdev_id;
254 	req_param.vdev_id = vdev_id;
255 	req_param.stats_id = stats_id;
256 
257 	ret = ath11k_debugfs_fw_stats_request(ar, &req_param);
258 	if (ret)
259 		ath11k_warn(ab, "failed to request fw stats: %d\n", ret);
260 
261 	ath11k_dbg(ab, ATH11K_DBG_WMI,
262 		   "debug get fw stat pdev id %d vdev id %d stats id 0x%x\n",
263 		   pdev_id, vdev_id, stats_id);
264 
265 err_unlock:
266 	mutex_unlock(&ar->conf_mutex);
267 
268 	return ret;
269 }
270 
271 static int ath11k_open_pdev_stats(struct inode *inode, struct file *file)
272 {
273 	struct ath11k *ar = inode->i_private;
274 	struct ath11k_base *ab = ar->ab;
275 	struct stats_request_params req_param;
276 	void *buf = NULL;
277 	int ret;
278 
279 	mutex_lock(&ar->conf_mutex);
280 
281 	if (ar->state != ATH11K_STATE_ON) {
282 		ret = -ENETDOWN;
283 		goto err_unlock;
284 	}
285 
286 	buf = vmalloc(ATH11K_FW_STATS_BUF_SIZE);
287 	if (!buf) {
288 		ret = -ENOMEM;
289 		goto err_unlock;
290 	}
291 
292 	req_param.pdev_id = ar->pdev->pdev_id;
293 	req_param.vdev_id = 0;
294 	req_param.stats_id = WMI_REQUEST_PDEV_STAT;
295 
296 	ret = ath11k_debugfs_fw_stats_request(ar, &req_param);
297 	if (ret) {
298 		ath11k_warn(ab, "failed to request fw pdev stats: %d\n", ret);
299 		goto err_free;
300 	}
301 
302 	ath11k_wmi_fw_stats_fill(ar, &ar->debug.fw_stats, req_param.stats_id,
303 				 buf);
304 
305 	file->private_data = buf;
306 
307 	mutex_unlock(&ar->conf_mutex);
308 	return 0;
309 
310 err_free:
311 	vfree(buf);
312 
313 err_unlock:
314 	mutex_unlock(&ar->conf_mutex);
315 	return ret;
316 }
317 
318 static int ath11k_release_pdev_stats(struct inode *inode, struct file *file)
319 {
320 	vfree(file->private_data);
321 
322 	return 0;
323 }
324 
325 static ssize_t ath11k_read_pdev_stats(struct file *file,
326 				      char __user *user_buf,
327 				      size_t count, loff_t *ppos)
328 {
329 	const char *buf = file->private_data;
330 	size_t len = strlen(buf);
331 
332 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
333 }
334 
335 static const struct file_operations fops_pdev_stats = {
336 	.open = ath11k_open_pdev_stats,
337 	.release = ath11k_release_pdev_stats,
338 	.read = ath11k_read_pdev_stats,
339 	.owner = THIS_MODULE,
340 	.llseek = default_llseek,
341 };
342 
343 static int ath11k_open_vdev_stats(struct inode *inode, struct file *file)
344 {
345 	struct ath11k *ar = inode->i_private;
346 	struct stats_request_params req_param;
347 	void *buf = NULL;
348 	int ret;
349 
350 	mutex_lock(&ar->conf_mutex);
351 
352 	if (ar->state != ATH11K_STATE_ON) {
353 		ret = -ENETDOWN;
354 		goto err_unlock;
355 	}
356 
357 	buf = vmalloc(ATH11K_FW_STATS_BUF_SIZE);
358 	if (!buf) {
359 		ret = -ENOMEM;
360 		goto err_unlock;
361 	}
362 
363 	req_param.pdev_id = ar->pdev->pdev_id;
364 	/* VDEV stats is always sent for all active VDEVs from FW */
365 	req_param.vdev_id = 0;
366 	req_param.stats_id = WMI_REQUEST_VDEV_STAT;
367 
368 	ret = ath11k_debugfs_fw_stats_request(ar, &req_param);
369 	if (ret) {
370 		ath11k_warn(ar->ab, "failed to request fw vdev stats: %d\n", ret);
371 		goto err_free;
372 	}
373 
374 	ath11k_wmi_fw_stats_fill(ar, &ar->debug.fw_stats, req_param.stats_id,
375 				 buf);
376 
377 	file->private_data = buf;
378 
379 	mutex_unlock(&ar->conf_mutex);
380 	return 0;
381 
382 err_free:
383 	vfree(buf);
384 
385 err_unlock:
386 	mutex_unlock(&ar->conf_mutex);
387 	return ret;
388 }
389 
390 static int ath11k_release_vdev_stats(struct inode *inode, struct file *file)
391 {
392 	vfree(file->private_data);
393 
394 	return 0;
395 }
396 
397 static ssize_t ath11k_read_vdev_stats(struct file *file,
398 				      char __user *user_buf,
399 				      size_t count, loff_t *ppos)
400 {
401 	const char *buf = file->private_data;
402 	size_t len = strlen(buf);
403 
404 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
405 }
406 
407 static const struct file_operations fops_vdev_stats = {
408 	.open = ath11k_open_vdev_stats,
409 	.release = ath11k_release_vdev_stats,
410 	.read = ath11k_read_vdev_stats,
411 	.owner = THIS_MODULE,
412 	.llseek = default_llseek,
413 };
414 
415 static int ath11k_open_bcn_stats(struct inode *inode, struct file *file)
416 {
417 	struct ath11k *ar = inode->i_private;
418 	struct ath11k_vif *arvif;
419 	struct stats_request_params req_param;
420 	void *buf = NULL;
421 	int ret;
422 
423 	mutex_lock(&ar->conf_mutex);
424 
425 	if (ar->state != ATH11K_STATE_ON) {
426 		ret = -ENETDOWN;
427 		goto err_unlock;
428 	}
429 
430 	buf = vmalloc(ATH11K_FW_STATS_BUF_SIZE);
431 	if (!buf) {
432 		ret = -ENOMEM;
433 		goto err_unlock;
434 	}
435 
436 	req_param.stats_id = WMI_REQUEST_BCN_STAT;
437 	req_param.pdev_id = ar->pdev->pdev_id;
438 
439 	/* loop all active VDEVs for bcn stats */
440 	list_for_each_entry(arvif, &ar->arvifs, list) {
441 		if (!arvif->is_up)
442 			continue;
443 
444 		req_param.vdev_id = arvif->vdev_id;
445 		ret = ath11k_debugfs_fw_stats_request(ar, &req_param);
446 		if (ret) {
447 			ath11k_warn(ar->ab, "failed to request fw bcn stats: %d\n", ret);
448 			goto err_free;
449 		}
450 	}
451 
452 	ath11k_wmi_fw_stats_fill(ar, &ar->debug.fw_stats, req_param.stats_id,
453 				 buf);
454 
455 	/* since beacon stats request is looped for all active VDEVs, saved fw
456 	 * stats is not freed for each request until done for all active VDEVs
457 	 */
458 	spin_lock_bh(&ar->data_lock);
459 	ath11k_fw_stats_bcn_free(&ar->debug.fw_stats.bcn);
460 	spin_unlock_bh(&ar->data_lock);
461 
462 	file->private_data = buf;
463 
464 	mutex_unlock(&ar->conf_mutex);
465 	return 0;
466 
467 err_free:
468 	vfree(buf);
469 
470 err_unlock:
471 	mutex_unlock(&ar->conf_mutex);
472 	return ret;
473 }
474 
475 static int ath11k_release_bcn_stats(struct inode *inode, struct file *file)
476 {
477 	vfree(file->private_data);
478 
479 	return 0;
480 }
481 
482 static ssize_t ath11k_read_bcn_stats(struct file *file,
483 				     char __user *user_buf,
484 				     size_t count, loff_t *ppos)
485 {
486 	const char *buf = file->private_data;
487 	size_t len = strlen(buf);
488 
489 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
490 }
491 
492 static const struct file_operations fops_bcn_stats = {
493 	.open = ath11k_open_bcn_stats,
494 	.release = ath11k_release_bcn_stats,
495 	.read = ath11k_read_bcn_stats,
496 	.owner = THIS_MODULE,
497 	.llseek = default_llseek,
498 };
499 
500 static ssize_t ath11k_read_simulate_fw_crash(struct file *file,
501 					     char __user *user_buf,
502 					     size_t count, loff_t *ppos)
503 {
504 	const char buf[] =
505 		"To simulate firmware crash write one of the keywords to this file:\n"
506 		"`assert` - this will send WMI_FORCE_FW_HANG_CMDID to firmware to cause assert.\n"
507 		"`hw-restart` - this will simply queue hw restart without fw/hw actually crashing.\n";
508 
509 	return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
510 }
511 
512 /* Simulate firmware crash:
513  * 'soft': Call wmi command causing firmware hang. This firmware hang is
514  * recoverable by warm firmware reset.
515  * 'hard': Force firmware crash by setting any vdev parameter for not allowed
516  * vdev id. This is hard firmware crash because it is recoverable only by cold
517  * firmware reset.
518  */
519 static ssize_t ath11k_write_simulate_fw_crash(struct file *file,
520 					      const char __user *user_buf,
521 					      size_t count, loff_t *ppos)
522 {
523 	struct ath11k_base *ab = file->private_data;
524 	struct ath11k_pdev *pdev;
525 	struct ath11k *ar = ab->pdevs[0].ar;
526 	char buf[32] = {0};
527 	ssize_t rc;
528 	int i, ret, radioup = 0;
529 
530 	for (i = 0; i < ab->num_radios; i++) {
531 		pdev = &ab->pdevs[i];
532 		ar = pdev->ar;
533 		if (ar && ar->state == ATH11K_STATE_ON) {
534 			radioup = 1;
535 			break;
536 		}
537 	}
538 	/* filter partial writes and invalid commands */
539 	if (*ppos != 0 || count >= sizeof(buf) || count == 0)
540 		return -EINVAL;
541 
542 	rc = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, user_buf, count);
543 	if (rc < 0)
544 		return rc;
545 
546 	/* drop the possible '\n' from the end */
547 	if (buf[*ppos - 1] == '\n')
548 		buf[*ppos - 1] = '\0';
549 
550 	if (radioup == 0) {
551 		ret = -ENETDOWN;
552 		goto exit;
553 	}
554 
555 	if (!strcmp(buf, "assert")) {
556 		ath11k_info(ab, "simulating firmware assert crash\n");
557 		ret = ath11k_wmi_force_fw_hang_cmd(ar,
558 						   ATH11K_WMI_FW_HANG_ASSERT_TYPE,
559 						   ATH11K_WMI_FW_HANG_DELAY);
560 	} else {
561 		ret = -EINVAL;
562 		goto exit;
563 	}
564 
565 	if (ret) {
566 		ath11k_warn(ab, "failed to simulate firmware crash: %d\n", ret);
567 		goto exit;
568 	}
569 
570 	ret = count;
571 
572 exit:
573 	return ret;
574 }
575 
576 static const struct file_operations fops_simulate_fw_crash = {
577 	.read = ath11k_read_simulate_fw_crash,
578 	.write = ath11k_write_simulate_fw_crash,
579 	.open = simple_open,
580 	.owner = THIS_MODULE,
581 	.llseek = default_llseek,
582 };
583 
584 static ssize_t ath11k_write_enable_extd_tx_stats(struct file *file,
585 						 const char __user *ubuf,
586 						 size_t count, loff_t *ppos)
587 {
588 	struct ath11k *ar = file->private_data;
589 	u32 filter;
590 	int ret;
591 
592 	if (kstrtouint_from_user(ubuf, count, 0, &filter))
593 		return -EINVAL;
594 
595 	mutex_lock(&ar->conf_mutex);
596 
597 	if (ar->state != ATH11K_STATE_ON) {
598 		ret = -ENETDOWN;
599 		goto out;
600 	}
601 
602 	if (filter == ar->debug.extd_tx_stats) {
603 		ret = count;
604 		goto out;
605 	}
606 
607 	ar->debug.extd_tx_stats = filter;
608 	ret = count;
609 
610 out:
611 	mutex_unlock(&ar->conf_mutex);
612 	return ret;
613 }
614 
615 static ssize_t ath11k_read_enable_extd_tx_stats(struct file *file,
616 						char __user *ubuf,
617 						size_t count, loff_t *ppos)
618 
619 {
620 	char buf[32] = {0};
621 	struct ath11k *ar = file->private_data;
622 	int len = 0;
623 
624 	mutex_lock(&ar->conf_mutex);
625 	len = scnprintf(buf, sizeof(buf) - len, "%08x\n",
626 			ar->debug.extd_tx_stats);
627 	mutex_unlock(&ar->conf_mutex);
628 
629 	return simple_read_from_buffer(ubuf, count, ppos, buf, len);
630 }
631 
632 static const struct file_operations fops_extd_tx_stats = {
633 	.read = ath11k_read_enable_extd_tx_stats,
634 	.write = ath11k_write_enable_extd_tx_stats,
635 	.open = simple_open
636 };
637 
638 static ssize_t ath11k_write_extd_rx_stats(struct file *file,
639 					  const char __user *ubuf,
640 					  size_t count, loff_t *ppos)
641 {
642 	struct ath11k *ar = file->private_data;
643 	struct ath11k_base *ab = ar->ab;
644 	struct htt_rx_ring_tlv_filter tlv_filter = {0};
645 	u32 enable, rx_filter = 0, ring_id;
646 	int i;
647 	int ret;
648 
649 	if (kstrtouint_from_user(ubuf, count, 0, &enable))
650 		return -EINVAL;
651 
652 	mutex_lock(&ar->conf_mutex);
653 
654 	if (ar->state != ATH11K_STATE_ON) {
655 		ret = -ENETDOWN;
656 		goto exit;
657 	}
658 
659 	if (enable > 1) {
660 		ret = -EINVAL;
661 		goto exit;
662 	}
663 
664 	if (enable == ar->debug.extd_rx_stats) {
665 		ret = count;
666 		goto exit;
667 	}
668 
669 	if (enable) {
670 		rx_filter =  HTT_RX_FILTER_TLV_FLAGS_MPDU_START;
671 		rx_filter |= HTT_RX_FILTER_TLV_FLAGS_PPDU_START;
672 		rx_filter |= HTT_RX_FILTER_TLV_FLAGS_PPDU_END;
673 		rx_filter |= HTT_RX_FILTER_TLV_FLAGS_PPDU_END_USER_STATS;
674 		rx_filter |= HTT_RX_FILTER_TLV_FLAGS_PPDU_END_USER_STATS_EXT;
675 		rx_filter |= HTT_RX_FILTER_TLV_FLAGS_PPDU_END_STATUS_DONE;
676 
677 		tlv_filter.rx_filter = rx_filter;
678 		tlv_filter.pkt_filter_flags0 = HTT_RX_FP_MGMT_FILTER_FLAGS0;
679 		tlv_filter.pkt_filter_flags1 = HTT_RX_FP_MGMT_FILTER_FLAGS1;
680 		tlv_filter.pkt_filter_flags2 = HTT_RX_FP_CTRL_FILTER_FLASG2;
681 		tlv_filter.pkt_filter_flags3 = HTT_RX_FP_CTRL_FILTER_FLASG3 |
682 			HTT_RX_FP_DATA_FILTER_FLASG3;
683 	} else {
684 		tlv_filter = ath11k_mac_mon_status_filter_default;
685 	}
686 
687 	ar->debug.rx_filter = tlv_filter.rx_filter;
688 
689 	for (i = 0; i < ab->hw_params.num_rxmda_per_pdev; i++) {
690 		ring_id = ar->dp.rx_mon_status_refill_ring[i].refill_buf_ring.ring_id;
691 		ret = ath11k_dp_tx_htt_rx_filter_setup(ar->ab, ring_id, ar->dp.mac_id,
692 						       HAL_RXDMA_MONITOR_STATUS,
693 						       DP_RX_BUFFER_SIZE, &tlv_filter);
694 
695 		if (ret) {
696 			ath11k_warn(ar->ab, "failed to set rx filter for monitor status ring\n");
697 			goto exit;
698 		}
699 	}
700 
701 	ar->debug.extd_rx_stats = enable;
702 	ret = count;
703 exit:
704 	mutex_unlock(&ar->conf_mutex);
705 	return ret;
706 }
707 
708 static ssize_t ath11k_read_extd_rx_stats(struct file *file,
709 					 char __user *ubuf,
710 					 size_t count, loff_t *ppos)
711 {
712 	struct ath11k *ar = file->private_data;
713 	char buf[32];
714 	int len = 0;
715 
716 	mutex_lock(&ar->conf_mutex);
717 	len = scnprintf(buf, sizeof(buf) - len, "%d\n",
718 			ar->debug.extd_rx_stats);
719 	mutex_unlock(&ar->conf_mutex);
720 
721 	return simple_read_from_buffer(ubuf, count, ppos, buf, len);
722 }
723 
724 static const struct file_operations fops_extd_rx_stats = {
725 	.read = ath11k_read_extd_rx_stats,
726 	.write = ath11k_write_extd_rx_stats,
727 	.open = simple_open,
728 };
729 
730 static int ath11k_fill_bp_stats(struct ath11k_base *ab,
731 				struct ath11k_bp_stats *bp_stats,
732 				char *buf, int len, int size)
733 {
734 	lockdep_assert_held(&ab->base_lock);
735 
736 	len += scnprintf(buf + len, size - len, "count: %u\n",
737 			 bp_stats->count);
738 	len += scnprintf(buf + len, size - len, "hp: %u\n",
739 			 bp_stats->hp);
740 	len += scnprintf(buf + len, size - len, "tp: %u\n",
741 			 bp_stats->tp);
742 	len += scnprintf(buf + len, size - len, "seen before: %ums\n\n",
743 			 jiffies_to_msecs(jiffies - bp_stats->jiffies));
744 	return len;
745 }
746 
747 static ssize_t ath11k_debugfs_dump_soc_ring_bp_stats(struct ath11k_base *ab,
748 						     char *buf, int size)
749 {
750 	struct ath11k_bp_stats *bp_stats;
751 	bool stats_rxd = false;
752 	u8 i, pdev_idx;
753 	int len = 0;
754 
755 	len += scnprintf(buf + len, size - len, "\nBackpressure Stats\n");
756 	len += scnprintf(buf + len, size - len, "==================\n");
757 
758 	spin_lock_bh(&ab->base_lock);
759 	for (i = 0; i < HTT_SW_UMAC_RING_IDX_MAX; i++) {
760 		bp_stats = &ab->soc_stats.bp_stats.umac_ring_bp_stats[i];
761 
762 		if (!bp_stats->count)
763 			continue;
764 
765 		len += scnprintf(buf + len, size - len, "Ring: %s\n",
766 				 htt_bp_umac_ring[i]);
767 		len = ath11k_fill_bp_stats(ab, bp_stats, buf, len, size);
768 		stats_rxd = true;
769 	}
770 
771 	for (i = 0; i < HTT_SW_LMAC_RING_IDX_MAX; i++) {
772 		for (pdev_idx = 0; pdev_idx < MAX_RADIOS; pdev_idx++) {
773 			bp_stats =
774 				&ab->soc_stats.bp_stats.lmac_ring_bp_stats[i][pdev_idx];
775 
776 			if (!bp_stats->count)
777 				continue;
778 
779 			len += scnprintf(buf + len, size - len, "Ring: %s\n",
780 					 htt_bp_lmac_ring[i]);
781 			len += scnprintf(buf + len, size - len, "pdev: %d\n",
782 					 pdev_idx);
783 			len = ath11k_fill_bp_stats(ab, bp_stats, buf, len, size);
784 			stats_rxd = true;
785 		}
786 	}
787 	spin_unlock_bh(&ab->base_lock);
788 
789 	if (!stats_rxd)
790 		len += scnprintf(buf + len, size - len,
791 				 "No Ring Backpressure stats received\n\n");
792 
793 	return len;
794 }
795 
796 static ssize_t ath11k_debugfs_dump_soc_dp_stats(struct file *file,
797 						char __user *user_buf,
798 						size_t count, loff_t *ppos)
799 {
800 	struct ath11k_base *ab = file->private_data;
801 	struct ath11k_soc_dp_stats *soc_stats = &ab->soc_stats;
802 	int len = 0, i, retval;
803 	const int size = 4096;
804 	static const char *rxdma_err[HAL_REO_ENTR_RING_RXDMA_ECODE_MAX] = {
805 			"Overflow", "MPDU len", "FCS", "Decrypt", "TKIP MIC",
806 			"Unencrypt", "MSDU len", "MSDU limit", "WiFi parse",
807 			"AMSDU parse", "SA timeout", "DA timeout",
808 			"Flow timeout", "Flush req"};
809 	static const char *reo_err[HAL_REO_DEST_RING_ERROR_CODE_MAX] = {
810 			"Desc addr zero", "Desc inval", "AMPDU in non BA",
811 			"Non BA dup", "BA dup", "Frame 2k jump", "BAR 2k jump",
812 			"Frame OOR", "BAR OOR", "No BA session",
813 			"Frame SN equal SSN", "PN check fail", "2k err",
814 			"PN err", "Desc blocked"};
815 
816 	char *buf;
817 
818 	buf = kzalloc(size, GFP_KERNEL);
819 	if (!buf)
820 		return -ENOMEM;
821 
822 	len += scnprintf(buf + len, size - len, "SOC RX STATS:\n\n");
823 	len += scnprintf(buf + len, size - len, "err ring pkts: %u\n",
824 			 soc_stats->err_ring_pkts);
825 	len += scnprintf(buf + len, size - len, "Invalid RBM: %u\n\n",
826 			 soc_stats->invalid_rbm);
827 	len += scnprintf(buf + len, size - len, "RXDMA errors:\n");
828 	for (i = 0; i < HAL_REO_ENTR_RING_RXDMA_ECODE_MAX; i++)
829 		len += scnprintf(buf + len, size - len, "%s: %u\n",
830 				 rxdma_err[i], soc_stats->rxdma_error[i]);
831 
832 	len += scnprintf(buf + len, size - len, "\nREO errors:\n");
833 	for (i = 0; i < HAL_REO_DEST_RING_ERROR_CODE_MAX; i++)
834 		len += scnprintf(buf + len, size - len, "%s: %u\n",
835 				 reo_err[i], soc_stats->reo_error[i]);
836 
837 	len += scnprintf(buf + len, size - len, "\nHAL REO errors:\n");
838 	len += scnprintf(buf + len, size - len,
839 			 "ring0: %u\nring1: %u\nring2: %u\nring3: %u\n",
840 			 soc_stats->hal_reo_error[0],
841 			 soc_stats->hal_reo_error[1],
842 			 soc_stats->hal_reo_error[2],
843 			 soc_stats->hal_reo_error[3]);
844 
845 	len += scnprintf(buf + len, size - len, "\nSOC TX STATS:\n");
846 	len += scnprintf(buf + len, size - len, "\nTCL Ring Full Failures:\n");
847 
848 	for (i = 0; i < ab->hw_params.max_tx_ring; i++)
849 		len += scnprintf(buf + len, size - len, "ring%d: %u\n",
850 				 i, soc_stats->tx_err.desc_na[i]);
851 
852 	len += scnprintf(buf + len, size - len,
853 			 "\nMisc Transmit Failures: %d\n",
854 			 atomic_read(&soc_stats->tx_err.misc_fail));
855 
856 	len += ath11k_debugfs_dump_soc_ring_bp_stats(ab, buf + len, size - len);
857 
858 	if (len > size)
859 		len = size;
860 	retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
861 	kfree(buf);
862 
863 	return retval;
864 }
865 
866 static const struct file_operations fops_soc_dp_stats = {
867 	.read = ath11k_debugfs_dump_soc_dp_stats,
868 	.open = simple_open,
869 	.owner = THIS_MODULE,
870 	.llseek = default_llseek,
871 };
872 
873 int ath11k_debugfs_pdev_create(struct ath11k_base *ab)
874 {
875 	if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
876 		return 0;
877 
878 	ab->debugfs_soc = debugfs_create_dir(ab->hw_params.name, ab->debugfs_ath11k);
879 	if (IS_ERR(ab->debugfs_soc))
880 		return PTR_ERR(ab->debugfs_soc);
881 
882 	debugfs_create_file("simulate_fw_crash", 0600, ab->debugfs_soc, ab,
883 			    &fops_simulate_fw_crash);
884 
885 	debugfs_create_file("soc_dp_stats", 0600, ab->debugfs_soc, ab,
886 			    &fops_soc_dp_stats);
887 
888 	return 0;
889 }
890 
891 void ath11k_debugfs_pdev_destroy(struct ath11k_base *ab)
892 {
893 	debugfs_remove_recursive(ab->debugfs_soc);
894 	ab->debugfs_soc = NULL;
895 }
896 
897 int ath11k_debugfs_soc_create(struct ath11k_base *ab)
898 {
899 	ab->debugfs_ath11k = debugfs_create_dir("ath11k", NULL);
900 
901 	return PTR_ERR_OR_ZERO(ab->debugfs_ath11k);
902 }
903 
904 void ath11k_debugfs_soc_destroy(struct ath11k_base *ab)
905 {
906 	debugfs_remove_recursive(ab->debugfs_ath11k);
907 	ab->debugfs_ath11k = NULL;
908 }
909 EXPORT_SYMBOL(ath11k_debugfs_soc_destroy);
910 
911 void ath11k_debugfs_fw_stats_init(struct ath11k *ar)
912 {
913 	struct dentry *fwstats_dir = debugfs_create_dir("fw_stats",
914 							ar->debug.debugfs_pdev);
915 
916 	ar->debug.fw_stats.debugfs_fwstats = fwstats_dir;
917 
918 	/* all stats debugfs files created are under "fw_stats" directory
919 	 * created per PDEV
920 	 */
921 	debugfs_create_file("pdev_stats", 0600, fwstats_dir, ar,
922 			    &fops_pdev_stats);
923 	debugfs_create_file("vdev_stats", 0600, fwstats_dir, ar,
924 			    &fops_vdev_stats);
925 	debugfs_create_file("beacon_stats", 0600, fwstats_dir, ar,
926 			    &fops_bcn_stats);
927 
928 	INIT_LIST_HEAD(&ar->debug.fw_stats.pdevs);
929 	INIT_LIST_HEAD(&ar->debug.fw_stats.vdevs);
930 	INIT_LIST_HEAD(&ar->debug.fw_stats.bcn);
931 
932 	init_completion(&ar->debug.fw_stats_complete);
933 }
934 
935 static ssize_t ath11k_write_pktlog_filter(struct file *file,
936 					  const char __user *ubuf,
937 					  size_t count, loff_t *ppos)
938 {
939 	struct ath11k *ar = file->private_data;
940 	struct ath11k_base *ab = ar->ab;
941 	struct htt_rx_ring_tlv_filter tlv_filter = {0};
942 	u32 rx_filter = 0, ring_id, filter, mode;
943 	u8 buf[128] = {0};
944 	int i, ret, rx_buf_sz = 0;
945 	ssize_t rc;
946 
947 	mutex_lock(&ar->conf_mutex);
948 	if (ar->state != ATH11K_STATE_ON) {
949 		ret = -ENETDOWN;
950 		goto out;
951 	}
952 
953 	rc = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, ubuf, count);
954 	if (rc < 0) {
955 		ret = rc;
956 		goto out;
957 	}
958 	buf[rc] = '\0';
959 
960 	ret = sscanf(buf, "0x%x %u", &filter, &mode);
961 	if (ret != 2) {
962 		ret = -EINVAL;
963 		goto out;
964 	}
965 
966 	if (filter) {
967 		ret = ath11k_wmi_pdev_pktlog_enable(ar, filter);
968 		if (ret) {
969 			ath11k_warn(ar->ab,
970 				    "failed to enable pktlog filter %x: %d\n",
971 				    ar->debug.pktlog_filter, ret);
972 			goto out;
973 		}
974 	} else {
975 		ret = ath11k_wmi_pdev_pktlog_disable(ar);
976 		if (ret) {
977 			ath11k_warn(ar->ab, "failed to disable pktlog: %d\n", ret);
978 			goto out;
979 		}
980 	}
981 
982 	/* Clear rx filter set for monitor mode and rx status */
983 	for (i = 0; i < ab->hw_params.num_rxmda_per_pdev; i++) {
984 		ring_id = ar->dp.rx_mon_status_refill_ring[i].refill_buf_ring.ring_id;
985 		ret = ath11k_dp_tx_htt_rx_filter_setup(ar->ab, ring_id, ar->dp.mac_id,
986 						       HAL_RXDMA_MONITOR_STATUS,
987 						       rx_buf_sz, &tlv_filter);
988 		if (ret) {
989 			ath11k_warn(ar->ab, "failed to set rx filter for monitor status ring\n");
990 			goto out;
991 		}
992 	}
993 #define HTT_RX_FILTER_TLV_LITE_MODE \
994 			(HTT_RX_FILTER_TLV_FLAGS_PPDU_START | \
995 			HTT_RX_FILTER_TLV_FLAGS_PPDU_END | \
996 			HTT_RX_FILTER_TLV_FLAGS_PPDU_END_USER_STATS | \
997 			HTT_RX_FILTER_TLV_FLAGS_PPDU_END_USER_STATS_EXT | \
998 			HTT_RX_FILTER_TLV_FLAGS_PPDU_END_STATUS_DONE | \
999 			HTT_RX_FILTER_TLV_FLAGS_MPDU_START)
1000 
1001 	if (mode == ATH11K_PKTLOG_MODE_FULL) {
1002 		rx_filter = HTT_RX_FILTER_TLV_LITE_MODE |
1003 			    HTT_RX_FILTER_TLV_FLAGS_MSDU_START |
1004 			    HTT_RX_FILTER_TLV_FLAGS_MSDU_END |
1005 			    HTT_RX_FILTER_TLV_FLAGS_MPDU_END |
1006 			    HTT_RX_FILTER_TLV_FLAGS_PACKET_HEADER |
1007 			    HTT_RX_FILTER_TLV_FLAGS_ATTENTION;
1008 		rx_buf_sz = DP_RX_BUFFER_SIZE;
1009 	} else if (mode == ATH11K_PKTLOG_MODE_LITE) {
1010 		ret = ath11k_dp_tx_htt_h2t_ppdu_stats_req(ar,
1011 							  HTT_PPDU_STATS_TAG_PKTLOG);
1012 		if (ret) {
1013 			ath11k_err(ar->ab, "failed to enable pktlog lite: %d\n", ret);
1014 			goto out;
1015 		}
1016 
1017 		rx_filter = HTT_RX_FILTER_TLV_LITE_MODE;
1018 		rx_buf_sz = DP_RX_BUFFER_SIZE_LITE;
1019 	} else {
1020 		rx_buf_sz = DP_RX_BUFFER_SIZE;
1021 		tlv_filter = ath11k_mac_mon_status_filter_default;
1022 		rx_filter = tlv_filter.rx_filter;
1023 
1024 		ret = ath11k_dp_tx_htt_h2t_ppdu_stats_req(ar,
1025 							  HTT_PPDU_STATS_TAG_DEFAULT);
1026 		if (ret) {
1027 			ath11k_err(ar->ab, "failed to send htt ppdu stats req: %d\n",
1028 				   ret);
1029 			goto out;
1030 		}
1031 	}
1032 
1033 	tlv_filter.rx_filter = rx_filter;
1034 	if (rx_filter) {
1035 		tlv_filter.pkt_filter_flags0 = HTT_RX_FP_MGMT_FILTER_FLAGS0;
1036 		tlv_filter.pkt_filter_flags1 = HTT_RX_FP_MGMT_FILTER_FLAGS1;
1037 		tlv_filter.pkt_filter_flags2 = HTT_RX_FP_CTRL_FILTER_FLASG2;
1038 		tlv_filter.pkt_filter_flags3 = HTT_RX_FP_CTRL_FILTER_FLASG3 |
1039 					       HTT_RX_FP_DATA_FILTER_FLASG3;
1040 	}
1041 
1042 	for (i = 0; i < ab->hw_params.num_rxmda_per_pdev; i++) {
1043 		ring_id = ar->dp.rx_mon_status_refill_ring[i].refill_buf_ring.ring_id;
1044 		ret = ath11k_dp_tx_htt_rx_filter_setup(ab, ring_id,
1045 						       ar->dp.mac_id + i,
1046 						       HAL_RXDMA_MONITOR_STATUS,
1047 						       rx_buf_sz, &tlv_filter);
1048 
1049 		if (ret) {
1050 			ath11k_warn(ab, "failed to set rx filter for monitor status ring\n");
1051 			goto out;
1052 		}
1053 	}
1054 
1055 	ath11k_info(ab, "pktlog mode %s\n",
1056 		    ((mode == ATH11K_PKTLOG_MODE_FULL) ? "full" : "lite"));
1057 
1058 	ar->debug.pktlog_filter = filter;
1059 	ar->debug.pktlog_mode = mode;
1060 	ret = count;
1061 
1062 out:
1063 	mutex_unlock(&ar->conf_mutex);
1064 	return ret;
1065 }
1066 
1067 static ssize_t ath11k_read_pktlog_filter(struct file *file,
1068 					 char __user *ubuf,
1069 					 size_t count, loff_t *ppos)
1070 
1071 {
1072 	char buf[32] = {0};
1073 	struct ath11k *ar = file->private_data;
1074 	int len = 0;
1075 
1076 	mutex_lock(&ar->conf_mutex);
1077 	len = scnprintf(buf, sizeof(buf) - len, "%08x %08x\n",
1078 			ar->debug.pktlog_filter,
1079 			ar->debug.pktlog_mode);
1080 	mutex_unlock(&ar->conf_mutex);
1081 
1082 	return simple_read_from_buffer(ubuf, count, ppos, buf, len);
1083 }
1084 
1085 static const struct file_operations fops_pktlog_filter = {
1086 	.read = ath11k_read_pktlog_filter,
1087 	.write = ath11k_write_pktlog_filter,
1088 	.open = simple_open
1089 };
1090 
1091 static ssize_t ath11k_write_simulate_radar(struct file *file,
1092 					   const char __user *user_buf,
1093 					   size_t count, loff_t *ppos)
1094 {
1095 	struct ath11k *ar = file->private_data;
1096 	int ret;
1097 
1098 	ret = ath11k_wmi_simulate_radar(ar);
1099 	if (ret)
1100 		return ret;
1101 
1102 	return count;
1103 }
1104 
1105 static const struct file_operations fops_simulate_radar = {
1106 	.write = ath11k_write_simulate_radar,
1107 	.open = simple_open
1108 };
1109 
1110 int ath11k_debugfs_register(struct ath11k *ar)
1111 {
1112 	struct ath11k_base *ab = ar->ab;
1113 	char pdev_name[5];
1114 	char buf[100] = {0};
1115 
1116 	snprintf(pdev_name, sizeof(pdev_name), "%s%d", "mac", ar->pdev_idx);
1117 
1118 	ar->debug.debugfs_pdev = debugfs_create_dir(pdev_name, ab->debugfs_soc);
1119 	if (IS_ERR(ar->debug.debugfs_pdev))
1120 		return PTR_ERR(ar->debug.debugfs_pdev);
1121 
1122 	/* Create a symlink under ieee80211/phy* */
1123 	snprintf(buf, 100, "../../ath11k/%pd2", ar->debug.debugfs_pdev);
1124 	debugfs_create_symlink("ath11k", ar->hw->wiphy->debugfsdir, buf);
1125 
1126 	ath11k_debugfs_htt_stats_init(ar);
1127 
1128 	ath11k_debugfs_fw_stats_init(ar);
1129 
1130 	debugfs_create_file("ext_tx_stats", 0644,
1131 			    ar->debug.debugfs_pdev, ar,
1132 			    &fops_extd_tx_stats);
1133 	debugfs_create_file("ext_rx_stats", 0644,
1134 			    ar->debug.debugfs_pdev, ar,
1135 			    &fops_extd_rx_stats);
1136 	debugfs_create_file("pktlog_filter", 0644,
1137 			    ar->debug.debugfs_pdev, ar,
1138 			    &fops_pktlog_filter);
1139 
1140 	if (ar->hw->wiphy->bands[NL80211_BAND_5GHZ]) {
1141 		debugfs_create_file("dfs_simulate_radar", 0200,
1142 				    ar->debug.debugfs_pdev, ar,
1143 				    &fops_simulate_radar);
1144 		debugfs_create_bool("dfs_block_radar_events", 0200,
1145 				    ar->debug.debugfs_pdev,
1146 				    &ar->dfs_block_radar_events);
1147 	}
1148 
1149 	return 0;
1150 }
1151 
1152 void ath11k_debugfs_unregister(struct ath11k *ar)
1153 {
1154 }
1155