dbg.c 89.4 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 - 2019 Intel Corporation
12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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.
 *
 * The full GNU General Public License is included in this distribution
 * in the file called COPYING.
 *
 * Contact Information:
26
 *  Intel Linux Wireless <linuxwifi@intel.com>
27 28 29 30 31 32
 * 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
33
 * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
34
 * Copyright(c) 2018 - 2019 Intel Corporation
35 36 37 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
 * 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>
65 66 67
#include "iwl-drv.h"
#include "runtime.h"
#include "dbg.h"
68
#include "debugfs.h"
69 70 71 72
#include "iwl-io.h"
#include "iwl-prph.h"
#include "iwl-csr.h"

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
/**
 * 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;
};

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

96 97
	IWL_DEBUG_INFO(fwrt, "WRT radio registers dump\n");

98
	if (!iwl_trans_grab_nic_access(fwrt->trans, &flags))
99 100 101 102 103 104 105 106 107
		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;
108 109
		iwl_write_prph_no_grab(fwrt->trans, RSP_RADIO_CMD, rd_cmd);
		*pos = (u8)iwl_read_prph_no_grab(fwrt->trans, RSP_RADIO_RDDAT);
110 111 112 113 114 115

		pos++;
	}

	*dump_data = iwl_fw_error_next_data(*dump_data);

116
	iwl_trans_release_nic_access(fwrt->trans, &flags);
117 118
}

119 120 121
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)
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
{
	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 =
142
		cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
143 144
						RXF_RD_D_SPACE + offset));
	fifo_hdr->wr_ptr =
145
		cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
146 147
						RXF_RD_WR_PTR + offset));
	fifo_hdr->rd_ptr =
148
		cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
149 150
						RXF_RD_RD_PTR + offset));
	fifo_hdr->fence_ptr =
151
		cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
152 153
						RXF_RD_FENCE_PTR + offset));
	fifo_hdr->fence_mode =
154
		cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
155 156 157
						RXF_SET_FENCE_MODE + offset));

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

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

174 175 176
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)
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
{
	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 =
197
		cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
198 199
						TXF_FIFO_ITEM_CNT + offset));
	fifo_hdr->wr_ptr =
200
		cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
201 202
						TXF_WR_PTR + offset));
	fifo_hdr->rd_ptr =
203
		cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
204 205
						TXF_RD_PTR + offset));
	fifo_hdr->fence_ptr =
206
		cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
207 208
						TXF_FENCE_PTR + offset));
	fifo_hdr->fence_mode =
209
		cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
210 211 212
						TXF_LOCK_FENCE + offset));

	/* Set the TXF_READ_MODIFY_ADDR to TXF_WR_PTR */
213
	iwl_trans_write_prph(fwrt->trans, TXF_READ_MODIFY_ADDR + offset,
214 215 216
			     TXF_WR_PTR + offset);

	/* Dummy-read to advance the read pointer to the head */
217
	iwl_trans_read_prph(fwrt->trans, TXF_READ_MODIFY_DATA + offset);
218 219 220 221

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

S
Sara Sharon 已提交
228 229
static void iwl_fw_dump_rxf(struct iwl_fw_runtime *fwrt,
			    struct iwl_fw_error_dump_data **dump_data)
230
{
231
	struct iwl_fwrt_shared_mem_cfg *cfg = &fwrt->smem_cfg;
232 233
	unsigned long flags;

S
Sara Sharon 已提交
234
	IWL_DEBUG_INFO(fwrt, "WRT RX FIFO dump\n");
235

236
	if (!iwl_trans_grab_nic_access(fwrt->trans, &flags))
237 238
		return;

239
	if (iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_RXF)) {
240 241 242 243 244
		/* Pull RXF1 */
		iwl_fwrt_dump_rxf(fwrt, dump_data,
				  cfg->lmac[0].rxfifo1_size, 0, 0);
		/* Pull RXF2 */
		iwl_fwrt_dump_rxf(fwrt, dump_data, cfg->rxfifo2_size,
245 246
				  RXF_DIFF_FROM_PREV +
				  fwrt->trans->cfg->umac_prph_offset, 1);
247 248 249 250 251
		/* Pull LMAC2 RXF1 */
		if (fwrt->smem_cfg.num_lmacs > 1)
			iwl_fwrt_dump_rxf(fwrt, dump_data,
					  cfg->lmac[1].rxfifo1_size,
					  LMAC2_PRPH_OFFSET, 2);
252
	}
253

S
Sara Sharon 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
	iwl_trans_release_nic_access(fwrt->trans, &flags);
}

static void iwl_fw_dump_txf(struct iwl_fw_runtime *fwrt,
			    struct iwl_fw_error_dump_data **dump_data)
{
	struct iwl_fw_error_dump_fifo *fifo_hdr;
	struct iwl_fwrt_shared_mem_cfg *cfg = &fwrt->smem_cfg;
	u32 *fifo_data;
	u32 fifo_len;
	unsigned long flags;
	int i, j;

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

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

272
	if (iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_TXF)) {
273
		/* Pull TXF data from LMAC1 */
274
		for (i = 0; i < fwrt->smem_cfg.num_txfifo_entries; i++) {
275
			/* Mark the number of TXF we're pulling now */
276
			iwl_trans_write_prph(fwrt->trans, TXF_LARC_NUM, i);
277
			iwl_fwrt_dump_txf(fwrt, dump_data,
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
					  cfg->lmac[0].txfifo_size[i], 0, i);
		}

		/* Pull TXF data from LMAC2 */
		if (fwrt->smem_cfg.num_lmacs > 1) {
			for (i = 0; i < fwrt->smem_cfg.num_txfifo_entries;
			     i++) {
				/* Mark the number of TXF we're pulling now */
				iwl_trans_write_prph(fwrt->trans,
						     TXF_LARC_NUM +
						     LMAC2_PRPH_OFFSET, i);
				iwl_fwrt_dump_txf(fwrt, dump_data,
						  cfg->lmac[1].txfifo_size[i],
						  LMAC2_PRPH_OFFSET,
						  i + cfg->num_txfifo_entries);
			}
294
		}
295 296
	}

297
	if (iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_INTERNAL_TXF) &&
298
	    fw_has_capa(&fwrt->fw->ucode_capa,
299 300 301
			IWL_UCODE_TLV_CAPA_EXTEND_SHARED_MEM_CFG)) {
		/* Pull UMAC internal TXF data from all TXFs */
		for (i = 0;
302
		     i < ARRAY_SIZE(fwrt->smem_cfg.internal_txfifo_size);
303 304 305
		     i++) {
			fifo_hdr = (void *)(*dump_data)->data;
			fifo_data = (void *)fifo_hdr->data;
306
			fifo_len = fwrt->smem_cfg.internal_txfifo_size[i];
307 308 309 310 311 312 313 314 315 316 317 318

			/* 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);
319 320

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

324
			fifo_hdr->available_bytes =
325
				cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
326 327
								TXF_CPU2_FIFO_ITEM_CNT));
			fifo_hdr->wr_ptr =
328
				cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
329 330
								TXF_CPU2_WR_PTR));
			fifo_hdr->rd_ptr =
331
				cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
332 333
								TXF_CPU2_RD_PTR));
			fifo_hdr->fence_ptr =
334
				cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
335 336
								TXF_CPU2_FENCE_PTR));
			fifo_hdr->fence_mode =
337
				cpu_to_le32(iwl_trans_read_prph(fwrt->trans,
338 339 340
								TXF_CPU2_LOCK_FENCE));

			/* Set TXF_CPU2_READ_MODIFY_ADDR to TXF_CPU2_WR_PTR */
341
			iwl_trans_write_prph(fwrt->trans,
342 343 344 345
					     TXF_CPU2_READ_MODIFY_ADDR,
					     TXF_CPU2_WR_PTR);

			/* Dummy-read to advance the read pointer to head */
346
			iwl_trans_read_prph(fwrt->trans,
347 348 349 350 351 352
					    TXF_CPU2_READ_MODIFY_DATA);

			/* Read FIFO */
			fifo_len /= sizeof(u32); /* Size in DWORDS */
			for (j = 0; j < fifo_len; j++)
				fifo_data[j] =
353
					iwl_trans_read_prph(fwrt->trans,
354 355 356 357 358
							    TXF_CPU2_READ_MODIFY_DATA);
			*dump_data = iwl_fw_error_next_data(*dump_data);
		}
	}

359
	iwl_trans_release_nic_access(fwrt->trans, &flags);
360 361 362 363 364
}

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

365
struct iwl_prph_range {
366
	u32 start, end;
367 368 369
};

static const struct iwl_prph_range iwl_prph_dump_addr_comm[] = {
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 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
	{ .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 },
};

466 467 468 469 470 471 472
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 },
};

473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 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
static const struct iwl_prph_range iwl_prph_dump_addr_22000[] = {
	{ .start = 0x00a00000, .end = 0x00a00000 },
	{ .start = 0x00a0000c, .end = 0x00a00024 },
	{ .start = 0x00a0002c, .end = 0x00a00034 },
	{ .start = 0x00a0003c, .end = 0x00a0003c },
	{ .start = 0x00a00410, .end = 0x00a00418 },
	{ .start = 0x00a00420, .end = 0x00a00420 },
	{ .start = 0x00a00428, .end = 0x00a00428 },
	{ .start = 0x00a00430, .end = 0x00a0043c },
	{ .start = 0x00a00444, .end = 0x00a00444 },
	{ .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 = 0x00a01050 },
	{ .start = 0x00a01058, .end = 0x00a01058 },
	{ .start = 0x00a01060, .end = 0x00a01070 },
	{ .start = 0x00a0108c, .end = 0x00a0108c },
	{ .start = 0x00a01c20, .end = 0x00a01c28 },
	{ .start = 0x00a01d10, .end = 0x00a01d10 },
	{ .start = 0x00a01e28, .end = 0x00a01e2c },
	{ .start = 0x00a01e60, .end = 0x00a01e60 },
	{ .start = 0x00a01e80, .end = 0x00a01e80 },
	{ .start = 0x00a01ea0, .end = 0x00a01ea0 },
	{ .start = 0x00a02000, .end = 0x00a0201c },
	{ .start = 0x00a02024, .end = 0x00a02024 },
	{ .start = 0x00a02040, .end = 0x00a02048 },
	{ .start = 0x00a020c0, .end = 0x00a020e0 },
	{ .start = 0x00a02400, .end = 0x00a02404 },
	{ .start = 0x00a0240c, .end = 0x00a02414 },
	{ .start = 0x00a0241c, .end = 0x00a0243c },
	{ .start = 0x00a02448, .end = 0x00a024bc },
	{ .start = 0x00a024c4, .end = 0x00a024cc },
	{ .start = 0x00a02508, .end = 0x00a02508 },
	{ .start = 0x00a02510, .end = 0x00a02514 },
	{ .start = 0x00a0251c, .end = 0x00a0251c },
	{ .start = 0x00a0252c, .end = 0x00a0255c },
	{ .start = 0x00a02564, .end = 0x00a025a0 },
	{ .start = 0x00a025a8, .end = 0x00a025b4 },
	{ .start = 0x00a025c0, .end = 0x00a025c0 },
	{ .start = 0x00a025e8, .end = 0x00a025f4 },
	{ .start = 0x00a02c08, .end = 0x00a02c18 },
	{ .start = 0x00a02c2c, .end = 0x00a02c38 },
	{ .start = 0x00a02c68, .end = 0x00a02c78 },
	{ .start = 0x00a03000, .end = 0x00a03000 },
	{ .start = 0x00a03010, .end = 0x00a03014 },
	{ .start = 0x00a0301c, .end = 0x00a0302c },
	{ .start = 0x00a03034, .end = 0x00a03038 },
	{ .start = 0x00a03040, .end = 0x00a03044 },
	{ .start = 0x00a03060, .end = 0x00a03068 },
	{ .start = 0x00a03070, .end = 0x00a03070 },
	{ .start = 0x00a0307c, .end = 0x00a03084 },
	{ .start = 0x00a0308c, .end = 0x00a03090 },
	{ .start = 0x00a03098, .end = 0x00a03098 },
	{ .start = 0x00a030a0, .end = 0x00a030a0 },
	{ .start = 0x00a030a8, .end = 0x00a030b4 },
	{ .start = 0x00a030bc, .end = 0x00a030c0 },
	{ .start = 0x00a030c8, .end = 0x00a030f4 },
	{ .start = 0x00a03100, .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 = 0x00a04560 },
	{ .start = 0x00a04570, .end = 0x00a0457c },
	{ .start = 0x00a04590, .end = 0x00a04590 },
	{ .start = 0x00a04598, .end = 0x00a04598 },
	{ .start = 0x00a045c0, .end = 0x00a045f4 },
548
	{ .start = 0x00a05c18, .end = 0x00a05c1c },
549 550 551 552 553 554 555 556 557 558 559 560
	{ .start = 0x00a0c000, .end = 0x00a0c018 },
	{ .start = 0x00a0c020, .end = 0x00a0c028 },
	{ .start = 0x00a0c038, .end = 0x00a0c094 },
	{ .start = 0x00a0c0c0, .end = 0x00a0c104 },
	{ .start = 0x00a0c10c, .end = 0x00a0c118 },
	{ .start = 0x00a0c150, .end = 0x00a0c174 },
	{ .start = 0x00a0c17c, .end = 0x00a0c188 },
	{ .start = 0x00a0c190, .end = 0x00a0c198 },
	{ .start = 0x00a0c1a0, .end = 0x00a0c1a8 },
	{ .start = 0x00a0c1b0, .end = 0x00a0c1b8 },
};

561 562
static const struct iwl_prph_range iwl_prph_dump_addr_ax210[] = {
	{ .start = 0x00d03c00, .end = 0x00d03c64 },
563
	{ .start = 0x00d05c18, .end = 0x00d05c1c },
564 565 566
	{ .start = 0x00d0c000, .end = 0x00d0c174 },
};

567 568
static void iwl_read_prph_block(struct iwl_trans *trans, u32 start,
				u32 len_bytes, __le32 *data)
569 570 571 572 573 574 575
{
	u32 i;

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

576
static void iwl_dump_prph(struct iwl_fw_runtime *fwrt,
577
			  const struct iwl_prph_range *iwl_prph_dump_addr,
578
			  u32 range_len, void *ptr)
579 580
{
	struct iwl_fw_error_dump_prph *prph;
581 582 583
	struct iwl_trans *trans = fwrt->trans;
	struct iwl_fw_error_dump_data **data =
		(struct iwl_fw_error_dump_data **)ptr;
584
	unsigned long flags;
585
	u32 i;
586

587 588 589
	if (!data)
		return;

590 591
	IWL_DEBUG_INFO(trans, "WRT PRPH dump\n");

592
	if (!iwl_trans_grab_nic_access(trans, &flags))
593
		return;
594

595
	for (i = 0; i < range_len; i++) {
596 597 598 599 600 601 602 603 604 605
		/* 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);

606 607 608 609 610
		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);
611

D
Dan Carpenter 已提交
612
		*data = iwl_fw_error_next_data(*data);
613 614 615 616 617
	}

	iwl_trans_release_nic_access(trans, &flags);
}

618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
/*
 * 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;
}

655 656 657
static void iwl_fw_get_prph_len(struct iwl_fw_runtime *fwrt,
				const struct iwl_prph_range *iwl_prph_dump_addr,
				u32 range_len, void *ptr)
S
Shahar S Matityahu 已提交
658
{
659 660 661 662 663
	u32 *prph_len = (u32 *)ptr;
	int i, num_bytes_in_chunk;

	if (!prph_len)
		return;
S
Shahar S Matityahu 已提交
664

665
	for (i = 0; i < range_len; i++) {
S
Shahar S Matityahu 已提交
666
		/* The range includes both boundaries */
667 668 669
		num_bytes_in_chunk =
			iwl_prph_dump_addr[i].end -
			iwl_prph_dump_addr[i].start + 4;
S
Shahar S Matityahu 已提交
670

671
		*prph_len += sizeof(struct iwl_fw_error_dump_data) +
S
Shahar S Matityahu 已提交
672 673 674
			sizeof(struct iwl_fw_error_dump_prph) +
			num_bytes_in_chunk;
	}
675
}
S
Shahar S Matityahu 已提交
676

677 678 679 680 681 682 683
static void iwl_fw_prph_handler(struct iwl_fw_runtime *fwrt, void *ptr,
				void (*handler)(struct iwl_fw_runtime *,
						const struct iwl_prph_range *,
						u32, void *))
{
	u32 range_len;

684
	if (fwrt->trans->cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
685 686
		range_len = ARRAY_SIZE(iwl_prph_dump_addr_ax210);
		handler(fwrt, iwl_prph_dump_addr_ax210, range_len, ptr);
687
	} else if (fwrt->trans->cfg->device_family >= IWL_DEVICE_FAMILY_22000) {
688 689 690 691 692 693 694 695 696
		range_len = ARRAY_SIZE(iwl_prph_dump_addr_22000);
		handler(fwrt, iwl_prph_dump_addr_22000, range_len, ptr);
	} else {
		range_len = ARRAY_SIZE(iwl_prph_dump_addr_comm);
		handler(fwrt, iwl_prph_dump_addr_comm, range_len, ptr);

		if (fwrt->trans->cfg->mq_rx_supported) {
			range_len = ARRAY_SIZE(iwl_prph_dump_addr_9000);
			handler(fwrt, iwl_prph_dump_addr_9000, range_len, ptr);
S
Shahar S Matityahu 已提交
697 698 699 700 701 702
		}
	}
}

static void iwl_fw_dump_mem(struct iwl_fw_runtime *fwrt,
			    struct iwl_fw_error_dump_data **dump_data,
703
			    u32 len, u32 ofs, u32 type)
S
Shahar S Matityahu 已提交
704 705 706
{
	struct iwl_fw_error_dump_mem *dump_mem;

707 708
	if (!len)
		return;
S
Shahar S Matityahu 已提交
709

710 711 712 713 714 715 716
	(*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 = cpu_to_le32(type);
	dump_mem->offset = cpu_to_le32(ofs);
	iwl_trans_read_mem_bytes(fwrt->trans, ofs, dump_mem->data, len);
	*dump_data = iwl_fw_error_next_data(*dump_data);
S
Shahar S Matityahu 已提交
717

718
	IWL_DEBUG_INFO(fwrt, "WRT memory dump. Type=%u\n", dump_mem->type);
S
Shahar S Matityahu 已提交
719 720
}

721 722 723 724
#define ADD_LEN(len, item_len, const_len) \
	do {size_t item = item_len; len += (!!item) * const_len + item; } \
	while (0)

S
Sara Sharon 已提交
725 726
static int iwl_fw_rxf_len(struct iwl_fw_runtime *fwrt,
			  struct iwl_fwrt_shared_mem_cfg *mem_cfg)
727 728 729 730 731 732
{
	size_t hdr_len = sizeof(struct iwl_fw_error_dump_data) +
			 sizeof(struct iwl_fw_error_dump_fifo);
	u32 fifo_len = 0;
	int i;

733
	if (!iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_RXF))
S
Sara Sharon 已提交
734
		return 0;
735 736 737 738 739

	/* Count RXF2 size */
	ADD_LEN(fifo_len, mem_cfg->rxfifo2_size, hdr_len);

	/* Count RXF1 sizes */
740 741 742
	if (WARN_ON(mem_cfg->num_lmacs > MAX_NUM_LMAC))
		mem_cfg->num_lmacs = MAX_NUM_LMAC;

743 744 745
	for (i = 0; i < mem_cfg->num_lmacs; i++)
		ADD_LEN(fifo_len, mem_cfg->lmac[i].rxfifo1_size, hdr_len);

S
Sara Sharon 已提交
746 747 748 749 750 751 752 753 754 755 756
	return fifo_len;
}

static int iwl_fw_txf_len(struct iwl_fw_runtime *fwrt,
			  struct iwl_fwrt_shared_mem_cfg *mem_cfg)
{
	size_t hdr_len = sizeof(struct iwl_fw_error_dump_data) +
			 sizeof(struct iwl_fw_error_dump_fifo);
	u32 fifo_len = 0;
	int i;

757
	if (!iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_TXF))
758 759 760
		goto dump_internal_txf;

	/* Count TXF sizes */
761 762 763
	if (WARN_ON(mem_cfg->num_lmacs > MAX_NUM_LMAC))
		mem_cfg->num_lmacs = MAX_NUM_LMAC;

764 765 766 767 768 769 770 771 772
	for (i = 0; i < mem_cfg->num_lmacs; i++) {
		int j;

		for (j = 0; j < mem_cfg->num_txfifo_entries; j++)
			ADD_LEN(fifo_len, mem_cfg->lmac[i].txfifo_size[j],
				hdr_len);
	}

dump_internal_txf:
773
	if (!(iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_INTERNAL_TXF) &&
774 775 776 777 778 779 780 781 782 783 784
	      fw_has_capa(&fwrt->fw->ucode_capa,
			  IWL_UCODE_TLV_CAPA_EXTEND_SHARED_MEM_CFG)))
		goto out;

	for (i = 0; i < ARRAY_SIZE(mem_cfg->internal_txfifo_size); i++)
		ADD_LEN(fifo_len, mem_cfg->internal_txfifo_size[i], hdr_len);

out:
	return fifo_len;
}

785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
static void iwl_dump_paging(struct iwl_fw_runtime *fwrt,
			    struct iwl_fw_error_dump_data **data)
{
	int i;

	IWL_DEBUG_INFO(fwrt, "WRT paging dump\n");
	for (i = 1; i < fwrt->num_of_paging_blk + 1; i++) {
		struct iwl_fw_error_dump_paging *paging;
		struct page *pages =
			fwrt->fw_paging_db[i].fw_paging_block;
		dma_addr_t addr = fwrt->fw_paging_db[i].fw_paging_phys;

		(*data)->type = cpu_to_le32(IWL_FW_ERROR_DUMP_PAGING);
		(*data)->len = cpu_to_le32(sizeof(*paging) +
					     PAGING_BLOCK_SIZE);
		paging =  (void *)(*data)->data;
		paging->index = cpu_to_le32(i);
		dma_sync_single_for_cpu(fwrt->trans->dev, addr,
					PAGING_BLOCK_SIZE,
					DMA_BIDIRECTIONAL);
		memcpy(paging->data, page_address(pages),
		       PAGING_BLOCK_SIZE);
807 808 809
		dma_sync_single_for_device(fwrt->trans->dev, addr,
					   PAGING_BLOCK_SIZE,
					   DMA_BIDIRECTIONAL);
810 811 812 813
		(*data) = iwl_fw_error_next_data(*data);
	}
}

814
static struct iwl_fw_error_dump_file *
815 816
iwl_fw_error_dump_file(struct iwl_fw_runtime *fwrt,
		       struct iwl_fw_dump_ptrs *fw_error_dump)
817 818 819 820
{
	struct iwl_fw_error_dump_file *dump_file;
	struct iwl_fw_error_dump_data *dump_data;
	struct iwl_fw_error_dump_info *dump_info;
821
	struct iwl_fw_error_dump_smem_cfg *dump_smem_cfg;
822 823
	struct iwl_fw_error_dump_trigger_desc *dump_trig;
	u32 sram_len, sram_ofs;
824
	const struct iwl_fw_dbg_mem_seg_tlv *fw_mem = fwrt->fw->dbg.mem_tlv;
825
	struct iwl_fwrt_shared_mem_cfg *mem_cfg = &fwrt->smem_cfg;
826
	u32 file_len, fifo_len = 0, prph_len = 0, radio_len = 0;
827 828
	u32 smem_len = fwrt->fw->dbg.n_mem_tlv ? 0 : fwrt->trans->cfg->smem_len;
	u32 sram2_len = fwrt->fw->dbg.n_mem_tlv ?
829
				0 : fwrt->trans->cfg->dccm2_len;
830 831 832
	int i;

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

L
Liad Kaufman 已提交
836 837
		if (fwrt->cur_fw_img >= IWL_UCODE_TYPE_MAX)
			return NULL;
838
		img = &fwrt->fw->img[fwrt->cur_fw_img];
839 840 841
		sram_ofs = img->sec[IWL_UCODE_SECTION_DATA].offset;
		sram_len = img->sec[IWL_UCODE_SECTION_DATA].len;
	} else {
842 843
		sram_ofs = fwrt->trans->cfg->dccm_offset;
		sram_len = fwrt->trans->cfg->dccm_len;
844 845 846
	}

	/* reading RXF/TXF sizes */
847
	if (test_bit(STATUS_FW_ERROR, &fwrt->trans->status)) {
S
Sara Sharon 已提交
848 849
		fifo_len = iwl_fw_rxf_len(fwrt, mem_cfg);
		fifo_len += iwl_fw_txf_len(fwrt, mem_cfg);
850

851
		/* Make room for PRPH registers */
852 853 854
		if (iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_PRPH))
			iwl_fw_prph_handler(fwrt, &prph_len,
					    iwl_fw_get_prph_len);
855

856
		if (fwrt->trans->cfg->device_family == IWL_DEVICE_FAMILY_7000 &&
857
		    iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_RADIO_REG))
858
			radio_len = sizeof(*dump_data) + RADIO_REG_MAX_READ;
859 860
	}

861
	file_len = sizeof(*dump_file) + fifo_len + prph_len + radio_len;
862

863
	if (iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_DEV_FW_INFO))
864
		file_len += sizeof(*dump_data) + sizeof(*dump_info);
865
	if (iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_MEM_CFG))
866 867
		file_len += sizeof(*dump_data) + sizeof(*dump_smem_cfg);

868
	if (iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_MEM)) {
869 870 871 872 873 874 875 876 877 878 879 880 881
		size_t hdr_len = sizeof(*dump_data) +
				 sizeof(struct iwl_fw_error_dump_mem);

		/* Dump SRAM only if no mem_tlvs */
		if (!fwrt->fw->dbg.n_mem_tlv)
			ADD_LEN(file_len, sram_len, hdr_len);

		/* Make room for all mem types that exist */
		ADD_LEN(file_len, smem_len, hdr_len);
		ADD_LEN(file_len, sram2_len, hdr_len);

		for (i = 0; i < fwrt->fw->dbg.n_mem_tlv; i++)
			ADD_LEN(file_len, le32_to_cpu(fw_mem[i].len), hdr_len);
882 883
	}

884
	/* Make room for fw's virtual image pages, if it exists */
885
	if (iwl_fw_dbg_is_paging_enabled(fwrt))
886
		file_len += fwrt->num_of_paging_blk *
887 888 889 890
			(sizeof(*dump_data) +
			 sizeof(struct iwl_fw_error_dump_paging) +
			 PAGING_BLOCK_SIZE);

891 892 893 894 895
	if (iwl_fw_dbg_is_d3_debug_enabled(fwrt) && fwrt->dump.d3_debug_data) {
		file_len += sizeof(*dump_data) +
			fwrt->trans->cfg->d3_debug_data_length * 2;
	}

896
	/* If we only want a monitor dump, reset the file length */
897
	if (fwrt->dump.monitor_only) {
898 899
		file_len = sizeof(*dump_file) + sizeof(*dump_data) * 2 +
			   sizeof(*dump_info) + sizeof(*dump_smem_cfg);
900 901
	}

902
	if (iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_ERROR_INFO) &&
903
	    fwrt->dump.desc)
904
		file_len += sizeof(*dump_data) + sizeof(*dump_trig) +
905
			    fwrt->dump.desc->len;
906 907

	dump_file = vzalloc(file_len);
908 909
	if (!dump_file)
		return NULL;
910

911
	fw_error_dump->fwrt_ptr = dump_file;
912 913 914 915

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

916
	if (iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_DEV_FW_INFO)) {
917 918 919
		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;
920 921
		dump_info->hw_type =
			cpu_to_le32(CSR_HW_REV_TYPE(fwrt->trans->hw_rev));
922 923 924 925 926 927 928 929
		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,
		       sizeof(dump_info->fw_human_readable));
		strncpy(dump_info->dev_human_readable, fwrt->trans->cfg->name,
			sizeof(dump_info->dev_human_readable) - 1);
		strncpy(dump_info->bus_human_readable, fwrt->dev->bus->name,
			sizeof(dump_info->bus_human_readable) - 1);
930 931 932 933 934 935 936
		dump_info->num_of_lmacs = fwrt->smem_cfg.num_lmacs;
		dump_info->lmac_err_id[0] =
			cpu_to_le32(fwrt->dump.lmac_err_id[0]);
		if (fwrt->smem_cfg.num_lmacs > 1)
			dump_info->lmac_err_id[1] =
				cpu_to_le32(fwrt->dump.lmac_err_id[1]);
		dump_info->umac_err_id = cpu_to_le32(fwrt->dump.umac_err_id);
937 938

		dump_data = iwl_fw_error_next_data(dump_data);
939 940
	}

941
	if (iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_MEM_CFG)) {
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
		/* 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;
			u32 *txf_size = mem_cfg->lmac[i].txfifo_size;

			for (j = 0; j < TX_FIFO_MAX_NUM; j++)
				dump_smem_cfg->lmac[i].txfifo_size[j] =
					cpu_to_le32(txf_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);
	}
970

971
	/* We only dump the FIFOs if the FW is in error state */
972
	if (fifo_len) {
S
Sara Sharon 已提交
973 974
		iwl_fw_dump_rxf(fwrt, &dump_data);
		iwl_fw_dump_txf(fwrt, &dump_data);
975
	}
976

977 978 979
	if (radio_len)
		iwl_read_radio_regs(fwrt, &dump_data);

980
	if (iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_ERROR_INFO) &&
981
	    fwrt->dump.desc) {
982 983
		dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_ERROR_INFO);
		dump_data->len = cpu_to_le32(sizeof(*dump_trig) +
984
					     fwrt->dump.desc->len);
985
		dump_trig = (void *)dump_data->data;
986 987
		memcpy(dump_trig, &fwrt->dump.desc->trig_desc,
		       sizeof(*dump_trig) + fwrt->dump.desc->len);
988 989 990 991 992

		dump_data = iwl_fw_error_next_data(dump_data);
	}

	/* In case we only want monitor dump, skip to dump trasport data */
993
	if (fwrt->dump.monitor_only)
994
		goto out;
995

996
	if (iwl_fw_dbg_type_on(fwrt, IWL_FW_ERROR_DUMP_MEM)) {
997 998 999 1000 1001 1002 1003 1004 1005 1006
		const struct iwl_fw_dbg_mem_seg_tlv *fw_dbg_mem =
			fwrt->fw->dbg.mem_tlv;

		if (!fwrt->fw->dbg.n_mem_tlv)
			iwl_fw_dump_mem(fwrt, &dump_data, sram_len, sram_ofs,
					IWL_FW_ERROR_DUMP_MEM_SRAM);

		for (i = 0; i < fwrt->fw->dbg.n_mem_tlv; i++) {
			u32 len = le32_to_cpu(fw_dbg_mem[i].len);
			u32 ofs = le32_to_cpu(fw_dbg_mem[i].ofs);
S
Shahar S Matityahu 已提交
1007

1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
			iwl_fw_dump_mem(fwrt, &dump_data, len, ofs,
					le32_to_cpu(fw_dbg_mem[i].data_type));
		}

		iwl_fw_dump_mem(fwrt, &dump_data, smem_len,
				fwrt->trans->cfg->smem_offset,
				IWL_FW_ERROR_DUMP_MEM_SMEM);

		iwl_fw_dump_mem(fwrt, &dump_data, sram2_len,
				fwrt->trans->cfg->dccm2_offset,
				IWL_FW_ERROR_DUMP_MEM_SRAM);
	}
1020

1021 1022 1023 1024 1025 1026 1027
	if (iwl_fw_dbg_is_d3_debug_enabled(fwrt) && fwrt->dump.d3_debug_data) {
		u32 addr = fwrt->trans->cfg->d3_debug_data_base_addr;
		size_t data_size = fwrt->trans->cfg->d3_debug_data_length;

		dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_D3_DEBUG_DATA);
		dump_data->len = cpu_to_le32(data_size * 2);

S
Shahar S Matityahu 已提交
1028
		memcpy(dump_data->data, fwrt->dump.d3_debug_data, data_size);
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039

		kfree(fwrt->dump.d3_debug_data);
		fwrt->dump.d3_debug_data = NULL;

		iwl_trans_read_mem_bytes(fwrt->trans, addr,
					 dump_data->data + data_size,
					 data_size);

		dump_data = iwl_fw_error_next_data(dump_data);
	}

1040
	/* Dump fw's virtual image */
1041 1042
	if (iwl_fw_dbg_is_paging_enabled(fwrt))
		iwl_dump_paging(fwrt, &dump_data);
1043

1044 1045
	if (prph_len)
		iwl_fw_prph_handler(fwrt, &dump_data, iwl_dump_prph);
1046

1047 1048 1049 1050 1051
out:
	dump_file->file_len = cpu_to_le32(file_len);
	return dump_file;
}

1052 1053
static int iwl_dump_ini_prph_iter(struct iwl_fw_runtime *fwrt,
				  struct iwl_fw_ini_region_cfg *reg,
1054
				  void *range_ptr, int idx)
1055
{
1056
	struct iwl_fw_ini_error_dump_range *range = range_ptr;
1057
	__le32 *val = range->data;
1058 1059
	u32 prph_val;
	u32 addr = le32_to_cpu(reg->start_addr[idx]) + le32_to_cpu(reg->offset);
1060 1061
	int i;

1062
	range->internal_base_addr = cpu_to_le32(addr);
1063 1064
	range->range_data_size = reg->internal.range_data_size;
	for (i = 0; i < le32_to_cpu(reg->internal.range_data_size); i += 4) {
1065
		prph_val = iwl_read_prph(fwrt->trans, addr + i);
1066
		if (prph_val == 0x5a5a5a5a)
1067
			return -EBUSY;
1068 1069
		*val++ = cpu_to_le32(prph_val);
	}
1070 1071

	return sizeof(*range) + le32_to_cpu(range->range_data_size);
1072 1073
}

1074 1075
static int iwl_dump_ini_csr_iter(struct iwl_fw_runtime *fwrt,
				 struct iwl_fw_ini_region_cfg *reg,
1076
				 void *range_ptr, int idx)
1077
{
1078
	struct iwl_fw_ini_error_dump_range *range = range_ptr;
1079
	__le32 *val = range->data;
1080
	u32 addr = le32_to_cpu(reg->start_addr[idx]) + le32_to_cpu(reg->offset);
1081 1082
	int i;

1083
	range->internal_base_addr = cpu_to_le32(addr);
1084
	range->range_data_size = reg->internal.range_data_size;
1085 1086
	for (i = 0; i < le32_to_cpu(reg->internal.range_data_size); i += 4)
		*val++ = cpu_to_le32(iwl_trans_read32(fwrt->trans, addr + i));
1087 1088

	return sizeof(*range) + le32_to_cpu(range->range_data_size);
1089 1090
}

1091 1092
static int iwl_dump_ini_dev_mem_iter(struct iwl_fw_runtime *fwrt,
				     struct iwl_fw_ini_region_cfg *reg,
1093
				     void *range_ptr, int idx)
1094
{
1095
	struct iwl_fw_ini_error_dump_range *range = range_ptr;
1096
	u32 addr = le32_to_cpu(reg->start_addr[idx]) + le32_to_cpu(reg->offset);
1097

1098
	range->internal_base_addr = cpu_to_le32(addr);
1099
	range->range_data_size = reg->internal.range_data_size;
1100
	iwl_trans_read_mem_bytes(fwrt->trans, addr, range->data,
1101
				 le32_to_cpu(reg->internal.range_data_size));
1102 1103

	return sizeof(*range) + le32_to_cpu(range->range_data_size);
1104 1105
}

1106 1107 1108
static int
iwl_dump_ini_paging_gen2_iter(struct iwl_fw_runtime *fwrt,
			      struct iwl_fw_ini_region_cfg *reg,
1109
			      void *range_ptr, int idx)
1110
{
1111
	struct iwl_fw_ini_error_dump_range *range = range_ptr;
1112 1113
	u32 page_size = fwrt->trans->init_dram.paging[idx].size;

1114
	range->page_num = cpu_to_le32(idx);
1115 1116 1117
	range->range_data_size = cpu_to_le32(page_size);
	memcpy(range->data, fwrt->trans->init_dram.paging[idx].block,
	       page_size);
1118 1119

	return sizeof(*range) + le32_to_cpu(range->range_data_size);
1120 1121 1122 1123
}

static int iwl_dump_ini_paging_iter(struct iwl_fw_runtime *fwrt,
				    struct iwl_fw_ini_region_cfg *reg,
1124
				    void *range_ptr, int idx)
1125 1126 1127 1128 1129
{
	/* increase idx by 1 since the pages are from 1 to
	 * fwrt->num_of_paging_blk + 1
	 */
	struct page *page = fwrt->fw_paging_db[++idx].fw_paging_block;
1130
	struct iwl_fw_ini_error_dump_range *range = range_ptr;
1131 1132 1133
	dma_addr_t addr = fwrt->fw_paging_db[idx].fw_paging_phys;
	u32 page_size = fwrt->fw_paging_db[idx].fw_paging_size;

1134
	range->page_num = cpu_to_le32(idx);
1135 1136 1137 1138 1139 1140
	range->range_data_size = cpu_to_le32(page_size);
	dma_sync_single_for_cpu(fwrt->trans->dev, addr,	page_size,
				DMA_BIDIRECTIONAL);
	memcpy(range->data, page_address(page), page_size);
	dma_sync_single_for_device(fwrt->trans->dev, addr, page_size,
				   DMA_BIDIRECTIONAL);
1141 1142

	return sizeof(*range) + le32_to_cpu(range->range_data_size);
1143 1144
}

1145 1146
static int
iwl_dump_ini_mon_dram_iter(struct iwl_fw_runtime *fwrt,
1147
			   struct iwl_fw_ini_region_cfg *reg, void *range_ptr,
1148 1149
			   int idx)
{
1150
	struct iwl_fw_ini_error_dump_range *range = range_ptr;
1151 1152
	u32 start_addr = iwl_read_umac_prph(fwrt->trans,
					    MON_BUFF_BASE_ADDR_VER2);
1153 1154

	if (start_addr == 0x5a5a5a5a)
1155
		return -EBUSY;
1156

1157
	range->dram_base_addr = cpu_to_le64(start_addr);
1158
	range->range_data_size = cpu_to_le32(fwrt->trans->dbg.fw_mon[idx].size);
1159

1160 1161
	memcpy(range->data, fwrt->trans->dbg.fw_mon[idx].block,
	       fwrt->trans->dbg.fw_mon[idx].size);
1162

1163
	return sizeof(*range) + le32_to_cpu(range->range_data_size);
1164 1165
}

1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
struct iwl_ini_txf_iter_data {
	int fifo;
	int lmac;
	u32 fifo_size;
	bool internal_txf;
	bool init;
};

static bool iwl_ini_txf_iter(struct iwl_fw_runtime *fwrt,
			     struct iwl_fw_ini_region_cfg *reg)
{
	struct iwl_ini_txf_iter_data *iter = fwrt->dump.fifo_iter;
	struct iwl_fwrt_shared_mem_cfg *cfg = &fwrt->smem_cfg;
	int txf_num = cfg->num_txfifo_entries;
	int int_txf_num = ARRAY_SIZE(cfg->internal_txfifo_size);
	u32 lmac_bitmap = le32_to_cpu(reg->fifos.fid1);

	if (!iter)
		return false;

	if (iter->init) {
		if (le32_to_cpu(reg->offset) &&
		    WARN_ONCE(cfg->num_lmacs == 1,
			      "Invalid lmac offset: 0x%x\n",
			      le32_to_cpu(reg->offset)))
			return false;

		iter->init = false;
		iter->internal_txf = false;
		iter->fifo_size = 0;
		iter->fifo = -1;
		if (le32_to_cpu(reg->offset))
			iter->lmac = 1;
		else
			iter->lmac = 0;
	}

	if (!iter->internal_txf)
		for (iter->fifo++; iter->fifo < txf_num; iter->fifo++) {
			iter->fifo_size =
				cfg->lmac[iter->lmac].txfifo_size[iter->fifo];
			if (iter->fifo_size && (lmac_bitmap & BIT(iter->fifo)))
				return true;
		}

	iter->internal_txf = true;

	if (!fw_has_capa(&fwrt->fw->ucode_capa,
			 IWL_UCODE_TLV_CAPA_EXTEND_SHARED_MEM_CFG))
		return false;

	for (iter->fifo++; iter->fifo < int_txf_num + txf_num; iter->fifo++) {
		iter->fifo_size =
			cfg->internal_txfifo_size[iter->fifo - txf_num];
		if (iter->fifo_size && (lmac_bitmap & BIT(iter->fifo)))
			return true;
	}

	return false;
}

static int iwl_dump_ini_txf_iter(struct iwl_fw_runtime *fwrt,
				 struct iwl_fw_ini_region_cfg *reg,
				 void *range_ptr, int idx)
{
1231
	struct iwl_fw_ini_error_dump_range *range = range_ptr;
1232
	struct iwl_ini_txf_iter_data *iter;
1233
	struct iwl_fw_ini_error_dump_register *reg_dump = (void *)range->data;
1234 1235
	u32 offs = le32_to_cpu(reg->offset), addr;
	u32 registers_size =
1236 1237
		le32_to_cpu(reg->fifos.num_of_registers) * sizeof(*reg_dump);
	__le32 *data;
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
	unsigned long flags;
	int i;

	if (!iwl_ini_txf_iter(fwrt, reg))
		return -EIO;

	if (!iwl_trans_grab_nic_access(fwrt->trans, &flags))
		return -EBUSY;

	iter = fwrt->dump.fifo_iter;

1249 1250
	range->fifo_hdr.fifo_num = cpu_to_le32(iter->fifo);
	range->fifo_hdr.num_of_registers = reg->fifos.num_of_registers;
1251 1252 1253 1254
	range->range_data_size = cpu_to_le32(iter->fifo_size + registers_size);

	iwl_write_prph_no_grab(fwrt->trans, TXF_LARC_NUM + offs, iter->fifo);

1255 1256 1257 1258
	/*
	 * read txf registers. for each register, write to the dump the
	 * register address and its value
	 */
1259 1260 1261
	for (i = 0; i < le32_to_cpu(reg->fifos.num_of_registers); i++) {
		addr = le32_to_cpu(reg->start_addr[i]) + offs;

1262 1263 1264 1265 1266
		reg_dump->addr = cpu_to_le32(addr);
		reg_dump->data = cpu_to_le32(iwl_read_prph_no_grab(fwrt->trans,
								   addr));

		reg_dump++;
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
	}

	if (reg->fifos.header_only) {
		range->range_data_size = cpu_to_le32(registers_size);
		goto out;
	}

	/* Set the TXF_READ_MODIFY_ADDR to TXF_WR_PTR */
	iwl_write_prph_no_grab(fwrt->trans, TXF_READ_MODIFY_ADDR + offs,
			       TXF_WR_PTR + offs);

	/* Dummy-read to advance the read pointer to the head */
	iwl_read_prph_no_grab(fwrt->trans, TXF_READ_MODIFY_DATA + offs);

	/* Read FIFO */
	addr = TXF_READ_MODIFY_DATA + offs;
1283 1284 1285
	data = (void *)reg_dump;
	for (i = 0; i < iter->fifo_size; i += sizeof(*data))
		*data++ = cpu_to_le32(iwl_read_prph_no_grab(fwrt->trans, addr));
1286 1287 1288 1289 1290 1291 1292

out:
	iwl_trans_release_nic_access(fwrt->trans, &flags);

	return sizeof(*range) + le32_to_cpu(range->range_data_size);
}

1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
struct iwl_ini_rxf_data {
	u32 fifo_num;
	u32 size;
	u32 offset;
};

static void iwl_ini_get_rxf_data(struct iwl_fw_runtime *fwrt,
				 struct iwl_fw_ini_region_cfg *reg,
				 struct iwl_ini_rxf_data *data)
{
	u32 fid1 = le32_to_cpu(reg->fifos.fid1);
	u32 fid2 = le32_to_cpu(reg->fifos.fid2);
	u32 fifo_idx;

	if (!data)
		return;

	memset(data, 0, sizeof(*data));

	if (WARN_ON_ONCE((fid1 && fid2) || (!fid1 && !fid2)))
		return;

	fifo_idx = ffs(fid1) - 1;
	if (fid1 && !WARN_ON_ONCE((~BIT(fifo_idx) & fid1) ||
				  fifo_idx >= MAX_NUM_LMAC)) {
		data->size = fwrt->smem_cfg.lmac[fifo_idx].rxfifo1_size;
		data->fifo_num = fifo_idx;
		return;
	}

	fifo_idx = ffs(fid2) - 1;
	if (fid2 && !WARN_ON_ONCE(fifo_idx != 0)) {
		data->size = fwrt->smem_cfg.rxfifo2_size;
		data->offset = RXF_DIFF_FROM_PREV;
		/* use bit 31 to distinguish between umac and lmac rxf while
		 * parsing the dump
		 */
		data->fifo_num = fifo_idx | IWL_RXF_UMAC_BIT;
		return;
	}
}

static int iwl_dump_ini_rxf_iter(struct iwl_fw_runtime *fwrt,
				 struct iwl_fw_ini_region_cfg *reg,
				 void *range_ptr, int idx)
{
1339
	struct iwl_fw_ini_error_dump_range *range = range_ptr;
1340
	struct iwl_ini_rxf_data rxf_data;
1341
	struct iwl_fw_ini_error_dump_register *reg_dump = (void *)range->data;
1342 1343
	u32 offs = le32_to_cpu(reg->offset), addr;
	u32 registers_size =
1344 1345
		le32_to_cpu(reg->fifos.num_of_registers) * sizeof(*reg_dump);
	__le32 *data;
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
	unsigned long flags;
	int i;

	iwl_ini_get_rxf_data(fwrt, reg, &rxf_data);
	if (!rxf_data.size)
		return -EIO;

	if (!iwl_trans_grab_nic_access(fwrt->trans, &flags))
		return -EBUSY;

1356 1357
	range->fifo_hdr.fifo_num = cpu_to_le32(rxf_data.fifo_num);
	range->fifo_hdr.num_of_registers = reg->fifos.num_of_registers;
1358 1359
	range->range_data_size = cpu_to_le32(rxf_data.size + registers_size);

1360 1361 1362 1363
	/*
	 * read rxf registers. for each register, write to the dump the
	 * register address and its value
	 */
1364 1365 1366
	for (i = 0; i < le32_to_cpu(reg->fifos.num_of_registers); i++) {
		addr = le32_to_cpu(reg->start_addr[i]) + offs;

1367 1368 1369 1370 1371
		reg_dump->addr = cpu_to_le32(addr);
		reg_dump->data = cpu_to_le32(iwl_read_prph_no_grab(fwrt->trans,
								   addr));

		reg_dump++;
1372 1373 1374 1375 1376 1377 1378
	}

	if (reg->fifos.header_only) {
		range->range_data_size = cpu_to_le32(registers_size);
		goto out;
	}

1379 1380 1381 1382 1383 1384
	/*
	 * region register have absolute value so apply rxf offset after
	 * reading the registers
	 */
	offs += rxf_data.offset;

1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
	/* Lock fence */
	iwl_write_prph_no_grab(fwrt->trans, RXF_SET_FENCE_MODE + offs, 0x1);
	/* Set fence pointer to the same place like WR pointer */
	iwl_write_prph_no_grab(fwrt->trans, RXF_LD_WR2FENCE + offs, 0x1);
	/* Set fence offset */
	iwl_write_prph_no_grab(fwrt->trans, RXF_LD_FENCE_OFFSET_ADDR + offs,
			       0x0);

	/* Read FIFO */
	addr =  RXF_FIFO_RD_FENCE_INC + offs;
1395 1396 1397
	data = (void *)reg_dump;
	for (i = 0; i < rxf_data.size; i += sizeof(*data))
		*data++ = cpu_to_le32(iwl_read_prph_no_grab(fwrt->trans, addr));
1398 1399 1400 1401 1402 1403 1404

out:
	iwl_trans_release_nic_access(fwrt->trans, &flags);

	return sizeof(*range) + le32_to_cpu(range->range_data_size);
}

1405 1406 1407
static void *iwl_dump_ini_mem_fill_header(struct iwl_fw_runtime *fwrt,
					  struct iwl_fw_ini_region_cfg *reg,
					  void *data)
1408 1409 1410
{
	struct iwl_fw_ini_error_dump *dump = data;

1411
	dump->header.version = cpu_to_le32(IWL_INI_DUMP_VER);
1412

1413 1414 1415
	return dump->ranges;
}

1416
static void
1417 1418 1419 1420 1421
*iwl_dump_ini_mon_fill_header(struct iwl_fw_runtime *fwrt,
			      struct iwl_fw_ini_region_cfg *reg,
			      struct iwl_fw_ini_monitor_dump *data,
			      u32 write_ptr_addr, u32 write_ptr_msk,
			      u32 cycle_cnt_addr, u32 cycle_cnt_msk)
1422 1423 1424 1425 1426
{
	u32 write_ptr, cycle_cnt;
	unsigned long flags;

	if (!iwl_trans_grab_nic_access(fwrt->trans, &flags)) {
1427
		IWL_ERR(fwrt, "Failed to get monitor header\n");
1428 1429
		return NULL;
	}
1430 1431 1432 1433

	write_ptr = iwl_read_prph_no_grab(fwrt->trans, write_ptr_addr);
	cycle_cnt = iwl_read_prph_no_grab(fwrt->trans, cycle_cnt_addr);

1434 1435
	iwl_trans_release_nic_access(fwrt->trans, &flags);

1436
	data->header.version = cpu_to_le32(IWL_INI_DUMP_VER);
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
	data->write_ptr = cpu_to_le32(write_ptr & write_ptr_msk);
	data->cycle_cnt = cpu_to_le32(cycle_cnt & cycle_cnt_msk);

	return data->ranges;
}

static void
*iwl_dump_ini_mon_dram_fill_header(struct iwl_fw_runtime *fwrt,
				   struct iwl_fw_ini_region_cfg *reg,
				   void *data)
{
	struct iwl_fw_ini_monitor_dump *mon_dump = (void *)data;
	u32 write_ptr_addr, write_ptr_msk, cycle_cnt_addr, cycle_cnt_msk;

	switch (fwrt->trans->cfg->device_family) {
	case IWL_DEVICE_FAMILY_9000:
	case IWL_DEVICE_FAMILY_22000:
		write_ptr_addr = MON_BUFF_WRPTR_VER2;
		write_ptr_msk = -1;
		cycle_cnt_addr = MON_BUFF_CYCLE_CNT_VER2;
		cycle_cnt_msk = -1;
		break;
	default:
		IWL_ERR(fwrt, "Unsupported device family %d\n",
			fwrt->trans->cfg->device_family);
		return NULL;
	}

	return iwl_dump_ini_mon_fill_header(fwrt, reg, mon_dump, write_ptr_addr,
					    write_ptr_msk, cycle_cnt_addr,
					    cycle_cnt_msk);
}

static void
*iwl_dump_ini_mon_smem_fill_header(struct iwl_fw_runtime *fwrt,
				   struct iwl_fw_ini_region_cfg *reg,
				   void *data)
{
	struct iwl_fw_ini_monitor_dump *mon_dump = (void *)data;
	const struct iwl_cfg *cfg = fwrt->trans->cfg;

	if (fwrt->trans->cfg->device_family != IWL_DEVICE_FAMILY_9000 &&
	    fwrt->trans->cfg->device_family != IWL_DEVICE_FAMILY_22000) {
		IWL_ERR(fwrt, "Unsupported device family %d\n",
			fwrt->trans->cfg->device_family);
		return NULL;
	}

	return iwl_dump_ini_mon_fill_header(fwrt, reg, mon_dump,
					    cfg->fw_mon_smem_write_ptr_addr,
					    cfg->fw_mon_smem_write_ptr_msk,
					    cfg->fw_mon_smem_cycle_cnt_ptr_addr,
					    cfg->fw_mon_smem_cycle_cnt_ptr_msk);
1490 1491 1492

}

1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
static u32 iwl_dump_ini_mem_ranges(struct iwl_fw_runtime *fwrt,
				   struct iwl_fw_ini_region_cfg *reg)
{
	return le32_to_cpu(reg->internal.num_of_ranges);
}

static u32 iwl_dump_ini_paging_gen2_ranges(struct iwl_fw_runtime *fwrt,
					   struct iwl_fw_ini_region_cfg *reg)
{
	return fwrt->trans->init_dram.paging_cnt;
}

static u32 iwl_dump_ini_paging_ranges(struct iwl_fw_runtime *fwrt,
				      struct iwl_fw_ini_region_cfg *reg)
{
	return fwrt->num_of_paging_blk;
}

static u32 iwl_dump_ini_mon_dram_ranges(struct iwl_fw_runtime *fwrt,
					struct iwl_fw_ini_region_cfg *reg)
{
	return 1;
}

1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
static u32 iwl_dump_ini_txf_ranges(struct iwl_fw_runtime *fwrt,
				   struct iwl_fw_ini_region_cfg *reg)
{
	struct iwl_ini_txf_iter_data iter = { .init = true };
	void *fifo_iter = fwrt->dump.fifo_iter;
	u32 num_of_fifos = 0;

	fwrt->dump.fifo_iter = &iter;
	while (iwl_ini_txf_iter(fwrt, reg))
		num_of_fifos++;

	fwrt->dump.fifo_iter = fifo_iter;

	return num_of_fifos;
}

1533 1534 1535 1536 1537 1538 1539 1540 1541
static u32 iwl_dump_ini_rxf_ranges(struct iwl_fw_runtime *fwrt,
				   struct iwl_fw_ini_region_cfg *reg)
{
	/* Each Rx fifo needs a different offset and therefore, it's
	 * region can contain only one fifo, i.e. 1 memory range.
	 */
	return 1;
}

1542 1543 1544
static u32 iwl_dump_ini_mem_get_size(struct iwl_fw_runtime *fwrt,
				     struct iwl_fw_ini_region_cfg *reg)
{
1545 1546 1547 1548
	return sizeof(struct iwl_fw_ini_error_dump) +
		iwl_dump_ini_mem_ranges(fwrt, reg) *
		(sizeof(struct iwl_fw_ini_error_dump_range) +
		 le32_to_cpu(reg->internal.range_data_size));
1549 1550
}

1551 1552 1553 1554
static u32 iwl_dump_ini_paging_gen2_get_size(struct iwl_fw_runtime *fwrt,
					     struct iwl_fw_ini_region_cfg *reg)
{
	int i;
1555 1556 1557 1558 1559 1560
	u32 range_header_len = sizeof(struct iwl_fw_ini_error_dump_range);
	u32 size = sizeof(struct iwl_fw_ini_error_dump);

	for (i = 0; i < iwl_dump_ini_paging_gen2_ranges(fwrt, reg); i++)
		size += range_header_len +
			fwrt->trans->init_dram.paging[i].size;
1561 1562 1563 1564 1565 1566 1567 1568

	return size;
}

static u32 iwl_dump_ini_paging_get_size(struct iwl_fw_runtime *fwrt,
					struct iwl_fw_ini_region_cfg *reg)
{
	int i;
1569 1570 1571 1572 1573
	u32 range_header_len = sizeof(struct iwl_fw_ini_error_dump_range);
	u32 size = sizeof(struct iwl_fw_ini_error_dump);

	for (i = 1; i <= iwl_dump_ini_paging_ranges(fwrt, reg); i++)
		size += range_header_len + fwrt->fw_paging_db[i].fw_paging_size;
1574 1575 1576 1577

	return size;
}

1578 1579 1580
static u32 iwl_dump_ini_mon_dram_get_size(struct iwl_fw_runtime *fwrt,
					  struct iwl_fw_ini_region_cfg *reg)
{
1581
	u32 size = sizeof(struct iwl_fw_ini_monitor_dump) +
1582
		sizeof(struct iwl_fw_ini_error_dump_range);
1583

1584 1585
	if (fwrt->trans->dbg.num_blocks)
		size += fwrt->trans->dbg.fw_mon[0].size;
1586

1587
	return size;
1588 1589
}

1590 1591 1592 1593 1594 1595 1596 1597 1598
static u32 iwl_dump_ini_mon_smem_get_size(struct iwl_fw_runtime *fwrt,
					  struct iwl_fw_ini_region_cfg *reg)
{
	return sizeof(struct iwl_fw_ini_monitor_dump) +
		iwl_dump_ini_mem_ranges(fwrt, reg) *
		(sizeof(struct iwl_fw_ini_error_dump_range) +
		 le32_to_cpu(reg->internal.range_data_size));
}

1599 1600 1601 1602 1603 1604
static u32 iwl_dump_ini_txf_get_size(struct iwl_fw_runtime *fwrt,
				     struct iwl_fw_ini_region_cfg *reg)
{
	struct iwl_ini_txf_iter_data iter = { .init = true };
	void *fifo_iter = fwrt->dump.fifo_iter;
	u32 size = 0;
1605 1606 1607
	u32 fifo_hdr = sizeof(struct iwl_fw_ini_error_dump_range) +
		le32_to_cpu(reg->fifos.num_of_registers) *
		sizeof(struct iwl_fw_ini_error_dump_register);
1608 1609 1610 1611 1612 1613 1614 1615 1616

	fwrt->dump.fifo_iter = &iter;
	while (iwl_ini_txf_iter(fwrt, reg)) {
		size += fifo_hdr;
		if (!reg->fifos.header_only)
			size += iter.fifo_size;
	}

	if (size)
1617
		size += sizeof(struct iwl_fw_ini_error_dump);
1618 1619 1620 1621 1622 1623

	fwrt->dump.fifo_iter = fifo_iter;

	return size;
}

1624 1625 1626 1627
static u32 iwl_dump_ini_rxf_get_size(struct iwl_fw_runtime *fwrt,
				     struct iwl_fw_ini_region_cfg *reg)
{
	struct iwl_ini_rxf_data rx_data;
1628 1629 1630 1631
	u32 size = sizeof(struct iwl_fw_ini_error_dump) +
		sizeof(struct iwl_fw_ini_error_dump_range) +
		le32_to_cpu(reg->fifos.num_of_registers) *
		sizeof(struct iwl_fw_ini_error_dump_register);
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641

	if (reg->fifos.header_only)
		return size;

	iwl_ini_get_rxf_data(fwrt, reg, &rx_data);
	size += rx_data.size;

	return size;
}

1642 1643 1644
/**
 * struct iwl_dump_ini_mem_ops - ini memory dump operations
 * @get_num_of_ranges: returns the number of memory ranges in the region.
1645
 * @get_size: returns the total size of the region.
1646 1647
 * @fill_mem_hdr: fills region type specific headers and returns pointer to
 *	the first range or NULL if failed to fill headers.
1648
 * @fill_range: copies a given memory range into the dump.
1649
 *	Returns the size of the range or negative error value otherwise.
1650 1651 1652 1653 1654 1655
 */
struct iwl_dump_ini_mem_ops {
	u32 (*get_num_of_ranges)(struct iwl_fw_runtime *fwrt,
				 struct iwl_fw_ini_region_cfg *reg);
	u32 (*get_size)(struct iwl_fw_runtime *fwrt,
			struct iwl_fw_ini_region_cfg *reg);
1656 1657
	void *(*fill_mem_hdr)(struct iwl_fw_runtime *fwrt,
			      struct iwl_fw_ini_region_cfg *reg, void *data);
1658
	int (*fill_range)(struct iwl_fw_runtime *fwrt,
1659 1660
			  struct iwl_fw_ini_region_cfg *reg, void *range,
			  int idx);
1661 1662 1663 1664 1665 1666 1667
};

/**
 * iwl_dump_ini_mem - copy a memory region into the dump
 * @fwrt: fw runtime struct.
 * @data: dump memory data.
 * @reg: region to copy to the dump.
1668
 * @ops: memory dump operations.
1669 1670 1671 1672 1673 1674 1675 1676
 */
static void
iwl_dump_ini_mem(struct iwl_fw_runtime *fwrt,
		 struct iwl_fw_error_dump_data **data,
		 struct iwl_fw_ini_region_cfg *reg,
		 struct iwl_dump_ini_mem_ops *ops)
{
	struct iwl_fw_ini_error_dump_header *header = (void *)(*data)->data;
1677
	u32 num_of_ranges, i, type = le32_to_cpu(reg->region_type), size;
1678
	void *range;
1679 1680 1681 1682 1683

	if (WARN_ON(!ops || !ops->get_num_of_ranges || !ops->get_size ||
		    !ops->fill_mem_hdr || !ops->fill_range))
		return;

1684 1685 1686 1687
	size = ops->get_size(fwrt, reg);
	if (!size)
		return;

1688 1689 1690
	IWL_DEBUG_FW(fwrt, "WRT: collecting region: id=%d, type=%d\n",
		     le32_to_cpu(reg->region_id), type);

1691 1692
	num_of_ranges = ops->get_num_of_ranges(fwrt, reg);

1693
	(*data)->type = cpu_to_le32(type);
1694
	(*data)->len = cpu_to_le32(size);
1695

1696
	header->region_id = reg->region_id;
1697 1698 1699 1700 1701
	header->num_of_ranges = cpu_to_le32(num_of_ranges);
	header->name_len = cpu_to_le32(min_t(int, IWL_FW_INI_MAX_NAME,
					     le32_to_cpu(reg->name_len)));
	memcpy(header->name, reg->name, le32_to_cpu(header->name_len));

1702
	range = ops->fill_mem_hdr(fwrt, reg, header);
1703
	if (!range) {
1704 1705
		IWL_ERR(fwrt,
			"WRT: failed to fill region header: id=%d, type=%d\n",
1706
			le32_to_cpu(reg->region_id), type);
1707
		memset(*data, 0, size);
1708
		return;
1709
	}
1710 1711

	for (i = 0; i < num_of_ranges; i++) {
1712
		int range_size = ops->fill_range(fwrt, reg, range, i);
1713

1714
		if (range_size < 0) {
1715 1716
			IWL_ERR(fwrt,
				"WRT: failed to dump region: id=%d, type=%d\n",
1717
				le32_to_cpu(reg->region_id), type);
1718
			memset(*data, 0, size);
1719 1720
			return;
		}
1721
		range = range + range_size;
1722 1723 1724 1725
	}
	*data = iwl_fw_error_next_data(*data);
}

1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
static void iwl_dump_ini_info(struct iwl_fw_runtime *fwrt,
			      struct iwl_fw_ini_trigger *trigger,
			      struct iwl_fw_error_dump_data **data)
{
	struct iwl_fw_ini_dump_info *dump = (void *)(*data)->data;
	u32 reg_ids_size = le32_to_cpu(trigger->num_regions) * sizeof(__le32);

	(*data)->type = cpu_to_le32(IWL_INI_DUMP_INFO_TYPE);
	(*data)->len = cpu_to_le32(sizeof(*dump) + reg_ids_size);

	dump->version = cpu_to_le32(IWL_INI_DUMP_VER);
	dump->trigger_id = trigger->trigger_id;
1738 1739
	dump->is_external_cfg =
		cpu_to_le32(fwrt->trans->dbg.external_ini_loaded);
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785

	dump->ver_type = cpu_to_le32(fwrt->dump.fw_ver.type);
	dump->ver_subtype = cpu_to_le32(fwrt->dump.fw_ver.subtype);

	dump->hw_step = cpu_to_le32(CSR_HW_REV_STEP(fwrt->trans->hw_rev));
	dump->hw_type = cpu_to_le32(CSR_HW_REV_TYPE(fwrt->trans->hw_rev));

	dump->rf_id_flavor =
		cpu_to_le32(CSR_HW_RFID_FLAVOR(fwrt->trans->hw_rf_id));
	dump->rf_id_dash = cpu_to_le32(CSR_HW_RFID_DASH(fwrt->trans->hw_rf_id));
	dump->rf_id_step = cpu_to_le32(CSR_HW_RFID_STEP(fwrt->trans->hw_rf_id));
	dump->rf_id_type = cpu_to_le32(CSR_HW_RFID_TYPE(fwrt->trans->hw_rf_id));

	dump->lmac_major = cpu_to_le32(fwrt->dump.fw_ver.lmac_major);
	dump->lmac_minor = cpu_to_le32(fwrt->dump.fw_ver.lmac_minor);
	dump->umac_major = cpu_to_le32(fwrt->dump.fw_ver.umac_major);
	dump->umac_minor = cpu_to_le32(fwrt->dump.fw_ver.umac_minor);

	dump->build_tag_len = cpu_to_le32(sizeof(dump->build_tag));
	memcpy(dump->build_tag, fwrt->fw->human_readable,
	       sizeof(dump->build_tag));

	dump->img_name_len = cpu_to_le32(sizeof(dump->img_name));
	memcpy(dump->img_name, fwrt->dump.img_name, sizeof(dump->img_name));

	dump->internal_dbg_cfg_name_len =
		cpu_to_le32(sizeof(dump->internal_dbg_cfg_name));
	memcpy(dump->internal_dbg_cfg_name, fwrt->dump.internal_dbg_cfg_name,
	       sizeof(dump->internal_dbg_cfg_name));

	dump->external_dbg_cfg_name_len =
		cpu_to_le32(sizeof(dump->external_dbg_cfg_name));

	/* dump info size is allocated in iwl_fw_ini_get_trigger_len.
	 * The driver allocates (sizeof(*dump) + reg_ids_size) so it is safe to
	 * use reg_ids_size
	 */
	memcpy(dump->external_dbg_cfg_name, fwrt->dump.external_dbg_cfg_name,
	       sizeof(dump->external_dbg_cfg_name));

	dump->regions_num = trigger->num_regions;
	memcpy(dump->region_ids, trigger->data, reg_ids_size);

	*data = iwl_fw_error_next_data(*data);
}

1786 1787 1788
static int iwl_fw_ini_get_trigger_len(struct iwl_fw_runtime *fwrt,
				      struct iwl_fw_ini_trigger *trigger)
{
1789 1790
	int i, ret_size = 0, hdr_len = sizeof(struct iwl_fw_error_dump_data);
	u32 size;
1791 1792 1793 1794

	if (!trigger || !trigger->num_regions)
		return 0;

1795
	for (i = 0; i < le32_to_cpu(trigger->num_regions); i++) {
1796 1797 1798 1799 1800 1801
		u32 reg_id = le32_to_cpu(trigger->data[i]);
		struct iwl_fw_ini_region_cfg *reg;

		if (WARN_ON(reg_id >= ARRAY_SIZE(fwrt->dump.active_regs)))
			continue;

1802
		reg = fwrt->dump.active_regs[reg_id];
1803 1804 1805 1806
		if (!reg) {
			IWL_WARN(fwrt,
				 "WRT: unassigned region id %d, skipping\n",
				 reg_id);
1807
			continue;
1808
		}
1809

1810 1811 1812 1813
		/* currently the driver supports always on domain only */
		if (le32_to_cpu(reg->domain) != IWL_FW_INI_DBG_DOMAIN_ALWAYS_ON)
			continue;

1814
		switch (le32_to_cpu(reg->region_type)) {
1815 1816 1817 1818
		case IWL_FW_INI_REGION_DEVICE_MEMORY:
		case IWL_FW_INI_REGION_PERIPHERY_MAC:
		case IWL_FW_INI_REGION_PERIPHERY_PHY:
		case IWL_FW_INI_REGION_PERIPHERY_AUX:
1819
		case IWL_FW_INI_REGION_CSR:
1820 1821
		case IWL_FW_INI_REGION_LMAC_ERROR_TABLE:
		case IWL_FW_INI_REGION_UMAC_ERROR_TABLE:
1822 1823 1824
			size = iwl_dump_ini_mem_get_size(fwrt, reg);
			if (size)
				ret_size += hdr_len + size;
1825 1826
			break;
		case IWL_FW_INI_REGION_TXF:
1827 1828 1829
			size = iwl_dump_ini_txf_get_size(fwrt, reg);
			if (size)
				ret_size += hdr_len + size;
1830 1831
			break;
		case IWL_FW_INI_REGION_RXF:
1832 1833 1834
			size = iwl_dump_ini_rxf_get_size(fwrt, reg);
			if (size)
				ret_size += hdr_len + size;
1835
			break;
1836
		case IWL_FW_INI_REGION_PAGING:
1837 1838 1839 1840 1841 1842 1843
			if (iwl_fw_dbg_is_paging_enabled(fwrt))
				size = iwl_dump_ini_paging_get_size(fwrt, reg);
			else
				size = iwl_dump_ini_paging_gen2_get_size(fwrt,
									 reg);
			if (size)
				ret_size += hdr_len + size;
1844 1845
			break;
		case IWL_FW_INI_REGION_DRAM_BUFFER:
1846
			if (!fwrt->trans->dbg.num_blocks)
1847
				break;
1848 1849 1850
			size = iwl_dump_ini_mon_dram_get_size(fwrt, reg);
			if (size)
				ret_size += hdr_len + size;
1851
			break;
1852
		case IWL_FW_INI_REGION_INTERNAL_BUFFER:
1853 1854 1855
			size = iwl_dump_ini_mon_smem_get_size(fwrt, reg);
			if (size)
				ret_size += hdr_len + size;
1856
			break;
1857 1858 1859 1860 1861 1862
		case IWL_FW_INI_REGION_DRAM_IMR:
			/* Undefined yet */
		default:
			break;
		}
	}
1863 1864 1865 1866 1867 1868

	/* add dump info size */
	if (ret_size)
		ret_size += hdr_len + sizeof(struct iwl_fw_ini_dump_info) +
			(le32_to_cpu(trigger->num_regions) * sizeof(__le32));

1869
	return ret_size;
1870 1871 1872 1873
}

static void iwl_fw_ini_dump_trigger(struct iwl_fw_runtime *fwrt,
				    struct iwl_fw_ini_trigger *trigger,
1874
				    struct iwl_fw_error_dump_data **data)
1875 1876 1877
{
	int i, num = le32_to_cpu(trigger->num_regions);

1878 1879
	iwl_dump_ini_info(fwrt, trigger, data);

1880 1881 1882
	for (i = 0; i < num; i++) {
		u32 reg_id = le32_to_cpu(trigger->data[i]);
		struct iwl_fw_ini_region_cfg *reg;
1883
		struct iwl_dump_ini_mem_ops ops;
1884 1885 1886 1887

		if (reg_id >= ARRAY_SIZE(fwrt->dump.active_regs))
			continue;

1888
		reg = fwrt->dump.active_regs[reg_id];
1889 1890 1891 1892
		/* Don't warn, get_trigger_len already warned */
		if (!reg)
			continue;

1893 1894 1895 1896
		/* currently the driver supports always on domain only */
		if (le32_to_cpu(reg->domain) != IWL_FW_INI_DBG_DOMAIN_ALWAYS_ON)
			continue;

1897
		switch (le32_to_cpu(reg->region_type)) {
1898
		case IWL_FW_INI_REGION_DEVICE_MEMORY:
1899 1900
		case IWL_FW_INI_REGION_LMAC_ERROR_TABLE:
		case IWL_FW_INI_REGION_UMAC_ERROR_TABLE:
1901 1902 1903 1904
			ops.get_num_of_ranges = iwl_dump_ini_mem_ranges;
			ops.get_size = iwl_dump_ini_mem_get_size;
			ops.fill_mem_hdr = iwl_dump_ini_mem_fill_header;
			ops.fill_range = iwl_dump_ini_dev_mem_iter;
1905
			iwl_dump_ini_mem(fwrt, data, reg, &ops);
1906 1907 1908 1909
			break;
		case IWL_FW_INI_REGION_PERIPHERY_MAC:
		case IWL_FW_INI_REGION_PERIPHERY_PHY:
		case IWL_FW_INI_REGION_PERIPHERY_AUX:
1910 1911 1912 1913
			ops.get_num_of_ranges =	iwl_dump_ini_mem_ranges;
			ops.get_size = iwl_dump_ini_mem_get_size;
			ops.fill_mem_hdr = iwl_dump_ini_mem_fill_header;
			ops.fill_range = iwl_dump_ini_prph_iter;
1914
			iwl_dump_ini_mem(fwrt, data, reg, &ops);
1915 1916
			break;
		case IWL_FW_INI_REGION_DRAM_BUFFER:
1917 1918 1919 1920
			ops.get_num_of_ranges = iwl_dump_ini_mon_dram_ranges;
			ops.get_size = iwl_dump_ini_mon_dram_get_size;
			ops.fill_mem_hdr = iwl_dump_ini_mon_dram_fill_header;
			ops.fill_range = iwl_dump_ini_mon_dram_iter;
1921
			iwl_dump_ini_mem(fwrt, data, reg, &ops);
1922 1923 1924 1925 1926 1927
			break;
		case IWL_FW_INI_REGION_INTERNAL_BUFFER:
			ops.get_num_of_ranges = iwl_dump_ini_mem_ranges;
			ops.get_size = iwl_dump_ini_mon_smem_get_size;
			ops.fill_mem_hdr = iwl_dump_ini_mon_smem_fill_header;
			ops.fill_range = iwl_dump_ini_dev_mem_iter;
1928
			iwl_dump_ini_mem(fwrt, data, reg, &ops);
1929
			break;
1930
		case IWL_FW_INI_REGION_PAGING:
1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
			ops.fill_mem_hdr = iwl_dump_ini_mem_fill_header;
			if (iwl_fw_dbg_is_paging_enabled(fwrt)) {
				ops.get_num_of_ranges =
					iwl_dump_ini_paging_ranges;
				ops.get_size = iwl_dump_ini_paging_get_size;
				ops.fill_range = iwl_dump_ini_paging_iter;
			} else {
				ops.get_num_of_ranges =
					iwl_dump_ini_paging_gen2_ranges;
				ops.get_size =
					iwl_dump_ini_paging_gen2_get_size;
				ops.fill_range = iwl_dump_ini_paging_gen2_iter;
			}

1945
			iwl_dump_ini_mem(fwrt, data, reg, &ops);
1946
			break;
1947 1948 1949 1950 1951 1952 1953
		case IWL_FW_INI_REGION_TXF: {
			struct iwl_ini_txf_iter_data iter = { .init = true };
			void *fifo_iter = fwrt->dump.fifo_iter;

			fwrt->dump.fifo_iter = &iter;
			ops.get_num_of_ranges = iwl_dump_ini_txf_ranges;
			ops.get_size = iwl_dump_ini_txf_get_size;
1954
			ops.fill_mem_hdr = iwl_dump_ini_mem_fill_header;
1955
			ops.fill_range = iwl_dump_ini_txf_iter;
1956
			iwl_dump_ini_mem(fwrt, data, reg, &ops);
1957
			fwrt->dump.fifo_iter = fifo_iter;
1958
			break;
1959
		}
1960
		case IWL_FW_INI_REGION_RXF:
1961 1962
			ops.get_num_of_ranges = iwl_dump_ini_rxf_ranges;
			ops.get_size = iwl_dump_ini_rxf_get_size;
1963
			ops.fill_mem_hdr = iwl_dump_ini_mem_fill_header;
1964
			ops.fill_range = iwl_dump_ini_rxf_iter;
1965
			iwl_dump_ini_mem(fwrt, data, reg, &ops);
1966 1967
			break;
		case IWL_FW_INI_REGION_CSR:
1968 1969 1970 1971
			ops.get_num_of_ranges =	iwl_dump_ini_mem_ranges;
			ops.get_size = iwl_dump_ini_mem_get_size;
			ops.fill_mem_hdr = iwl_dump_ini_mem_fill_header;
			ops.fill_range = iwl_dump_ini_csr_iter;
1972
			iwl_dump_ini_mem(fwrt, data, reg, &ops);
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982
			break;
		case IWL_FW_INI_REGION_DRAM_IMR:
			/* This is undefined yet */
		default:
			break;
		}
	}
}

static struct iwl_fw_error_dump_file *
1983 1984
iwl_fw_error_ini_dump_file(struct iwl_fw_runtime *fwrt,
			   enum iwl_fw_ini_trigger_id trig_id)
1985
{
1986
	int size;
1987 1988
	struct iwl_fw_error_dump_data *dump_data;
	struct iwl_fw_error_dump_file *dump_file;
1989
	struct iwl_fw_ini_trigger *trigger;
1990

1991
	if (!iwl_fw_ini_trigger_on(fwrt, trig_id))
1992 1993
		return NULL;

1994
	trigger = fwrt->dump.active_trigs[trig_id].trig;
1995

1996
	size = iwl_fw_ini_get_trigger_len(fwrt, trigger);
1997 1998 1999
	if (!size)
		return NULL;

2000 2001
	size += sizeof(*dump_file);

2002 2003 2004 2005
	dump_file = vzalloc(size);
	if (!dump_file)
		return NULL;

2006
	dump_file->barker = cpu_to_le32(IWL_FW_INI_ERROR_DUMP_BARKER);
2007 2008 2009
	dump_data = (void *)dump_file->data;
	dump_file->file_len = cpu_to_le32(size);

2010
	iwl_fw_ini_dump_trigger(fwrt, trigger, &dump_data);
2011 2012 2013 2014

	return dump_file;
}

2015
static void iwl_fw_error_dump(struct iwl_fw_runtime *fwrt)
2016
{
2017
	struct iwl_fw_dump_ptrs fw_error_dump = {};
2018 2019 2020
	struct iwl_fw_error_dump_file *dump_file;
	struct scatterlist *sg_dump_data;
	u32 file_len;
2021
	u32 dump_mask = fwrt->fw->dbg.dump_mask;
2022

2023 2024
	dump_file = iwl_fw_error_dump_file(fwrt, &fw_error_dump);
	if (!dump_file)
2025 2026
		goto out;

2027
	if (fwrt->dump.monitor_only)
2028 2029
		dump_mask &= IWL_FW_ERROR_DUMP_FW_MONITOR;

2030
	fw_error_dump.trans_ptr = iwl_trans_dump_data(fwrt->trans, dump_mask);
2031
	file_len = le32_to_cpu(dump_file->file_len);
2032 2033 2034 2035
	fw_error_dump.fwrt_len = file_len;

	if (fw_error_dump.trans_ptr) {
		file_len += fw_error_dump.trans_ptr->len;
2036 2037
		dump_file->file_len = cpu_to_le32(file_len);
	}
2038

2039 2040 2041 2042
	sg_dump_data = alloc_sgtable(file_len);
	if (sg_dump_data) {
		sg_pcopy_from_buffer(sg_dump_data,
				     sg_nents(sg_dump_data),
2043 2044 2045
				     fw_error_dump.fwrt_ptr,
				     fw_error_dump.fwrt_len, 0);
		if (fw_error_dump.trans_ptr)
2046 2047
			sg_pcopy_from_buffer(sg_dump_data,
					     sg_nents(sg_dump_data),
2048 2049 2050
					     fw_error_dump.trans_ptr->data,
					     fw_error_dump.trans_ptr->len,
					     fw_error_dump.fwrt_len);
2051
		dev_coredumpsg(fwrt->trans->dev, sg_dump_data, file_len,
2052 2053
			       GFP_KERNEL);
	}
2054 2055
	vfree(fw_error_dump.fwrt_ptr);
	vfree(fw_error_dump.trans_ptr);
2056

2057
out:
2058
	iwl_fw_free_dump_desc(fwrt);
2059
}
2060

2061
static void iwl_fw_error_ini_dump(struct iwl_fw_runtime *fwrt, u8 wk_idx)
2062
{
2063
	enum iwl_fw_ini_trigger_id trig_id = fwrt->dump.wks[wk_idx].ini_trig_id;
2064 2065 2066 2067
	struct iwl_fw_error_dump_file *dump_file;
	struct scatterlist *sg_dump_data;
	u32 file_len;

2068
	dump_file = iwl_fw_error_ini_dump_file(fwrt, trig_id);
2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082
	if (!dump_file)
		goto out;

	file_len = le32_to_cpu(dump_file->file_len);

	sg_dump_data = alloc_sgtable(file_len);
	if (sg_dump_data) {
		sg_pcopy_from_buffer(sg_dump_data, sg_nents(sg_dump_data),
				     dump_file, file_len, 0);
		dev_coredumpsg(fwrt->trans->dev, sg_dump_data, file_len,
			       GFP_KERNEL);
	}
	vfree(dump_file);
out:
2083
	fwrt->dump.wks[wk_idx].ini_trig_id = IWL_FW_TRIGGER_ID_INVALID;
2084
}
2085

2086
const struct iwl_fw_dump_desc iwl_dump_desc_assert = {
2087 2088 2089 2090
	.trig_desc = {
		.type = cpu_to_le32(FW_DBG_TRIGGER_FW_ASSERT),
	},
};
2091
IWL_EXPORT_SYMBOL(iwl_dump_desc_assert);
2092

2093
int iwl_fw_dbg_collect_desc(struct iwl_fw_runtime *fwrt,
2094 2095
			    const struct iwl_fw_dump_desc *desc,
			    bool monitor_only,
2096
			    unsigned int delay)
2097
{
2098 2099 2100
	u32 trig_type = le32_to_cpu(desc->trig_desc.type);
	int ret;

2101
	if (fwrt->trans->dbg.ini_valid) {
2102 2103 2104 2105 2106 2107 2108
		ret = iwl_fw_dbg_ini_collect(fwrt, trig_type);
		if (!ret)
			iwl_fw_free_dump_desc(fwrt);

		return ret;
	}

2109 2110 2111 2112
	/* use wks[0] since dump flow prior to ini does not need to support
	 * consecutive triggers collection
	 */
	if (test_and_set_bit(fwrt->dump.wks[0].idx, &fwrt->dump.active_wks))
2113 2114
		return -EBUSY;

2115 2116
	if (WARN_ON(fwrt->dump.desc))
		iwl_fw_free_dump_desc(fwrt);
2117

2118
	IWL_WARN(fwrt, "Collecting data: trigger %d fired.\n",
2119 2120
		 le32_to_cpu(desc->trig_desc.type));

2121
	fwrt->dump.desc = desc;
2122
	fwrt->dump.monitor_only = monitor_only;
2123

2124
	schedule_delayed_work(&fwrt->dump.wks[0].wk, usecs_to_jiffies(delay));
2125 2126 2127

	return 0;
}
2128
IWL_EXPORT_SYMBOL(iwl_fw_dbg_collect_desc);
2129

2130 2131 2132 2133
int iwl_fw_dbg_error_collect(struct iwl_fw_runtime *fwrt,
			     enum iwl_fw_dbg_trigger trig_type)
{
	int ret;
2134
	struct iwl_fw_dump_desc *iwl_dump_error_desc;
2135

2136 2137 2138 2139
	if (!test_bit(STATUS_DEVICE_ENABLED, &fwrt->trans->status))
		return -EIO;

	iwl_dump_error_desc = kmalloc(sizeof(*iwl_dump_error_desc), GFP_KERNEL);
2140 2141 2142 2143 2144 2145 2146
	if (!iwl_dump_error_desc)
		return -ENOMEM;

	iwl_dump_error_desc->trig_desc.type = cpu_to_le32(trig_type);
	iwl_dump_error_desc->len = 0;

	ret = iwl_fw_dbg_collect_desc(fwrt, iwl_dump_error_desc, false, 0);
2147
	if (ret)
2148
		kfree(iwl_dump_error_desc);
2149 2150
	else
		iwl_trans_sync_nmi(fwrt->trans);
2151 2152 2153 2154 2155

	return ret;
}
IWL_EXPORT_SYMBOL(iwl_fw_dbg_error_collect);

2156 2157 2158 2159
int iwl_fw_dbg_collect(struct iwl_fw_runtime *fwrt,
		       enum iwl_fw_dbg_trigger trig,
		       const char *str, size_t len,
		       struct iwl_fw_dbg_trigger_tlv *trigger)
2160
{
2161
	struct iwl_fw_dump_desc *desc;
2162
	unsigned int delay = 0;
2163
	bool monitor_only = false;
2164

2165 2166
	if (trigger) {
		u16 occurrences = le16_to_cpu(trigger->occurrences) - 1;
2167

2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178
		if (!le16_to_cpu(trigger->occurrences))
			return 0;

		if (trigger->flags & IWL_FW_DBG_FORCE_RESTART) {
			IWL_WARN(fwrt, "Force restart: trigger %d fired.\n",
				 trig);
			iwl_force_nmi(fwrt->trans);
			return 0;
		}

		trigger->occurrences = cpu_to_le16(occurrences);
2179
		monitor_only = trigger->mode & IWL_FW_DBG_TRIGGER_MONITOR_ONLY;
2180 2181 2182

		/* convert msec to usec */
		delay = le32_to_cpu(trigger->stop_delay) * USEC_PER_MSEC;
2183 2184
	}

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

2189

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

2194
	return iwl_fw_dbg_collect_desc(fwrt, desc, monitor_only, delay);
2195
}
2196
IWL_EXPORT_SYMBOL(iwl_fw_dbg_collect);
2197

2198 2199
int _iwl_fw_dbg_ini_collect(struct iwl_fw_runtime *fwrt,
			    enum iwl_fw_ini_trigger_id id)
2200
{
2201
	struct iwl_fw_ini_active_triggers *active;
2202
	u32 occur, delay;
2203
	unsigned long idx;
2204

2205 2206
	if (WARN_ON(!iwl_fw_ini_trigger_on(fwrt, id)))
		return -EINVAL;
2207

2208 2209 2210 2211 2212 2213
	if (!iwl_fw_ini_trigger_on(fwrt, id)) {
		IWL_WARN(fwrt, "WRT: Trigger %d is not active, aborting dump\n",
			 id);
		return -EINVAL;
	}

2214 2215 2216
	active = &fwrt->dump.active_trigs[id];
	delay = le32_to_cpu(active->trig->dump_delay);
	occur = le32_to_cpu(active->trig->occurrences);
2217 2218 2219
	if (!occur)
		return 0;

2220 2221
	active->trig->occurrences = cpu_to_le32(--occur);

2222
	if (le32_to_cpu(active->trig->force_restart)) {
2223
		IWL_WARN(fwrt, "WRT: force restart: trigger %d fired.\n", id);
2224 2225 2226 2227
		iwl_force_nmi(fwrt->trans);
		return 0;
	}

2228 2229 2230 2231 2232
	/* Check there is an available worker.
	 * ffz return value is undefined if no zero exists,
	 * so check against ~0UL first.
	 */
	if (fwrt->dump.active_wks == ~0UL)
2233 2234
		return -EBUSY;

2235 2236 2237 2238 2239 2240 2241
	idx = ffz(fwrt->dump.active_wks);

	if (idx >= IWL_FW_RUNTIME_DUMP_WK_NUM ||
	    test_and_set_bit(fwrt->dump.wks[idx].idx, &fwrt->dump.active_wks))
		return -EBUSY;

	fwrt->dump.wks[idx].ini_trig_id = id;
2242

2243
	IWL_WARN(fwrt, "WRT: collecting data: ini trigger %d fired.\n", id);
2244

2245
	schedule_delayed_work(&fwrt->dump.wks[idx].wk, usecs_to_jiffies(delay));
2246

2247
	return 0;
2248
}
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270
IWL_EXPORT_SYMBOL(_iwl_fw_dbg_ini_collect);

int iwl_fw_dbg_ini_collect(struct iwl_fw_runtime *fwrt, u32 legacy_trigger_id)
{
	int id;

	switch (legacy_trigger_id) {
	case FW_DBG_TRIGGER_FW_ASSERT:
	case FW_DBG_TRIGGER_ALIVE_TIMEOUT:
	case FW_DBG_TRIGGER_DRIVER:
		id = IWL_FW_TRIGGER_ID_FW_ASSERT;
		break;
	case FW_DBG_TRIGGER_USER:
		id = IWL_FW_TRIGGER_ID_USER_TRIGGER;
		break;
	default:
		return -EIO;
	}

	return _iwl_fw_dbg_ini_collect(fwrt, id);
}
IWL_EXPORT_SYMBOL(iwl_fw_dbg_ini_collect);
2271

2272 2273 2274
int iwl_fw_dbg_collect_trig(struct iwl_fw_runtime *fwrt,
			    struct iwl_fw_dbg_trigger_tlv *trigger,
			    const char *fmt, ...)
2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294
{
	int ret, len = 0;
	char buf[64];

	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;
	}

2295 2296
	ret = iwl_fw_dbg_collect(fwrt, le32_to_cpu(trigger->id), buf, len,
				 trigger);
2297 2298 2299 2300 2301 2302

	if (ret)
		return ret;

	return 0;
}
2303
IWL_EXPORT_SYMBOL(iwl_fw_dbg_collect_trig);
2304

2305
int iwl_fw_start_dbg_conf(struct iwl_fw_runtime *fwrt, u8 conf_id)
2306 2307 2308 2309 2310
{
	u8 *ptr;
	int ret;
	int i;

2311
	if (WARN_ONCE(conf_id >= ARRAY_SIZE(fwrt->fw->dbg.conf_tlv),
2312 2313 2314 2315
		      "Invalid configuration %d\n", conf_id))
		return -EINVAL;

	/* EARLY START - firmware's configuration is hard coded */
2316 2317
	if ((!fwrt->fw->dbg.conf_tlv[conf_id] ||
	     !fwrt->fw->dbg.conf_tlv[conf_id]->num_of_hcmds) &&
2318
	    conf_id == FW_DBG_START_FROM_ALIVE)
2319 2320
		return 0;

2321
	if (!fwrt->fw->dbg.conf_tlv[conf_id])
2322 2323
		return -EINVAL;

2324 2325 2326
	if (fwrt->dump.conf != FW_DBG_INVALID)
		IWL_WARN(fwrt, "FW already configured (%d) - re-configuring\n",
			 fwrt->dump.conf);
2327 2328

	/* Send all HCMDs for configuring the FW debug */
2329 2330
	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++) {
2331
		struct iwl_fw_dbg_conf_hcmd *cmd = (void *)ptr;
2332 2333 2334 2335 2336
		struct iwl_host_cmd hcmd = {
			.id = cmd->id,
			.len = { le16_to_cpu(cmd->len), },
			.data = { cmd->data, },
		};
2337

2338
		ret = iwl_trans_send_cmd(fwrt->trans, &hcmd);
2339 2340 2341 2342 2343 2344 2345
		if (ret)
			return ret;

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

2346
	fwrt->dump.conf = conf_id;
2347 2348

	return 0;
2349
}
2350 2351
IWL_EXPORT_SYMBOL(iwl_fw_start_dbg_conf);

2352 2353 2354
/* this function assumes dump_start was called beforehand and dump_end will be
 * called afterwards
 */
2355
static void iwl_fw_dbg_collect_sync(struct iwl_fw_runtime *fwrt, u8 wk_idx)
2356
{
2357
	struct iwl_fw_dbg_params params = {0};
2358

2359
	if (!test_bit(wk_idx, &fwrt->dump.active_wks))
2360 2361
		return;

2362 2363 2364 2365
	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);
2366
		goto out;
2367 2368
	}

2369 2370 2371
	/* there's no point in fw dump if the bus is dead */
	if (test_bit(STATUS_TRANS_DEAD, &fwrt->trans->status)) {
		IWL_ERR(fwrt, "Skip fw error dump since bus is dead\n");
2372
		goto out;
2373 2374
	}

2375 2376 2377 2378
	if (iwl_fw_dbg_stop_restart_recording(fwrt, &params, true)) {
		IWL_ERR(fwrt, "Failed to stop DBGC recording, aborting dump\n");
		goto out;
	}
2379

2380
	IWL_DEBUG_FW_INFO(fwrt, "WRT: data collection start\n");
2381
	if (fwrt->trans->dbg.ini_valid)
2382
		iwl_fw_error_ini_dump(fwrt, wk_idx);
2383 2384
	else
		iwl_fw_error_dump(fwrt);
2385
	IWL_DEBUG_FW_INFO(fwrt, "WRT: data collection done\n");
2386

2387
	iwl_fw_dbg_stop_restart_recording(fwrt, &params, false);
2388 2389 2390

out:
	clear_bit(wk_idx, &fwrt->dump.active_wks);
2391 2392 2393 2394
}

void iwl_fw_error_dump_wk(struct work_struct *work)
{
2395 2396
	struct iwl_fw_runtime *fwrt;
	typeof(fwrt->dump.wks[0]) *wks;
2397

2398 2399 2400 2401 2402 2403
	wks = container_of(work, typeof(fwrt->dump.wks[0]), wk.work);
	fwrt = container_of(wks, struct iwl_fw_runtime, dump.wks[wks->idx]);

	/* assumes the op mode mutex is locked in dump_start since
	 * iwl_fw_dbg_collect_sync can't run in parallel
	 */
2404 2405 2406 2407
	if (fwrt->ops && fwrt->ops->dump_start &&
	    fwrt->ops->dump_start(fwrt->ops_ctx))
		return;

2408
	iwl_fw_dbg_collect_sync(fwrt, wks->idx);
2409

2410 2411 2412 2413
	if (fwrt->ops && fwrt->ops->dump_end)
		fwrt->ops->dump_end(fwrt->ops_ctx);
}

2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436
void iwl_fw_dbg_read_d3_debug_data(struct iwl_fw_runtime *fwrt)
{
	const struct iwl_cfg *cfg = fwrt->trans->cfg;

	if (!iwl_fw_dbg_is_d3_debug_enabled(fwrt))
		return;

	if (!fwrt->dump.d3_debug_data) {
		fwrt->dump.d3_debug_data = kmalloc(cfg->d3_debug_data_length,
						   GFP_KERNEL);
		if (!fwrt->dump.d3_debug_data) {
			IWL_ERR(fwrt,
				"failed to allocate memory for D3 debug data\n");
			return;
		}
	}

	/* if the buffer holds previous debug data it is overwritten */
	iwl_trans_read_mem_bytes(fwrt->trans, cfg->d3_debug_data_base_addr,
				 fwrt->dump.d3_debug_data,
				 cfg->d3_debug_data_length);
}
IWL_EXPORT_SYMBOL(iwl_fw_dbg_read_d3_debug_data);
S
Sara Sharon 已提交
2437

2438 2439 2440 2441 2442 2443 2444 2445
static void iwl_fw_dbg_info_apply(struct iwl_fw_runtime *fwrt,
				  struct iwl_fw_ini_debug_info_tlv *dbg_info,
				  bool ext, enum iwl_fw_ini_apply_point pnt)
{
	u32 img_name_len = le32_to_cpu(dbg_info->img_name_len);
	u32 dbg_cfg_name_len = le32_to_cpu(dbg_info->dbg_cfg_name_len);

	if (img_name_len != IWL_FW_INI_MAX_IMG_NAME_LEN) {
2446 2447 2448
		IWL_WARN(fwrt,
			 "WRT: ext=%d. Invalid image name length %d, expected %d\n",
			 ext, img_name_len,
2449 2450 2451 2452 2453
			 IWL_FW_INI_MAX_IMG_NAME_LEN);
		return;
	}

	if (dbg_cfg_name_len != IWL_FW_INI_MAX_DBG_CFG_NAME_LEN) {
2454 2455 2456
		IWL_WARN(fwrt,
			 "WRT: ext=%d. Invalid debug cfg name length %d, expected %d\n",
			 ext, dbg_cfg_name_len,
2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471
			 IWL_FW_INI_MAX_DBG_CFG_NAME_LEN);
		return;
	}

	if (ext) {
		memcpy(fwrt->dump.external_dbg_cfg_name, dbg_info->dbg_cfg_name,
		       sizeof(fwrt->dump.external_dbg_cfg_name));
	} else {
		memcpy(fwrt->dump.img_name, dbg_info->img_name,
		       sizeof(fwrt->dump.img_name));
		memcpy(fwrt->dump.internal_dbg_cfg_name, dbg_info->dbg_cfg_name,
		       sizeof(fwrt->dump.internal_dbg_cfg_name));
	}
}

S
Sara Sharon 已提交
2472
static void
2473
iwl_fw_dbg_buffer_allocation(struct iwl_fw_runtime *fwrt, u32 size)
S
Sara Sharon 已提交
2474 2475 2476 2477 2478
{
	struct iwl_trans *trans = fwrt->trans;
	void *virtual_addr = NULL;
	dma_addr_t phys_addr;

2479 2480
	if (WARN_ON_ONCE(trans->dbg.num_blocks ==
			 ARRAY_SIZE(trans->dbg.fw_mon)))
S
Sara Sharon 已提交
2481 2482
		return;

2483 2484 2485 2486
	virtual_addr =
		dma_alloc_coherent(fwrt->trans->dev, size, &phys_addr,
				   GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO |
				   __GFP_COMP);
S
Sara Sharon 已提交
2487 2488 2489 2490 2491

	/* TODO: alloc fragments if needed */
	if (!virtual_addr)
		IWL_ERR(fwrt, "Failed to allocate debug memory\n");

2492 2493
	IWL_DEBUG_FW(trans,
		     "Allocated DRAM buffer[%d], size=0x%x\n",
2494
		     trans->dbg.num_blocks, size);
2495

2496 2497 2498 2499
	trans->dbg.fw_mon[trans->dbg.num_blocks].block = virtual_addr;
	trans->dbg.fw_mon[trans->dbg.num_blocks].physical = phys_addr;
	trans->dbg.fw_mon[trans->dbg.num_blocks].size = size;
	trans->dbg.num_blocks++;
2500 2501 2502
}

static void iwl_fw_dbg_buffer_apply(struct iwl_fw_runtime *fwrt,
2503 2504
				    struct iwl_fw_ini_allocation_data *alloc,
				    enum iwl_fw_ini_apply_point pnt)
2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516
{
	struct iwl_trans *trans = fwrt->trans;
	struct iwl_ldbg_config_cmd ldbg_cmd = {
		.type = cpu_to_le32(BUFFER_ALLOCATION),
	};
	struct iwl_buffer_allocation_cmd *cmd = &ldbg_cmd.buffer_allocation;
	struct iwl_host_cmd hcmd = {
		.id = LDBG_CONFIG_CMD,
		.flags = CMD_ASYNC,
		.data[0] = &ldbg_cmd,
		.len[0] = sizeof(ldbg_cmd),
	};
2517
	int block_idx = trans->dbg.num_blocks;
2518 2519
	u32 buf_location = le32_to_cpu(alloc->tlv.buffer_location);

2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530
	if (fwrt->trans->dbg.ini_dest == IWL_FW_INI_LOCATION_INVALID)
		fwrt->trans->dbg.ini_dest = buf_location;

	if (buf_location != fwrt->trans->dbg.ini_dest) {
		WARN(fwrt,
		     "WRT: attempt to override buffer location on apply point %d\n",
		     pnt);

		return;
	}

2531
	if (buf_location == IWL_FW_INI_LOCATION_SRAM_PATH) {
2532 2533 2534 2535 2536
		IWL_DEBUG_FW(trans, "WRT: applying SMEM buffer destination\n");
		/* set sram monitor by enabling bit 7 */
		iwl_set_bit(fwrt->trans, CSR_HW_IF_CONFIG_REG,
			    CSR_HW_IF_CONFIG_REG_BIT_MONITOR_SRAM);

2537 2538
		return;
	}
2539

2540
	if (buf_location != IWL_FW_INI_LOCATION_DRAM_PATH)
2541 2542 2543 2544 2545
		return;

	if (!alloc->is_alloc) {
		iwl_fw_dbg_buffer_allocation(fwrt,
					     le32_to_cpu(alloc->tlv.size));
2546
		if (block_idx == trans->dbg.num_blocks)
2547 2548 2549
			return;
		alloc->is_alloc = 1;
	}
S
Sara Sharon 已提交
2550 2551

	/* First block is assigned via registers / context info */
2552
	if (trans->dbg.num_blocks == 1)
S
Sara Sharon 已提交
2553 2554
		return;

2555 2556 2557
	IWL_DEBUG_FW(trans,
		     "WRT: applying DRAM buffer[%d] destination\n", block_idx);

S
Sara Sharon 已提交
2558
	cmd->num_frags = cpu_to_le32(1);
2559
	cmd->fragments[0].address =
2560
		cpu_to_le64(trans->dbg.fw_mon[block_idx].physical);
2561 2562 2563
	cmd->fragments[0].size = alloc->tlv.size;
	cmd->allocation_id = alloc->tlv.allocation_id;
	cmd->buffer_location = alloc->tlv.buffer_location;
S
Sara Sharon 已提交
2564 2565 2566 2567 2568

	iwl_trans_send_cmd(trans, &hcmd);
}

static void iwl_fw_dbg_send_hcmd(struct iwl_fw_runtime *fwrt,
2569 2570
				 struct iwl_ucode_tlv *tlv,
				 bool ext)
S
Sara Sharon 已提交
2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581
{
	struct iwl_fw_ini_hcmd_tlv *hcmd_tlv = (void *)&tlv->data[0];
	struct iwl_fw_ini_hcmd *data = &hcmd_tlv->hcmd;
	u16 len = le32_to_cpu(tlv->length) - sizeof(*hcmd_tlv);

	struct iwl_host_cmd hcmd = {
		.id = WIDE_ID(data->group, data->id),
		.len = { len, },
		.data = { data->data, },
	};

2582 2583 2584 2585
	/* currently the driver supports always on domain only */
	if (le32_to_cpu(hcmd_tlv->domain) != IWL_FW_INI_DBG_DOMAIN_ALWAYS_ON)
		return;

2586 2587 2588 2589
	IWL_DEBUG_FW(fwrt,
		     "WRT: ext=%d. Sending host command id=0x%x, group=0x%x\n",
		     ext, data->id, data->group);

S
Sara Sharon 已提交
2590 2591 2592 2593 2594 2595 2596 2597 2598
	iwl_trans_send_cmd(fwrt->trans, &hcmd);
}

static void iwl_fw_dbg_update_regions(struct iwl_fw_runtime *fwrt,
				      struct iwl_fw_ini_region_tlv *tlv,
				      bool ext, enum iwl_fw_ini_apply_point pnt)
{
	void *iter = (void *)tlv->region_config;
	int i, size = le32_to_cpu(tlv->num_regions);
2599 2600
	const char *err_st =
		"WRT: ext=%d. Invalid region %s %d for apply point %d\n";
S
Sara Sharon 已提交
2601 2602

	for (i = 0; i < size; i++) {
2603
		struct iwl_fw_ini_region_cfg *reg = iter, **active;
S
Sara Sharon 已提交
2604
		int id = le32_to_cpu(reg->region_id);
2605
		u32 type = le32_to_cpu(reg->region_type);
S
Sara Sharon 已提交
2606

2607 2608 2609 2610 2611 2612
		if (WARN(id >= ARRAY_SIZE(fwrt->dump.active_regs), err_st, ext,
			 "id", id, pnt))
			break;

		if (WARN(type == 0 || type >= IWL_FW_INI_REGION_NUM, err_st,
			 ext, "type", type, pnt))
S
Sara Sharon 已提交
2613 2614 2615 2616
			break;

		active = &fwrt->dump.active_regs[id];

2617
		if (*active)
2618 2619 2620
			IWL_WARN(fwrt->trans,
				 "WRT: ext=%d. Region id %d override\n",
				 ext, id);
S
Sara Sharon 已提交
2621 2622

		IWL_DEBUG_FW(fwrt,
2623 2624
			     "WRT: ext=%d. Activating region id %d\n",
			     ext, id);
S
Sara Sharon 已提交
2625

2626
		*active = reg;
S
Sara Sharon 已提交
2627

2628 2629 2630 2631
		if (type == IWL_FW_INI_REGION_TXF ||
		    type == IWL_FW_INI_REGION_RXF)
			iter += le32_to_cpu(reg->fifos.num_of_registers) *
				sizeof(__le32);
2632 2633 2634 2635 2636 2637
		else if (type == IWL_FW_INI_REGION_DEVICE_MEMORY ||
			 type == IWL_FW_INI_REGION_PERIPHERY_MAC ||
			 type == IWL_FW_INI_REGION_PERIPHERY_PHY ||
			 type == IWL_FW_INI_REGION_PERIPHERY_AUX ||
			 type == IWL_FW_INI_REGION_INTERNAL_BUFFER ||
			 type == IWL_FW_INI_REGION_PAGING ||
2638 2639 2640
			 type == IWL_FW_INI_REGION_CSR ||
			 type == IWL_FW_INI_REGION_LMAC_ERROR_TABLE ||
			 type == IWL_FW_INI_REGION_UMAC_ERROR_TABLE)
2641
			iter += le32_to_cpu(reg->internal.num_of_ranges) *
2642
				sizeof(__le32);
S
Sara Sharon 已提交
2643 2644 2645 2646 2647

		iter += sizeof(*reg);
	}
}

2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658
static int iwl_fw_dbg_trig_realloc(struct iwl_fw_runtime *fwrt,
				   struct iwl_fw_ini_active_triggers *active,
				   u32 id, int size)
{
	void *ptr;

	if (size <= active->size)
		return 0;

	ptr = krealloc(active->trig, size, GFP_KERNEL);
	if (!ptr) {
2659 2660
		IWL_ERR(fwrt, "WRT: Failed to allocate memory for trigger %d\n",
			id);
2661 2662 2663 2664 2665 2666 2667 2668
		return -ENOMEM;
	}
	active->trig = ptr;
	active->size = size;

	return 0;
}

S
Sara Sharon 已提交
2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680
static void iwl_fw_dbg_update_triggers(struct iwl_fw_runtime *fwrt,
				       struct iwl_fw_ini_trigger_tlv *tlv,
				       bool ext,
				       enum iwl_fw_ini_apply_point apply_point)
{
	int i, size = le32_to_cpu(tlv->num_triggers);
	void *iter = (void *)tlv->trigger_config;

	for (i = 0; i < size; i++) {
		struct iwl_fw_ini_trigger *trig = iter;
		struct iwl_fw_ini_active_triggers *active;
		int id = le32_to_cpu(trig->trigger_id);
2681 2682
		u32 trig_regs_size = le32_to_cpu(trig->num_regions) *
			sizeof(__le32);
S
Sara Sharon 已提交
2683

2684 2685 2686
		if (WARN(id >= ARRAY_SIZE(fwrt->dump.active_trigs),
			 "WRT: ext=%d. Invalid trigger id %d for apply point %d\n",
			 ext, id, apply_point))
S
Sara Sharon 已提交
2687 2688 2689 2690
			break;

		active = &fwrt->dump.active_trigs[id];

2691 2692 2693
		if (!active->active) {
			size_t trig_size = sizeof(*trig) + trig_regs_size;

2694 2695 2696 2697
			IWL_DEBUG_FW(fwrt,
				     "WRT: ext=%d. Activating trigger %d\n",
				     ext, id);

2698 2699 2700
			if (iwl_fw_dbg_trig_realloc(fwrt, active, id,
						    trig_size))
				goto next;
S
Sara Sharon 已提交
2701

2702
			memcpy(active->trig, trig, trig_size);
S
Sara Sharon 已提交
2703

2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715
		} else {
			u32 conf_override =
				!(le32_to_cpu(trig->override_trig) & 0xff);
			u32 region_override =
				!(le32_to_cpu(trig->override_trig) & 0xff00);
			u32 offset = 0;
			u32 active_regs =
				le32_to_cpu(active->trig->num_regions);
			u32 new_regs = le32_to_cpu(trig->num_regions);
			int mem_to_add = trig_regs_size;

			if (region_override) {
2716 2717 2718 2719
				IWL_DEBUG_FW(fwrt,
					     "WRT: ext=%d. Trigger %d regions override\n",
					     ext, id);

2720
				mem_to_add -= active_regs * sizeof(__le32);
S
Sara Sharon 已提交
2721
			} else {
2722 2723 2724 2725
				IWL_DEBUG_FW(fwrt,
					     "WRT: ext=%d. Trigger %d regions appending\n",
					     ext, id);

2726 2727
				offset += active_regs;
				new_regs += active_regs;
S
Sara Sharon 已提交
2728
			}
2729 2730 2731 2732 2733

			if (iwl_fw_dbg_trig_realloc(fwrt, active, id,
						    active->size + mem_to_add))
				goto next;

2734 2735 2736 2737 2738
			if (conf_override) {
				IWL_DEBUG_FW(fwrt,
					     "WRT: ext=%d. Trigger %d configuration override\n",
					     ext, id);

2739
				memcpy(active->trig, trig, sizeof(*trig));
2740
			}
2741 2742 2743 2744

			memcpy(active->trig->data + offset, trig->data,
			       trig_regs_size);
			active->trig->num_regions = cpu_to_le32(new_regs);
S
Sara Sharon 已提交
2745 2746
		}

2747
		/* Since zero means infinity - just set to -1 */
2748 2749
		if (!le32_to_cpu(active->trig->occurrences))
			active->trig->occurrences = cpu_to_le32(-1);
2750

2751
		active->active = true;
2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765

		if (id == IWL_FW_TRIGGER_ID_PERIODIC_TRIGGER) {
			u32 collect_interval = le32_to_cpu(trig->trigger_data);

			/* the minimum allowed interval is 50ms */
			if (collect_interval < 50) {
				collect_interval = 50;
				trig->trigger_data =
					cpu_to_le32(collect_interval);
			}

			mod_timer(&fwrt->dump.periodic_trig,
				  jiffies + msecs_to_jiffies(collect_interval));
		}
2766 2767
next:
		iter += sizeof(*trig) + trig_regs_size;
S
Sara Sharon 已提交
2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784

	}
}

static void _iwl_fw_dbg_apply_point(struct iwl_fw_runtime *fwrt,
				    struct iwl_apply_point_data *data,
				    enum iwl_fw_ini_apply_point pnt,
				    bool ext)
{
	void *iter = data->data;

	while (iter && iter < data->data + data->size) {
		struct iwl_ucode_tlv *tlv = iter;
		void *ini_tlv = (void *)tlv->data;
		u32 type = le32_to_cpu(tlv->type);

		switch (type) {
2785 2786 2787
		case IWL_UCODE_TLV_TYPE_DEBUG_INFO:
			iwl_fw_dbg_info_apply(fwrt, ini_tlv, ext, pnt);
			break;
2788 2789 2790
		case IWL_UCODE_TLV_TYPE_BUFFER_ALLOCATION: {
			struct iwl_fw_ini_allocation_data *buf_alloc = ini_tlv;

2791
			if (pnt != IWL_FW_INI_APPLY_EARLY) {
2792 2793 2794
				IWL_ERR(fwrt,
					"WRT: ext=%d. Invalid apply point %d for buffer allocation\n",
					ext, pnt);
2795 2796 2797
				goto next;
			}

2798
			iwl_fw_dbg_buffer_apply(fwrt, ini_tlv, pnt);
2799
			iter += sizeof(buf_alloc->is_alloc);
S
Sara Sharon 已提交
2800
			break;
2801
		}
S
Sara Sharon 已提交
2802 2803
		case IWL_UCODE_TLV_TYPE_HCMD:
			if (pnt < IWL_FW_INI_APPLY_AFTER_ALIVE) {
2804 2805 2806
				IWL_ERR(fwrt,
					"WRT: ext=%d. Invalid apply point %d for host command\n",
					ext, pnt);
S
Sara Sharon 已提交
2807 2808
				goto next;
			}
2809
			iwl_fw_dbg_send_hcmd(fwrt, tlv, ext);
S
Sara Sharon 已提交
2810 2811 2812 2813 2814 2815 2816 2817 2818 2819
			break;
		case IWL_UCODE_TLV_TYPE_REGIONS:
			iwl_fw_dbg_update_regions(fwrt, ini_tlv, ext, pnt);
			break;
		case IWL_UCODE_TLV_TYPE_TRIGGERS:
			iwl_fw_dbg_update_triggers(fwrt, ini_tlv, ext, pnt);
			break;
		case IWL_UCODE_TLV_TYPE_DEBUG_FLOW:
			break;
		default:
2820 2821 2822
			WARN_ONCE(1,
				  "WRT: ext=%d. Invalid TLV 0x%x for apply point\n",
				  ext, type);
S
Sara Sharon 已提交
2823 2824 2825 2826 2827 2828 2829
			break;
		}
next:
		iter += sizeof(*tlv) + le32_to_cpu(tlv->length);
	}
}

2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846
static void iwl_fw_dbg_ini_reset_cfg(struct iwl_fw_runtime *fwrt)
{
	int i;

	for (i = 0; i < IWL_FW_INI_MAX_REGION_ID; i++)
		fwrt->dump.active_regs[i] = NULL;

	/* disable the triggers, used in recovery flow */
	for (i = 0; i < IWL_FW_TRIGGER_ID_NUM; i++)
		fwrt->dump.active_trigs[i].active = false;

	memset(fwrt->dump.img_name, 0,
	       sizeof(fwrt->dump.img_name));
	memset(fwrt->dump.internal_dbg_cfg_name, 0,
	       sizeof(fwrt->dump.internal_dbg_cfg_name));
	memset(fwrt->dump.external_dbg_cfg_name, 0,
	       sizeof(fwrt->dump.external_dbg_cfg_name));
2847 2848

	fwrt->trans->dbg.ini_dest = IWL_FW_INI_LOCATION_INVALID;
2849 2850
}

S
Sara Sharon 已提交
2851 2852 2853
void iwl_fw_dbg_apply_point(struct iwl_fw_runtime *fwrt,
			    enum iwl_fw_ini_apply_point apply_point)
{
2854
	void *data = &fwrt->trans->dbg.apply_points[apply_point];
2855

2856 2857
	IWL_DEBUG_FW(fwrt, "WRT: enabling apply point %d\n", apply_point);

2858 2859
	if (apply_point == IWL_FW_INI_APPLY_EARLY)
		iwl_fw_dbg_ini_reset_cfg(fwrt);
S
Sara Sharon 已提交
2860 2861 2862

	_iwl_fw_dbg_apply_point(fwrt, data, apply_point, false);

2863
	data = &fwrt->trans->dbg.apply_points_ext[apply_point];
S
Sara Sharon 已提交
2864 2865 2866
	_iwl_fw_dbg_apply_point(fwrt, data, apply_point, true);
}
IWL_EXPORT_SYMBOL(iwl_fw_dbg_apply_point);
2867

2868
void iwl_fw_dbg_stop_sync(struct iwl_fw_runtime *fwrt)
2869
{
2870 2871
	int i;

2872
	del_timer(&fwrt->dump.periodic_trig);
2873 2874
	for (i = 0; i < IWL_FW_RUNTIME_DUMP_WK_NUM; i++)
		iwl_fw_dbg_collect_sync(fwrt, i);
2875

2876
	iwl_fw_dbg_stop_restart_recording(fwrt, NULL, true);
2877
}
2878
IWL_EXPORT_SYMBOL(iwl_fw_dbg_stop_sync);
2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903

void iwl_fw_dbg_periodic_trig_handler(struct timer_list *t)
{
	struct iwl_fw_runtime *fwrt;
	enum iwl_fw_ini_trigger_id id = IWL_FW_TRIGGER_ID_PERIODIC_TRIGGER;
	int ret;
	typeof(fwrt->dump) *dump_ptr = container_of(t, typeof(fwrt->dump),
						    periodic_trig);

	fwrt = container_of(dump_ptr, typeof(*fwrt), dump);

	ret = _iwl_fw_dbg_ini_collect(fwrt, id);
	if (!ret || ret == -EBUSY) {
		struct iwl_fw_ini_trigger *trig =
			fwrt->dump.active_trigs[id].trig;
		u32 occur = le32_to_cpu(trig->occurrences);
		u32 collect_interval = le32_to_cpu(trig->trigger_data);

		if (!occur)
			return;

		mod_timer(&fwrt->dump.periodic_trig,
			  jiffies + msecs_to_jiffies(collect_interval));
	}
}
2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942

#define FSEQ_REG(x) { .addr = (x), .str = #x, }

void iwl_fw_error_print_fseq_regs(struct iwl_fw_runtime *fwrt)
{
	struct iwl_trans *trans = fwrt->trans;
	unsigned long flags;
	int i;
	struct {
		u32 addr;
		const char *str;
	} fseq_regs[] = {
		FSEQ_REG(FSEQ_ERROR_CODE),
		FSEQ_REG(FSEQ_TOP_INIT_VERSION),
		FSEQ_REG(FSEQ_CNVIO_INIT_VERSION),
		FSEQ_REG(FSEQ_OTP_VERSION),
		FSEQ_REG(FSEQ_TOP_CONTENT_VERSION),
		FSEQ_REG(FSEQ_ALIVE_TOKEN),
		FSEQ_REG(FSEQ_CNVI_ID),
		FSEQ_REG(FSEQ_CNVR_ID),
		FSEQ_REG(CNVI_AUX_MISC_CHIP),
		FSEQ_REG(CNVR_AUX_MISC_CHIP),
		FSEQ_REG(CNVR_SCU_SD_REGS_SD_REG_DIG_DCDC_VTRIM),
		FSEQ_REG(CNVR_SCU_SD_REGS_SD_REG_ACTIVE_VDIG_MIRROR),
	};

	if (!iwl_trans_grab_nic_access(trans, &flags))
		return;

	IWL_ERR(fwrt, "Fseq Registers:\n");

	for (i = 0; i < ARRAY_SIZE(fseq_regs); i++)
		IWL_ERR(fwrt, "0x%08X | %s\n",
			iwl_read_prph_no_grab(trans, fseq_regs[i].addr),
			fseq_regs[i].str);

	iwl_trans_release_nic_access(trans, &flags);
}
IWL_EXPORT_SYMBOL(iwl_fw_error_print_fseq_regs);
2943

2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961
static int iwl_fw_dbg_suspend_resume_hcmd(struct iwl_trans *trans, bool suspend)
{
	struct iwl_dbg_suspend_resume_cmd cmd = {
		.operation = suspend ?
			cpu_to_le32(DBGC_SUSPEND_CMD) :
			cpu_to_le32(DBGC_RESUME_CMD),
	};
	struct iwl_host_cmd hcmd = {
		.id = WIDE_ID(DEBUG_GROUP, DBGC_SUSPEND_RESUME),
		.data[0] = &cmd,
		.len[0] = sizeof(cmd),
	};

	return iwl_trans_send_cmd(trans, &hcmd);
}

static void iwl_fw_dbg_stop_recording(struct iwl_trans *trans,
				      struct iwl_fw_dbg_params *params)
2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980
{
	if (trans->cfg->device_family == IWL_DEVICE_FAMILY_7000) {
		iwl_set_bits_prph(trans, MON_BUFF_SAMPLE_CTL, 0x100);
		return;
	}

	if (params) {
		params->in_sample = iwl_read_umac_prph(trans, DBGC_IN_SAMPLE);
		params->out_ctrl = iwl_read_umac_prph(trans, DBGC_OUT_CTRL);
	}

	iwl_write_umac_prph(trans, DBGC_IN_SAMPLE, 0);
	/* wait for the DBGC to finish writing the internal buffer to DRAM to
	 * avoid halting the HW while writing
	 */
	usleep_range(700, 1000);
	iwl_write_umac_prph(trans, DBGC_OUT_CTRL, 0);
}

2981 2982
static int iwl_fw_dbg_restart_recording(struct iwl_trans *trans,
					struct iwl_fw_dbg_params *params)
2983
{
2984 2985
	if (!params)
		return -EIO;
2986 2987 2988 2989 2990 2991 2992 2993 2994

	if (trans->cfg->device_family == IWL_DEVICE_FAMILY_7000) {
		iwl_clear_bits_prph(trans, MON_BUFF_SAMPLE_CTL, 0x100);
		iwl_clear_bits_prph(trans, MON_BUFF_SAMPLE_CTL, 0x1);
		iwl_set_bits_prph(trans, MON_BUFF_SAMPLE_CTL, 0x1);
	} else {
		iwl_write_umac_prph(trans, DBGC_IN_SAMPLE, params->in_sample);
		iwl_write_umac_prph(trans, DBGC_OUT_CTRL, params->out_ctrl);
	}
2995 2996

	return 0;
2997 2998
}

2999 3000 3001
int iwl_fw_dbg_stop_restart_recording(struct iwl_fw_runtime *fwrt,
				      struct iwl_fw_dbg_params *params,
				      bool stop)
3002
{
3003 3004
	int ret = 0;

3005
	/* if the FW crashed or not debug monitor cfg was given, there is
3006
	 * no point in changing the recording state
3007 3008 3009 3010
	 */
	if (test_bit(STATUS_FW_ERROR, &fwrt->trans->status) ||
	    (!fwrt->trans->dbg.dest_tlv &&
	     fwrt->trans->dbg.ini_dest == IWL_FW_INI_LOCATION_INVALID))
3011
		return 0;
3012

3013 3014 3015 3016 3017 3018 3019
	if (fw_has_capa(&fwrt->fw->ucode_capa,
			IWL_UCODE_TLV_CAPA_DBG_SUSPEND_RESUME_CMD_SUPP))
		ret = iwl_fw_dbg_suspend_resume_hcmd(fwrt->trans, stop);
	else if (stop)
		iwl_fw_dbg_stop_recording(fwrt->trans, params);
	else
		ret = iwl_fw_dbg_restart_recording(fwrt->trans, params);
3020
#ifdef CONFIG_IWLWIFI_DEBUGFS
3021 3022 3023 3024 3025 3026
	if (!ret) {
		if (stop)
			fwrt->trans->dbg.rec_on = false;
		else
			iwl_fw_set_dbg_rec_on(fwrt);
	}
3027
#endif
3028 3029

	return ret;
3030
}
3031
IWL_EXPORT_SYMBOL(iwl_fw_dbg_stop_restart_recording);