162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/* Copyright(c) 2013 - 2018 Intel Corporation. */
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci#include "i40e_diag.h"
562306a36Sopenharmony_ci#include "i40e_prototype.h"
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci/**
862306a36Sopenharmony_ci * i40e_diag_reg_pattern_test
962306a36Sopenharmony_ci * @hw: pointer to the hw struct
1062306a36Sopenharmony_ci * @reg: reg to be tested
1162306a36Sopenharmony_ci * @mask: bits to be touched
1262306a36Sopenharmony_ci **/
1362306a36Sopenharmony_cistatic int i40e_diag_reg_pattern_test(struct i40e_hw *hw,
1462306a36Sopenharmony_ci				      u32 reg, u32 mask)
1562306a36Sopenharmony_ci{
1662306a36Sopenharmony_ci	static const u32 patterns[] = {
1762306a36Sopenharmony_ci		0x5A5A5A5A, 0xA5A5A5A5, 0x00000000, 0xFFFFFFFF
1862306a36Sopenharmony_ci	};
1962306a36Sopenharmony_ci	u32 pat, val, orig_val;
2062306a36Sopenharmony_ci	int i;
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci	orig_val = rd32(hw, reg);
2362306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(patterns); i++) {
2462306a36Sopenharmony_ci		pat = patterns[i];
2562306a36Sopenharmony_ci		wr32(hw, reg, (pat & mask));
2662306a36Sopenharmony_ci		val = rd32(hw, reg);
2762306a36Sopenharmony_ci		if ((val & mask) != (pat & mask)) {
2862306a36Sopenharmony_ci			i40e_debug(hw, I40E_DEBUG_DIAG,
2962306a36Sopenharmony_ci				   "%s: reg pattern test failed - reg 0x%08x pat 0x%08x val 0x%08x\n",
3062306a36Sopenharmony_ci				   __func__, reg, pat, val);
3162306a36Sopenharmony_ci			return -EIO;
3262306a36Sopenharmony_ci		}
3362306a36Sopenharmony_ci	}
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci	wr32(hw, reg, orig_val);
3662306a36Sopenharmony_ci	val = rd32(hw, reg);
3762306a36Sopenharmony_ci	if (val != orig_val) {
3862306a36Sopenharmony_ci		i40e_debug(hw, I40E_DEBUG_DIAG,
3962306a36Sopenharmony_ci			   "%s: reg restore test failed - reg 0x%08x orig_val 0x%08x val 0x%08x\n",
4062306a36Sopenharmony_ci			   __func__, reg, orig_val, val);
4162306a36Sopenharmony_ci		return -EIO;
4262306a36Sopenharmony_ci	}
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	return 0;
4562306a36Sopenharmony_ci}
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ciconst struct i40e_diag_reg_test_info i40e_reg_list[] = {
4862306a36Sopenharmony_ci	/* offset               mask         elements   stride */
4962306a36Sopenharmony_ci	{I40E_QTX_CTL(0),       0x0000FFBF, 1,
5062306a36Sopenharmony_ci		I40E_QTX_CTL(1) - I40E_QTX_CTL(0)},
5162306a36Sopenharmony_ci	{I40E_PFINT_ITR0(0),    0x00000FFF, 3,
5262306a36Sopenharmony_ci		I40E_PFINT_ITR0(1) - I40E_PFINT_ITR0(0)},
5362306a36Sopenharmony_ci	{I40E_PFINT_ITRN(0, 0), 0x00000FFF, 1,
5462306a36Sopenharmony_ci		I40E_PFINT_ITRN(0, 1) - I40E_PFINT_ITRN(0, 0)},
5562306a36Sopenharmony_ci	{I40E_PFINT_ITRN(1, 0), 0x00000FFF, 1,
5662306a36Sopenharmony_ci		I40E_PFINT_ITRN(1, 1) - I40E_PFINT_ITRN(1, 0)},
5762306a36Sopenharmony_ci	{I40E_PFINT_ITRN(2, 0), 0x00000FFF, 1,
5862306a36Sopenharmony_ci		I40E_PFINT_ITRN(2, 1) - I40E_PFINT_ITRN(2, 0)},
5962306a36Sopenharmony_ci	{I40E_PFINT_STAT_CTL0,  0x0000000C, 1, 0},
6062306a36Sopenharmony_ci	{I40E_PFINT_LNKLST0,    0x00001FFF, 1, 0},
6162306a36Sopenharmony_ci	{I40E_PFINT_LNKLSTN(0), 0x000007FF, 1,
6262306a36Sopenharmony_ci		I40E_PFINT_LNKLSTN(1) - I40E_PFINT_LNKLSTN(0)},
6362306a36Sopenharmony_ci	{I40E_QINT_TQCTL(0),    0x000000FF, 1,
6462306a36Sopenharmony_ci		I40E_QINT_TQCTL(1) - I40E_QINT_TQCTL(0)},
6562306a36Sopenharmony_ci	{I40E_QINT_RQCTL(0),    0x000000FF, 1,
6662306a36Sopenharmony_ci		I40E_QINT_RQCTL(1) - I40E_QINT_RQCTL(0)},
6762306a36Sopenharmony_ci	{I40E_PFINT_ICR0_ENA,   0xF7F20000, 1, 0},
6862306a36Sopenharmony_ci	{ 0 }
6962306a36Sopenharmony_ci};
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci/**
7262306a36Sopenharmony_ci * i40e_diag_reg_test
7362306a36Sopenharmony_ci * @hw: pointer to the hw struct
7462306a36Sopenharmony_ci *
7562306a36Sopenharmony_ci * Perform registers diagnostic test
7662306a36Sopenharmony_ci **/
7762306a36Sopenharmony_ciint i40e_diag_reg_test(struct i40e_hw *hw)
7862306a36Sopenharmony_ci{
7962306a36Sopenharmony_ci	int ret_code = 0;
8062306a36Sopenharmony_ci	u32 reg, mask;
8162306a36Sopenharmony_ci	u32 elements;
8262306a36Sopenharmony_ci	u32 i, j;
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	for (i = 0; i40e_reg_list[i].offset != 0 &&
8562306a36Sopenharmony_ci					     !ret_code; i++) {
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci		elements = i40e_reg_list[i].elements;
8862306a36Sopenharmony_ci		/* set actual reg range for dynamically allocated resources */
8962306a36Sopenharmony_ci		if (i40e_reg_list[i].offset == I40E_QTX_CTL(0) &&
9062306a36Sopenharmony_ci		    hw->func_caps.num_tx_qp != 0)
9162306a36Sopenharmony_ci			elements = hw->func_caps.num_tx_qp;
9262306a36Sopenharmony_ci		if ((i40e_reg_list[i].offset == I40E_PFINT_ITRN(0, 0) ||
9362306a36Sopenharmony_ci		     i40e_reg_list[i].offset == I40E_PFINT_ITRN(1, 0) ||
9462306a36Sopenharmony_ci		     i40e_reg_list[i].offset == I40E_PFINT_ITRN(2, 0) ||
9562306a36Sopenharmony_ci		     i40e_reg_list[i].offset == I40E_QINT_TQCTL(0) ||
9662306a36Sopenharmony_ci		     i40e_reg_list[i].offset == I40E_QINT_RQCTL(0)) &&
9762306a36Sopenharmony_ci		    hw->func_caps.num_msix_vectors != 0)
9862306a36Sopenharmony_ci			elements = hw->func_caps.num_msix_vectors - 1;
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci		/* test register access */
10162306a36Sopenharmony_ci		mask = i40e_reg_list[i].mask;
10262306a36Sopenharmony_ci		for (j = 0; j < elements && !ret_code; j++) {
10362306a36Sopenharmony_ci			reg = i40e_reg_list[i].offset +
10462306a36Sopenharmony_ci			      (j * i40e_reg_list[i].stride);
10562306a36Sopenharmony_ci			ret_code = i40e_diag_reg_pattern_test(hw, reg, mask);
10662306a36Sopenharmony_ci		}
10762306a36Sopenharmony_ci	}
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	return ret_code;
11062306a36Sopenharmony_ci}
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci/**
11362306a36Sopenharmony_ci * i40e_diag_eeprom_test
11462306a36Sopenharmony_ci * @hw: pointer to the hw struct
11562306a36Sopenharmony_ci *
11662306a36Sopenharmony_ci * Perform EEPROM diagnostic test
11762306a36Sopenharmony_ci **/
11862306a36Sopenharmony_ciint i40e_diag_eeprom_test(struct i40e_hw *hw)
11962306a36Sopenharmony_ci{
12062306a36Sopenharmony_ci	int ret_code;
12162306a36Sopenharmony_ci	u16 reg_val;
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	/* read NVM control word and if NVM valid, validate EEPROM checksum*/
12462306a36Sopenharmony_ci	ret_code = i40e_read_nvm_word(hw, I40E_SR_NVM_CONTROL_WORD, &reg_val);
12562306a36Sopenharmony_ci	if (!ret_code &&
12662306a36Sopenharmony_ci	    ((reg_val & I40E_SR_CONTROL_WORD_1_MASK) ==
12762306a36Sopenharmony_ci	     BIT(I40E_SR_CONTROL_WORD_1_SHIFT)))
12862306a36Sopenharmony_ci		return i40e_validate_nvm_checksum(hw, NULL);
12962306a36Sopenharmony_ci	else
13062306a36Sopenharmony_ci		return -EIO;
13162306a36Sopenharmony_ci}
132