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