xref: /freebsd/sys/contrib/dev/iwlwifi/mvm/debugfs-vif.c (revision 4b9d605768acabc460aa6dcfe8a1f8db35b16794)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2023 Intel Corporation
4  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5  * Copyright (C) 2016-2017 Intel Deutschland GmbH
6  */
7 #include "mvm.h"
8 #include "debugfs.h"
9 #if defined(__FreeBSD__)
10 #include <linux/math64.h>
11 #endif
12 
13 static void iwl_dbgfs_update_pm(struct iwl_mvm *mvm,
14 				 struct ieee80211_vif *vif,
15 				 enum iwl_dbgfs_pm_mask param, int val)
16 {
17 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
18 	struct iwl_dbgfs_pm *dbgfs_pm = &mvmvif->dbgfs_pm;
19 
20 	dbgfs_pm->mask |= param;
21 
22 	switch (param) {
23 	case MVM_DEBUGFS_PM_KEEP_ALIVE: {
24 		int dtimper = vif->bss_conf.dtim_period ?: 1;
25 		int dtimper_msec = dtimper * vif->bss_conf.beacon_int;
26 
27 		IWL_DEBUG_POWER(mvm, "debugfs: set keep_alive= %d sec\n", val);
28 		if (val * MSEC_PER_SEC < 3 * dtimper_msec)
29 			IWL_WARN(mvm,
30 				 "debugfs: keep alive period (%ld msec) is less than minimum required (%d msec)\n",
31 				 val * MSEC_PER_SEC, 3 * dtimper_msec);
32 		dbgfs_pm->keep_alive_seconds = val;
33 		break;
34 	}
35 	case MVM_DEBUGFS_PM_SKIP_OVER_DTIM:
36 		IWL_DEBUG_POWER(mvm, "skip_over_dtim %s\n",
37 				val ? "enabled" : "disabled");
38 		dbgfs_pm->skip_over_dtim = val;
39 		break;
40 	case MVM_DEBUGFS_PM_SKIP_DTIM_PERIODS:
41 		IWL_DEBUG_POWER(mvm, "skip_dtim_periods=%d\n", val);
42 		dbgfs_pm->skip_dtim_periods = val;
43 		break;
44 	case MVM_DEBUGFS_PM_RX_DATA_TIMEOUT:
45 		IWL_DEBUG_POWER(mvm, "rx_data_timeout=%d\n", val);
46 		dbgfs_pm->rx_data_timeout = val;
47 		break;
48 	case MVM_DEBUGFS_PM_TX_DATA_TIMEOUT:
49 		IWL_DEBUG_POWER(mvm, "tx_data_timeout=%d\n", val);
50 		dbgfs_pm->tx_data_timeout = val;
51 		break;
52 	case MVM_DEBUGFS_PM_LPRX_ENA:
53 		IWL_DEBUG_POWER(mvm, "lprx %s\n", val ? "enabled" : "disabled");
54 		dbgfs_pm->lprx_ena = val;
55 		break;
56 	case MVM_DEBUGFS_PM_LPRX_RSSI_THRESHOLD:
57 		IWL_DEBUG_POWER(mvm, "lprx_rssi_threshold=%d\n", val);
58 		dbgfs_pm->lprx_rssi_threshold = val;
59 		break;
60 	case MVM_DEBUGFS_PM_SNOOZE_ENABLE:
61 		IWL_DEBUG_POWER(mvm, "snooze_enable=%d\n", val);
62 		dbgfs_pm->snooze_ena = val;
63 		break;
64 	case MVM_DEBUGFS_PM_UAPSD_MISBEHAVING:
65 		IWL_DEBUG_POWER(mvm, "uapsd_misbehaving_enable=%d\n", val);
66 		dbgfs_pm->uapsd_misbehaving = val;
67 		break;
68 	case MVM_DEBUGFS_PM_USE_PS_POLL:
69 		IWL_DEBUG_POWER(mvm, "use_ps_poll=%d\n", val);
70 		dbgfs_pm->use_ps_poll = val;
71 		break;
72 	}
73 }
74 
75 static ssize_t iwl_dbgfs_pm_params_write(struct ieee80211_vif *vif, char *buf,
76 					 size_t count, loff_t *ppos)
77 {
78 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
79 	struct iwl_mvm *mvm = mvmvif->mvm;
80 	enum iwl_dbgfs_pm_mask param;
81 	int val, ret;
82 
83 	if (!strncmp("keep_alive=", buf, 11)) {
84 		if (sscanf(buf + 11, "%d", &val) != 1)
85 			return -EINVAL;
86 		param = MVM_DEBUGFS_PM_KEEP_ALIVE;
87 	} else if (!strncmp("skip_over_dtim=", buf, 15)) {
88 		if (sscanf(buf + 15, "%d", &val) != 1)
89 			return -EINVAL;
90 		param = MVM_DEBUGFS_PM_SKIP_OVER_DTIM;
91 	} else if (!strncmp("skip_dtim_periods=", buf, 18)) {
92 		if (sscanf(buf + 18, "%d", &val) != 1)
93 			return -EINVAL;
94 		param = MVM_DEBUGFS_PM_SKIP_DTIM_PERIODS;
95 	} else if (!strncmp("rx_data_timeout=", buf, 16)) {
96 		if (sscanf(buf + 16, "%d", &val) != 1)
97 			return -EINVAL;
98 		param = MVM_DEBUGFS_PM_RX_DATA_TIMEOUT;
99 	} else if (!strncmp("tx_data_timeout=", buf, 16)) {
100 		if (sscanf(buf + 16, "%d", &val) != 1)
101 			return -EINVAL;
102 		param = MVM_DEBUGFS_PM_TX_DATA_TIMEOUT;
103 	} else if (!strncmp("lprx=", buf, 5)) {
104 		if (sscanf(buf + 5, "%d", &val) != 1)
105 			return -EINVAL;
106 		param = MVM_DEBUGFS_PM_LPRX_ENA;
107 	} else if (!strncmp("lprx_rssi_threshold=", buf, 20)) {
108 		if (sscanf(buf + 20, "%d", &val) != 1)
109 			return -EINVAL;
110 		if (val > POWER_LPRX_RSSI_THRESHOLD_MAX || val <
111 		    POWER_LPRX_RSSI_THRESHOLD_MIN)
112 			return -EINVAL;
113 		param = MVM_DEBUGFS_PM_LPRX_RSSI_THRESHOLD;
114 	} else if (!strncmp("snooze_enable=", buf, 14)) {
115 		if (sscanf(buf + 14, "%d", &val) != 1)
116 			return -EINVAL;
117 		param = MVM_DEBUGFS_PM_SNOOZE_ENABLE;
118 	} else if (!strncmp("uapsd_misbehaving=", buf, 18)) {
119 		if (sscanf(buf + 18, "%d", &val) != 1)
120 			return -EINVAL;
121 		param = MVM_DEBUGFS_PM_UAPSD_MISBEHAVING;
122 	} else if (!strncmp("use_ps_poll=", buf, 12)) {
123 		if (sscanf(buf + 12, "%d", &val) != 1)
124 			return -EINVAL;
125 		param = MVM_DEBUGFS_PM_USE_PS_POLL;
126 	} else {
127 		return -EINVAL;
128 	}
129 
130 	mutex_lock(&mvm->mutex);
131 	iwl_dbgfs_update_pm(mvm, vif, param, val);
132 	ret = iwl_mvm_power_update_mac(mvm);
133 	mutex_unlock(&mvm->mutex);
134 
135 	return ret ?: count;
136 }
137 
138 static ssize_t iwl_dbgfs_tx_pwr_lmt_read(struct file *file,
139 					 char __user *user_buf,
140 					 size_t count, loff_t *ppos)
141 {
142 	struct ieee80211_vif *vif = file->private_data;
143 	char buf[64];
144 	int bufsz = sizeof(buf);
145 	int pos;
146 
147 	pos = scnprintf(buf, bufsz, "bss limit = %d\n",
148 			vif->bss_conf.txpower);
149 
150 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
151 }
152 
153 static ssize_t iwl_dbgfs_pm_params_read(struct file *file,
154 					char __user *user_buf,
155 					size_t count, loff_t *ppos)
156 {
157 	struct ieee80211_vif *vif = file->private_data;
158 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
159 	struct iwl_mvm *mvm = mvmvif->mvm;
160 	char buf[512];
161 	int bufsz = sizeof(buf);
162 	int pos;
163 
164 	pos = iwl_mvm_power_mac_dbgfs_read(mvm, vif, buf, bufsz);
165 
166 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
167 }
168 
169 static ssize_t iwl_dbgfs_mac_params_read(struct file *file,
170 					 char __user *user_buf,
171 					 size_t count, loff_t *ppos)
172 {
173 	struct ieee80211_vif *vif = file->private_data;
174 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
175 	struct iwl_mvm *mvm = mvmvif->mvm;
176 	u8 ap_sta_id;
177 	struct ieee80211_chanctx_conf *chanctx_conf;
178 	char buf[512];
179 	int bufsz = sizeof(buf);
180 	int pos = 0;
181 	int i;
182 
183 	mutex_lock(&mvm->mutex);
184 
185 	ap_sta_id = mvmvif->deflink.ap_sta_id;
186 
187 	switch (ieee80211_vif_type_p2p(vif)) {
188 	case NL80211_IFTYPE_ADHOC:
189 		pos += scnprintf(buf+pos, bufsz-pos, "type: ibss\n");
190 		break;
191 	case NL80211_IFTYPE_STATION:
192 		pos += scnprintf(buf+pos, bufsz-pos, "type: bss\n");
193 		break;
194 	case NL80211_IFTYPE_AP:
195 		pos += scnprintf(buf+pos, bufsz-pos, "type: ap\n");
196 		break;
197 	case NL80211_IFTYPE_P2P_CLIENT:
198 		pos += scnprintf(buf+pos, bufsz-pos, "type: p2p client\n");
199 		break;
200 	case NL80211_IFTYPE_P2P_GO:
201 		pos += scnprintf(buf+pos, bufsz-pos, "type: p2p go\n");
202 		break;
203 	case NL80211_IFTYPE_P2P_DEVICE:
204 		pos += scnprintf(buf+pos, bufsz-pos, "type: p2p dev\n");
205 		break;
206 	default:
207 		break;
208 	}
209 
210 	pos += scnprintf(buf+pos, bufsz-pos, "mac id/color: %d / %d\n",
211 			 mvmvif->id, mvmvif->color);
212 	pos += scnprintf(buf+pos, bufsz-pos, "bssid: %pM\n",
213 			 vif->bss_conf.bssid);
214 	pos += scnprintf(buf+pos, bufsz-pos, "Load: %d\n",
215 			 mvm->tcm.result.load[mvmvif->id]);
216 	pos += scnprintf(buf+pos, bufsz-pos, "QoS:\n");
217 	for (i = 0; i < ARRAY_SIZE(mvmvif->deflink.queue_params); i++)
218 		pos += scnprintf(buf+pos, bufsz-pos,
219 				 "\t%d: txop:%d - cw_min:%d - cw_max = %d - aifs = %d upasd = %d\n",
220 				 i, mvmvif->deflink.queue_params[i].txop,
221 				 mvmvif->deflink.queue_params[i].cw_min,
222 				 mvmvif->deflink.queue_params[i].cw_max,
223 				 mvmvif->deflink.queue_params[i].aifs,
224 				 mvmvif->deflink.queue_params[i].uapsd);
225 
226 	if (vif->type == NL80211_IFTYPE_STATION &&
227 	    ap_sta_id != IWL_MVM_INVALID_STA) {
228 		struct iwl_mvm_sta *mvm_sta;
229 
230 		mvm_sta = iwl_mvm_sta_from_staid_protected(mvm, ap_sta_id);
231 		if (mvm_sta) {
232 			pos += scnprintf(buf+pos, bufsz-pos,
233 					 "ap_sta_id %d - reduced Tx power %d\n",
234 					 ap_sta_id,
235 					 mvm_sta->bt_reduced_txpower);
236 		}
237 	}
238 
239 	rcu_read_lock();
240 	chanctx_conf = rcu_dereference(vif->bss_conf.chanctx_conf);
241 	if (chanctx_conf)
242 		pos += scnprintf(buf+pos, bufsz-pos,
243 				 "idle rx chains %d, active rx chains: %d\n",
244 				 chanctx_conf->rx_chains_static,
245 				 chanctx_conf->rx_chains_dynamic);
246 	rcu_read_unlock();
247 
248 	mutex_unlock(&mvm->mutex);
249 
250 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
251 }
252 
253 static void iwl_dbgfs_update_bf(struct ieee80211_vif *vif,
254 				enum iwl_dbgfs_bf_mask param, int value)
255 {
256 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
257 	struct iwl_dbgfs_bf *dbgfs_bf = &mvmvif->dbgfs_bf;
258 
259 	dbgfs_bf->mask |= param;
260 
261 	switch (param) {
262 	case MVM_DEBUGFS_BF_ENERGY_DELTA:
263 		dbgfs_bf->bf_energy_delta = value;
264 		break;
265 	case MVM_DEBUGFS_BF_ROAMING_ENERGY_DELTA:
266 		dbgfs_bf->bf_roaming_energy_delta = value;
267 		break;
268 	case MVM_DEBUGFS_BF_ROAMING_STATE:
269 		dbgfs_bf->bf_roaming_state = value;
270 		break;
271 	case MVM_DEBUGFS_BF_TEMP_THRESHOLD:
272 		dbgfs_bf->bf_temp_threshold = value;
273 		break;
274 	case MVM_DEBUGFS_BF_TEMP_FAST_FILTER:
275 		dbgfs_bf->bf_temp_fast_filter = value;
276 		break;
277 	case MVM_DEBUGFS_BF_TEMP_SLOW_FILTER:
278 		dbgfs_bf->bf_temp_slow_filter = value;
279 		break;
280 	case MVM_DEBUGFS_BF_ENABLE_BEACON_FILTER:
281 		dbgfs_bf->bf_enable_beacon_filter = value;
282 		break;
283 	case MVM_DEBUGFS_BF_DEBUG_FLAG:
284 		dbgfs_bf->bf_debug_flag = value;
285 		break;
286 	case MVM_DEBUGFS_BF_ESCAPE_TIMER:
287 		dbgfs_bf->bf_escape_timer = value;
288 		break;
289 	case MVM_DEBUGFS_BA_ENABLE_BEACON_ABORT:
290 		dbgfs_bf->ba_enable_beacon_abort = value;
291 		break;
292 	case MVM_DEBUGFS_BA_ESCAPE_TIMER:
293 		dbgfs_bf->ba_escape_timer = value;
294 		break;
295 	}
296 }
297 
298 static ssize_t iwl_dbgfs_bf_params_write(struct ieee80211_vif *vif, char *buf,
299 					 size_t count, loff_t *ppos)
300 {
301 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
302 	struct iwl_mvm *mvm = mvmvif->mvm;
303 	enum iwl_dbgfs_bf_mask param;
304 	int value, ret = 0;
305 
306 	if (!strncmp("bf_energy_delta=", buf, 16)) {
307 		if (sscanf(buf+16, "%d", &value) != 1)
308 			return -EINVAL;
309 		if (value < IWL_BF_ENERGY_DELTA_MIN ||
310 		    value > IWL_BF_ENERGY_DELTA_MAX)
311 			return -EINVAL;
312 		param = MVM_DEBUGFS_BF_ENERGY_DELTA;
313 	} else if (!strncmp("bf_roaming_energy_delta=", buf, 24)) {
314 		if (sscanf(buf+24, "%d", &value) != 1)
315 			return -EINVAL;
316 		if (value < IWL_BF_ROAMING_ENERGY_DELTA_MIN ||
317 		    value > IWL_BF_ROAMING_ENERGY_DELTA_MAX)
318 			return -EINVAL;
319 		param = MVM_DEBUGFS_BF_ROAMING_ENERGY_DELTA;
320 	} else if (!strncmp("bf_roaming_state=", buf, 17)) {
321 		if (sscanf(buf+17, "%d", &value) != 1)
322 			return -EINVAL;
323 		if (value < IWL_BF_ROAMING_STATE_MIN ||
324 		    value > IWL_BF_ROAMING_STATE_MAX)
325 			return -EINVAL;
326 		param = MVM_DEBUGFS_BF_ROAMING_STATE;
327 	} else if (!strncmp("bf_temp_threshold=", buf, 18)) {
328 		if (sscanf(buf+18, "%d", &value) != 1)
329 			return -EINVAL;
330 		if (value < IWL_BF_TEMP_THRESHOLD_MIN ||
331 		    value > IWL_BF_TEMP_THRESHOLD_MAX)
332 			return -EINVAL;
333 		param = MVM_DEBUGFS_BF_TEMP_THRESHOLD;
334 	} else if (!strncmp("bf_temp_fast_filter=", buf, 20)) {
335 		if (sscanf(buf+20, "%d", &value) != 1)
336 			return -EINVAL;
337 		if (value < IWL_BF_TEMP_FAST_FILTER_MIN ||
338 		    value > IWL_BF_TEMP_FAST_FILTER_MAX)
339 			return -EINVAL;
340 		param = MVM_DEBUGFS_BF_TEMP_FAST_FILTER;
341 	} else if (!strncmp("bf_temp_slow_filter=", buf, 20)) {
342 		if (sscanf(buf+20, "%d", &value) != 1)
343 			return -EINVAL;
344 		if (value < IWL_BF_TEMP_SLOW_FILTER_MIN ||
345 		    value > IWL_BF_TEMP_SLOW_FILTER_MAX)
346 			return -EINVAL;
347 		param = MVM_DEBUGFS_BF_TEMP_SLOW_FILTER;
348 	} else if (!strncmp("bf_enable_beacon_filter=", buf, 24)) {
349 		if (sscanf(buf+24, "%d", &value) != 1)
350 			return -EINVAL;
351 		if (value < 0 || value > 1)
352 			return -EINVAL;
353 		param = MVM_DEBUGFS_BF_ENABLE_BEACON_FILTER;
354 	} else if (!strncmp("bf_debug_flag=", buf, 14)) {
355 		if (sscanf(buf+14, "%d", &value) != 1)
356 			return -EINVAL;
357 		if (value < 0 || value > 1)
358 			return -EINVAL;
359 		param = MVM_DEBUGFS_BF_DEBUG_FLAG;
360 	} else if (!strncmp("bf_escape_timer=", buf, 16)) {
361 		if (sscanf(buf+16, "%d", &value) != 1)
362 			return -EINVAL;
363 		if (value < IWL_BF_ESCAPE_TIMER_MIN ||
364 		    value > IWL_BF_ESCAPE_TIMER_MAX)
365 			return -EINVAL;
366 		param = MVM_DEBUGFS_BF_ESCAPE_TIMER;
367 	} else if (!strncmp("ba_escape_timer=", buf, 16)) {
368 		if (sscanf(buf+16, "%d", &value) != 1)
369 			return -EINVAL;
370 		if (value < IWL_BA_ESCAPE_TIMER_MIN ||
371 		    value > IWL_BA_ESCAPE_TIMER_MAX)
372 			return -EINVAL;
373 		param = MVM_DEBUGFS_BA_ESCAPE_TIMER;
374 	} else if (!strncmp("ba_enable_beacon_abort=", buf, 23)) {
375 		if (sscanf(buf+23, "%d", &value) != 1)
376 			return -EINVAL;
377 		if (value < 0 || value > 1)
378 			return -EINVAL;
379 		param = MVM_DEBUGFS_BA_ENABLE_BEACON_ABORT;
380 	} else {
381 		return -EINVAL;
382 	}
383 
384 	mutex_lock(&mvm->mutex);
385 	iwl_dbgfs_update_bf(vif, param, value);
386 	if (param == MVM_DEBUGFS_BF_ENABLE_BEACON_FILTER && !value)
387 		ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
388 	else
389 		ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
390 	mutex_unlock(&mvm->mutex);
391 
392 	return ret ?: count;
393 }
394 
395 static ssize_t iwl_dbgfs_bf_params_read(struct file *file,
396 					char __user *user_buf,
397 					size_t count, loff_t *ppos)
398 {
399 	struct ieee80211_vif *vif = file->private_data;
400 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
401 	char buf[256];
402 	int pos = 0;
403 	const size_t bufsz = sizeof(buf);
404 	struct iwl_beacon_filter_cmd cmd = {
405 		IWL_BF_CMD_CONFIG_DEFAULTS,
406 		.bf_enable_beacon_filter =
407 			cpu_to_le32(IWL_BF_ENABLE_BEACON_FILTER_DEFAULT),
408 		.ba_enable_beacon_abort =
409 			cpu_to_le32(IWL_BA_ENABLE_BEACON_ABORT_DEFAULT),
410 	};
411 
412 	iwl_mvm_beacon_filter_debugfs_parameters(vif, &cmd);
413 	if (mvmvif->bf_data.bf_enabled)
414 		cmd.bf_enable_beacon_filter = cpu_to_le32(1);
415 	else
416 		cmd.bf_enable_beacon_filter = 0;
417 
418 	pos += scnprintf(buf+pos, bufsz-pos, "bf_energy_delta = %d\n",
419 			 le32_to_cpu(cmd.bf_energy_delta));
420 	pos += scnprintf(buf+pos, bufsz-pos, "bf_roaming_energy_delta = %d\n",
421 			 le32_to_cpu(cmd.bf_roaming_energy_delta));
422 	pos += scnprintf(buf+pos, bufsz-pos, "bf_roaming_state = %d\n",
423 			 le32_to_cpu(cmd.bf_roaming_state));
424 	pos += scnprintf(buf+pos, bufsz-pos, "bf_temp_threshold = %d\n",
425 			 le32_to_cpu(cmd.bf_temp_threshold));
426 	pos += scnprintf(buf+pos, bufsz-pos, "bf_temp_fast_filter = %d\n",
427 			 le32_to_cpu(cmd.bf_temp_fast_filter));
428 	pos += scnprintf(buf+pos, bufsz-pos, "bf_temp_slow_filter = %d\n",
429 			 le32_to_cpu(cmd.bf_temp_slow_filter));
430 	pos += scnprintf(buf+pos, bufsz-pos, "bf_enable_beacon_filter = %d\n",
431 			 le32_to_cpu(cmd.bf_enable_beacon_filter));
432 	pos += scnprintf(buf+pos, bufsz-pos, "bf_debug_flag = %d\n",
433 			 le32_to_cpu(cmd.bf_debug_flag));
434 	pos += scnprintf(buf+pos, bufsz-pos, "bf_escape_timer = %d\n",
435 			 le32_to_cpu(cmd.bf_escape_timer));
436 	pos += scnprintf(buf+pos, bufsz-pos, "ba_escape_timer = %d\n",
437 			 le32_to_cpu(cmd.ba_escape_timer));
438 	pos += scnprintf(buf+pos, bufsz-pos, "ba_enable_beacon_abort = %d\n",
439 			 le32_to_cpu(cmd.ba_enable_beacon_abort));
440 
441 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
442 }
443 
444 static ssize_t iwl_dbgfs_os_device_timediff_read(struct file *file,
445 						 char __user *user_buf,
446 						 size_t count, loff_t *ppos)
447 {
448 	struct ieee80211_vif *vif = file->private_data;
449 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
450 	struct iwl_mvm *mvm = mvmvif->mvm;
451 	u32 curr_gp2;
452 	u64 curr_os;
453 	s64 diff;
454 	char buf[64];
455 	const size_t bufsz = sizeof(buf);
456 	int pos = 0;
457 
458 	mutex_lock(&mvm->mutex);
459 	iwl_mvm_get_sync_time(mvm, CLOCK_BOOTTIME, &curr_gp2, &curr_os, NULL);
460 	mutex_unlock(&mvm->mutex);
461 
462 	do_div(curr_os, NSEC_PER_USEC);
463 	diff = curr_os - curr_gp2;
464 	pos += scnprintf(buf + pos, bufsz - pos, "diff=%lld\n", diff);
465 
466 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
467 }
468 
469 static ssize_t iwl_dbgfs_low_latency_write(struct ieee80211_vif *vif, char *buf,
470 					   size_t count, loff_t *ppos)
471 {
472 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
473 	struct iwl_mvm *mvm = mvmvif->mvm;
474 	u8 value;
475 	int ret;
476 
477 	ret = kstrtou8(buf, 0, &value);
478 	if (ret)
479 		return ret;
480 	if (value > 1)
481 		return -EINVAL;
482 
483 	mutex_lock(&mvm->mutex);
484 	iwl_mvm_update_low_latency(mvm, vif, value, LOW_LATENCY_DEBUGFS);
485 	mutex_unlock(&mvm->mutex);
486 
487 	return count;
488 }
489 
490 static ssize_t
491 iwl_dbgfs_low_latency_force_write(struct ieee80211_vif *vif, char *buf,
492 				  size_t count, loff_t *ppos)
493 {
494 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
495 	struct iwl_mvm *mvm = mvmvif->mvm;
496 	u8 value;
497 	int ret;
498 
499 	ret = kstrtou8(buf, 0, &value);
500 	if (ret)
501 		return ret;
502 
503 	if (value > NUM_LOW_LATENCY_FORCE)
504 		return -EINVAL;
505 
506 	mutex_lock(&mvm->mutex);
507 	if (value == LOW_LATENCY_FORCE_UNSET) {
508 		iwl_mvm_update_low_latency(mvm, vif, false,
509 					   LOW_LATENCY_DEBUGFS_FORCE);
510 		iwl_mvm_update_low_latency(mvm, vif, false,
511 					   LOW_LATENCY_DEBUGFS_FORCE_ENABLE);
512 	} else {
513 		iwl_mvm_update_low_latency(mvm, vif,
514 					   value == LOW_LATENCY_FORCE_ON,
515 					   LOW_LATENCY_DEBUGFS_FORCE);
516 		iwl_mvm_update_low_latency(mvm, vif, true,
517 					   LOW_LATENCY_DEBUGFS_FORCE_ENABLE);
518 	}
519 	mutex_unlock(&mvm->mutex);
520 	return count;
521 }
522 
523 static ssize_t iwl_dbgfs_low_latency_read(struct file *file,
524 					  char __user *user_buf,
525 					  size_t count, loff_t *ppos)
526 {
527 	struct ieee80211_vif *vif = file->private_data;
528 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
529 	char format[] = "traffic=%d\ndbgfs=%d\nvcmd=%d\nvif_type=%d\n"
530 			"dbgfs_force_enable=%d\ndbgfs_force=%d\nactual=%d\n";
531 
532 	/*
533 	 * all values in format are boolean so the size of format is enough
534 	 * for holding the result string
535 	 */
536 	char buf[sizeof(format) + 1] = {};
537 	int len;
538 
539 	len = scnprintf(buf, sizeof(buf) - 1, format,
540 			!!(mvmvif->low_latency & LOW_LATENCY_TRAFFIC),
541 			!!(mvmvif->low_latency & LOW_LATENCY_DEBUGFS),
542 			!!(mvmvif->low_latency & LOW_LATENCY_VCMD),
543 			!!(mvmvif->low_latency & LOW_LATENCY_VIF_TYPE),
544 			!!(mvmvif->low_latency &
545 			   LOW_LATENCY_DEBUGFS_FORCE_ENABLE),
546 			!!(mvmvif->low_latency & LOW_LATENCY_DEBUGFS_FORCE),
547 			!!(mvmvif->low_latency_actual));
548 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
549 }
550 
551 static ssize_t iwl_dbgfs_uapsd_misbehaving_read(struct file *file,
552 						char __user *user_buf,
553 						size_t count, loff_t *ppos)
554 {
555 	struct ieee80211_vif *vif = file->private_data;
556 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
557 	char buf[20];
558 	int len;
559 
560 #if defined(__linux__)
561 	len = sprintf(buf, "%pM\n", mvmvif->uapsd_misbehaving_ap_addr);
562 #elif defined(__FreeBSD__)
563 	len = sprintf(buf, "%6D\n", mvmvif->uapsd_misbehaving_ap_addr, ":");
564 #endif
565 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
566 }
567 
568 static ssize_t iwl_dbgfs_uapsd_misbehaving_write(struct ieee80211_vif *vif,
569 						 char *buf, size_t count,
570 						 loff_t *ppos)
571 {
572 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
573 	struct iwl_mvm *mvm = mvmvif->mvm;
574 	bool ret;
575 
576 	mutex_lock(&mvm->mutex);
577 	ret = mac_pton(buf, mvmvif->uapsd_misbehaving_ap_addr);
578 	mutex_unlock(&mvm->mutex);
579 
580 	return ret ? count : -EINVAL;
581 }
582 
583 static ssize_t iwl_dbgfs_rx_phyinfo_write(struct ieee80211_vif *vif, char *buf,
584 					  size_t count, loff_t *ppos)
585 {
586 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
587 	struct iwl_mvm *mvm = mvmvif->mvm;
588 	struct ieee80211_chanctx_conf *chanctx_conf;
589 	struct iwl_mvm_phy_ctxt *phy_ctxt;
590 	u16 value;
591 	int ret;
592 
593 	ret = kstrtou16(buf, 0, &value);
594 	if (ret)
595 		return ret;
596 
597 	mutex_lock(&mvm->mutex);
598 	rcu_read_lock();
599 
600 	chanctx_conf = rcu_dereference(vif->bss_conf.chanctx_conf);
601 	/* make sure the channel context is assigned */
602 	if (!chanctx_conf) {
603 		rcu_read_unlock();
604 		mutex_unlock(&mvm->mutex);
605 		return -EINVAL;
606 	}
607 
608 	phy_ctxt = &mvm->phy_ctxts[*(u16 *)chanctx_conf->drv_priv];
609 	rcu_read_unlock();
610 
611 	mvm->dbgfs_rx_phyinfo = value;
612 
613 	ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chanctx_conf->min_def,
614 				       chanctx_conf->rx_chains_static,
615 				       chanctx_conf->rx_chains_dynamic);
616 	mutex_unlock(&mvm->mutex);
617 
618 	return ret ?: count;
619 }
620 
621 static ssize_t iwl_dbgfs_rx_phyinfo_read(struct file *file,
622 					 char __user *user_buf,
623 					 size_t count, loff_t *ppos)
624 {
625 	struct ieee80211_vif *vif = file->private_data;
626 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
627 	char buf[8];
628 	int len;
629 
630 	len = scnprintf(buf, sizeof(buf), "0x%04x\n",
631 			mvmvif->mvm->dbgfs_rx_phyinfo);
632 
633 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
634 }
635 
636 static void iwl_dbgfs_quota_check(void *data, u8 *mac,
637 				  struct ieee80211_vif *vif)
638 {
639 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
640 	int *ret = data;
641 
642 	if (mvmvif->dbgfs_quota_min)
643 		*ret = -EINVAL;
644 }
645 
646 static ssize_t iwl_dbgfs_quota_min_write(struct ieee80211_vif *vif, char *buf,
647 					 size_t count, loff_t *ppos)
648 {
649 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
650 	struct iwl_mvm *mvm = mvmvif->mvm;
651 	u16 value;
652 	int ret;
653 
654 	ret = kstrtou16(buf, 0, &value);
655 	if (ret)
656 		return ret;
657 
658 	if (value > 95)
659 		return -EINVAL;
660 
661 	mutex_lock(&mvm->mutex);
662 
663 	mvmvif->dbgfs_quota_min = 0;
664 	ieee80211_iterate_interfaces(mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
665 				     iwl_dbgfs_quota_check, &ret);
666 	if (ret == 0) {
667 		mvmvif->dbgfs_quota_min = value;
668 		iwl_mvm_update_quotas(mvm, false, NULL);
669 	}
670 	mutex_unlock(&mvm->mutex);
671 
672 	return ret ?: count;
673 }
674 
675 static ssize_t iwl_dbgfs_quota_min_read(struct file *file,
676 					char __user *user_buf,
677 					size_t count, loff_t *ppos)
678 {
679 	struct ieee80211_vif *vif = file->private_data;
680 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
681 	char buf[10];
682 	int len;
683 
684 	len = scnprintf(buf, sizeof(buf), "%d\n", mvmvif->dbgfs_quota_min);
685 
686 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
687 }
688 
689 #define MVM_DEBUGFS_WRITE_FILE_OPS(name, bufsz) \
690 	_MVM_DEBUGFS_WRITE_FILE_OPS(name, bufsz, struct ieee80211_vif)
691 #define MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz) \
692 	_MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz, struct ieee80211_vif)
693 #define MVM_DEBUGFS_ADD_FILE_VIF(name, parent, mode) do {		\
694 		debugfs_create_file(#name, mode, parent, vif,		\
695 				    &iwl_dbgfs_##name##_ops);		\
696 	} while (0)
697 
698 MVM_DEBUGFS_READ_FILE_OPS(mac_params);
699 MVM_DEBUGFS_READ_FILE_OPS(tx_pwr_lmt);
700 MVM_DEBUGFS_READ_WRITE_FILE_OPS(pm_params, 32);
701 MVM_DEBUGFS_READ_WRITE_FILE_OPS(bf_params, 256);
702 MVM_DEBUGFS_READ_WRITE_FILE_OPS(low_latency, 10);
703 MVM_DEBUGFS_WRITE_FILE_OPS(low_latency_force, 10);
704 MVM_DEBUGFS_READ_WRITE_FILE_OPS(uapsd_misbehaving, 20);
705 MVM_DEBUGFS_READ_WRITE_FILE_OPS(rx_phyinfo, 10);
706 MVM_DEBUGFS_READ_WRITE_FILE_OPS(quota_min, 32);
707 MVM_DEBUGFS_READ_FILE_OPS(os_device_timediff);
708 
709 
710 void iwl_mvm_vif_dbgfs_register(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
711 {
712 	struct dentry *dbgfs_dir = vif->debugfs_dir;
713 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
714 #if defined(__linux__)
715 	char buf[100];
716 #endif
717 
718 	/*
719 	 * Check if debugfs directory already exist before creating it.
720 	 * This may happen when, for example, resetting hw or suspend-resume
721 	 */
722 	if (!dbgfs_dir || mvmvif->dbgfs_dir)
723 		return;
724 
725 	mvmvif->dbgfs_dir = debugfs_create_dir("iwlmvm", dbgfs_dir);
726 	if (IS_ERR_OR_NULL(mvmvif->dbgfs_dir)) {
727 		IWL_ERR(mvm, "Failed to create debugfs directory under %pd\n",
728 			dbgfs_dir);
729 		return;
730 	}
731 
732 	if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM &&
733 	    ((vif->type == NL80211_IFTYPE_STATION && !vif->p2p) ||
734 	     (vif->type == NL80211_IFTYPE_STATION && vif->p2p)))
735 		MVM_DEBUGFS_ADD_FILE_VIF(pm_params, mvmvif->dbgfs_dir, 0600);
736 
737 	MVM_DEBUGFS_ADD_FILE_VIF(tx_pwr_lmt, mvmvif->dbgfs_dir, 0400);
738 	MVM_DEBUGFS_ADD_FILE_VIF(mac_params, mvmvif->dbgfs_dir, 0400);
739 	MVM_DEBUGFS_ADD_FILE_VIF(low_latency, mvmvif->dbgfs_dir, 0600);
740 	MVM_DEBUGFS_ADD_FILE_VIF(low_latency_force, mvmvif->dbgfs_dir, 0600);
741 	MVM_DEBUGFS_ADD_FILE_VIF(uapsd_misbehaving, mvmvif->dbgfs_dir, 0600);
742 	MVM_DEBUGFS_ADD_FILE_VIF(rx_phyinfo, mvmvif->dbgfs_dir, 0600);
743 	MVM_DEBUGFS_ADD_FILE_VIF(quota_min, mvmvif->dbgfs_dir, 0600);
744 	MVM_DEBUGFS_ADD_FILE_VIF(os_device_timediff, mvmvif->dbgfs_dir, 0400);
745 
746 	if (vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
747 	    mvmvif == mvm->bf_allowed_vif)
748 		MVM_DEBUGFS_ADD_FILE_VIF(bf_params, mvmvif->dbgfs_dir, 0600);
749 
750 #if defined(__linux__)
751 	/*
752 	 * Create symlink for convenience pointing to interface specific
753 	 * debugfs entries for the driver. For example, under
754 	 * /sys/kernel/debug/iwlwifi/0000\:02\:00.0/iwlmvm/
755 	 * find
756 	 * netdev:wlan0 -> ../../../ieee80211/phy0/netdev:wlan0/iwlmvm/
757 	 */
758 	snprintf(buf, 100, "../../../%pd3/%pd",
759 		 dbgfs_dir,
760 		 mvmvif->dbgfs_dir);
761 
762 	mvmvif->dbgfs_slink = debugfs_create_symlink(dbgfs_dir->d_name.name,
763 						     mvm->debugfs_dir, buf);
764 #endif
765 }
766 
767 void iwl_mvm_vif_dbgfs_clean(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
768 {
769 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
770 
771 	debugfs_remove(mvmvif->dbgfs_slink);
772 	mvmvif->dbgfs_slink = NULL;
773 
774 	debugfs_remove_recursive(mvmvif->dbgfs_dir);
775 	mvmvif->dbgfs_dir = NULL;
776 }
777