f24bf81b906a35de2fd0990433c172ed156e2f6b
[dpdk.git] / lib / librte_pmd_i40e / i40e / i40e_diag.c
1 /*******************************************************************************
2
3 Copyright (c) 2013 - 2014, Intel Corporation
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8
9  1. Redistributions of source code must retain the above copyright notice,
10     this list of conditions and the following disclaimer.
11
12  2. Redistributions in binary form must reproduce the above copyright
13     notice, this list of conditions and the following disclaimer in the
14     documentation and/or other materials provided with the distribution.
15
16  3. Neither the name of the Intel Corporation nor the names of its
17     contributors may be used to endorse or promote products derived from
18     this software without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
31
32 ***************************************************************************/
33
34 #include "i40e_diag.h"
35 #include "i40e_prototype.h"
36
37 /**
38  * i40e_diag_set_loopback
39  * @hw: pointer to the hw struct
40  * @mode: loopback mode
41  *
42  * Set chosen loopback mode
43  **/
44 enum i40e_status_code i40e_diag_set_loopback(struct i40e_hw *hw,
45                                              enum i40e_lb_mode mode)
46 {
47         enum i40e_status_code ret_code = I40E_SUCCESS;
48
49         if (i40e_aq_set_lb_modes(hw, mode, NULL))
50                 ret_code = I40E_ERR_DIAG_TEST_FAILED;
51
52         return ret_code;
53 }
54
55 /**
56  * i40e_diag_reg_pattern_test
57  * @hw: pointer to the hw struct
58  * @reg: reg to be tested
59  * @mask: bits to be touched
60  **/
61 static enum i40e_status_code i40e_diag_reg_pattern_test(struct i40e_hw *hw,
62                                                         u32 reg, u32 mask)
63 {
64         const u32 patterns[] = {0x5A5A5A5A, 0xA5A5A5A5, 0x00000000, 0xFFFFFFFF};
65         u32 pat, val, orig_val;
66         int i;
67
68         orig_val = rd32(hw, reg);
69         for (i = 0; i < ARRAY_SIZE(patterns); i++) {
70                 pat = patterns[i];
71                 wr32(hw, reg, (pat & mask));
72                 val = rd32(hw, reg);
73                 if ((val & mask) != (pat & mask)) {
74 #ifdef ETHTOOL_TEST
75                         i40e_debug(hw, I40E_DEBUG_DIAG,
76                                    "%s: reg pattern test failed - reg 0x%08x pat 0x%08x val 0x%08x\n",
77                                    __func__, reg, pat, val);
78 #endif
79                         return I40E_ERR_DIAG_TEST_FAILED;
80                 }
81         }
82
83         wr32(hw, reg, orig_val);
84         val = rd32(hw, reg);
85         if (val != orig_val) {
86 #ifdef ETHTOOL_TEST
87                 i40e_debug(hw, I40E_DEBUG_DIAG,
88                            "%s: reg restore test failed - reg 0x%08x orig_val 0x%08x val 0x%08x\n",
89                            __func__, reg, orig_val, val);
90 #endif
91                 return I40E_ERR_DIAG_TEST_FAILED;
92         }
93
94         return I40E_SUCCESS;
95 }
96
97 struct i40e_diag_reg_test_info i40e_reg_list[] = {
98         /* offset               mask         elements   stride */
99         {I40E_QTX_CTL(0),       0x0000FFBF, 1, I40E_QTX_CTL(1) - I40E_QTX_CTL(0)},
100         {I40E_PFINT_ITR0(0),    0x00000FFF, 3, I40E_PFINT_ITR0(1) - I40E_PFINT_ITR0(0)},
101         {I40E_PFINT_ITRN(0, 0), 0x00000FFF, 1, I40E_PFINT_ITRN(0, 1) - I40E_PFINT_ITRN(0, 0)},
102         {I40E_PFINT_ITRN(1, 0), 0x00000FFF, 1, I40E_PFINT_ITRN(1, 1) - I40E_PFINT_ITRN(1, 0)},
103         {I40E_PFINT_ITRN(2, 0), 0x00000FFF, 1, I40E_PFINT_ITRN(2, 1) - I40E_PFINT_ITRN(2, 0)},
104         {I40E_PFINT_STAT_CTL0,  0x0000000C, 1, 0},
105         {I40E_PFINT_LNKLST0,    0x00001FFF, 1, 0},
106         {I40E_PFINT_LNKLSTN(0), 0x000007FF, 1, I40E_PFINT_LNKLSTN(1) - I40E_PFINT_LNKLSTN(0)},
107         {I40E_QINT_TQCTL(0),    0x000000FF, 1, I40E_QINT_TQCTL(1) - I40E_QINT_TQCTL(0)},
108         {I40E_QINT_RQCTL(0),    0x000000FF, 1, I40E_QINT_RQCTL(1) - I40E_QINT_RQCTL(0)},
109         {I40E_PFINT_ICR0_ENA,   0xF7F20000, 1, 0},
110         { 0 }
111 };
112
113 /**
114  * i40e_diag_reg_test
115  * @hw: pointer to the hw struct
116  *
117  * Perform registers diagnostic test
118  **/
119 enum i40e_status_code i40e_diag_reg_test(struct i40e_hw *hw)
120 {
121         enum i40e_status_code ret_code = I40E_SUCCESS;
122         u32 reg, mask;
123         u32 i, j;
124
125         for (i = 0; i40e_reg_list[i].offset != 0 &&
126                                              ret_code == I40E_SUCCESS; i++) {
127
128                 /* set actual reg range for dynamically allocated resources */
129                 if (i40e_reg_list[i].offset == I40E_QTX_CTL(0) &&
130                     hw->func_caps.num_tx_qp != 0)
131                         i40e_reg_list[i].elements = hw->func_caps.num_tx_qp;
132                 if ((i40e_reg_list[i].offset == I40E_PFINT_ITRN(0, 0) ||
133                      i40e_reg_list[i].offset == I40E_PFINT_ITRN(1, 0) ||
134                      i40e_reg_list[i].offset == I40E_PFINT_ITRN(2, 0) ||
135                      i40e_reg_list[i].offset == I40E_QINT_TQCTL(0) ||
136                      i40e_reg_list[i].offset == I40E_QINT_RQCTL(0)) &&
137                     hw->func_caps.num_msix_vectors != 0)
138                         i40e_reg_list[i].elements =
139                                 hw->func_caps.num_msix_vectors - 1;
140
141                 /* test register access */
142                 mask = i40e_reg_list[i].mask;
143                 for (j = 0; j < i40e_reg_list[i].elements &&
144                             ret_code == I40E_SUCCESS; j++) {
145                         reg = i40e_reg_list[i].offset
146                                 + (j * i40e_reg_list[i].stride);
147                         ret_code = i40e_diag_reg_pattern_test(hw, reg, mask);
148                 }
149         }
150
151         return ret_code;
152 }
153
154 /**
155  * i40e_diag_eeprom_test
156  * @hw: pointer to the hw struct
157  *
158  * Perform EEPROM diagnostic test
159  **/
160 enum i40e_status_code i40e_diag_eeprom_test(struct i40e_hw *hw)
161 {
162         enum i40e_status_code ret_code;
163         u16 reg_val;
164
165         /* read NVM control word and if NVM valid, validate EEPROM checksum*/
166         ret_code = i40e_read_nvm_word(hw, I40E_SR_NVM_CONTROL_WORD, &reg_val);
167         if ((ret_code == I40E_SUCCESS) &&
168             ((reg_val & I40E_SR_CONTROL_WORD_1_MASK) ==
169              (0x01 << I40E_SR_CONTROL_WORD_1_SHIFT))) {
170                 ret_code = i40e_validate_nvm_checksum(hw, NULL);
171         } else {
172                 ret_code = I40E_ERR_DIAG_TEST_FAILED;
173         }
174
175         return ret_code;
176 }
177
178 /**
179  * i40e_diag_fw_alive_test
180  * @hw: pointer to the hw struct
181  *
182  * Perform FW alive diagnostic test
183  **/
184 enum i40e_status_code i40e_diag_fw_alive_test(struct i40e_hw *hw)
185 {
186         UNREFERENCED_1PARAMETER(hw);
187         return I40E_SUCCESS;
188 }