xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/debugfs.c (revision f315296c92fd4b7716bdea17f727ab431891dc3b)
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 "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_mvm_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 	};
556 	const char * const tas_current_status[TAS_DYNA_STATUS_MAX] = {
557 		[TAS_DYNA_INACTIVE] = "INACTIVE",
558 		[TAS_DYNA_INACTIVE_MVM_MODE] =
559 			"inactive due to mvm mode",
560 		[TAS_DYNA_INACTIVE_TRIGGER_MODE] =
561 			"inactive due to trigger mode",
562 		[TAS_DYNA_INACTIVE_BLOCK_LISTED] =
563 			"inactive due to block listed",
564 		[TAS_DYNA_INACTIVE_UHB_NON_US] =
565 			"inactive due to uhb non US",
566 		[TAS_DYNA_ACTIVE] = "ACTIVE",
567 	};
568 	struct iwl_host_cmd hcmd = {
569 		.id = WIDE_ID(DEBUG_GROUP, GET_TAS_STATUS),
570 		.flags = CMD_WANT_SKB,
571 		.len = { 0, },
572 		.data = { NULL, },
573 	};
574 	int ret, i, tmp;
575 	bool tas_enabled = false;
576 	unsigned long dyn_status;
577 
578 	if (!iwl_mvm_firmware_running(mvm))
579 		return -ENODEV;
580 
581 	if (iwl_fw_lookup_notif_ver(mvm->fw, DEBUG_GROUP, GET_TAS_STATUS,
582 				    0) != 3)
583 		return -EOPNOTSUPP;
584 
585 	mutex_lock(&mvm->mutex);
586 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
587 	mutex_unlock(&mvm->mutex);
588 	if (ret < 0)
589 		return ret;
590 
591 	buff = kzalloc(bufsz, GFP_KERNEL);
592 	if (!buff)
593 		return -ENOMEM;
594 	pos = buff;
595 	endpos = pos + bufsz;
596 
597 	rsp = (void *)hcmd.resp_pkt->data;
598 
599 	pos += scnprintf(pos, endpos - pos, "TAS Conclusion:\n");
600 	for (i = 0; i < rsp->in_dual_radio + 1; i++) {
601 		if (rsp->tas_status_mac[i].band != TAS_LMAC_BAND_INVALID &&
602 		    rsp->tas_status_mac[i].dynamic_status & BIT(TAS_DYNA_ACTIVE)) {
603 			pos += scnprintf(pos, endpos - pos, "\tON for ");
604 			switch (rsp->tas_status_mac[i].band) {
605 			case TAS_LMAC_BAND_HB:
606 				pos += scnprintf(pos, endpos - pos, "HB\n");
607 				break;
608 			case TAS_LMAC_BAND_LB:
609 				pos += scnprintf(pos, endpos - pos, "LB\n");
610 				break;
611 			case TAS_LMAC_BAND_UHB:
612 				pos += scnprintf(pos, endpos - pos, "UHB\n");
613 				break;
614 			case TAS_LMAC_BAND_INVALID:
615 				pos += scnprintf(pos, endpos - pos,
616 						 "INVALID BAND\n");
617 				break;
618 			default:
619 				pos += scnprintf(pos, endpos - pos,
620 						 "Unsupported band (%d)\n",
621 						 rsp->tas_status_mac[i].band);
622 				goto out;
623 			}
624 			tas_enabled = true;
625 		}
626 	}
627 	if (!tas_enabled)
628 		pos += scnprintf(pos, endpos - pos, "\tOFF\n");
629 
630 	pos += scnprintf(pos, endpos - pos, "TAS Report\n");
631 	pos += scnprintf(pos, endpos - pos, "TAS FW version: %d\n",
632 			 rsp->tas_fw_version);
633 	pos += scnprintf(pos, endpos - pos, "Is UHB enabled for USA?: %s\n",
634 			 rsp->is_uhb_for_usa_enable ? "True" : "False");
635 
636 	if (fw_has_capa(&mvm->fw->ucode_capa,
637 			IWL_UCODE_TLV_CAPA_UHB_CANADA_TAS_SUPPORT))
638 		pos += scnprintf(pos, endpos - pos,
639 				 "Is UHB enabled for CANADA?: %s\n",
640 				 rsp->uhb_allowed_flags &
641 				 TAS_UHB_ALLOWED_CANADA ? "True" : "False");
642 
643 	pos += scnprintf(pos, endpos - pos, "Current MCC: 0x%x\n",
644 			 le16_to_cpu(rsp->curr_mcc));
645 
646 	pos += scnprintf(pos, endpos - pos, "Block list entries:");
647 	for (i = 0; i < IWL_WTAS_BLACK_LIST_MAX; i++)
648 		pos += scnprintf(pos, endpos - pos, " 0x%x",
649 				 le16_to_cpu(rsp->block_list[i]));
650 
651 	pos += scnprintf(pos, endpos - pos, "\nOEM name: %s\n",
652 			 dmi_get_system_info(DMI_SYS_VENDOR) ?: "<unknown>");
653 	pos += scnprintf(pos, endpos - pos, "\tVendor In Approved List: %s\n",
654 			 iwl_is_tas_approved() ? "YES" : "NO");
655 	pos += scnprintf(pos, endpos - pos,
656 			 "\tDo TAS Support Dual Radio?: %s\n",
657 			 rsp->in_dual_radio ? "TRUE" : "FALSE");
658 
659 	for (i = 0; i < rsp->in_dual_radio + 1; i++) {
660 		if (rsp->tas_status_mac[i].static_status == 0) {
661 			pos += scnprintf(pos, endpos - pos,
662 					 "Static status: disabled\n");
663 			pos += scnprintf(pos, endpos - pos,
664 					 "Static disabled reason: %s (0)\n",
665 					 tas_dis_reason[0]);
666 			goto out;
667 		}
668 
669 		pos += scnprintf(pos, endpos - pos, "TAS status for ");
670 		switch (rsp->tas_status_mac[i].band) {
671 		case TAS_LMAC_BAND_HB:
672 			pos += scnprintf(pos, endpos - pos, "High band\n");
673 			break;
674 		case TAS_LMAC_BAND_LB:
675 			pos += scnprintf(pos, endpos - pos, "Low band\n");
676 			break;
677 		case TAS_LMAC_BAND_UHB:
678 			pos += scnprintf(pos, endpos - pos,
679 					 "Ultra high band\n");
680 			break;
681 		case TAS_LMAC_BAND_INVALID:
682 			pos += scnprintf(pos, endpos - pos,
683 					 "INVALID band\n");
684 			break;
685 		default:
686 			pos += scnprintf(pos, endpos - pos,
687 					 "Unsupported band (%d)\n",
688 					 rsp->tas_status_mac[i].band);
689 			goto out;
690 		}
691 		pos += scnprintf(pos, endpos - pos, "Static status: %sabled\n",
692 				 rsp->tas_status_mac[i].static_status ?
693 				 "En" : "Dis");
694 		pos += scnprintf(pos, endpos - pos,
695 				 "\tStatic Disabled Reason: ");
696 		if (rsp->tas_status_mac[i].static_dis_reason < TAS_DISABLED_REASON_MAX)
697 			pos += scnprintf(pos, endpos - pos, "%s (%d)\n",
698 					 tas_dis_reason[rsp->tas_status_mac[i].static_dis_reason],
699 					 rsp->tas_status_mac[i].static_dis_reason);
700 		else
701 			pos += scnprintf(pos, endpos - pos,
702 					 "unsupported value (%d)\n",
703 					 rsp->tas_status_mac[i].static_dis_reason);
704 
705 		pos += scnprintf(pos, endpos - pos, "Dynamic status:\n");
706 		dyn_status = (rsp->tas_status_mac[i].dynamic_status);
707 		for_each_set_bit(tmp, &dyn_status, sizeof(dyn_status)) {
708 			if (tmp >= 0 && tmp < TAS_DYNA_STATUS_MAX)
709 				pos += scnprintf(pos, endpos - pos,
710 						 "\t%s (%d)\n",
711 						 tas_current_status[tmp], tmp);
712 		}
713 
714 		pos += scnprintf(pos, endpos - pos,
715 				 "Is near disconnection?: %s\n",
716 				 rsp->tas_status_mac[i].near_disconnection ?
717 				 "True" : "False");
718 		tmp = le16_to_cpu(rsp->tas_status_mac[i].max_reg_pwr_limit);
719 		pos += scnprintf(pos, endpos - pos,
720 				 "Max. regulatory pwr limit (dBm): %d.%03d\n",
721 				 tmp / 8, 125 * (tmp % 8));
722 		tmp = le16_to_cpu(rsp->tas_status_mac[i].sar_limit);
723 		pos += scnprintf(pos, endpos - pos,
724 				 "SAR limit (dBm): %d.%03d\n",
725 				 tmp / 8, 125 * (tmp % 8));
726 	}
727 
728 out:
729 	ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
730 	kfree(buff);
731 	iwl_free_resp(&hcmd);
732 	return ret;
733 }
734 
iwl_dbgfs_phy_integration_ver_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)735 static ssize_t iwl_dbgfs_phy_integration_ver_read(struct file *file,
736 						  char __user *user_buf,
737 						  size_t count, loff_t *ppos)
738 {
739 	struct iwl_mvm *mvm = file->private_data;
740 	char *buf;
741 	size_t bufsz;
742 	int pos;
743 	ssize_t ret;
744 
745 	bufsz = mvm->fw->phy_integration_ver_len + 2;
746 	buf = kmalloc(bufsz, GFP_KERNEL);
747 	if (!buf)
748 		return -ENOMEM;
749 
750 	pos = scnprintf(buf, bufsz, "%.*s\n", mvm->fw->phy_integration_ver_len,
751 			mvm->fw->phy_integration_ver);
752 
753 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
754 
755 	kfree(buf);
756 	return ret;
757 }
758 
759 #define PRINT_STATS_LE32(_struct, _memb)				\
760 			 pos += scnprintf(buf + pos, bufsz - pos,	\
761 					  fmt_table, #_memb,		\
762 					  le32_to_cpu(_struct->_memb))
763 
iwl_dbgfs_fw_rx_stats_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)764 static ssize_t iwl_dbgfs_fw_rx_stats_read(struct file *file,
765 					  char __user *user_buf, size_t count,
766 					  loff_t *ppos)
767 {
768 	struct iwl_mvm *mvm = file->private_data;
769 	static const char *fmt_table = "\t%-30s %10u\n";
770 	static const char *fmt_header = "%-32s\n";
771 	int pos = 0;
772 	char *buf;
773 	int ret;
774 	size_t bufsz;
775 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
776 					   WIDE_ID(SYSTEM_GROUP,
777 						   SYSTEM_STATISTICS_CMD),
778 					   IWL_FW_CMD_VER_UNKNOWN);
779 
780 	if (cmd_ver != IWL_FW_CMD_VER_UNKNOWN)
781 		return -EOPNOTSUPP;
782 
783 	if (iwl_mvm_has_new_rx_stats_api(mvm))
784 		bufsz = ((sizeof(struct mvm_statistics_rx) /
785 			  sizeof(__le32)) * 43) + (4 * 33) + 1;
786 	else
787 		/* 43 = size of each data line; 33 = size of each header */
788 		bufsz = ((sizeof(struct mvm_statistics_rx_v3) /
789 			  sizeof(__le32)) * 43) + (4 * 33) + 1;
790 
791 	buf = kzalloc(bufsz, GFP_KERNEL);
792 	if (!buf)
793 		return -ENOMEM;
794 
795 	mutex_lock(&mvm->mutex);
796 
797 	if (iwl_mvm_firmware_running(mvm))
798 		iwl_mvm_request_statistics(mvm, false);
799 
800 	pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
801 			 "Statistics_Rx - OFDM");
802 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
803 		struct mvm_statistics_rx_phy_v2 *ofdm = &mvm->rx_stats_v3.ofdm;
804 
805 		PRINT_STATS_LE32(ofdm, ina_cnt);
806 		PRINT_STATS_LE32(ofdm, fina_cnt);
807 		PRINT_STATS_LE32(ofdm, plcp_err);
808 		PRINT_STATS_LE32(ofdm, crc32_err);
809 		PRINT_STATS_LE32(ofdm, overrun_err);
810 		PRINT_STATS_LE32(ofdm, early_overrun_err);
811 		PRINT_STATS_LE32(ofdm, crc32_good);
812 		PRINT_STATS_LE32(ofdm, false_alarm_cnt);
813 		PRINT_STATS_LE32(ofdm, fina_sync_err_cnt);
814 		PRINT_STATS_LE32(ofdm, sfd_timeout);
815 		PRINT_STATS_LE32(ofdm, fina_timeout);
816 		PRINT_STATS_LE32(ofdm, unresponded_rts);
817 		PRINT_STATS_LE32(ofdm, rxe_frame_lmt_overrun);
818 		PRINT_STATS_LE32(ofdm, sent_ack_cnt);
819 		PRINT_STATS_LE32(ofdm, sent_cts_cnt);
820 		PRINT_STATS_LE32(ofdm, sent_ba_rsp_cnt);
821 		PRINT_STATS_LE32(ofdm, dsp_self_kill);
822 		PRINT_STATS_LE32(ofdm, mh_format_err);
823 		PRINT_STATS_LE32(ofdm, re_acq_main_rssi_sum);
824 		PRINT_STATS_LE32(ofdm, reserved);
825 	} else {
826 		struct mvm_statistics_rx_phy *ofdm = &mvm->rx_stats.ofdm;
827 
828 		PRINT_STATS_LE32(ofdm, unresponded_rts);
829 		PRINT_STATS_LE32(ofdm, rxe_frame_lmt_overrun);
830 		PRINT_STATS_LE32(ofdm, sent_ba_rsp_cnt);
831 		PRINT_STATS_LE32(ofdm, dsp_self_kill);
832 		PRINT_STATS_LE32(ofdm, reserved);
833 	}
834 
835 	pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
836 			 "Statistics_Rx - CCK");
837 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
838 		struct mvm_statistics_rx_phy_v2 *cck = &mvm->rx_stats_v3.cck;
839 
840 		PRINT_STATS_LE32(cck, ina_cnt);
841 		PRINT_STATS_LE32(cck, fina_cnt);
842 		PRINT_STATS_LE32(cck, plcp_err);
843 		PRINT_STATS_LE32(cck, crc32_err);
844 		PRINT_STATS_LE32(cck, overrun_err);
845 		PRINT_STATS_LE32(cck, early_overrun_err);
846 		PRINT_STATS_LE32(cck, crc32_good);
847 		PRINT_STATS_LE32(cck, false_alarm_cnt);
848 		PRINT_STATS_LE32(cck, fina_sync_err_cnt);
849 		PRINT_STATS_LE32(cck, sfd_timeout);
850 		PRINT_STATS_LE32(cck, fina_timeout);
851 		PRINT_STATS_LE32(cck, unresponded_rts);
852 		PRINT_STATS_LE32(cck, rxe_frame_lmt_overrun);
853 		PRINT_STATS_LE32(cck, sent_ack_cnt);
854 		PRINT_STATS_LE32(cck, sent_cts_cnt);
855 		PRINT_STATS_LE32(cck, sent_ba_rsp_cnt);
856 		PRINT_STATS_LE32(cck, dsp_self_kill);
857 		PRINT_STATS_LE32(cck, mh_format_err);
858 		PRINT_STATS_LE32(cck, re_acq_main_rssi_sum);
859 		PRINT_STATS_LE32(cck, reserved);
860 	} else {
861 		struct mvm_statistics_rx_phy *cck = &mvm->rx_stats.cck;
862 
863 		PRINT_STATS_LE32(cck, unresponded_rts);
864 		PRINT_STATS_LE32(cck, rxe_frame_lmt_overrun);
865 		PRINT_STATS_LE32(cck, sent_ba_rsp_cnt);
866 		PRINT_STATS_LE32(cck, dsp_self_kill);
867 		PRINT_STATS_LE32(cck, reserved);
868 	}
869 
870 	pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
871 			 "Statistics_Rx - GENERAL");
872 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
873 		struct mvm_statistics_rx_non_phy_v3 *general =
874 			&mvm->rx_stats_v3.general;
875 
876 		PRINT_STATS_LE32(general, bogus_cts);
877 		PRINT_STATS_LE32(general, bogus_ack);
878 		PRINT_STATS_LE32(general, non_bssid_frames);
879 		PRINT_STATS_LE32(general, filtered_frames);
880 		PRINT_STATS_LE32(general, non_channel_beacons);
881 		PRINT_STATS_LE32(general, channel_beacons);
882 		PRINT_STATS_LE32(general, num_missed_bcon);
883 		PRINT_STATS_LE32(general, adc_rx_saturation_time);
884 		PRINT_STATS_LE32(general, ina_detection_search_time);
885 		PRINT_STATS_LE32(general, beacon_silence_rssi_a);
886 		PRINT_STATS_LE32(general, beacon_silence_rssi_b);
887 		PRINT_STATS_LE32(general, beacon_silence_rssi_c);
888 		PRINT_STATS_LE32(general, interference_data_flag);
889 		PRINT_STATS_LE32(general, channel_load);
890 		PRINT_STATS_LE32(general, dsp_false_alarms);
891 		PRINT_STATS_LE32(general, beacon_rssi_a);
892 		PRINT_STATS_LE32(general, beacon_rssi_b);
893 		PRINT_STATS_LE32(general, beacon_rssi_c);
894 		PRINT_STATS_LE32(general, beacon_energy_a);
895 		PRINT_STATS_LE32(general, beacon_energy_b);
896 		PRINT_STATS_LE32(general, beacon_energy_c);
897 		PRINT_STATS_LE32(general, num_bt_kills);
898 		PRINT_STATS_LE32(general, mac_id);
899 		PRINT_STATS_LE32(general, directed_data_mpdu);
900 	} else {
901 		struct mvm_statistics_rx_non_phy *general =
902 			&mvm->rx_stats.general;
903 
904 		PRINT_STATS_LE32(general, bogus_cts);
905 		PRINT_STATS_LE32(general, bogus_ack);
906 		PRINT_STATS_LE32(general, non_channel_beacons);
907 		PRINT_STATS_LE32(general, channel_beacons);
908 		PRINT_STATS_LE32(general, num_missed_bcon);
909 		PRINT_STATS_LE32(general, adc_rx_saturation_time);
910 		PRINT_STATS_LE32(general, ina_detection_search_time);
911 		PRINT_STATS_LE32(general, beacon_silence_rssi_a);
912 		PRINT_STATS_LE32(general, beacon_silence_rssi_b);
913 		PRINT_STATS_LE32(general, beacon_silence_rssi_c);
914 		PRINT_STATS_LE32(general, interference_data_flag);
915 		PRINT_STATS_LE32(general, channel_load);
916 		PRINT_STATS_LE32(general, beacon_rssi_a);
917 		PRINT_STATS_LE32(general, beacon_rssi_b);
918 		PRINT_STATS_LE32(general, beacon_rssi_c);
919 		PRINT_STATS_LE32(general, beacon_energy_a);
920 		PRINT_STATS_LE32(general, beacon_energy_b);
921 		PRINT_STATS_LE32(general, beacon_energy_c);
922 		PRINT_STATS_LE32(general, num_bt_kills);
923 		PRINT_STATS_LE32(general, mac_id);
924 	}
925 
926 	pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
927 			 "Statistics_Rx - HT");
928 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
929 		struct mvm_statistics_rx_ht_phy_v1 *ht =
930 			&mvm->rx_stats_v3.ofdm_ht;
931 
932 		PRINT_STATS_LE32(ht, plcp_err);
933 		PRINT_STATS_LE32(ht, overrun_err);
934 		PRINT_STATS_LE32(ht, early_overrun_err);
935 		PRINT_STATS_LE32(ht, crc32_good);
936 		PRINT_STATS_LE32(ht, crc32_err);
937 		PRINT_STATS_LE32(ht, mh_format_err);
938 		PRINT_STATS_LE32(ht, agg_crc32_good);
939 		PRINT_STATS_LE32(ht, agg_mpdu_cnt);
940 		PRINT_STATS_LE32(ht, agg_cnt);
941 		PRINT_STATS_LE32(ht, unsupport_mcs);
942 	} else {
943 		struct mvm_statistics_rx_ht_phy *ht =
944 			&mvm->rx_stats.ofdm_ht;
945 
946 		PRINT_STATS_LE32(ht, mh_format_err);
947 		PRINT_STATS_LE32(ht, agg_mpdu_cnt);
948 		PRINT_STATS_LE32(ht, agg_cnt);
949 		PRINT_STATS_LE32(ht, unsupport_mcs);
950 	}
951 
952 	mutex_unlock(&mvm->mutex);
953 
954 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
955 	kfree(buf);
956 
957 	return ret;
958 }
959 #undef PRINT_STAT_LE32
960 
iwl_dbgfs_fw_system_stats_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)961 static ssize_t iwl_dbgfs_fw_system_stats_read(struct file *file,
962 					      char __user *user_buf,
963 					      size_t count, loff_t *ppos)
964 {
965 	char *buff, *pos, *endpos;
966 	int ret;
967 	size_t bufsz;
968 	int i;
969 	struct iwl_mvm_vif *mvmvif;
970 	struct ieee80211_vif *vif;
971 	struct iwl_mvm *mvm = file->private_data;
972 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
973 					   WIDE_ID(SYSTEM_GROUP,
974 						   SYSTEM_STATISTICS_CMD),
975 					   IWL_FW_CMD_VER_UNKNOWN);
976 
977 	/* in case of a wrong cmd version, allocate buffer only for error msg */
978 	bufsz = (cmd_ver == 1) ? 4096 : 64;
979 
980 	buff = kzalloc(bufsz, GFP_KERNEL);
981 	if (!buff)
982 		return -ENOMEM;
983 
984 	pos = buff;
985 	endpos = pos + bufsz;
986 
987 	if (cmd_ver != 1) {
988 		pos += scnprintf(pos, endpos - pos,
989 				 "System stats not supported:%d\n", cmd_ver);
990 		goto send_out;
991 	}
992 
993 	mutex_lock(&mvm->mutex);
994 	if (iwl_mvm_firmware_running(mvm))
995 		iwl_mvm_request_statistics(mvm, false);
996 
997 	for (i = 0; i < NUM_MAC_INDEX_DRIVER; i++) {
998 		vif = iwl_mvm_rcu_dereference_vif_id(mvm, i, false);
999 		if (!vif)
1000 			continue;
1001 
1002 		if (vif->type == NL80211_IFTYPE_STATION)
1003 			break;
1004 	}
1005 
1006 	if (i == NUM_MAC_INDEX_DRIVER || !vif) {
1007 		pos += scnprintf(pos, endpos - pos, "vif is NULL\n");
1008 		goto release_send_out;
1009 	}
1010 
1011 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
1012 	if (!mvmvif) {
1013 		pos += scnprintf(pos, endpos - pos, "mvmvif is NULL\n");
1014 		goto release_send_out;
1015 	}
1016 
1017 	for_each_mvm_vif_valid_link(mvmvif, i) {
1018 		struct iwl_mvm_vif_link_info *link_info = mvmvif->link[i];
1019 
1020 		pos += scnprintf(pos, endpos - pos,
1021 				 "link_id %d", i);
1022 		pos += scnprintf(pos, endpos - pos,
1023 				 " num_beacons %d",
1024 				 link_info->beacon_stats.num_beacons);
1025 		pos += scnprintf(pos, endpos - pos,
1026 				 " accu_num_beacons %d",
1027 				 link_info->beacon_stats.accu_num_beacons);
1028 		pos += scnprintf(pos, endpos - pos,
1029 				 " avg_signal %d\n",
1030 				 link_info->beacon_stats.avg_signal);
1031 	}
1032 
1033 	pos += scnprintf(pos, endpos - pos,
1034 			 "radio_stats.rx_time %lld\n",
1035 			 mvm->radio_stats.rx_time);
1036 	pos += scnprintf(pos, endpos - pos,
1037 			 "radio_stats.tx_time %lld\n",
1038 			 mvm->radio_stats.tx_time);
1039 	pos += scnprintf(pos, endpos - pos,
1040 			 "accu_radio_stats.rx_time %lld\n",
1041 			 mvm->accu_radio_stats.rx_time);
1042 	pos += scnprintf(pos, endpos - pos,
1043 			 "accu_radio_stats.tx_time %lld\n",
1044 			 mvm->accu_radio_stats.tx_time);
1045 
1046 release_send_out:
1047 	mutex_unlock(&mvm->mutex);
1048 
1049 send_out:
1050 	ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
1051 	kfree(buff);
1052 
1053 	return ret;
1054 }
1055 
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)1056 static ssize_t iwl_dbgfs_frame_stats_read(struct iwl_mvm *mvm,
1057 					  char __user *user_buf, size_t count,
1058 					  loff_t *ppos,
1059 					  struct iwl_mvm_frame_stats *stats)
1060 {
1061 	char *buff, *pos, *endpos;
1062 	int idx, i;
1063 	int ret;
1064 	static const size_t bufsz = 1024;
1065 
1066 	buff = kmalloc(bufsz, GFP_KERNEL);
1067 	if (!buff)
1068 		return -ENOMEM;
1069 
1070 	spin_lock_bh(&mvm->drv_stats_lock);
1071 
1072 	pos = buff;
1073 	endpos = pos + bufsz;
1074 
1075 	pos += scnprintf(pos, endpos - pos,
1076 			 "Legacy/HT/VHT\t:\t%d/%d/%d\n",
1077 			 stats->legacy_frames,
1078 			 stats->ht_frames,
1079 			 stats->vht_frames);
1080 	pos += scnprintf(pos, endpos - pos, "20/40/80\t:\t%d/%d/%d\n",
1081 			 stats->bw_20_frames,
1082 			 stats->bw_40_frames,
1083 			 stats->bw_80_frames);
1084 	pos += scnprintf(pos, endpos - pos, "NGI/SGI\t\t:\t%d/%d\n",
1085 			 stats->ngi_frames,
1086 			 stats->sgi_frames);
1087 	pos += scnprintf(pos, endpos - pos, "SISO/MIMO2\t:\t%d/%d\n",
1088 			 stats->siso_frames,
1089 			 stats->mimo2_frames);
1090 	pos += scnprintf(pos, endpos - pos, "FAIL/SCSS\t:\t%d/%d\n",
1091 			 stats->fail_frames,
1092 			 stats->success_frames);
1093 	pos += scnprintf(pos, endpos - pos, "MPDUs agg\t:\t%d\n",
1094 			 stats->agg_frames);
1095 	pos += scnprintf(pos, endpos - pos, "A-MPDUs\t\t:\t%d\n",
1096 			 stats->ampdu_count);
1097 	pos += scnprintf(pos, endpos - pos, "Avg MPDUs/A-MPDU:\t%d\n",
1098 			 stats->ampdu_count > 0 ?
1099 			 (stats->agg_frames / stats->ampdu_count) : 0);
1100 
1101 	pos += scnprintf(pos, endpos - pos, "Last Rates\n");
1102 
1103 	idx = stats->last_frame_idx - 1;
1104 	for (i = 0; i < ARRAY_SIZE(stats->last_rates); i++) {
1105 		idx = (idx + 1) % ARRAY_SIZE(stats->last_rates);
1106 		if (stats->last_rates[idx] == 0)
1107 			continue;
1108 		pos += scnprintf(pos, endpos - pos, "Rate[%d]: ",
1109 				 (int)(ARRAY_SIZE(stats->last_rates) - i));
1110 		pos += rs_pretty_print_rate_v1(pos, endpos - pos,
1111 					       stats->last_rates[idx]);
1112 		if (pos < endpos - 1)
1113 			*pos++ = '\n';
1114 	}
1115 	spin_unlock_bh(&mvm->drv_stats_lock);
1116 
1117 	ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
1118 	kfree(buff);
1119 
1120 	return ret;
1121 }
1122 
iwl_dbgfs_drv_rx_stats_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)1123 static ssize_t iwl_dbgfs_drv_rx_stats_read(struct file *file,
1124 					   char __user *user_buf, size_t count,
1125 					   loff_t *ppos)
1126 {
1127 	struct iwl_mvm *mvm = file->private_data;
1128 
1129 	return iwl_dbgfs_frame_stats_read(mvm, user_buf, count, ppos,
1130 					  &mvm->drv_rx_stats);
1131 }
1132 
iwl_dbgfs_fw_restart_write(struct iwl_mvm * mvm,char * buf,size_t count,loff_t * ppos)1133 static ssize_t iwl_dbgfs_fw_restart_write(struct iwl_mvm *mvm, char *buf,
1134 					  size_t count, loff_t *ppos)
1135 {
1136 	int __maybe_unused ret;
1137 
1138 	if (!iwl_mvm_firmware_running(mvm))
1139 		return -EIO;
1140 
1141 	mutex_lock(&mvm->mutex);
1142 
1143 	if (count == 6 && !strcmp(buf, "nolog\n")) {
1144 		set_bit(IWL_MVM_STATUS_SUPPRESS_ERROR_LOG_ONCE, &mvm->status);
1145 		set_bit(STATUS_SUPPRESS_CMD_ERROR_ONCE, &mvm->trans->status);
1146 	}
1147 
1148 	/* take the return value to make compiler happy - it will fail anyway */
1149 	ret = iwl_mvm_send_cmd_pdu(mvm,
1150 				   WIDE_ID(LONG_GROUP, REPLY_ERROR),
1151 				   0, 0, NULL);
1152 
1153 	mutex_unlock(&mvm->mutex);
1154 
1155 	return count;
1156 }
1157 
iwl_dbgfs_fw_nmi_write(struct iwl_mvm * mvm,char * buf,size_t count,loff_t * ppos)1158 static ssize_t iwl_dbgfs_fw_nmi_write(struct iwl_mvm *mvm, char *buf,
1159 				      size_t count, loff_t *ppos)
1160 {
1161 	if (!iwl_mvm_firmware_running(mvm))
1162 		return -EIO;
1163 
1164 	IWL_ERR(mvm, "Triggering an NMI from debugfs\n");
1165 
1166 	if (count == 6 && !strcmp(buf, "nolog\n"))
1167 		set_bit(IWL_MVM_STATUS_SUPPRESS_ERROR_LOG_ONCE, &mvm->status);
1168 
1169 	iwl_force_nmi(mvm->trans);
1170 
1171 	return count;
1172 }
1173 
1174 static ssize_t
iwl_dbgfs_scan_ant_rxchain_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)1175 iwl_dbgfs_scan_ant_rxchain_read(struct file *file,
1176 				char __user *user_buf,
1177 				size_t count, loff_t *ppos)
1178 {
1179 	struct iwl_mvm *mvm = file->private_data;
1180 	int pos = 0;
1181 	char buf[32];
1182 	const size_t bufsz = sizeof(buf);
1183 
1184 	/* print which antennas were set for the scan command by the user */
1185 	pos += scnprintf(buf + pos, bufsz - pos, "Antennas for scan: ");
1186 	if (mvm->scan_rx_ant & ANT_A)
1187 		pos += scnprintf(buf + pos, bufsz - pos, "A");
1188 	if (mvm->scan_rx_ant & ANT_B)
1189 		pos += scnprintf(buf + pos, bufsz - pos, "B");
1190 	pos += scnprintf(buf + pos, bufsz - pos, " (%x)\n", mvm->scan_rx_ant);
1191 
1192 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1193 }
1194 
1195 static ssize_t
iwl_dbgfs_scan_ant_rxchain_write(struct iwl_mvm * mvm,char * buf,size_t count,loff_t * ppos)1196 iwl_dbgfs_scan_ant_rxchain_write(struct iwl_mvm *mvm, char *buf,
1197 				 size_t count, loff_t *ppos)
1198 {
1199 	u8 scan_rx_ant;
1200 
1201 	if (!iwl_mvm_firmware_running(mvm))
1202 		return -EIO;
1203 
1204 	if (sscanf(buf, "%hhx", &scan_rx_ant) != 1)
1205 		return -EINVAL;
1206 	if (scan_rx_ant > ANT_ABC)
1207 		return -EINVAL;
1208 	if (scan_rx_ant & ~(iwl_mvm_get_valid_rx_ant(mvm)))
1209 		return -EINVAL;
1210 
1211 	if (mvm->scan_rx_ant != scan_rx_ant) {
1212 		mvm->scan_rx_ant = scan_rx_ant;
1213 		if (fw_has_capa(&mvm->fw->ucode_capa,
1214 				IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1215 			iwl_mvm_config_scan(mvm);
1216 	}
1217 
1218 	return count;
1219 }
1220 
iwl_dbgfs_indirection_tbl_write(struct iwl_mvm * mvm,char * buf,size_t count,loff_t * ppos)1221 static ssize_t iwl_dbgfs_indirection_tbl_write(struct iwl_mvm *mvm,
1222 					       char *buf, size_t count,
1223 					       loff_t *ppos)
1224 {
1225 	struct iwl_rss_config_cmd cmd = {
1226 		.flags = cpu_to_le32(IWL_RSS_ENABLE),
1227 		.hash_mask = IWL_RSS_HASH_TYPE_IPV4_TCP |
1228 			     IWL_RSS_HASH_TYPE_IPV4_UDP |
1229 			     IWL_RSS_HASH_TYPE_IPV4_PAYLOAD |
1230 			     IWL_RSS_HASH_TYPE_IPV6_TCP |
1231 			     IWL_RSS_HASH_TYPE_IPV6_UDP |
1232 			     IWL_RSS_HASH_TYPE_IPV6_PAYLOAD,
1233 	};
1234 	int ret, i, num_repeats, nbytes = count / 2;
1235 
1236 	ret = hex2bin(cmd.indirection_table, buf, nbytes);
1237 	if (ret)
1238 		return ret;
1239 
1240 	/*
1241 	 * The input is the redirection table, partial or full.
1242 	 * Repeat the pattern if needed.
1243 	 * For example, input of 01020F will be repeated 42 times,
1244 	 * indirecting RSS hash results to queues 1, 2, 15 (skipping
1245 	 * queues 3 - 14).
1246 	 */
1247 	num_repeats = ARRAY_SIZE(cmd.indirection_table) / nbytes;
1248 	for (i = 1; i < num_repeats; i++)
1249 		memcpy(&cmd.indirection_table[i * nbytes],
1250 		       cmd.indirection_table, nbytes);
1251 	/* handle cut in the middle pattern for the last places */
1252 	memcpy(&cmd.indirection_table[i * nbytes], cmd.indirection_table,
1253 	       ARRAY_SIZE(cmd.indirection_table) % nbytes);
1254 
1255 	netdev_rss_key_fill(cmd.secret_key, sizeof(cmd.secret_key));
1256 
1257 	mutex_lock(&mvm->mutex);
1258 	if (iwl_mvm_firmware_running(mvm))
1259 		ret = iwl_mvm_send_cmd_pdu(mvm, RSS_CONFIG_CMD, 0,
1260 					   sizeof(cmd), &cmd);
1261 	else
1262 		ret = 0;
1263 	mutex_unlock(&mvm->mutex);
1264 
1265 	return ret ?: count;
1266 }
1267 
iwl_dbgfs_inject_packet_write(struct iwl_mvm * mvm,char * buf,size_t count,loff_t * ppos)1268 static ssize_t iwl_dbgfs_inject_packet_write(struct iwl_mvm *mvm,
1269 					     char *buf, size_t count,
1270 					     loff_t *ppos)
1271 {
1272 	struct iwl_op_mode *opmode = container_of((void *)mvm,
1273 						  struct iwl_op_mode,
1274 						  op_mode_specific);
1275 	struct iwl_rx_cmd_buffer rxb = {
1276 		._rx_page_order = 0,
1277 		.truesize = 0, /* not used */
1278 		._offset = 0,
1279 	};
1280 	struct iwl_rx_packet *pkt;
1281 	int bin_len = count / 2;
1282 	int ret = -EINVAL;
1283 
1284 	if (!iwl_mvm_firmware_running(mvm))
1285 		return -EIO;
1286 
1287 	/* supporting only MQ RX */
1288 	if (!mvm->trans->trans_cfg->mq_rx_supported)
1289 		return -EOPNOTSUPP;
1290 
1291 	rxb._page = alloc_pages(GFP_ATOMIC, 0);
1292 	if (!rxb._page)
1293 		return -ENOMEM;
1294 	pkt = rxb_addr(&rxb);
1295 
1296 	ret = hex2bin(page_address(rxb._page), buf, bin_len);
1297 	if (ret)
1298 		goto out;
1299 
1300 	/* avoid invalid memory access and malformed packet */
1301 	if (bin_len < sizeof(*pkt) ||
1302 	    bin_len != sizeof(*pkt) + iwl_rx_packet_payload_len(pkt))
1303 		goto out;
1304 
1305 	local_bh_disable();
1306 	iwl_mvm_rx_mq(opmode, NULL, &rxb);
1307 	local_bh_enable();
1308 	ret = 0;
1309 
1310 out:
1311 	iwl_free_rxb(&rxb);
1312 
1313 	return ret ?: count;
1314 }
1315 
_iwl_dbgfs_inject_beacon_ie(struct iwl_mvm * mvm,char * bin,int len)1316 static int _iwl_dbgfs_inject_beacon_ie(struct iwl_mvm *mvm, char *bin, int len)
1317 {
1318 	struct ieee80211_vif *vif;
1319 	struct iwl_mvm_vif *mvmvif;
1320 	struct sk_buff *beacon;
1321 	struct ieee80211_tx_info *info;
1322 	struct iwl_mac_beacon_cmd beacon_cmd = {};
1323 	unsigned int link_id;
1324 	u8 rate;
1325 	int i;
1326 
1327 	len /= 2;
1328 
1329 	/* Element len should be represented by u8 */
1330 	if (len >= U8_MAX)
1331 		return -EINVAL;
1332 
1333 	if (!iwl_mvm_firmware_running(mvm))
1334 		return -EIO;
1335 
1336 	if (!iwl_mvm_has_new_tx_api(mvm) &&
1337 	    !fw_has_api(&mvm->fw->ucode_capa,
1338 			IWL_UCODE_TLV_API_NEW_BEACON_TEMPLATE))
1339 		return -EINVAL;
1340 
1341 	mutex_lock(&mvm->mutex);
1342 
1343 	for (i = 0; i < NUM_MAC_INDEX_DRIVER; i++) {
1344 		vif = iwl_mvm_rcu_dereference_vif_id(mvm, i, false);
1345 		if (!vif)
1346 			continue;
1347 
1348 		if (vif->type == NL80211_IFTYPE_AP)
1349 			break;
1350 	}
1351 
1352 	if (i == NUM_MAC_INDEX_DRIVER || !vif)
1353 		goto out_err;
1354 
1355 	mvm->hw->extra_beacon_tailroom = len;
1356 
1357 	beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL, 0);
1358 	if (!beacon)
1359 		goto out_err;
1360 
1361 	if (len && hex2bin(skb_put_zero(beacon, len), bin, len)) {
1362 		dev_kfree_skb(beacon);
1363 		goto out_err;
1364 	}
1365 
1366 	mvm->beacon_inject_active = true;
1367 
1368 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
1369 	info = IEEE80211_SKB_CB(beacon);
1370 	rate = iwl_mvm_mac_ctxt_get_beacon_rate(mvm, info, vif);
1371 
1372 	for_each_mvm_vif_valid_link(mvmvif, link_id) {
1373 		beacon_cmd.flags =
1374 			cpu_to_le16(iwl_mvm_mac_ctxt_get_beacon_flags(mvm->fw,
1375 								      rate));
1376 		beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);
1377 		if (iwl_fw_lookup_cmd_ver(mvm->fw, BEACON_TEMPLATE_CMD, 0) > 12)
1378 			beacon_cmd.link_id =
1379 				cpu_to_le32(mvmvif->link[link_id]->fw_link_id);
1380 		else
1381 			beacon_cmd.link_id = cpu_to_le32((u32)mvmvif->id);
1382 
1383 		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
1384 					 &beacon_cmd.tim_size,
1385 					 beacon->data, beacon->len);
1386 
1387 		if (iwl_fw_lookup_cmd_ver(mvm->fw,
1388 					  BEACON_TEMPLATE_CMD, 0) >= 14) {
1389 			u32 offset = iwl_find_ie_offset(beacon->data,
1390 							WLAN_EID_S1G_TWT,
1391 							beacon->len);
1392 
1393 			beacon_cmd.btwt_offset = cpu_to_le32(offset);
1394 		}
1395 
1396 		iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
1397 						 sizeof(beacon_cmd));
1398 	}
1399 	mutex_unlock(&mvm->mutex);
1400 
1401 	dev_kfree_skb(beacon);
1402 
1403 	return 0;
1404 
1405 out_err:
1406 	mutex_unlock(&mvm->mutex);
1407 	return -EINVAL;
1408 }
1409 
iwl_dbgfs_inject_beacon_ie_write(struct iwl_mvm * mvm,char * buf,size_t count,loff_t * ppos)1410 static ssize_t iwl_dbgfs_inject_beacon_ie_write(struct iwl_mvm *mvm,
1411 						char *buf, size_t count,
1412 						loff_t *ppos)
1413 {
1414 	int ret = _iwl_dbgfs_inject_beacon_ie(mvm, buf, count);
1415 
1416 	mvm->hw->extra_beacon_tailroom = 0;
1417 	return ret ?: count;
1418 }
1419 
iwl_dbgfs_inject_beacon_ie_restore_write(struct iwl_mvm * mvm,char * buf,size_t count,loff_t * ppos)1420 static ssize_t iwl_dbgfs_inject_beacon_ie_restore_write(struct iwl_mvm *mvm,
1421 							char *buf,
1422 							size_t count,
1423 							loff_t *ppos)
1424 {
1425 	int ret = _iwl_dbgfs_inject_beacon_ie(mvm, NULL, 0);
1426 
1427 	mvm->hw->extra_beacon_tailroom = 0;
1428 	mvm->beacon_inject_active = false;
1429 	return ret ?: count;
1430 }
1431 
iwl_dbgfs_fw_dbg_conf_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)1432 static ssize_t iwl_dbgfs_fw_dbg_conf_read(struct file *file,
1433 					  char __user *user_buf,
1434 					  size_t count, loff_t *ppos)
1435 {
1436 	struct iwl_mvm *mvm = file->private_data;
1437 	int conf;
1438 	char buf[8];
1439 	const size_t bufsz = sizeof(buf);
1440 	int pos = 0;
1441 
1442 	mutex_lock(&mvm->mutex);
1443 	conf = mvm->fwrt.dump.conf;
1444 	mutex_unlock(&mvm->mutex);
1445 
1446 	pos += scnprintf(buf + pos, bufsz - pos, "%d\n", conf);
1447 
1448 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1449 }
1450 
iwl_dbgfs_fw_dbg_conf_write(struct iwl_mvm * mvm,char * buf,size_t count,loff_t * ppos)1451 static ssize_t iwl_dbgfs_fw_dbg_conf_write(struct iwl_mvm *mvm,
1452 					   char *buf, size_t count,
1453 					   loff_t *ppos)
1454 {
1455 	unsigned int conf_id;
1456 	int ret;
1457 
1458 	if (!iwl_mvm_firmware_running(mvm))
1459 		return -EIO;
1460 
1461 	ret = kstrtouint(buf, 0, &conf_id);
1462 	if (ret)
1463 		return ret;
1464 
1465 	if (WARN_ON(conf_id >= FW_DBG_CONF_MAX))
1466 		return -EINVAL;
1467 
1468 	mutex_lock(&mvm->mutex);
1469 	ret = iwl_fw_start_dbg_conf(&mvm->fwrt, conf_id);
1470 	mutex_unlock(&mvm->mutex);
1471 
1472 	return ret ?: count;
1473 }
1474 
iwl_dbgfs_fw_dbg_clear_write(struct iwl_mvm * mvm,char * buf,size_t count,loff_t * ppos)1475 static ssize_t iwl_dbgfs_fw_dbg_clear_write(struct iwl_mvm *mvm,
1476 					    char *buf, size_t count,
1477 					    loff_t *ppos)
1478 {
1479 	if (mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_9000)
1480 		return -EOPNOTSUPP;
1481 
1482 	/*
1483 	 * If the firmware is not running, silently succeed since there is
1484 	 * no data to clear.
1485 	 */
1486 	if (!iwl_mvm_firmware_running(mvm))
1487 		return count;
1488 
1489 	mutex_lock(&mvm->mutex);
1490 	iwl_fw_dbg_clear_monitor_buf(&mvm->fwrt);
1491 	mutex_unlock(&mvm->mutex);
1492 
1493 	return count;
1494 }
1495 
iwl_dbgfs_dbg_time_point_write(struct iwl_mvm * mvm,char * buf,size_t count,loff_t * ppos)1496 static ssize_t iwl_dbgfs_dbg_time_point_write(struct iwl_mvm *mvm,
1497 					      char *buf, size_t count,
1498 					      loff_t *ppos)
1499 {
1500 	u32 timepoint;
1501 
1502 	if (kstrtou32(buf, 0, &timepoint))
1503 		return -EINVAL;
1504 
1505 	if (timepoint == IWL_FW_INI_TIME_POINT_INVALID ||
1506 	    timepoint >= IWL_FW_INI_TIME_POINT_NUM)
1507 		return -EINVAL;
1508 
1509 	iwl_dbg_tlv_time_point(&mvm->fwrt, timepoint, NULL);
1510 
1511 	return count;
1512 }
1513 
1514 #define MVM_DEBUGFS_WRITE_FILE_OPS(name, bufsz) \
1515 	_MVM_DEBUGFS_WRITE_FILE_OPS(name, bufsz, struct iwl_mvm)
1516 #define MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz) \
1517 	_MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz, struct iwl_mvm)
1518 #define MVM_DEBUGFS_ADD_FILE_ALIAS(alias, name, parent, mode) do {	\
1519 		debugfs_create_file(alias, mode, parent, mvm,		\
1520 				    &iwl_dbgfs_##name##_ops);		\
1521 	} while (0)
1522 #define MVM_DEBUGFS_ADD_FILE(name, parent, mode) \
1523 	MVM_DEBUGFS_ADD_FILE_ALIAS(#name, name, parent, mode)
1524 
1525 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)1526 _iwl_dbgfs_link_sta_wrap_write(ssize_t (*real)(struct ieee80211_link_sta *,
1527 					       struct iwl_mvm_sta *,
1528 					       struct iwl_mvm *,
1529 					       struct iwl_mvm_link_sta *,
1530 					       char *,
1531 					       size_t, loff_t *),
1532 			   struct file *file,
1533 			   char *buf, size_t buf_size, loff_t *ppos)
1534 {
1535 	struct ieee80211_link_sta *link_sta = file->private_data;
1536 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(link_sta->sta);
1537 	struct iwl_mvm *mvm = iwl_mvm_vif_from_mac80211(mvmsta->vif)->mvm;
1538 	struct iwl_mvm_link_sta *mvm_link_sta;
1539 	ssize_t ret;
1540 
1541 	mutex_lock(&mvm->mutex);
1542 
1543 	mvm_link_sta = rcu_dereference_protected(mvmsta->link[link_sta->link_id],
1544 						 lockdep_is_held(&mvm->mutex));
1545 	if (WARN_ON(!mvm_link_sta)) {
1546 		mutex_unlock(&mvm->mutex);
1547 		return -ENODEV;
1548 	}
1549 
1550 	ret = real(link_sta, mvmsta, mvm, mvm_link_sta, buf, buf_size, ppos);
1551 
1552 	mutex_unlock(&mvm->mutex);
1553 
1554 	return ret;
1555 }
1556 
1557 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)1558 _iwl_dbgfs_link_sta_wrap_read(ssize_t (*real)(struct ieee80211_link_sta *,
1559 					      struct iwl_mvm_sta *,
1560 					      struct iwl_mvm *,
1561 					      struct iwl_mvm_link_sta *,
1562 					      char __user *,
1563 					      size_t, loff_t *),
1564 			   struct file *file,
1565 			   char __user *user_buf, size_t count, loff_t *ppos)
1566 {
1567 	struct ieee80211_link_sta *link_sta = file->private_data;
1568 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(link_sta->sta);
1569 	struct iwl_mvm *mvm = iwl_mvm_vif_from_mac80211(mvmsta->vif)->mvm;
1570 	struct iwl_mvm_link_sta *mvm_link_sta;
1571 	ssize_t ret;
1572 
1573 	mutex_lock(&mvm->mutex);
1574 
1575 	mvm_link_sta = rcu_dereference_protected(mvmsta->link[link_sta->link_id],
1576 						 lockdep_is_held(&mvm->mutex));
1577 	if (WARN_ON(!mvm_link_sta)) {
1578 		mutex_unlock(&mvm->mutex);
1579 		return -ENODEV;
1580 	}
1581 
1582 	ret = real(link_sta, mvmsta, mvm, mvm_link_sta, user_buf, count, ppos);
1583 
1584 	mutex_unlock(&mvm->mutex);
1585 
1586 	return ret;
1587 }
1588 
1589 #define MVM_DEBUGFS_LINK_STA_WRITE_WRAPPER(name, buflen)		\
1590 static ssize_t _iwl_dbgfs_link_sta_##name##_write(struct file *file,	\
1591 					 const char __user *user_buf,	\
1592 					 size_t count, loff_t *ppos)	\
1593 {									\
1594 	char buf[buflen] = {};						\
1595 	size_t buf_size = min(count, sizeof(buf) -  1);			\
1596 									\
1597 	if (copy_from_user(buf, user_buf, buf_size))			\
1598 		return -EFAULT;						\
1599 									\
1600 	return _iwl_dbgfs_link_sta_wrap_write(iwl_dbgfs_##name##_write,	\
1601 					      file,			\
1602 					      buf, buf_size, ppos);	\
1603 }									\
1604 
1605 #define MVM_DEBUGFS_LINK_STA_READ_WRAPPER(name)		\
1606 static ssize_t _iwl_dbgfs_link_sta_##name##_read(struct file *file,	\
1607 					 char __user *user_buf,		\
1608 					 size_t count, loff_t *ppos)	\
1609 {									\
1610 	return _iwl_dbgfs_link_sta_wrap_read(iwl_dbgfs_##name##_read,	\
1611 					     file,			\
1612 					     user_buf, count, ppos);	\
1613 }									\
1614 
1615 #define MVM_DEBUGFS_WRITE_LINK_STA_FILE_OPS(name, bufsz)		\
1616 MVM_DEBUGFS_LINK_STA_WRITE_WRAPPER(name, bufsz)				\
1617 static const struct file_operations iwl_dbgfs_link_sta_##name##_ops = {	\
1618 	.write = _iwl_dbgfs_link_sta_##name##_write,			\
1619 	.open = simple_open,						\
1620 	.llseek = generic_file_llseek,					\
1621 }
1622 
1623 #define MVM_DEBUGFS_READ_LINK_STA_FILE_OPS(name)			\
1624 MVM_DEBUGFS_LINK_STA_READ_WRAPPER(name)					\
1625 static const struct file_operations iwl_dbgfs_link_sta_##name##_ops = {	\
1626 	.read = _iwl_dbgfs_link_sta_##name##_read,			\
1627 	.open = simple_open,						\
1628 	.llseek = generic_file_llseek,					\
1629 }
1630 
1631 #define MVM_DEBUGFS_READ_WRITE_LINK_STA_FILE_OPS(name, bufsz)		\
1632 MVM_DEBUGFS_LINK_STA_READ_WRAPPER(name)					\
1633 MVM_DEBUGFS_LINK_STA_WRITE_WRAPPER(name, bufsz)				\
1634 static const struct file_operations iwl_dbgfs_link_sta_##name##_ops = {	\
1635 	.read = _iwl_dbgfs_link_sta_##name##_read,			\
1636 	.write = _iwl_dbgfs_link_sta_##name##_write,			\
1637 	.open = simple_open,						\
1638 	.llseek = generic_file_llseek,					\
1639 }
1640 
1641 #define MVM_DEBUGFS_ADD_LINK_STA_FILE_ALIAS(alias, name, parent, mode)	\
1642 		debugfs_create_file(alias, mode, parent, link_sta,	\
1643 				    &iwl_dbgfs_link_sta_##name##_ops)
1644 #define MVM_DEBUGFS_ADD_LINK_STA_FILE(name, parent, mode) \
1645 	MVM_DEBUGFS_ADD_LINK_STA_FILE_ALIAS(#name, name, parent, mode)
1646 
1647 static ssize_t
iwl_dbgfs_prph_reg_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)1648 iwl_dbgfs_prph_reg_read(struct file *file,
1649 			char __user *user_buf,
1650 			size_t count, loff_t *ppos)
1651 {
1652 	struct iwl_mvm *mvm = file->private_data;
1653 	int pos = 0;
1654 	char buf[32];
1655 	const size_t bufsz = sizeof(buf);
1656 
1657 	if (!mvm->dbgfs_prph_reg_addr)
1658 		return -EINVAL;
1659 
1660 	pos += scnprintf(buf + pos, bufsz - pos, "Reg 0x%x: (0x%x)\n",
1661 		mvm->dbgfs_prph_reg_addr,
1662 		iwl_read_prph(mvm->trans, mvm->dbgfs_prph_reg_addr));
1663 
1664 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1665 }
1666 
1667 static ssize_t
iwl_dbgfs_prph_reg_write(struct iwl_mvm * mvm,char * buf,size_t count,loff_t * ppos)1668 iwl_dbgfs_prph_reg_write(struct iwl_mvm *mvm, char *buf,
1669 			 size_t count, loff_t *ppos)
1670 {
1671 	u8 args;
1672 	u32 value;
1673 
1674 	args = sscanf(buf, "%i %i", &mvm->dbgfs_prph_reg_addr, &value);
1675 	/* if we only want to set the reg address - nothing more to do */
1676 	if (args == 1)
1677 		goto out;
1678 
1679 	/* otherwise, make sure we have both address and value */
1680 	if (args != 2)
1681 		return -EINVAL;
1682 
1683 	iwl_write_prph(mvm->trans, mvm->dbgfs_prph_reg_addr, value);
1684 
1685 out:
1686 	return count;
1687 }
1688 
1689 static ssize_t
iwl_dbgfs_send_echo_cmd_write(struct iwl_mvm * mvm,char * buf,size_t count,loff_t * ppos)1690 iwl_dbgfs_send_echo_cmd_write(struct iwl_mvm *mvm, char *buf,
1691 			      size_t count, loff_t *ppos)
1692 {
1693 	int ret;
1694 
1695 	if (!iwl_mvm_firmware_running(mvm))
1696 		return -EIO;
1697 
1698 	mutex_lock(&mvm->mutex);
1699 	ret = iwl_mvm_send_cmd_pdu(mvm, ECHO_CMD, 0, 0, NULL);
1700 	mutex_unlock(&mvm->mutex);
1701 
1702 	return ret ?: count;
1703 }
1704 
1705 struct iwl_mvm_sniffer_apply {
1706 	struct iwl_mvm *mvm;
1707 	u8 *bssid;
1708 	u16 aid;
1709 };
1710 
iwl_mvm_sniffer_apply(struct iwl_notif_wait_data * notif_data,struct iwl_rx_packet * pkt,void * data)1711 static bool iwl_mvm_sniffer_apply(struct iwl_notif_wait_data *notif_data,
1712 				  struct iwl_rx_packet *pkt, void *data)
1713 {
1714 	struct iwl_mvm_sniffer_apply *apply = data;
1715 
1716 	apply->mvm->cur_aid = cpu_to_le16(apply->aid);
1717 	memcpy(apply->mvm->cur_bssid, apply->bssid,
1718 	       sizeof(apply->mvm->cur_bssid));
1719 
1720 	return true;
1721 }
1722 
1723 static ssize_t
iwl_dbgfs_he_sniffer_params_write(struct iwl_mvm * mvm,char * buf,size_t count,loff_t * ppos)1724 iwl_dbgfs_he_sniffer_params_write(struct iwl_mvm *mvm, char *buf,
1725 				  size_t count, loff_t *ppos)
1726 {
1727 	struct iwl_notification_wait wait;
1728 	struct iwl_he_monitor_cmd he_mon_cmd = {};
1729 	struct iwl_mvm_sniffer_apply apply = {
1730 		.mvm = mvm,
1731 	};
1732 	u16 wait_cmds[] = {
1733 		WIDE_ID(DATA_PATH_GROUP, HE_AIR_SNIFFER_CONFIG_CMD),
1734 	};
1735 	u32 aid;
1736 	int ret;
1737 
1738 	if (!iwl_mvm_firmware_running(mvm))
1739 		return -EIO;
1740 
1741 	ret = sscanf(buf, "%x %2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx", &aid,
1742 		     &he_mon_cmd.bssid[0], &he_mon_cmd.bssid[1],
1743 		     &he_mon_cmd.bssid[2], &he_mon_cmd.bssid[3],
1744 		     &he_mon_cmd.bssid[4], &he_mon_cmd.bssid[5]);
1745 	if (ret != 7)
1746 		return -EINVAL;
1747 
1748 	he_mon_cmd.aid = cpu_to_le16(aid);
1749 
1750 	apply.aid = aid;
1751 	apply.bssid = (void *)he_mon_cmd.bssid;
1752 
1753 	mutex_lock(&mvm->mutex);
1754 
1755 	/*
1756 	 * Use the notification waiter to get our function triggered
1757 	 * in sequence with other RX. This ensures that frames we get
1758 	 * on the RX queue _before_ the new configuration is applied
1759 	 * still have mvm->cur_aid pointing to the old AID, and that
1760 	 * frames on the RX queue _after_ the firmware processed the
1761 	 * new configuration (and sent the response, synchronously)
1762 	 * get mvm->cur_aid correctly set to the new AID.
1763 	 */
1764 	iwl_init_notification_wait(&mvm->notif_wait, &wait,
1765 				   wait_cmds, ARRAY_SIZE(wait_cmds),
1766 				   iwl_mvm_sniffer_apply, &apply);
1767 
1768 	ret = iwl_mvm_send_cmd_pdu(mvm,
1769 				   WIDE_ID(DATA_PATH_GROUP, HE_AIR_SNIFFER_CONFIG_CMD),
1770 				   0,
1771 				   sizeof(he_mon_cmd), &he_mon_cmd);
1772 
1773 	/* no need to really wait, we already did anyway */
1774 	iwl_remove_notification(&mvm->notif_wait, &wait);
1775 
1776 	mutex_unlock(&mvm->mutex);
1777 
1778 	return ret ?: count;
1779 }
1780 
1781 static ssize_t
iwl_dbgfs_he_sniffer_params_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)1782 iwl_dbgfs_he_sniffer_params_read(struct file *file, char __user *user_buf,
1783 				 size_t count, loff_t *ppos)
1784 {
1785 	struct iwl_mvm *mvm = file->private_data;
1786 	u8 buf[32];
1787 	int len;
1788 
1789 	len = scnprintf(buf, sizeof(buf),
1790 			"%d %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",
1791 			le16_to_cpu(mvm->cur_aid), mvm->cur_bssid[0],
1792 			mvm->cur_bssid[1], mvm->cur_bssid[2], mvm->cur_bssid[3],
1793 			mvm->cur_bssid[4], mvm->cur_bssid[5]);
1794 
1795 	return simple_read_from_buffer(user_buf, count, ppos, buf, len);
1796 }
1797 
1798 static ssize_t
iwl_dbgfs_uapsd_noagg_bssids_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)1799 iwl_dbgfs_uapsd_noagg_bssids_read(struct file *file, char __user *user_buf,
1800 				  size_t count, loff_t *ppos)
1801 {
1802 	struct iwl_mvm *mvm = file->private_data;
1803 	u8 buf[IWL_MVM_UAPSD_NOAGG_BSSIDS_NUM * ETH_ALEN * 3 + 1];
1804 	unsigned int pos = 0;
1805 	size_t bufsz = sizeof(buf);
1806 	int i;
1807 
1808 	mutex_lock(&mvm->mutex);
1809 
1810 	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++)
1811 		pos += scnprintf(buf + pos, bufsz - pos, "%pM\n",
1812 				 mvm->uapsd_noagg_bssids[i].addr);
1813 
1814 	mutex_unlock(&mvm->mutex);
1815 
1816 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1817 }
1818 
1819 static ssize_t
iwl_dbgfs_ltr_config_write(struct iwl_mvm * mvm,char * buf,size_t count,loff_t * ppos)1820 iwl_dbgfs_ltr_config_write(struct iwl_mvm *mvm,
1821 			   char *buf, size_t count, loff_t *ppos)
1822 {
1823 	int ret;
1824 	struct iwl_ltr_config_cmd ltr_config = {0};
1825 
1826 	if (!iwl_mvm_firmware_running(mvm))
1827 		return -EIO;
1828 
1829 	if (sscanf(buf, "%x,%x,%x,%x,%x,%x,%x",
1830 		   &ltr_config.flags,
1831 		   &ltr_config.static_long,
1832 		   &ltr_config.static_short,
1833 		   &ltr_config.ltr_cfg_values[0],
1834 		   &ltr_config.ltr_cfg_values[1],
1835 		   &ltr_config.ltr_cfg_values[2],
1836 		   &ltr_config.ltr_cfg_values[3]) != 7) {
1837 		return -EINVAL;
1838 	}
1839 
1840 	mutex_lock(&mvm->mutex);
1841 	ret = iwl_mvm_send_cmd_pdu(mvm, LTR_CONFIG, 0, sizeof(ltr_config),
1842 				   &ltr_config);
1843 	mutex_unlock(&mvm->mutex);
1844 
1845 	if (ret)
1846 		IWL_ERR(mvm, "failed to send ltr configuration cmd\n");
1847 
1848 	return ret ?: count;
1849 }
1850 
iwl_dbgfs_rfi_freq_table_write(struct iwl_mvm * mvm,char * buf,size_t count,loff_t * ppos)1851 static ssize_t iwl_dbgfs_rfi_freq_table_write(struct iwl_mvm *mvm, char *buf,
1852 					      size_t count, loff_t *ppos)
1853 {
1854 	int ret = 0;
1855 	u16 op_id;
1856 
1857 	if (kstrtou16(buf, 10, &op_id))
1858 		return -EINVAL;
1859 
1860 	/* value zero triggers re-sending the default table to the device */
1861 	if (!op_id) {
1862 		mutex_lock(&mvm->mutex);
1863 		ret = iwl_rfi_send_config_cmd(mvm, NULL);
1864 		mutex_unlock(&mvm->mutex);
1865 	} else {
1866 		ret = -EOPNOTSUPP; /* in the future a new table will be added */
1867 	}
1868 
1869 	return ret ?: count;
1870 }
1871 
1872 /* The size computation is as follows:
1873  * each number needs at most 3 characters, number of rows is the size of
1874  * the table; So, need 5 chars for the "freq: " part and each tuple afterwards
1875  * needs 6 characters for numbers and 5 for the punctuation around.
1876  */
1877 #define IWL_RFI_BUF_SIZE (IWL_RFI_LUT_INSTALLED_SIZE *\
1878 				(5 + IWL_RFI_LUT_ENTRY_CHANNELS_NUM * (6 + 5)))
1879 
iwl_dbgfs_rfi_freq_table_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)1880 static ssize_t iwl_dbgfs_rfi_freq_table_read(struct file *file,
1881 					     char __user *user_buf,
1882 					     size_t count, loff_t *ppos)
1883 {
1884 	struct iwl_mvm *mvm = file->private_data;
1885 	struct iwl_rfi_freq_table_resp_cmd *resp;
1886 	u32 status;
1887 	char buf[IWL_RFI_BUF_SIZE];
1888 	int i, j, pos = 0;
1889 
1890 	resp = iwl_rfi_get_freq_table(mvm);
1891 	if (IS_ERR(resp))
1892 		return PTR_ERR(resp);
1893 
1894 	status = le32_to_cpu(resp->status);
1895 	if (status != RFI_FREQ_TABLE_OK) {
1896 		scnprintf(buf, IWL_RFI_BUF_SIZE, "status = %d\n", status);
1897 		goto out;
1898 	}
1899 
1900 	for (i = 0; i < ARRAY_SIZE(resp->table); i++) {
1901 		pos += scnprintf(buf + pos, IWL_RFI_BUF_SIZE - pos, "%d: ",
1902 				 resp->table[i].freq);
1903 
1904 		for (j = 0; j < ARRAY_SIZE(resp->table[i].channels); j++)
1905 			pos += scnprintf(buf + pos, IWL_RFI_BUF_SIZE - pos,
1906 					 "(%d, %d) ",
1907 					 resp->table[i].channels[j],
1908 					 resp->table[i].bands[j]);
1909 		pos += scnprintf(buf + pos, IWL_RFI_BUF_SIZE - pos, "\n");
1910 	}
1911 
1912 out:
1913 	kfree(resp);
1914 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1915 }
1916 
1917 MVM_DEBUGFS_READ_WRITE_FILE_OPS(prph_reg, 64);
1918 
1919 /* Device wide debugfs entries */
1920 MVM_DEBUGFS_READ_FILE_OPS(ctdp_budget);
1921 MVM_DEBUGFS_WRITE_FILE_OPS(stop_ctdp, 8);
1922 MVM_DEBUGFS_WRITE_FILE_OPS(start_ctdp, 8);
1923 MVM_DEBUGFS_WRITE_FILE_OPS(force_ctkill, 8);
1924 MVM_DEBUGFS_WRITE_FILE_OPS(tx_flush, 16);
1925 MVM_DEBUGFS_WRITE_FILE_OPS(send_echo_cmd, 8);
1926 MVM_DEBUGFS_READ_WRITE_FILE_OPS(sram, 64);
1927 MVM_DEBUGFS_READ_WRITE_FILE_OPS(set_nic_temperature, 64);
1928 MVM_DEBUGFS_READ_FILE_OPS(nic_temp);
1929 MVM_DEBUGFS_READ_FILE_OPS(stations);
1930 MVM_DEBUGFS_READ_LINK_STA_FILE_OPS(rs_data);
1931 MVM_DEBUGFS_READ_WRITE_FILE_OPS(disable_power_off, 64);
1932 MVM_DEBUGFS_READ_FILE_OPS(fw_rx_stats);
1933 MVM_DEBUGFS_READ_FILE_OPS(drv_rx_stats);
1934 MVM_DEBUGFS_READ_FILE_OPS(fw_system_stats);
1935 MVM_DEBUGFS_READ_FILE_OPS(phy_integration_ver);
1936 MVM_DEBUGFS_READ_FILE_OPS(tas_get_status);
1937 MVM_DEBUGFS_WRITE_FILE_OPS(fw_restart, 10);
1938 MVM_DEBUGFS_WRITE_FILE_OPS(fw_nmi, 10);
1939 MVM_DEBUGFS_READ_WRITE_FILE_OPS(scan_ant_rxchain, 8);
1940 MVM_DEBUGFS_READ_WRITE_FILE_OPS(fw_dbg_conf, 8);
1941 MVM_DEBUGFS_WRITE_FILE_OPS(fw_dbg_clear, 64);
1942 MVM_DEBUGFS_WRITE_FILE_OPS(dbg_time_point, 64);
1943 MVM_DEBUGFS_WRITE_FILE_OPS(indirection_tbl,
1944 			   (IWL_RSS_INDIRECTION_TABLE_SIZE * 2));
1945 MVM_DEBUGFS_WRITE_FILE_OPS(inject_packet, 512);
1946 MVM_DEBUGFS_WRITE_FILE_OPS(inject_beacon_ie, 512);
1947 MVM_DEBUGFS_WRITE_FILE_OPS(inject_beacon_ie_restore, 512);
1948 
1949 MVM_DEBUGFS_READ_FILE_OPS(uapsd_noagg_bssids);
1950 
1951 #ifdef CONFIG_ACPI
1952 MVM_DEBUGFS_READ_FILE_OPS(sar_geo_profile);
1953 MVM_DEBUGFS_READ_FILE_OPS(wifi_6e_enable);
1954 #endif
1955 
1956 MVM_DEBUGFS_READ_WRITE_LINK_STA_FILE_OPS(amsdu_len, 16);
1957 
1958 MVM_DEBUGFS_READ_WRITE_FILE_OPS(he_sniffer_params, 32);
1959 
1960 MVM_DEBUGFS_WRITE_FILE_OPS(ltr_config, 512);
1961 MVM_DEBUGFS_READ_WRITE_FILE_OPS(rfi_freq_table, 16);
1962 
iwl_dbgfs_mem_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)1963 static ssize_t iwl_dbgfs_mem_read(struct file *file, char __user *user_buf,
1964 				  size_t count, loff_t *ppos)
1965 {
1966 	struct iwl_mvm *mvm = file->private_data;
1967 	struct iwl_dbg_mem_access_cmd cmd = {};
1968 	struct iwl_dbg_mem_access_rsp *rsp;
1969 	struct iwl_host_cmd hcmd = {
1970 		.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
1971 		.data = { &cmd, },
1972 		.len = { sizeof(cmd) },
1973 	};
1974 	size_t delta;
1975 	ssize_t ret, len;
1976 
1977 	if (!iwl_mvm_firmware_running(mvm))
1978 		return -EIO;
1979 
1980 	hcmd.id = WIDE_ID(DEBUG_GROUP, *ppos >> 24 ? UMAC_RD_WR : LMAC_RD_WR);
1981 	cmd.op = cpu_to_le32(DEBUG_MEM_OP_READ);
1982 
1983 	/* Take care of alignment of both the position and the length */
1984 	delta = *ppos & 0x3;
1985 	cmd.addr = cpu_to_le32(*ppos - delta);
1986 	cmd.len = cpu_to_le32(min(ALIGN(count + delta, 4) / 4,
1987 				  (size_t)DEBUG_MEM_MAX_SIZE_DWORDS));
1988 
1989 	mutex_lock(&mvm->mutex);
1990 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1991 	mutex_unlock(&mvm->mutex);
1992 
1993 	if (ret < 0)
1994 		return ret;
1995 
1996 	if (iwl_rx_packet_payload_len(hcmd.resp_pkt) < sizeof(*rsp)) {
1997 		ret = -EIO;
1998 		goto out;
1999 	}
2000 
2001 	rsp = (void *)hcmd.resp_pkt->data;
2002 	if (le32_to_cpu(rsp->status) != DEBUG_MEM_STATUS_SUCCESS) {
2003 		ret = -ENXIO;
2004 		goto out;
2005 	}
2006 
2007 	len = min((size_t)le32_to_cpu(rsp->len) << 2,
2008 		  iwl_rx_packet_payload_len(hcmd.resp_pkt) - sizeof(*rsp));
2009 	len = min(len - delta, count);
2010 	if (len < 0) {
2011 		ret = -EFAULT;
2012 		goto out;
2013 	}
2014 
2015 	ret = len - copy_to_user(user_buf, (u8 *)rsp->data + delta, len);
2016 	*ppos += ret;
2017 
2018 out:
2019 	iwl_free_resp(&hcmd);
2020 	return ret;
2021 }
2022 
iwl_dbgfs_mem_write(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)2023 static ssize_t iwl_dbgfs_mem_write(struct file *file,
2024 				   const char __user *user_buf, size_t count,
2025 				   loff_t *ppos)
2026 {
2027 	struct iwl_mvm *mvm = file->private_data;
2028 	struct iwl_dbg_mem_access_cmd *cmd;
2029 	struct iwl_dbg_mem_access_rsp *rsp;
2030 	struct iwl_host_cmd hcmd = {};
2031 	size_t cmd_size;
2032 	size_t data_size;
2033 	u32 op, len;
2034 	ssize_t ret;
2035 
2036 	if (!iwl_mvm_firmware_running(mvm))
2037 		return -EIO;
2038 
2039 	hcmd.id = WIDE_ID(DEBUG_GROUP, *ppos >> 24 ? UMAC_RD_WR : LMAC_RD_WR);
2040 
2041 	if (*ppos & 0x3 || count < 4) {
2042 		op = DEBUG_MEM_OP_WRITE_BYTES;
2043 		len = min(count, (size_t)(4 - (*ppos & 0x3)));
2044 		data_size = len;
2045 	} else {
2046 		op = DEBUG_MEM_OP_WRITE;
2047 		len = min(count >> 2, (size_t)DEBUG_MEM_MAX_SIZE_DWORDS);
2048 		data_size = len << 2;
2049 	}
2050 
2051 	cmd_size = sizeof(*cmd) + ALIGN(data_size, 4);
2052 	cmd = kzalloc(cmd_size, GFP_KERNEL);
2053 	if (!cmd)
2054 		return -ENOMEM;
2055 
2056 	cmd->op = cpu_to_le32(op);
2057 	cmd->len = cpu_to_le32(len);
2058 	cmd->addr = cpu_to_le32(*ppos);
2059 	if (copy_from_user((void *)cmd->data, user_buf, data_size)) {
2060 		kfree(cmd);
2061 		return -EFAULT;
2062 	}
2063 
2064 	hcmd.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
2065 	hcmd.data[0] = (void *)cmd;
2066 	hcmd.len[0] = cmd_size;
2067 
2068 	mutex_lock(&mvm->mutex);
2069 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
2070 	mutex_unlock(&mvm->mutex);
2071 
2072 	kfree(cmd);
2073 
2074 	if (ret < 0)
2075 		return ret;
2076 
2077 	if (iwl_rx_packet_payload_len(hcmd.resp_pkt) < sizeof(*rsp)) {
2078 		ret = -EIO;
2079 		goto out;
2080 	}
2081 
2082 	rsp = (void *)hcmd.resp_pkt->data;
2083 	if (rsp->status != DEBUG_MEM_STATUS_SUCCESS) {
2084 		ret = -ENXIO;
2085 		goto out;
2086 	}
2087 
2088 	ret = data_size;
2089 	*ppos += ret;
2090 
2091 out:
2092 	iwl_free_resp(&hcmd);
2093 	return ret;
2094 }
2095 
2096 static const struct file_operations iwl_dbgfs_mem_ops = {
2097 	.read = iwl_dbgfs_mem_read,
2098 	.write = iwl_dbgfs_mem_write,
2099 	.open = simple_open,
2100 	.llseek = default_llseek,
2101 };
2102 
iwl_mvm_link_sta_add_debugfs(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_link_sta * link_sta,struct dentry * dir)2103 void iwl_mvm_link_sta_add_debugfs(struct ieee80211_hw *hw,
2104 				  struct ieee80211_vif *vif,
2105 				  struct ieee80211_link_sta *link_sta,
2106 				  struct dentry *dir)
2107 {
2108 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2109 
2110 	if (iwl_mvm_has_tlc_offload(mvm)) {
2111 		MVM_DEBUGFS_ADD_LINK_STA_FILE(rs_data, dir, 0400);
2112 	}
2113 
2114 	MVM_DEBUGFS_ADD_LINK_STA_FILE(amsdu_len, dir, 0600);
2115 }
2116 
iwl_mvm_dbgfs_register(struct iwl_mvm * mvm)2117 void iwl_mvm_dbgfs_register(struct iwl_mvm *mvm)
2118 {
2119 	struct dentry *bcast_dir __maybe_unused;
2120 
2121 	spin_lock_init(&mvm->drv_stats_lock);
2122 
2123 	MVM_DEBUGFS_ADD_FILE(tx_flush, mvm->debugfs_dir, 0200);
2124 	MVM_DEBUGFS_ADD_FILE(sram, mvm->debugfs_dir, 0600);
2125 	MVM_DEBUGFS_ADD_FILE(set_nic_temperature, mvm->debugfs_dir, 0600);
2126 	MVM_DEBUGFS_ADD_FILE(nic_temp, mvm->debugfs_dir, 0400);
2127 	MVM_DEBUGFS_ADD_FILE(ctdp_budget, mvm->debugfs_dir, 0400);
2128 	MVM_DEBUGFS_ADD_FILE(stop_ctdp, mvm->debugfs_dir, 0200);
2129 	MVM_DEBUGFS_ADD_FILE(start_ctdp, mvm->debugfs_dir, 0200);
2130 	MVM_DEBUGFS_ADD_FILE(force_ctkill, mvm->debugfs_dir, 0200);
2131 	MVM_DEBUGFS_ADD_FILE(stations, mvm->debugfs_dir, 0400);
2132 	MVM_DEBUGFS_ADD_FILE(disable_power_off, mvm->debugfs_dir, 0600);
2133 	MVM_DEBUGFS_ADD_FILE(fw_rx_stats, mvm->debugfs_dir, 0400);
2134 	MVM_DEBUGFS_ADD_FILE(drv_rx_stats, mvm->debugfs_dir, 0400);
2135 	MVM_DEBUGFS_ADD_FILE(fw_system_stats, mvm->debugfs_dir, 0400);
2136 	MVM_DEBUGFS_ADD_FILE(fw_restart, mvm->debugfs_dir, 0200);
2137 	MVM_DEBUGFS_ADD_FILE(fw_nmi, mvm->debugfs_dir, 0200);
2138 	MVM_DEBUGFS_ADD_FILE(scan_ant_rxchain, mvm->debugfs_dir, 0600);
2139 	MVM_DEBUGFS_ADD_FILE(prph_reg, mvm->debugfs_dir, 0600);
2140 	MVM_DEBUGFS_ADD_FILE(fw_dbg_conf, mvm->debugfs_dir, 0600);
2141 	MVM_DEBUGFS_ADD_FILE(fw_dbg_clear, mvm->debugfs_dir, 0200);
2142 	MVM_DEBUGFS_ADD_FILE(dbg_time_point, mvm->debugfs_dir, 0200);
2143 	MVM_DEBUGFS_ADD_FILE(send_echo_cmd, mvm->debugfs_dir, 0200);
2144 	MVM_DEBUGFS_ADD_FILE(indirection_tbl, mvm->debugfs_dir, 0200);
2145 	MVM_DEBUGFS_ADD_FILE(inject_packet, mvm->debugfs_dir, 0200);
2146 	MVM_DEBUGFS_ADD_FILE(inject_beacon_ie, mvm->debugfs_dir, 0200);
2147 	MVM_DEBUGFS_ADD_FILE(inject_beacon_ie_restore, mvm->debugfs_dir, 0200);
2148 	MVM_DEBUGFS_ADD_FILE(rfi_freq_table, mvm->debugfs_dir, 0600);
2149 
2150 	if (mvm->fw->phy_integration_ver)
2151 		MVM_DEBUGFS_ADD_FILE(phy_integration_ver, mvm->debugfs_dir, 0400);
2152 	MVM_DEBUGFS_ADD_FILE(tas_get_status, mvm->debugfs_dir, 0400);
2153 #ifdef CONFIG_ACPI
2154 	MVM_DEBUGFS_ADD_FILE(sar_geo_profile, mvm->debugfs_dir, 0400);
2155 	MVM_DEBUGFS_ADD_FILE(wifi_6e_enable, mvm->debugfs_dir, 0400);
2156 #endif
2157 	MVM_DEBUGFS_ADD_FILE(he_sniffer_params, mvm->debugfs_dir, 0600);
2158 
2159 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_SET_LTR_GEN2))
2160 		MVM_DEBUGFS_ADD_FILE(ltr_config, mvm->debugfs_dir, 0200);
2161 
2162 	debugfs_create_bool("enable_scan_iteration_notif", 0600,
2163 			    mvm->debugfs_dir, &mvm->scan_iter_notif_enabled);
2164 	debugfs_create_bool("drop_bcn_ap_mode", 0600, mvm->debugfs_dir,
2165 			    &mvm->drop_bcn_ap_mode);
2166 
2167 	MVM_DEBUGFS_ADD_FILE(uapsd_noagg_bssids, mvm->debugfs_dir, S_IRUSR);
2168 
2169 #ifdef CONFIG_PM_SLEEP
2170 	MVM_DEBUGFS_ADD_FILE(d3_test, mvm->debugfs_dir, 0400);
2171 	debugfs_create_bool("d3_wake_sysassert", 0600, mvm->debugfs_dir,
2172 			    &mvm->d3_wake_sysassert);
2173 	debugfs_create_u32("last_netdetect_scans", 0400, mvm->debugfs_dir,
2174 			   &mvm->last_netdetect_scans);
2175 #endif
2176 
2177 	debugfs_create_u8("ps_disabled", 0400, mvm->debugfs_dir,
2178 			  &mvm->ps_disabled);
2179 	debugfs_create_blob("nvm_hw", 0400, mvm->debugfs_dir,
2180 			    &mvm->nvm_hw_blob);
2181 	debugfs_create_blob("nvm_sw", 0400, mvm->debugfs_dir,
2182 			    &mvm->nvm_sw_blob);
2183 	debugfs_create_blob("nvm_calib", 0400, mvm->debugfs_dir,
2184 			    &mvm->nvm_calib_blob);
2185 	debugfs_create_blob("nvm_prod", 0400, mvm->debugfs_dir,
2186 			    &mvm->nvm_prod_blob);
2187 	debugfs_create_blob("nvm_phy_sku", 0400, mvm->debugfs_dir,
2188 			    &mvm->nvm_phy_sku_blob);
2189 	debugfs_create_blob("nvm_reg", S_IRUSR,
2190 			    mvm->debugfs_dir, &mvm->nvm_reg_blob);
2191 
2192 	debugfs_create_file("mem", 0600, mvm->debugfs_dir, mvm,
2193 			    &iwl_dbgfs_mem_ops);
2194 
2195 	debugfs_create_bool("rx_ts_ptp", 0600, mvm->debugfs_dir,
2196 			    &mvm->rx_ts_ptp);
2197 
2198 	/*
2199 	 * Create a symlink with mac80211. It will be removed when mac80211
2200 	 * exists (before the opmode exists which removes the target.)
2201 	 */
2202 	if (!IS_ERR(mvm->debugfs_dir)) {
2203 		char buf[100];
2204 
2205 		snprintf(buf, 100, "../../%pd2", mvm->debugfs_dir->d_parent);
2206 		debugfs_create_symlink("iwlwifi", mvm->hw->wiphy->debugfsdir,
2207 				       buf);
2208 	}
2209 }
2210