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_disable_power_off_read(struct file *file,
429 						char __user *user_buf,
430 						size_t count, loff_t *ppos)
431 {
432 	struct iwl_mvm *mvm = file->private_data;
433 	char buf[64];
434 	int bufsz = sizeof(buf);
435 	int pos = 0;
436 
437 	pos += scnprintf(buf+pos, bufsz-pos, "disable_power_off_d0=%d\n",
438 			 mvm->disable_power_off);
439 	pos += scnprintf(buf+pos, bufsz-pos, "disable_power_off_d3=%d\n",
440 			 mvm->disable_power_off_d3);
441 
442 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
443 }
444 
445 static ssize_t iwl_dbgfs_disable_power_off_write(struct iwl_mvm *mvm, char *buf,
446 						 size_t count, loff_t *ppos)
447 {
448 	int ret, val;
449 
450 	if (!iwl_mvm_firmware_running(mvm))
451 		return -EIO;
452 
453 	if (!strncmp("disable_power_off_d0=", buf, 21)) {
454 		if (sscanf(buf + 21, "%d", &val) != 1)
455 			return -EINVAL;
456 		mvm->disable_power_off = val;
457 	} else if (!strncmp("disable_power_off_d3=", buf, 21)) {
458 		if (sscanf(buf + 21, "%d", &val) != 1)
459 			return -EINVAL;
460 		mvm->disable_power_off_d3 = val;
461 	} else {
462 		return -EINVAL;
463 	}
464 
465 	mutex_lock(&mvm->mutex);
466 	ret = iwl_mvm_power_update_device(mvm);
467 	mutex_unlock(&mvm->mutex);
468 
469 	return ret ?: count;
470 }
471 
472 static
473 int iwl_mvm_coex_dump_mbox(struct iwl_mvm *mvm,
474 			   struct iwl_bt_coex_profile_notif *notif, char *buf,
475 			   int pos, int bufsz)
476 {
477 	pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw0:\n");
478 
479 	BT_MBOX_PRINT(0, LE_SLAVE_LAT, false);
480 	BT_MBOX_PRINT(0, LE_PROF1, false);
481 	BT_MBOX_PRINT(0, LE_PROF2, false);
482 	BT_MBOX_PRINT(0, LE_PROF_OTHER, false);
483 	BT_MBOX_PRINT(0, CHL_SEQ_N, false);
484 	BT_MBOX_PRINT(0, INBAND_S, false);
485 	BT_MBOX_PRINT(0, LE_MIN_RSSI, false);
486 	BT_MBOX_PRINT(0, LE_SCAN, false);
487 	BT_MBOX_PRINT(0, LE_ADV, false);
488 	BT_MBOX_PRINT(0, LE_MAX_TX_POWER, false);
489 	BT_MBOX_PRINT(0, OPEN_CON_1, true);
490 
491 	pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw1:\n");
492 
493 	BT_MBOX_PRINT(1, BR_MAX_TX_POWER, false);
494 	BT_MBOX_PRINT(1, IP_SR, false);
495 	BT_MBOX_PRINT(1, LE_MSTR, false);
496 	BT_MBOX_PRINT(1, AGGR_TRFC_LD, false);
497 	BT_MBOX_PRINT(1, MSG_TYPE, false);
498 	BT_MBOX_PRINT(1, SSN, true);
499 
500 	pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw2:\n");
501 
502 	BT_MBOX_PRINT(2, SNIFF_ACT, false);
503 	BT_MBOX_PRINT(2, PAG, false);
504 	BT_MBOX_PRINT(2, INQUIRY, false);
505 	BT_MBOX_PRINT(2, CONN, false);
506 	BT_MBOX_PRINT(2, SNIFF_INTERVAL, false);
507 	BT_MBOX_PRINT(2, DISC, false);
508 	BT_MBOX_PRINT(2, SCO_TX_ACT, false);
509 	BT_MBOX_PRINT(2, SCO_RX_ACT, false);
510 	BT_MBOX_PRINT(2, ESCO_RE_TX, false);
511 	BT_MBOX_PRINT(2, SCO_DURATION, true);
512 
513 	pos += scnprintf(buf+pos, bufsz-pos, "MBOX dw3:\n");
514 
515 	BT_MBOX_PRINT(3, SCO_STATE, false);
516 	BT_MBOX_PRINT(3, SNIFF_STATE, false);
517 	BT_MBOX_PRINT(3, A2DP_STATE, false);
518 	BT_MBOX_PRINT(3, A2DP_SRC, false);
519 	BT_MBOX_PRINT(3, ACL_STATE, false);
520 	BT_MBOX_PRINT(3, MSTR_STATE, false);
521 	BT_MBOX_PRINT(3, OBX_STATE, false);
522 	BT_MBOX_PRINT(3, OPEN_CON_2, false);
523 	BT_MBOX_PRINT(3, TRAFFIC_LOAD, false);
524 	BT_MBOX_PRINT(3, CHL_SEQN_LSB, false);
525 	BT_MBOX_PRINT(3, INBAND_P, false);
526 	BT_MBOX_PRINT(3, MSG_TYPE_2, false);
527 	BT_MBOX_PRINT(3, SSN_2, false);
528 	BT_MBOX_PRINT(3, UPDATE_REQUEST, !iwl_mvm_has_new_ats_coex_api(mvm));
529 
530 	if (iwl_mvm_has_new_ats_coex_api(mvm)) {
531 		BT_MBOX_PRINT(4, ATS_BT_INTERVAL, false);
532 		BT_MBOX_PRINT(4, ATS_BT_ACTIVE_MAX_TH, true);
533 	}
534 
535 	return pos;
536 }
537 
538 static ssize_t iwl_dbgfs_bt_notif_read(struct file *file, char __user *user_buf,
539 				       size_t count, loff_t *ppos)
540 {
541 	struct iwl_mvm *mvm = file->private_data;
542 	struct iwl_bt_coex_profile_notif *notif = &mvm->last_bt_notif;
543 	char *buf;
544 	int ret, pos = 0, bufsz = sizeof(char) * 1024;
545 
546 	buf = kmalloc(bufsz, GFP_KERNEL);
547 	if (!buf)
548 		return -ENOMEM;
549 
550 	mutex_lock(&mvm->mutex);
551 
552 	pos += iwl_mvm_coex_dump_mbox(mvm, notif, buf, pos, bufsz);
553 
554 	pos += scnprintf(buf + pos, bufsz - pos, "bt_ci_compliance = %d\n",
555 			 notif->bt_ci_compliance);
556 	pos += scnprintf(buf + pos, bufsz - pos, "primary_ch_lut = %d\n",
557 			 le32_to_cpu(notif->primary_ch_lut));
558 	pos += scnprintf(buf + pos, bufsz - pos, "secondary_ch_lut = %d\n",
559 			 le32_to_cpu(notif->secondary_ch_lut));
560 	pos += scnprintf(buf + pos,
561 			 bufsz - pos, "bt_activity_grading = %d\n",
562 			 le32_to_cpu(notif->bt_activity_grading));
563 	pos += scnprintf(buf + pos, bufsz - pos, "bt_rrc = %d\n",
564 			 notif->rrc_status & 0xF);
565 	pos += scnprintf(buf + pos, bufsz - pos, "bt_ttc = %d\n",
566 			 notif->ttc_status & 0xF);
567 
568 	pos += scnprintf(buf + pos, bufsz - pos, "sync_sco = %d\n",
569 			 IWL_MVM_BT_COEX_SYNC2SCO);
570 	pos += scnprintf(buf + pos, bufsz - pos, "mplut = %d\n",
571 			 IWL_MVM_BT_COEX_MPLUT);
572 
573 	mutex_unlock(&mvm->mutex);
574 
575 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
576 	kfree(buf);
577 
578 	return ret;
579 }
580 #undef BT_MBOX_PRINT
581 
582 static ssize_t iwl_dbgfs_bt_cmd_read(struct file *file, char __user *user_buf,
583 				     size_t count, loff_t *ppos)
584 {
585 	struct iwl_mvm *mvm = file->private_data;
586 	struct iwl_bt_coex_ci_cmd *cmd = &mvm->last_bt_ci_cmd;
587 	char buf[256];
588 	int bufsz = sizeof(buf);
589 	int pos = 0;
590 
591 	mutex_lock(&mvm->mutex);
592 
593 	pos += scnprintf(buf + pos, bufsz - pos, "Channel inhibition CMD\n");
594 	pos += scnprintf(buf + pos, bufsz - pos,
595 			 "\tPrimary Channel Bitmap 0x%016llx\n",
596 			 le64_to_cpu(cmd->bt_primary_ci));
597 	pos += scnprintf(buf + pos, bufsz - pos,
598 			 "\tSecondary Channel Bitmap 0x%016llx\n",
599 			 le64_to_cpu(cmd->bt_secondary_ci));
600 
601 	mutex_unlock(&mvm->mutex);
602 
603 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
604 }
605 
606 static ssize_t
607 iwl_dbgfs_bt_tx_prio_write(struct iwl_mvm *mvm, char *buf,
608 			   size_t count, loff_t *ppos)
609 {
610 	u32 bt_tx_prio;
611 
612 	if (sscanf(buf, "%u", &bt_tx_prio) != 1)
613 		return -EINVAL;
614 	if (bt_tx_prio > 4)
615 		return -EINVAL;
616 
617 	mvm->bt_tx_prio = bt_tx_prio;
618 
619 	return count;
620 }
621 
622 static ssize_t
623 iwl_dbgfs_bt_force_ant_write(struct iwl_mvm *mvm, char *buf,
624 			     size_t count, loff_t *ppos)
625 {
626 	static const char * const modes_str[BT_FORCE_ANT_MAX] = {
627 		[BT_FORCE_ANT_DIS] = "dis",
628 		[BT_FORCE_ANT_AUTO] = "auto",
629 		[BT_FORCE_ANT_BT] = "bt",
630 		[BT_FORCE_ANT_WIFI] = "wifi",
631 	};
632 	int ret, bt_force_ant_mode;
633 
634 	for (bt_force_ant_mode = 0;
635 	     bt_force_ant_mode < ARRAY_SIZE(modes_str);
636 	     bt_force_ant_mode++) {
637 		if (!strcmp(buf, modes_str[bt_force_ant_mode]))
638 			break;
639 	}
640 
641 	if (bt_force_ant_mode >= ARRAY_SIZE(modes_str))
642 		return -EINVAL;
643 
644 	ret = 0;
645 	mutex_lock(&mvm->mutex);
646 	if (mvm->bt_force_ant_mode == bt_force_ant_mode)
647 		goto out;
648 
649 	mvm->bt_force_ant_mode = bt_force_ant_mode;
650 	IWL_DEBUG_COEX(mvm, "Force mode: %s\n",
651 		       modes_str[mvm->bt_force_ant_mode]);
652 
653 	if (iwl_mvm_firmware_running(mvm))
654 		ret = iwl_mvm_send_bt_init_conf(mvm);
655 	else
656 		ret = 0;
657 
658 out:
659 	mutex_unlock(&mvm->mutex);
660 	return ret ?: count;
661 }
662 
663 static ssize_t iwl_dbgfs_fw_ver_read(struct file *file, char __user *user_buf,
664 				     size_t count, loff_t *ppos)
665 {
666 	struct iwl_mvm *mvm = file->private_data;
667 	char *buff, *pos, *endpos;
668 	static const size_t bufsz = 1024;
669 	int ret;
670 
671 	buff = kmalloc(bufsz, GFP_KERNEL);
672 	if (!buff)
673 		return -ENOMEM;
674 
675 	pos = buff;
676 	endpos = pos + bufsz;
677 
678 	pos += scnprintf(pos, endpos - pos, "FW prefix: %s\n",
679 			 mvm->trans->cfg->fw_name_pre);
680 	pos += scnprintf(pos, endpos - pos, "FW: %s\n",
681 			 mvm->fwrt.fw->human_readable);
682 	pos += scnprintf(pos, endpos - pos, "Device: %s\n",
683 			 mvm->fwrt.trans->cfg->name);
684 	pos += scnprintf(pos, endpos - pos, "Bus: %s\n",
685 			 mvm->fwrt.dev->bus->name);
686 
687 	ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
688 	kfree(buff);
689 
690 	return ret;
691 }
692 
693 #define PRINT_STATS_LE32(_struct, _memb)				\
694 			 pos += scnprintf(buf + pos, bufsz - pos,	\
695 					  fmt_table, #_memb,		\
696 					  le32_to_cpu(_struct->_memb))
697 
698 static ssize_t iwl_dbgfs_fw_rx_stats_read(struct file *file,
699 					  char __user *user_buf, size_t count,
700 					  loff_t *ppos)
701 {
702 	struct iwl_mvm *mvm = file->private_data;
703 	static const char *fmt_table = "\t%-30s %10u\n";
704 	static const char *fmt_header = "%-32s\n";
705 	int pos = 0;
706 	char *buf;
707 	int ret;
708 	size_t bufsz;
709 
710 	if (iwl_mvm_has_new_rx_stats_api(mvm))
711 		bufsz = ((sizeof(struct mvm_statistics_rx) /
712 			  sizeof(__le32)) * 43) + (4 * 33) + 1;
713 	else
714 		/* 43 = size of each data line; 33 = size of each header */
715 		bufsz = ((sizeof(struct mvm_statistics_rx_v3) /
716 			  sizeof(__le32)) * 43) + (4 * 33) + 1;
717 
718 	buf = kzalloc(bufsz, GFP_KERNEL);
719 	if (!buf)
720 		return -ENOMEM;
721 
722 	mutex_lock(&mvm->mutex);
723 
724 	pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
725 			 "Statistics_Rx - OFDM");
726 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
727 		struct mvm_statistics_rx_phy_v2 *ofdm = &mvm->rx_stats_v3.ofdm;
728 
729 		PRINT_STATS_LE32(ofdm, ina_cnt);
730 		PRINT_STATS_LE32(ofdm, fina_cnt);
731 		PRINT_STATS_LE32(ofdm, plcp_err);
732 		PRINT_STATS_LE32(ofdm, crc32_err);
733 		PRINT_STATS_LE32(ofdm, overrun_err);
734 		PRINT_STATS_LE32(ofdm, early_overrun_err);
735 		PRINT_STATS_LE32(ofdm, crc32_good);
736 		PRINT_STATS_LE32(ofdm, false_alarm_cnt);
737 		PRINT_STATS_LE32(ofdm, fina_sync_err_cnt);
738 		PRINT_STATS_LE32(ofdm, sfd_timeout);
739 		PRINT_STATS_LE32(ofdm, fina_timeout);
740 		PRINT_STATS_LE32(ofdm, unresponded_rts);
741 		PRINT_STATS_LE32(ofdm, rxe_frame_lmt_overrun);
742 		PRINT_STATS_LE32(ofdm, sent_ack_cnt);
743 		PRINT_STATS_LE32(ofdm, sent_cts_cnt);
744 		PRINT_STATS_LE32(ofdm, sent_ba_rsp_cnt);
745 		PRINT_STATS_LE32(ofdm, dsp_self_kill);
746 		PRINT_STATS_LE32(ofdm, mh_format_err);
747 		PRINT_STATS_LE32(ofdm, re_acq_main_rssi_sum);
748 		PRINT_STATS_LE32(ofdm, reserved);
749 	} else {
750 		struct mvm_statistics_rx_phy *ofdm = &mvm->rx_stats.ofdm;
751 
752 		PRINT_STATS_LE32(ofdm, unresponded_rts);
753 		PRINT_STATS_LE32(ofdm, rxe_frame_lmt_overrun);
754 		PRINT_STATS_LE32(ofdm, sent_ba_rsp_cnt);
755 		PRINT_STATS_LE32(ofdm, dsp_self_kill);
756 		PRINT_STATS_LE32(ofdm, reserved);
757 	}
758 
759 	pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
760 			 "Statistics_Rx - CCK");
761 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
762 		struct mvm_statistics_rx_phy_v2 *cck = &mvm->rx_stats_v3.cck;
763 
764 		PRINT_STATS_LE32(cck, ina_cnt);
765 		PRINT_STATS_LE32(cck, fina_cnt);
766 		PRINT_STATS_LE32(cck, plcp_err);
767 		PRINT_STATS_LE32(cck, crc32_err);
768 		PRINT_STATS_LE32(cck, overrun_err);
769 		PRINT_STATS_LE32(cck, early_overrun_err);
770 		PRINT_STATS_LE32(cck, crc32_good);
771 		PRINT_STATS_LE32(cck, false_alarm_cnt);
772 		PRINT_STATS_LE32(cck, fina_sync_err_cnt);
773 		PRINT_STATS_LE32(cck, sfd_timeout);
774 		PRINT_STATS_LE32(cck, fina_timeout);
775 		PRINT_STATS_LE32(cck, unresponded_rts);
776 		PRINT_STATS_LE32(cck, rxe_frame_lmt_overrun);
777 		PRINT_STATS_LE32(cck, sent_ack_cnt);
778 		PRINT_STATS_LE32(cck, sent_cts_cnt);
779 		PRINT_STATS_LE32(cck, sent_ba_rsp_cnt);
780 		PRINT_STATS_LE32(cck, dsp_self_kill);
781 		PRINT_STATS_LE32(cck, mh_format_err);
782 		PRINT_STATS_LE32(cck, re_acq_main_rssi_sum);
783 		PRINT_STATS_LE32(cck, reserved);
784 	} else {
785 		struct mvm_statistics_rx_phy *cck = &mvm->rx_stats.cck;
786 
787 		PRINT_STATS_LE32(cck, unresponded_rts);
788 		PRINT_STATS_LE32(cck, rxe_frame_lmt_overrun);
789 		PRINT_STATS_LE32(cck, sent_ba_rsp_cnt);
790 		PRINT_STATS_LE32(cck, dsp_self_kill);
791 		PRINT_STATS_LE32(cck, reserved);
792 	}
793 
794 	pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
795 			 "Statistics_Rx - GENERAL");
796 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
797 		struct mvm_statistics_rx_non_phy_v3 *general =
798 			&mvm->rx_stats_v3.general;
799 
800 		PRINT_STATS_LE32(general, bogus_cts);
801 		PRINT_STATS_LE32(general, bogus_ack);
802 		PRINT_STATS_LE32(general, non_bssid_frames);
803 		PRINT_STATS_LE32(general, filtered_frames);
804 		PRINT_STATS_LE32(general, non_channel_beacons);
805 		PRINT_STATS_LE32(general, channel_beacons);
806 		PRINT_STATS_LE32(general, num_missed_bcon);
807 		PRINT_STATS_LE32(general, adc_rx_saturation_time);
808 		PRINT_STATS_LE32(general, ina_detection_search_time);
809 		PRINT_STATS_LE32(general, beacon_silence_rssi_a);
810 		PRINT_STATS_LE32(general, beacon_silence_rssi_b);
811 		PRINT_STATS_LE32(general, beacon_silence_rssi_c);
812 		PRINT_STATS_LE32(general, interference_data_flag);
813 		PRINT_STATS_LE32(general, channel_load);
814 		PRINT_STATS_LE32(general, dsp_false_alarms);
815 		PRINT_STATS_LE32(general, beacon_rssi_a);
816 		PRINT_STATS_LE32(general, beacon_rssi_b);
817 		PRINT_STATS_LE32(general, beacon_rssi_c);
818 		PRINT_STATS_LE32(general, beacon_energy_a);
819 		PRINT_STATS_LE32(general, beacon_energy_b);
820 		PRINT_STATS_LE32(general, beacon_energy_c);
821 		PRINT_STATS_LE32(general, num_bt_kills);
822 		PRINT_STATS_LE32(general, mac_id);
823 		PRINT_STATS_LE32(general, directed_data_mpdu);
824 	} else {
825 		struct mvm_statistics_rx_non_phy *general =
826 			&mvm->rx_stats.general;
827 
828 		PRINT_STATS_LE32(general, bogus_cts);
829 		PRINT_STATS_LE32(general, bogus_ack);
830 		PRINT_STATS_LE32(general, non_channel_beacons);
831 		PRINT_STATS_LE32(general, channel_beacons);
832 		PRINT_STATS_LE32(general, num_missed_bcon);
833 		PRINT_STATS_LE32(general, adc_rx_saturation_time);
834 		PRINT_STATS_LE32(general, ina_detection_search_time);
835 		PRINT_STATS_LE32(general, beacon_silence_rssi_a);
836 		PRINT_STATS_LE32(general, beacon_silence_rssi_b);
837 		PRINT_STATS_LE32(general, beacon_silence_rssi_c);
838 		PRINT_STATS_LE32(general, interference_data_flag);
839 		PRINT_STATS_LE32(general, channel_load);
840 		PRINT_STATS_LE32(general, beacon_rssi_a);
841 		PRINT_STATS_LE32(general, beacon_rssi_b);
842 		PRINT_STATS_LE32(general, beacon_rssi_c);
843 		PRINT_STATS_LE32(general, beacon_energy_a);
844 		PRINT_STATS_LE32(general, beacon_energy_b);
845 		PRINT_STATS_LE32(general, beacon_energy_c);
846 		PRINT_STATS_LE32(general, num_bt_kills);
847 		PRINT_STATS_LE32(general, mac_id);
848 	}
849 
850 	pos += scnprintf(buf + pos, bufsz - pos, fmt_header,
851 			 "Statistics_Rx - HT");
852 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
853 		struct mvm_statistics_rx_ht_phy_v1 *ht =
854 			&mvm->rx_stats_v3.ofdm_ht;
855 
856 		PRINT_STATS_LE32(ht, plcp_err);
857 		PRINT_STATS_LE32(ht, overrun_err);
858 		PRINT_STATS_LE32(ht, early_overrun_err);
859 		PRINT_STATS_LE32(ht, crc32_good);
860 		PRINT_STATS_LE32(ht, crc32_err);
861 		PRINT_STATS_LE32(ht, mh_format_err);
862 		PRINT_STATS_LE32(ht, agg_crc32_good);
863 		PRINT_STATS_LE32(ht, agg_mpdu_cnt);
864 		PRINT_STATS_LE32(ht, agg_cnt);
865 		PRINT_STATS_LE32(ht, unsupport_mcs);
866 	} else {
867 		struct mvm_statistics_rx_ht_phy *ht =
868 			&mvm->rx_stats.ofdm_ht;
869 
870 		PRINT_STATS_LE32(ht, mh_format_err);
871 		PRINT_STATS_LE32(ht, agg_mpdu_cnt);
872 		PRINT_STATS_LE32(ht, agg_cnt);
873 		PRINT_STATS_LE32(ht, unsupport_mcs);
874 	}
875 
876 	mutex_unlock(&mvm->mutex);
877 
878 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
879 	kfree(buf);
880 
881 	return ret;
882 }
883 #undef PRINT_STAT_LE32
884 
885 static ssize_t iwl_dbgfs_frame_stats_read(struct iwl_mvm *mvm,
886 					  char __user *user_buf, size_t count,
887 					  loff_t *ppos,
888 					  struct iwl_mvm_frame_stats *stats)
889 {
890 	char *buff, *pos, *endpos;
891 	int idx, i;
892 	int ret;
893 	static const size_t bufsz = 1024;
894 
895 	buff = kmalloc(bufsz, GFP_KERNEL);
896 	if (!buff)
897 		return -ENOMEM;
898 
899 	spin_lock_bh(&mvm->drv_stats_lock);
900 
901 	pos = buff;
902 	endpos = pos + bufsz;
903 
904 	pos += scnprintf(pos, endpos - pos,
905 			 "Legacy/HT/VHT\t:\t%d/%d/%d\n",
906 			 stats->legacy_frames,
907 			 stats->ht_frames,
908 			 stats->vht_frames);
909 	pos += scnprintf(pos, endpos - pos, "20/40/80\t:\t%d/%d/%d\n",
910 			 stats->bw_20_frames,
911 			 stats->bw_40_frames,
912 			 stats->bw_80_frames);
913 	pos += scnprintf(pos, endpos - pos, "NGI/SGI\t\t:\t%d/%d\n",
914 			 stats->ngi_frames,
915 			 stats->sgi_frames);
916 	pos += scnprintf(pos, endpos - pos, "SISO/MIMO2\t:\t%d/%d\n",
917 			 stats->siso_frames,
918 			 stats->mimo2_frames);
919 	pos += scnprintf(pos, endpos - pos, "FAIL/SCSS\t:\t%d/%d\n",
920 			 stats->fail_frames,
921 			 stats->success_frames);
922 	pos += scnprintf(pos, endpos - pos, "MPDUs agg\t:\t%d\n",
923 			 stats->agg_frames);
924 	pos += scnprintf(pos, endpos - pos, "A-MPDUs\t\t:\t%d\n",
925 			 stats->ampdu_count);
926 	pos += scnprintf(pos, endpos - pos, "Avg MPDUs/A-MPDU:\t%d\n",
927 			 stats->ampdu_count > 0 ?
928 			 (stats->agg_frames / stats->ampdu_count) : 0);
929 
930 	pos += scnprintf(pos, endpos - pos, "Last Rates\n");
931 
932 	idx = stats->last_frame_idx - 1;
933 	for (i = 0; i < ARRAY_SIZE(stats->last_rates); i++) {
934 		idx = (idx + 1) % ARRAY_SIZE(stats->last_rates);
935 		if (stats->last_rates[idx] == 0)
936 			continue;
937 		pos += scnprintf(pos, endpos - pos, "Rate[%d]: ",
938 				 (int)(ARRAY_SIZE(stats->last_rates) - i));
939 		pos += rs_pretty_print_rate(pos, stats->last_rates[idx]);
940 	}
941 	spin_unlock_bh(&mvm->drv_stats_lock);
942 
943 	ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
944 	kfree(buff);
945 
946 	return ret;
947 }
948 
949 static ssize_t iwl_dbgfs_drv_rx_stats_read(struct file *file,
950 					   char __user *user_buf, size_t count,
951 					   loff_t *ppos)
952 {
953 	struct iwl_mvm *mvm = file->private_data;
954 
955 	return iwl_dbgfs_frame_stats_read(mvm, user_buf, count, ppos,
956 					  &mvm->drv_rx_stats);
957 }
958 
959 static ssize_t iwl_dbgfs_fw_restart_write(struct iwl_mvm *mvm, char *buf,
960 					  size_t count, loff_t *ppos)
961 {
962 	int __maybe_unused ret;
963 
964 	if (!iwl_mvm_firmware_running(mvm))
965 		return -EIO;
966 
967 	mutex_lock(&mvm->mutex);
968 
969 	/* allow one more restart that we're provoking here */
970 	if (mvm->fw_restart >= 0)
971 		mvm->fw_restart++;
972 
973 	/* take the return value to make compiler happy - it will fail anyway */
974 	ret = iwl_mvm_send_cmd_pdu(mvm, REPLY_ERROR, 0, 0, NULL);
975 
976 	mutex_unlock(&mvm->mutex);
977 
978 	return count;
979 }
980 
981 static ssize_t iwl_dbgfs_fw_nmi_write(struct iwl_mvm *mvm, char *buf,
982 				      size_t count, loff_t *ppos)
983 {
984 	int ret;
985 
986 	if (!iwl_mvm_firmware_running(mvm))
987 		return -EIO;
988 
989 	ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_NMI);
990 	if (ret)
991 		return ret;
992 
993 	iwl_force_nmi(mvm->trans);
994 
995 	iwl_mvm_unref(mvm, IWL_MVM_REF_NMI);
996 
997 	return count;
998 }
999 
1000 static ssize_t
1001 iwl_dbgfs_scan_ant_rxchain_read(struct file *file,
1002 				char __user *user_buf,
1003 				size_t count, loff_t *ppos)
1004 {
1005 	struct iwl_mvm *mvm = file->private_data;
1006 	int pos = 0;
1007 	char buf[32];
1008 	const size_t bufsz = sizeof(buf);
1009 
1010 	/* print which antennas were set for the scan command by the user */
1011 	pos += scnprintf(buf + pos, bufsz - pos, "Antennas for scan: ");
1012 	if (mvm->scan_rx_ant & ANT_A)
1013 		pos += scnprintf(buf + pos, bufsz - pos, "A");
1014 	if (mvm->scan_rx_ant & ANT_B)
1015 		pos += scnprintf(buf + pos, bufsz - pos, "B");
1016 	if (mvm->scan_rx_ant & ANT_C)
1017 		pos += scnprintf(buf + pos, bufsz - pos, "C");
1018 	pos += scnprintf(buf + pos, bufsz - pos, " (%hhx)\n", mvm->scan_rx_ant);
1019 
1020 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1021 }
1022 
1023 static ssize_t
1024 iwl_dbgfs_scan_ant_rxchain_write(struct iwl_mvm *mvm, char *buf,
1025 				 size_t count, loff_t *ppos)
1026 {
1027 	u8 scan_rx_ant;
1028 
1029 	if (!iwl_mvm_firmware_running(mvm))
1030 		return -EIO;
1031 
1032 	if (sscanf(buf, "%hhx", &scan_rx_ant) != 1)
1033 		return -EINVAL;
1034 	if (scan_rx_ant > ANT_ABC)
1035 		return -EINVAL;
1036 	if (scan_rx_ant & ~(iwl_mvm_get_valid_rx_ant(mvm)))
1037 		return -EINVAL;
1038 
1039 	if (mvm->scan_rx_ant != scan_rx_ant) {
1040 		mvm->scan_rx_ant = scan_rx_ant;
1041 		if (fw_has_capa(&mvm->fw->ucode_capa,
1042 				IWL_UCODE_TLV_CAPA_UMAC_SCAN))
1043 			iwl_mvm_config_scan(mvm);
1044 	}
1045 
1046 	return count;
1047 }
1048 
1049 static ssize_t iwl_dbgfs_indirection_tbl_write(struct iwl_mvm *mvm,
1050 					       char *buf, size_t count,
1051 					       loff_t *ppos)
1052 {
1053 	struct iwl_rss_config_cmd cmd = {
1054 		.flags = cpu_to_le32(IWL_RSS_ENABLE),
1055 		.hash_mask = IWL_RSS_HASH_TYPE_IPV4_TCP |
1056 			     IWL_RSS_HASH_TYPE_IPV4_UDP |
1057 			     IWL_RSS_HASH_TYPE_IPV4_PAYLOAD |
1058 			     IWL_RSS_HASH_TYPE_IPV6_TCP |
1059 			     IWL_RSS_HASH_TYPE_IPV6_UDP |
1060 			     IWL_RSS_HASH_TYPE_IPV6_PAYLOAD,
1061 	};
1062 	int ret, i, num_repeats, nbytes = count / 2;
1063 
1064 	ret = hex2bin(cmd.indirection_table, buf, nbytes);
1065 	if (ret)
1066 		return ret;
1067 
1068 	/*
1069 	 * The input is the redirection table, partial or full.
1070 	 * Repeat the pattern if needed.
1071 	 * For example, input of 01020F will be repeated 42 times,
1072 	 * indirecting RSS hash results to queues 1, 2, 15 (skipping
1073 	 * queues 3 - 14).
1074 	 */
1075 	num_repeats = ARRAY_SIZE(cmd.indirection_table) / nbytes;
1076 	for (i = 1; i < num_repeats; i++)
1077 		memcpy(&cmd.indirection_table[i * nbytes],
1078 		       cmd.indirection_table, nbytes);
1079 	/* handle cut in the middle pattern for the last places */
1080 	memcpy(&cmd.indirection_table[i * nbytes], cmd.indirection_table,
1081 	       ARRAY_SIZE(cmd.indirection_table) % nbytes);
1082 
1083 	netdev_rss_key_fill(cmd.secret_key, sizeof(cmd.secret_key));
1084 
1085 	mutex_lock(&mvm->mutex);
1086 	if (iwl_mvm_firmware_running(mvm))
1087 		ret = iwl_mvm_send_cmd_pdu(mvm, RSS_CONFIG_CMD, 0,
1088 					   sizeof(cmd), &cmd);
1089 	else
1090 		ret = 0;
1091 	mutex_unlock(&mvm->mutex);
1092 
1093 	return ret ?: count;
1094 }
1095 
1096 static ssize_t iwl_dbgfs_inject_packet_write(struct iwl_mvm *mvm,
1097 					     char *buf, size_t count,
1098 					     loff_t *ppos)
1099 {
1100 	struct iwl_rx_cmd_buffer rxb = {
1101 		._rx_page_order = 0,
1102 		.truesize = 0, /* not used */
1103 		._offset = 0,
1104 	};
1105 	struct iwl_rx_packet *pkt;
1106 	struct iwl_rx_mpdu_desc *desc;
1107 	int bin_len = count / 2;
1108 	int ret = -EINVAL;
1109 
1110 	if (!iwl_mvm_firmware_running(mvm))
1111 		return -EIO;
1112 
1113 	/* supporting only 9000 descriptor */
1114 	if (!mvm->trans->cfg->mq_rx_supported)
1115 		return -ENOTSUPP;
1116 
1117 	rxb._page = alloc_pages(GFP_ATOMIC, 0);
1118 	if (!rxb._page)
1119 		return -ENOMEM;
1120 	pkt = rxb_addr(&rxb);
1121 
1122 	ret = hex2bin(page_address(rxb._page), buf, bin_len);
1123 	if (ret)
1124 		goto out;
1125 
1126 	/* avoid invalid memory access */
1127 	if (bin_len < sizeof(*pkt) + sizeof(*desc))
1128 		goto out;
1129 
1130 	/* check this is RX packet */
1131 	if (WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd) !=
1132 	    WIDE_ID(LEGACY_GROUP, REPLY_RX_MPDU_CMD))
1133 		goto out;
1134 
1135 	/* check the length in metadata matches actual received length */
1136 	desc = (void *)pkt->data;
1137 	if (le16_to_cpu(desc->mpdu_len) !=
1138 	    (bin_len - sizeof(*desc) - sizeof(*pkt)))
1139 		goto out;
1140 
1141 	local_bh_disable();
1142 	iwl_mvm_rx_mpdu_mq(mvm, NULL, &rxb, 0);
1143 	local_bh_enable();
1144 	ret = 0;
1145 
1146 out:
1147 	iwl_free_rxb(&rxb);
1148 
1149 	return ret ?: count;
1150 }
1151 
1152 static ssize_t iwl_dbgfs_fw_dbg_conf_read(struct file *file,
1153 					  char __user *user_buf,
1154 					  size_t count, loff_t *ppos)
1155 {
1156 	struct iwl_mvm *mvm = file->private_data;
1157 	int conf;
1158 	char buf[8];
1159 	const size_t bufsz = sizeof(buf);
1160 	int pos = 0;
1161 
1162 	mutex_lock(&mvm->mutex);
1163 	conf = mvm->fwrt.dump.conf;
1164 	mutex_unlock(&mvm->mutex);
1165 
1166 	pos += scnprintf(buf + pos, bufsz - pos, "%d\n", conf);
1167 
1168 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1169 }
1170 
1171 /*
1172  * Enable / Disable continuous recording.
1173  * Cause the FW to start continuous recording, by sending the relevant hcmd.
1174  * Enable: input of every integer larger than 0, ENABLE_CONT_RECORDING.
1175  * Disable: for 0 as input, DISABLE_CONT_RECORDING.
1176  */
1177 static ssize_t iwl_dbgfs_cont_recording_write(struct iwl_mvm *mvm,
1178 					      char *buf, size_t count,
1179 					      loff_t *ppos)
1180 {
1181 	struct iwl_trans *trans = mvm->trans;
1182 	const struct iwl_fw_dbg_dest_tlv *dest = trans->dbg_dest_tlv;
1183 	struct iwl_continuous_record_cmd cont_rec = {};
1184 	int ret, rec_mode;
1185 
1186 	if (!iwl_mvm_firmware_running(mvm))
1187 		return -EIO;
1188 
1189 	if (!dest)
1190 		return -EOPNOTSUPP;
1191 
1192 	if (dest->monitor_mode != SMEM_MODE ||
1193 	    trans->cfg->device_family < IWL_DEVICE_FAMILY_8000)
1194 		return -EOPNOTSUPP;
1195 
1196 	ret = kstrtoint(buf, 0, &rec_mode);
1197 	if (ret)
1198 		return ret;
1199 
1200 	cont_rec.record_mode.enable_recording = rec_mode ?
1201 		cpu_to_le16(ENABLE_CONT_RECORDING) :
1202 		cpu_to_le16(DISABLE_CONT_RECORDING);
1203 
1204 	mutex_lock(&mvm->mutex);
1205 	ret = iwl_mvm_send_cmd_pdu(mvm, LDBG_CONFIG_CMD, 0,
1206 				   sizeof(cont_rec), &cont_rec);
1207 	mutex_unlock(&mvm->mutex);
1208 
1209 	return ret ?: count;
1210 }
1211 
1212 static ssize_t iwl_dbgfs_fw_dbg_conf_write(struct iwl_mvm *mvm,
1213 					   char *buf, size_t count,
1214 					   loff_t *ppos)
1215 {
1216 	unsigned int conf_id;
1217 	int ret;
1218 
1219 	if (!iwl_mvm_firmware_running(mvm))
1220 		return -EIO;
1221 
1222 	ret = kstrtouint(buf, 0, &conf_id);
1223 	if (ret)
1224 		return ret;
1225 
1226 	if (WARN_ON(conf_id >= FW_DBG_CONF_MAX))
1227 		return -EINVAL;
1228 
1229 	mutex_lock(&mvm->mutex);
1230 	ret = iwl_fw_start_dbg_conf(&mvm->fwrt, conf_id);
1231 	mutex_unlock(&mvm->mutex);
1232 
1233 	return ret ?: count;
1234 }
1235 
1236 static ssize_t iwl_dbgfs_fw_dbg_collect_write(struct iwl_mvm *mvm,
1237 					      char *buf, size_t count,
1238 					      loff_t *ppos)
1239 {
1240 	int ret;
1241 
1242 	if (!iwl_mvm_firmware_running(mvm))
1243 		return -EIO;
1244 
1245 	ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PRPH_WRITE);
1246 	if (ret)
1247 		return ret;
1248 	if (count == 0)
1249 		return 0;
1250 
1251 	iwl_fw_dbg_collect(&mvm->fwrt, FW_DBG_TRIGGER_USER, buf,
1252 			   (count - 1), NULL);
1253 
1254 	iwl_mvm_unref(mvm, IWL_MVM_REF_PRPH_WRITE);
1255 
1256 	return count;
1257 }
1258 
1259 static ssize_t iwl_dbgfs_max_amsdu_len_write(struct iwl_mvm *mvm,
1260 					     char *buf, size_t count,
1261 					     loff_t *ppos)
1262 {
1263 	unsigned int max_amsdu_len;
1264 	int ret;
1265 
1266 	ret = kstrtouint(buf, 0, &max_amsdu_len);
1267 	if (ret)
1268 		return ret;
1269 
1270 	if (max_amsdu_len > IEEE80211_MAX_MPDU_LEN_VHT_11454)
1271 		return -EINVAL;
1272 	mvm->max_amsdu_len = max_amsdu_len;
1273 
1274 	return count;
1275 }
1276 
1277 #define ADD_TEXT(...) pos += scnprintf(buf + pos, bufsz - pos, __VA_ARGS__)
1278 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1279 static ssize_t iwl_dbgfs_bcast_filters_read(struct file *file,
1280 					    char __user *user_buf,
1281 					    size_t count, loff_t *ppos)
1282 {
1283 	struct iwl_mvm *mvm = file->private_data;
1284 	struct iwl_bcast_filter_cmd cmd;
1285 	const struct iwl_fw_bcast_filter *filter;
1286 	char *buf;
1287 	int bufsz = 1024;
1288 	int i, j, pos = 0;
1289 	ssize_t ret;
1290 
1291 	buf = kzalloc(bufsz, GFP_KERNEL);
1292 	if (!buf)
1293 		return -ENOMEM;
1294 
1295 	mutex_lock(&mvm->mutex);
1296 	if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd)) {
1297 		ADD_TEXT("None\n");
1298 		mutex_unlock(&mvm->mutex);
1299 		goto out;
1300 	}
1301 	mutex_unlock(&mvm->mutex);
1302 
1303 	for (i = 0; cmd.filters[i].attrs[0].mask; i++) {
1304 		filter = &cmd.filters[i];
1305 
1306 		ADD_TEXT("Filter [%d]:\n", i);
1307 		ADD_TEXT("\tDiscard=%d\n", filter->discard);
1308 		ADD_TEXT("\tFrame Type: %s\n",
1309 			 filter->frame_type ? "IPv4" : "Generic");
1310 
1311 		for (j = 0; j < ARRAY_SIZE(filter->attrs); j++) {
1312 			const struct iwl_fw_bcast_filter_attr *attr;
1313 
1314 			attr = &filter->attrs[j];
1315 			if (!attr->mask)
1316 				break;
1317 
1318 			ADD_TEXT("\tAttr [%d]: offset=%d (from %s), mask=0x%x, value=0x%x reserved=0x%x\n",
1319 				 j, attr->offset,
1320 				 attr->offset_type ? "IP End" :
1321 						     "Payload Start",
1322 				 be32_to_cpu(attr->mask),
1323 				 be32_to_cpu(attr->val),
1324 				 le16_to_cpu(attr->reserved1));
1325 		}
1326 	}
1327 out:
1328 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1329 	kfree(buf);
1330 	return ret;
1331 }
1332 
1333 static ssize_t iwl_dbgfs_bcast_filters_write(struct iwl_mvm *mvm, char *buf,
1334 					     size_t count, loff_t *ppos)
1335 {
1336 	int pos, next_pos;
1337 	struct iwl_fw_bcast_filter filter = {};
1338 	struct iwl_bcast_filter_cmd cmd;
1339 	u32 filter_id, attr_id, mask, value;
1340 	int err = 0;
1341 
1342 	if (sscanf(buf, "%d %hhi %hhi %n", &filter_id, &filter.discard,
1343 		   &filter.frame_type, &pos) != 3)
1344 		return -EINVAL;
1345 
1346 	if (filter_id >= ARRAY_SIZE(mvm->dbgfs_bcast_filtering.cmd.filters) ||
1347 	    filter.frame_type > BCAST_FILTER_FRAME_TYPE_IPV4)
1348 		return -EINVAL;
1349 
1350 	for (attr_id = 0; attr_id < ARRAY_SIZE(filter.attrs);
1351 	     attr_id++) {
1352 		struct iwl_fw_bcast_filter_attr *attr =
1353 				&filter.attrs[attr_id];
1354 
1355 		if (pos >= count)
1356 			break;
1357 
1358 		if (sscanf(&buf[pos], "%hhi %hhi %i %i %n",
1359 			   &attr->offset, &attr->offset_type,
1360 			   &mask, &value, &next_pos) != 4)
1361 			return -EINVAL;
1362 
1363 		attr->mask = cpu_to_be32(mask);
1364 		attr->val = cpu_to_be32(value);
1365 		if (mask)
1366 			filter.num_attrs++;
1367 
1368 		pos += next_pos;
1369 	}
1370 
1371 	mutex_lock(&mvm->mutex);
1372 	memcpy(&mvm->dbgfs_bcast_filtering.cmd.filters[filter_id],
1373 	       &filter, sizeof(filter));
1374 
1375 	/* send updated bcast filtering configuration */
1376 	if (iwl_mvm_firmware_running(mvm) &&
1377 	    mvm->dbgfs_bcast_filtering.override &&
1378 	    iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
1379 		err = iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, 0,
1380 					   sizeof(cmd), &cmd);
1381 	mutex_unlock(&mvm->mutex);
1382 
1383 	return err ?: count;
1384 }
1385 
1386 static ssize_t iwl_dbgfs_bcast_filters_macs_read(struct file *file,
1387 						 char __user *user_buf,
1388 						 size_t count, loff_t *ppos)
1389 {
1390 	struct iwl_mvm *mvm = file->private_data;
1391 	struct iwl_bcast_filter_cmd cmd;
1392 	char *buf;
1393 	int bufsz = 1024;
1394 	int i, pos = 0;
1395 	ssize_t ret;
1396 
1397 	buf = kzalloc(bufsz, GFP_KERNEL);
1398 	if (!buf)
1399 		return -ENOMEM;
1400 
1401 	mutex_lock(&mvm->mutex);
1402 	if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd)) {
1403 		ADD_TEXT("None\n");
1404 		mutex_unlock(&mvm->mutex);
1405 		goto out;
1406 	}
1407 	mutex_unlock(&mvm->mutex);
1408 
1409 	for (i = 0; i < ARRAY_SIZE(cmd.macs); i++) {
1410 		const struct iwl_fw_bcast_mac *mac = &cmd.macs[i];
1411 
1412 		ADD_TEXT("Mac [%d]: discard=%d attached_filters=0x%x\n",
1413 			 i, mac->default_discard, mac->attached_filters);
1414 	}
1415 out:
1416 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1417 	kfree(buf);
1418 	return ret;
1419 }
1420 
1421 static ssize_t iwl_dbgfs_bcast_filters_macs_write(struct iwl_mvm *mvm,
1422 						  char *buf, size_t count,
1423 						  loff_t *ppos)
1424 {
1425 	struct iwl_bcast_filter_cmd cmd;
1426 	struct iwl_fw_bcast_mac mac = {};
1427 	u32 mac_id, attached_filters;
1428 	int err = 0;
1429 
1430 	if (!mvm->bcast_filters)
1431 		return -ENOENT;
1432 
1433 	if (sscanf(buf, "%d %hhi %i", &mac_id, &mac.default_discard,
1434 		   &attached_filters) != 3)
1435 		return -EINVAL;
1436 
1437 	if (mac_id >= ARRAY_SIZE(cmd.macs) ||
1438 	    mac.default_discard > 1 ||
1439 	    attached_filters >= BIT(ARRAY_SIZE(cmd.filters)))
1440 		return -EINVAL;
1441 
1442 	mac.attached_filters = cpu_to_le16(attached_filters);
1443 
1444 	mutex_lock(&mvm->mutex);
1445 	memcpy(&mvm->dbgfs_bcast_filtering.cmd.macs[mac_id],
1446 	       &mac, sizeof(mac));
1447 
1448 	/* send updated bcast filtering configuration */
1449 	if (iwl_mvm_firmware_running(mvm) &&
1450 	    mvm->dbgfs_bcast_filtering.override &&
1451 	    iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
1452 		err = iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, 0,
1453 					   sizeof(cmd), &cmd);
1454 	mutex_unlock(&mvm->mutex);
1455 
1456 	return err ?: count;
1457 }
1458 #endif
1459 
1460 #ifdef CONFIG_PM_SLEEP
1461 static ssize_t iwl_dbgfs_d3_sram_write(struct iwl_mvm *mvm, char *buf,
1462 				       size_t count, loff_t *ppos)
1463 {
1464 	int store;
1465 
1466 	if (sscanf(buf, "%d", &store) != 1)
1467 		return -EINVAL;
1468 
1469 	mvm->store_d3_resume_sram = store;
1470 
1471 	return count;
1472 }
1473 
1474 static ssize_t iwl_dbgfs_d3_sram_read(struct file *file, char __user *user_buf,
1475 				      size_t count, loff_t *ppos)
1476 {
1477 	struct iwl_mvm *mvm = file->private_data;
1478 	const struct fw_img *img;
1479 	int ofs, len, pos = 0;
1480 	size_t bufsz, ret;
1481 	char *buf;
1482 	u8 *ptr = mvm->d3_resume_sram;
1483 
1484 	img = &mvm->fw->img[IWL_UCODE_WOWLAN];
1485 	len = img->sec[IWL_UCODE_SECTION_DATA].len;
1486 
1487 	bufsz = len * 4 + 256;
1488 	buf = kzalloc(bufsz, GFP_KERNEL);
1489 	if (!buf)
1490 		return -ENOMEM;
1491 
1492 	pos += scnprintf(buf, bufsz, "D3 SRAM capture: %sabled\n",
1493 			 mvm->store_d3_resume_sram ? "en" : "dis");
1494 
1495 	if (ptr) {
1496 		for (ofs = 0; ofs < len; ofs += 16) {
1497 			pos += scnprintf(buf + pos, bufsz - pos,
1498 					 "0x%.4x %16ph\n", ofs, ptr + ofs);
1499 		}
1500 	} else {
1501 		pos += scnprintf(buf + pos, bufsz - pos,
1502 				 "(no data captured)\n");
1503 	}
1504 
1505 	ret = simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1506 
1507 	kfree(buf);
1508 
1509 	return ret;
1510 }
1511 #endif
1512 
1513 #define PRINT_MVM_REF(ref) do {						\
1514 	if (mvm->refs[ref])						\
1515 		pos += scnprintf(buf + pos, bufsz - pos,		\
1516 				 "\t(0x%lx): %d %s\n",			\
1517 				 BIT(ref), mvm->refs[ref], #ref);	\
1518 } while (0)
1519 
1520 static ssize_t iwl_dbgfs_d0i3_refs_read(struct file *file,
1521 					char __user *user_buf,
1522 					size_t count, loff_t *ppos)
1523 {
1524 	struct iwl_mvm *mvm = file->private_data;
1525 	int i, pos = 0;
1526 	char buf[256];
1527 	const size_t bufsz = sizeof(buf);
1528 	u32 refs = 0;
1529 
1530 	for (i = 0; i < IWL_MVM_REF_COUNT; i++)
1531 		if (mvm->refs[i])
1532 			refs |= BIT(i);
1533 
1534 	pos += scnprintf(buf + pos, bufsz - pos, "taken mvm refs: 0x%x\n",
1535 			 refs);
1536 
1537 	PRINT_MVM_REF(IWL_MVM_REF_UCODE_DOWN);
1538 	PRINT_MVM_REF(IWL_MVM_REF_SCAN);
1539 	PRINT_MVM_REF(IWL_MVM_REF_ROC);
1540 	PRINT_MVM_REF(IWL_MVM_REF_ROC_AUX);
1541 	PRINT_MVM_REF(IWL_MVM_REF_P2P_CLIENT);
1542 	PRINT_MVM_REF(IWL_MVM_REF_AP_IBSS);
1543 	PRINT_MVM_REF(IWL_MVM_REF_USER);
1544 	PRINT_MVM_REF(IWL_MVM_REF_TX);
1545 	PRINT_MVM_REF(IWL_MVM_REF_TX_AGG);
1546 	PRINT_MVM_REF(IWL_MVM_REF_ADD_IF);
1547 	PRINT_MVM_REF(IWL_MVM_REF_START_AP);
1548 	PRINT_MVM_REF(IWL_MVM_REF_BSS_CHANGED);
1549 	PRINT_MVM_REF(IWL_MVM_REF_PREPARE_TX);
1550 	PRINT_MVM_REF(IWL_MVM_REF_PROTECT_TDLS);
1551 	PRINT_MVM_REF(IWL_MVM_REF_CHECK_CTKILL);
1552 	PRINT_MVM_REF(IWL_MVM_REF_PRPH_READ);
1553 	PRINT_MVM_REF(IWL_MVM_REF_PRPH_WRITE);
1554 	PRINT_MVM_REF(IWL_MVM_REF_NMI);
1555 	PRINT_MVM_REF(IWL_MVM_REF_TM_CMD);
1556 	PRINT_MVM_REF(IWL_MVM_REF_EXIT_WORK);
1557 	PRINT_MVM_REF(IWL_MVM_REF_PROTECT_CSA);
1558 	PRINT_MVM_REF(IWL_MVM_REF_FW_DBG_COLLECT);
1559 	PRINT_MVM_REF(IWL_MVM_REF_INIT_UCODE);
1560 	PRINT_MVM_REF(IWL_MVM_REF_SENDING_CMD);
1561 	PRINT_MVM_REF(IWL_MVM_REF_RX);
1562 
1563 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1564 }
1565 
1566 static ssize_t iwl_dbgfs_d0i3_refs_write(struct iwl_mvm *mvm, char *buf,
1567 					 size_t count, loff_t *ppos)
1568 {
1569 	unsigned long value;
1570 	int ret;
1571 	bool taken;
1572 
1573 	ret = kstrtoul(buf, 10, &value);
1574 	if (ret < 0)
1575 		return ret;
1576 
1577 	mutex_lock(&mvm->mutex);
1578 
1579 	taken = mvm->refs[IWL_MVM_REF_USER];
1580 	if (value == 1 && !taken)
1581 		iwl_mvm_ref(mvm, IWL_MVM_REF_USER);
1582 	else if (value == 0 && taken)
1583 		iwl_mvm_unref(mvm, IWL_MVM_REF_USER);
1584 	else
1585 		ret = -EINVAL;
1586 
1587 	mutex_unlock(&mvm->mutex);
1588 
1589 	if (ret < 0)
1590 		return ret;
1591 	return count;
1592 }
1593 
1594 #define MVM_DEBUGFS_WRITE_FILE_OPS(name, bufsz) \
1595 	_MVM_DEBUGFS_WRITE_FILE_OPS(name, bufsz, struct iwl_mvm)
1596 #define MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz) \
1597 	_MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz, struct iwl_mvm)
1598 #define MVM_DEBUGFS_ADD_FILE_ALIAS(alias, name, parent, mode) do {	\
1599 		if (!debugfs_create_file(alias, mode, parent, mvm,	\
1600 					 &iwl_dbgfs_##name##_ops))	\
1601 			goto err;					\
1602 	} while (0)
1603 #define MVM_DEBUGFS_ADD_FILE(name, parent, mode) \
1604 	MVM_DEBUGFS_ADD_FILE_ALIAS(#name, name, parent, mode)
1605 
1606 static ssize_t
1607 iwl_dbgfs_prph_reg_read(struct file *file,
1608 			char __user *user_buf,
1609 			size_t count, loff_t *ppos)
1610 {
1611 	struct iwl_mvm *mvm = file->private_data;
1612 	int pos = 0;
1613 	char buf[32];
1614 	const size_t bufsz = sizeof(buf);
1615 	int ret;
1616 
1617 	if (!mvm->dbgfs_prph_reg_addr)
1618 		return -EINVAL;
1619 
1620 	ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PRPH_READ);
1621 	if (ret)
1622 		return ret;
1623 
1624 	pos += scnprintf(buf + pos, bufsz - pos, "Reg 0x%x: (0x%x)\n",
1625 		mvm->dbgfs_prph_reg_addr,
1626 		iwl_read_prph(mvm->trans, mvm->dbgfs_prph_reg_addr));
1627 
1628 	iwl_mvm_unref(mvm, IWL_MVM_REF_PRPH_READ);
1629 
1630 	return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
1631 }
1632 
1633 static ssize_t
1634 iwl_dbgfs_prph_reg_write(struct iwl_mvm *mvm, char *buf,
1635 			 size_t count, loff_t *ppos)
1636 {
1637 	u8 args;
1638 	u32 value;
1639 	int ret;
1640 
1641 	args = sscanf(buf, "%i %i", &mvm->dbgfs_prph_reg_addr, &value);
1642 	/* if we only want to set the reg address - nothing more to do */
1643 	if (args == 1)
1644 		goto out;
1645 
1646 	/* otherwise, make sure we have both address and value */
1647 	if (args != 2)
1648 		return -EINVAL;
1649 
1650 	ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PRPH_WRITE);
1651 	if (ret)
1652 		return ret;
1653 
1654 	iwl_write_prph(mvm->trans, mvm->dbgfs_prph_reg_addr, value);
1655 
1656 	iwl_mvm_unref(mvm, IWL_MVM_REF_PRPH_WRITE);
1657 out:
1658 	return count;
1659 }
1660 
1661 static ssize_t
1662 iwl_dbgfs_send_echo_cmd_write(struct iwl_mvm *mvm, char *buf,
1663 			      size_t count, loff_t *ppos)
1664 {
1665 	int ret;
1666 
1667 	if (!iwl_mvm_firmware_running(mvm))
1668 		return -EIO;
1669 
1670 	mutex_lock(&mvm->mutex);
1671 	ret = iwl_mvm_send_cmd_pdu(mvm, ECHO_CMD, 0, 0, NULL);
1672 	mutex_unlock(&mvm->mutex);
1673 
1674 	return ret ?: count;
1675 }
1676 
1677 MVM_DEBUGFS_READ_WRITE_FILE_OPS(prph_reg, 64);
1678 
1679 /* Device wide debugfs entries */
1680 MVM_DEBUGFS_READ_FILE_OPS(ctdp_budget);
1681 MVM_DEBUGFS_WRITE_FILE_OPS(stop_ctdp, 8);
1682 MVM_DEBUGFS_WRITE_FILE_OPS(force_ctkill, 8);
1683 MVM_DEBUGFS_WRITE_FILE_OPS(tx_flush, 16);
1684 MVM_DEBUGFS_WRITE_FILE_OPS(sta_drain, 8);
1685 MVM_DEBUGFS_WRITE_FILE_OPS(send_echo_cmd, 8);
1686 MVM_DEBUGFS_READ_WRITE_FILE_OPS(sram, 64);
1687 MVM_DEBUGFS_READ_WRITE_FILE_OPS(set_nic_temperature, 64);
1688 MVM_DEBUGFS_READ_FILE_OPS(nic_temp);
1689 MVM_DEBUGFS_READ_FILE_OPS(stations);
1690 MVM_DEBUGFS_READ_FILE_OPS(bt_notif);
1691 MVM_DEBUGFS_READ_FILE_OPS(bt_cmd);
1692 MVM_DEBUGFS_READ_WRITE_FILE_OPS(disable_power_off, 64);
1693 MVM_DEBUGFS_READ_FILE_OPS(fw_rx_stats);
1694 MVM_DEBUGFS_READ_FILE_OPS(drv_rx_stats);
1695 MVM_DEBUGFS_READ_FILE_OPS(fw_ver);
1696 MVM_DEBUGFS_WRITE_FILE_OPS(fw_restart, 10);
1697 MVM_DEBUGFS_WRITE_FILE_OPS(fw_nmi, 10);
1698 MVM_DEBUGFS_WRITE_FILE_OPS(bt_tx_prio, 10);
1699 MVM_DEBUGFS_WRITE_FILE_OPS(bt_force_ant, 10);
1700 MVM_DEBUGFS_READ_WRITE_FILE_OPS(scan_ant_rxchain, 8);
1701 MVM_DEBUGFS_READ_WRITE_FILE_OPS(d0i3_refs, 8);
1702 MVM_DEBUGFS_READ_WRITE_FILE_OPS(fw_dbg_conf, 8);
1703 MVM_DEBUGFS_WRITE_FILE_OPS(fw_dbg_collect, 64);
1704 MVM_DEBUGFS_WRITE_FILE_OPS(cont_recording, 8);
1705 MVM_DEBUGFS_WRITE_FILE_OPS(max_amsdu_len, 8);
1706 MVM_DEBUGFS_WRITE_FILE_OPS(indirection_tbl,
1707 			   (IWL_RSS_INDIRECTION_TABLE_SIZE * 2));
1708 MVM_DEBUGFS_WRITE_FILE_OPS(inject_packet, 512);
1709 
1710 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1711 MVM_DEBUGFS_READ_WRITE_FILE_OPS(bcast_filters, 256);
1712 MVM_DEBUGFS_READ_WRITE_FILE_OPS(bcast_filters_macs, 256);
1713 #endif
1714 
1715 #ifdef CONFIG_PM_SLEEP
1716 MVM_DEBUGFS_READ_WRITE_FILE_OPS(d3_sram, 8);
1717 #endif
1718 #ifdef CONFIG_ACPI
1719 MVM_DEBUGFS_READ_FILE_OPS(sar_geo_profile);
1720 #endif
1721 
1722 static ssize_t iwl_dbgfs_mem_read(struct file *file, char __user *user_buf,
1723 				  size_t count, loff_t *ppos)
1724 {
1725 	struct iwl_mvm *mvm = file->private_data;
1726 	struct iwl_dbg_mem_access_cmd cmd = {};
1727 	struct iwl_dbg_mem_access_rsp *rsp;
1728 	struct iwl_host_cmd hcmd = {
1729 		.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
1730 		.data = { &cmd, },
1731 		.len = { sizeof(cmd) },
1732 	};
1733 	size_t delta;
1734 	ssize_t ret, len;
1735 
1736 	if (!iwl_mvm_firmware_running(mvm))
1737 		return -EIO;
1738 
1739 	hcmd.id = iwl_cmd_id(*ppos >> 24 ? UMAC_RD_WR : LMAC_RD_WR,
1740 			     DEBUG_GROUP, 0);
1741 	cmd.op = cpu_to_le32(DEBUG_MEM_OP_READ);
1742 
1743 	/* Take care of alignment of both the position and the length */
1744 	delta = *ppos & 0x3;
1745 	cmd.addr = cpu_to_le32(*ppos - delta);
1746 	cmd.len = cpu_to_le32(min(ALIGN(count + delta, 4) / 4,
1747 				  (size_t)DEBUG_MEM_MAX_SIZE_DWORDS));
1748 
1749 	mutex_lock(&mvm->mutex);
1750 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1751 	mutex_unlock(&mvm->mutex);
1752 
1753 	if (ret < 0)
1754 		return ret;
1755 
1756 	rsp = (void *)hcmd.resp_pkt->data;
1757 	if (le32_to_cpu(rsp->status) != DEBUG_MEM_STATUS_SUCCESS) {
1758 		ret = -ENXIO;
1759 		goto out;
1760 	}
1761 
1762 	len = min((size_t)le32_to_cpu(rsp->len) << 2,
1763 		  iwl_rx_packet_payload_len(hcmd.resp_pkt) - sizeof(*rsp));
1764 	len = min(len - delta, count);
1765 	if (len < 0) {
1766 		ret = -EFAULT;
1767 		goto out;
1768 	}
1769 
1770 	ret = len - copy_to_user(user_buf, (void *)rsp->data + delta, len);
1771 	*ppos += ret;
1772 
1773 out:
1774 	iwl_free_resp(&hcmd);
1775 	return ret;
1776 }
1777 
1778 static ssize_t iwl_dbgfs_mem_write(struct file *file,
1779 				   const char __user *user_buf, size_t count,
1780 				   loff_t *ppos)
1781 {
1782 	struct iwl_mvm *mvm = file->private_data;
1783 	struct iwl_dbg_mem_access_cmd *cmd;
1784 	struct iwl_dbg_mem_access_rsp *rsp;
1785 	struct iwl_host_cmd hcmd = {};
1786 	size_t cmd_size;
1787 	size_t data_size;
1788 	u32 op, len;
1789 	ssize_t ret;
1790 
1791 	if (!iwl_mvm_firmware_running(mvm))
1792 		return -EIO;
1793 
1794 	hcmd.id = iwl_cmd_id(*ppos >> 24 ? UMAC_RD_WR : LMAC_RD_WR,
1795 			     DEBUG_GROUP, 0);
1796 
1797 	if (*ppos & 0x3 || count < 4) {
1798 		op = DEBUG_MEM_OP_WRITE_BYTES;
1799 		len = min(count, (size_t)(4 - (*ppos & 0x3)));
1800 		data_size = len;
1801 	} else {
1802 		op = DEBUG_MEM_OP_WRITE;
1803 		len = min(count >> 2, (size_t)DEBUG_MEM_MAX_SIZE_DWORDS);
1804 		data_size = len << 2;
1805 	}
1806 
1807 	cmd_size = sizeof(*cmd) + ALIGN(data_size, 4);
1808 	cmd = kzalloc(cmd_size, GFP_KERNEL);
1809 	if (!cmd)
1810 		return -ENOMEM;
1811 
1812 	cmd->op = cpu_to_le32(op);
1813 	cmd->len = cpu_to_le32(len);
1814 	cmd->addr = cpu_to_le32(*ppos);
1815 	if (copy_from_user((void *)cmd->data, user_buf, data_size)) {
1816 		kfree(cmd);
1817 		return -EFAULT;
1818 	}
1819 
1820 	hcmd.flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
1821 	hcmd.data[0] = (void *)cmd;
1822 	hcmd.len[0] = cmd_size;
1823 
1824 	mutex_lock(&mvm->mutex);
1825 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
1826 	mutex_unlock(&mvm->mutex);
1827 
1828 	kfree(cmd);
1829 
1830 	if (ret < 0)
1831 		return ret;
1832 
1833 	rsp = (void *)hcmd.resp_pkt->data;
1834 	if (rsp->status != DEBUG_MEM_STATUS_SUCCESS) {
1835 		ret = -ENXIO;
1836 		goto out;
1837 	}
1838 
1839 	ret = data_size;
1840 	*ppos += ret;
1841 
1842 out:
1843 	iwl_free_resp(&hcmd);
1844 	return ret;
1845 }
1846 
1847 static const struct file_operations iwl_dbgfs_mem_ops = {
1848 	.read = iwl_dbgfs_mem_read,
1849 	.write = iwl_dbgfs_mem_write,
1850 	.open = simple_open,
1851 	.llseek = default_llseek,
1852 };
1853 
1854 int iwl_mvm_dbgfs_register(struct iwl_mvm *mvm, struct dentry *dbgfs_dir)
1855 {
1856 	struct dentry *bcast_dir __maybe_unused;
1857 	char buf[100];
1858 
1859 	spin_lock_init(&mvm->drv_stats_lock);
1860 
1861 	mvm->debugfs_dir = dbgfs_dir;
1862 
1863 	MVM_DEBUGFS_ADD_FILE(tx_flush, mvm->debugfs_dir, S_IWUSR);
1864 	MVM_DEBUGFS_ADD_FILE(sta_drain, mvm->debugfs_dir, S_IWUSR);
1865 	MVM_DEBUGFS_ADD_FILE(sram, mvm->debugfs_dir, S_IWUSR | S_IRUSR);
1866 	MVM_DEBUGFS_ADD_FILE(set_nic_temperature, mvm->debugfs_dir,
1867 			     S_IWUSR | S_IRUSR);
1868 	MVM_DEBUGFS_ADD_FILE(nic_temp, dbgfs_dir, S_IRUSR);
1869 	MVM_DEBUGFS_ADD_FILE(ctdp_budget, dbgfs_dir, S_IRUSR);
1870 	MVM_DEBUGFS_ADD_FILE(stop_ctdp, dbgfs_dir, S_IWUSR);
1871 	MVM_DEBUGFS_ADD_FILE(force_ctkill, dbgfs_dir, S_IWUSR);
1872 	MVM_DEBUGFS_ADD_FILE(stations, dbgfs_dir, S_IRUSR);
1873 	MVM_DEBUGFS_ADD_FILE(bt_notif, dbgfs_dir, S_IRUSR);
1874 	MVM_DEBUGFS_ADD_FILE(bt_cmd, dbgfs_dir, S_IRUSR);
1875 	MVM_DEBUGFS_ADD_FILE(disable_power_off, mvm->debugfs_dir,
1876 			     S_IRUSR | S_IWUSR);
1877 	MVM_DEBUGFS_ADD_FILE(fw_ver, mvm->debugfs_dir, S_IRUSR);
1878 	MVM_DEBUGFS_ADD_FILE(fw_rx_stats, mvm->debugfs_dir, S_IRUSR);
1879 	MVM_DEBUGFS_ADD_FILE(drv_rx_stats, mvm->debugfs_dir, S_IRUSR);
1880 	MVM_DEBUGFS_ADD_FILE(fw_restart, mvm->debugfs_dir, S_IWUSR);
1881 	MVM_DEBUGFS_ADD_FILE(fw_nmi, mvm->debugfs_dir, S_IWUSR);
1882 	MVM_DEBUGFS_ADD_FILE(bt_tx_prio, mvm->debugfs_dir, S_IWUSR);
1883 	MVM_DEBUGFS_ADD_FILE(bt_force_ant, mvm->debugfs_dir, S_IWUSR);
1884 	MVM_DEBUGFS_ADD_FILE(scan_ant_rxchain, mvm->debugfs_dir,
1885 			     S_IWUSR | S_IRUSR);
1886 	MVM_DEBUGFS_ADD_FILE(prph_reg, mvm->debugfs_dir, S_IWUSR | S_IRUSR);
1887 	MVM_DEBUGFS_ADD_FILE(d0i3_refs, mvm->debugfs_dir, S_IRUSR | S_IWUSR);
1888 	MVM_DEBUGFS_ADD_FILE(fw_dbg_conf, mvm->debugfs_dir, S_IRUSR | S_IWUSR);
1889 	MVM_DEBUGFS_ADD_FILE(fw_dbg_collect, mvm->debugfs_dir, S_IWUSR);
1890 	MVM_DEBUGFS_ADD_FILE(max_amsdu_len, mvm->debugfs_dir, S_IWUSR);
1891 	MVM_DEBUGFS_ADD_FILE(send_echo_cmd, mvm->debugfs_dir, S_IWUSR);
1892 	MVM_DEBUGFS_ADD_FILE(cont_recording, mvm->debugfs_dir, S_IWUSR);
1893 	MVM_DEBUGFS_ADD_FILE(indirection_tbl, mvm->debugfs_dir, S_IWUSR);
1894 	MVM_DEBUGFS_ADD_FILE(inject_packet, mvm->debugfs_dir, S_IWUSR);
1895 #ifdef CONFIG_ACPI
1896 	MVM_DEBUGFS_ADD_FILE(sar_geo_profile, dbgfs_dir, S_IRUSR);
1897 #endif
1898 
1899 	if (!debugfs_create_bool("enable_scan_iteration_notif",
1900 				 S_IRUSR | S_IWUSR,
1901 				 mvm->debugfs_dir,
1902 				 &mvm->scan_iter_notif_enabled))
1903 		goto err;
1904 	if (!debugfs_create_bool("drop_bcn_ap_mode", S_IRUSR | S_IWUSR,
1905 				 mvm->debugfs_dir, &mvm->drop_bcn_ap_mode))
1906 		goto err;
1907 
1908 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1909 	if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BCAST_FILTERING) {
1910 		bcast_dir = debugfs_create_dir("bcast_filtering",
1911 					       mvm->debugfs_dir);
1912 		if (!bcast_dir)
1913 			goto err;
1914 
1915 		if (!debugfs_create_bool("override", S_IRUSR | S_IWUSR,
1916 				bcast_dir,
1917 				&mvm->dbgfs_bcast_filtering.override))
1918 			goto err;
1919 
1920 		MVM_DEBUGFS_ADD_FILE_ALIAS("filters", bcast_filters,
1921 					   bcast_dir, S_IWUSR | S_IRUSR);
1922 		MVM_DEBUGFS_ADD_FILE_ALIAS("macs", bcast_filters_macs,
1923 					   bcast_dir, S_IWUSR | S_IRUSR);
1924 	}
1925 #endif
1926 
1927 #ifdef CONFIG_PM_SLEEP
1928 	MVM_DEBUGFS_ADD_FILE(d3_sram, mvm->debugfs_dir, S_IRUSR | S_IWUSR);
1929 	MVM_DEBUGFS_ADD_FILE(d3_test, mvm->debugfs_dir, S_IRUSR);
1930 	if (!debugfs_create_bool("d3_wake_sysassert", S_IRUSR | S_IWUSR,
1931 				 mvm->debugfs_dir, &mvm->d3_wake_sysassert))
1932 		goto err;
1933 	if (!debugfs_create_u32("last_netdetect_scans", S_IRUSR,
1934 				mvm->debugfs_dir, &mvm->last_netdetect_scans))
1935 		goto err;
1936 #endif
1937 
1938 	if (!debugfs_create_u8("ps_disabled", S_IRUSR,
1939 			       mvm->debugfs_dir, &mvm->ps_disabled))
1940 		goto err;
1941 	if (!debugfs_create_blob("nvm_hw", S_IRUSR,
1942 				  mvm->debugfs_dir, &mvm->nvm_hw_blob))
1943 		goto err;
1944 	if (!debugfs_create_blob("nvm_sw", S_IRUSR,
1945 				  mvm->debugfs_dir, &mvm->nvm_sw_blob))
1946 		goto err;
1947 	if (!debugfs_create_blob("nvm_calib", S_IRUSR,
1948 				  mvm->debugfs_dir, &mvm->nvm_calib_blob))
1949 		goto err;
1950 	if (!debugfs_create_blob("nvm_prod", S_IRUSR,
1951 				  mvm->debugfs_dir, &mvm->nvm_prod_blob))
1952 		goto err;
1953 	if (!debugfs_create_blob("nvm_phy_sku", S_IRUSR,
1954 				 mvm->debugfs_dir, &mvm->nvm_phy_sku_blob))
1955 		goto err;
1956 
1957 	debugfs_create_file("mem", S_IRUSR | S_IWUSR, dbgfs_dir, mvm,
1958 			    &iwl_dbgfs_mem_ops);
1959 
1960 	/*
1961 	 * Create a symlink with mac80211. It will be removed when mac80211
1962 	 * exists (before the opmode exists which removes the target.)
1963 	 */
1964 	snprintf(buf, 100, "../../%pd2", dbgfs_dir->d_parent);
1965 	if (!debugfs_create_symlink("iwlwifi", mvm->hw->wiphy->debugfsdir, buf))
1966 		goto err;
1967 
1968 	return 0;
1969 err:
1970 	IWL_ERR(mvm, "Can't create the mvm debugfs directory\n");
1971 	return -ENOMEM;
1972 }
1973