]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/wireless/intel/iwlwifi/fw/dbg.h
iwlwifi: fw: stop and start debugging using host command
[linux.git] / drivers / net / wireless / intel / iwlwifi / fw / dbg.h
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
76 /**
77  * struct iwl_fw_dump_desc - describes the dump
78  * @len: length of trig_desc->data
79  * @trig_desc: the description of the dump
80  */
81 struct iwl_fw_dump_desc {
82         size_t len;
83         /* must be last */
84         struct iwl_fw_error_dump_trigger_desc trig_desc;
85 };
86
87 /**
88  * struct iwl_fw_dbg_params - register values to restore
89  * @in_sample: DBGC_IN_SAMPLE value
90  * @out_ctrl: DBGC_OUT_CTRL value
91  */
92 struct iwl_fw_dbg_params {
93         u32 in_sample;
94         u32 out_ctrl;
95 };
96
97 extern const struct iwl_fw_dump_desc iwl_dump_desc_assert;
98
99 static inline void iwl_fw_free_dump_desc(struct iwl_fw_runtime *fwrt)
100 {
101         if (fwrt->dump.desc != &iwl_dump_desc_assert)
102                 kfree(fwrt->dump.desc);
103         fwrt->dump.desc = NULL;
104         fwrt->dump.trig = 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                             const struct iwl_fw_dbg_trigger_tlv *trigger);
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                        const 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,
158                           struct iwl_fw_dbg_trigger_tlv *trig)
159 {
160         unsigned long wind_jiff =
161                 msecs_to_jiffies(le16_to_cpu(trig->trig_dis_ms));
162         u32 id = le32_to_cpu(trig->id);
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, trig)) {
183                 IWL_WARN(fwrt, "Trigger %d occurred while no-collect window.\n",
184                          trig->id);
185                 return false;
186         }
187
188         return iwl_fw_dbg_trigger_stop_conf_match(fwrt, trig);
189 }
190
191 static inline void
192 _iwl_fw_dbg_trigger_simple_stop(struct iwl_fw_runtime *fwrt,
193                                 struct wireless_dev *wdev,
194                                 struct iwl_fw_dbg_trigger_tlv *trigger)
195 {
196         if (!trigger)
197                 return;
198
199         if (!iwl_fw_dbg_trigger_check_stop(fwrt, wdev, trigger))
200                 return;
201
202         iwl_fw_dbg_collect_trig(fwrt, trigger, NULL);
203 }
204
205 #define iwl_fw_dbg_trigger_simple_stop(fwrt, wdev, trig)        \
206         _iwl_fw_dbg_trigger_simple_stop((fwrt), (wdev),         \
207                                         iwl_fw_dbg_get_trigger((fwrt)->fw,\
208                                                                (trig)))
209
210 static int iwl_fw_dbg_start_stop_hcmd(struct iwl_fw_runtime *fwrt, bool start)
211 {
212         struct iwl_continuous_record_cmd cont_rec = {};
213         struct iwl_host_cmd hcmd = {
214                 .id = LDBG_CONFIG_CMD,
215                 .flags = CMD_ASYNC,
216                 .data[0] = &cont_rec,
217                 .len[0] = sizeof(cont_rec),
218         };
219
220         cont_rec.record_mode.enable_recording = start ?
221                 cpu_to_le16(START_DEBUG_RECORDING) :
222                 cpu_to_le16(STOP_DEBUG_RECORDING);
223
224         return iwl_trans_send_cmd(fwrt->trans, &hcmd);
225 }
226
227 static inline void
228 _iwl_fw_dbg_stop_recording(struct iwl_trans *trans,
229                            struct iwl_fw_dbg_params *params)
230 {
231         if (trans->cfg->device_family == IWL_DEVICE_FAMILY_7000) {
232                 iwl_set_bits_prph(trans, MON_BUFF_SAMPLE_CTL, 0x100);
233                 return;
234         }
235
236         if (params) {
237                 params->in_sample = iwl_read_prph(trans, DBGC_IN_SAMPLE);
238                 params->out_ctrl = iwl_read_prph(trans, DBGC_OUT_CTRL);
239         }
240
241         iwl_write_prph(trans, DBGC_IN_SAMPLE, 0);
242         udelay(100);
243         iwl_write_prph(trans, DBGC_OUT_CTRL, 0);
244 }
245
246 static inline void
247 iwl_fw_dbg_stop_recording(struct iwl_fw_runtime *fwrt,
248                           struct iwl_fw_dbg_params *params)
249 {
250         if (fwrt->trans->cfg->device_family < IWL_DEVICE_FAMILY_22560)
251                 _iwl_fw_dbg_stop_recording(fwrt->trans, params);
252         else
253                 iwl_fw_dbg_start_stop_hcmd(fwrt, false);
254 }
255
256 static inline void
257 _iwl_fw_dbg_restart_recording(struct iwl_trans *trans,
258                               struct iwl_fw_dbg_params *params)
259 {
260         if (WARN_ON(!params))
261                 return;
262
263         if (trans->cfg->device_family == IWL_DEVICE_FAMILY_7000) {
264                 iwl_clear_bits_prph(trans, MON_BUFF_SAMPLE_CTL, 0x100);
265                 iwl_clear_bits_prph(trans, MON_BUFF_SAMPLE_CTL, 0x1);
266                 iwl_set_bits_prph(trans, MON_BUFF_SAMPLE_CTL, 0x1);
267         } else {
268                 iwl_write_prph(trans, DBGC_IN_SAMPLE, params->in_sample);
269                 udelay(100);
270                 iwl_write_prph(trans, DBGC_OUT_CTRL, params->out_ctrl);
271         }
272 }
273
274 static inline void
275 iwl_fw_dbg_restart_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_restart_recording(fwrt->trans, params);
280         else
281                 iwl_fw_dbg_start_stop_hcmd(fwrt, true);
282 }
283
284 static inline void iwl_fw_dump_conf_clear(struct iwl_fw_runtime *fwrt)
285 {
286         fwrt->dump.conf = FW_DBG_INVALID;
287 }
288
289 void iwl_fw_error_dump_wk(struct work_struct *work);
290
291 static inline bool iwl_fw_dbg_is_d3_debug_enabled(struct iwl_fw_runtime *fwrt)
292 {
293         return fw_has_capa(&fwrt->fw->ucode_capa,
294                            IWL_UCODE_TLV_CAPA_D3_DEBUG) &&
295                 fwrt->trans->cfg->d3_debug_data_length &&
296                 fwrt->fw->dbg_dump_mask & BIT(IWL_FW_ERROR_DUMP_D3_DEBUG_DATA);
297 }
298
299 void iwl_fw_dbg_read_d3_debug_data(struct iwl_fw_runtime *fwrt);
300
301 static inline void iwl_fw_flush_dump(struct iwl_fw_runtime *fwrt)
302 {
303         flush_delayed_work(&fwrt->dump.wk);
304 }
305
306 static inline void iwl_fw_cancel_dump(struct iwl_fw_runtime *fwrt)
307 {
308         cancel_delayed_work_sync(&fwrt->dump.wk);
309 }
310
311 #ifdef CONFIG_IWLWIFI_DEBUGFS
312 static inline void iwl_fw_cancel_timestamp(struct iwl_fw_runtime *fwrt)
313 {
314         fwrt->timestamp.delay = 0;
315         cancel_delayed_work_sync(&fwrt->timestamp.wk);
316 }
317
318 void iwl_fw_trigger_timestamp(struct iwl_fw_runtime *fwrt, u32 delay);
319
320 static inline void iwl_fw_suspend_timestamp(struct iwl_fw_runtime *fwrt)
321 {
322         cancel_delayed_work_sync(&fwrt->timestamp.wk);
323 }
324
325 static inline void iwl_fw_resume_timestamp(struct iwl_fw_runtime *fwrt)
326 {
327         if (!fwrt->timestamp.delay)
328                 return;
329
330         schedule_delayed_work(&fwrt->timestamp.wk,
331                               round_jiffies_relative(fwrt->timestamp.delay));
332 }
333
334 #else
335
336 static inline void iwl_fw_cancel_timestamp(struct iwl_fw_runtime *fwrt) {}
337
338 static inline void iwl_fw_trigger_timestamp(struct iwl_fw_runtime *fwrt,
339                                             u32 delay) {}
340
341 static inline void iwl_fw_suspend_timestamp(struct iwl_fw_runtime *fwrt) {}
342
343 static inline void iwl_fw_resume_timestamp(struct iwl_fw_runtime *fwrt) {}
344
345 #endif /* CONFIG_IWLWIFI_DEBUGFS */
346
347 #endif  /* __iwl_fw_dbg_h__ */