1 /*
2  * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
3  * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
4  * Copyright (C) 2018 Stanislaw Gruszka <stf_xl@wp.pl>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2
8  * as published by the Free Software Foundation
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15 
16 #include <linux/debugfs.h>
17 
18 #include "mt76x0.h"
19 #include "eeprom.h"
20 
21 static int
mt76_reg_set(void * data,u64 val)22 mt76_reg_set(void *data, u64 val)
23 {
24 	struct mt76x0_dev *dev = data;
25 
26 	mt76_wr(dev, dev->debugfs_reg, val);
27 	return 0;
28 }
29 
30 static int
mt76_reg_get(void * data,u64 * val)31 mt76_reg_get(void *data, u64 *val)
32 {
33 	struct mt76x0_dev *dev = data;
34 
35 	*val = mt76_rr(dev, dev->debugfs_reg);
36 	return 0;
37 }
38 
39 DEFINE_SIMPLE_ATTRIBUTE(fops_regval, mt76_reg_get, mt76_reg_set, "0x%08llx\n");
40 
41 static int
mt76x0_ampdu_stat_read(struct seq_file * file,void * data)42 mt76x0_ampdu_stat_read(struct seq_file *file, void *data)
43 {
44 	struct mt76x0_dev *dev = file->private;
45 	int i, j;
46 
47 #define stat_printf(grp, off, name)					\
48 	seq_printf(file, #name ":\t%llu\n", dev->stats.grp[off])
49 
50 	stat_printf(rx_stat, 0, rx_crc_err);
51 	stat_printf(rx_stat, 1, rx_phy_err);
52 	stat_printf(rx_stat, 2, rx_false_cca);
53 	stat_printf(rx_stat, 3, rx_plcp_err);
54 	stat_printf(rx_stat, 4, rx_fifo_overflow);
55 	stat_printf(rx_stat, 5, rx_duplicate);
56 
57 	stat_printf(tx_stat, 0, tx_fail_cnt);
58 	stat_printf(tx_stat, 1, tx_bcn_cnt);
59 	stat_printf(tx_stat, 2, tx_success);
60 	stat_printf(tx_stat, 3, tx_retransmit);
61 	stat_printf(tx_stat, 4, tx_zero_len);
62 	stat_printf(tx_stat, 5, tx_underflow);
63 
64 	stat_printf(aggr_stat, 0, non_aggr_tx);
65 	stat_printf(aggr_stat, 1, aggr_tx);
66 
67 	stat_printf(zero_len_del, 0, tx_zero_len_del);
68 	stat_printf(zero_len_del, 1, rx_zero_len_del);
69 #undef stat_printf
70 
71 	seq_puts(file, "Aggregations stats:\n");
72 	for (i = 0; i < 4; i++) {
73 		for (j = 0; j < 8; j++)
74 			seq_printf(file, "%08llx ",
75 				   dev->stats.aggr_n[i * 8 + j]);
76 		seq_putc(file, '\n');
77 	}
78 
79 	seq_printf(file, "recent average AMPDU len: %d\n",
80 		   atomic_read(&dev->avg_ampdu_len));
81 
82 	return 0;
83 }
84 
85 static int
mt76x0_ampdu_stat_open(struct inode * inode,struct file * f)86 mt76x0_ampdu_stat_open(struct inode *inode, struct file *f)
87 {
88 	return single_open(f, mt76x0_ampdu_stat_read, inode->i_private);
89 }
90 
91 static const struct file_operations fops_ampdu_stat = {
92 	.open = mt76x0_ampdu_stat_open,
93 	.read = seq_read,
94 	.llseek = seq_lseek,
95 	.release = single_release,
96 };
97 
98 static int
mt76x0_eeprom_param_read(struct seq_file * file,void * data)99 mt76x0_eeprom_param_read(struct seq_file *file, void *data)
100 {
101 	struct mt76x0_dev *dev = file->private;
102 	int i;
103 
104 	seq_printf(file, "RF freq offset: %hhx\n", dev->ee->rf_freq_off);
105 	seq_printf(file, "RSSI offset 2GHz: %hhx %hhx\n",
106 		   dev->ee->rssi_offset_2ghz[0], dev->ee->rssi_offset_2ghz[1]);
107 	seq_printf(file, "RSSI offset 5GHz: %hhx %hhx %hhx\n",
108 		   dev->ee->rssi_offset_5ghz[0], dev->ee->rssi_offset_5ghz[1],
109 		   dev->ee->rssi_offset_5ghz[2]);
110 	seq_printf(file, "Temperature offset: %hhx\n", dev->ee->temp_off);
111 	seq_printf(file, "LNA gain 2Ghz: %hhx\n", dev->ee->lna_gain_2ghz);
112 	seq_printf(file, "LNA gain 5Ghz: %hhx %hhx %hhx\n",
113 		   dev->ee->lna_gain_5ghz[0], dev->ee->lna_gain_5ghz[1],
114 		   dev->ee->lna_gain_5ghz[2]);
115 	seq_printf(file, "Power Amplifier type %hhx\n", dev->ee->pa_type);
116 	seq_printf(file, "Reg channels: %hhu-%hhu\n", dev->ee->reg.start,
117 		   dev->ee->reg.start + dev->ee->reg.num - 1);
118 
119 	seq_puts(file, "Per channel power:\n");
120 	for (i = 0; i < 58; i++)
121 		seq_printf(file, "\t%d chan:%d pwr:%d\n", i, i,
122 			   dev->ee->tx_pwr_per_chan[i]);
123 
124 	seq_puts(file, "Per rate power 2GHz:\n");
125 	for (i = 0; i < 5; i++)
126 		seq_printf(file, "\t %d bw20:%d bw40:%d\n",
127 			   i, dev->ee->tx_pwr_cfg_2g[i][0],
128 			      dev->ee->tx_pwr_cfg_5g[i][1]);
129 
130 	seq_puts(file, "Per rate power 5GHz:\n");
131 	for (i = 0; i < 5; i++)
132 		seq_printf(file, "\t %d bw20:%d bw40:%d\n",
133 			   i, dev->ee->tx_pwr_cfg_5g[i][0],
134 			      dev->ee->tx_pwr_cfg_5g[i][1]);
135 
136 	return 0;
137 }
138 
139 static int
mt76x0_eeprom_param_open(struct inode * inode,struct file * f)140 mt76x0_eeprom_param_open(struct inode *inode, struct file *f)
141 {
142 	return single_open(f, mt76x0_eeprom_param_read, inode->i_private);
143 }
144 
145 static const struct file_operations fops_eeprom_param = {
146 	.open = mt76x0_eeprom_param_open,
147 	.read = seq_read,
148 	.llseek = seq_lseek,
149 	.release = single_release,
150 };
151 
mt76x0_init_debugfs(struct mt76x0_dev * dev)152 void mt76x0_init_debugfs(struct mt76x0_dev *dev)
153 {
154 	struct dentry *dir;
155 
156 	dir = debugfs_create_dir("mt76x0", dev->mt76.hw->wiphy->debugfsdir);
157 	if (!dir)
158 		return;
159 
160 	debugfs_create_u32("regidx", S_IRUSR | S_IWUSR, dir, &dev->debugfs_reg);
161 	debugfs_create_file("regval", S_IRUSR | S_IWUSR, dir, dev,
162 			    &fops_regval);
163 	debugfs_create_file("ampdu_stat", S_IRUSR, dir, dev, &fops_ampdu_stat);
164 	debugfs_create_file("eeprom_param", S_IRUSR, dir, dev,
165 			    &fops_eeprom_param);
166 }
167