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        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        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.rt_status = 0;
106 }
107 
108 void iwl_fw_error_dump(struct iwl_fw_runtime *fwrt);
109 int iwl_fw_dbg_collect_desc(struct iwl_fw_runtime *fwrt,
110 			    const struct iwl_fw_dump_desc *desc,
111 			    bool monitor_only, unsigned int delay);
112 int _iwl_fw_dbg_collect(struct iwl_fw_runtime *fwrt,
113 			enum iwl_fw_dbg_trigger trig,
114 			const char *str, size_t len,
115 			struct iwl_fw_dbg_trigger_tlv *trigger);
116 int iwl_fw_dbg_collect(struct iwl_fw_runtime *fwrt,
117 		       u32 id, const char *str, size_t len);
118 int iwl_fw_dbg_collect_trig(struct iwl_fw_runtime *fwrt,
119 			    struct iwl_fw_dbg_trigger_tlv *trigger,
120 			    const char *fmt, ...) __printf(3, 4);
121 int iwl_fw_start_dbg_conf(struct iwl_fw_runtime *fwrt, u8 id);
122 
123 #define iwl_fw_dbg_trigger_enabled(fw, id) ({			\
124 	void *__dbg_trigger = (fw)->dbg.trigger_tlv[(id)];	\
125 	unlikely(__dbg_trigger);				\
126 })
127 
128 static inline struct iwl_fw_dbg_trigger_tlv*
129 _iwl_fw_dbg_get_trigger(const struct iwl_fw *fw, enum iwl_fw_dbg_trigger id)
130 {
131 	return fw->dbg.trigger_tlv[id];
132 }
133 
134 #define iwl_fw_dbg_get_trigger(fw, id) ({			\
135 	BUILD_BUG_ON(!__builtin_constant_p(id));		\
136 	BUILD_BUG_ON((id) >= FW_DBG_TRIGGER_MAX);		\
137 	_iwl_fw_dbg_get_trigger((fw), (id));			\
138 })
139 
140 static inline bool
141 iwl_fw_dbg_trigger_vif_match(struct iwl_fw_dbg_trigger_tlv *trig,
142 			     struct wireless_dev *wdev)
143 {
144 	u32 trig_vif = le32_to_cpu(trig->vif_type);
145 
146 	return trig_vif == IWL_FW_DBG_CONF_VIF_ANY ||
147 	       wdev->iftype == trig_vif;
148 }
149 
150 static inline bool
151 iwl_fw_dbg_trigger_stop_conf_match(struct iwl_fw_runtime *fwrt,
152 				   struct iwl_fw_dbg_trigger_tlv *trig)
153 {
154 	return ((trig->mode & IWL_FW_DBG_TRIGGER_STOP) &&
155 		(fwrt->dump.conf == FW_DBG_INVALID ||
156 		(BIT(fwrt->dump.conf) & le32_to_cpu(trig->stop_conf_ids))));
157 }
158 
159 static inline bool
160 iwl_fw_dbg_no_trig_window(struct iwl_fw_runtime *fwrt, u32 id, u32 dis_ms)
161 {
162 	unsigned long wind_jiff = msecs_to_jiffies(dis_ms);
163 
164 	/* If this is the first event checked, jump to update start ts */
165 	if (fwrt->dump.non_collect_ts_start[id] &&
166 	    (time_after(fwrt->dump.non_collect_ts_start[id] + wind_jiff,
167 			jiffies)))
168 		return true;
169 
170 	fwrt->dump.non_collect_ts_start[id] = jiffies;
171 	return false;
172 }
173 
174 static inline bool
175 iwl_fw_dbg_trigger_check_stop(struct iwl_fw_runtime *fwrt,
176 			      struct wireless_dev *wdev,
177 			      struct iwl_fw_dbg_trigger_tlv *trig)
178 {
179 	if (wdev && !iwl_fw_dbg_trigger_vif_match(trig, wdev))
180 		return false;
181 
182 	if (iwl_fw_dbg_no_trig_window(fwrt, le32_to_cpu(trig->id),
183 				      le16_to_cpu(trig->trig_dis_ms))) {
184 		IWL_WARN(fwrt, "Trigger %d occurred while no-collect window.\n",
185 			 trig->id);
186 		return false;
187 	}
188 
189 	return iwl_fw_dbg_trigger_stop_conf_match(fwrt, trig);
190 }
191 
192 static inline struct iwl_fw_dbg_trigger_tlv*
193 _iwl_fw_dbg_trigger_on(struct iwl_fw_runtime *fwrt,
194 		       struct wireless_dev *wdev,
195 		       const enum iwl_fw_dbg_trigger id)
196 {
197 	struct iwl_fw_dbg_trigger_tlv *trig;
198 
199 	if (fwrt->trans->ini_valid)
200 		return NULL;
201 
202 	if (!iwl_fw_dbg_trigger_enabled(fwrt->fw, id))
203 		return NULL;
204 
205 	trig = _iwl_fw_dbg_get_trigger(fwrt->fw, id);
206 
207 	if (!iwl_fw_dbg_trigger_check_stop(fwrt, wdev, trig))
208 		return NULL;
209 
210 	return trig;
211 }
212 
213 #define iwl_fw_dbg_trigger_on(fwrt, wdev, id) ({		\
214 	BUILD_BUG_ON(!__builtin_constant_p(id));		\
215 	BUILD_BUG_ON((id) >= FW_DBG_TRIGGER_MAX);		\
216 	_iwl_fw_dbg_trigger_on((fwrt), (wdev), (id));		\
217 })
218 
219 static inline bool
220 _iwl_fw_ini_trigger_on(struct iwl_fw_runtime *fwrt,
221 		       const enum iwl_fw_dbg_trigger id)
222 {
223 	struct iwl_fw_ini_active_triggers *trig = &fwrt->dump.active_trigs[id];
224 	u32 ms;
225 
226 	if (!fwrt->trans->ini_valid)
227 		return false;
228 
229 	if (!trig || !trig->active)
230 		return false;
231 
232 	ms = le32_to_cpu(trig->conf->ignore_consec);
233 	if (ms)
234 		ms /= USEC_PER_MSEC;
235 
236 	if (iwl_fw_dbg_no_trig_window(fwrt, id, ms)) {
237 		IWL_WARN(fwrt, "Trigger %d fired in no-collect window\n", id);
238 		return false;
239 	}
240 
241 	return true;
242 }
243 
244 #define iwl_fw_ini_trigger_on(fwrt, wdev, id) ({		\
245 	BUILD_BUG_ON(!__builtin_constant_p(id));		\
246 	BUILD_BUG_ON((id) >= IWL_FW_TRIGGER_ID_NUM);		\
247 	_iwl_fw_ini_trigger_on((fwrt), (wdev), (id));		\
248 })
249 
250 static inline void
251 _iwl_fw_dbg_trigger_simple_stop(struct iwl_fw_runtime *fwrt,
252 				struct wireless_dev *wdev,
253 				struct iwl_fw_dbg_trigger_tlv *trigger)
254 {
255 	if (!trigger)
256 		return;
257 
258 	if (!iwl_fw_dbg_trigger_check_stop(fwrt, wdev, trigger))
259 		return;
260 
261 	iwl_fw_dbg_collect_trig(fwrt, trigger, NULL);
262 }
263 
264 #define iwl_fw_dbg_trigger_simple_stop(fwrt, wdev, trig)	\
265 	_iwl_fw_dbg_trigger_simple_stop((fwrt), (wdev),		\
266 					iwl_fw_dbg_get_trigger((fwrt)->fw,\
267 							       (trig)))
268 
269 static int iwl_fw_dbg_start_stop_hcmd(struct iwl_fw_runtime *fwrt, bool start)
270 {
271 	struct iwl_continuous_record_cmd cont_rec = {};
272 	struct iwl_host_cmd hcmd = {
273 		.id = LDBG_CONFIG_CMD,
274 		.flags = CMD_ASYNC,
275 		.data[0] = &cont_rec,
276 		.len[0] = sizeof(cont_rec),
277 	};
278 
279 	cont_rec.record_mode.enable_recording = start ?
280 		cpu_to_le16(START_DEBUG_RECORDING) :
281 		cpu_to_le16(STOP_DEBUG_RECORDING);
282 
283 	return iwl_trans_send_cmd(fwrt->trans, &hcmd);
284 }
285 
286 static inline void
287 _iwl_fw_dbg_stop_recording(struct iwl_trans *trans,
288 			   struct iwl_fw_dbg_params *params)
289 {
290 	if (trans->cfg->device_family == IWL_DEVICE_FAMILY_7000) {
291 		iwl_set_bits_prph(trans, MON_BUFF_SAMPLE_CTL, 0x100);
292 		return;
293 	}
294 
295 	if (params) {
296 		params->in_sample = iwl_read_prph(trans, DBGC_IN_SAMPLE);
297 		params->out_ctrl = iwl_read_prph(trans, DBGC_OUT_CTRL);
298 	}
299 
300 	iwl_write_prph(trans, DBGC_IN_SAMPLE, 0);
301 	udelay(100);
302 	iwl_write_prph(trans, DBGC_OUT_CTRL, 0);
303 #ifdef CONFIG_IWLWIFI_DEBUGFS
304 	trans->dbg_rec_on = false;
305 #endif
306 }
307 
308 static inline void
309 iwl_fw_dbg_stop_recording(struct iwl_fw_runtime *fwrt,
310 			  struct iwl_fw_dbg_params *params)
311 {
312 	if (fwrt->trans->cfg->device_family < IWL_DEVICE_FAMILY_22560)
313 		_iwl_fw_dbg_stop_recording(fwrt->trans, params);
314 	else
315 		iwl_fw_dbg_start_stop_hcmd(fwrt, false);
316 }
317 
318 static inline void
319 _iwl_fw_dbg_restart_recording(struct iwl_trans *trans,
320 			      struct iwl_fw_dbg_params *params)
321 {
322 	if (WARN_ON(!params))
323 		return;
324 
325 	if (trans->cfg->device_family == IWL_DEVICE_FAMILY_7000) {
326 		iwl_clear_bits_prph(trans, MON_BUFF_SAMPLE_CTL, 0x100);
327 		iwl_clear_bits_prph(trans, MON_BUFF_SAMPLE_CTL, 0x1);
328 		iwl_set_bits_prph(trans, MON_BUFF_SAMPLE_CTL, 0x1);
329 	} else {
330 		iwl_write_prph(trans, DBGC_IN_SAMPLE, params->in_sample);
331 		udelay(100);
332 		iwl_write_prph(trans, DBGC_OUT_CTRL, params->out_ctrl);
333 	}
334 }
335 
336 #ifdef CONFIG_IWLWIFI_DEBUGFS
337 static inline void iwl_fw_set_dbg_rec_on(struct iwl_fw_runtime *fwrt)
338 {
339 	if (fwrt->fw->dbg.dest_tlv && fwrt->cur_fw_img == IWL_UCODE_REGULAR)
340 		fwrt->trans->dbg_rec_on = true;
341 }
342 #endif
343 
344 static inline void
345 iwl_fw_dbg_restart_recording(struct iwl_fw_runtime *fwrt,
346 			     struct iwl_fw_dbg_params *params)
347 {
348 	if (fwrt->trans->cfg->device_family < IWL_DEVICE_FAMILY_22560)
349 		_iwl_fw_dbg_restart_recording(fwrt->trans, params);
350 	else
351 		iwl_fw_dbg_start_stop_hcmd(fwrt, true);
352 #ifdef CONFIG_IWLWIFI_DEBUGFS
353 	iwl_fw_set_dbg_rec_on(fwrt);
354 #endif
355 }
356 
357 static inline void iwl_fw_dump_conf_clear(struct iwl_fw_runtime *fwrt)
358 {
359 	fwrt->dump.conf = FW_DBG_INVALID;
360 }
361 
362 void iwl_fw_error_dump_wk(struct work_struct *work);
363 
364 static inline bool iwl_fw_dbg_type_on(struct iwl_fw_runtime *fwrt, u32 type)
365 {
366 	return (fwrt->fw->dbg.dump_mask & BIT(type) || fwrt->trans->ini_valid);
367 }
368 
369 static inline bool iwl_fw_dbg_is_d3_debug_enabled(struct iwl_fw_runtime *fwrt)
370 {
371 	return fw_has_capa(&fwrt->fw->ucode_capa,
372 			   IWL_UCODE_TLV_CAPA_D3_DEBUG) &&
373 		fwrt->trans->cfg->d3_debug_data_length &&
374 		iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_D3_DEBUG_DATA);
375 }
376 
377 static inline bool iwl_fw_dbg_is_paging_enabled(struct iwl_fw_runtime *fwrt)
378 {
379 	return iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_PAGING) &&
380 		!fwrt->trans->cfg->gen2 &&
381 		fwrt->fw->img[fwrt->cur_fw_img].paging_mem_size &&
382 		fwrt->fw_paging_db[0].fw_paging_block;
383 }
384 
385 void iwl_fw_dbg_read_d3_debug_data(struct iwl_fw_runtime *fwrt);
386 
387 static inline void iwl_fw_flush_dump(struct iwl_fw_runtime *fwrt)
388 {
389 	flush_delayed_work(&fwrt->dump.wk);
390 }
391 
392 static inline void iwl_fw_cancel_dump(struct iwl_fw_runtime *fwrt)
393 {
394 	cancel_delayed_work_sync(&fwrt->dump.wk);
395 }
396 
397 #ifdef CONFIG_IWLWIFI_DEBUGFS
398 static inline void iwl_fw_cancel_timestamp(struct iwl_fw_runtime *fwrt)
399 {
400 	fwrt->timestamp.delay = 0;
401 	cancel_delayed_work_sync(&fwrt->timestamp.wk);
402 }
403 
404 void iwl_fw_trigger_timestamp(struct iwl_fw_runtime *fwrt, u32 delay);
405 
406 static inline void iwl_fw_suspend_timestamp(struct iwl_fw_runtime *fwrt)
407 {
408 	cancel_delayed_work_sync(&fwrt->timestamp.wk);
409 }
410 
411 static inline void iwl_fw_resume_timestamp(struct iwl_fw_runtime *fwrt)
412 {
413 	if (!fwrt->timestamp.delay)
414 		return;
415 
416 	schedule_delayed_work(&fwrt->timestamp.wk,
417 			      round_jiffies_relative(fwrt->timestamp.delay));
418 }
419 
420 #else
421 
422 static inline void iwl_fw_cancel_timestamp(struct iwl_fw_runtime *fwrt) {}
423 
424 static inline void iwl_fw_trigger_timestamp(struct iwl_fw_runtime *fwrt,
425 					    u32 delay) {}
426 
427 static inline void iwl_fw_suspend_timestamp(struct iwl_fw_runtime *fwrt) {}
428 
429 static inline void iwl_fw_resume_timestamp(struct iwl_fw_runtime *fwrt) {}
430 
431 #endif /* CONFIG_IWLWIFI_DEBUGFS */
432 
433 void iwl_fw_assert_error_dump(struct iwl_fw_runtime *fwrt);
434 void iwl_fw_alive_error_dump(struct iwl_fw_runtime *fwrt);
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 #endif  /* __iwl_fw_dbg_h__ */
440