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