xref: /freebsd/sys/contrib/dev/iwlwifi/mvm/debugfs.c (revision a64729f5077d77e13b9497cb33ecb3c82e606ee8)
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 <linux/vmalloc.h>
8 #include <linux/err.h>
9 #include <linux/ieee80211.h>
10 #include <linux/netdevice.h>
11 #include <linux/dmi.h>
12 
13 #include "mvm.h"
14 #include "sta.h"
15 #include "iwl-io.h"
16 #include "debugfs.h"
17 #include "iwl-modparams.h"
18 #include "iwl-drv.h"
19 #include "fw/error-dump.h"
20 #include "fw/api/phy-ctxt.h"
21 
22 static ssize_t iwl_dbgfs_ctdp_budget_read(struct file *file,
23 					  char __user *user_buf,
24 					  size_t count, loff_t *ppos)
25 {
26 	struct iwl_mvm *mvm = file->private_data;
27 	char buf[16];
28 	int pos, budget;
29 
30 	if (!iwl_mvm_is_ctdp_supported(mvm))
31 		return -EOPNOTSUPP;
32 
33 	if (!iwl_mvm_firmware_running(mvm) ||
34 	    mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
35 		return -EIO;
36 
37 	mutex_lock(&mvm->mutex);
38 	budget = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_REPORT, 0);
39 	mutex_unlock(&mvm->mutex);
40 
41 	if (budget < 0)
42 		return budget;
43 
44 	pos = scnprintf(buf, sizeof(buf), "%d\n", budget);
45 
46 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
47 }
48 
49 static ssize_t iwl_dbgfs_stop_ctdp_write(struct iwl_mvm *mvm, char *buf,
50 					 size_t count, loff_t *ppos)
51 {
52 	int ret;
53 
54 	if (!iwl_mvm_is_ctdp_supported(mvm))
55 		return -EOPNOTSUPP;
56 
57 	if (!iwl_mvm_firmware_running(mvm) ||
58 	    mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
59 		return -EIO;
60 
61 	mutex_lock(&mvm->mutex);
62 	ret = iwl_mvm_ctdp_command(mvm, CTDP_CMD_OPERATION_STOP, 0);
63 	mutex_unlock(&mvm->mutex);
64 
65 	return ret ?: count;
66 }
67 
68 static ssize_t iwl_dbgfs_force_ctkill_write(struct iwl_mvm *mvm, char *buf,
69 					    size_t count, loff_t *ppos)
70 {
71 	if (!iwl_mvm_firmware_running(mvm) ||
72 	    mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
73 		return -EIO;
74 
75 	iwl_mvm_enter_ctkill(mvm);
76 
77 	return count;
78 }
79 
80 static ssize_t iwl_dbgfs_tx_flush_write(struct iwl_mvm *mvm, char *buf,
81 					size_t count, loff_t *ppos)
82 {
83 	int ret;
84 	u32 flush_arg;
85 
86 	if (!iwl_mvm_firmware_running(mvm) ||
87 	    mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
88 		return -EIO;
89 
90 	if (kstrtou32(buf, 0, &flush_arg))
91 		return -EINVAL;
92 
93 	if (iwl_mvm_has_new_tx_api(mvm)) {
94 		IWL_DEBUG_TX_QUEUES(mvm,
95 				    "FLUSHING all tids queues on sta_id = %d\n",
96 				    flush_arg);
97 		mutex_lock(&mvm->mutex);
98 		ret = iwl_mvm_flush_sta_tids(mvm, flush_arg, 0xFFFF)
99 			? : count;
100 		mutex_unlock(&mvm->mutex);
101 		return ret;
102 	}
103 
104 	IWL_DEBUG_TX_QUEUES(mvm, "FLUSHING queues mask to flush = 0x%x\n",
105 			    flush_arg);
106 
107 	mutex_lock(&mvm->mutex);
108 	ret =  iwl_mvm_flush_tx_path(mvm, flush_arg) ? : count;
109 	mutex_unlock(&mvm->mutex);
110 
111 	return ret;
112 }
113 
114 static ssize_t iwl_dbgfs_sta_drain_write(struct iwl_mvm *mvm, char *buf,
115 					 size_t count, loff_t *ppos)
116 {
117 	struct iwl_mvm_sta *mvmsta;
118 	int sta_id, drain, ret;
119 
120 	if (!iwl_mvm_firmware_running(mvm) ||
121 	    mvm->fwrt.cur_fw_img != IWL_UCODE_REGULAR)
122 		return -EIO;
123 
124 	if (sscanf(buf, "%d %d", &sta_id, &drain) != 2)
125 		return -EINVAL;
126 	if (sta_id < 0 || sta_id >= mvm->fw->ucode_capa.num_stations)
127 		return -EINVAL;
128 	if (drain < 0 || drain > 1)
129 		return -EINVAL;
130 
131 	mutex_lock(&mvm->mutex);
132 
133 	mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
134 
135 	if (!mvmsta)
136 		ret = -ENOENT;
137 	else
138 		ret = iwl_mvm_drain_sta(mvm, mvmsta, drain) ? : count;
139 
140 	mutex_unlock(&mvm->mutex);
141 
142 	return ret;
143 }
144 
145 static ssize_t iwl_dbgfs_sram_read(struct file *file, char __user *user_buf,
146 				   size_t count, loff_t *ppos)
147 {
148 	struct iwl_mvm *mvm = file->private_data;
149 	const struct fw_img *img;
150 	unsigned int ofs, len;
151 	size_t ret;
152 	u8 *ptr;
153 
154 	if (!iwl_mvm_firmware_running(mvm))
155 		return -EINVAL;
156 
157 	/* default is to dump the entire data segment */
158 	img = &mvm->fw->img[mvm->fwrt.cur_fw_img];
159 	ofs = img->sec[IWL_UCODE_SECTION_DATA].offset;
160 	len = img->sec[IWL_UCODE_SECTION_DATA].len;
161 
162 	if (mvm->dbgfs_sram_len) {
163 		ofs = mvm->dbgfs_sram_offset;
164 		len = mvm->dbgfs_sram_len;
165 	}
166 
167 	ptr = kzalloc(len, GFP_KERNEL);
168 	if (!ptr)
169 		return -ENOMEM;
170 
171 	iwl_trans_read_mem_bytes(mvm->trans, ofs, ptr, len);
172 
173 	ret = simple_read_from_buffer(user_buf, count, ppos, ptr, len);
174 
175 	kfree(ptr);
176 
177 	return ret;
178 }
179 
180 static ssize_t iwl_dbgfs_sram_write(struct iwl_mvm *mvm, char *buf,
181 				    size_t count, loff_t *ppos)
182 {
183 	const struct fw_img *img;
184 	u32 offset, len;
185 	u32 img_offset, img_len;
186 
187 	if (!iwl_mvm_firmware_running(mvm))
188 		return -EINVAL;
189 
190 	img = &mvm->fw->img[mvm->fwrt.cur_fw_img];
191 	img_offset = img->sec[IWL_UCODE_SECTION_DATA].offset;
192 	img_len = img->sec[IWL_UCODE_SECTION_DATA].len;
193 
194 	if (sscanf(buf, "%x,%x", &offset, &len) == 2) {
195 		if ((offset & 0x3) || (len & 0x3))
196 			return -EINVAL;
197 
198 		if (offset + len > img_offset + img_len)
199 			return -EINVAL;
200 
201 		mvm->dbgfs_sram_offset = offset;
202 		mvm->dbgfs_sram_len = len;
203 	} else {
204 		mvm->dbgfs_sram_offset = 0;
205 		mvm->dbgfs_sram_len = 0;
206 	}
207 
208 	return count;
209 }
210 
211 static ssize_t iwl_dbgfs_set_nic_temperature_read(struct file *file,
212 						  char __user *user_buf,
213 						  size_t count, loff_t *ppos)
214 {
215 	struct iwl_mvm *mvm = file->private_data;
216 	char buf[16];
217 	int pos;
218 
219 	if (!mvm->temperature_test)
220 		pos = scnprintf(buf, sizeof(buf), "disabled\n");
221 	else
222 		pos = scnprintf(buf, sizeof(buf), "%d\n", mvm->temperature);
223 
224 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
225 }
226 
227 /*
228  * Set NIC Temperature
229  * Cause the driver to ignore the actual NIC temperature reported by the FW
230  * Enable: any value between IWL_MVM_DEBUG_SET_TEMPERATURE_MIN -
231  * IWL_MVM_DEBUG_SET_TEMPERATURE_MAX
232  * Disable: IWL_MVM_DEBUG_SET_TEMPERATURE_DISABLE
233  */
234 static ssize_t iwl_dbgfs_set_nic_temperature_write(struct iwl_mvm *mvm,
235 						   char *buf, size_t count,
236 						   loff_t *ppos)
237 {
238 	int temperature;
239 
240 	if (!iwl_mvm_firmware_running(mvm) && !mvm->temperature_test)
241 		return -EIO;
242 
243 	if (kstrtoint(buf, 10, &temperature))
244 		return -EINVAL;
245 	/* not a legal temperature */
246 	if ((temperature > IWL_MVM_DEBUG_SET_TEMPERATURE_MAX &&
247 	     temperature != IWL_MVM_DEBUG_SET_TEMPERATURE_DISABLE) ||
248 	    temperature < IWL_MVM_DEBUG_SET_TEMPERATURE_MIN)
249 		return -EINVAL;
250 
251 	mutex_lock(&mvm->mutex);
252 	if (temperature == IWL_MVM_DEBUG_SET_TEMPERATURE_DISABLE) {
253 		if (!mvm->temperature_test)
254 			goto out;
255 
256 		mvm->temperature_test = false;
257 		/* Since we can't read the temp while awake, just set
258 		 * it to zero until we get the next RX stats from the
259 		 * firmware.
260 		 */
261 		mvm->temperature = 0;
262 	} else {
263 		mvm->temperature_test = true;
264 		mvm->temperature = temperature;
265 	}
266 	IWL_DEBUG_TEMP(mvm, "%sabling debug set temperature (temp = %d)\n",
267 		       mvm->temperature_test ? "En" : "Dis",
268 		       mvm->temperature);
269 	/* handle the temperature change */
270 	iwl_mvm_tt_handler(mvm);
271 
272 out:
273 	mutex_unlock(&mvm->mutex);
274 
275 	return count;
276 }
277 
278 static ssize_t iwl_dbgfs_nic_temp_read(struct file *file,
279 				       char __user *user_buf,
280 				       size_t count, loff_t *ppos)
281 {
282 	struct iwl_mvm *mvm = file->private_data;
283 	char buf[16];
284 	int pos, ret;
285 	s32 temp;
286 
287 	if (!iwl_mvm_firmware_running(mvm))
288 		return -EIO;
289 
290 	mutex_lock(&mvm->mutex);
291 	ret = iwl_mvm_get_temp(mvm, &temp);
292 	mutex_unlock(&mvm->mutex);
293 
294 	if (ret)
295 		return -EIO;
296 
297 	pos = scnprintf(buf, sizeof(buf), "%d\n", temp);
298 
299 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
300 }
301 
302 #ifdef CONFIG_ACPI
303 static ssize_t iwl_dbgfs_sar_geo_profile_read(struct file *file,
304 					      char __user *user_buf,
305 					      size_t count, loff_t *ppos)
306 {
307 	struct iwl_mvm *mvm = file->private_data;
308 	char buf[256];
309 	int pos = 0;
310 	int bufsz = sizeof(buf);
311 	int tbl_idx;
312 
313 	if (!iwl_mvm_firmware_running(mvm))
314 		return -EIO;
315 
316 	mutex_lock(&mvm->mutex);
317 	tbl_idx = iwl_mvm_get_sar_geo_profile(mvm);
318 	if (tbl_idx < 0) {
319 		mutex_unlock(&mvm->mutex);
320 		return tbl_idx;
321 	}
322 
323 	if (!tbl_idx) {
324 		pos = scnprintf(buf, bufsz,
325 				"SAR geographic profile disabled\n");
326 	} else {
327 		pos += scnprintf(buf + pos, bufsz - pos,
328 				 "Use geographic profile %d\n", tbl_idx);
329 		pos += scnprintf(buf + pos, bufsz - pos,
330 				 "2.4GHz:\n\tChain A offset: %u dBm\n\tChain B offset: %u dBm\n\tmax tx power: %u dBm\n",
331 				 mvm->fwrt.geo_profiles[tbl_idx - 1].bands[0].chains[0],
332 				 mvm->fwrt.geo_profiles[tbl_idx - 1].bands[0].chains[1],
333 				 mvm->fwrt.geo_profiles[tbl_idx - 1].bands[0].max);
334 		pos += scnprintf(buf + pos, bufsz - pos,
335 				 "5.2GHz:\n\tChain A offset: %u dBm\n\tChain B offset: %u dBm\n\tmax tx power: %u dBm\n",
336 				 mvm->fwrt.geo_profiles[tbl_idx - 1].bands[1].chains[0],
337 				 mvm->fwrt.geo_profiles[tbl_idx - 1].bands[1].chains[1],
338 				 mvm->fwrt.geo_profiles[tbl_idx - 1].bands[1].max);
339 	}
340 	mutex_unlock(&mvm->mutex);
341 
342 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
343 }
344 
345 static ssize_t iwl_dbgfs_wifi_6e_enable_read(struct file *file,
346 					     char __user *user_buf,
347 					     size_t count, loff_t *ppos)
348 {
349 	struct iwl_mvm *mvm = file->private_data;
350 	int err, pos;
351 	char buf[12];
352 	u32 value;
353 
354 	err = iwl_acpi_get_dsm_u32(mvm->fwrt.dev, 0,
355 				   DSM_FUNC_ENABLE_6E,
356 				   &iwl_guid, &value);
357 	if (err)
358 		return err;
359 
360 	pos = sprintf(buf, "0x%08x\n", value);
361 
362 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
363 }
364 #endif
365 
366 static ssize_t iwl_dbgfs_stations_read(struct file *file, char __user *user_buf,
367 				       size_t count, loff_t *ppos)
368 {
369 	struct iwl_mvm *mvm = file->private_data;
370 	struct ieee80211_sta *sta;
371 	char buf[400];
372 	int i, pos = 0, bufsz = sizeof(buf);
373 
374 	mutex_lock(&mvm->mutex);
375 
376 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
377 		pos += scnprintf(buf + pos, bufsz - pos, "%.2d: ", i);
378 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
379 						lockdep_is_held(&mvm->mutex));
380 		if (!sta)
381 			pos += scnprintf(buf + pos, bufsz - pos, "N/A\n");
382 		else if (IS_ERR(sta))
383 			pos += scnprintf(buf + pos, bufsz - pos, "%ld\n",
384 					 PTR_ERR(sta));
385 		else
386 			pos += scnprintf(buf + pos, bufsz - pos, "%pM\n",
387 					 sta->addr);
388 	}
389 
390 	mutex_unlock(&mvm->mutex);
391 
392 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
393 }
394 
395 static ssize_t iwl_dbgfs_rs_data_read(struct ieee80211_link_sta *link_sta,
396 				      struct iwl_mvm_sta *mvmsta,
397 				      struct iwl_mvm *mvm,
398 				      struct iwl_mvm_link_sta *mvm_link_sta,
399 				      char __user *user_buf,
400 				      size_t count, loff_t *ppos)
401 {
402 	struct iwl_lq_sta_rs_fw *lq_sta = &mvm_link_sta->lq_sta.rs_fw;
403 	static const size_t bufsz = 2048;
404 	char *buff;
405 	int desc = 0;
406 	ssize_t ret;
407 
408 	buff = kmalloc(bufsz, GFP_KERNEL);
409 	if (!buff)
410 		return -ENOMEM;
411 
412 	desc += scnprintf(buff + desc, bufsz - desc, "sta_id %d\n",
413 			  lq_sta->pers.sta_id);
414 	desc += scnprintf(buff + desc, bufsz - desc,
415 			  "fixed rate 0x%X\n",
416 			  lq_sta->pers.dbg_fixed_rate);
417 	desc += scnprintf(buff + desc, bufsz - desc,
418 			  "A-MPDU size limit %d\n",
419 			  lq_sta->pers.dbg_agg_frame_count_lim);
420 	desc += scnprintf(buff + desc, bufsz - desc,
421 			  "valid_tx_ant %s%s\n",
422 		(iwl_mvm_get_valid_tx_ant(mvm) & ANT_A) ? "ANT_A," : "",
423 		(iwl_mvm_get_valid_tx_ant(mvm) & ANT_B) ? "ANT_B," : "");
424 	desc += scnprintf(buff + desc, bufsz - desc,
425 			  "last tx rate=0x%X ",
426 			  lq_sta->last_rate_n_flags);
427 
428 	desc += rs_pretty_print_rate(buff + desc, bufsz - desc,
429 				     lq_sta->last_rate_n_flags);
430 	if (desc < bufsz - 1)
431 		buff[desc++] = '\n';
432 
433 	ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
434 	kfree(buff);
435 	return ret;
436 }
437 
438 static ssize_t iwl_dbgfs_amsdu_len_write(struct ieee80211_link_sta *link_sta,
439 					 struct iwl_mvm_sta *mvmsta,
440 					 struct iwl_mvm *mvm,
441 					 struct iwl_mvm_link_sta *mvm_link_sta,
442 					 char *buf, size_t count,
443 					 loff_t *ppos)
444 {
445 	int i;
446 	u16 amsdu_len;
447 
448 	if (kstrtou16(buf, 0, &amsdu_len))
449 		return -EINVAL;
450 
451 	/* only change from debug set <-> debug unset */
452 	if (amsdu_len && mvm_link_sta->orig_amsdu_len)
453 		return -EBUSY;
454 
455 	if (amsdu_len) {
456 		mvm_link_sta->orig_amsdu_len = link_sta->agg.max_amsdu_len;
457 		link_sta->agg.max_amsdu_len = amsdu_len;
458 		link_sta->agg.max_amsdu_len = amsdu_len;
459 		for (i = 0; i < ARRAY_SIZE(link_sta->agg.max_tid_amsdu_len); i++)
460 			link_sta->agg.max_tid_amsdu_len[i] = amsdu_len;
461 	} else {
462 		link_sta->agg.max_amsdu_len = mvm_link_sta->orig_amsdu_len;
463 		mvm_link_sta->orig_amsdu_len = 0;
464 	}
465 
466 	ieee80211_sta_recalc_aggregates(link_sta->sta);
467 
468 	return count;
469 }
470 
471 static ssize_t iwl_dbgfs_amsdu_len_read(struct ieee80211_link_sta *link_sta,
472 					struct iwl_mvm_sta *mvmsta,
473 					struct iwl_mvm *mvm,
474 					struct iwl_mvm_link_sta *mvm_link_sta,
475 					char __user *user_buf,
476 					size_t count, loff_t *ppos)
477 {
478 	char buf[32];
479 	int pos;
480 
481 	pos = scnprintf(buf, sizeof(buf), "current %d ",
482 			link_sta->agg.max_amsdu_len);
483 	pos += scnprintf(buf + pos, sizeof(buf) - pos, "stored %d\n",
484 			 mvm_link_sta->orig_amsdu_len);
485 
486 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
487 }
488 
489 static ssize_t iwl_dbgfs_disable_power_off_read(struct file *file,
490 						char __user *user_buf,
491 						size_t count, loff_t *ppos)
492 {
493 	struct iwl_mvm *mvm = file->private_data;
494 	char buf[64];
495 	int bufsz = sizeof(buf);
496 	int pos = 0;
497 
498 	pos += scnprintf(buf+pos, bufsz-pos, "disable_power_off_d0=%d\n",
499 			 mvm->disable_power_off);
500 	pos += scnprintf(buf+pos, bufsz-pos, "disable_power_off_d3=%d\n",
501 			 mvm->disable_power_off_d3);
502 
503 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
504 }
505 
506 static ssize_t iwl_dbgfs_disable_power_off_write(struct iwl_mvm *mvm, char *buf,
507 						 size_t count, loff_t *ppos)
508 {
509 	int ret, val;
510 
511 	if (!iwl_mvm_firmware_running(mvm))
512 		return -EIO;
513 
514 	if (!strncmp("disable_power_off_d0=", buf, 21)) {
515 		if (sscanf(buf + 21, "%d", &val) != 1)
516 			return -EINVAL;
517 		mvm->disable_power_off = val;
518 	} else if (!strncmp("disable_power_off_d3=", buf, 21)) {
519 		if (sscanf(buf + 21, "%d", &val) != 1)
520 			return -EINVAL;
521 		mvm->disable_power_off_d3 = val;
522 	} else {
523 		return -EINVAL;
524 	}
525 
526 	mutex_lock(&mvm->mutex);
527 	ret = iwl_mvm_power_update_device(mvm);
528 	mutex_unlock(&mvm->mutex);
529 
530 	return ret ?: count;
531 }
532 
533 static
534 int iwl_mvm_coex_dump_mbox(struct iwl_bt_coex_profile_notif *notif, char *buf,
535 			   int pos, int bufsz)
536 {
537 	pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw0:\n");
538 
539 	BT_MBOX_PRINT(0, LE_SLAVE_LAT, false);
540 	BT_MBOX_PRINT(0, LE_PROF1, false);
541 	BT_MBOX_PRINT(0, LE_PROF2, false);
542 	BT_MBOX_PRINT(0, LE_PROF_OTHER, false);
543 	BT_MBOX_PRINT(0, CHL_SEQ_N, false);
544 	BT_MBOX_PRINT(0, INBAND_S, false);
545 	BT_MBOX_PRINT(0, LE_MIN_RSSI, false);
546 	BT_MBOX_PRINT(0, LE_SCAN, false);
547 	BT_MBOX_PRINT(0, LE_ADV, false);
548 	BT_MBOX_PRINT(0, LE_MAX_TX_POWER, false);
549 	BT_MBOX_PRINT(0, OPEN_CON_1, true);
550 
551 	pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw1:\n");
552 
553 	BT_MBOX_PRINT(1, BR_MAX_TX_POWER, false);
554 	BT_MBOX_PRINT(1, IP_SR, false);
555 	BT_MBOX_PRINT(1, LE_MSTR, false);
556 	BT_MBOX_PRINT(1, AGGR_TRFC_LD, false);
557 	BT_MBOX_PRINT(1, MSG_TYPE, false);
558 	BT_MBOX_PRINT(1, SSN, true);
559 
560 	pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw2:\n");
561 
562 	BT_MBOX_PRINT(2, SNIFF_ACT, false);
563 	BT_MBOX_PRINT(2, PAG, false);
564 	BT_MBOX_PRINT(2, INQUIRY, false);
565 	BT_MBOX_PRINT(2, CONN, false);
566 	BT_MBOX_PRINT(2, SNIFF_INTERVAL, false);
567 	BT_MBOX_PRINT(2, DISC, false);
568 	BT_MBOX_PRINT(2, SCO_TX_ACT, false);
569 	BT_MBOX_PRINT(2, SCO_RX_ACT, false);
570 	BT_MBOX_PRINT(2, ESCO_RE_TX, false);
571 	BT_MBOX_PRINT(2, SCO_DURATION, true);
572 
573 	pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw3:\n");
574 
575 	BT_MBOX_PRINT(3, SCO_STATE, false);
576 	BT_MBOX_PRINT(3, SNIFF_STATE, false);
577 	BT_MBOX_PRINT(3, A2DP_STATE, false);
578 	BT_MBOX_PRINT(3, A2DP_SRC, false);
579 	BT_MBOX_PRINT(3, ACL_STATE, false);
580 	BT_MBOX_PRINT(3, MSTR_STATE, false);
581 	BT_MBOX_PRINT(3, OBX_STATE, false);
582 	BT_MBOX_PRINT(3, OPEN_CON_2, false);
583 	BT_MBOX_PRINT(3, TRAFFIC_LOAD, false);
584 	BT_MBOX_PRINT(3, CHL_SEQN_LSB, false);
585 	BT_MBOX_PRINT(3, INBAND_P, false);
586 	BT_MBOX_PRINT(3, MSG_TYPE_2, false);
587 	BT_MBOX_PRINT(3, SSN_2, false);
588 	BT_MBOX_PRINT(3, UPDATE_REQUEST, true);
589 
590 	return pos;
591 }
592 
593 static ssize_t iwl_dbgfs_bt_notif_read(struct file *file, char __user *user_buf,
594 				       size_t count, loff_t *ppos)
595 {
596 	struct iwl_mvm *mvm = file->private_data;
597 	struct iwl_bt_coex_profile_notif *notif = &mvm->last_bt_notif;
598 	char *buf;
599 	int ret, pos = 0, bufsz = sizeof(char) * 1024;
600 
601 	buf = kmalloc(bufsz, GFP_KERNEL);
602 	if (!buf)
603 		return -ENOMEM;
604 
605 	mutex_lock(&mvm->mutex);
606 
607 	pos += iwl_mvm_coex_dump_mbox(notif, buf, pos, bufsz);
608 
609 	pos += scnprintf(buf + pos, bufsz - pos, "bt_ci_compliance = %d\n",
610 			 notif->bt_ci_compliance);
611 	pos += scnprintf(buf + pos, bufsz - pos, "primary_ch_lut = %d\n",
612 			 le32_to_cpu(notif->primary_ch_lut));
613 	pos += scnprintf(buf + pos, bufsz - pos, "secondary_ch_lut = %d\n",
614 			 le32_to_cpu(notif->secondary_ch_lut));
615 	pos += scnprintf(buf + pos,
616 			 bufsz - pos, "bt_activity_grading = %d\n",
617 			 le32_to_cpu(notif->bt_activity_grading));
618 	pos += scnprintf(buf + pos, bufsz - pos, "bt_rrc = %d\n",
619 			 notif->rrc_status & 0xF);
620 	pos += scnprintf(buf + pos, bufsz - pos, "bt_ttc = %d\n",
621 			 notif->ttc_status & 0xF);
622 
623 	pos += scnprintf(buf + pos, bufsz - pos, "sync_sco = %d\n",
624 			 IWL_MVM_BT_COEX_SYNC2SCO);
625 	pos += scnprintf(buf + pos, bufsz - pos, "mplut = %d\n",
626 			 IWL_MVM_BT_COEX_MPLUT);
627 
628 	mutex_unlock(&mvm->mutex);
629 
630 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
631 	kfree(buf);
632 
633 	return ret;
634 }
635 #undef BT_MBOX_PRINT
636 
637 static ssize_t iwl_dbgfs_bt_cmd_read(struct file *file, char __user *user_buf,
638 				     size_t count, loff_t *ppos)
639 {
640 	struct iwl_mvm *mvm = file->private_data;
641 	struct iwl_bt_coex_ci_cmd *cmd = &mvm->last_bt_ci_cmd;
642 	char buf[256];
643 	int bufsz = sizeof(buf);
644 	int pos = 0;
645 
646 	mutex_lock(&mvm->mutex);
647 
648 	pos += scnprintf(buf + pos, bufsz - pos, "Channel inhibition CMD\n");
649 	pos += scnprintf(buf + pos, bufsz - pos,
650 			 "\tPrimary Channel Bitmap 0x%016llx\n",
651 			 le64_to_cpu(cmd->bt_primary_ci));
652 	pos += scnprintf(buf + pos, bufsz - pos,
653 			 "\tSecondary Channel Bitmap 0x%016llx\n",
654 			 le64_to_cpu(cmd->bt_secondary_ci));
655 
656 	mutex_unlock(&mvm->mutex);
657 
658 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
659 }
660 
661 static ssize_t
662 iwl_dbgfs_bt_tx_prio_write(struct iwl_mvm *mvm, char *buf,
663 			   size_t count, loff_t *ppos)
664 {
665 	u32 bt_tx_prio;
666 
667 	if (sscanf(buf, "%u", &bt_tx_prio) != 1)
668 		return -EINVAL;
669 	if (bt_tx_prio > 4)
670 		return -EINVAL;
671 
672 	mvm->bt_tx_prio = bt_tx_prio;
673 
674 	return count;
675 }
676 
677 static ssize_t
678 iwl_dbgfs_bt_force_ant_write(struct iwl_mvm *mvm, char *buf,
679 			     size_t count, loff_t *ppos)
680 {
681 	static const char * const modes_str[BT_FORCE_ANT_MAX] = {
682 		[BT_FORCE_ANT_DIS] = "dis",
683 		[BT_FORCE_ANT_AUTO] = "auto",
684 		[BT_FORCE_ANT_BT] = "bt",
685 		[BT_FORCE_ANT_WIFI] = "wifi",
686 	};
687 	int ret, bt_force_ant_mode;
688 
689 	ret = match_string(modes_str, ARRAY_SIZE(modes_str), buf);
690 	if (ret < 0)
691 		return ret;
692 
693 	bt_force_ant_mode = ret;
694 	ret = 0;
695 	mutex_lock(&mvm->mutex);
696 	if (mvm->bt_force_ant_mode == bt_force_ant_mode)
697 		goto out;
698 
699 	mvm->bt_force_ant_mode = bt_force_ant_mode;
700 	IWL_DEBUG_COEX(mvm, "Force mode: %s\n",
701 		       modes_str[mvm->bt_force_ant_mode]);
702 
703 	if (iwl_mvm_firmware_running(mvm))
704 		ret = iwl_mvm_send_bt_init_conf(mvm);
705 	else
706 		ret = 0;
707 
708 out:
709 	mutex_unlock(&mvm->mutex);
710 	return ret ?: count;
711 }
712 
713 static ssize_t iwl_dbgfs_fw_ver_read(struct file *file, char __user *user_buf,
714 				     size_t count, loff_t *ppos)
715 {
716 	struct iwl_mvm *mvm = file->private_data;
717 	char *buff, *pos, *endpos;
718 	static const size_t bufsz = 1024;
719 	char _fw_name_pre[FW_NAME_PRE_BUFSIZE];
720 	int ret;
721 
722 	buff = kmalloc(bufsz, GFP_KERNEL);
723 	if (!buff)
724 		return -ENOMEM;
725 
726 	pos = buff;
727 	endpos = pos + bufsz;
728 
729 	pos += scnprintf(pos, endpos - pos, "FW prefix: %s\n",
730 			 iwl_drv_get_fwname_pre(mvm->trans, _fw_name_pre));
731 	pos += scnprintf(pos, endpos - pos, "FW: %s\n",
732 			 mvm->fwrt.fw->human_readable);
733 	pos += scnprintf(pos, endpos - pos, "Device: %s\n",
734 			 mvm->fwrt.trans->name);
735 	pos += scnprintf(pos, endpos - pos, "Bus: %s\n",
736 #if defined(__linux__)
737 			 mvm->fwrt.dev->bus->name);
738 #elif defined(__FreeBSD__)
739 			"<bus>");
740 #endif
741 
742 	ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
743 	kfree(buff);
744 
745 	return ret;
746 }
747 
748 static ssize_t iwl_dbgfs_tas_get_status_read(struct file *file,
749 					     char __user *user_buf,
750 					     size_t count, loff_t *ppos)
751 {
752 	struct iwl_mvm *mvm = file->private_data;
753 	struct iwl_mvm_tas_status_resp tas_rsp;
754 	struct iwl_mvm_tas_status_resp *rsp = &tas_rsp;
755 	static const size_t bufsz = 1024;
756 	char *buff, *pos, *endpos;
757 	const char * const tas_dis_reason[TAS_DISABLED_REASON_MAX] = {
758 		[TAS_DISABLED_DUE_TO_BIOS] =
759 			"Due To BIOS",
760 		[TAS_DISABLED_DUE_TO_SAR_6DBM] =
761 			"Due To SAR Limit Less Than 6 dBm",
762 		[TAS_DISABLED_REASON_INVALID] =
763 			"N/A",
764 	};
765 	const char * const tas_current_status[TAS_DYNA_STATUS_MAX] = {
766 		[TAS_DYNA_INACTIVE] = "INACTIVE",
767 		[TAS_DYNA_INACTIVE_MVM_MODE] =
768 			"inactive due to mvm mode",
769 		[TAS_DYNA_INACTIVE_TRIGGER_MODE] =
770 			"inactive due to trigger mode",
771 		[TAS_DYNA_INACTIVE_BLOCK_LISTED] =
772 			"inactive due to block listed",
773 		[TAS_DYNA_INACTIVE_UHB_NON_US] =
774 			"inactive due to uhb non US",
775 		[TAS_DYNA_ACTIVE] = "ACTIVE",
776 	};
777 	struct iwl_host_cmd hcmd = {
778 		.id = WIDE_ID(DEBUG_GROUP, GET_TAS_STATUS),
779 		.flags = CMD_WANT_SKB,
780 		.len = { 0, },
781 		.data = { NULL, },
782 	};
783 	int ret, i, tmp;
784 	bool tas_enabled = false;
785 	unsigned long dyn_status;
786 
787 	if (!iwl_mvm_firmware_running(mvm))
788 		return -ENODEV;
789 
790 	mutex_lock(&mvm->mutex);
791 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
792 	mutex_unlock(&mvm->mutex);
793 	if (ret < 0)
794 		return ret;
795 
796 	buff = kzalloc(bufsz, GFP_KERNEL);
797 	if (!buff)
798 		return -ENOMEM;
799 	pos = buff;
800 	endpos = pos + bufsz;
801 
802 	rsp = (void *)hcmd.resp_pkt->data;
803 
804 	pos += scnprintf(pos, endpos - pos, "TAS Conclusion:\n");
805 	for (i = 0; i < rsp->in_dual_radio + 1; i++) {
806 		if (rsp->tas_status_mac[i].band != TAS_LMAC_BAND_INVALID &&
807 		    rsp->tas_status_mac[i].dynamic_status & BIT(TAS_DYNA_ACTIVE)) {
808 			pos += scnprintf(pos, endpos - pos, "\tON for ");
809 			switch (rsp->tas_status_mac[i].band) {
810 			case TAS_LMAC_BAND_HB:
811 				pos += scnprintf(pos, endpos - pos, "HB\n");
812 				break;
813 			case TAS_LMAC_BAND_LB:
814 				pos += scnprintf(pos, endpos - pos, "LB\n");
815 				break;
816 			case TAS_LMAC_BAND_UHB:
817 				pos += scnprintf(pos, endpos - pos, "UHB\n");
818 				break;
819 			case TAS_LMAC_BAND_INVALID:
820 				pos += scnprintf(pos, endpos - pos,
821 						 "INVALID BAND\n");
822 				break;
823 			default:
824 				pos += scnprintf(pos, endpos - pos,
825 						 "Unsupported band (%d)\n",
826 						 rsp->tas_status_mac[i].band);
827 				goto out;
828 			}
829 			tas_enabled = true;
830 		}
831 	}
832 	if (!tas_enabled)
833 		pos += scnprintf(pos, endpos - pos, "\tOFF\n");
834 
835 	pos += scnprintf(pos, endpos - pos, "TAS Report\n");
836 	pos += scnprintf(pos, endpos - pos, "TAS FW version: %d\n",
837 			 rsp->tas_fw_version);
838 	pos += scnprintf(pos, endpos - pos, "Is UHB enabled for USA?: %s\n",
839 			 rsp->is_uhb_for_usa_enable ? "True" : "False");
840 	pos += scnprintf(pos, endpos - pos, "Current MCC: 0x%x\n",
841 			 le16_to_cpu(rsp->curr_mcc));
842 
843 	pos += scnprintf(pos, endpos - pos, "Block list entries:");
844 	for (i = 0; i < APCI_WTAS_BLACK_LIST_MAX; i++)
845 		pos += scnprintf(pos, endpos - pos, " 0x%x",
846 				 le16_to_cpu(rsp->block_list[i]));
847 
848 	pos += scnprintf(pos, endpos - pos, "\nOEM name: %s\n",
849 			 dmi_get_system_info(DMI_SYS_VENDOR));
850 	pos += scnprintf(pos, endpos - pos, "\tVendor In Approved List: %s\n",
851 			 iwl_mvm_is_vendor_in_approved_list() ? "YES" : "NO");
852 	pos += scnprintf(pos, endpos - pos,
853 			 "\tDo TAS Support Dual Radio?: %s\n",
854 			 rsp->in_dual_radio ? "TRUE" : "FALSE");
855 
856 	for (i = 0; i < rsp->in_dual_radio + 1; i++) {
857 		if (rsp->tas_status_mac[i].static_status == 0) {
858 			pos += scnprintf(pos, endpos - pos,
859 					 "Static status: disabled\n");
860 			pos += scnprintf(pos, endpos - pos,
861 					 "Static disabled reason: %s (0)\n",
862 					 tas_dis_reason[0]);
863 			goto out;
864 		}
865 
866 		pos += scnprintf(pos, endpos - pos, "TAS status for ");
867 		switch (rsp->tas_status_mac[i].band) {
868 		case TAS_LMAC_BAND_HB:
869 			pos += scnprintf(pos, endpos - pos, "High band\n");
870 			break;
871 		case TAS_LMAC_BAND_LB:
872 			pos += scnprintf(pos, endpos - pos, "Low band\n");
873 			break;
874 		case TAS_LMAC_BAND_UHB:
875 			pos += scnprintf(pos, endpos - pos,
876 					 "Ultra high band\n");
877 			break;
878 		case TAS_LMAC_BAND_INVALID:
879 			pos += scnprintf(pos, endpos - pos,
880 					 "INVALID band\n");
881 			break;
882 		default:
883 			pos += scnprintf(pos, endpos - pos,
884 					 "Unsupported band (%d)\n",
885 					 rsp->tas_status_mac[i].band);
886 			goto out;
887 		}
888 		pos += scnprintf(pos, endpos - pos, "Static status: %sabled\n",
889 				 rsp->tas_status_mac[i].static_status ?
890 				 "En" : "Dis");
891 		pos += scnprintf(pos, endpos - pos,
892 				 "\tStatic Disabled Reason: ");
893 		if (rsp->tas_status_mac[i].static_dis_reason < TAS_DISABLED_REASON_MAX)
894 			pos += scnprintf(pos, endpos - pos, "%s (%d)\n",
895 					 tas_dis_reason[rsp->tas_status_mac[i].static_dis_reason],
896 					 rsp->tas_status_mac[i].static_dis_reason);
897 		else
898 			pos += scnprintf(pos, endpos - pos,
899 					 "unsupported value (%d)\n",
900 					 rsp->tas_status_mac[i].static_dis_reason);
901 
902 		pos += scnprintf(pos, endpos - pos, "Dynamic status:\n");
903 		dyn_status = (rsp->tas_status_mac[i].dynamic_status);
904 		for_each_set_bit(tmp, &dyn_status, sizeof(dyn_status)) {
905 			if (tmp >= 0 && tmp < TAS_DYNA_STATUS_MAX)
906 				pos += scnprintf(pos, endpos - pos,
907 						 "\t%s (%d)\n",
908 						 tas_current_status[tmp], tmp);
909 		}
910 
911 		pos += scnprintf(pos, endpos - pos,
912 				 "Is near disconnection?: %s\n",
913 				 rsp->tas_status_mac[i].near_disconnection ?
914 				 "True" : "False");
915 		tmp = le16_to_cpu(rsp->tas_status_mac[i].max_reg_pwr_limit);
916 		pos += scnprintf(pos, endpos - pos,
917 				 "Max. regulatory pwr limit (dBm): %d.%03d\n",
918 				 tmp / 8, 125 * (tmp % 8));
919 		tmp = le16_to_cpu(rsp->tas_status_mac[i].sar_limit);
920 		pos += scnprintf(pos, endpos - pos,
921 				 "SAR limit (dBm): %d.%03d\n",
922 				 tmp / 8, 125 * (tmp % 8));
923 	}
924 
925 out:
926 	ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
927 	kfree(buff);
928 	iwl_free_resp(&hcmd);
929 	return ret;
930 }
931 
932 static ssize_t iwl_dbgfs_phy_integration_ver_read(struct file *file,
933 						  char __user *user_buf,
934 						  size_t count, loff_t *ppos)
935 {
936 	struct iwl_mvm *mvm = file->private_data;
937 	char *buf;
938 	size_t bufsz;
939 	int pos;
940 	ssize_t ret;
941 
942 	bufsz = mvm->fw->phy_integration_ver_len + 2;
943 	buf = kmalloc(bufsz, GFP_KERNEL);
944 	if (!buf)
945 		return -ENOMEM;
946 
947 	pos = scnprintf(buf, bufsz, "%.*s\n", mvm->fw->phy_integration_ver_len,
948 			mvm->fw->phy_integration_ver);
949 
950 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
951 
952 	kfree(buf);
953 	return ret;
954 }
955 
956 #define PRINT_STATS_LE32(_struct, _memb)				\
957 			 pos += scnprintf(buf + pos, bufsz - pos,	\
958 					  fmt_table, #_memb,		\
959 					  le32_to_cpu(_struct->_memb))
960 
961 static ssize_t iwl_dbgfs_fw_rx_stats_read(struct file *file,
962 					  char __user *user_buf, size_t count,
963 					  loff_t *ppos)
964 {
965 	struct iwl_mvm *mvm = file->private_data;
966 	static const char *fmt_table = "\t%-30s %10u\n";
967 	static const char *fmt_header = "%-32s\n";
968 	int pos = 0;
969 	char *buf;
970 	int ret;
971 	size_t bufsz;
972 
973 	if (iwl_mvm_has_new_rx_stats_api(mvm))
974 		bufsz = ((sizeof(struct mvm_statistics_rx) /
975 			  sizeof(__le32)) * 43) + (4 * 33) + 1;
976 	else
977 		/* 43 = size of each data line; 33 = size of each header */
978 		bufsz = ((sizeof(struct mvm_statistics_rx_v3) /
979 			  sizeof(__le32)) * 43) + (4 * 33) + 1;
980 
981 	buf = kzalloc(bufsz, GFP_KERNEL);
982 	if (!buf)
983 		return -ENOMEM;
984 
985 	mutex_lock(&mvm->mutex);
986 
987 	if (iwl_mvm_firmware_running(mvm))
988 		iwl_mvm_request_statistics(mvm, false);
989 
990 	pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
991 			 "Statistics_Rx - OFDM");
992 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
993 		struct mvm_statistics_rx_phy_v2 *ofdm = &mvm->rx_stats_v3.ofdm;
994 
995 		PRINT_STATS_LE32(ofdm, ina_cnt);
996 		PRINT_STATS_LE32(ofdm, fina_cnt);
997 		PRINT_STATS_LE32(ofdm, plcp_err);
998 		PRINT_STATS_LE32(ofdm, crc32_err);
999 		PRINT_STATS_LE32(ofdm, overrun_err);
1000 		PRINT_STATS_LE32(ofdm, early_overrun_err);
1001 		PRINT_STATS_LE32(ofdm, crc32_good);
1002 		PRINT_STATS_LE32(ofdm, false_alarm_cnt);
1003 		PRINT_STATS_LE32(ofdm, fina_sync_err_cnt);
1004 		PRINT_STATS_LE32(ofdm, sfd_timeout);
1005 		PRINT_STATS_LE32(ofdm, fina_timeout);
1006 		PRINT_STATS_LE32(ofdm, unresponded_rts);
1007 		PRINT_STATS_LE32(ofdm, rxe_frame_lmt_overrun);
1008 		PRINT_STATS_LE32(ofdm, sent_ack_cnt);
1009 		PRINT_STATS_LE32(ofdm, sent_cts_cnt);
1010 		PRINT_STATS_LE32(ofdm, sent_ba_rsp_cnt);
1011 		PRINT_STATS_LE32(ofdm, dsp_self_kill);
1012 		PRINT_STATS_LE32(ofdm, mh_format_err);
1013 		PRINT_STATS_LE32(ofdm, re_acq_main_rssi_sum);
1014 		PRINT_STATS_LE32(ofdm, reserved);
1015 	} else {
1016 		struct mvm_statistics_rx_phy *ofdm = &mvm->rx_stats.ofdm;
1017 
1018 		PRINT_STATS_LE32(ofdm, unresponded_rts);
1019 		PRINT_STATS_LE32(ofdm, rxe_frame_lmt_overrun);
1020 		PRINT_STATS_LE32(ofdm, sent_ba_rsp_cnt);
1021 		PRINT_STATS_LE32(ofdm, dsp_self_kill);
1022 		PRINT_STATS_LE32(ofdm, reserved);
1023 	}
1024 
1025 	pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
1026 			 "Statistics_Rx - CCK");
1027 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
1028 		struct mvm_statistics_rx_phy_v2 *cck = &mvm->rx_stats_v3.cck;
1029 
1030 		PRINT_STATS_LE32(cck, ina_cnt);
1031 		PRINT_STATS_LE32(cck, fina_cnt);
1032 		PRINT_STATS_LE32(cck, plcp_err);
1033 		PRINT_STATS_LE32(cck, crc32_err);
1034 		PRINT_STATS_LE32(cck, overrun_err);
1035 		PRINT_STATS_LE32(cck, early_overrun_err);
1036 		PRINT_STATS_LE32(cck, crc32_good);
1037 		PRINT_STATS_LE32(cck, false_alarm_cnt);
1038 		PRINT_STATS_LE32(cck, fina_sync_err_cnt);
1039 		PRINT_STATS_LE32(cck, sfd_timeout);
1040 		PRINT_STATS_LE32(cck, fina_timeout);
1041 		PRINT_STATS_LE32(cck, unresponded_rts);
1042 		PRINT_STATS_LE32(cck, rxe_frame_lmt_overrun);
1043 		PRINT_STATS_LE32(cck, sent_ack_cnt);
1044 		PRINT_STATS_LE32(cck, sent_cts_cnt);
1045 		PRINT_STATS_LE32(cck, sent_ba_rsp_cnt);
1046 		PRINT_STATS_LE32(cck, dsp_self_kill);
1047 		PRINT_STATS_LE32(cck, mh_format_err);
1048 		PRINT_STATS_LE32(cck, re_acq_main_rssi_sum);
1049 		PRINT_STATS_LE32(cck, reserved);
1050 	} else {
1051 		struct mvm_statistics_rx_phy *cck = &mvm->rx_stats.cck;
1052 
1053 		PRINT_STATS_LE32(cck, unresponded_rts);
1054 		PRINT_STATS_LE32(cck, rxe_frame_lmt_overrun);
1055 		PRINT_STATS_LE32(cck, sent_ba_rsp_cnt);
1056 		PRINT_STATS_LE32(cck, dsp_self_kill);
1057 		PRINT_STATS_LE32(cck, reserved);
1058 	}
1059 
1060 	pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
1061 			 "Statistics_Rx - GENERAL");
1062 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
1063 		struct mvm_statistics_rx_non_phy_v3 *general =
1064 			&mvm->rx_stats_v3.general;
1065 
1066 		PRINT_STATS_LE32(general, bogus_cts);
1067 		PRINT_STATS_LE32(general, bogus_ack);
1068 		PRINT_STATS_LE32(general, non_bssid_frames);
1069 		PRINT_STATS_LE32(general, filtered_frames);
1070 		PRINT_STATS_LE32(general, non_channel_beacons);
1071 		PRINT_STATS_LE32(general, channel_beacons);
1072 		PRINT_STATS_LE32(general, num_missed_bcon);
1073 		PRINT_STATS_LE32(general, adc_rx_saturation_time);
1074 		PRINT_STATS_LE32(general, ina_detection_search_time);
1075 		PRINT_STATS_LE32(general, beacon_silence_rssi_a);
1076 		PRINT_STATS_LE32(general, beacon_silence_rssi_b);
1077 		PRINT_STATS_LE32(general, beacon_silence_rssi_c);
1078 		PRINT_STATS_LE32(general, interference_data_flag);
1079 		PRINT_STATS_LE32(general, channel_load);
1080 		PRINT_STATS_LE32(general, dsp_false_alarms);
1081 		PRINT_STATS_LE32(general, beacon_rssi_a);
1082 		PRINT_STATS_LE32(general, beacon_rssi_b);
1083 		PRINT_STATS_LE32(general, beacon_rssi_c);
1084 		PRINT_STATS_LE32(general, beacon_energy_a);
1085 		PRINT_STATS_LE32(general, beacon_energy_b);
1086 		PRINT_STATS_LE32(general, beacon_energy_c);
1087 		PRINT_STATS_LE32(general, num_bt_kills);
1088 		PRINT_STATS_LE32(general, mac_id);
1089 		PRINT_STATS_LE32(general, directed_data_mpdu);
1090 	} else {
1091 		struct mvm_statistics_rx_non_phy *general =
1092 			&mvm->rx_stats.general;
1093 
1094 		PRINT_STATS_LE32(general, bogus_cts);
1095 		PRINT_STATS_LE32(general, bogus_ack);
1096 		PRINT_STATS_LE32(general, non_channel_beacons);
1097 		PRINT_STATS_LE32(general, channel_beacons);
1098 		PRINT_STATS_LE32(general, num_missed_bcon);
1099 		PRINT_STATS_LE32(general, adc_rx_saturation_time);
1100 		PRINT_STATS_LE32(general, ina_detection_search_time);
1101 		PRINT_STATS_LE32(general, beacon_silence_rssi_a);
1102 		PRINT_STATS_LE32(general, beacon_silence_rssi_b);
1103 		PRINT_STATS_LE32(general, beacon_silence_rssi_c);
1104 		PRINT_STATS_LE32(general, interference_data_flag);
1105 		PRINT_STATS_LE32(general, channel_load);
1106 		PRINT_STATS_LE32(general, beacon_rssi_a);
1107 		PRINT_STATS_LE32(general, beacon_rssi_b);
1108 		PRINT_STATS_LE32(general, beacon_rssi_c);
1109 		PRINT_STATS_LE32(general, beacon_energy_a);
1110 		PRINT_STATS_LE32(general, beacon_energy_b);
1111 		PRINT_STATS_LE32(general, beacon_energy_c);
1112 		PRINT_STATS_LE32(general, num_bt_kills);
1113 		PRINT_STATS_LE32(general, mac_id);
1114 	}
1115 
1116 	pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
1117 			 "Statistics_Rx - HT");
1118 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
1119 		struct mvm_statistics_rx_ht_phy_v1 *ht =
1120 			&mvm->rx_stats_v3.ofdm_ht;
1121 
1122 		PRINT_STATS_LE32(ht, plcp_err);
1123 		PRINT_STATS_LE32(ht, overrun_err);
1124 		PRINT_STATS_LE32(ht, early_overrun_err);
1125 		PRINT_STATS_LE32(ht, crc32_good);
1126 		PRINT_STATS_LE32(ht, crc32_err);
1127 		PRINT_STATS_LE32(ht, mh_format_err);
1128 		PRINT_STATS_LE32(ht, agg_crc32_good);
1129 		PRINT_STATS_LE32(ht, agg_mpdu_cnt);
1130 		PRINT_STATS_LE32(ht, agg_cnt);
1131 		PRINT_STATS_LE32(ht, unsupport_mcs);
1132 	} else {
1133 		struct mvm_statistics_rx_ht_phy *ht =
1134 			&mvm->rx_stats.ofdm_ht;
1135 
1136 		PRINT_STATS_LE32(ht, mh_format_err);
1137 		PRINT_STATS_LE32(ht, agg_mpdu_cnt);
1138 		PRINT_STATS_LE32(ht, agg_cnt);
1139 		PRINT_STATS_LE32(ht, unsupport_mcs);
1140 	}
1141 
1142 	mutex_unlock(&mvm->mutex);
1143 
1144 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1145 	kfree(buf);
1146 
1147 	return ret;
1148 }
1149 #undef PRINT_STAT_LE32
1150 
1151 static ssize_t iwl_dbgfs_frame_stats_read(struct iwl_mvm *mvm,
1152 					  char __user *user_buf, size_t count,
1153 					  loff_t *ppos,
1154 					  struct iwl_mvm_frame_stats *stats)
1155 {
1156 	char *buff, *pos, *endpos;
1157 	int idx, i;
1158 	int ret;
1159 	static const size_t bufsz = 1024;
1160 
1161 	buff = kmalloc(bufsz, GFP_KERNEL);
1162 	if (!buff)
1163 		return -ENOMEM;
1164 
1165 	spin_lock_bh(&mvm->drv_stats_lock);
1166 
1167 	pos = buff;
1168 	endpos = pos + bufsz;
1169 
1170 	pos += scnprintf(pos, endpos - pos,
1171 			 "Legacy/HT/VHT\t:\t%d/%d/%d\n",
1172 			 stats->legacy_frames,
1173 			 stats->ht_frames,
1174 			 stats->vht_frames);
1175 	pos += scnprintf(pos, endpos - pos, "20/40/80\t:\t%d/%d/%d\n",
1176 			 stats->bw_20_frames,
1177 			 stats->bw_40_frames,
1178 			 stats->bw_80_frames);
1179 	pos += scnprintf(pos, endpos - pos, "NGI/SGI\t\t:\t%d/%d\n",
1180 			 stats->ngi_frames,
1181 			 stats->sgi_frames);
1182 	pos += scnprintf(pos, endpos - pos, "SISO/MIMO2\t:\t%d/%d\n",
1183 			 stats->siso_frames,
1184 			 stats->mimo2_frames);
1185 	pos += scnprintf(pos, endpos - pos, "FAIL/SCSS\t:\t%d/%d\n",
1186 			 stats->fail_frames,
1187 			 stats->success_frames);
1188 	pos += scnprintf(pos, endpos - pos, "MPDUs agg\t:\t%d\n",
1189 			 stats->agg_frames);
1190 	pos += scnprintf(pos, endpos - pos, "A-MPDUs\t\t:\t%d\n",
1191 			 stats->ampdu_count);
1192 	pos += scnprintf(pos, endpos - pos, "Avg MPDUs/A-MPDU:\t%d\n",
1193 			 stats->ampdu_count > 0 ?
1194 			 (stats->agg_frames / stats->ampdu_count) : 0);
1195 
1196 	pos += scnprintf(pos, endpos - pos, "Last Rates\n");
1197 
1198 	idx = stats->last_frame_idx - 1;
1199 	for (i = 0; i < ARRAY_SIZE(stats->last_rates); i++) {
1200 		idx = (idx + 1) % ARRAY_SIZE(stats->last_rates);
1201 		if (stats->last_rates[idx] == 0)
1202 			continue;
1203 		pos += scnprintf(pos, endpos - pos, "Rate[%d]: ",
1204 				 (int)(ARRAY_SIZE(stats->last_rates) - i));
1205 		pos += rs_pretty_print_rate_v1(pos, endpos - pos,
1206 					       stats->last_rates[idx]);
1207 		if (pos < endpos - 1)
1208 			*pos++ = '\n';
1209 	}
1210 	spin_unlock_bh(&mvm->drv_stats_lock);
1211 
1212 	ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
1213 	kfree(buff);
1214 
1215 	return ret;
1216 }
1217 
1218 static ssize_t iwl_dbgfs_drv_rx_stats_read(struct file *file,
1219 					   char __user *user_buf, size_t count,
1220 					   loff_t *ppos)
1221 {
1222 	struct iwl_mvm *mvm = file->private_data;
1223 
1224 	return iwl_dbgfs_frame_stats_read(mvm, user_buf, count, ppos,
1225 					  &mvm->drv_rx_stats);
1226 }
1227 
1228 static ssize_t iwl_dbgfs_fw_restart_write(struct iwl_mvm *mvm, char *buf,
1229 					  size_t count, loff_t *ppos)
1230 {
1231 	int __maybe_unused ret;
1232 
1233 	if (!iwl_mvm_firmware_running(mvm))
1234 		return -EIO;
1235 
1236 	mutex_lock(&mvm->mutex);
1237 
1238 	/* allow one more restart that we're provoking here */
1239 	if (mvm->fw_restart >= 0)
1240 		mvm->fw_restart++;
1241 
1242 	if (count == 6 && !strcmp(buf, "nolog\n")) {
1243 		set_bit(IWL_MVM_STATUS_SUPPRESS_ERROR_LOG_ONCE, &mvm->status);
1244 		set_bit(STATUS_SUPPRESS_CMD_ERROR_ONCE, &mvm->trans->status);
1245 	}
1246 
1247 	/* take the return value to make compiler happy - it will fail anyway */
1248 	ret = iwl_mvm_send_cmd_pdu(mvm,
1249 				   WIDE_ID(LONG_GROUP, REPLY_ERROR),
1250 				   0, 0, NULL);
1251 
1252 	mutex_unlock(&mvm->mutex);
1253 
1254 	return count;
1255 }
1256 
1257 static ssize_t iwl_dbgfs_fw_nmi_write(struct iwl_mvm *mvm, char *buf,
1258 				      size_t count, loff_t *ppos)
1259 {
1260 	if (!iwl_mvm_firmware_running(mvm))
1261 		return -EIO;
1262 
1263 	if (count == 6 && !strcmp(buf, "nolog\n"))
1264 		set_bit(IWL_MVM_STATUS_SUPPRESS_ERROR_LOG_ONCE, &mvm->status);
1265 
1266 	iwl_force_nmi(mvm->trans);
1267 
1268 	return count;
1269 }
1270 
1271 static ssize_t
1272 iwl_dbgfs_scan_ant_rxchain_read(struct file *file,
1273 				char __user *user_buf,
1274 				size_t count, loff_t *ppos)
1275 {
1276 	struct iwl_mvm *mvm = file->private_data;
1277 	int pos = 0;
1278 	char buf[32];
1279 	const size_t bufsz = sizeof(buf);
1280 
1281 	/* print which antennas were set for the scan command by the user */
1282 	pos += scnprintf(buf + pos, bufsz - pos, "Antennas for scan: ");
1283 	if (mvm->scan_rx_ant & ANT_A)
1284 		pos += scnprintf(buf + pos, bufsz - pos, "A");
1285 	if (mvm->scan_rx_ant & ANT_B)
1286 		pos += scnprintf(buf + pos, bufsz - pos, "B");
1287 	pos += scnprintf(buf + pos, bufsz - pos, " (%x)\n", mvm->scan_rx_ant);
1288 
1289 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1290 }
1291 
1292 static ssize_t
1293 iwl_dbgfs_scan_ant_rxchain_write(struct iwl_mvm *mvm, char *buf,
1294 				 size_t count, loff_t *ppos)
1295 {
1296 	u8 scan_rx_ant;
1297 
1298 	if (!iwl_mvm_firmware_running(mvm))
1299 		return -EIO;
1300 
1301 	if (sscanf(buf, "%hhx", &scan_rx_ant) != 1)
1302 		return -EINVAL;
1303 	if (scan_rx_ant > ANT_ABC)
1304 		return -EINVAL;
1305 	if (scan_rx_ant & ~(iwl_mvm_get_valid_rx_ant(mvm)))
1306 		return -EINVAL;
1307 
1308 	if (mvm->scan_rx_ant != scan_rx_ant) {
1309 		mvm->scan_rx_ant = scan_rx_ant;
1310 		if (fw_has_capa(&mvm->fw->ucode_capa,
1311 				IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1312 			iwl_mvm_config_scan(mvm);
1313 	}
1314 
1315 	return count;
1316 }
1317 
1318 static ssize_t iwl_dbgfs_indirection_tbl_write(struct iwl_mvm *mvm,
1319 					       char *buf, size_t count,
1320 					       loff_t *ppos)
1321 {
1322 	struct iwl_rss_config_cmd cmd = {
1323 		.flags = cpu_to_le32(IWL_RSS_ENABLE),
1324 		.hash_mask = IWL_RSS_HASH_TYPE_IPV4_TCP |
1325 			     IWL_RSS_HASH_TYPE_IPV4_UDP |
1326 			     IWL_RSS_HASH_TYPE_IPV4_PAYLOAD |
1327 			     IWL_RSS_HASH_TYPE_IPV6_TCP |
1328 			     IWL_RSS_HASH_TYPE_IPV6_UDP |
1329 			     IWL_RSS_HASH_TYPE_IPV6_PAYLOAD,
1330 	};
1331 	int ret, i, num_repeats, nbytes = count / 2;
1332 
1333 	ret = hex2bin(cmd.indirection_table, buf, nbytes);
1334 	if (ret)
1335 		return ret;
1336 
1337 	/*
1338 	 * The input is the redirection table, partial or full.
1339 	 * Repeat the pattern if needed.
1340 	 * For example, input of 01020F will be repeated 42 times,
1341 	 * indirecting RSS hash results to queues 1, 2, 15 (skipping
1342 	 * queues 3 - 14).
1343 	 */
1344 	num_repeats = ARRAY_SIZE(cmd.indirection_table) / nbytes;
1345 	for (i = 1; i < num_repeats; i++)
1346 		memcpy(&cmd.indirection_table[i * nbytes],
1347 		       cmd.indirection_table, nbytes);
1348 	/* handle cut in the middle pattern for the last places */
1349 	memcpy(&cmd.indirection_table[i * nbytes], cmd.indirection_table,
1350 	       ARRAY_SIZE(cmd.indirection_table) % nbytes);
1351 
1352 	netdev_rss_key_fill(cmd.secret_key, sizeof(cmd.secret_key));
1353 
1354 	mutex_lock(&mvm->mutex);
1355 	if (iwl_mvm_firmware_running(mvm))
1356 		ret = iwl_mvm_send_cmd_pdu(mvm, RSS_CONFIG_CMD, 0,
1357 					   sizeof(cmd), &cmd);
1358 	else
1359 		ret = 0;
1360 	mutex_unlock(&mvm->mutex);
1361 
1362 	return ret ?: count;
1363 }
1364 
1365 static ssize_t iwl_dbgfs_inject_packet_write(struct iwl_mvm *mvm,
1366 					     char *buf, size_t count,
1367 					     loff_t *ppos)
1368 {
1369 	struct iwl_op_mode *opmode = container_of((void *)mvm,
1370 						  struct iwl_op_mode,
1371 						  op_mode_specific);
1372 	struct iwl_rx_cmd_buffer rxb = {
1373 		._rx_page_order = 0,
1374 		.truesize = 0, /* not used */
1375 		._offset = 0,
1376 	};
1377 	struct iwl_rx_packet *pkt;
1378 	int bin_len = count / 2;
1379 	int ret = -EINVAL;
1380 
1381 	if (!iwl_mvm_firmware_running(mvm))
1382 		return -EIO;
1383 
1384 	/* supporting only MQ RX */
1385 	if (!mvm->trans->trans_cfg->mq_rx_supported)
1386 		return -ENOTSUPP;
1387 
1388 	rxb._page = alloc_pages(GFP_ATOMIC, 0);
1389 	if (!rxb._page)
1390 		return -ENOMEM;
1391 	pkt = rxb_addr(&rxb);
1392 
1393 	ret = hex2bin(page_address(rxb._page), buf, bin_len);
1394 	if (ret)
1395 		goto out;
1396 
1397 	/* avoid invalid memory access and malformed packet */
1398 	if (bin_len < sizeof(*pkt) ||
1399 	    bin_len != sizeof(*pkt) + iwl_rx_packet_payload_len(pkt))
1400 		goto out;
1401 
1402 	local_bh_disable();
1403 	iwl_mvm_rx_mq(opmode, NULL, &rxb);
1404 	local_bh_enable();
1405 	ret = 0;
1406 
1407 out:
1408 	iwl_free_rxb(&rxb);
1409 
1410 	return ret ?: count;
1411 }
1412 
1413 static int _iwl_dbgfs_inject_beacon_ie(struct iwl_mvm *mvm, char *bin, int len)
1414 {
1415 	struct ieee80211_vif *vif;
1416 	struct iwl_mvm_vif *mvmvif;
1417 	struct sk_buff *beacon;
1418 	struct ieee80211_tx_info *info;
1419 	struct iwl_mac_beacon_cmd beacon_cmd = {};
1420 	unsigned int link_id;
1421 	u8 rate;
1422 	int i;
1423 
1424 	len /= 2;
1425 
1426 	/* Element len should be represented by u8 */
1427 	if (len >= U8_MAX)
1428 		return -EINVAL;
1429 
1430 	if (!iwl_mvm_firmware_running(mvm))
1431 		return -EIO;
1432 
1433 	if (!iwl_mvm_has_new_tx_api(mvm) &&
1434 	    !fw_has_api(&mvm->fw->ucode_capa,
1435 			IWL_UCODE_TLV_API_NEW_BEACON_TEMPLATE))
1436 		return -EINVAL;
1437 
1438 	mutex_lock(&mvm->mutex);
1439 
1440 	for (i = 0; i < NUM_MAC_INDEX_DRIVER; i++) {
1441 		vif = iwl_mvm_rcu_dereference_vif_id(mvm, i, false);
1442 		if (!vif)
1443 			continue;
1444 
1445 		if (vif->type == NL80211_IFTYPE_AP)
1446 			break;
1447 	}
1448 
1449 	if (i == NUM_MAC_INDEX_DRIVER || !vif)
1450 		goto out_err;
1451 
1452 	mvm->hw->extra_beacon_tailroom = len;
1453 
1454 	beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL, 0);
1455 	if (!beacon)
1456 		goto out_err;
1457 
1458 	if (len && hex2bin(skb_put_zero(beacon, len), bin, len)) {
1459 		dev_kfree_skb(beacon);
1460 		goto out_err;
1461 	}
1462 
1463 	mvm->beacon_inject_active = true;
1464 
1465 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
1466 	info = IEEE80211_SKB_CB(beacon);
1467 	rate = iwl_mvm_mac_ctxt_get_beacon_rate(mvm, info, vif);
1468 
1469 	for_each_mvm_vif_valid_link(mvmvif, link_id) {
1470 		beacon_cmd.flags =
1471 			cpu_to_le16(iwl_mvm_mac_ctxt_get_beacon_flags(mvm->fw,
1472 								      rate));
1473 		beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);
1474 		if (iwl_fw_lookup_cmd_ver(mvm->fw, BEACON_TEMPLATE_CMD, 0) > 12)
1475 			beacon_cmd.link_id =
1476 				cpu_to_le32(mvmvif->link[link_id]->fw_link_id);
1477 		else
1478 			beacon_cmd.link_id = cpu_to_le32((u32)mvmvif->id);
1479 
1480 		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1481 					 &beacon_cmd.tim_size,
1482 					 beacon->data, beacon->len);
1483 
1484 		iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1485 						 sizeof(beacon_cmd));
1486 	}
1487 	mutex_unlock(&mvm->mutex);
1488 
1489 	dev_kfree_skb(beacon);
1490 
1491 	return 0;
1492 
1493 out_err:
1494 	mutex_unlock(&mvm->mutex);
1495 	return -EINVAL;
1496 }
1497 
1498 static ssize_t iwl_dbgfs_inject_beacon_ie_write(struct iwl_mvm *mvm,
1499 						char *buf, size_t count,
1500 						loff_t *ppos)
1501 {
1502 	int ret = _iwl_dbgfs_inject_beacon_ie(mvm, buf, count);
1503 
1504 	mvm->hw->extra_beacon_tailroom = 0;
1505 	return ret ?: count;
1506 }
1507 
1508 static ssize_t iwl_dbgfs_inject_beacon_ie_restore_write(struct iwl_mvm *mvm,
1509 							char *buf,
1510 							size_t count,
1511 							loff_t *ppos)
1512 {
1513 	int ret = _iwl_dbgfs_inject_beacon_ie(mvm, NULL, 0);
1514 
1515 	mvm->hw->extra_beacon_tailroom = 0;
1516 	mvm->beacon_inject_active = false;
1517 	return ret ?: count;
1518 }
1519 
1520 static ssize_t iwl_dbgfs_fw_dbg_conf_read(struct file *file,
1521 					  char __user *user_buf,
1522 					  size_t count, loff_t *ppos)
1523 {
1524 	struct iwl_mvm *mvm = file->private_data;
1525 	int conf;
1526 	char buf[8];
1527 	const size_t bufsz = sizeof(buf);
1528 	int pos = 0;
1529 
1530 	mutex_lock(&mvm->mutex);
1531 	conf = mvm->fwrt.dump.conf;
1532 	mutex_unlock(&mvm->mutex);
1533 
1534 	pos += scnprintf(buf + pos, bufsz - pos, "%d\n", conf);
1535 
1536 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1537 }
1538 
1539 static ssize_t iwl_dbgfs_fw_dbg_conf_write(struct iwl_mvm *mvm,
1540 					   char *buf, size_t count,
1541 					   loff_t *ppos)
1542 {
1543 	unsigned int conf_id;
1544 	int ret;
1545 
1546 	if (!iwl_mvm_firmware_running(mvm))
1547 		return -EIO;
1548 
1549 	ret = kstrtouint(buf, 0, &conf_id);
1550 	if (ret)
1551 		return ret;
1552 
1553 	if (WARN_ON(conf_id >= FW_DBG_CONF_MAX))
1554 		return -EINVAL;
1555 
1556 	mutex_lock(&mvm->mutex);
1557 	ret = iwl_fw_start_dbg_conf(&mvm->fwrt, conf_id);
1558 	mutex_unlock(&mvm->mutex);
1559 
1560 	return ret ?: count;
1561 }
1562 
1563 static ssize_t iwl_dbgfs_fw_dbg_collect_write(struct iwl_mvm *mvm,
1564 					      char *buf, size_t count,
1565 					      loff_t *ppos)
1566 {
1567 	if (count == 0)
1568 		return 0;
1569 
1570 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_USER_TRIGGER,
1571 			       NULL);
1572 
1573 	iwl_fw_dbg_collect(&mvm->fwrt, FW_DBG_TRIGGER_USER, buf,
1574 			   (count - 1), NULL);
1575 
1576 	return count;
1577 }
1578 
1579 static ssize_t iwl_dbgfs_dbg_time_point_write(struct iwl_mvm *mvm,
1580 					      char *buf, size_t count,
1581 					      loff_t *ppos)
1582 {
1583 	u32 timepoint;
1584 
1585 	if (kstrtou32(buf, 0, &timepoint))
1586 		return -EINVAL;
1587 
1588 	if (timepoint == IWL_FW_INI_TIME_POINT_INVALID ||
1589 	    timepoint >= IWL_FW_INI_TIME_POINT_NUM)
1590 		return -EINVAL;
1591 
1592 	iwl_dbg_tlv_time_point(&mvm->fwrt, timepoint, NULL);
1593 
1594 	return count;
1595 }
1596 
1597 #define MVM_DEBUGFS_WRITE_FILE_OPS(name, bufsz) \
1598 	_MVM_DEBUGFS_WRITE_FILE_OPS(name, bufsz, struct iwl_mvm)
1599 #define MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz) \
1600 	_MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz, struct iwl_mvm)
1601 #define MVM_DEBUGFS_ADD_FILE_ALIAS(alias, name, parent, mode) do {	\
1602 		debugfs_create_file(alias, mode, parent, mvm,		\
1603 				    &iwl_dbgfs_##name##_ops);		\
1604 	} while (0)
1605 #define MVM_DEBUGFS_ADD_FILE(name, parent, mode) \
1606 	MVM_DEBUGFS_ADD_FILE_ALIAS(#name, name, parent, mode)
1607 
1608 static ssize_t
1609 _iwl_dbgfs_link_sta_wrap_write(ssize_t (*real)(struct ieee80211_link_sta *,
1610 					       struct iwl_mvm_sta *,
1611 					       struct iwl_mvm *,
1612 					       struct iwl_mvm_link_sta *,
1613 					       char *,
1614 					       size_t, loff_t *),
1615 			   struct file *file,
1616 			   char *buf, size_t buf_size, loff_t *ppos)
1617 {
1618 	struct ieee80211_link_sta *link_sta = file->private_data;
1619 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(link_sta->sta);
1620 	struct iwl_mvm *mvm = iwl_mvm_vif_from_mac80211(mvmsta->vif)->mvm;
1621 	struct iwl_mvm_link_sta *mvm_link_sta;
1622 	ssize_t ret;
1623 
1624 	mutex_lock(&mvm->mutex);
1625 
1626 	mvm_link_sta = rcu_dereference_protected(mvmsta->link[link_sta->link_id],
1627 						 lockdep_is_held(&mvm->mutex));
1628 	if (WARN_ON(!mvm_link_sta)) {
1629 		mutex_unlock(&mvm->mutex);
1630 		return -ENODEV;
1631 	}
1632 
1633 	ret = real(link_sta, mvmsta, mvm, mvm_link_sta, buf, buf_size, ppos);
1634 
1635 	mutex_unlock(&mvm->mutex);
1636 
1637 	return ret;
1638 }
1639 
1640 static ssize_t
1641 _iwl_dbgfs_link_sta_wrap_read(ssize_t (*real)(struct ieee80211_link_sta *,
1642 					      struct iwl_mvm_sta *,
1643 					      struct iwl_mvm *,
1644 					      struct iwl_mvm_link_sta *,
1645 					      char __user *,
1646 					      size_t, loff_t *),
1647 			   struct file *file,
1648 			   char __user *user_buf, size_t count, loff_t *ppos)
1649 {
1650 	struct ieee80211_link_sta *link_sta = file->private_data;
1651 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(link_sta->sta);
1652 	struct iwl_mvm *mvm = iwl_mvm_vif_from_mac80211(mvmsta->vif)->mvm;
1653 	struct iwl_mvm_link_sta *mvm_link_sta;
1654 	ssize_t ret;
1655 
1656 	mutex_lock(&mvm->mutex);
1657 
1658 	mvm_link_sta = rcu_dereference_protected(mvmsta->link[link_sta->link_id],
1659 						 lockdep_is_held(&mvm->mutex));
1660 	if (WARN_ON(!mvm_link_sta)) {
1661 		mutex_unlock(&mvm->mutex);
1662 		return -ENODEV;
1663 	}
1664 
1665 	ret = real(link_sta, mvmsta, mvm, mvm_link_sta, user_buf, count, ppos);
1666 
1667 	mutex_unlock(&mvm->mutex);
1668 
1669 	return ret;
1670 }
1671 
1672 #define MVM_DEBUGFS_LINK_STA_WRITE_WRAPPER(name, buflen)		\
1673 static ssize_t _iwl_dbgfs_link_sta_##name##_write(struct file *file,	\
1674 					 const char __user *user_buf,	\
1675 					 size_t count, loff_t *ppos)	\
1676 {									\
1677 	char buf[buflen] = {};						\
1678 	size_t buf_size = min(count, sizeof(buf) -  1);			\
1679 									\
1680 	if (copy_from_user(buf, user_buf, sizeof(buf)))			\
1681 		return -EFAULT;						\
1682 									\
1683 	return _iwl_dbgfs_link_sta_wrap_write(iwl_dbgfs_##name##_write,	\
1684 					      file,			\
1685 					      buf, buf_size, ppos);	\
1686 }									\
1687 
1688 #define MVM_DEBUGFS_LINK_STA_READ_WRAPPER(name)		\
1689 static ssize_t _iwl_dbgfs_link_sta_##name##_read(struct file *file,	\
1690 					 char __user *user_buf,		\
1691 					 size_t count, loff_t *ppos)	\
1692 {									\
1693 	return _iwl_dbgfs_link_sta_wrap_read(iwl_dbgfs_##name##_read,	\
1694 					     file,			\
1695 					     user_buf, count, ppos);	\
1696 }									\
1697 
1698 #define MVM_DEBUGFS_WRITE_LINK_STA_FILE_OPS(name, bufsz)		\
1699 MVM_DEBUGFS_LINK_STA_WRITE_WRAPPER(name, bufsz)				\
1700 static const struct file_operations iwl_dbgfs_link_sta_##name##_ops = {	\
1701 	.write = _iwl_dbgfs_link_sta_##name##_write,			\
1702 	.open = simple_open,						\
1703 	.llseek = generic_file_llseek,					\
1704 }
1705 
1706 #define MVM_DEBUGFS_READ_LINK_STA_FILE_OPS(name)			\
1707 MVM_DEBUGFS_LINK_STA_READ_WRAPPER(name)					\
1708 static const struct file_operations iwl_dbgfs_link_sta_##name##_ops = {	\
1709 	.read = _iwl_dbgfs_link_sta_##name##_read,			\
1710 	.open = simple_open,						\
1711 	.llseek = generic_file_llseek,					\
1712 }
1713 
1714 #define MVM_DEBUGFS_READ_WRITE_LINK_STA_FILE_OPS(name, bufsz)		\
1715 MVM_DEBUGFS_LINK_STA_READ_WRAPPER(name)					\
1716 MVM_DEBUGFS_LINK_STA_WRITE_WRAPPER(name, bufsz)				\
1717 static const struct file_operations iwl_dbgfs_link_sta_##name##_ops = {	\
1718 	.read = _iwl_dbgfs_link_sta_##name##_read,			\
1719 	.write = _iwl_dbgfs_link_sta_##name##_write,			\
1720 	.open = simple_open,						\
1721 	.llseek = generic_file_llseek,					\
1722 }
1723 
1724 #define MVM_DEBUGFS_ADD_LINK_STA_FILE_ALIAS(alias, name, parent, mode)	\
1725 		debugfs_create_file(alias, mode, parent, link_sta,	\
1726 				    &iwl_dbgfs_link_sta_##name##_ops)
1727 #define MVM_DEBUGFS_ADD_LINK_STA_FILE(name, parent, mode) \
1728 	MVM_DEBUGFS_ADD_LINK_STA_FILE_ALIAS(#name, name, parent, mode)
1729 
1730 static ssize_t
1731 iwl_dbgfs_prph_reg_read(struct file *file,
1732 			char __user *user_buf,
1733 			size_t count, loff_t *ppos)
1734 {
1735 	struct iwl_mvm *mvm = file->private_data;
1736 	int pos = 0;
1737 	char buf[32];
1738 	const size_t bufsz = sizeof(buf);
1739 
1740 	if (!mvm->dbgfs_prph_reg_addr)
1741 		return -EINVAL;
1742 
1743 	pos += scnprintf(buf + pos, bufsz - pos, "Reg 0x%x: (0x%x)\n",
1744 		mvm->dbgfs_prph_reg_addr,
1745 		iwl_read_prph(mvm->trans, mvm->dbgfs_prph_reg_addr));
1746 
1747 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1748 }
1749 
1750 static ssize_t
1751 iwl_dbgfs_prph_reg_write(struct iwl_mvm *mvm, char *buf,
1752 			 size_t count, loff_t *ppos)
1753 {
1754 	u8 args;
1755 	u32 value;
1756 
1757 	args = sscanf(buf, "%i %i", &mvm->dbgfs_prph_reg_addr, &value);
1758 	/* if we only want to set the reg address - nothing more to do */
1759 	if (args == 1)
1760 		goto out;
1761 
1762 	/* otherwise, make sure we have both address and value */
1763 	if (args != 2)
1764 		return -EINVAL;
1765 
1766 	iwl_write_prph(mvm->trans, mvm->dbgfs_prph_reg_addr, value);
1767 
1768 out:
1769 	return count;
1770 }
1771 
1772 static ssize_t
1773 iwl_dbgfs_send_echo_cmd_write(struct iwl_mvm *mvm, char *buf,
1774 			      size_t count, loff_t *ppos)
1775 {
1776 	int ret;
1777 
1778 	if (!iwl_mvm_firmware_running(mvm))
1779 		return -EIO;
1780 
1781 	mutex_lock(&mvm->mutex);
1782 	ret = iwl_mvm_send_cmd_pdu(mvm, ECHO_CMD, 0, 0, NULL);
1783 	mutex_unlock(&mvm->mutex);
1784 
1785 	return ret ?: count;
1786 }
1787 
1788 struct iwl_mvm_sniffer_apply {
1789 	struct iwl_mvm *mvm;
1790 	u8 *bssid;
1791 	u16 aid;
1792 };
1793 
1794 static bool iwl_mvm_sniffer_apply(struct iwl_notif_wait_data *notif_data,
1795 				  struct iwl_rx_packet *pkt, void *data)
1796 {
1797 	struct iwl_mvm_sniffer_apply *apply = data;
1798 
1799 	apply->mvm->cur_aid = cpu_to_le16(apply->aid);
1800 	memcpy(apply->mvm->cur_bssid, apply->bssid,
1801 	       sizeof(apply->mvm->cur_bssid));
1802 
1803 	return true;
1804 }
1805 
1806 static ssize_t
1807 iwl_dbgfs_he_sniffer_params_write(struct iwl_mvm *mvm, char *buf,
1808 				  size_t count, loff_t *ppos)
1809 {
1810 	struct iwl_notification_wait wait;
1811 	struct iwl_he_monitor_cmd he_mon_cmd = {};
1812 	struct iwl_mvm_sniffer_apply apply = {
1813 		.mvm = mvm,
1814 	};
1815 	u16 wait_cmds[] = {
1816 		WIDE_ID(DATA_PATH_GROUP, HE_AIR_SNIFFER_CONFIG_CMD),
1817 	};
1818 	u32 aid;
1819 	int ret;
1820 
1821 	if (!iwl_mvm_firmware_running(mvm))
1822 		return -EIO;
1823 
1824 	ret = sscanf(buf, "%x %2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx", &aid,
1825 		     &he_mon_cmd.bssid[0], &he_mon_cmd.bssid[1],
1826 		     &he_mon_cmd.bssid[2], &he_mon_cmd.bssid[3],
1827 		     &he_mon_cmd.bssid[4], &he_mon_cmd.bssid[5]);
1828 	if (ret != 7)
1829 		return -EINVAL;
1830 
1831 	he_mon_cmd.aid = cpu_to_le16(aid);
1832 
1833 	apply.aid = aid;
1834 	apply.bssid = (void *)he_mon_cmd.bssid;
1835 
1836 	mutex_lock(&mvm->mutex);
1837 
1838 	/*
1839 	 * Use the notification waiter to get our function triggered
1840 	 * in sequence with other RX. This ensures that frames we get
1841 	 * on the RX queue _before_ the new configuration is applied
1842 	 * still have mvm->cur_aid pointing to the old AID, and that
1843 	 * frames on the RX queue _after_ the firmware processed the
1844 	 * new configuration (and sent the response, synchronously)
1845 	 * get mvm->cur_aid correctly set to the new AID.
1846 	 */
1847 	iwl_init_notification_wait(&mvm->notif_wait, &wait,
1848 				   wait_cmds, ARRAY_SIZE(wait_cmds),
1849 				   iwl_mvm_sniffer_apply, &apply);
1850 
1851 	ret = iwl_mvm_send_cmd_pdu(mvm,
1852 				   WIDE_ID(DATA_PATH_GROUP, HE_AIR_SNIFFER_CONFIG_CMD),
1853 				   0,
1854 				   sizeof(he_mon_cmd), &he_mon_cmd);
1855 
1856 	/* no need to really wait, we already did anyway */
1857 	iwl_remove_notification(&mvm->notif_wait, &wait);
1858 
1859 	mutex_unlock(&mvm->mutex);
1860 
1861 	return ret ?: count;
1862 }
1863 
1864 static ssize_t
1865 iwl_dbgfs_he_sniffer_params_read(struct file *file, char __user *user_buf,
1866 				 size_t count, loff_t *ppos)
1867 {
1868 	struct iwl_mvm *mvm = file->private_data;
1869 	u8 buf[32];
1870 	int len;
1871 
1872 	len = scnprintf(buf, sizeof(buf),
1873 			"%d %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",
1874 			le16_to_cpu(mvm->cur_aid), mvm->cur_bssid[0],
1875 			mvm->cur_bssid[1], mvm->cur_bssid[2], mvm->cur_bssid[3],
1876 			mvm->cur_bssid[4], mvm->cur_bssid[5]);
1877 
1878 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1879 }
1880 
1881 static ssize_t
1882 iwl_dbgfs_uapsd_noagg_bssids_read(struct file *file, char __user *user_buf,
1883 				  size_t count, loff_t *ppos)
1884 {
1885 	struct iwl_mvm *mvm = file->private_data;
1886 	u8 buf[IWL_MVM_UAPSD_NOAGG_BSSIDS_NUM * ETH_ALEN * 3 + 1];
1887 	unsigned int pos = 0;
1888 	size_t bufsz = sizeof(buf);
1889 	int i;
1890 
1891 	mutex_lock(&mvm->mutex);
1892 
1893 	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++)
1894 		pos += scnprintf(buf + pos, bufsz - pos, "%pM\n",
1895 				 mvm->uapsd_noagg_bssids[i].addr);
1896 
1897 	mutex_unlock(&mvm->mutex);
1898 
1899 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1900 }
1901 
1902 static ssize_t
1903 iwl_dbgfs_ltr_config_write(struct iwl_mvm *mvm,
1904 			   char *buf, size_t count, loff_t *ppos)
1905 {
1906 	int ret;
1907 	struct iwl_ltr_config_cmd ltr_config = {0};
1908 
1909 	if (!iwl_mvm_firmware_running(mvm))
1910 		return -EIO;
1911 
1912 	if (sscanf(buf, "%x,%x,%x,%x,%x,%x,%x",
1913 		   &ltr_config.flags,
1914 		   &ltr_config.static_long,
1915 		   &ltr_config.static_short,
1916 		   &ltr_config.ltr_cfg_values[0],
1917 		   &ltr_config.ltr_cfg_values[1],
1918 		   &ltr_config.ltr_cfg_values[2],
1919 		   &ltr_config.ltr_cfg_values[3]) != 7) {
1920 		return -EINVAL;
1921 	}
1922 
1923 	mutex_lock(&mvm->mutex);
1924 	ret = iwl_mvm_send_cmd_pdu(mvm, LTR_CONFIG, 0, sizeof(ltr_config),
1925 				   &ltr_config);
1926 	mutex_unlock(&mvm->mutex);
1927 
1928 	if (ret)
1929 		IWL_ERR(mvm, "failed to send ltr configuration cmd\n");
1930 
1931 	return ret ?: count;
1932 }
1933 
1934 static ssize_t iwl_dbgfs_rfi_freq_table_write(struct iwl_mvm *mvm, char *buf,
1935 					      size_t count, loff_t *ppos)
1936 {
1937 	int ret = 0;
1938 	u16 op_id;
1939 
1940 	if (kstrtou16(buf, 10, &op_id))
1941 		return -EINVAL;
1942 
1943 	/* value zero triggers re-sending the default table to the device */
1944 	if (!op_id) {
1945 		mutex_lock(&mvm->mutex);
1946 		ret = iwl_rfi_send_config_cmd(mvm, NULL);
1947 		mutex_unlock(&mvm->mutex);
1948 	} else {
1949 		ret = -EOPNOTSUPP; /* in the future a new table will be added */
1950 	}
1951 
1952 	return ret ?: count;
1953 }
1954 
1955 /* The size computation is as follows:
1956  * each number needs at most 3 characters, number of rows is the size of
1957  * the table; So, need 5 chars for the "freq: " part and each tuple afterwards
1958  * needs 6 characters for numbers and 5 for the punctuation around.
1959  */
1960 #define IWL_RFI_BUF_SIZE (IWL_RFI_LUT_INSTALLED_SIZE *\
1961 				(5 + IWL_RFI_LUT_ENTRY_CHANNELS_NUM * (6 + 5)))
1962 
1963 static ssize_t iwl_dbgfs_rfi_freq_table_read(struct file *file,
1964 					     char __user *user_buf,
1965 					     size_t count, loff_t *ppos)
1966 {
1967 	struct iwl_mvm *mvm = file->private_data;
1968 	struct iwl_rfi_freq_table_resp_cmd *resp;
1969 	u32 status;
1970 	char buf[IWL_RFI_BUF_SIZE];
1971 	int i, j, pos = 0;
1972 
1973 	resp = iwl_rfi_get_freq_table(mvm);
1974 	if (IS_ERR(resp))
1975 		return PTR_ERR(resp);
1976 
1977 	status = le32_to_cpu(resp->status);
1978 	if (status != RFI_FREQ_TABLE_OK) {
1979 		scnprintf(buf, IWL_RFI_BUF_SIZE, "status = %d\n", status);
1980 		goto out;
1981 	}
1982 
1983 	for (i = 0; i < ARRAY_SIZE(resp->table); i++) {
1984 		pos += scnprintf(buf + pos, IWL_RFI_BUF_SIZE - pos, "%d: ",
1985 				 resp->table[i].freq);
1986 
1987 		for (j = 0; j < ARRAY_SIZE(resp->table[i].channels); j++)
1988 			pos += scnprintf(buf + pos, IWL_RFI_BUF_SIZE - pos,
1989 					 "(%d, %d) ",
1990 					 resp->table[i].channels[j],
1991 					 resp->table[i].bands[j]);
1992 		pos += scnprintf(buf + pos, IWL_RFI_BUF_SIZE - pos, "\n");
1993 	}
1994 
1995 out:
1996 	kfree(resp);
1997 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1998 }
1999 
2000 MVM_DEBUGFS_READ_WRITE_FILE_OPS(prph_reg, 64);
2001 
2002 /* Device wide debugfs entries */
2003 MVM_DEBUGFS_READ_FILE_OPS(ctdp_budget);
2004 MVM_DEBUGFS_WRITE_FILE_OPS(stop_ctdp, 8);
2005 MVM_DEBUGFS_WRITE_FILE_OPS(force_ctkill, 8);
2006 MVM_DEBUGFS_WRITE_FILE_OPS(tx_flush, 16);
2007 MVM_DEBUGFS_WRITE_FILE_OPS(sta_drain, 8);
2008 MVM_DEBUGFS_WRITE_FILE_OPS(send_echo_cmd, 8);
2009 MVM_DEBUGFS_READ_WRITE_FILE_OPS(sram, 64);
2010 MVM_DEBUGFS_READ_WRITE_FILE_OPS(set_nic_temperature, 64);
2011 MVM_DEBUGFS_READ_FILE_OPS(nic_temp);
2012 MVM_DEBUGFS_READ_FILE_OPS(stations);
2013 MVM_DEBUGFS_READ_LINK_STA_FILE_OPS(rs_data);
2014 MVM_DEBUGFS_READ_FILE_OPS(bt_notif);
2015 MVM_DEBUGFS_READ_FILE_OPS(bt_cmd);
2016 MVM_DEBUGFS_READ_WRITE_FILE_OPS(disable_power_off, 64);
2017 MVM_DEBUGFS_READ_FILE_OPS(fw_rx_stats);
2018 MVM_DEBUGFS_READ_FILE_OPS(drv_rx_stats);
2019 MVM_DEBUGFS_READ_FILE_OPS(fw_ver);
2020 MVM_DEBUGFS_READ_FILE_OPS(phy_integration_ver);
2021 MVM_DEBUGFS_READ_FILE_OPS(tas_get_status);
2022 MVM_DEBUGFS_WRITE_FILE_OPS(fw_restart, 10);
2023 MVM_DEBUGFS_WRITE_FILE_OPS(fw_nmi, 10);
2024 MVM_DEBUGFS_WRITE_FILE_OPS(bt_tx_prio, 10);
2025 MVM_DEBUGFS_WRITE_FILE_OPS(bt_force_ant, 10);
2026 MVM_DEBUGFS_READ_WRITE_FILE_OPS(scan_ant_rxchain, 8);
2027 MVM_DEBUGFS_READ_WRITE_FILE_OPS(fw_dbg_conf, 8);
2028 MVM_DEBUGFS_WRITE_FILE_OPS(fw_dbg_collect, 64);
2029 MVM_DEBUGFS_WRITE_FILE_OPS(dbg_time_point, 64);
2030 MVM_DEBUGFS_WRITE_FILE_OPS(indirection_tbl,
2031 			   (IWL_RSS_INDIRECTION_TABLE_SIZE * 2));
2032 MVM_DEBUGFS_WRITE_FILE_OPS(inject_packet, 512);
2033 MVM_DEBUGFS_WRITE_FILE_OPS(inject_beacon_ie, 512);
2034 MVM_DEBUGFS_WRITE_FILE_OPS(inject_beacon_ie_restore, 512);
2035 
2036 MVM_DEBUGFS_READ_FILE_OPS(uapsd_noagg_bssids);
2037 
2038 #ifdef CONFIG_ACPI
2039 MVM_DEBUGFS_READ_FILE_OPS(sar_geo_profile);
2040 MVM_DEBUGFS_READ_FILE_OPS(wifi_6e_enable);
2041 #endif
2042 
2043 MVM_DEBUGFS_READ_WRITE_LINK_STA_FILE_OPS(amsdu_len, 16);
2044 
2045 MVM_DEBUGFS_READ_WRITE_FILE_OPS(he_sniffer_params, 32);
2046 
2047 MVM_DEBUGFS_WRITE_FILE_OPS(ltr_config, 512);
2048 MVM_DEBUGFS_READ_WRITE_FILE_OPS(rfi_freq_table, 16);
2049 
2050 static ssize_t iwl_dbgfs_mem_read(struct file *file, char __user *user_buf,
2051 				  size_t count, loff_t *ppos)
2052 {
2053 	struct iwl_mvm *mvm = file->private_data;
2054 	struct iwl_dbg_mem_access_cmd cmd = {};
2055 	struct iwl_dbg_mem_access_rsp *rsp;
2056 	struct iwl_host_cmd hcmd = {
2057 		.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
2058 		.data = { &cmd, },
2059 		.len = { sizeof(cmd) },
2060 	};
2061 	size_t delta;
2062 	ssize_t ret, len;
2063 
2064 	if (!iwl_mvm_firmware_running(mvm))
2065 		return -EIO;
2066 
2067 	hcmd.id = WIDE_ID(DEBUG_GROUP, *ppos >> 24 ? UMAC_RD_WR : LMAC_RD_WR);
2068 	cmd.op = cpu_to_le32(DEBUG_MEM_OP_READ);
2069 
2070 	/* Take care of alignment of both the position and the length */
2071 	delta = *ppos & 0x3;
2072 	cmd.addr = cpu_to_le32(*ppos - delta);
2073 	cmd.len = cpu_to_le32(min(ALIGN(count + delta, 4) / 4,
2074 				  (size_t)DEBUG_MEM_MAX_SIZE_DWORDS));
2075 
2076 	mutex_lock(&mvm->mutex);
2077 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
2078 	mutex_unlock(&mvm->mutex);
2079 
2080 	if (ret < 0)
2081 		return ret;
2082 
2083 	if (iwl_rx_packet_payload_len(hcmd.resp_pkt) < sizeof(*rsp)) {
2084 		ret = -EIO;
2085 		goto out;
2086 	}
2087 
2088 	rsp = (void *)hcmd.resp_pkt->data;
2089 	if (le32_to_cpu(rsp->status) != DEBUG_MEM_STATUS_SUCCESS) {
2090 		ret = -ENXIO;
2091 		goto out;
2092 	}
2093 
2094 	len = min((size_t)le32_to_cpu(rsp->len) << 2,
2095 		  iwl_rx_packet_payload_len(hcmd.resp_pkt) - sizeof(*rsp));
2096 	len = min(len - delta, count);
2097 	if (len < 0) {
2098 		ret = -EFAULT;
2099 		goto out;
2100 	}
2101 
2102 	ret = len - copy_to_user(user_buf, (u8 *)rsp->data + delta, len);
2103 	*ppos += ret;
2104 
2105 out:
2106 	iwl_free_resp(&hcmd);
2107 	return ret;
2108 }
2109 
2110 static ssize_t iwl_dbgfs_mem_write(struct file *file,
2111 				   const char __user *user_buf, size_t count,
2112 				   loff_t *ppos)
2113 {
2114 	struct iwl_mvm *mvm = file->private_data;
2115 	struct iwl_dbg_mem_access_cmd *cmd;
2116 	struct iwl_dbg_mem_access_rsp *rsp;
2117 	struct iwl_host_cmd hcmd = {};
2118 	size_t cmd_size;
2119 	size_t data_size;
2120 	u32 op, len;
2121 	ssize_t ret;
2122 
2123 	if (!iwl_mvm_firmware_running(mvm))
2124 		return -EIO;
2125 
2126 	hcmd.id = WIDE_ID(DEBUG_GROUP, *ppos >> 24 ? UMAC_RD_WR : LMAC_RD_WR);
2127 
2128 	if (*ppos & 0x3 || count < 4) {
2129 		op = DEBUG_MEM_OP_WRITE_BYTES;
2130 		len = min(count, (size_t)(4 - (*ppos & 0x3)));
2131 		data_size = len;
2132 	} else {
2133 		op = DEBUG_MEM_OP_WRITE;
2134 		len = min(count >> 2, (size_t)DEBUG_MEM_MAX_SIZE_DWORDS);
2135 		data_size = len << 2;
2136 	}
2137 
2138 	cmd_size = sizeof(*cmd) + ALIGN(data_size, 4);
2139 	cmd = kzalloc(cmd_size, GFP_KERNEL);
2140 	if (!cmd)
2141 		return -ENOMEM;
2142 
2143 	cmd->op = cpu_to_le32(op);
2144 	cmd->len = cpu_to_le32(len);
2145 	cmd->addr = cpu_to_le32(*ppos);
2146 	if (copy_from_user((void *)cmd->data, user_buf, data_size)) {
2147 		kfree(cmd);
2148 		return -EFAULT;
2149 	}
2150 
2151 	hcmd.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
2152 	hcmd.data[0] = (void *)cmd;
2153 	hcmd.len[0] = cmd_size;
2154 
2155 	mutex_lock(&mvm->mutex);
2156 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
2157 	mutex_unlock(&mvm->mutex);
2158 
2159 	kfree(cmd);
2160 
2161 	if (ret < 0)
2162 		return ret;
2163 
2164 	if (iwl_rx_packet_payload_len(hcmd.resp_pkt) < sizeof(*rsp)) {
2165 		ret = -EIO;
2166 		goto out;
2167 	}
2168 
2169 	rsp = (void *)hcmd.resp_pkt->data;
2170 	if (rsp->status != DEBUG_MEM_STATUS_SUCCESS) {
2171 		ret = -ENXIO;
2172 		goto out;
2173 	}
2174 
2175 	ret = data_size;
2176 	*ppos += ret;
2177 
2178 out:
2179 	iwl_free_resp(&hcmd);
2180 	return ret;
2181 }
2182 
2183 static const struct file_operations iwl_dbgfs_mem_ops = {
2184 	.read = iwl_dbgfs_mem_read,
2185 	.write = iwl_dbgfs_mem_write,
2186 	.open = simple_open,
2187 	.llseek = default_llseek,
2188 };
2189 
2190 void iwl_mvm_link_sta_add_debugfs(struct ieee80211_hw *hw,
2191 				  struct ieee80211_vif *vif,
2192 				  struct ieee80211_link_sta *link_sta,
2193 				  struct dentry *dir)
2194 {
2195 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2196 
2197 	if (iwl_mvm_has_tlc_offload(mvm)) {
2198 		MVM_DEBUGFS_ADD_LINK_STA_FILE(rs_data, dir, 0400);
2199 	}
2200 
2201 	MVM_DEBUGFS_ADD_LINK_STA_FILE(amsdu_len, dir, 0600);
2202 }
2203 
2204 void iwl_mvm_dbgfs_register(struct iwl_mvm *mvm)
2205 {
2206 	struct dentry *bcast_dir __maybe_unused;
2207 
2208 	spin_lock_init(&mvm->drv_stats_lock);
2209 
2210 	MVM_DEBUGFS_ADD_FILE(tx_flush, mvm->debugfs_dir, 0200);
2211 	MVM_DEBUGFS_ADD_FILE(sta_drain, mvm->debugfs_dir, 0200);
2212 	MVM_DEBUGFS_ADD_FILE(sram, mvm->debugfs_dir, 0600);
2213 	MVM_DEBUGFS_ADD_FILE(set_nic_temperature, mvm->debugfs_dir, 0600);
2214 	MVM_DEBUGFS_ADD_FILE(nic_temp, mvm->debugfs_dir, 0400);
2215 	MVM_DEBUGFS_ADD_FILE(ctdp_budget, mvm->debugfs_dir, 0400);
2216 	MVM_DEBUGFS_ADD_FILE(stop_ctdp, mvm->debugfs_dir, 0200);
2217 	MVM_DEBUGFS_ADD_FILE(force_ctkill, mvm->debugfs_dir, 0200);
2218 	MVM_DEBUGFS_ADD_FILE(stations, mvm->debugfs_dir, 0400);
2219 	MVM_DEBUGFS_ADD_FILE(bt_notif, mvm->debugfs_dir, 0400);
2220 	MVM_DEBUGFS_ADD_FILE(bt_cmd, mvm->debugfs_dir, 0400);
2221 	MVM_DEBUGFS_ADD_FILE(disable_power_off, mvm->debugfs_dir, 0600);
2222 	MVM_DEBUGFS_ADD_FILE(fw_ver, mvm->debugfs_dir, 0400);
2223 	MVM_DEBUGFS_ADD_FILE(fw_rx_stats, mvm->debugfs_dir, 0400);
2224 	MVM_DEBUGFS_ADD_FILE(drv_rx_stats, mvm->debugfs_dir, 0400);
2225 	MVM_DEBUGFS_ADD_FILE(fw_restart, mvm->debugfs_dir, 0200);
2226 	MVM_DEBUGFS_ADD_FILE(fw_nmi, mvm->debugfs_dir, 0200);
2227 	MVM_DEBUGFS_ADD_FILE(bt_tx_prio, mvm->debugfs_dir, 0200);
2228 	MVM_DEBUGFS_ADD_FILE(bt_force_ant, mvm->debugfs_dir, 0200);
2229 	MVM_DEBUGFS_ADD_FILE(scan_ant_rxchain, mvm->debugfs_dir, 0600);
2230 	MVM_DEBUGFS_ADD_FILE(prph_reg, mvm->debugfs_dir, 0600);
2231 	MVM_DEBUGFS_ADD_FILE(fw_dbg_conf, mvm->debugfs_dir, 0600);
2232 	MVM_DEBUGFS_ADD_FILE(fw_dbg_collect, mvm->debugfs_dir, 0200);
2233 	MVM_DEBUGFS_ADD_FILE(dbg_time_point, mvm->debugfs_dir, 0200);
2234 	MVM_DEBUGFS_ADD_FILE(send_echo_cmd, mvm->debugfs_dir, 0200);
2235 	MVM_DEBUGFS_ADD_FILE(indirection_tbl, mvm->debugfs_dir, 0200);
2236 	MVM_DEBUGFS_ADD_FILE(inject_packet, mvm->debugfs_dir, 0200);
2237 	MVM_DEBUGFS_ADD_FILE(inject_beacon_ie, mvm->debugfs_dir, 0200);
2238 	MVM_DEBUGFS_ADD_FILE(inject_beacon_ie_restore, mvm->debugfs_dir, 0200);
2239 	MVM_DEBUGFS_ADD_FILE(rfi_freq_table, mvm->debugfs_dir, 0600);
2240 
2241 	if (mvm->fw->phy_integration_ver)
2242 		MVM_DEBUGFS_ADD_FILE(phy_integration_ver, mvm->debugfs_dir, 0400);
2243 	MVM_DEBUGFS_ADD_FILE(tas_get_status, mvm->debugfs_dir, 0400);
2244 #ifdef CONFIG_ACPI
2245 	MVM_DEBUGFS_ADD_FILE(sar_geo_profile, mvm->debugfs_dir, 0400);
2246 	MVM_DEBUGFS_ADD_FILE(wifi_6e_enable, mvm->debugfs_dir, 0400);
2247 #endif
2248 	MVM_DEBUGFS_ADD_FILE(he_sniffer_params, mvm->debugfs_dir, 0600);
2249 
2250 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_SET_LTR_GEN2))
2251 		MVM_DEBUGFS_ADD_FILE(ltr_config, mvm->debugfs_dir, 0200);
2252 
2253 	debugfs_create_bool("enable_scan_iteration_notif", 0600,
2254 			    mvm->debugfs_dir, &mvm->scan_iter_notif_enabled);
2255 	debugfs_create_bool("drop_bcn_ap_mode", 0600, mvm->debugfs_dir,
2256 			    &mvm->drop_bcn_ap_mode);
2257 
2258 	MVM_DEBUGFS_ADD_FILE(uapsd_noagg_bssids, mvm->debugfs_dir, S_IRUSR);
2259 
2260 #ifdef CONFIG_PM_SLEEP
2261 	MVM_DEBUGFS_ADD_FILE(d3_test, mvm->debugfs_dir, 0400);
2262 	debugfs_create_bool("d3_wake_sysassert", 0600, mvm->debugfs_dir,
2263 			    &mvm->d3_wake_sysassert);
2264 	debugfs_create_u32("last_netdetect_scans", 0400, mvm->debugfs_dir,
2265 			   &mvm->last_netdetect_scans);
2266 #endif
2267 
2268 	debugfs_create_u8("ps_disabled", 0400, mvm->debugfs_dir,
2269 			  &mvm->ps_disabled);
2270 	debugfs_create_blob("nvm_hw", 0400, mvm->debugfs_dir,
2271 			    &mvm->nvm_hw_blob);
2272 	debugfs_create_blob("nvm_sw", 0400, mvm->debugfs_dir,
2273 			    &mvm->nvm_sw_blob);
2274 	debugfs_create_blob("nvm_calib", 0400, mvm->debugfs_dir,
2275 			    &mvm->nvm_calib_blob);
2276 	debugfs_create_blob("nvm_prod", 0400, mvm->debugfs_dir,
2277 			    &mvm->nvm_prod_blob);
2278 	debugfs_create_blob("nvm_phy_sku", 0400, mvm->debugfs_dir,
2279 			    &mvm->nvm_phy_sku_blob);
2280 	debugfs_create_blob("nvm_reg", S_IRUSR,
2281 			    mvm->debugfs_dir, &mvm->nvm_reg_blob);
2282 
2283 	debugfs_create_file("mem", 0600, mvm->debugfs_dir, mvm,
2284 			    &iwl_dbgfs_mem_ops);
2285 
2286 #if defined(__linux__)
2287 	/*
2288 	 * Create a symlink with mac80211. It will be removed when mac80211
2289 	 * exists (before the opmode exists which removes the target.)
2290 	 */
2291 	if (!IS_ERR(mvm->debugfs_dir)) {
2292 		char buf[100];
2293 
2294 		snprintf(buf, 100, "../../%pd2", mvm->debugfs_dir->d_parent);
2295 		debugfs_create_symlink("iwlwifi", mvm->hw->wiphy->debugfsdir,
2296 				       buf);
2297 	}
2298 #endif
2299 }
2300