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