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#include "api/alive.h"
77
78/**
79 * struct iwl_fw_dump_desc - describes the dump
80 * @len: length of trig_desc->data
81 * @trig_desc: the description of the dump
82 */
83struct iwl_fw_dump_desc {
84	size_t len;
85	/* must be last */
86	struct iwl_fw_error_dump_trigger_desc trig_desc;
87};
88
89/**
90 * struct iwl_fw_dbg_params - register values to restore
91 * @in_sample: DBGC_IN_SAMPLE value
92 * @out_ctrl: DBGC_OUT_CTRL value
93 */
94struct iwl_fw_dbg_params {
95	u32 in_sample;
96	u32 out_ctrl;
97};
98
99extern const struct iwl_fw_dump_desc iwl_dump_desc_assert;
100
101int iwl_fw_dbg_collect_desc(struct iwl_fw_runtime *fwrt,
102			    const struct iwl_fw_dump_desc *desc,
103			    bool monitor_only, unsigned int delay);
104int iwl_fw_dbg_error_collect(struct iwl_fw_runtime *fwrt,
105			     enum iwl_fw_dbg_trigger trig_type);
106int iwl_fw_dbg_ini_collect(struct iwl_fw_runtime *fwrt,
107			   struct iwl_fwrt_dump_data *dump_data);
108int iwl_fw_dbg_collect(struct iwl_fw_runtime *fwrt,
109		       enum iwl_fw_dbg_trigger trig, const char *str,
110		       size_t len, struct iwl_fw_dbg_trigger_tlv *trigger);
111int iwl_fw_dbg_collect_trig(struct iwl_fw_runtime *fwrt,
112			    struct iwl_fw_dbg_trigger_tlv *trigger,
113			    const char *fmt, ...) __printf(3, 4);
114int iwl_fw_start_dbg_conf(struct iwl_fw_runtime *fwrt, u8 id);
115
116#define iwl_fw_dbg_trigger_enabled(fw, id) ({			\
117	void *__dbg_trigger = (fw)->dbg.trigger_tlv[(id)];	\
118	unlikely(__dbg_trigger);				\
119})
120
121static inline struct iwl_fw_dbg_trigger_tlv*
122_iwl_fw_dbg_get_trigger(const struct iwl_fw *fw, enum iwl_fw_dbg_trigger id)
123{
124	return fw->dbg.trigger_tlv[id];
125}
126
127#define iwl_fw_dbg_get_trigger(fw, id) ({			\
128	BUILD_BUG_ON(!__builtin_constant_p(id));		\
129	BUILD_BUG_ON((id) >= FW_DBG_TRIGGER_MAX);		\
130	_iwl_fw_dbg_get_trigger((fw), (id));			\
131})
132
133static inline bool
134iwl_fw_dbg_trigger_vif_match(struct iwl_fw_dbg_trigger_tlv *trig,
135			     struct wireless_dev *wdev)
136{
137	u32 trig_vif = le32_to_cpu(trig->vif_type);
138
139	return trig_vif == IWL_FW_DBG_CONF_VIF_ANY ||
140	       wdev->iftype == trig_vif;
141}
142
143static inline bool
144iwl_fw_dbg_trigger_stop_conf_match(struct iwl_fw_runtime *fwrt,
145				   struct iwl_fw_dbg_trigger_tlv *trig)
146{
147	return ((trig->mode & IWL_FW_DBG_TRIGGER_STOP) &&
148		(fwrt->dump.conf == FW_DBG_INVALID ||
149		(BIT(fwrt->dump.conf) & le32_to_cpu(trig->stop_conf_ids))));
150}
151
152static inline bool
153iwl_fw_dbg_no_trig_window(struct iwl_fw_runtime *fwrt, u32 id, u32 dis_usec)
154{
155	unsigned long wind_jiff = usecs_to_jiffies(dis_usec);
156
157	/* If this is the first event checked, jump to update start ts */
158	if (fwrt->dump.non_collect_ts_start[id] &&
159	    (time_after(fwrt->dump.non_collect_ts_start[id] + wind_jiff,
160			jiffies)))
161		return true;
162
163	fwrt->dump.non_collect_ts_start[id] = jiffies;
164	return false;
165}
166
167static inline bool
168iwl_fw_dbg_trigger_check_stop(struct iwl_fw_runtime *fwrt,
169			      struct wireless_dev *wdev,
170			      struct iwl_fw_dbg_trigger_tlv *trig)
171{
172	u32 usec = le16_to_cpu(trig->trig_dis_ms) * USEC_PER_MSEC;
173
174	if (wdev && !iwl_fw_dbg_trigger_vif_match(trig, wdev))
175		return false;
176
177	if (iwl_fw_dbg_no_trig_window(fwrt, le32_to_cpu(trig->id), usec)) {
178		IWL_WARN(fwrt, "Trigger %d occurred while no-collect window.\n",
179			 trig->id);
180		return false;
181	}
182
183	return iwl_fw_dbg_trigger_stop_conf_match(fwrt, trig);
184}
185
186static inline struct iwl_fw_dbg_trigger_tlv*
187_iwl_fw_dbg_trigger_on(struct iwl_fw_runtime *fwrt,
188		       struct wireless_dev *wdev,
189		       const enum iwl_fw_dbg_trigger id)
190{
191	struct iwl_fw_dbg_trigger_tlv *trig;
192
193	if (iwl_trans_dbg_ini_valid(fwrt->trans))
194		return NULL;
195
196	if (!iwl_fw_dbg_trigger_enabled(fwrt->fw, id))
197		return NULL;
198
199	trig = _iwl_fw_dbg_get_trigger(fwrt->fw, id);
200
201	if (!iwl_fw_dbg_trigger_check_stop(fwrt, wdev, trig))
202		return NULL;
203
204	return trig;
205}
206
207#define iwl_fw_dbg_trigger_on(fwrt, wdev, id) ({		\
208	BUILD_BUG_ON(!__builtin_constant_p(id));		\
209	BUILD_BUG_ON((id) >= FW_DBG_TRIGGER_MAX);		\
210	_iwl_fw_dbg_trigger_on((fwrt), (wdev), (id));		\
211})
212
213static inline void
214_iwl_fw_dbg_trigger_simple_stop(struct iwl_fw_runtime *fwrt,
215				struct wireless_dev *wdev,
216				struct iwl_fw_dbg_trigger_tlv *trigger)
217{
218	if (!trigger)
219		return;
220
221	if (!iwl_fw_dbg_trigger_check_stop(fwrt, wdev, trigger))
222		return;
223
224	iwl_fw_dbg_collect_trig(fwrt, trigger, NULL);
225}
226
227#define iwl_fw_dbg_trigger_simple_stop(fwrt, wdev, trig)	\
228	_iwl_fw_dbg_trigger_simple_stop((fwrt), (wdev),		\
229					iwl_fw_dbg_get_trigger((fwrt)->fw,\
230							       (trig)))
231void iwl_fw_dbg_stop_restart_recording(struct iwl_fw_runtime *fwrt,
232				       struct iwl_fw_dbg_params *params,
233				       bool stop);
234
235#ifdef CONFIG_IWLWIFI_DEBUGFS
236static inline void iwl_fw_set_dbg_rec_on(struct iwl_fw_runtime *fwrt)
237{
238	if (fwrt->cur_fw_img == IWL_UCODE_REGULAR &&
239	    (fwrt->fw->dbg.dest_tlv ||
240	     fwrt->trans->dbg.ini_dest != IWL_FW_INI_LOCATION_INVALID))
241		fwrt->trans->dbg.rec_on = true;
242}
243#endif
244
245static inline void iwl_fw_dump_conf_clear(struct iwl_fw_runtime *fwrt)
246{
247	fwrt->dump.conf = FW_DBG_INVALID;
248}
249
250void iwl_fw_error_dump_wk(struct work_struct *work);
251
252static inline bool iwl_fw_dbg_type_on(struct iwl_fw_runtime *fwrt, u32 type)
253{
254	return (fwrt->fw->dbg.dump_mask & BIT(type));
255}
256
257static inline bool iwl_fw_dbg_is_d3_debug_enabled(struct iwl_fw_runtime *fwrt)
258{
259	return fw_has_capa(&fwrt->fw->ucode_capa,
260			   IWL_UCODE_TLV_CAPA_D3_DEBUG) &&
261		fwrt->trans->cfg->d3_debug_data_length && fwrt->ops &&
262		fwrt->ops->d3_debug_enable &&
263		fwrt->ops->d3_debug_enable(fwrt->ops_ctx) &&
264		iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_D3_DEBUG_DATA);
265}
266
267static inline bool iwl_fw_dbg_is_paging_enabled(struct iwl_fw_runtime *fwrt)
268{
269	return iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_PAGING) &&
270		!fwrt->trans->trans_cfg->gen2 &&
271		fwrt->cur_fw_img < IWL_UCODE_TYPE_MAX &&
272		fwrt->fw->img[fwrt->cur_fw_img].paging_mem_size &&
273		fwrt->fw_paging_db[0].fw_paging_block;
274}
275
276void iwl_fw_dbg_read_d3_debug_data(struct iwl_fw_runtime *fwrt);
277
278static inline void iwl_fw_flush_dumps(struct iwl_fw_runtime *fwrt)
279{
280	int i;
281
282	iwl_dbg_tlv_del_timers(fwrt->trans);
283	for (i = 0; i < IWL_FW_RUNTIME_DUMP_WK_NUM; i++)
284		flush_delayed_work(&fwrt->dump.wks[i].wk);
285}
286
287#ifdef CONFIG_IWLWIFI_DEBUGFS
288static inline void iwl_fw_cancel_timestamp(struct iwl_fw_runtime *fwrt)
289{
290	fwrt->timestamp.delay = 0;
291	cancel_delayed_work_sync(&fwrt->timestamp.wk);
292}
293
294void iwl_fw_trigger_timestamp(struct iwl_fw_runtime *fwrt, u32 delay);
295
296static inline void iwl_fw_suspend_timestamp(struct iwl_fw_runtime *fwrt)
297{
298	cancel_delayed_work_sync(&fwrt->timestamp.wk);
299}
300
301static inline void iwl_fw_resume_timestamp(struct iwl_fw_runtime *fwrt)
302{
303	if (!fwrt->timestamp.delay)
304		return;
305
306	schedule_delayed_work(&fwrt->timestamp.wk,
307			      round_jiffies_relative(fwrt->timestamp.delay));
308}
309
310#else
311
312static inline void iwl_fw_cancel_timestamp(struct iwl_fw_runtime *fwrt) {}
313
314static inline void iwl_fw_trigger_timestamp(struct iwl_fw_runtime *fwrt,
315					    u32 delay) {}
316
317static inline void iwl_fw_suspend_timestamp(struct iwl_fw_runtime *fwrt) {}
318
319static inline void iwl_fw_resume_timestamp(struct iwl_fw_runtime *fwrt) {}
320
321#endif /* CONFIG_IWLWIFI_DEBUGFS */
322
323void iwl_fw_dbg_stop_sync(struct iwl_fw_runtime *fwrt);
324
325static inline void iwl_fw_lmac1_set_alive_err_table(struct iwl_trans *trans,
326						    u32 lmac_error_event_table)
327{
328	if (!(trans->dbg.error_event_table_tlv_status &
329	      IWL_ERROR_EVENT_TABLE_LMAC1) ||
330	    WARN_ON(trans->dbg.lmac_error_event_table[0] !=
331		    lmac_error_event_table))
332		trans->dbg.lmac_error_event_table[0] = lmac_error_event_table;
333}
334
335static inline void iwl_fw_umac_set_alive_err_table(struct iwl_trans *trans,
336						   u32 umac_error_event_table)
337{
338	if (!(trans->dbg.error_event_table_tlv_status &
339	      IWL_ERROR_EVENT_TABLE_UMAC) ||
340	    WARN_ON(trans->dbg.umac_error_event_table !=
341		    umac_error_event_table))
342		trans->dbg.umac_error_event_table = umac_error_event_table;
343}
344
345static inline void iwl_fw_error_collect(struct iwl_fw_runtime *fwrt)
346{
347	enum iwl_fw_ini_time_point tp_id;
348
349	if (!iwl_trans_dbg_ini_valid(fwrt->trans)) {
350		iwl_fw_dbg_collect_desc(fwrt, &iwl_dump_desc_assert, false, 0);
351		return;
352	}
353
354	if (fwrt->trans->dbg.hw_error) {
355		tp_id = IWL_FW_INI_TIME_POINT_FW_HW_ERROR;
356		fwrt->trans->dbg.hw_error = false;
357	} else {
358		tp_id = IWL_FW_INI_TIME_POINT_FW_ASSERT;
359	}
360
361	iwl_dbg_tlv_time_point(fwrt, tp_id, NULL);
362}
363
364void iwl_fw_error_print_fseq_regs(struct iwl_fw_runtime *fwrt);
365
366static inline void iwl_fwrt_update_fw_versions(struct iwl_fw_runtime *fwrt,
367					       struct iwl_lmac_alive *lmac,
368					       struct iwl_umac_alive *umac)
369{
370	if (lmac) {
371		fwrt->dump.fw_ver.type = lmac->ver_type;
372		fwrt->dump.fw_ver.subtype = lmac->ver_subtype;
373		fwrt->dump.fw_ver.lmac_major = le32_to_cpu(lmac->ucode_major);
374		fwrt->dump.fw_ver.lmac_minor = le32_to_cpu(lmac->ucode_minor);
375	}
376
377	if (umac) {
378		fwrt->dump.fw_ver.umac_major = le32_to_cpu(umac->umac_major);
379		fwrt->dump.fw_ver.umac_minor = le32_to_cpu(umac->umac_minor);
380	}
381}
382#endif  /* __iwl_fw_dbg_h__ */
383