• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Debugfs interface.
4  *
5  * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
6  * Copyright (c) 2010, ST-Ericsson
7  */
8 #include <linux/debugfs.h>
9 #include <linux/seq_file.h>
10 #include <linux/crc32.h>
11 
12 #include "debug.h"
13 #include "wfx.h"
14 #include "sta.h"
15 #include "main.h"
16 #include "hif_tx.h"
17 #include "hif_tx_mib.h"
18 
19 #define CREATE_TRACE_POINTS
20 #include "traces.h"
21 
22 static const struct trace_print_flags hif_msg_print_map[] = {
23 	hif_msg_list,
24 };
25 
26 static const struct trace_print_flags hif_mib_print_map[] = {
27 	hif_mib_list,
28 };
29 
30 static const struct trace_print_flags wfx_reg_print_map[] = {
31 	wfx_reg_list,
32 };
33 
get_symbol(unsigned long val,const struct trace_print_flags * symbol_array)34 static const char *get_symbol(unsigned long val,
35 			      const struct trace_print_flags *symbol_array)
36 {
37 	int i;
38 
39 	for (i = 0; symbol_array[i].mask != -1; i++) {
40 		if (val == symbol_array[i].mask)
41 			return symbol_array[i].name;
42 	}
43 
44 	return "unknown";
45 }
46 
get_hif_name(unsigned long id)47 const char *get_hif_name(unsigned long id)
48 {
49 	return get_symbol(id, hif_msg_print_map);
50 }
51 
get_mib_name(unsigned long id)52 const char *get_mib_name(unsigned long id)
53 {
54 	return get_symbol(id, hif_mib_print_map);
55 }
56 
get_reg_name(unsigned long id)57 const char *get_reg_name(unsigned long id)
58 {
59 	return get_symbol(id, wfx_reg_print_map);
60 }
61 
wfx_counters_show(struct seq_file * seq,void * v)62 static int wfx_counters_show(struct seq_file *seq, void *v)
63 {
64 	int ret, i;
65 	struct wfx_dev *wdev = seq->private;
66 	struct hif_mib_extended_count_table counters[3];
67 
68 	for (i = 0; i < ARRAY_SIZE(counters); i++) {
69 		ret = hif_get_counters_table(wdev, i, counters + i);
70 		if (ret < 0)
71 			return ret;
72 		if (ret > 0)
73 			return -EIO;
74 	}
75 
76 	seq_printf(seq, "%-24s %12s %12s %12s\n",
77 		   "", "global", "iface 0", "iface 1");
78 
79 #define PUT_COUNTER(name) \
80 	seq_printf(seq, "%-24s %12d %12d %12d\n", #name, \
81 		   le32_to_cpu(counters[2].count_##name), \
82 		   le32_to_cpu(counters[0].count_##name), \
83 		   le32_to_cpu(counters[1].count_##name))
84 
85 	PUT_COUNTER(tx_packets);
86 	PUT_COUNTER(tx_multicast_frames);
87 	PUT_COUNTER(tx_frames_success);
88 	PUT_COUNTER(tx_frame_failures);
89 	PUT_COUNTER(tx_frames_retried);
90 	PUT_COUNTER(tx_frames_multi_retried);
91 
92 	PUT_COUNTER(rts_success);
93 	PUT_COUNTER(rts_failures);
94 	PUT_COUNTER(ack_failures);
95 
96 	PUT_COUNTER(rx_packets);
97 	PUT_COUNTER(rx_frames_success);
98 	PUT_COUNTER(rx_packet_errors);
99 	PUT_COUNTER(plcp_errors);
100 	PUT_COUNTER(fcs_errors);
101 	PUT_COUNTER(rx_decryption_failures);
102 	PUT_COUNTER(rx_mic_failures);
103 	PUT_COUNTER(rx_no_key_failures);
104 	PUT_COUNTER(rx_frame_duplicates);
105 	PUT_COUNTER(rx_multicast_frames);
106 	PUT_COUNTER(rx_cmacicv_errors);
107 	PUT_COUNTER(rx_cmac_replays);
108 	PUT_COUNTER(rx_mgmt_ccmp_replays);
109 
110 	PUT_COUNTER(rx_beacon);
111 	PUT_COUNTER(miss_beacon);
112 
113 #undef PUT_COUNTER
114 
115 	for (i = 0; i < ARRAY_SIZE(counters[0].reserved); i++)
116 		seq_printf(seq, "reserved[%02d]%12s %12d %12d %12d\n", i, "",
117 			   le32_to_cpu(counters[2].reserved[i]),
118 			   le32_to_cpu(counters[0].reserved[i]),
119 			   le32_to_cpu(counters[1].reserved[i]));
120 
121 	return 0;
122 }
123 DEFINE_SHOW_ATTRIBUTE(wfx_counters);
124 
125 static const char * const channel_names[] = {
126 	[0] = "1M",
127 	[1] = "2M",
128 	[2] = "5.5M",
129 	[3] = "11M",
130 	/* Entries 4 and 5 does not exist */
131 	[6] = "6M",
132 	[7] = "9M",
133 	[8] = "12M",
134 	[9] = "18M",
135 	[10] = "24M",
136 	[11] = "36M",
137 	[12] = "48M",
138 	[13] = "54M",
139 	[14] = "MCS0",
140 	[15] = "MCS1",
141 	[16] = "MCS2",
142 	[17] = "MCS3",
143 	[18] = "MCS4",
144 	[19] = "MCS5",
145 	[20] = "MCS6",
146 	[21] = "MCS7",
147 };
148 
wfx_rx_stats_show(struct seq_file * seq,void * v)149 static int wfx_rx_stats_show(struct seq_file *seq, void *v)
150 {
151 	struct wfx_dev *wdev = seq->private;
152 	struct hif_rx_stats *st = &wdev->rx_stats;
153 	int i;
154 
155 	mutex_lock(&wdev->rx_stats_lock);
156 	seq_printf(seq, "Timestamp: %dus\n", st->date);
157 	seq_printf(seq, "Low power clock: frequency %uHz, external %s\n",
158 		   le32_to_cpu(st->pwr_clk_freq),
159 		   st->is_ext_pwr_clk ? "yes" : "no");
160 	seq_printf(seq,
161 		   "Num. of frames: %d, PER (x10e4): %d, Throughput: %dKbps/s\n",
162 		   st->nb_rx_frame, st->per_total, st->throughput);
163 	seq_puts(seq, "       Num. of      PER     RSSI      SNR      CFO\n");
164 	seq_puts(seq, "        frames  (x10e4)    (dBm)     (dB)    (kHz)\n");
165 	for (i = 0; i < ARRAY_SIZE(channel_names); i++) {
166 		if (channel_names[i])
167 			seq_printf(seq, "%5s %8d %8d %8d %8d %8d\n",
168 				   channel_names[i],
169 				   le32_to_cpu(st->nb_rx_by_rate[i]),
170 				   le16_to_cpu(st->per[i]),
171 				   (s16)le16_to_cpu(st->rssi[i]) / 100,
172 				   (s16)le16_to_cpu(st->snr[i]) / 100,
173 				   (s16)le16_to_cpu(st->cfo[i]));
174 	}
175 	mutex_unlock(&wdev->rx_stats_lock);
176 
177 	return 0;
178 }
179 DEFINE_SHOW_ATTRIBUTE(wfx_rx_stats);
180 
wfx_tx_power_loop_show(struct seq_file * seq,void * v)181 static int wfx_tx_power_loop_show(struct seq_file *seq, void *v)
182 {
183 	struct wfx_dev *wdev = seq->private;
184 	struct hif_tx_power_loop_info *st = &wdev->tx_power_loop_info;
185 	int tmp;
186 
187 	mutex_lock(&wdev->tx_power_loop_info_lock);
188 	tmp = le16_to_cpu(st->tx_gain_dig);
189 	seq_printf(seq, "Tx gain digital: %d\n", tmp);
190 	tmp = le16_to_cpu(st->tx_gain_pa);
191 	seq_printf(seq, "Tx gain PA: %d\n", tmp);
192 	tmp = (s16)le16_to_cpu(st->target_pout);
193 	seq_printf(seq, "Target Pout: %d.%02d dBm\n", tmp / 4, (tmp % 4) * 25);
194 	tmp = (s16)le16_to_cpu(st->p_estimation);
195 	seq_printf(seq, "FEM Pout: %d.%02d dBm\n", tmp / 4, (tmp % 4) * 25);
196 	tmp = le16_to_cpu(st->vpdet);
197 	seq_printf(seq, "Vpdet: %d mV\n", tmp);
198 	seq_printf(seq, "Measure index: %d\n", st->measurement_index);
199 	mutex_unlock(&wdev->tx_power_loop_info_lock);
200 
201 	return 0;
202 }
203 DEFINE_SHOW_ATTRIBUTE(wfx_tx_power_loop);
204 
wfx_send_pds_write(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)205 static ssize_t wfx_send_pds_write(struct file *file,
206 				  const char __user *user_buf,
207 				  size_t count, loff_t *ppos)
208 {
209 	struct wfx_dev *wdev = file->private_data;
210 	char *buf;
211 	int ret;
212 
213 	if (*ppos != 0) {
214 		dev_dbg(wdev->dev, "PDS data must be written in one transaction");
215 		return -EBUSY;
216 	}
217 	buf = memdup_user(user_buf, count);
218 	if (IS_ERR(buf))
219 		return PTR_ERR(buf);
220 	*ppos = *ppos + count;
221 	ret = wfx_send_pds(wdev, buf, count);
222 	kfree(buf);
223 	if (ret < 0)
224 		return ret;
225 	return count;
226 }
227 
228 static const struct file_operations wfx_send_pds_fops = {
229 	.open = simple_open,
230 	.write = wfx_send_pds_write,
231 };
232 
233 struct dbgfs_hif_msg {
234 	struct wfx_dev *wdev;
235 	struct completion complete;
236 	u8 reply[1024];
237 	int ret;
238 };
239 
wfx_send_hif_msg_write(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)240 static ssize_t wfx_send_hif_msg_write(struct file *file,
241 				      const char __user *user_buf,
242 				      size_t count, loff_t *ppos)
243 {
244 	struct dbgfs_hif_msg *context = file->private_data;
245 	struct wfx_dev *wdev = context->wdev;
246 	struct hif_msg *request;
247 
248 	if (completion_done(&context->complete)) {
249 		dev_dbg(wdev->dev, "read previous result before start a new one\n");
250 		return -EBUSY;
251 	}
252 	if (count < sizeof(struct hif_msg))
253 		return -EINVAL;
254 
255 	// wfx_cmd_send() checks that reply buffer is wide enough, but does not
256 	// return precise length read. User have to know how many bytes should
257 	// be read. Filling reply buffer with a memory pattern may help user.
258 	memset(context->reply, 0xFF, sizeof(context->reply));
259 	request = memdup_user(user_buf, count);
260 	if (IS_ERR(request))
261 		return PTR_ERR(request);
262 	if (le16_to_cpu(request->len) != count) {
263 		kfree(request);
264 		return -EINVAL;
265 	}
266 	context->ret = wfx_cmd_send(wdev, request, context->reply,
267 				    sizeof(context->reply), false);
268 
269 	kfree(request);
270 	complete(&context->complete);
271 	return count;
272 }
273 
wfx_send_hif_msg_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)274 static ssize_t wfx_send_hif_msg_read(struct file *file, char __user *user_buf,
275 				     size_t count, loff_t *ppos)
276 {
277 	struct dbgfs_hif_msg *context = file->private_data;
278 	int ret;
279 
280 	if (count > sizeof(context->reply))
281 		return -EINVAL;
282 	ret = wait_for_completion_interruptible(&context->complete);
283 	if (ret)
284 		return ret;
285 	if (context->ret < 0)
286 		return context->ret;
287 	// Be careful, write() is waiting for a full message while read()
288 	// only returns a payload
289 	if (copy_to_user(user_buf, context->reply, count))
290 		return -EFAULT;
291 
292 	return count;
293 }
294 
wfx_send_hif_msg_open(struct inode * inode,struct file * file)295 static int wfx_send_hif_msg_open(struct inode *inode, struct file *file)
296 {
297 	struct dbgfs_hif_msg *context = kzalloc(sizeof(*context), GFP_KERNEL);
298 
299 	if (!context)
300 		return -ENOMEM;
301 	context->wdev = inode->i_private;
302 	init_completion(&context->complete);
303 	file->private_data = context;
304 	return 0;
305 }
306 
wfx_send_hif_msg_release(struct inode * inode,struct file * file)307 static int wfx_send_hif_msg_release(struct inode *inode, struct file *file)
308 {
309 	struct dbgfs_hif_msg *context = file->private_data;
310 
311 	kfree(context);
312 	return 0;
313 }
314 
315 static const struct file_operations wfx_send_hif_msg_fops = {
316 	.open = wfx_send_hif_msg_open,
317 	.release = wfx_send_hif_msg_release,
318 	.write = wfx_send_hif_msg_write,
319 	.read = wfx_send_hif_msg_read,
320 };
321 
wfx_ps_timeout_set(void * data,u64 val)322 static int wfx_ps_timeout_set(void *data, u64 val)
323 {
324 	struct wfx_dev *wdev = (struct wfx_dev *)data;
325 	struct wfx_vif *wvif;
326 
327 	wdev->force_ps_timeout = val;
328 	wvif = NULL;
329 	while ((wvif = wvif_iterate(wdev, wvif)) != NULL)
330 		wfx_update_pm(wvif);
331 	return 0;
332 }
333 
wfx_ps_timeout_get(void * data,u64 * val)334 static int wfx_ps_timeout_get(void *data, u64 *val)
335 {
336 	struct wfx_dev *wdev = (struct wfx_dev *)data;
337 
338 	*val = wdev->force_ps_timeout;
339 	return 0;
340 }
341 
342 DEFINE_DEBUGFS_ATTRIBUTE(wfx_ps_timeout_fops, wfx_ps_timeout_get, wfx_ps_timeout_set, "%lld\n");
343 
wfx_debug_init(struct wfx_dev * wdev)344 int wfx_debug_init(struct wfx_dev *wdev)
345 {
346 	struct dentry *d;
347 
348 	d = debugfs_create_dir("wfx", wdev->hw->wiphy->debugfsdir);
349 	debugfs_create_file("counters", 0444, d, wdev, &wfx_counters_fops);
350 	debugfs_create_file("rx_stats", 0444, d, wdev, &wfx_rx_stats_fops);
351 	debugfs_create_file("tx_power_loop", 0444, d, wdev,
352 			    &wfx_tx_power_loop_fops);
353 	debugfs_create_file("send_pds", 0200, d, wdev, &wfx_send_pds_fops);
354 	debugfs_create_file("send_hif_msg", 0600, d, wdev,
355 			    &wfx_send_hif_msg_fops);
356 	debugfs_create_file("ps_timeout", 0600, d, wdev, &wfx_ps_timeout_fops);
357 
358 	return 0;
359 }
360