netfilter: netns: shrink netns_ct struct
[linux-2.6-microblaze.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                             void *trigger, 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 (!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
213 static 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)))
231
232 static int iwl_fw_dbg_start_stop_hcmd(struct iwl_fw_runtime *fwrt, bool start)
233 {
234         struct iwl_continuous_record_cmd cont_rec = {};
235         struct iwl_host_cmd hcmd = {
236                 .id = LDBG_CONFIG_CMD,
237                 .flags = CMD_ASYNC,
238                 .data[0] = &cont_rec,
239                 .len[0] = sizeof(cont_rec),
240         };
241
242         cont_rec.record_mode.enable_recording = start ?
243                 cpu_to_le16(START_DEBUG_RECORDING) :
244                 cpu_to_le16(STOP_DEBUG_RECORDING);
245
246         return iwl_trans_send_cmd(fwrt->trans, &hcmd);
247 }
248
249 static inline void
250 _iwl_fw_dbg_stop_recording(struct iwl_trans *trans,
251                            struct iwl_fw_dbg_params *params)
252 {
253         if (trans->cfg->device_family == IWL_DEVICE_FAMILY_7000) {
254                 iwl_set_bits_prph(trans, MON_BUFF_SAMPLE_CTL, 0x100);
255                 return;
256         }
257
258         if (params) {
259                 params->in_sample = iwl_read_prph(trans, DBGC_IN_SAMPLE);
260                 params->out_ctrl = iwl_read_prph(trans, DBGC_OUT_CTRL);
261         }
262
263         iwl_write_prph(trans, DBGC_IN_SAMPLE, 0);
264         udelay(100);
265         iwl_write_prph(trans, DBGC_OUT_CTRL, 0);
266 }
267
268 static inline void
269 iwl_fw_dbg_stop_recording(struct iwl_fw_runtime *fwrt,
270                           struct iwl_fw_dbg_params *params)
271 {
272         if (fwrt->trans->cfg->device_family < IWL_DEVICE_FAMILY_22560)
273                 _iwl_fw_dbg_stop_recording(fwrt->trans, params);
274         else
275                 iwl_fw_dbg_start_stop_hcmd(fwrt, false);
276 }
277
278 static inline void
279 _iwl_fw_dbg_restart_recording(struct iwl_trans *trans,
280                               struct iwl_fw_dbg_params *params)
281 {
282         if (WARN_ON(!params))
283                 return;
284
285         if (trans->cfg->device_family == IWL_DEVICE_FAMILY_7000) {
286                 iwl_clear_bits_prph(trans, MON_BUFF_SAMPLE_CTL, 0x100);
287                 iwl_clear_bits_prph(trans, MON_BUFF_SAMPLE_CTL, 0x1);
288                 iwl_set_bits_prph(trans, MON_BUFF_SAMPLE_CTL, 0x1);
289         } else {
290                 iwl_write_prph(trans, DBGC_IN_SAMPLE, params->in_sample);
291                 udelay(100);
292                 iwl_write_prph(trans, DBGC_OUT_CTRL, params->out_ctrl);
293         }
294 }
295
296 static inline void
297 iwl_fw_dbg_restart_recording(struct iwl_fw_runtime *fwrt,
298                              struct iwl_fw_dbg_params *params)
299 {
300         if (fwrt->trans->cfg->device_family < IWL_DEVICE_FAMILY_22560)
301                 _iwl_fw_dbg_restart_recording(fwrt->trans, params);
302         else
303                 iwl_fw_dbg_start_stop_hcmd(fwrt, true);
304 }
305
306 static inline void iwl_fw_dump_conf_clear(struct iwl_fw_runtime *fwrt)
307 {
308         fwrt->dump.conf = FW_DBG_INVALID;
309 }
310
311 void iwl_fw_error_dump_wk(struct work_struct *work);
312
313 static inline bool iwl_fw_dbg_is_d3_debug_enabled(struct iwl_fw_runtime *fwrt)
314 {
315         return fw_has_capa(&fwrt->fw->ucode_capa,
316                            IWL_UCODE_TLV_CAPA_D3_DEBUG) &&
317                 fwrt->trans->cfg->d3_debug_data_length &&
318                 fwrt->fw->dbg.dump_mask & BIT(IWL_FW_ERROR_DUMP_D3_DEBUG_DATA);
319 }
320
321 void iwl_fw_dbg_read_d3_debug_data(struct iwl_fw_runtime *fwrt);
322
323 static inline void iwl_fw_flush_dump(struct iwl_fw_runtime *fwrt)
324 {
325         flush_delayed_work(&fwrt->dump.wk);
326 }
327
328 static inline void iwl_fw_cancel_dump(struct iwl_fw_runtime *fwrt)
329 {
330         cancel_delayed_work_sync(&fwrt->dump.wk);
331 }
332
333 #ifdef CONFIG_IWLWIFI_DEBUGFS
334 static inline void iwl_fw_cancel_timestamp(struct iwl_fw_runtime *fwrt)
335 {
336         fwrt->timestamp.delay = 0;
337         cancel_delayed_work_sync(&fwrt->timestamp.wk);
338 }
339
340 void iwl_fw_trigger_timestamp(struct iwl_fw_runtime *fwrt, u32 delay);
341
342 static inline void iwl_fw_suspend_timestamp(struct iwl_fw_runtime *fwrt)
343 {
344         cancel_delayed_work_sync(&fwrt->timestamp.wk);
345 }
346
347 static inline void iwl_fw_resume_timestamp(struct iwl_fw_runtime *fwrt)
348 {
349         if (!fwrt->timestamp.delay)
350                 return;
351
352         schedule_delayed_work(&fwrt->timestamp.wk,
353                               round_jiffies_relative(fwrt->timestamp.delay));
354 }
355
356 #else
357
358 static inline void iwl_fw_cancel_timestamp(struct iwl_fw_runtime *fwrt) {}
359
360 static inline void iwl_fw_trigger_timestamp(struct iwl_fw_runtime *fwrt,
361                                             u32 delay) {}
362
363 static inline void iwl_fw_suspend_timestamp(struct iwl_fw_runtime *fwrt) {}
364
365 static inline void iwl_fw_resume_timestamp(struct iwl_fw_runtime *fwrt) {}
366
367 #endif /* CONFIG_IWLWIFI_DEBUGFS */
368
369 void iwl_fw_alive_error_dump(struct iwl_fw_runtime *fwrt);
370 void iwl_fw_dbg_collect_sync(struct iwl_fw_runtime *fwrt);
371 #endif  /* __iwl_fw_dbg_h__ */