blob: f3d1c85d7ab8b0ec5d6ff763712e2d6f32e1f628 [file] [log] [blame]
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Greg Rosedc641b72013-12-18 13:45:51 +00004 * Copyright(c) 2013 - 2014 Intel Corporation.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +00005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
Greg Rosedc641b72013-12-18 13:45:51 +000015 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000017 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
27#include "i40e_prototype.h"
28
29/**
Shannon Nelson3e261862014-02-06 05:51:06 +000030 * i40e_init_nvm_ops - Initialize NVM function pointers
31 * @hw: pointer to the HW structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000032 *
Shannon Nelson3e261862014-02-06 05:51:06 +000033 * Setup the function pointers and the NVM info structure. Should be called
34 * once per NVM initialization, e.g. inside the i40e_init_shared_code().
35 * Please notice that the NVM term is used here (& in all methods covered
36 * in this file) as an equivalent of the FLASH part mapped into the SR.
37 * We are accessing FLASH always thru the Shadow RAM.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000038 **/
39i40e_status i40e_init_nvm(struct i40e_hw *hw)
40{
41 struct i40e_nvm_info *nvm = &hw->nvm;
42 i40e_status ret_code = 0;
43 u32 fla, gens;
44 u8 sr_size;
45
46 /* The SR size is stored regardless of the nvm programming mode
47 * as the blank mode may be used in the factory line.
48 */
49 gens = rd32(hw, I40E_GLNVM_GENS);
50 sr_size = ((gens & I40E_GLNVM_GENS_SR_SIZE_MASK) >>
51 I40E_GLNVM_GENS_SR_SIZE_SHIFT);
Shannon Nelson3e261862014-02-06 05:51:06 +000052 /* Switching to words (sr_size contains power of 2KB) */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000053 nvm->sr_size = (1 << sr_size) * I40E_SR_WORDS_IN_1KB;
54
Shannon Nelson3e261862014-02-06 05:51:06 +000055 /* Check if we are in the normal or blank NVM programming mode */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000056 fla = rd32(hw, I40E_GLNVM_FLA);
Shannon Nelson3e261862014-02-06 05:51:06 +000057 if (fla & I40E_GLNVM_FLA_LOCKED_MASK) { /* Normal programming mode */
58 /* Max NVM timeout */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000059 nvm->timeout = I40E_MAX_NVM_TIMEOUT;
60 nvm->blank_nvm_mode = false;
Shannon Nelson3e261862014-02-06 05:51:06 +000061 } else { /* Blank programming mode */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000062 nvm->blank_nvm_mode = true;
63 ret_code = I40E_ERR_NVM_BLANK_MODE;
Shannon Nelson74d0d0e2014-11-13 08:23:15 +000064 i40e_debug(hw, I40E_DEBUG_NVM, "NVM init error: unsupported blank mode.\n");
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000065 }
66
67 return ret_code;
68}
69
70/**
Shannon Nelson3e261862014-02-06 05:51:06 +000071 * i40e_acquire_nvm - Generic request for acquiring the NVM ownership
72 * @hw: pointer to the HW structure
73 * @access: NVM access type (read or write)
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000074 *
Shannon Nelson3e261862014-02-06 05:51:06 +000075 * This function will request NVM ownership for reading
76 * via the proper Admin Command.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000077 **/
78i40e_status i40e_acquire_nvm(struct i40e_hw *hw,
79 enum i40e_aq_resource_access_type access)
80{
81 i40e_status ret_code = 0;
82 u64 gtime, timeout;
Shannon Nelsonc509c1d2014-11-13 08:23:19 +000083 u64 time_left = 0;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000084
85 if (hw->nvm.blank_nvm_mode)
86 goto i40e_i40e_acquire_nvm_exit;
87
88 ret_code = i40e_aq_request_resource(hw, I40E_NVM_RESOURCE_ID, access,
Shannon Nelsonc509c1d2014-11-13 08:23:19 +000089 0, &time_left, NULL);
Shannon Nelson3e261862014-02-06 05:51:06 +000090 /* Reading the Global Device Timer */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000091 gtime = rd32(hw, I40E_GLVFGEN_TIMER);
92
Shannon Nelson3e261862014-02-06 05:51:06 +000093 /* Store the timeout */
Shannon Nelsonc509c1d2014-11-13 08:23:19 +000094 hw->nvm.hw_semaphore_timeout = I40E_MS_TO_GTIME(time_left) + gtime;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000095
96 if (ret_code) {
Shannon Nelson3e261862014-02-06 05:51:06 +000097 /* Poll until the current NVM owner timeouts */
Shannon Nelsonc509c1d2014-11-13 08:23:19 +000098 timeout = I40E_MS_TO_GTIME(I40E_MAX_NVM_TIMEOUT) + gtime;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000099 while (gtime < timeout) {
100 usleep_range(10000, 20000);
Shannon Nelsonc509c1d2014-11-13 08:23:19 +0000101 gtime = rd32(hw, I40E_GLVFGEN_TIMER);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000102 ret_code = i40e_aq_request_resource(hw,
103 I40E_NVM_RESOURCE_ID,
Shannon Nelsonc509c1d2014-11-13 08:23:19 +0000104 access, 0, &time_left,
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000105 NULL);
106 if (!ret_code) {
107 hw->nvm.hw_semaphore_timeout =
Shannon Nelsonc509c1d2014-11-13 08:23:19 +0000108 I40E_MS_TO_GTIME(time_left) + gtime;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000109 break;
110 }
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000111 }
112 if (ret_code) {
113 hw->nvm.hw_semaphore_timeout = 0;
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000114 i40e_debug(hw, I40E_DEBUG_NVM,
115 "NVM acquire timed out, wait %llu ms before trying again.\n",
Shannon Nelsonc509c1d2014-11-13 08:23:19 +0000116 time_left);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000117 }
118 }
119
120i40e_i40e_acquire_nvm_exit:
121 return ret_code;
122}
123
124/**
Shannon Nelson3e261862014-02-06 05:51:06 +0000125 * i40e_release_nvm - Generic request for releasing the NVM ownership
126 * @hw: pointer to the HW structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000127 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000128 * This function will release NVM resource via the proper Admin Command.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000129 **/
130void i40e_release_nvm(struct i40e_hw *hw)
131{
132 if (!hw->nvm.blank_nvm_mode)
133 i40e_aq_release_resource(hw, I40E_NVM_RESOURCE_ID, 0, NULL);
134}
135
136/**
Shannon Nelson3e261862014-02-06 05:51:06 +0000137 * i40e_poll_sr_srctl_done_bit - Polls the GLNVM_SRCTL done bit
138 * @hw: pointer to the HW structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000139 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000140 * Polls the SRCTL Shadow RAM register done bit.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000141 **/
142static i40e_status i40e_poll_sr_srctl_done_bit(struct i40e_hw *hw)
143{
144 i40e_status ret_code = I40E_ERR_TIMEOUT;
145 u32 srctl, wait_cnt;
146
Shannon Nelson3e261862014-02-06 05:51:06 +0000147 /* Poll the I40E_GLNVM_SRCTL until the done bit is set */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000148 for (wait_cnt = 0; wait_cnt < I40E_SRRD_SRCTL_ATTEMPTS; wait_cnt++) {
149 srctl = rd32(hw, I40E_GLNVM_SRCTL);
150 if (srctl & I40E_GLNVM_SRCTL_DONE_MASK) {
151 ret_code = 0;
152 break;
153 }
154 udelay(5);
155 }
156 if (ret_code == I40E_ERR_TIMEOUT)
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000157 i40e_debug(hw, I40E_DEBUG_NVM, "Done bit in GLNVM_SRCTL not set");
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000158 return ret_code;
159}
160
161/**
Shannon Nelson3e261862014-02-06 05:51:06 +0000162 * i40e_read_nvm_word - Reads Shadow RAM
163 * @hw: pointer to the HW structure
164 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
165 * @data: word read from the Shadow RAM
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000166 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000167 * Reads one 16 bit word from the Shadow RAM using the GLNVM_SRCTL register.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000168 **/
Shannon Nelsona4bcfbb2013-12-11 08:17:15 +0000169i40e_status i40e_read_nvm_word(struct i40e_hw *hw, u16 offset,
170 u16 *data)
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000171{
172 i40e_status ret_code = I40E_ERR_TIMEOUT;
173 u32 sr_reg;
174
175 if (offset >= hw->nvm.sr_size) {
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000176 i40e_debug(hw, I40E_DEBUG_NVM,
177 "NVM read error: offset %d beyond Shadow RAM limit %d\n",
178 offset, hw->nvm.sr_size);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000179 ret_code = I40E_ERR_PARAM;
180 goto read_nvm_exit;
181 }
182
Shannon Nelson3e261862014-02-06 05:51:06 +0000183 /* Poll the done bit first */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000184 ret_code = i40e_poll_sr_srctl_done_bit(hw);
185 if (!ret_code) {
Shannon Nelson3e261862014-02-06 05:51:06 +0000186 /* Write the address and start reading */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000187 sr_reg = (u32)(offset << I40E_GLNVM_SRCTL_ADDR_SHIFT) |
188 (1 << I40E_GLNVM_SRCTL_START_SHIFT);
189 wr32(hw, I40E_GLNVM_SRCTL, sr_reg);
190
Shannon Nelson3e261862014-02-06 05:51:06 +0000191 /* Poll I40E_GLNVM_SRCTL until the done bit is set */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000192 ret_code = i40e_poll_sr_srctl_done_bit(hw);
193 if (!ret_code) {
194 sr_reg = rd32(hw, I40E_GLNVM_SRDATA);
195 *data = (u16)((sr_reg &
196 I40E_GLNVM_SRDATA_RDDATA_MASK)
197 >> I40E_GLNVM_SRDATA_RDDATA_SHIFT);
198 }
199 }
200 if (ret_code)
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000201 i40e_debug(hw, I40E_DEBUG_NVM,
202 "NVM read error: Couldn't access Shadow RAM address: 0x%x\n",
203 offset);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000204
205read_nvm_exit:
206 return ret_code;
207}
208
209/**
Shannon Nelson3e261862014-02-06 05:51:06 +0000210 * i40e_read_nvm_buffer - Reads Shadow RAM buffer
211 * @hw: pointer to the HW structure
212 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
213 * @words: (in) number of words to read; (out) number of words actually read
214 * @data: words read from the Shadow RAM
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000215 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000216 * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_srrd()
217 * method. The buffer read is preceded by the NVM ownership take
218 * and followed by the release.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000219 **/
220i40e_status i40e_read_nvm_buffer(struct i40e_hw *hw, u16 offset,
221 u16 *words, u16 *data)
222{
223 i40e_status ret_code = 0;
224 u16 index, word;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000225
Shannon Nelson3e261862014-02-06 05:51:06 +0000226 /* Loop thru the selected region */
Shannon Nelsona4bcfbb2013-12-11 08:17:15 +0000227 for (word = 0; word < *words; word++) {
228 index = offset + word;
229 ret_code = i40e_read_nvm_word(hw, index, &data[word]);
230 if (ret_code)
231 break;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000232 }
233
Shannon Nelson3e261862014-02-06 05:51:06 +0000234 /* Update the number of words read from the Shadow RAM */
Shannon Nelsona4bcfbb2013-12-11 08:17:15 +0000235 *words = word;
236
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000237 return ret_code;
238}
239
240/**
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000241 * i40e_write_nvm_aq - Writes Shadow RAM.
242 * @hw: pointer to the HW structure.
243 * @module_pointer: module pointer location in words from the NVM beginning
244 * @offset: offset in words from module start
245 * @words: number of words to write
246 * @data: buffer with words to write to the Shadow RAM
247 * @last_command: tells the AdminQ that this is the last command
248 *
249 * Writes a 16 bit words buffer to the Shadow RAM using the admin command.
250 **/
Wei Yongjun952d9632014-07-30 09:02:53 +0000251static i40e_status i40e_write_nvm_aq(struct i40e_hw *hw, u8 module_pointer,
252 u32 offset, u16 words, void *data,
253 bool last_command)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000254{
255 i40e_status ret_code = I40E_ERR_NVM;
256
257 /* Here we are checking the SR limit only for the flat memory model.
258 * We cannot do it for the module-based model, as we did not acquire
259 * the NVM resource yet (we cannot get the module pointer value).
260 * Firmware will check the module-based model.
261 */
262 if ((offset + words) > hw->nvm.sr_size)
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000263 i40e_debug(hw, I40E_DEBUG_NVM,
264 "NVM write error: offset %d beyond Shadow RAM limit %d\n",
265 (offset + words), hw->nvm.sr_size);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000266 else if (words > I40E_SR_SECTOR_SIZE_IN_WORDS)
267 /* We can write only up to 4KB (one sector), in one AQ write */
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000268 i40e_debug(hw, I40E_DEBUG_NVM,
269 "NVM write fail error: tried to write %d words, limit is %d.\n",
270 words, I40E_SR_SECTOR_SIZE_IN_WORDS);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000271 else if (((offset + (words - 1)) / I40E_SR_SECTOR_SIZE_IN_WORDS)
272 != (offset / I40E_SR_SECTOR_SIZE_IN_WORDS))
273 /* A single write cannot spread over two sectors */
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000274 i40e_debug(hw, I40E_DEBUG_NVM,
275 "NVM write error: cannot spread over two sectors in a single write offset=%d words=%d\n",
276 offset, words);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000277 else
278 ret_code = i40e_aq_update_nvm(hw, module_pointer,
279 2 * offset, /*bytes*/
280 2 * words, /*bytes*/
281 data, last_command, NULL);
282
283 return ret_code;
284}
285
286/**
Shannon Nelson3e261862014-02-06 05:51:06 +0000287 * i40e_calc_nvm_checksum - Calculates and returns the checksum
288 * @hw: pointer to hardware structure
289 * @checksum: pointer to the checksum
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000290 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000291 * This function calculates SW Checksum that covers the whole 64kB shadow RAM
292 * except the VPD and PCIe ALT Auto-load modules. The structure and size of VPD
293 * is customer specific and unknown. Therefore, this function skips all maximum
294 * possible size of VPD (1kB).
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000295 **/
296static i40e_status i40e_calc_nvm_checksum(struct i40e_hw *hw,
297 u16 *checksum)
298{
299 i40e_status ret_code = 0;
300 u16 pcie_alt_module = 0;
301 u16 checksum_local = 0;
302 u16 vpd_module = 0;
303 u16 word = 0;
304 u32 i = 0;
305
306 /* read pointer to VPD area */
Shannon Nelsona4bcfbb2013-12-11 08:17:15 +0000307 ret_code = i40e_read_nvm_word(hw, I40E_SR_VPD_PTR, &vpd_module);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000308 if (ret_code) {
309 ret_code = I40E_ERR_NVM_CHECKSUM;
310 goto i40e_calc_nvm_checksum_exit;
311 }
312
313 /* read pointer to PCIe Alt Auto-load module */
Shannon Nelsona4bcfbb2013-12-11 08:17:15 +0000314 ret_code = i40e_read_nvm_word(hw, I40E_SR_PCIE_ALT_AUTO_LOAD_PTR,
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000315 &pcie_alt_module);
316 if (ret_code) {
317 ret_code = I40E_ERR_NVM_CHECKSUM;
318 goto i40e_calc_nvm_checksum_exit;
319 }
320
321 /* Calculate SW checksum that covers the whole 64kB shadow RAM
322 * except the VPD and PCIe ALT Auto-load modules
323 */
324 for (i = 0; i < hw->nvm.sr_size; i++) {
325 /* Skip Checksum word */
326 if (i == I40E_SR_SW_CHECKSUM_WORD)
327 i++;
328 /* Skip VPD module (convert byte size to word count) */
329 if (i == (u32)vpd_module) {
330 i += (I40E_SR_VPD_MODULE_MAX_SIZE / 2);
331 if (i >= hw->nvm.sr_size)
332 break;
333 }
334 /* Skip PCIe ALT module (convert byte size to word count) */
335 if (i == (u32)pcie_alt_module) {
336 i += (I40E_SR_PCIE_ALT_MODULE_MAX_SIZE / 2);
337 if (i >= hw->nvm.sr_size)
338 break;
339 }
340
Shannon Nelsona4bcfbb2013-12-11 08:17:15 +0000341 ret_code = i40e_read_nvm_word(hw, (u16)i, &word);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000342 if (ret_code) {
343 ret_code = I40E_ERR_NVM_CHECKSUM;
344 goto i40e_calc_nvm_checksum_exit;
345 }
346 checksum_local += word;
347 }
348
349 *checksum = (u16)I40E_SR_SW_CHECKSUM_BASE - checksum_local;
350
351i40e_calc_nvm_checksum_exit:
352 return ret_code;
353}
354
355/**
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000356 * i40e_update_nvm_checksum - Updates the NVM checksum
357 * @hw: pointer to hardware structure
358 *
359 * NVM ownership must be acquired before calling this function and released
360 * on ARQ completion event reception by caller.
361 * This function will commit SR to NVM.
362 **/
363i40e_status i40e_update_nvm_checksum(struct i40e_hw *hw)
364{
365 i40e_status ret_code = 0;
366 u16 checksum;
367
368 ret_code = i40e_calc_nvm_checksum(hw, &checksum);
369 if (!ret_code)
370 ret_code = i40e_write_nvm_aq(hw, 0x00, I40E_SR_SW_CHECKSUM_WORD,
371 1, &checksum, true);
372
373 return ret_code;
374}
375
376/**
Shannon Nelson3e261862014-02-06 05:51:06 +0000377 * i40e_validate_nvm_checksum - Validate EEPROM checksum
378 * @hw: pointer to hardware structure
379 * @checksum: calculated checksum
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000380 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000381 * Performs checksum calculation and validates the NVM SW checksum. If the
382 * caller does not need checksum, the value can be NULL.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000383 **/
384i40e_status i40e_validate_nvm_checksum(struct i40e_hw *hw,
385 u16 *checksum)
386{
387 i40e_status ret_code = 0;
388 u16 checksum_sr = 0;
Jesse Brandeburge15c9fa2014-01-17 15:36:31 -0800389 u16 checksum_local = 0;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000390
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000391 ret_code = i40e_calc_nvm_checksum(hw, &checksum_local);
392 if (ret_code)
Kamil Krawczyk7a208e82014-06-04 04:22:36 +0000393 goto i40e_validate_nvm_checksum_exit;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000394
395 /* Do not use i40e_read_nvm_word() because we do not want to take
396 * the synchronization semaphores twice here.
397 */
Shannon Nelsona4bcfbb2013-12-11 08:17:15 +0000398 i40e_read_nvm_word(hw, I40E_SR_SW_CHECKSUM_WORD, &checksum_sr);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000399
400 /* Verify read checksum from EEPROM is the same as
401 * calculated checksum
402 */
403 if (checksum_local != checksum_sr)
404 ret_code = I40E_ERR_NVM_CHECKSUM;
405
406 /* If the user cares, return the calculated checksum */
407 if (checksum)
408 *checksum = checksum_local;
409
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000410i40e_validate_nvm_checksum_exit:
411 return ret_code;
412}
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000413
414static i40e_status i40e_nvmupd_state_init(struct i40e_hw *hw,
415 struct i40e_nvm_access *cmd,
416 u8 *bytes, int *errno);
417static i40e_status i40e_nvmupd_state_reading(struct i40e_hw *hw,
418 struct i40e_nvm_access *cmd,
419 u8 *bytes, int *errno);
420static i40e_status i40e_nvmupd_state_writing(struct i40e_hw *hw,
421 struct i40e_nvm_access *cmd,
422 u8 *bytes, int *errno);
423static enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
424 struct i40e_nvm_access *cmd,
425 int *errno);
426static i40e_status i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
427 struct i40e_nvm_access *cmd,
428 int *errno);
429static i40e_status i40e_nvmupd_nvm_write(struct i40e_hw *hw,
430 struct i40e_nvm_access *cmd,
431 u8 *bytes, int *errno);
432static i40e_status i40e_nvmupd_nvm_read(struct i40e_hw *hw,
433 struct i40e_nvm_access *cmd,
434 u8 *bytes, int *errno);
435static inline u8 i40e_nvmupd_get_module(u32 val)
436{
437 return (u8)(val & I40E_NVM_MOD_PNT_MASK);
438}
439static inline u8 i40e_nvmupd_get_transaction(u32 val)
440{
441 return (u8)((val & I40E_NVM_TRANS_MASK) >> I40E_NVM_TRANS_SHIFT);
442}
443
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000444static char *i40e_nvm_update_state_str[] = {
445 "I40E_NVMUPD_INVALID",
446 "I40E_NVMUPD_READ_CON",
447 "I40E_NVMUPD_READ_SNT",
448 "I40E_NVMUPD_READ_LCB",
449 "I40E_NVMUPD_READ_SA",
450 "I40E_NVMUPD_WRITE_ERA",
451 "I40E_NVMUPD_WRITE_CON",
452 "I40E_NVMUPD_WRITE_SNT",
453 "I40E_NVMUPD_WRITE_LCB",
454 "I40E_NVMUPD_WRITE_SA",
455 "I40E_NVMUPD_CSUM_CON",
456 "I40E_NVMUPD_CSUM_SA",
457 "I40E_NVMUPD_CSUM_LCB",
458};
459
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000460/**
461 * i40e_nvmupd_command - Process an NVM update command
462 * @hw: pointer to hardware structure
463 * @cmd: pointer to nvm update command
464 * @bytes: pointer to the data buffer
465 * @errno: pointer to return error code
466 *
467 * Dispatches command depending on what update state is current
468 **/
469i40e_status i40e_nvmupd_command(struct i40e_hw *hw,
470 struct i40e_nvm_access *cmd,
471 u8 *bytes, int *errno)
472{
473 i40e_status status;
474
475 /* assume success */
476 *errno = 0;
477
478 switch (hw->nvmupd_state) {
479 case I40E_NVMUPD_STATE_INIT:
480 status = i40e_nvmupd_state_init(hw, cmd, bytes, errno);
481 break;
482
483 case I40E_NVMUPD_STATE_READING:
484 status = i40e_nvmupd_state_reading(hw, cmd, bytes, errno);
485 break;
486
487 case I40E_NVMUPD_STATE_WRITING:
488 status = i40e_nvmupd_state_writing(hw, cmd, bytes, errno);
489 break;
490
491 default:
492 /* invalid state, should never happen */
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000493 i40e_debug(hw, I40E_DEBUG_NVM,
494 "NVMUPD: no such state %d\n", hw->nvmupd_state);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000495 status = I40E_NOT_SUPPORTED;
496 *errno = -ESRCH;
497 break;
498 }
499 return status;
500}
501
502/**
503 * i40e_nvmupd_state_init - Handle NVM update state Init
504 * @hw: pointer to hardware structure
505 * @cmd: pointer to nvm update command buffer
506 * @bytes: pointer to the data buffer
507 * @errno: pointer to return error code
508 *
509 * Process legitimate commands of the Init state and conditionally set next
510 * state. Reject all other commands.
511 **/
512static i40e_status i40e_nvmupd_state_init(struct i40e_hw *hw,
513 struct i40e_nvm_access *cmd,
514 u8 *bytes, int *errno)
515{
516 i40e_status status = 0;
517 enum i40e_nvmupd_cmd upd_cmd;
518
519 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, errno);
520
521 switch (upd_cmd) {
522 case I40E_NVMUPD_READ_SA:
523 status = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
524 if (status) {
525 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
526 } else {
527 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, errno);
528 i40e_release_nvm(hw);
529 }
530 break;
531
532 case I40E_NVMUPD_READ_SNT:
533 status = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
534 if (status) {
535 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
536 } else {
537 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, errno);
Shannon Nelson0fdd0522014-11-13 08:23:20 +0000538 if (status)
539 i40e_release_nvm(hw);
540 else
541 hw->nvmupd_state = I40E_NVMUPD_STATE_READING;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000542 }
543 break;
544
545 case I40E_NVMUPD_WRITE_ERA:
546 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
547 if (status) {
548 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
549 } else {
550 status = i40e_nvmupd_nvm_erase(hw, cmd, errno);
551 if (status)
552 i40e_release_nvm(hw);
553 else
554 hw->aq.nvm_release_on_done = true;
555 }
556 break;
557
558 case I40E_NVMUPD_WRITE_SA:
559 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
560 if (status) {
561 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
562 } else {
563 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, errno);
564 if (status)
565 i40e_release_nvm(hw);
566 else
567 hw->aq.nvm_release_on_done = true;
568 }
569 break;
570
571 case I40E_NVMUPD_WRITE_SNT:
572 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
573 if (status) {
574 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
575 } else {
576 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, errno);
Shannon Nelson0fdd0522014-11-13 08:23:20 +0000577 if (status)
578 i40e_release_nvm(hw);
579 else
580 hw->nvmupd_state = I40E_NVMUPD_STATE_WRITING;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000581 }
582 break;
583
584 case I40E_NVMUPD_CSUM_SA:
585 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
586 if (status) {
587 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
588 } else {
589 status = i40e_update_nvm_checksum(hw);
590 if (status) {
591 *errno = hw->aq.asq_last_status ?
592 i40e_aq_rc_to_posix(hw->aq.asq_last_status) :
593 -EIO;
594 i40e_release_nvm(hw);
595 } else {
596 hw->aq.nvm_release_on_done = true;
597 }
598 }
599 break;
600
601 default:
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000602 i40e_debug(hw, I40E_DEBUG_NVM,
603 "NVMUPD: bad cmd %s in init state\n",
604 i40e_nvm_update_state_str[upd_cmd]);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000605 status = I40E_ERR_NVM;
606 *errno = -ESRCH;
607 break;
608 }
609 return status;
610}
611
612/**
613 * i40e_nvmupd_state_reading - Handle NVM update state Reading
614 * @hw: pointer to hardware structure
615 * @cmd: pointer to nvm update command buffer
616 * @bytes: pointer to the data buffer
617 * @errno: pointer to return error code
618 *
619 * NVM ownership is already held. Process legitimate commands and set any
620 * change in state; reject all other commands.
621 **/
622static i40e_status i40e_nvmupd_state_reading(struct i40e_hw *hw,
623 struct i40e_nvm_access *cmd,
624 u8 *bytes, int *errno)
625{
626 i40e_status status;
627 enum i40e_nvmupd_cmd upd_cmd;
628
629 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, errno);
630
631 switch (upd_cmd) {
632 case I40E_NVMUPD_READ_SA:
633 case I40E_NVMUPD_READ_CON:
634 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, errno);
635 break;
636
637 case I40E_NVMUPD_READ_LCB:
638 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, errno);
639 i40e_release_nvm(hw);
640 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
641 break;
642
643 default:
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000644 i40e_debug(hw, I40E_DEBUG_NVM,
645 "NVMUPD: bad cmd %s in reading state.\n",
646 i40e_nvm_update_state_str[upd_cmd]);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000647 status = I40E_NOT_SUPPORTED;
648 *errno = -ESRCH;
649 break;
650 }
651 return status;
652}
653
654/**
655 * i40e_nvmupd_state_writing - Handle NVM update state Writing
656 * @hw: pointer to hardware structure
657 * @cmd: pointer to nvm update command buffer
658 * @bytes: pointer to the data buffer
659 * @errno: pointer to return error code
660 *
661 * NVM ownership is already held. Process legitimate commands and set any
662 * change in state; reject all other commands
663 **/
664static i40e_status i40e_nvmupd_state_writing(struct i40e_hw *hw,
665 struct i40e_nvm_access *cmd,
666 u8 *bytes, int *errno)
667{
668 i40e_status status;
669 enum i40e_nvmupd_cmd upd_cmd;
670
671 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, errno);
672
673 switch (upd_cmd) {
674 case I40E_NVMUPD_WRITE_CON:
675 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, errno);
676 break;
677
678 case I40E_NVMUPD_WRITE_LCB:
679 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, errno);
Shannon Nelson0fdd0522014-11-13 08:23:20 +0000680 if (!status)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000681 hw->aq.nvm_release_on_done = true;
Shannon Nelson0fdd0522014-11-13 08:23:20 +0000682 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000683 break;
684
685 case I40E_NVMUPD_CSUM_CON:
686 status = i40e_update_nvm_checksum(hw);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000687 if (status) {
688 *errno = hw->aq.asq_last_status ?
689 i40e_aq_rc_to_posix(hw->aq.asq_last_status) :
690 -EIO;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000691 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
692 }
693 break;
694
Shannon Nelson0fdd0522014-11-13 08:23:20 +0000695 case I40E_NVMUPD_CSUM_LCB:
696 status = i40e_update_nvm_checksum(hw);
697 if (status)
698 *errno = hw->aq.asq_last_status ?
699 i40e_aq_rc_to_posix(hw->aq.asq_last_status) :
700 -EIO;
701 else
702 hw->aq.nvm_release_on_done = true;
703 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
704 break;
705
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000706 default:
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000707 i40e_debug(hw, I40E_DEBUG_NVM,
708 "NVMUPD: bad cmd %s in writing state.\n",
709 i40e_nvm_update_state_str[upd_cmd]);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000710 status = I40E_NOT_SUPPORTED;
711 *errno = -ESRCH;
712 break;
713 }
714 return status;
715}
716
717/**
718 * i40e_nvmupd_validate_command - Validate given command
719 * @hw: pointer to hardware structure
720 * @cmd: pointer to nvm update command buffer
721 * @errno: pointer to return error code
722 *
723 * Return one of the valid command types or I40E_NVMUPD_INVALID
724 **/
725static enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
726 struct i40e_nvm_access *cmd,
727 int *errno)
728{
729 enum i40e_nvmupd_cmd upd_cmd;
730 u8 transaction, module;
731
732 /* anything that doesn't match a recognized case is an error */
733 upd_cmd = I40E_NVMUPD_INVALID;
734
735 transaction = i40e_nvmupd_get_transaction(cmd->config);
736 module = i40e_nvmupd_get_module(cmd->config);
737
738 /* limits on data size */
739 if ((cmd->data_size < 1) ||
740 (cmd->data_size > I40E_NVMUPD_MAX_DATA)) {
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000741 i40e_debug(hw, I40E_DEBUG_NVM,
742 "i40e_nvmupd_validate_command data_size %d\n",
743 cmd->data_size);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000744 *errno = -EFAULT;
745 return I40E_NVMUPD_INVALID;
746 }
747
748 switch (cmd->command) {
749 case I40E_NVM_READ:
750 switch (transaction) {
751 case I40E_NVM_CON:
752 upd_cmd = I40E_NVMUPD_READ_CON;
753 break;
754 case I40E_NVM_SNT:
755 upd_cmd = I40E_NVMUPD_READ_SNT;
756 break;
757 case I40E_NVM_LCB:
758 upd_cmd = I40E_NVMUPD_READ_LCB;
759 break;
760 case I40E_NVM_SA:
761 upd_cmd = I40E_NVMUPD_READ_SA;
762 break;
763 }
764 break;
765
766 case I40E_NVM_WRITE:
767 switch (transaction) {
768 case I40E_NVM_CON:
769 upd_cmd = I40E_NVMUPD_WRITE_CON;
770 break;
771 case I40E_NVM_SNT:
772 upd_cmd = I40E_NVMUPD_WRITE_SNT;
773 break;
774 case I40E_NVM_LCB:
775 upd_cmd = I40E_NVMUPD_WRITE_LCB;
776 break;
777 case I40E_NVM_SA:
778 upd_cmd = I40E_NVMUPD_WRITE_SA;
779 break;
780 case I40E_NVM_ERA:
781 upd_cmd = I40E_NVMUPD_WRITE_ERA;
782 break;
783 case I40E_NVM_CSUM:
784 upd_cmd = I40E_NVMUPD_CSUM_CON;
785 break;
786 case (I40E_NVM_CSUM|I40E_NVM_SA):
787 upd_cmd = I40E_NVMUPD_CSUM_SA;
788 break;
789 case (I40E_NVM_CSUM|I40E_NVM_LCB):
790 upd_cmd = I40E_NVMUPD_CSUM_LCB;
791 break;
792 }
793 break;
794 }
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000795 i40e_debug(hw, I40E_DEBUG_NVM, "%s\n",
796 i40e_nvm_update_state_str[upd_cmd]);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000797
798 if (upd_cmd == I40E_NVMUPD_INVALID) {
799 *errno = -EFAULT;
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000800 i40e_debug(hw, I40E_DEBUG_NVM,
801 "i40e_nvmupd_validate_command returns %d errno %d\n",
802 upd_cmd, *errno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000803 }
804 return upd_cmd;
805}
806
807/**
808 * i40e_nvmupd_nvm_read - Read NVM
809 * @hw: pointer to hardware structure
810 * @cmd: pointer to nvm update command buffer
811 * @bytes: pointer to the data buffer
812 * @errno: pointer to return error code
813 *
814 * cmd structure contains identifiers and data buffer
815 **/
816static i40e_status i40e_nvmupd_nvm_read(struct i40e_hw *hw,
817 struct i40e_nvm_access *cmd,
818 u8 *bytes, int *errno)
819{
820 i40e_status status;
821 u8 module, transaction;
822 bool last;
823
824 transaction = i40e_nvmupd_get_transaction(cmd->config);
825 module = i40e_nvmupd_get_module(cmd->config);
826 last = (transaction == I40E_NVM_LCB) || (transaction == I40E_NVM_SA);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000827
828 status = i40e_aq_read_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
829 bytes, last, NULL);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000830 if (status) {
831 i40e_debug(hw, I40E_DEBUG_NVM,
832 "i40e_nvmupd_nvm_read mod 0x%x off 0x%x len 0x%x\n",
833 module, cmd->offset, cmd->data_size);
834 i40e_debug(hw, I40E_DEBUG_NVM,
835 "i40e_nvmupd_nvm_read status %d aq %d\n",
836 status, hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000837 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000838 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000839
840 return status;
841}
842
843/**
844 * i40e_nvmupd_nvm_erase - Erase an NVM module
845 * @hw: pointer to hardware structure
846 * @cmd: pointer to nvm update command buffer
847 * @errno: pointer to return error code
848 *
849 * module, offset, data_size and data are in cmd structure
850 **/
851static i40e_status i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
852 struct i40e_nvm_access *cmd,
853 int *errno)
854{
855 i40e_status status = 0;
856 u8 module, transaction;
857 bool last;
858
859 transaction = i40e_nvmupd_get_transaction(cmd->config);
860 module = i40e_nvmupd_get_module(cmd->config);
861 last = (transaction & I40E_NVM_LCB);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000862 status = i40e_aq_erase_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
863 last, NULL);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000864 if (status) {
865 i40e_debug(hw, I40E_DEBUG_NVM,
866 "i40e_nvmupd_nvm_erase mod 0x%x off 0x%x len 0x%x\n",
867 module, cmd->offset, cmd->data_size);
868 i40e_debug(hw, I40E_DEBUG_NVM,
869 "i40e_nvmupd_nvm_erase status %d aq %d\n",
870 status, hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000871 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000872 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000873
874 return status;
875}
876
877/**
878 * i40e_nvmupd_nvm_write - Write NVM
879 * @hw: pointer to hardware structure
880 * @cmd: pointer to nvm update command buffer
881 * @bytes: pointer to the data buffer
882 * @errno: pointer to return error code
883 *
884 * module, offset, data_size and data are in cmd structure
885 **/
886static i40e_status i40e_nvmupd_nvm_write(struct i40e_hw *hw,
887 struct i40e_nvm_access *cmd,
888 u8 *bytes, int *errno)
889{
890 i40e_status status = 0;
891 u8 module, transaction;
892 bool last;
893
894 transaction = i40e_nvmupd_get_transaction(cmd->config);
895 module = i40e_nvmupd_get_module(cmd->config);
896 last = (transaction & I40E_NVM_LCB);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000897
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000898 status = i40e_aq_update_nvm(hw, module, cmd->offset,
899 (u16)cmd->data_size, bytes, last, NULL);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000900 if (status) {
901 i40e_debug(hw, I40E_DEBUG_NVM,
902 "i40e_nvmupd_nvm_write mod 0x%x off 0x%x len 0x%x\n",
903 module, cmd->offset, cmd->data_size);
904 i40e_debug(hw, I40E_DEBUG_NVM,
905 "i40e_nvmupd_nvm_write status %d aq %d\n",
906 status, hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000907 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000908 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000909
910 return status;
911}