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