xref: /freebsd/sys/contrib/dev/iwlwifi/mvm/debugfs-vif.c (revision db33c6f3ae9d1231087710068ee4ea5398aacca7)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2024 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);
388 	else
389 		ret = iwl_mvm_enable_beacon_filter(mvm, vif);
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_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_bss_conf *link_conf;
589 	u16 value;
590 	int link_id, ret = -EINVAL;
591 
592 	ret = kstrtou16(buf, 0, &value);
593 	if (ret)
594 		return ret;
595 
596 	mutex_lock(&mvm->mutex);
597 
598 	mvm->dbgfs_rx_phyinfo = value;
599 
600 	for_each_vif_active_link(vif, link_conf, link_id) {
601 		struct ieee80211_chanctx_conf *chanctx_conf;
602 		struct cfg80211_chan_def min_def, ap_def;
603 		struct iwl_mvm_phy_ctxt *phy_ctxt;
604 		u8 chains_static, chains_dynamic;
605 
606 		rcu_read_lock();
607 		chanctx_conf = rcu_dereference(link_conf->chanctx_conf);
608 		if (!chanctx_conf) {
609 			rcu_read_unlock();
610 			continue;
611 		}
612 		/* A command can't be sent with RCU lock held, so copy
613 		 * everything here and use it after unlocking
614 		 */
615 		min_def = chanctx_conf->min_def;
616 		ap_def = chanctx_conf->ap;
617 		chains_static = chanctx_conf->rx_chains_static;
618 		chains_dynamic = chanctx_conf->rx_chains_dynamic;
619 		rcu_read_unlock();
620 
621 		phy_ctxt = mvmvif->link[link_id]->phy_ctxt;
622 		if (!phy_ctxt)
623 			continue;
624 
625 		ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &min_def, &ap_def,
626 					       chains_static, chains_dynamic);
627 	}
628 
629 	mutex_unlock(&mvm->mutex);
630 
631 	return ret ?: count;
632 }
633 
634 static ssize_t iwl_dbgfs_rx_phyinfo_read(struct file *file,
635 					 char __user *user_buf,
636 					 size_t count, loff_t *ppos)
637 {
638 	struct ieee80211_vif *vif = file->private_data;
639 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
640 	char buf[8];
641 	int len;
642 
643 	len = scnprintf(buf, sizeof(buf), "0x%04x\n",
644 			mvmvif->mvm->dbgfs_rx_phyinfo);
645 
646 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
647 }
648 
649 static void iwl_dbgfs_quota_check(void *data, u8 *mac,
650 				  struct ieee80211_vif *vif)
651 {
652 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
653 	int *ret = data;
654 
655 	if (mvmvif->dbgfs_quota_min)
656 		*ret = -EINVAL;
657 }
658 
659 static ssize_t iwl_dbgfs_quota_min_write(struct ieee80211_vif *vif, char *buf,
660 					 size_t count, loff_t *ppos)
661 {
662 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
663 	struct iwl_mvm *mvm = mvmvif->mvm;
664 	u16 value;
665 	int ret;
666 
667 	ret = kstrtou16(buf, 0, &value);
668 	if (ret)
669 		return ret;
670 
671 	if (value > 95)
672 		return -EINVAL;
673 
674 	mutex_lock(&mvm->mutex);
675 
676 	mvmvif->dbgfs_quota_min = 0;
677 	ieee80211_iterate_interfaces(mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
678 				     iwl_dbgfs_quota_check, &ret);
679 	if (ret == 0) {
680 		mvmvif->dbgfs_quota_min = value;
681 		iwl_mvm_update_quotas(mvm, false, NULL);
682 	}
683 	mutex_unlock(&mvm->mutex);
684 
685 	return ret ?: count;
686 }
687 
688 static ssize_t iwl_dbgfs_quota_min_read(struct file *file,
689 					char __user *user_buf,
690 					size_t count, loff_t *ppos)
691 {
692 	struct ieee80211_vif *vif = file->private_data;
693 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
694 	char buf[10];
695 	int len;
696 
697 	len = scnprintf(buf, sizeof(buf), "%d\n", mvmvif->dbgfs_quota_min);
698 
699 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
700 }
701 
702 static ssize_t iwl_dbgfs_max_tx_op_write(struct ieee80211_vif *vif, char *buf,
703 					 size_t count, loff_t *ppos)
704 {
705 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
706 	struct iwl_mvm *mvm = mvmvif->mvm;
707 	u16 value;
708 	int ret;
709 
710 	ret = kstrtou16(buf, 0, &value);
711 	if (ret)
712 		return ret;
713 
714 	mutex_lock(&mvm->mutex);
715 	mvmvif->max_tx_op = value;
716 	mutex_unlock(&mvm->mutex);
717 
718 	return count;
719 }
720 
721 static ssize_t iwl_dbgfs_max_tx_op_read(struct file *file,
722 					char __user *user_buf,
723 					size_t count, loff_t *ppos)
724 {
725 	struct ieee80211_vif *vif = file->private_data;
726 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
727 	struct iwl_mvm *mvm = mvmvif->mvm;
728 	char buf[10];
729 	int len;
730 
731 	mutex_lock(&mvm->mutex);
732 	len = scnprintf(buf, sizeof(buf), "%hu\n", mvmvif->max_tx_op);
733 	mutex_unlock(&mvm->mutex);
734 
735 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
736 }
737 
738 static ssize_t iwl_dbgfs_int_mlo_scan_write(struct ieee80211_vif *vif,
739 					    char *buf, size_t count,
740 					    loff_t *ppos)
741 {
742 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
743 	struct iwl_mvm *mvm = mvmvif->mvm;
744 	u32 action;
745 	int ret;
746 
747 	if (!vif->cfg.assoc || !ieee80211_vif_is_mld(vif))
748 		return -EINVAL;
749 
750 	if (kstrtou32(buf, 0, &action))
751 		return -EINVAL;
752 
753 	mutex_lock(&mvm->mutex);
754 
755 	if (!action) {
756 		ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_INT_MLO, false);
757 	} else if (action == 1) {
758 		ret = iwl_mvm_int_mlo_scan(mvm, vif);
759 	} else {
760 		ret = -EINVAL;
761 	}
762 
763 	mutex_unlock(&mvm->mutex);
764 
765 	return ret ?: count;
766 }
767 
768 static ssize_t iwl_dbgfs_esr_disable_reason_read(struct file *file,
769 						 char __user *user_buf,
770 						 size_t count, loff_t *ppos)
771 {
772 	struct ieee80211_vif *vif = file->private_data;
773 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
774 	struct iwl_mvm *mvm = mvmvif->mvm;
775 	unsigned long esr_mask;
776 	char *buf;
777 	int bufsz, pos, i;
778 	ssize_t rv;
779 
780 	mutex_lock(&mvm->mutex);
781 	esr_mask = mvmvif->esr_disable_reason;
782 	mutex_unlock(&mvm->mutex);
783 
784 	bufsz = hweight32(esr_mask) * 32 + 40;
785 	buf = kmalloc(bufsz, GFP_KERNEL);
786 	if (!buf)
787 		return -ENOMEM;
788 
789 	pos = scnprintf(buf, bufsz, "EMLSR state: '0x%lx'\nreasons:\n",
790 			esr_mask);
791 	for_each_set_bit(i, &esr_mask, BITS_PER_LONG)
792 		pos += scnprintf(buf + pos, bufsz - pos, " - %s\n",
793 				 iwl_get_esr_state_string(BIT(i)));
794 
795 	rv = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
796 	kfree(buf);
797 	return rv;
798 }
799 
800 static ssize_t iwl_dbgfs_esr_disable_reason_write(struct ieee80211_vif *vif,
801 						  char *buf, size_t count,
802 						  loff_t *ppos)
803 {
804 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
805 	struct iwl_mvm *mvm = mvmvif->mvm;
806 	u32 reason;
807 	u8 block;
808 	int ret;
809 
810 	ret = sscanf(buf, "%u %hhu", &reason, &block);
811 	if (ret < 0)
812 		return ret;
813 
814 	if (hweight16(reason) != 1 || !(reason & IWL_MVM_BLOCK_ESR_REASONS))
815 		return -EINVAL;
816 
817 	mutex_lock(&mvm->mutex);
818 	if (block)
819 		iwl_mvm_block_esr(mvm, vif, reason,
820 				  iwl_mvm_get_primary_link(vif));
821 	else
822 		iwl_mvm_unblock_esr(mvm, vif, reason);
823 	mutex_unlock(&mvm->mutex);
824 
825 	return count;
826 }
827 
828 #define MVM_DEBUGFS_WRITE_FILE_OPS(name, bufsz) \
829 	_MVM_DEBUGFS_WRITE_FILE_OPS(name, bufsz, struct ieee80211_vif)
830 #define MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz) \
831 	_MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz, struct ieee80211_vif)
832 #define MVM_DEBUGFS_ADD_FILE_VIF(name, parent, mode) do {		\
833 		debugfs_create_file(#name, mode, parent, vif,		\
834 				    &iwl_dbgfs_##name##_ops);		\
835 	} while (0)
836 
837 MVM_DEBUGFS_READ_FILE_OPS(mac_params);
838 MVM_DEBUGFS_READ_FILE_OPS(tx_pwr_lmt);
839 MVM_DEBUGFS_READ_WRITE_FILE_OPS(pm_params, 32);
840 MVM_DEBUGFS_READ_WRITE_FILE_OPS(bf_params, 256);
841 MVM_DEBUGFS_READ_WRITE_FILE_OPS(low_latency, 10);
842 MVM_DEBUGFS_WRITE_FILE_OPS(low_latency_force, 10);
843 MVM_DEBUGFS_READ_WRITE_FILE_OPS(uapsd_misbehaving, 20);
844 MVM_DEBUGFS_READ_WRITE_FILE_OPS(rx_phyinfo, 10);
845 MVM_DEBUGFS_READ_WRITE_FILE_OPS(quota_min, 32);
846 MVM_DEBUGFS_READ_FILE_OPS(os_device_timediff);
847 MVM_DEBUGFS_READ_WRITE_FILE_OPS(max_tx_op, 10);
848 MVM_DEBUGFS_WRITE_FILE_OPS(int_mlo_scan, 32);
849 MVM_DEBUGFS_READ_WRITE_FILE_OPS(esr_disable_reason, 32);
850 
851 void iwl_mvm_vif_add_debugfs(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
852 {
853 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
854 	struct dentry *dbgfs_dir = vif->debugfs_dir;
855 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
856 
857 	mvmvif->dbgfs_dir = debugfs_create_dir("iwlmvm", dbgfs_dir);
858 	if (IS_ERR_OR_NULL(mvmvif->dbgfs_dir)) {
859 		IWL_ERR(mvm, "Failed to create debugfs directory under %pd\n",
860 			dbgfs_dir);
861 		return;
862 	}
863 
864 	if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM &&
865 	    ((vif->type == NL80211_IFTYPE_STATION && !vif->p2p) ||
866 	     (vif->type == NL80211_IFTYPE_STATION && vif->p2p)))
867 		MVM_DEBUGFS_ADD_FILE_VIF(pm_params, mvmvif->dbgfs_dir, 0600);
868 
869 	MVM_DEBUGFS_ADD_FILE_VIF(tx_pwr_lmt, mvmvif->dbgfs_dir, 0400);
870 	MVM_DEBUGFS_ADD_FILE_VIF(mac_params, mvmvif->dbgfs_dir, 0400);
871 	MVM_DEBUGFS_ADD_FILE_VIF(low_latency, mvmvif->dbgfs_dir, 0600);
872 	MVM_DEBUGFS_ADD_FILE_VIF(low_latency_force, mvmvif->dbgfs_dir, 0600);
873 	MVM_DEBUGFS_ADD_FILE_VIF(uapsd_misbehaving, mvmvif->dbgfs_dir, 0600);
874 	MVM_DEBUGFS_ADD_FILE_VIF(rx_phyinfo, mvmvif->dbgfs_dir, 0600);
875 	MVM_DEBUGFS_ADD_FILE_VIF(quota_min, mvmvif->dbgfs_dir, 0600);
876 	MVM_DEBUGFS_ADD_FILE_VIF(os_device_timediff, mvmvif->dbgfs_dir, 0400);
877 	MVM_DEBUGFS_ADD_FILE_VIF(max_tx_op, mvmvif->dbgfs_dir, 0600);
878 	debugfs_create_bool("ftm_unprotected", 0200, mvmvif->dbgfs_dir,
879 			    &mvmvif->ftm_unprotected);
880 	MVM_DEBUGFS_ADD_FILE_VIF(int_mlo_scan, mvmvif->dbgfs_dir, 0200);
881 	MVM_DEBUGFS_ADD_FILE_VIF(esr_disable_reason, mvmvif->dbgfs_dir, 0600);
882 
883 	if (vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
884 	    mvmvif == mvm->bf_allowed_vif)
885 		MVM_DEBUGFS_ADD_FILE_VIF(bf_params, mvmvif->dbgfs_dir, 0600);
886 }
887 
888 void iwl_mvm_vif_dbgfs_add_link(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
889 {
890 	struct dentry *dbgfs_dir = vif->debugfs_dir;
891 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
892 	char buf[3 * 3 + 11 + (NL80211_WIPHY_NAME_MAXLEN + 1) +
893 		 (7 + IFNAMSIZ + 1) + 6 + 1];
894 	char name[7 + IFNAMSIZ + 1];
895 
896 	/* this will happen in monitor mode */
897 	if (!dbgfs_dir)
898 		return;
899 
900 #if defined(__linux__)
901 	/*
902 	 * Create symlink for convenience pointing to interface specific
903 	 * debugfs entries for the driver. For example, under
904 	 * /sys/kernel/debug/iwlwifi/0000\:02\:00.0/iwlmvm/
905 	 * find
906 	 * netdev:wlan0 -> ../../../ieee80211/phy0/netdev:wlan0/iwlmvm/
907 	 */
908 	snprintf(name, sizeof(name), "%pd", dbgfs_dir);
909 	snprintf(buf, sizeof(buf), "../../../%pd3/iwlmvm", dbgfs_dir);
910 
911 	mvmvif->dbgfs_slink =
912 		debugfs_create_symlink(name, mvm->debugfs_dir, buf);
913 #endif
914 }
915 
916 void iwl_mvm_vif_dbgfs_rm_link(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
917 {
918 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
919 
920 	debugfs_remove(mvmvif->dbgfs_slink);
921 	mvmvif->dbgfs_slink = NULL;
922 }
923 
924 #define MVM_DEBUGFS_WRITE_LINK_FILE_OPS(name, bufsz)			\
925 	_MVM_DEBUGFS_WRITE_FILE_OPS(link_##name, bufsz,			\
926 				    struct ieee80211_bss_conf)
927 #define MVM_DEBUGFS_READ_WRITE_LINK_FILE_OPS(name, bufsz)		\
928 	_MVM_DEBUGFS_READ_WRITE_FILE_OPS(link_##name, bufsz,		\
929 					 struct ieee80211_bss_conf)
930 #define MVM_DEBUGFS_ADD_LINK_FILE(name, parent, mode)			\
931 	debugfs_create_file(#name, mode, parent, link_conf,		\
932 			    &iwl_dbgfs_link_##name##_ops)
933 
934 static void iwl_mvm_debugfs_add_link_files(struct ieee80211_vif *vif,
935 					   struct ieee80211_bss_conf *link_conf,
936 					   struct dentry *mvm_dir)
937 {
938 	/* Add per-link files here*/
939 }
940 
941 void iwl_mvm_link_add_debugfs(struct ieee80211_hw *hw,
942 			      struct ieee80211_vif *vif,
943 			      struct ieee80211_bss_conf *link_conf,
944 			      struct dentry *dir)
945 {
946 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
947 	struct iwl_mvm *mvm = mvmvif->mvm;
948 	unsigned int link_id = link_conf->link_id;
949 	struct iwl_mvm_vif_link_info *link_info = mvmvif->link[link_id];
950 	struct dentry *mvm_dir;
951 
952 	if (WARN_ON(!link_info) || !dir)
953 		return;
954 
955 	if (dir == vif->debugfs_dir) {
956 		WARN_ON(!mvmvif->dbgfs_dir);
957 		mvm_dir = mvmvif->dbgfs_dir;
958 	} else {
959 		mvm_dir = debugfs_create_dir("iwlmvm", dir);
960 		if (IS_ERR_OR_NULL(mvm_dir)) {
961 			IWL_ERR(mvm, "Failed to create debugfs directory under %pd\n",
962 				dir);
963 			return;
964 		}
965 	}
966 
967 	iwl_mvm_debugfs_add_link_files(vif, link_conf, mvm_dir);
968 }
969