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