1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
2 /*
3  * Copyright (C) 2005-2014, 2018-2019 Intel Corporation
4  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5  * Copyright (C) 2015-2017 Intel Deutschland GmbH
6  */
7 #ifndef __iwl_fw_dbg_h__
8 #define __iwl_fw_dbg_h__
9 #include <linux/workqueue.h>
10 #include <net/cfg80211.h>
11 #include "runtime.h"
12 #include "iwl-prph.h"
13 #include "iwl-io.h"
14 #include "file.h"
15 #include "error-dump.h"
16 #include "api/commands.h"
17 #include "api/dbg-tlv.h"
18 #include "api/alive.h"
19 
20 /**
21  * struct iwl_fw_dump_desc - describes the dump
22  * @len: length of trig_desc->data
23  * @trig_desc: the description of the dump
24  */
25 struct iwl_fw_dump_desc {
26 	size_t len;
27 	/* must be last */
28 	struct iwl_fw_error_dump_trigger_desc trig_desc;
29 };
30 
31 /**
32  * struct iwl_fw_dbg_params - register values to restore
33  * @in_sample: DBGC_IN_SAMPLE value
34  * @out_ctrl: DBGC_OUT_CTRL value
35  */
36 struct iwl_fw_dbg_params {
37 	u32 in_sample;
38 	u32 out_ctrl;
39 };
40 
41 extern const struct iwl_fw_dump_desc iwl_dump_desc_assert;
42 
43 int iwl_fw_dbg_collect_desc(struct iwl_fw_runtime *fwrt,
44 			    const struct iwl_fw_dump_desc *desc,
45 			    bool monitor_only, unsigned int delay);
46 int iwl_fw_dbg_error_collect(struct iwl_fw_runtime *fwrt,
47 			     enum iwl_fw_dbg_trigger trig_type);
48 int iwl_fw_dbg_ini_collect(struct iwl_fw_runtime *fwrt,
49 			   struct iwl_fwrt_dump_data *dump_data);
50 int iwl_fw_dbg_collect(struct iwl_fw_runtime *fwrt,
51 		       enum iwl_fw_dbg_trigger trig, const char *str,
52 		       size_t len, struct iwl_fw_dbg_trigger_tlv *trigger);
53 int iwl_fw_dbg_collect_trig(struct iwl_fw_runtime *fwrt,
54 			    struct iwl_fw_dbg_trigger_tlv *trigger,
55 			    const char *fmt, ...) __printf(3, 4);
56 int iwl_fw_start_dbg_conf(struct iwl_fw_runtime *fwrt, u8 id);
57 
58 #define iwl_fw_dbg_trigger_enabled(fw, id) ({			\
59 	void *__dbg_trigger = (fw)->dbg.trigger_tlv[(id)];	\
60 	unlikely(__dbg_trigger);				\
61 })
62 
63 static inline struct iwl_fw_dbg_trigger_tlv*
64 _iwl_fw_dbg_get_trigger(const struct iwl_fw *fw, enum iwl_fw_dbg_trigger id)
65 {
66 	return fw->dbg.trigger_tlv[id];
67 }
68 
69 #define iwl_fw_dbg_get_trigger(fw, id) ({			\
70 	BUILD_BUG_ON(!__builtin_constant_p(id));		\
71 	BUILD_BUG_ON((id) >= FW_DBG_TRIGGER_MAX);		\
72 	_iwl_fw_dbg_get_trigger((fw), (id));			\
73 })
74 
75 static inline bool
76 iwl_fw_dbg_trigger_vif_match(struct iwl_fw_dbg_trigger_tlv *trig,
77 			     struct wireless_dev *wdev)
78 {
79 	u32 trig_vif = le32_to_cpu(trig->vif_type);
80 
81 	return trig_vif == IWL_FW_DBG_CONF_VIF_ANY ||
82 	       wdev->iftype == trig_vif;
83 }
84 
85 static inline bool
86 iwl_fw_dbg_trigger_stop_conf_match(struct iwl_fw_runtime *fwrt,
87 				   struct iwl_fw_dbg_trigger_tlv *trig)
88 {
89 	return ((trig->mode & IWL_FW_DBG_TRIGGER_STOP) &&
90 		(fwrt->dump.conf == FW_DBG_INVALID ||
91 		(BIT(fwrt->dump.conf) & le32_to_cpu(trig->stop_conf_ids))));
92 }
93 
94 static inline bool
95 iwl_fw_dbg_no_trig_window(struct iwl_fw_runtime *fwrt, u32 id, u32 dis_usec)
96 {
97 	unsigned long wind_jiff = usecs_to_jiffies(dis_usec);
98 
99 	/* If this is the first event checked, jump to update start ts */
100 	if (fwrt->dump.non_collect_ts_start[id] &&
101 	    (time_after(fwrt->dump.non_collect_ts_start[id] + wind_jiff,
102 			jiffies)))
103 		return true;
104 
105 	fwrt->dump.non_collect_ts_start[id] = jiffies;
106 	return false;
107 }
108 
109 static inline bool
110 iwl_fw_dbg_trigger_check_stop(struct iwl_fw_runtime *fwrt,
111 			      struct wireless_dev *wdev,
112 			      struct iwl_fw_dbg_trigger_tlv *trig)
113 {
114 	u32 usec = le16_to_cpu(trig->trig_dis_ms) * USEC_PER_MSEC;
115 
116 	if (wdev && !iwl_fw_dbg_trigger_vif_match(trig, wdev))
117 		return false;
118 
119 	if (iwl_fw_dbg_no_trig_window(fwrt, le32_to_cpu(trig->id), usec)) {
120 		IWL_WARN(fwrt, "Trigger %d occurred while no-collect window.\n",
121 			 trig->id);
122 		return false;
123 	}
124 
125 	return iwl_fw_dbg_trigger_stop_conf_match(fwrt, trig);
126 }
127 
128 static inline struct iwl_fw_dbg_trigger_tlv*
129 _iwl_fw_dbg_trigger_on(struct iwl_fw_runtime *fwrt,
130 		       struct wireless_dev *wdev,
131 		       const enum iwl_fw_dbg_trigger id)
132 {
133 	struct iwl_fw_dbg_trigger_tlv *trig;
134 
135 	if (iwl_trans_dbg_ini_valid(fwrt->trans))
136 		return NULL;
137 
138 	if (!iwl_fw_dbg_trigger_enabled(fwrt->fw, id))
139 		return NULL;
140 
141 	trig = _iwl_fw_dbg_get_trigger(fwrt->fw, id);
142 
143 	if (!iwl_fw_dbg_trigger_check_stop(fwrt, wdev, trig))
144 		return NULL;
145 
146 	return trig;
147 }
148 
149 #define iwl_fw_dbg_trigger_on(fwrt, wdev, id) ({		\
150 	BUILD_BUG_ON(!__builtin_constant_p(id));		\
151 	BUILD_BUG_ON((id) >= FW_DBG_TRIGGER_MAX);		\
152 	_iwl_fw_dbg_trigger_on((fwrt), (wdev), (id));		\
153 })
154 
155 static inline void
156 _iwl_fw_dbg_trigger_simple_stop(struct iwl_fw_runtime *fwrt,
157 				struct wireless_dev *wdev,
158 				struct iwl_fw_dbg_trigger_tlv *trigger)
159 {
160 	if (!trigger)
161 		return;
162 
163 	if (!iwl_fw_dbg_trigger_check_stop(fwrt, wdev, trigger))
164 		return;
165 
166 	iwl_fw_dbg_collect_trig(fwrt, trigger, NULL);
167 }
168 
169 #define iwl_fw_dbg_trigger_simple_stop(fwrt, wdev, trig)	\
170 	_iwl_fw_dbg_trigger_simple_stop((fwrt), (wdev),		\
171 					iwl_fw_dbg_get_trigger((fwrt)->fw,\
172 							       (trig)))
173 void iwl_fw_dbg_stop_restart_recording(struct iwl_fw_runtime *fwrt,
174 				       struct iwl_fw_dbg_params *params,
175 				       bool stop);
176 
177 #ifdef CONFIG_IWLWIFI_DEBUGFS
178 static inline void iwl_fw_set_dbg_rec_on(struct iwl_fw_runtime *fwrt)
179 {
180 	if (fwrt->cur_fw_img == IWL_UCODE_REGULAR &&
181 	    (fwrt->fw->dbg.dest_tlv ||
182 	     fwrt->trans->dbg.ini_dest != IWL_FW_INI_LOCATION_INVALID))
183 		fwrt->trans->dbg.rec_on = true;
184 }
185 #endif
186 
187 static inline void iwl_fw_dump_conf_clear(struct iwl_fw_runtime *fwrt)
188 {
189 	fwrt->dump.conf = FW_DBG_INVALID;
190 }
191 
192 void iwl_fw_error_dump_wk(struct work_struct *work);
193 
194 static inline bool iwl_fw_dbg_type_on(struct iwl_fw_runtime *fwrt, u32 type)
195 {
196 	return (fwrt->fw->dbg.dump_mask & BIT(type));
197 }
198 
199 static inline bool iwl_fw_dbg_is_d3_debug_enabled(struct iwl_fw_runtime *fwrt)
200 {
201 	return fw_has_capa(&fwrt->fw->ucode_capa,
202 			   IWL_UCODE_TLV_CAPA_D3_DEBUG) &&
203 		fwrt->trans->cfg->d3_debug_data_length && fwrt->ops &&
204 		fwrt->ops->d3_debug_enable &&
205 		fwrt->ops->d3_debug_enable(fwrt->ops_ctx) &&
206 		iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_D3_DEBUG_DATA);
207 }
208 
209 static inline bool iwl_fw_dbg_is_paging_enabled(struct iwl_fw_runtime *fwrt)
210 {
211 	return iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_PAGING) &&
212 		!fwrt->trans->trans_cfg->gen2 &&
213 		fwrt->cur_fw_img < IWL_UCODE_TYPE_MAX &&
214 		fwrt->fw->img[fwrt->cur_fw_img].paging_mem_size &&
215 		fwrt->fw_paging_db[0].fw_paging_block;
216 }
217 
218 void iwl_fw_dbg_read_d3_debug_data(struct iwl_fw_runtime *fwrt);
219 
220 static inline void iwl_fw_flush_dumps(struct iwl_fw_runtime *fwrt)
221 {
222 	int i;
223 
224 	iwl_dbg_tlv_del_timers(fwrt->trans);
225 	for (i = 0; i < IWL_FW_RUNTIME_DUMP_WK_NUM; i++)
226 		flush_delayed_work(&fwrt->dump.wks[i].wk);
227 }
228 
229 #ifdef CONFIG_IWLWIFI_DEBUGFS
230 static inline void iwl_fw_cancel_timestamp(struct iwl_fw_runtime *fwrt)
231 {
232 	fwrt->timestamp.delay = 0;
233 	cancel_delayed_work_sync(&fwrt->timestamp.wk);
234 }
235 
236 void iwl_fw_trigger_timestamp(struct iwl_fw_runtime *fwrt, u32 delay);
237 
238 static inline void iwl_fw_suspend_timestamp(struct iwl_fw_runtime *fwrt)
239 {
240 	cancel_delayed_work_sync(&fwrt->timestamp.wk);
241 }
242 
243 static inline void iwl_fw_resume_timestamp(struct iwl_fw_runtime *fwrt)
244 {
245 	if (!fwrt->timestamp.delay)
246 		return;
247 
248 	schedule_delayed_work(&fwrt->timestamp.wk,
249 			      round_jiffies_relative(fwrt->timestamp.delay));
250 }
251 
252 #else
253 
254 static inline void iwl_fw_cancel_timestamp(struct iwl_fw_runtime *fwrt) {}
255 
256 static inline void iwl_fw_trigger_timestamp(struct iwl_fw_runtime *fwrt,
257 					    u32 delay) {}
258 
259 static inline void iwl_fw_suspend_timestamp(struct iwl_fw_runtime *fwrt) {}
260 
261 static inline void iwl_fw_resume_timestamp(struct iwl_fw_runtime *fwrt) {}
262 
263 #endif /* CONFIG_IWLWIFI_DEBUGFS */
264 
265 void iwl_fw_dbg_stop_sync(struct iwl_fw_runtime *fwrt);
266 
267 static inline void iwl_fw_lmac1_set_alive_err_table(struct iwl_trans *trans,
268 						    u32 lmac_error_event_table)
269 {
270 	if (!(trans->dbg.error_event_table_tlv_status &
271 	      IWL_ERROR_EVENT_TABLE_LMAC1) ||
272 	    WARN_ON(trans->dbg.lmac_error_event_table[0] !=
273 		    lmac_error_event_table))
274 		trans->dbg.lmac_error_event_table[0] = lmac_error_event_table;
275 }
276 
277 static inline void iwl_fw_umac_set_alive_err_table(struct iwl_trans *trans,
278 						   u32 umac_error_event_table)
279 {
280 	if (!(trans->dbg.error_event_table_tlv_status &
281 	      IWL_ERROR_EVENT_TABLE_UMAC) ||
282 	    WARN_ON(trans->dbg.umac_error_event_table !=
283 		    umac_error_event_table))
284 		trans->dbg.umac_error_event_table = umac_error_event_table;
285 }
286 
287 static inline void iwl_fw_error_collect(struct iwl_fw_runtime *fwrt)
288 {
289 	enum iwl_fw_ini_time_point tp_id;
290 
291 	if (!iwl_trans_dbg_ini_valid(fwrt->trans)) {
292 		iwl_fw_dbg_collect_desc(fwrt, &iwl_dump_desc_assert, false, 0);
293 		return;
294 	}
295 
296 	if (fwrt->trans->dbg.hw_error) {
297 		tp_id = IWL_FW_INI_TIME_POINT_FW_HW_ERROR;
298 		fwrt->trans->dbg.hw_error = false;
299 	} else {
300 		tp_id = IWL_FW_INI_TIME_POINT_FW_ASSERT;
301 	}
302 
303 	iwl_dbg_tlv_time_point(fwrt, tp_id, NULL);
304 }
305 
306 void iwl_fw_error_print_fseq_regs(struct iwl_fw_runtime *fwrt);
307 
308 static inline void iwl_fwrt_update_fw_versions(struct iwl_fw_runtime *fwrt,
309 					       struct iwl_lmac_alive *lmac,
310 					       struct iwl_umac_alive *umac)
311 {
312 	if (lmac) {
313 		fwrt->dump.fw_ver.type = lmac->ver_type;
314 		fwrt->dump.fw_ver.subtype = lmac->ver_subtype;
315 		fwrt->dump.fw_ver.lmac_major = le32_to_cpu(lmac->ucode_major);
316 		fwrt->dump.fw_ver.lmac_minor = le32_to_cpu(lmac->ucode_minor);
317 	}
318 
319 	if (umac) {
320 		fwrt->dump.fw_ver.umac_major = le32_to_cpu(umac->umac_major);
321 		fwrt->dump.fw_ver.umac_minor = le32_to_cpu(umac->umac_minor);
322 	}
323 }
324 #endif  /* __iwl_fw_dbg_h__ */
325