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) 2008 - 2014 Intel Corporation. All rights reserved.
9  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10  * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
11  * Copyright(c) 2018 - 2019 Intel Corporation
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of version 2 of the GNU General Public License as
15  * published by the Free Software Foundation.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * General Public License for more details.
21  *
22  * The full GNU General Public License is included in this distribution
23  * in the file called COPYING.
24  *
25  * Contact Information:
26  *  Intel Linux Wireless <linuxwifi@intel.com>
27  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28  *
29  * BSD LICENSE
30  *
31  * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
32  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
33  * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
34  * Copyright(c) 2018 - 2019 Intel Corporation
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  *
41  *  * Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  *  * Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in
45  *    the documentation and/or other materials provided with the
46  *    distribution.
47  *  * Neither the name Intel Corporation nor the names of its
48  *    contributors may be used to endorse or promote products derived
49  *    from this software without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  *
63  *****************************************************************************/
64 
65 #ifndef __iwl_fw_dbg_h__
66 #define __iwl_fw_dbg_h__
67 #include <linux/workqueue.h>
68 #include <net/cfg80211.h>
69 #include "runtime.h"
70 #include "iwl-prph.h"
71 #include "iwl-io.h"
72 #include "file.h"
73 #include "error-dump.h"
74 #include "api/commands.h"
75 #include "api/dbg-tlv.h"
76 
77 /**
78  * struct iwl_fw_dump_desc - describes the dump
79  * @len: length of trig_desc->data
80  * @trig_desc: the description of the dump
81  */
82 struct iwl_fw_dump_desc {
83 	size_t len;
84 	/* must be last */
85 	struct iwl_fw_error_dump_trigger_desc trig_desc;
86 };
87 
88 /**
89  * struct iwl_fw_dbg_params - register values to restore
90  * @in_sample: DBGC_IN_SAMPLE value
91  * @out_ctrl: DBGC_OUT_CTRL value
92  */
93 struct iwl_fw_dbg_params {
94 	u32 in_sample;
95 	u32 out_ctrl;
96 };
97 
98 extern const struct iwl_fw_dump_desc iwl_dump_desc_assert;
99 
100 static inline void iwl_fw_free_dump_desc(struct iwl_fw_runtime *fwrt)
101 {
102 	if (fwrt->dump.desc != &iwl_dump_desc_assert)
103 		kfree(fwrt->dump.desc);
104 	fwrt->dump.desc = NULL;
105 	fwrt->dump.lmac_err_id[0] = 0;
106 	if (fwrt->smem_cfg.num_lmacs > 1)
107 		fwrt->dump.lmac_err_id[1] = 0;
108 	fwrt->dump.umac_err_id = 0;
109 }
110 
111 void iwl_fw_error_dump(struct iwl_fw_runtime *fwrt);
112 int iwl_fw_dbg_collect_desc(struct iwl_fw_runtime *fwrt,
113 			    const struct iwl_fw_dump_desc *desc,
114 			    bool monitor_only, unsigned int delay);
115 int iwl_fw_dbg_error_collect(struct iwl_fw_runtime *fwrt,
116 			     enum iwl_fw_dbg_trigger trig_type);
117 int _iwl_fw_dbg_collect(struct iwl_fw_runtime *fwrt,
118 			enum iwl_fw_dbg_trigger trig,
119 			const char *str, size_t len,
120 			struct iwl_fw_dbg_trigger_tlv *trigger);
121 int iwl_fw_dbg_collect(struct iwl_fw_runtime *fwrt,
122 		       u32 id, const char *str, size_t len);
123 int iwl_fw_dbg_collect_trig(struct iwl_fw_runtime *fwrt,
124 			    struct iwl_fw_dbg_trigger_tlv *trigger,
125 			    const char *fmt, ...) __printf(3, 4);
126 int iwl_fw_start_dbg_conf(struct iwl_fw_runtime *fwrt, u8 id);
127 
128 #define iwl_fw_dbg_trigger_enabled(fw, id) ({			\
129 	void *__dbg_trigger = (fw)->dbg.trigger_tlv[(id)];	\
130 	unlikely(__dbg_trigger);				\
131 })
132 
133 static inline struct iwl_fw_dbg_trigger_tlv*
134 _iwl_fw_dbg_get_trigger(const struct iwl_fw *fw, enum iwl_fw_dbg_trigger id)
135 {
136 	return fw->dbg.trigger_tlv[id];
137 }
138 
139 #define iwl_fw_dbg_get_trigger(fw, id) ({			\
140 	BUILD_BUG_ON(!__builtin_constant_p(id));		\
141 	BUILD_BUG_ON((id) >= FW_DBG_TRIGGER_MAX);		\
142 	_iwl_fw_dbg_get_trigger((fw), (id));			\
143 })
144 
145 static inline bool
146 iwl_fw_dbg_trigger_vif_match(struct iwl_fw_dbg_trigger_tlv *trig,
147 			     struct wireless_dev *wdev)
148 {
149 	u32 trig_vif = le32_to_cpu(trig->vif_type);
150 
151 	return trig_vif == IWL_FW_DBG_CONF_VIF_ANY ||
152 	       wdev->iftype == trig_vif;
153 }
154 
155 static inline bool
156 iwl_fw_dbg_trigger_stop_conf_match(struct iwl_fw_runtime *fwrt,
157 				   struct iwl_fw_dbg_trigger_tlv *trig)
158 {
159 	return ((trig->mode & IWL_FW_DBG_TRIGGER_STOP) &&
160 		(fwrt->dump.conf == FW_DBG_INVALID ||
161 		(BIT(fwrt->dump.conf) & le32_to_cpu(trig->stop_conf_ids))));
162 }
163 
164 static inline bool
165 iwl_fw_dbg_no_trig_window(struct iwl_fw_runtime *fwrt, u32 id, u32 dis_usec)
166 {
167 	unsigned long wind_jiff = usecs_to_jiffies(dis_usec);
168 
169 	/* If this is the first event checked, jump to update start ts */
170 	if (fwrt->dump.non_collect_ts_start[id] &&
171 	    (time_after(fwrt->dump.non_collect_ts_start[id] + wind_jiff,
172 			jiffies)))
173 		return true;
174 
175 	fwrt->dump.non_collect_ts_start[id] = jiffies;
176 	return false;
177 }
178 
179 static inline bool
180 iwl_fw_dbg_trigger_check_stop(struct iwl_fw_runtime *fwrt,
181 			      struct wireless_dev *wdev,
182 			      struct iwl_fw_dbg_trigger_tlv *trig)
183 {
184 	u32 usec = le16_to_cpu(trig->trig_dis_ms) * USEC_PER_MSEC;
185 
186 	if (wdev && !iwl_fw_dbg_trigger_vif_match(trig, wdev))
187 		return false;
188 
189 	if (iwl_fw_dbg_no_trig_window(fwrt, le32_to_cpu(trig->id), usec)) {
190 		IWL_WARN(fwrt, "Trigger %d occurred while no-collect window.\n",
191 			 trig->id);
192 		return false;
193 	}
194 
195 	return iwl_fw_dbg_trigger_stop_conf_match(fwrt, trig);
196 }
197 
198 static inline struct iwl_fw_dbg_trigger_tlv*
199 _iwl_fw_dbg_trigger_on(struct iwl_fw_runtime *fwrt,
200 		       struct wireless_dev *wdev,
201 		       const enum iwl_fw_dbg_trigger id)
202 {
203 	struct iwl_fw_dbg_trigger_tlv *trig;
204 
205 	if (fwrt->trans->ini_valid)
206 		return NULL;
207 
208 	if (!iwl_fw_dbg_trigger_enabled(fwrt->fw, id))
209 		return NULL;
210 
211 	trig = _iwl_fw_dbg_get_trigger(fwrt->fw, id);
212 
213 	if (!iwl_fw_dbg_trigger_check_stop(fwrt, wdev, trig))
214 		return NULL;
215 
216 	return trig;
217 }
218 
219 #define iwl_fw_dbg_trigger_on(fwrt, wdev, id) ({		\
220 	BUILD_BUG_ON(!__builtin_constant_p(id));		\
221 	BUILD_BUG_ON((id) >= FW_DBG_TRIGGER_MAX);		\
222 	_iwl_fw_dbg_trigger_on((fwrt), (wdev), (id));		\
223 })
224 
225 static inline bool
226 iwl_fw_ini_trigger_on(struct iwl_fw_runtime *fwrt,
227 		      enum iwl_fw_ini_trigger_id id)
228 {
229 	struct iwl_fw_ini_trigger *trig;
230 	u32 usec;
231 
232 
233 
234 	if (!fwrt->trans->ini_valid || id >= IWL_FW_TRIGGER_ID_NUM ||
235 	    !fwrt->dump.active_trigs[id].active)
236 		return false;
237 
238 	trig = fwrt->dump.active_trigs[id].trig;
239 	usec = le32_to_cpu(trig->ignore_consec);
240 
241 	if (iwl_fw_dbg_no_trig_window(fwrt, id, usec)) {
242 		IWL_WARN(fwrt, "Trigger %d fired in no-collect window\n", id);
243 		return false;
244 	}
245 
246 	return true;
247 }
248 
249 static inline void
250 _iwl_fw_dbg_trigger_simple_stop(struct iwl_fw_runtime *fwrt,
251 				struct wireless_dev *wdev,
252 				struct iwl_fw_dbg_trigger_tlv *trigger)
253 {
254 	if (!trigger)
255 		return;
256 
257 	if (!iwl_fw_dbg_trigger_check_stop(fwrt, wdev, trigger))
258 		return;
259 
260 	iwl_fw_dbg_collect_trig(fwrt, trigger, NULL);
261 }
262 
263 #define iwl_fw_dbg_trigger_simple_stop(fwrt, wdev, trig)	\
264 	_iwl_fw_dbg_trigger_simple_stop((fwrt), (wdev),		\
265 					iwl_fw_dbg_get_trigger((fwrt)->fw,\
266 							       (trig)))
267 
268 static inline int
269 iwl_fw_dbg_start_stop_hcmd(struct iwl_fw_runtime *fwrt, bool start)
270 {
271 	struct iwl_ldbg_config_cmd cmd = {
272 		.type = start ? cpu_to_le32(START_DEBUG_RECORDING) :
273 				cpu_to_le32(STOP_DEBUG_RECORDING),
274 	};
275 	struct iwl_host_cmd hcmd = {
276 		.id = LDBG_CONFIG_CMD,
277 		.flags = CMD_ASYNC,
278 		.data[0] = &cmd,
279 		.len[0] = sizeof(cmd),
280 	};
281 
282 	return iwl_trans_send_cmd(fwrt->trans, &hcmd);
283 }
284 
285 static inline void
286 _iwl_fw_dbg_stop_recording(struct iwl_trans *trans,
287 			   struct iwl_fw_dbg_params *params)
288 {
289 	if (trans->cfg->device_family == IWL_DEVICE_FAMILY_7000) {
290 		iwl_set_bits_prph(trans, MON_BUFF_SAMPLE_CTL, 0x100);
291 		return;
292 	}
293 
294 	if (params) {
295 		params->in_sample = iwl_read_umac_prph(trans, DBGC_IN_SAMPLE);
296 		params->out_ctrl = iwl_read_umac_prph(trans, DBGC_OUT_CTRL);
297 	}
298 
299 	iwl_write_umac_prph(trans, DBGC_IN_SAMPLE, 0);
300 	udelay(100);
301 	iwl_write_umac_prph(trans, DBGC_OUT_CTRL, 0);
302 #ifdef CONFIG_IWLWIFI_DEBUGFS
303 	trans->dbg_rec_on = false;
304 #endif
305 }
306 
307 static inline void
308 iwl_fw_dbg_stop_recording(struct iwl_fw_runtime *fwrt,
309 			  struct iwl_fw_dbg_params *params)
310 {
311 	if (fwrt->trans->cfg->device_family < IWL_DEVICE_FAMILY_22560)
312 		_iwl_fw_dbg_stop_recording(fwrt->trans, params);
313 	else
314 		iwl_fw_dbg_start_stop_hcmd(fwrt, false);
315 }
316 
317 static inline void
318 _iwl_fw_dbg_restart_recording(struct iwl_trans *trans,
319 			      struct iwl_fw_dbg_params *params)
320 {
321 	if (WARN_ON(!params))
322 		return;
323 
324 	if (trans->cfg->device_family == IWL_DEVICE_FAMILY_7000) {
325 		iwl_clear_bits_prph(trans, MON_BUFF_SAMPLE_CTL, 0x100);
326 		iwl_clear_bits_prph(trans, MON_BUFF_SAMPLE_CTL, 0x1);
327 		iwl_set_bits_prph(trans, MON_BUFF_SAMPLE_CTL, 0x1);
328 	} else {
329 		iwl_write_umac_prph(trans, DBGC_IN_SAMPLE, params->in_sample);
330 		udelay(100);
331 		iwl_write_umac_prph(trans, DBGC_OUT_CTRL, params->out_ctrl);
332 	}
333 }
334 
335 #ifdef CONFIG_IWLWIFI_DEBUGFS
336 static inline void iwl_fw_set_dbg_rec_on(struct iwl_fw_runtime *fwrt)
337 {
338 	if (fwrt->fw->dbg.dest_tlv && fwrt->cur_fw_img == IWL_UCODE_REGULAR)
339 		fwrt->trans->dbg_rec_on = true;
340 }
341 #endif
342 
343 static inline void
344 iwl_fw_dbg_restart_recording(struct iwl_fw_runtime *fwrt,
345 			     struct iwl_fw_dbg_params *params)
346 {
347 	if (fwrt->trans->cfg->device_family < IWL_DEVICE_FAMILY_22560)
348 		_iwl_fw_dbg_restart_recording(fwrt->trans, params);
349 	else
350 		iwl_fw_dbg_start_stop_hcmd(fwrt, true);
351 #ifdef CONFIG_IWLWIFI_DEBUGFS
352 	iwl_fw_set_dbg_rec_on(fwrt);
353 #endif
354 }
355 
356 static inline void iwl_fw_dump_conf_clear(struct iwl_fw_runtime *fwrt)
357 {
358 	fwrt->dump.conf = FW_DBG_INVALID;
359 }
360 
361 void iwl_fw_error_dump_wk(struct work_struct *work);
362 
363 static inline bool iwl_fw_dbg_type_on(struct iwl_fw_runtime *fwrt, u32 type)
364 {
365 	return (fwrt->fw->dbg.dump_mask & BIT(type) || fwrt->trans->ini_valid);
366 }
367 
368 static inline bool iwl_fw_dbg_is_d3_debug_enabled(struct iwl_fw_runtime *fwrt)
369 {
370 	return fw_has_capa(&fwrt->fw->ucode_capa,
371 			   IWL_UCODE_TLV_CAPA_D3_DEBUG) &&
372 		fwrt->trans->cfg->d3_debug_data_length && fwrt->ops &&
373 		fwrt->ops->d3_debug_enable &&
374 		fwrt->ops->d3_debug_enable(fwrt->ops_ctx) &&
375 		iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_D3_DEBUG_DATA);
376 }
377 
378 static inline bool iwl_fw_dbg_is_paging_enabled(struct iwl_fw_runtime *fwrt)
379 {
380 	return iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_PAGING) &&
381 		!fwrt->trans->cfg->gen2 &&
382 		fwrt->cur_fw_img < IWL_UCODE_TYPE_MAX &&
383 		fwrt->fw->img[fwrt->cur_fw_img].paging_mem_size &&
384 		fwrt->fw_paging_db[0].fw_paging_block;
385 }
386 
387 void iwl_fw_dbg_read_d3_debug_data(struct iwl_fw_runtime *fwrt);
388 
389 static inline void iwl_fw_flush_dump(struct iwl_fw_runtime *fwrt)
390 {
391 	flush_delayed_work(&fwrt->dump.wk);
392 }
393 
394 static inline void iwl_fw_cancel_dump(struct iwl_fw_runtime *fwrt)
395 {
396 	cancel_delayed_work_sync(&fwrt->dump.wk);
397 }
398 
399 #ifdef CONFIG_IWLWIFI_DEBUGFS
400 static inline void iwl_fw_cancel_timestamp(struct iwl_fw_runtime *fwrt)
401 {
402 	fwrt->timestamp.delay = 0;
403 	cancel_delayed_work_sync(&fwrt->timestamp.wk);
404 }
405 
406 void iwl_fw_trigger_timestamp(struct iwl_fw_runtime *fwrt, u32 delay);
407 
408 static inline void iwl_fw_suspend_timestamp(struct iwl_fw_runtime *fwrt)
409 {
410 	cancel_delayed_work_sync(&fwrt->timestamp.wk);
411 }
412 
413 static inline void iwl_fw_resume_timestamp(struct iwl_fw_runtime *fwrt)
414 {
415 	if (!fwrt->timestamp.delay)
416 		return;
417 
418 	schedule_delayed_work(&fwrt->timestamp.wk,
419 			      round_jiffies_relative(fwrt->timestamp.delay));
420 }
421 
422 #else
423 
424 static inline void iwl_fw_cancel_timestamp(struct iwl_fw_runtime *fwrt) {}
425 
426 static inline void iwl_fw_trigger_timestamp(struct iwl_fw_runtime *fwrt,
427 					    u32 delay) {}
428 
429 static inline void iwl_fw_suspend_timestamp(struct iwl_fw_runtime *fwrt) {}
430 
431 static inline void iwl_fw_resume_timestamp(struct iwl_fw_runtime *fwrt) {}
432 
433 #endif /* CONFIG_IWLWIFI_DEBUGFS */
434 
435 void iwl_fw_dbg_collect_sync(struct iwl_fw_runtime *fwrt);
436 void iwl_fw_dbg_apply_point(struct iwl_fw_runtime *fwrt,
437 			    enum iwl_fw_ini_apply_point apply_point);
438 
439 void iwl_fwrt_stop_device(struct iwl_fw_runtime *fwrt);
440 
441 static inline void iwl_fw_lmac1_set_alive_err_table(struct iwl_trans *trans,
442 						    u32 lmac_error_event_table)
443 {
444 	if (!(trans->error_event_table_tlv_status &
445 	      IWL_ERROR_EVENT_TABLE_LMAC1) ||
446 	    WARN_ON(trans->lmac_error_event_table[0] !=
447 		    lmac_error_event_table))
448 		trans->lmac_error_event_table[0] = lmac_error_event_table;
449 }
450 
451 static inline void iwl_fw_umac_set_alive_err_table(struct iwl_trans *trans,
452 						   u32 umac_error_event_table)
453 {
454 	if (!(trans->error_event_table_tlv_status &
455 	      IWL_ERROR_EVENT_TABLE_UMAC) ||
456 	    WARN_ON(trans->umac_error_event_table !=
457 		    umac_error_event_table))
458 		trans->umac_error_event_table = umac_error_event_table;
459 }
460 
461 /* This bit is used to differentiate the legacy dump from the ini dump */
462 #define INI_DUMP_BIT BIT(31)
463 
464 #endif  /* __iwl_fw_dbg_h__ */
465