net/i40e/base: enhance loopback AQ command
[dpdk.git] / drivers / net / i40e / base / i40e_diag.c
1 /*******************************************************************************
2
3 Copyright (c) 2013 - 2015, 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         u8 speed = I40E_AQ_LB_SPEED_10G;
49         u8 level = 0;
50         u8 type = 0;
51
52         if (mode == I40E_LB_MODE_PHY_LOCAL) {
53                 level = I40E_AQ_LB_PHY_INT;
54                 type = I40E_AQ_LB_LOCAL;
55         } else if (mode == I40E_LB_MODE_PHY_REMOTE) {
56                 level = I40E_AQ_LB_PHY_INT;
57                 type = I40E_AQ_LB_FAR;
58         } else if (mode == I40E_LB_MODE_MAC_LOCAL) {
59                 level = I40E_AQ_LB_MAC;
60                 type = I40E_AQ_LB_LOCAL;
61         }
62         if (i40e_aq_set_lb_modes(hw, level, type, speed, NULL))
63                 ret_code = I40E_ERR_DIAG_TEST_FAILED;
64
65         return ret_code;
66 }
67
68 /**
69  * i40e_diag_reg_pattern_test
70  * @hw: pointer to the hw struct
71  * @reg: reg to be tested
72  * @mask: bits to be touched
73  **/
74 static enum i40e_status_code i40e_diag_reg_pattern_test(struct i40e_hw *hw,
75                                                         u32 reg, u32 mask)
76 {
77         const u32 patterns[] = {0x5A5A5A5A, 0xA5A5A5A5, 0x00000000, 0xFFFFFFFF};
78         u32 pat, val, orig_val;
79         int i;
80
81         orig_val = rd32(hw, reg);
82         for (i = 0; i < ARRAY_SIZE(patterns); i++) {
83                 pat = patterns[i];
84                 wr32(hw, reg, (pat & mask));
85                 val = rd32(hw, reg);
86                 if ((val & mask) != (pat & mask)) {
87                         return I40E_ERR_DIAG_TEST_FAILED;
88                 }
89         }
90
91         wr32(hw, reg, orig_val);
92         val = rd32(hw, reg);
93         if (val != orig_val) {
94                 return I40E_ERR_DIAG_TEST_FAILED;
95         }
96
97         return I40E_SUCCESS;
98 }
99
100 struct i40e_diag_reg_test_info i40e_reg_list[] = {
101         /* offset               mask         elements   stride */
102         {I40E_QTX_CTL(0),       0x0000FFBF, 1, I40E_QTX_CTL(1) - I40E_QTX_CTL(0)},
103         {I40E_PFINT_ITR0(0),    0x00000FFF, 3, I40E_PFINT_ITR0(1) - I40E_PFINT_ITR0(0)},
104         {I40E_PFINT_ITRN(0, 0), 0x00000FFF, 1, I40E_PFINT_ITRN(0, 1) - I40E_PFINT_ITRN(0, 0)},
105         {I40E_PFINT_ITRN(1, 0), 0x00000FFF, 1, I40E_PFINT_ITRN(1, 1) - I40E_PFINT_ITRN(1, 0)},
106         {I40E_PFINT_ITRN(2, 0), 0x00000FFF, 1, I40E_PFINT_ITRN(2, 1) - I40E_PFINT_ITRN(2, 0)},
107         {I40E_PFINT_STAT_CTL0,  0x0000000C, 1, 0},
108         {I40E_PFINT_LNKLST0,    0x00001FFF, 1, 0},
109         {I40E_PFINT_LNKLSTN(0), 0x000007FF, 1, I40E_PFINT_LNKLSTN(1) - I40E_PFINT_LNKLSTN(0)},
110         {I40E_QINT_TQCTL(0),    0x000000FF, 1, I40E_QINT_TQCTL(1) - I40E_QINT_TQCTL(0)},
111         {I40E_QINT_RQCTL(0),    0x000000FF, 1, I40E_QINT_RQCTL(1) - I40E_QINT_RQCTL(0)},
112         {I40E_PFINT_ICR0_ENA,   0xF7F20000, 1, 0},
113         { 0 }
114 };
115
116 /**
117  * i40e_diag_reg_test
118  * @hw: pointer to the hw struct
119  *
120  * Perform registers diagnostic test
121  **/
122 enum i40e_status_code i40e_diag_reg_test(struct i40e_hw *hw)
123 {
124         enum i40e_status_code ret_code = I40E_SUCCESS;
125         u32 reg, mask;
126         u32 i, j;
127
128         for (i = 0; i40e_reg_list[i].offset != 0 &&
129                                              ret_code == I40E_SUCCESS; i++) {
130
131                 /* set actual reg range for dynamically allocated resources */
132                 if (i40e_reg_list[i].offset == I40E_QTX_CTL(0) &&
133                     hw->func_caps.num_tx_qp != 0)
134                         i40e_reg_list[i].elements = hw->func_caps.num_tx_qp;
135                 if ((i40e_reg_list[i].offset == I40E_PFINT_ITRN(0, 0) ||
136                      i40e_reg_list[i].offset == I40E_PFINT_ITRN(1, 0) ||
137                      i40e_reg_list[i].offset == I40E_PFINT_ITRN(2, 0) ||
138                      i40e_reg_list[i].offset == I40E_QINT_TQCTL(0) ||
139                      i40e_reg_list[i].offset == I40E_QINT_RQCTL(0)) &&
140                     hw->func_caps.num_msix_vectors != 0)
141                         i40e_reg_list[i].elements =
142                                 hw->func_caps.num_msix_vectors - 1;
143
144                 /* test register access */
145                 mask = i40e_reg_list[i].mask;
146                 for (j = 0; j < i40e_reg_list[i].elements &&
147                             ret_code == I40E_SUCCESS; j++) {
148                         reg = i40e_reg_list[i].offset
149                                 + (j * i40e_reg_list[i].stride);
150                         ret_code = i40e_diag_reg_pattern_test(hw, reg, mask);
151                 }
152         }
153
154         return ret_code;
155 }
156
157 /**
158  * i40e_diag_eeprom_test
159  * @hw: pointer to the hw struct
160  *
161  * Perform EEPROM diagnostic test
162  **/
163 enum i40e_status_code i40e_diag_eeprom_test(struct i40e_hw *hw)
164 {
165         enum i40e_status_code ret_code;
166         u16 reg_val;
167
168         /* read NVM control word and if NVM valid, validate EEPROM checksum*/
169         ret_code = i40e_read_nvm_word(hw, I40E_SR_NVM_CONTROL_WORD, &reg_val);
170         if ((ret_code == I40E_SUCCESS) &&
171             ((reg_val & I40E_SR_CONTROL_WORD_1_MASK) ==
172              BIT(I40E_SR_CONTROL_WORD_1_SHIFT)))
173                 return i40e_validate_nvm_checksum(hw, NULL);
174         else
175                 return I40E_ERR_DIAG_TEST_FAILED;
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 }