dbg.c 36.1 KB
Newer Older
1 2 3 4 5 6 7 8 9
/******************************************************************************
 *
 * This file is provided under a dual BSD/GPLv2 license.  When using or
 * redistributing this file, you may do so under either license.
 *
 * GPL LICENSE SUMMARY
 *
 * Copyright(c) 2008 - 2014 Intel Corporation. All rights reserved.
 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10
 * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
11
 * Copyright(c) 2018        Intel Corporation
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program;
 *
 * The full GNU General Public License is included in this distribution
 * in the file called COPYING.
 *
 * Contact Information:
29
 *  Intel Linux Wireless <linuxwifi@intel.com>
30 31 32 33 34 35
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *
 * BSD LICENSE
 *
 * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
36
 * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
37
 * Copyright(c) 2018        Intel Corporation
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name Intel Corporation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *****************************************************************************/
#include <linux/devcoredump.h>
68 69 70
#include "iwl-drv.h"
#include "runtime.h"
#include "dbg.h"
71
#include "debugfs.h"
72 73 74 75
#include "iwl-io.h"
#include "iwl-prph.h"
#include "iwl-csr.h"

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
/**
 * struct iwl_fw_dump_ptrs - set of pointers needed for the fw-error-dump
 *
 * @fwrt_ptr: pointer to the buffer coming from fwrt
 * @trans_ptr: pointer to struct %iwl_trans_dump_data which contains the
 *	transport's data.
 * @trans_len: length of the valid data in trans_ptr
 * @fwrt_len: length of the valid data in fwrt_ptr
 */
struct iwl_fw_dump_ptrs {
	struct iwl_trans_dump_data *trans_ptr;
	void *fwrt_ptr;
	u32 fwrt_len;
};

91
#define RADIO_REG_MAX_READ 0x2ad
92 93
static void iwl_read_radio_regs(struct iwl_fw_runtime *fwrt,
				struct iwl_fw_error_dump_data **dump_data)
94 95 96 97 98
{
	u8 *pos = (void *)(*dump_data)->data;
	unsigned long flags;
	int i;

99 100
	IWL_DEBUG_INFO(fwrt, "WRT radio registers dump\n");

101
	if (!iwl_trans_grab_nic_access(fwrt->trans, &flags))
102 103 104 105 106 107 108 109 110
		return;

	(*dump_data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_RADIO_REG);
	(*dump_data)->len = cpu_to_le32(RADIO_REG_MAX_READ);

	for (i = 0; i < RADIO_REG_MAX_READ; i++) {
		u32 rd_cmd = RADIO_RSP_RD_CMD;

		rd_cmd |= i << RADIO_RSP_ADDR_POS;
111 112
		iwl_write_prph_no_grab(fwrt->trans, RSP_RADIO_CMD, rd_cmd);
		*pos = (u8)iwl_read_prph_no_grab(fwrt->trans, RSP_RADIO_RDDAT);
113 114 115 116 117 118

		pos++;
	}

	*dump_data = iwl_fw_error_next_data(*dump_data);

119
	iwl_trans_release_nic_access(fwrt->trans, &flags);
120 121
}

122 123 124
static void iwl_fwrt_dump_rxf(struct iwl_fw_runtime *fwrt,
			      struct iwl_fw_error_dump_data **dump_data,
			      int size, u32 offset, int fifo_num)
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
{
	struct iwl_fw_error_dump_fifo *fifo_hdr;
	u32 *fifo_data;
	u32 fifo_len;
	int i;

	fifo_hdr = (void *)(*dump_data)->data;
	fifo_data = (void *)fifo_hdr->data;
	fifo_len = size;

	/* No need to try to read the data if the length is 0 */
	if (fifo_len == 0)
		return;

	/* Add a TLV for the RXF */
	(*dump_data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_RXF);
	(*dump_data)->len = cpu_to_le32(fifo_len + sizeof(*fifo_hdr));

	fifo_hdr->fifo_num = cpu_to_le32(fifo_num);
	fifo_hdr->available_bytes =
145
		cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
146 147
						RXF_RD_D_SPACE + offset));
	fifo_hdr->wr_ptr =
148
		cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
149 150
						RXF_RD_WR_PTR + offset));
	fifo_hdr->rd_ptr =
151
		cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
152 153
						RXF_RD_RD_PTR + offset));
	fifo_hdr->fence_ptr =
154
		cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
155 156
						RXF_RD_FENCE_PTR + offset));
	fifo_hdr->fence_mode =
157
		cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
158 159 160
						RXF_SET_FENCE_MODE + offset));

	/* Lock fence */
161
	iwl_trans_write_prph(fwrt->trans, RXF_SET_FENCE_MODE + offset, 0x1);
162
	/* Set fence pointer to the same place like WR pointer */
163
	iwl_trans_write_prph(fwrt->trans, RXF_LD_WR2FENCE + offset, 0x1);
164
	/* Set fence offset */
165
	iwl_trans_write_prph(fwrt->trans,
166 167 168 169 170
			     RXF_LD_FENCE_OFFSET_ADDR + offset, 0x0);

	/* Read FIFO */
	fifo_len /= sizeof(u32); /* Size in DWORDS */
	for (i = 0; i < fifo_len; i++)
171
		fifo_data[i] = iwl_trans_read_prph(fwrt->trans,
172 173 174 175 176
						 RXF_FIFO_RD_FENCE_INC +
						 offset);
	*dump_data = iwl_fw_error_next_data(*dump_data);
}

177 178 179
static void iwl_fwrt_dump_txf(struct iwl_fw_runtime *fwrt,
			      struct iwl_fw_error_dump_data **dump_data,
			      int size, u32 offset, int fifo_num)
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
{
	struct iwl_fw_error_dump_fifo *fifo_hdr;
	u32 *fifo_data;
	u32 fifo_len;
	int i;

	fifo_hdr = (void *)(*dump_data)->data;
	fifo_data = (void *)fifo_hdr->data;
	fifo_len = size;

	/* No need to try to read the data if the length is 0 */
	if (fifo_len == 0)
		return;

	/* Add a TLV for the FIFO */
	(*dump_data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_TXF);
	(*dump_data)->len = cpu_to_le32(fifo_len + sizeof(*fifo_hdr));

	fifo_hdr->fifo_num = cpu_to_le32(fifo_num);
	fifo_hdr->available_bytes =
200
		cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
201 202
						TXF_FIFO_ITEM_CNT + offset));
	fifo_hdr->wr_ptr =
203
		cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
204 205
						TXF_WR_PTR + offset));
	fifo_hdr->rd_ptr =
206
		cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
207 208
						TXF_RD_PTR + offset));
	fifo_hdr->fence_ptr =
209
		cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
210 211
						TXF_FENCE_PTR + offset));
	fifo_hdr->fence_mode =
212
		cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
213 214 215
						TXF_LOCK_FENCE + offset));

	/* Set the TXF_READ_MODIFY_ADDR to TXF_WR_PTR */
216
	iwl_trans_write_prph(fwrt->trans, TXF_READ_MODIFY_ADDR + offset,
217 218 219
			     TXF_WR_PTR + offset);

	/* Dummy-read to advance the read pointer to the head */
220
	iwl_trans_read_prph(fwrt->trans, TXF_READ_MODIFY_DATA + offset);
221 222 223 224

	/* Read FIFO */
	fifo_len /= sizeof(u32); /* Size in DWORDS */
	for (i = 0; i < fifo_len; i++)
225
		fifo_data[i] = iwl_trans_read_prph(fwrt->trans,
226 227 228 229 230
						  TXF_READ_MODIFY_DATA +
						  offset);
	*dump_data = iwl_fw_error_next_data(*dump_data);
}

231 232
static void iwl_fw_dump_fifos(struct iwl_fw_runtime *fwrt,
			      struct iwl_fw_error_dump_data **dump_data)
233 234
{
	struct iwl_fw_error_dump_fifo *fifo_hdr;
235
	struct iwl_fwrt_shared_mem_cfg *cfg = &fwrt->smem_cfg;
236 237 238 239 240
	u32 *fifo_data;
	u32 fifo_len;
	unsigned long flags;
	int i, j;

241 242
	IWL_DEBUG_INFO(fwrt, "WRT FIFO dump\n");

243
	if (!iwl_trans_grab_nic_access(fwrt->trans, &flags))
244 245
		return;

246
	/* Pull RXF1 */
247
	iwl_fwrt_dump_rxf(fwrt, dump_data, cfg->lmac[0].rxfifo1_size, 0, 0);
248
	/* Pull RXF2 */
249 250
	iwl_fwrt_dump_rxf(fwrt, dump_data, cfg->rxfifo2_size,
			  RXF_DIFF_FROM_PREV, 1);
251
	/* Pull LMAC2 RXF1 */
252 253 254
	if (fwrt->smem_cfg.num_lmacs > 1)
		iwl_fwrt_dump_rxf(fwrt, dump_data, cfg->lmac[1].rxfifo1_size,
				  LMAC2_PRPH_OFFSET, 2);
255 256

	/* Pull TXF data from LMAC1 */
257
	for (i = 0; i < fwrt->smem_cfg.num_txfifo_entries; i++) {
258
		/* Mark the number of TXF we're pulling now */
259 260 261
		iwl_trans_write_prph(fwrt->trans, TXF_LARC_NUM, i);
		iwl_fwrt_dump_txf(fwrt, dump_data, cfg->lmac[0].txfifo_size[i],
				  0, i);
262
	}
263

264
	/* Pull TXF data from LMAC2 */
265 266
	if (fwrt->smem_cfg.num_lmacs > 1) {
		for (i = 0; i < fwrt->smem_cfg.num_txfifo_entries; i++) {
267
			/* Mark the number of TXF we're pulling now */
268
			iwl_trans_write_prph(fwrt->trans,
269 270
					     TXF_LARC_NUM + LMAC2_PRPH_OFFSET,
					     i);
271 272 273 274
			iwl_fwrt_dump_txf(fwrt, dump_data,
					  cfg->lmac[1].txfifo_size[i],
					  LMAC2_PRPH_OFFSET,
					  i + cfg->num_txfifo_entries);
275
		}
276 277
	}

278
	if (fw_has_capa(&fwrt->fw->ucode_capa,
279 280 281
			IWL_UCODE_TLV_CAPA_EXTEND_SHARED_MEM_CFG)) {
		/* Pull UMAC internal TXF data from all TXFs */
		for (i = 0;
282
		     i < ARRAY_SIZE(fwrt->smem_cfg.internal_txfifo_size);
283 284 285
		     i++) {
			fifo_hdr = (void *)(*dump_data)->data;
			fifo_data = (void *)fifo_hdr->data;
286
			fifo_len = fwrt->smem_cfg.internal_txfifo_size[i];
287 288 289 290 291 292 293 294 295 296 297 298

			/* No need to try to read the data if the length is 0 */
			if (fifo_len == 0)
				continue;

			/* Add a TLV for the internal FIFOs */
			(*dump_data)->type =
				cpu_to_le32(IWL_FW_ERROR_DUMP_INTERNAL_TXF);
			(*dump_data)->len =
				cpu_to_le32(fifo_len + sizeof(*fifo_hdr));

			fifo_hdr->fifo_num = cpu_to_le32(i);
299 300

			/* Mark the number of TXF we're pulling now */
301 302
			iwl_trans_write_prph(fwrt->trans, TXF_CPU2_NUM, i +
				fwrt->smem_cfg.num_txfifo_entries);
303

304
			fifo_hdr->available_bytes =
305
				cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
306 307
								TXF_CPU2_FIFO_ITEM_CNT));
			fifo_hdr->wr_ptr =
308
				cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
309 310
								TXF_CPU2_WR_PTR));
			fifo_hdr->rd_ptr =
311
				cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
312 313
								TXF_CPU2_RD_PTR));
			fifo_hdr->fence_ptr =
314
				cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
315 316
								TXF_CPU2_FENCE_PTR));
			fifo_hdr->fence_mode =
317
				cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
318 319 320
								TXF_CPU2_LOCK_FENCE));

			/* Set TXF_CPU2_READ_MODIFY_ADDR to TXF_CPU2_WR_PTR */
321
			iwl_trans_write_prph(fwrt->trans,
322 323 324 325
					     TXF_CPU2_READ_MODIFY_ADDR,
					     TXF_CPU2_WR_PTR);

			/* Dummy-read to advance the read pointer to head */
326
			iwl_trans_read_prph(fwrt->trans,
327 328 329 330 331 332
					    TXF_CPU2_READ_MODIFY_DATA);

			/* Read FIFO */
			fifo_len /= sizeof(u32); /* Size in DWORDS */
			for (j = 0; j < fifo_len; j++)
				fifo_data[j] =
333
					iwl_trans_read_prph(fwrt->trans,
334 335 336 337 338
							    TXF_CPU2_READ_MODIFY_DATA);
			*dump_data = iwl_fw_error_next_data(*dump_data);
		}
	}

339
	iwl_trans_release_nic_access(fwrt->trans, &flags);
340 341 342 343 344
}

#define IWL8260_ICCM_OFFSET		0x44000 /* Only for B-step */
#define IWL8260_ICCM_LEN		0xC000 /* Only for B-step */

345
struct iwl_prph_range {
346
	u32 start, end;
347 348 349
};

static const struct iwl_prph_range iwl_prph_dump_addr_comm[] = {
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
	{ .start = 0x00a00000, .end = 0x00a00000 },
	{ .start = 0x00a0000c, .end = 0x00a00024 },
	{ .start = 0x00a0002c, .end = 0x00a0003c },
	{ .start = 0x00a00410, .end = 0x00a00418 },
	{ .start = 0x00a00420, .end = 0x00a00420 },
	{ .start = 0x00a00428, .end = 0x00a00428 },
	{ .start = 0x00a00430, .end = 0x00a0043c },
	{ .start = 0x00a00444, .end = 0x00a00444 },
	{ .start = 0x00a004c0, .end = 0x00a004cc },
	{ .start = 0x00a004d8, .end = 0x00a004d8 },
	{ .start = 0x00a004e0, .end = 0x00a004f0 },
	{ .start = 0x00a00840, .end = 0x00a00840 },
	{ .start = 0x00a00850, .end = 0x00a00858 },
	{ .start = 0x00a01004, .end = 0x00a01008 },
	{ .start = 0x00a01010, .end = 0x00a01010 },
	{ .start = 0x00a01018, .end = 0x00a01018 },
	{ .start = 0x00a01024, .end = 0x00a01024 },
	{ .start = 0x00a0102c, .end = 0x00a01034 },
	{ .start = 0x00a0103c, .end = 0x00a01040 },
	{ .start = 0x00a01048, .end = 0x00a01094 },
	{ .start = 0x00a01c00, .end = 0x00a01c20 },
	{ .start = 0x00a01c58, .end = 0x00a01c58 },
	{ .start = 0x00a01c7c, .end = 0x00a01c7c },
	{ .start = 0x00a01c28, .end = 0x00a01c54 },
	{ .start = 0x00a01c5c, .end = 0x00a01c5c },
	{ .start = 0x00a01c60, .end = 0x00a01cdc },
	{ .start = 0x00a01ce0, .end = 0x00a01d0c },
	{ .start = 0x00a01d18, .end = 0x00a01d20 },
	{ .start = 0x00a01d2c, .end = 0x00a01d30 },
	{ .start = 0x00a01d40, .end = 0x00a01d5c },
	{ .start = 0x00a01d80, .end = 0x00a01d80 },
	{ .start = 0x00a01d98, .end = 0x00a01d9c },
	{ .start = 0x00a01da8, .end = 0x00a01da8 },
	{ .start = 0x00a01db8, .end = 0x00a01df4 },
	{ .start = 0x00a01dc0, .end = 0x00a01dfc },
	{ .start = 0x00a01e00, .end = 0x00a01e2c },
	{ .start = 0x00a01e40, .end = 0x00a01e60 },
	{ .start = 0x00a01e68, .end = 0x00a01e6c },
	{ .start = 0x00a01e74, .end = 0x00a01e74 },
	{ .start = 0x00a01e84, .end = 0x00a01e90 },
	{ .start = 0x00a01e9c, .end = 0x00a01ec4 },
	{ .start = 0x00a01ed0, .end = 0x00a01ee0 },
	{ .start = 0x00a01f00, .end = 0x00a01f1c },
	{ .start = 0x00a01f44, .end = 0x00a01ffc },
	{ .start = 0x00a02000, .end = 0x00a02048 },
	{ .start = 0x00a02068, .end = 0x00a020f0 },
	{ .start = 0x00a02100, .end = 0x00a02118 },
	{ .start = 0x00a02140, .end = 0x00a0214c },
	{ .start = 0x00a02168, .end = 0x00a0218c },
	{ .start = 0x00a021c0, .end = 0x00a021c0 },
	{ .start = 0x00a02400, .end = 0x00a02410 },
	{ .start = 0x00a02418, .end = 0x00a02420 },
	{ .start = 0x00a02428, .end = 0x00a0242c },
	{ .start = 0x00a02434, .end = 0x00a02434 },
	{ .start = 0x00a02440, .end = 0x00a02460 },
	{ .start = 0x00a02468, .end = 0x00a024b0 },
	{ .start = 0x00a024c8, .end = 0x00a024cc },
	{ .start = 0x00a02500, .end = 0x00a02504 },
	{ .start = 0x00a0250c, .end = 0x00a02510 },
	{ .start = 0x00a02540, .end = 0x00a02554 },
	{ .start = 0x00a02580, .end = 0x00a025f4 },
	{ .start = 0x00a02600, .end = 0x00a0260c },
	{ .start = 0x00a02648, .end = 0x00a02650 },
	{ .start = 0x00a02680, .end = 0x00a02680 },
	{ .start = 0x00a026c0, .end = 0x00a026d0 },
	{ .start = 0x00a02700, .end = 0x00a0270c },
	{ .start = 0x00a02804, .end = 0x00a02804 },
	{ .start = 0x00a02818, .end = 0x00a0281c },
	{ .start = 0x00a02c00, .end = 0x00a02db4 },
	{ .start = 0x00a02df4, .end = 0x00a02fb0 },
	{ .start = 0x00a03000, .end = 0x00a03014 },
	{ .start = 0x00a0301c, .end = 0x00a0302c },
	{ .start = 0x00a03034, .end = 0x00a03038 },
	{ .start = 0x00a03040, .end = 0x00a03048 },
	{ .start = 0x00a03060, .end = 0x00a03068 },
	{ .start = 0x00a03070, .end = 0x00a03074 },
	{ .start = 0x00a0307c, .end = 0x00a0307c },
	{ .start = 0x00a03080, .end = 0x00a03084 },
	{ .start = 0x00a0308c, .end = 0x00a03090 },
	{ .start = 0x00a03098, .end = 0x00a03098 },
	{ .start = 0x00a030a0, .end = 0x00a030a0 },
	{ .start = 0x00a030a8, .end = 0x00a030b4 },
	{ .start = 0x00a030bc, .end = 0x00a030bc },
	{ .start = 0x00a030c0, .end = 0x00a0312c },
	{ .start = 0x00a03c00, .end = 0x00a03c5c },
	{ .start = 0x00a04400, .end = 0x00a04454 },
	{ .start = 0x00a04460, .end = 0x00a04474 },
	{ .start = 0x00a044c0, .end = 0x00a044ec },
	{ .start = 0x00a04500, .end = 0x00a04504 },
	{ .start = 0x00a04510, .end = 0x00a04538 },
	{ .start = 0x00a04540, .end = 0x00a04548 },
	{ .start = 0x00a04560, .end = 0x00a0457c },
	{ .start = 0x00a04590, .end = 0x00a04598 },
	{ .start = 0x00a045c0, .end = 0x00a045f4 },
};

446 447 448 449 450 451 452
static const struct iwl_prph_range iwl_prph_dump_addr_9000[] = {
	{ .start = 0x00a05c00, .end = 0x00a05c18 },
	{ .start = 0x00a05400, .end = 0x00a056e8 },
	{ .start = 0x00a08000, .end = 0x00a098bc },
	{ .start = 0x00a02400, .end = 0x00a02758 },
};

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
static void _iwl_read_prph_block(struct iwl_trans *trans, u32 start,
				 u32 len_bytes, __le32 *data)
{
	u32 i;

	for (i = 0; i < len_bytes; i += 4)
		*data++ = cpu_to_le32(iwl_read_prph_no_grab(trans, start + i));
}

static bool iwl_read_prph_block(struct iwl_trans *trans, u32 start,
				u32 len_bytes, __le32 *data)
{
	unsigned long flags;
	bool success = false;

	if (iwl_trans_grab_nic_access(trans, &flags)) {
		success = true;
		_iwl_read_prph_block(trans, start, len_bytes, data);
		iwl_trans_release_nic_access(trans, &flags);
	}

	return success;
}

477 478 479 480
static void iwl_dump_prph(struct iwl_trans *trans,
			  struct iwl_fw_error_dump_data **data,
			  const struct iwl_prph_range *iwl_prph_dump_addr,
			  u32 range_len)
481 482 483
{
	struct iwl_fw_error_dump_prph *prph;
	unsigned long flags;
484
	u32 i;
485

486 487
	IWL_DEBUG_INFO(trans, "WRT PRPH dump\n");

488
	if (!iwl_trans_grab_nic_access(trans, &flags))
489
		return;
490

491
	for (i = 0; i < range_len; i++) {
492 493 494 495 496 497 498 499 500 501
		/* The range includes both boundaries */
		int num_bytes_in_chunk = iwl_prph_dump_addr[i].end -
			 iwl_prph_dump_addr[i].start + 4;

		(*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_PRPH);
		(*data)->len = cpu_to_le32(sizeof(*prph) +
					num_bytes_in_chunk);
		prph = (void *)(*data)->data;
		prph->prph_start = cpu_to_le32(iwl_prph_dump_addr[i].start);

502 503 504 505 506
		_iwl_read_prph_block(trans, iwl_prph_dump_addr[i].start,
				     /* our range is inclusive, hence + 4 */
				     iwl_prph_dump_addr[i].end -
				     iwl_prph_dump_addr[i].start + 4,
				     (void *)prph->data);
507

D
Dan Carpenter 已提交
508
		*data = iwl_fw_error_next_data(*data);
509 510 511 512 513
	}

	iwl_trans_release_nic_access(trans, &flags);
}

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
/*
 * alloc_sgtable - allocates scallerlist table in the given size,
 * fills it with pages and returns it
 * @size: the size (in bytes) of the table
*/
static struct scatterlist *alloc_sgtable(int size)
{
	int alloc_size, nents, i;
	struct page *new_page;
	struct scatterlist *iter;
	struct scatterlist *table;

	nents = DIV_ROUND_UP(size, PAGE_SIZE);
	table = kcalloc(nents, sizeof(*table), GFP_KERNEL);
	if (!table)
		return NULL;
	sg_init_table(table, nents);
	iter = table;
	for_each_sg(table, iter, sg_nents(table), i) {
		new_page = alloc_page(GFP_KERNEL);
		if (!new_page) {
			/* release all previous allocated pages in the table */
			iter = table;
			for_each_sg(table, iter, sg_nents(table), i) {
				new_page = sg_page(iter);
				if (new_page)
					__free_page(new_page);
			}
			return NULL;
		}
		alloc_size = min_t(int, size, PAGE_SIZE);
		size -= PAGE_SIZE;
		sg_set_page(iter, new_page, alloc_size, 0);
	}
	return table;
}

551
void iwl_fw_error_dump(struct iwl_fw_runtime *fwrt)
552 553 554 555 556
{
	struct iwl_fw_error_dump_file *dump_file;
	struct iwl_fw_error_dump_data *dump_data;
	struct iwl_fw_error_dump_info *dump_info;
	struct iwl_fw_error_dump_mem *dump_mem;
557
	struct iwl_fw_error_dump_smem_cfg *dump_smem_cfg;
558
	struct iwl_fw_error_dump_trigger_desc *dump_trig;
559
	struct iwl_fw_dump_ptrs *fw_error_dump;
560
	struct scatterlist *sg_dump_data;
561
	u32 sram_len, sram_ofs;
562
	const struct iwl_fw_dbg_mem_seg_tlv *fw_dbg_mem = fwrt->fw->dbg_mem_tlv;
563
	struct iwl_fwrt_shared_mem_cfg *mem_cfg = &fwrt->smem_cfg;
564
	u32 file_len, fifo_data_len = 0, prph_len = 0, radio_len = 0;
565 566 567
	u32 smem_len = fwrt->fw->n_dbg_mem_tlv ? 0 : fwrt->trans->cfg->smem_len;
	u32 sram2_len = fwrt->fw->n_dbg_mem_tlv ?
				0 : fwrt->trans->cfg->dccm2_len;
568 569 570
	bool monitor_dump_only = false;
	int i;

571 572
	IWL_DEBUG_INFO(fwrt, "WRT dump start\n");

573
	/* there's no point in fw dump if the bus is dead */
574 575
	if (test_bit(STATUS_TRANS_DEAD, &fwrt->trans->status)) {
		IWL_ERR(fwrt, "Skip fw error dump since bus is dead\n");
576
		goto out;
577 578
	}

579 580
	if (fwrt->dump.trig &&
	    fwrt->dump.trig->mode & IWL_FW_DBG_TRIGGER_MONITOR_ONLY)
581 582 583 584
		monitor_dump_only = true;

	fw_error_dump = kzalloc(sizeof(*fw_error_dump), GFP_KERNEL);
	if (!fw_error_dump)
585
		goto out;
586 587

	/* SRAM - include stack CCM if driver knows the values for it */
588
	if (!fwrt->trans->cfg->dccm_offset || !fwrt->trans->cfg->dccm_len) {
589 590
		const struct fw_img *img;

591
		img = &fwrt->fw->img[fwrt->cur_fw_img];
592 593 594
		sram_ofs = img->sec[IWL_UCODE_SECTION_DATA].offset;
		sram_len = img->sec[IWL_UCODE_SECTION_DATA].len;
	} else {
595 596
		sram_ofs = fwrt->trans->cfg->dccm_offset;
		sram_len = fwrt->trans->cfg->dccm_len;
597 598 599
	}

	/* reading RXF/TXF sizes */
600
	if (test_bit(STATUS_FW_ERROR, &fwrt->trans->status)) {
601 602
		fifo_data_len = 0;

603 604
		/* Count RXF2 size */
		if (mem_cfg->rxfifo2_size) {
605
			/* Add header info */
606
			fifo_data_len += mem_cfg->rxfifo2_size +
607 608 609 610
					 sizeof(*dump_data) +
					 sizeof(struct iwl_fw_error_dump_fifo);
		}

611 612 613
		/* Count RXF1 sizes */
		for (i = 0; i < mem_cfg->num_lmacs; i++) {
			if (!mem_cfg->lmac[i].rxfifo1_size)
614 615 616
				continue;

			/* Add header info */
617
			fifo_data_len += mem_cfg->lmac[i].rxfifo1_size +
618 619 620
					 sizeof(*dump_data) +
					 sizeof(struct iwl_fw_error_dump_fifo);
		}
621

622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
		/* Count TXF sizes */
		for (i = 0; i < mem_cfg->num_lmacs; i++) {
			int j;

			for (j = 0; j < mem_cfg->num_txfifo_entries; j++) {
				if (!mem_cfg->lmac[i].txfifo_size[j])
					continue;

				/* Add header info */
				fifo_data_len +=
					mem_cfg->lmac[i].txfifo_size[j] +
					sizeof(*dump_data) +
					sizeof(struct iwl_fw_error_dump_fifo);
			}
		}

638
		if (fw_has_capa(&fwrt->fw->ucode_capa,
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
				IWL_UCODE_TLV_CAPA_EXTEND_SHARED_MEM_CFG)) {
			for (i = 0;
			     i < ARRAY_SIZE(mem_cfg->internal_txfifo_size);
			     i++) {
				if (!mem_cfg->internal_txfifo_size[i])
					continue;

				/* Add header info */
				fifo_data_len +=
					mem_cfg->internal_txfifo_size[i] +
					sizeof(*dump_data) +
					sizeof(struct iwl_fw_error_dump_fifo);
			}
		}

654
		/* Make room for PRPH registers */
655
		if (!fwrt->trans->cfg->gen2) {
656 657 658 659 660 661 662 663 664 665 666
			for (i = 0; i < ARRAY_SIZE(iwl_prph_dump_addr_comm);
			     i++) {
				/* The range includes both boundaries */
				int num_bytes_in_chunk =
					iwl_prph_dump_addr_comm[i].end -
					iwl_prph_dump_addr_comm[i].start + 4;

				prph_len += sizeof(*dump_data) +
					sizeof(struct iwl_fw_error_dump_prph) +
					num_bytes_in_chunk;
			}
667
		}
668

669 670
		if (!fwrt->trans->cfg->gen2 &&
		    fwrt->trans->cfg->mq_rx_supported) {
671 672 673 674 675 676 677 678 679 680 681 682 683
			for (i = 0; i <
				ARRAY_SIZE(iwl_prph_dump_addr_9000); i++) {
				/* The range includes both boundaries */
				int num_bytes_in_chunk =
					iwl_prph_dump_addr_9000[i].end -
					iwl_prph_dump_addr_9000[i].start + 4;

				prph_len += sizeof(*dump_data) +
					sizeof(struct iwl_fw_error_dump_prph) +
					num_bytes_in_chunk;
			}
		}

684
		if (fwrt->trans->cfg->device_family == IWL_DEVICE_FAMILY_7000)
685
			radio_len = sizeof(*dump_data) + RADIO_REG_MAX_READ;
686 687 688
	}

	file_len = sizeof(*dump_file) +
689 690
		   sizeof(*dump_data) * 3 +
		   sizeof(*dump_smem_cfg) +
691
		   fifo_data_len +
692
		   prph_len +
693
		   radio_len +
694 695 696 697 698 699 700 701 702 703
		   sizeof(*dump_info);

	/* Make room for the SMEM, if it exists */
	if (smem_len)
		file_len += sizeof(*dump_data) + sizeof(*dump_mem) + smem_len;

	/* Make room for the secondary SRAM, if it exists */
	if (sram2_len)
		file_len += sizeof(*dump_data) + sizeof(*dump_mem) + sram2_len;

704
	/* Make room for MEM segments */
705
	for (i = 0; i < fwrt->fw->n_dbg_mem_tlv; i++) {
706 707
		file_len += sizeof(*dump_data) + sizeof(*dump_mem) +
			    le32_to_cpu(fw_dbg_mem[i].len);
708 709
	}

710
	/* Make room for fw's virtual image pages, if it exists */
711 712 713 714
	if (!fwrt->trans->cfg->gen2 &&
	    fwrt->fw->img[fwrt->cur_fw_img].paging_mem_size &&
	    fwrt->fw_paging_db[0].fw_paging_block)
		file_len += fwrt->num_of_paging_blk *
715 716 717 718 719 720
			(sizeof(*dump_data) +
			 sizeof(struct iwl_fw_error_dump_paging) +
			 PAGING_BLOCK_SIZE);

	/* If we only want a monitor dump, reset the file length */
	if (monitor_dump_only) {
721 722
		file_len = sizeof(*dump_file) + sizeof(*dump_data) * 2 +
			   sizeof(*dump_info) + sizeof(*dump_smem_cfg);
723 724
	}

725
	if (fwrt->dump.desc)
726
		file_len += sizeof(*dump_data) + sizeof(*dump_trig) +
727
			    fwrt->dump.desc->len;
728

729
	if (!fwrt->fw->n_dbg_mem_tlv)
730 731
		file_len += sram_len + sizeof(*dump_mem);

732 733 734
	dump_file = vzalloc(file_len);
	if (!dump_file) {
		kfree(fw_error_dump);
735
		goto out;
736 737
	}

738
	fw_error_dump->fwrt_ptr = dump_file;
739 740 741 742 743 744 745 746

	dump_file->barker = cpu_to_le32(IWL_FW_ERROR_DUMP_BARKER);
	dump_data = (void *)dump_file->data;

	dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_DEV_FW_INFO);
	dump_data->len = cpu_to_le32(sizeof(*dump_info));
	dump_info = (void *)dump_data->data;
	dump_info->device_family =
747
		fwrt->trans->cfg->device_family == IWL_DEVICE_FAMILY_7000 ?
748 749
			cpu_to_le32(IWL_FW_ERROR_DUMP_FAMILY_7) :
			cpu_to_le32(IWL_FW_ERROR_DUMP_FAMILY_8);
750 751
	dump_info->hw_step = cpu_to_le32(CSR_HW_REV_STEP(fwrt->trans->hw_rev));
	memcpy(dump_info->fw_human_readable, fwrt->fw->human_readable,
752
	       sizeof(dump_info->fw_human_readable));
753
	strncpy(dump_info->dev_human_readable, fwrt->trans->cfg->name,
754
		sizeof(dump_info->dev_human_readable));
755
	strncpy(dump_info->bus_human_readable, fwrt->dev->bus->name,
756 757 758
		sizeof(dump_info->bus_human_readable));

	dump_data = iwl_fw_error_next_data(dump_data);
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785

	/* Dump shared memory configuration */
	dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_MEM_CFG);
	dump_data->len = cpu_to_le32(sizeof(*dump_smem_cfg));
	dump_smem_cfg = (void *)dump_data->data;
	dump_smem_cfg->num_lmacs = cpu_to_le32(mem_cfg->num_lmacs);
	dump_smem_cfg->num_txfifo_entries =
		cpu_to_le32(mem_cfg->num_txfifo_entries);
	for (i = 0; i < MAX_NUM_LMAC; i++) {
		int j;

		for (j = 0; j < TX_FIFO_MAX_NUM; j++)
			dump_smem_cfg->lmac[i].txfifo_size[j] =
				cpu_to_le32(mem_cfg->lmac[i].txfifo_size[j]);
		dump_smem_cfg->lmac[i].rxfifo1_size =
			cpu_to_le32(mem_cfg->lmac[i].rxfifo1_size);
	}
	dump_smem_cfg->rxfifo2_size = cpu_to_le32(mem_cfg->rxfifo2_size);
	dump_smem_cfg->internal_txfifo_addr =
		cpu_to_le32(mem_cfg->internal_txfifo_addr);
	for (i = 0; i < TX_FIFO_INTERNAL_MAX_NUM; i++) {
		dump_smem_cfg->internal_txfifo_size[i] =
			cpu_to_le32(mem_cfg->internal_txfifo_size[i]);
	}

	dump_data = iwl_fw_error_next_data(dump_data);

786
	/* We only dump the FIFOs if the FW is in error state */
787 788
	if (test_bit(STATUS_FW_ERROR, &fwrt->trans->status)) {
		iwl_fw_dump_fifos(fwrt, &dump_data);
789
		if (radio_len)
790
			iwl_read_radio_regs(fwrt, &dump_data);
791
	}
792

793
	if (fwrt->dump.desc) {
794 795
		dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_ERROR_INFO);
		dump_data->len = cpu_to_le32(sizeof(*dump_trig) +
796
					     fwrt->dump.desc->len);
797
		dump_trig = (void *)dump_data->data;
798 799
		memcpy(dump_trig, &fwrt->dump.desc->trig_desc,
		       sizeof(*dump_trig) + fwrt->dump.desc->len);
800 801 802 803 804 805 806 807

		dump_data = iwl_fw_error_next_data(dump_data);
	}

	/* In case we only want monitor dump, skip to dump trasport data */
	if (monitor_dump_only)
		goto dump_trans_data;

808
	if (!fwrt->fw->n_dbg_mem_tlv) {
809 810 811 812 813
		dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_MEM);
		dump_data->len = cpu_to_le32(sram_len + sizeof(*dump_mem));
		dump_mem = (void *)dump_data->data;
		dump_mem->type = cpu_to_le32(IWL_FW_ERROR_DUMP_MEM_SRAM);
		dump_mem->offset = cpu_to_le32(sram_ofs);
814
		iwl_trans_read_mem_bytes(fwrt->trans, sram_ofs, dump_mem->data,
815 816 817 818
					 sram_len);
		dump_data = iwl_fw_error_next_data(dump_data);
	}

819
	for (i = 0; i < fwrt->fw->n_dbg_mem_tlv; i++) {
820 821
		u32 len = le32_to_cpu(fw_dbg_mem[i].len);
		u32 ofs = le32_to_cpu(fw_dbg_mem[i].ofs);
822
		bool success;
823 824 825 826 827 828

		dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_MEM);
		dump_data->len = cpu_to_le32(len + sizeof(*dump_mem));
		dump_mem = (void *)dump_data->data;
		dump_mem->type = fw_dbg_mem[i].data_type;
		dump_mem->offset = cpu_to_le32(ofs);
829

830 831 832
		IWL_DEBUG_INFO(fwrt, "WRT memory dump. Type=%u\n",
			       dump_mem->type);

833 834
		switch (dump_mem->type & cpu_to_le32(FW_DBG_MEM_TYPE_MASK)) {
		case cpu_to_le32(FW_DBG_MEM_TYPE_REGULAR):
835
			iwl_trans_read_mem_bytes(fwrt->trans, ofs,
836 837 838 839 840
						 dump_mem->data,
						 len);
			success = true;
			break;
		case cpu_to_le32(FW_DBG_MEM_TYPE_PRPH):
841
			success = iwl_read_prph_block(fwrt->trans, ofs, len,
842 843 844 845 846 847 848 849 850 851 852 853 854
						      (void *)dump_mem->data);
			break;
		default:
			/*
			 * shouldn't get here, we ignored this kind
			 * of TLV earlier during the TLV parsing?!
			 */
			WARN_ON(1);
			success = false;
		}

		if (success)
			dump_data = iwl_fw_error_next_data(dump_data);
855
	}
856 857

	if (smem_len) {
858
		IWL_DEBUG_INFO(fwrt, "WRT SMEM dump\n");
859 860 861 862
		dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_MEM);
		dump_data->len = cpu_to_le32(smem_len + sizeof(*dump_mem));
		dump_mem = (void *)dump_data->data;
		dump_mem->type = cpu_to_le32(IWL_FW_ERROR_DUMP_MEM_SMEM);
863 864 865
		dump_mem->offset = cpu_to_le32(fwrt->trans->cfg->smem_offset);
		iwl_trans_read_mem_bytes(fwrt->trans,
					 fwrt->trans->cfg->smem_offset,
866
					 dump_mem->data, smem_len);
867
		dump_data = iwl_fw_error_next_data(dump_data);
868 869 870
	}

	if (sram2_len) {
871
		IWL_DEBUG_INFO(fwrt, "WRT SRAM dump\n");
872 873 874 875
		dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_MEM);
		dump_data->len = cpu_to_le32(sram2_len + sizeof(*dump_mem));
		dump_mem = (void *)dump_data->data;
		dump_mem->type = cpu_to_le32(IWL_FW_ERROR_DUMP_MEM_SRAM);
876 877 878
		dump_mem->offset = cpu_to_le32(fwrt->trans->cfg->dccm2_offset);
		iwl_trans_read_mem_bytes(fwrt->trans,
					 fwrt->trans->cfg->dccm2_offset,
879
					 dump_mem->data, sram2_len);
880
		dump_data = iwl_fw_error_next_data(dump_data);
881 882 883
	}

	/* Dump fw's virtual image */
884 885 886
	if (!fwrt->trans->cfg->gen2 &&
	    fwrt->fw->img[fwrt->cur_fw_img].paging_mem_size &&
	    fwrt->fw_paging_db[0].fw_paging_block) {
887
		IWL_DEBUG_INFO(fwrt, "WRT paging dump\n");
888
		for (i = 1; i < fwrt->num_of_paging_blk + 1; i++) {
889 890
			struct iwl_fw_error_dump_paging *paging;
			struct page *pages =
891 892
				fwrt->fw_paging_db[i].fw_paging_block;
			dma_addr_t addr = fwrt->fw_paging_db[i].fw_paging_phys;
893 894 895 896 897 898

			dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_PAGING);
			dump_data->len = cpu_to_le32(sizeof(*paging) +
						     PAGING_BLOCK_SIZE);
			paging = (void *)dump_data->data;
			paging->index = cpu_to_le32(i);
899
			dma_sync_single_for_cpu(fwrt->trans->dev, addr,
900 901
						PAGING_BLOCK_SIZE,
						DMA_BIDIRECTIONAL);
902 903
			memcpy(paging->data, page_address(pages),
			       PAGING_BLOCK_SIZE);
904
			dump_data = iwl_fw_error_next_data(dump_data);
905 906 907
		}
	}

908
	if (prph_len) {
909
		iwl_dump_prph(fwrt->trans, &dump_data,
910 911 912
			      iwl_prph_dump_addr_comm,
			      ARRAY_SIZE(iwl_prph_dump_addr_comm));

913 914
		if (fwrt->trans->cfg->mq_rx_supported)
			iwl_dump_prph(fwrt->trans, &dump_data,
915 916 917
				      iwl_prph_dump_addr_9000,
				      ARRAY_SIZE(iwl_prph_dump_addr_9000));
	}
918 919

dump_trans_data:
920 921 922
	fw_error_dump->trans_ptr = iwl_trans_dump_data(fwrt->trans,
						       fwrt->dump.trig);
	fw_error_dump->fwrt_len = file_len;
923 924 925 926
	if (fw_error_dump->trans_ptr)
		file_len += fw_error_dump->trans_ptr->len;
	dump_file->file_len = cpu_to_le32(file_len);

927 928 929 930
	sg_dump_data = alloc_sgtable(file_len);
	if (sg_dump_data) {
		sg_pcopy_from_buffer(sg_dump_data,
				     sg_nents(sg_dump_data),
931 932
				     fw_error_dump->fwrt_ptr,
				     fw_error_dump->fwrt_len, 0);
933 934 935 936 937
		if (fw_error_dump->trans_ptr)
			sg_pcopy_from_buffer(sg_dump_data,
					     sg_nents(sg_dump_data),
					     fw_error_dump->trans_ptr->data,
					     fw_error_dump->trans_ptr->len,
938 939
					     fw_error_dump->fwrt_len);
		dev_coredumpsg(fwrt->trans->dev, sg_dump_data, file_len,
940 941
			       GFP_KERNEL);
	}
942
	vfree(fw_error_dump->fwrt_ptr);
943 944
	vfree(fw_error_dump->trans_ptr);
	kfree(fw_error_dump);
945

946
out:
947 948
	iwl_fw_free_dump_desc(fwrt);
	clear_bit(IWL_FWRT_STATUS_DUMPING, &fwrt->status);
949
	IWL_DEBUG_INFO(fwrt, "WRT dump done\n");
950
}
951
IWL_EXPORT_SYMBOL(iwl_fw_error_dump);
952

953
const struct iwl_fw_dump_desc iwl_dump_desc_assert = {
954 955 956 957
	.trig_desc = {
		.type = cpu_to_le32(FW_DBG_TRIGGER_FW_ASSERT),
	},
};
958
IWL_EXPORT_SYMBOL(iwl_dump_desc_assert);
959

960 961 962
int iwl_fw_dbg_collect_desc(struct iwl_fw_runtime *fwrt,
			    const struct iwl_fw_dump_desc *desc,
			    const struct iwl_fw_dbg_trigger_tlv *trigger)
963 964 965 966 967 968
{
	unsigned int delay = 0;

	if (trigger)
		delay = msecs_to_jiffies(le32_to_cpu(trigger->stop_delay));

969 970 971 972 973 974 975 976 977 978 979 980 981 982
	/*
	 * If the loading of the FW completed successfully, the next step is to
	 * get the SMEM config data. Thus, if fwrt->smem_cfg.num_lmacs is non
	 * zero, the FW was already loaded successully. If the state is "NO_FW"
	 * in such a case - WARN and exit, since FW may be dead. Otherwise, we
	 * can try to collect the data, since FW might just not be fully
	 * loaded (no "ALIVE" yet), and the debug data is accessible.
	 *
	 * Corner case: got the FW alive but crashed before getting the SMEM
	 *	config. In such a case, due to HW access problems, we might
	 *	collect garbage.
	 */
	if (WARN((fwrt->trans->state == IWL_TRANS_NO_FW) &&
		 fwrt->smem_cfg.num_lmacs,
983 984 985
		 "Can't collect dbg data when FW isn't alive\n"))
		return -EIO;

986
	if (test_and_set_bit(IWL_FWRT_STATUS_DUMPING, &fwrt->status))
987 988
		return -EBUSY;

989 990
	if (WARN_ON(fwrt->dump.desc))
		iwl_fw_free_dump_desc(fwrt);
991

992
	IWL_WARN(fwrt, "Collecting data: trigger %d fired.\n",
993 994
		 le32_to_cpu(desc->trig_desc.type));

995 996
	fwrt->dump.desc = desc;
	fwrt->dump.trig = trigger;
997

998
	schedule_delayed_work(&fwrt->dump.wk, delay);
999 1000 1001

	return 0;
}
1002
IWL_EXPORT_SYMBOL(iwl_fw_dbg_collect_desc);
1003

1004 1005 1006 1007
int iwl_fw_dbg_collect(struct iwl_fw_runtime *fwrt,
		       enum iwl_fw_dbg_trigger trig,
		       const char *str, size_t len,
		       const struct iwl_fw_dbg_trigger_tlv *trigger)
1008
{
1009
	struct iwl_fw_dump_desc *desc;
1010 1011 1012 1013 1014 1015 1016 1017 1018

	desc = kzalloc(sizeof(*desc) + len, GFP_ATOMIC);
	if (!desc)
		return -ENOMEM;

	desc->len = len;
	desc->trig_desc.type = cpu_to_le32(trig);
	memcpy(desc->trig_desc.data, str, len);

1019
	return iwl_fw_dbg_collect_desc(fwrt, desc, trigger);
1020
}
1021
IWL_EXPORT_SYMBOL(iwl_fw_dbg_collect);
1022

1023 1024 1025
int iwl_fw_dbg_collect_trig(struct iwl_fw_runtime *fwrt,
			    struct iwl_fw_dbg_trigger_tlv *trigger,
			    const char *fmt, ...)
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
{
	u16 occurrences = le16_to_cpu(trigger->occurrences);
	int ret, len = 0;
	char buf[64];

	if (!occurrences)
		return 0;

	if (fmt) {
		va_list ap;

		buf[sizeof(buf) - 1] = '\0';

		va_start(ap, fmt);
		vsnprintf(buf, sizeof(buf), fmt, ap);
		va_end(ap);

		/* check for truncation */
		if (WARN_ON_ONCE(buf[sizeof(buf) - 1]))
			buf[sizeof(buf) - 1] = '\0';

		len = strlen(buf) + 1;
	}

1050 1051
	ret = iwl_fw_dbg_collect(fwrt, le32_to_cpu(trigger->id), buf, len,
				 trigger);
1052 1053 1054 1055 1056 1057 1058

	if (ret)
		return ret;

	trigger->occurrences = cpu_to_le16(occurrences - 1);
	return 0;
}
1059
IWL_EXPORT_SYMBOL(iwl_fw_dbg_collect_trig);
1060

1061
int iwl_fw_start_dbg_conf(struct iwl_fw_runtime *fwrt, u8 conf_id)
1062 1063 1064 1065 1066
{
	u8 *ptr;
	int ret;
	int i;

1067
	if (WARN_ONCE(conf_id >= ARRAY_SIZE(fwrt->fw->dbg_conf_tlv),
1068 1069 1070 1071
		      "Invalid configuration %d\n", conf_id))
		return -EINVAL;

	/* EARLY START - firmware's configuration is hard coded */
1072 1073
	if ((!fwrt->fw->dbg_conf_tlv[conf_id] ||
	     !fwrt->fw->dbg_conf_tlv[conf_id]->num_of_hcmds) &&
1074
	    conf_id == FW_DBG_START_FROM_ALIVE)
1075 1076
		return 0;

1077
	if (!fwrt->fw->dbg_conf_tlv[conf_id])
1078 1079
		return -EINVAL;

1080 1081 1082
	if (fwrt->dump.conf != FW_DBG_INVALID)
		IWL_WARN(fwrt, "FW already configured (%d) - re-configuring\n",
			 fwrt->dump.conf);
1083

1084 1085 1086
	/* start default config marker cmd for syncing logs */
	iwl_fw_trigger_timestamp(fwrt, 1);

1087
	/* Send all HCMDs for configuring the FW debug */
1088 1089
	ptr = (void *)&fwrt->fw->dbg_conf_tlv[conf_id]->hcmd;
	for (i = 0; i < fwrt->fw->dbg_conf_tlv[conf_id]->num_of_hcmds; i++) {
1090
		struct iwl_fw_dbg_conf_hcmd *cmd = (void *)ptr;
1091 1092 1093 1094 1095
		struct iwl_host_cmd hcmd = {
			.id = cmd->id,
			.len = { le16_to_cpu(cmd->len), },
			.data = { cmd->data, },
		};
1096

1097
		ret = iwl_trans_send_cmd(fwrt->trans, &hcmd);
1098 1099 1100 1101 1102 1103 1104
		if (ret)
			return ret;

		ptr += sizeof(*cmd);
		ptr += le16_to_cpu(cmd->len);
	}

1105
	fwrt->dump.conf = conf_id;
1106 1107

	return 0;
1108
}
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
IWL_EXPORT_SYMBOL(iwl_fw_start_dbg_conf);

void iwl_fw_error_dump_wk(struct work_struct *work)
{
	struct iwl_fw_runtime *fwrt =
		container_of(work, struct iwl_fw_runtime, dump.wk.work);

	if (fwrt->ops && fwrt->ops->dump_start &&
	    fwrt->ops->dump_start(fwrt->ops_ctx))
		return;

1120 1121 1122 1123 1124 1125 1126 1127
	if (fwrt->ops && fwrt->ops->fw_running &&
	    !fwrt->ops->fw_running(fwrt->ops_ctx)) {
		IWL_ERR(fwrt, "Firmware not running - cannot dump error\n");
		iwl_fw_free_dump_desc(fwrt);
		clear_bit(IWL_FWRT_STATUS_DUMPING, &fwrt->status);
		goto out;
	}

1128 1129
	if (fwrt->trans->cfg->device_family == IWL_DEVICE_FAMILY_7000) {
		/* stop recording */
1130
		iwl_fw_dbg_stop_recording(fwrt);
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147

		iwl_fw_error_dump(fwrt);

		/* start recording again if the firmware is not crashed */
		if (!test_bit(STATUS_FW_ERROR, &fwrt->trans->status) &&
		    fwrt->fw->dbg_dest_tlv) {
			iwl_clear_bits_prph(fwrt->trans,
					    MON_BUFF_SAMPLE_CTL, 0x100);
			iwl_clear_bits_prph(fwrt->trans,
					    MON_BUFF_SAMPLE_CTL, 0x1);
			iwl_set_bits_prph(fwrt->trans,
					  MON_BUFF_SAMPLE_CTL, 0x1);
		}
	} else {
		u32 in_sample = iwl_read_prph(fwrt->trans, DBGC_IN_SAMPLE);
		u32 out_ctrl = iwl_read_prph(fwrt->trans, DBGC_OUT_CTRL);

1148
		iwl_fw_dbg_stop_recording(fwrt);
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
		/* wait before we collect the data till the DBGC stop */
		udelay(500);

		iwl_fw_error_dump(fwrt);

		/* start recording again if the firmware is not crashed */
		if (!test_bit(STATUS_FW_ERROR, &fwrt->trans->status) &&
		    fwrt->fw->dbg_dest_tlv) {
			iwl_write_prph(fwrt->trans, DBGC_IN_SAMPLE, in_sample);
			iwl_write_prph(fwrt->trans, DBGC_OUT_CTRL, out_ctrl);
		}
	}
1161
out:
1162 1163 1164 1165
	if (fwrt->ops && fwrt->ops->dump_end)
		fwrt->ops->dump_end(fwrt->ops_ctx);
}