blob: f55e52b2a0036aec2c43f23ef5ec315091cb1708 [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);
538 hw->nvmupd_state = I40E_NVMUPD_STATE_READING;
539 }
540 break;
541
542 case I40E_NVMUPD_WRITE_ERA:
543 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
544 if (status) {
545 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
546 } else {
547 status = i40e_nvmupd_nvm_erase(hw, cmd, errno);
548 if (status)
549 i40e_release_nvm(hw);
550 else
551 hw->aq.nvm_release_on_done = true;
552 }
553 break;
554
555 case I40E_NVMUPD_WRITE_SA:
556 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
557 if (status) {
558 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
559 } else {
560 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, errno);
561 if (status)
562 i40e_release_nvm(hw);
563 else
564 hw->aq.nvm_release_on_done = true;
565 }
566 break;
567
568 case I40E_NVMUPD_WRITE_SNT:
569 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
570 if (status) {
571 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
572 } else {
573 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, errno);
574 hw->nvmupd_state = I40E_NVMUPD_STATE_WRITING;
575 }
576 break;
577
578 case I40E_NVMUPD_CSUM_SA:
579 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
580 if (status) {
581 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
582 } else {
583 status = i40e_update_nvm_checksum(hw);
584 if (status) {
585 *errno = hw->aq.asq_last_status ?
586 i40e_aq_rc_to_posix(hw->aq.asq_last_status) :
587 -EIO;
588 i40e_release_nvm(hw);
589 } else {
590 hw->aq.nvm_release_on_done = true;
591 }
592 }
593 break;
594
595 default:
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000596 i40e_debug(hw, I40E_DEBUG_NVM,
597 "NVMUPD: bad cmd %s in init state\n",
598 i40e_nvm_update_state_str[upd_cmd]);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000599 status = I40E_ERR_NVM;
600 *errno = -ESRCH;
601 break;
602 }
603 return status;
604}
605
606/**
607 * i40e_nvmupd_state_reading - Handle NVM update state Reading
608 * @hw: pointer to hardware structure
609 * @cmd: pointer to nvm update command buffer
610 * @bytes: pointer to the data buffer
611 * @errno: pointer to return error code
612 *
613 * NVM ownership is already held. Process legitimate commands and set any
614 * change in state; reject all other commands.
615 **/
616static i40e_status i40e_nvmupd_state_reading(struct i40e_hw *hw,
617 struct i40e_nvm_access *cmd,
618 u8 *bytes, int *errno)
619{
620 i40e_status status;
621 enum i40e_nvmupd_cmd upd_cmd;
622
623 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, errno);
624
625 switch (upd_cmd) {
626 case I40E_NVMUPD_READ_SA:
627 case I40E_NVMUPD_READ_CON:
628 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, errno);
629 break;
630
631 case I40E_NVMUPD_READ_LCB:
632 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, errno);
633 i40e_release_nvm(hw);
634 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
635 break;
636
637 default:
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000638 i40e_debug(hw, I40E_DEBUG_NVM,
639 "NVMUPD: bad cmd %s in reading state.\n",
640 i40e_nvm_update_state_str[upd_cmd]);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000641 status = I40E_NOT_SUPPORTED;
642 *errno = -ESRCH;
643 break;
644 }
645 return status;
646}
647
648/**
649 * i40e_nvmupd_state_writing - Handle NVM update state Writing
650 * @hw: pointer to hardware structure
651 * @cmd: pointer to nvm update command buffer
652 * @bytes: pointer to the data buffer
653 * @errno: pointer to return error code
654 *
655 * NVM ownership is already held. Process legitimate commands and set any
656 * change in state; reject all other commands
657 **/
658static i40e_status i40e_nvmupd_state_writing(struct i40e_hw *hw,
659 struct i40e_nvm_access *cmd,
660 u8 *bytes, int *errno)
661{
662 i40e_status status;
663 enum i40e_nvmupd_cmd upd_cmd;
664
665 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, errno);
666
667 switch (upd_cmd) {
668 case I40E_NVMUPD_WRITE_CON:
669 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, errno);
670 break;
671
672 case I40E_NVMUPD_WRITE_LCB:
673 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, errno);
674 if (!status) {
675 hw->aq.nvm_release_on_done = true;
676 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
677 }
678 break;
679
680 case I40E_NVMUPD_CSUM_CON:
681 status = i40e_update_nvm_checksum(hw);
682 if (status)
683 *errno = hw->aq.asq_last_status ?
684 i40e_aq_rc_to_posix(hw->aq.asq_last_status) :
685 -EIO;
686 break;
687
688 case I40E_NVMUPD_CSUM_LCB:
689 status = i40e_update_nvm_checksum(hw);
690 if (status) {
691 *errno = hw->aq.asq_last_status ?
692 i40e_aq_rc_to_posix(hw->aq.asq_last_status) :
693 -EIO;
694 } else {
695 hw->aq.nvm_release_on_done = true;
696 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
697 }
698 break;
699
700 default:
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000701 i40e_debug(hw, I40E_DEBUG_NVM,
702 "NVMUPD: bad cmd %s in writing state.\n",
703 i40e_nvm_update_state_str[upd_cmd]);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000704 status = I40E_NOT_SUPPORTED;
705 *errno = -ESRCH;
706 break;
707 }
708 return status;
709}
710
711/**
712 * i40e_nvmupd_validate_command - Validate given command
713 * @hw: pointer to hardware structure
714 * @cmd: pointer to nvm update command buffer
715 * @errno: pointer to return error code
716 *
717 * Return one of the valid command types or I40E_NVMUPD_INVALID
718 **/
719static enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
720 struct i40e_nvm_access *cmd,
721 int *errno)
722{
723 enum i40e_nvmupd_cmd upd_cmd;
724 u8 transaction, module;
725
726 /* anything that doesn't match a recognized case is an error */
727 upd_cmd = I40E_NVMUPD_INVALID;
728
729 transaction = i40e_nvmupd_get_transaction(cmd->config);
730 module = i40e_nvmupd_get_module(cmd->config);
731
732 /* limits on data size */
733 if ((cmd->data_size < 1) ||
734 (cmd->data_size > I40E_NVMUPD_MAX_DATA)) {
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000735 i40e_debug(hw, I40E_DEBUG_NVM,
736 "i40e_nvmupd_validate_command data_size %d\n",
737 cmd->data_size);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000738 *errno = -EFAULT;
739 return I40E_NVMUPD_INVALID;
740 }
741
742 switch (cmd->command) {
743 case I40E_NVM_READ:
744 switch (transaction) {
745 case I40E_NVM_CON:
746 upd_cmd = I40E_NVMUPD_READ_CON;
747 break;
748 case I40E_NVM_SNT:
749 upd_cmd = I40E_NVMUPD_READ_SNT;
750 break;
751 case I40E_NVM_LCB:
752 upd_cmd = I40E_NVMUPD_READ_LCB;
753 break;
754 case I40E_NVM_SA:
755 upd_cmd = I40E_NVMUPD_READ_SA;
756 break;
757 }
758 break;
759
760 case I40E_NVM_WRITE:
761 switch (transaction) {
762 case I40E_NVM_CON:
763 upd_cmd = I40E_NVMUPD_WRITE_CON;
764 break;
765 case I40E_NVM_SNT:
766 upd_cmd = I40E_NVMUPD_WRITE_SNT;
767 break;
768 case I40E_NVM_LCB:
769 upd_cmd = I40E_NVMUPD_WRITE_LCB;
770 break;
771 case I40E_NVM_SA:
772 upd_cmd = I40E_NVMUPD_WRITE_SA;
773 break;
774 case I40E_NVM_ERA:
775 upd_cmd = I40E_NVMUPD_WRITE_ERA;
776 break;
777 case I40E_NVM_CSUM:
778 upd_cmd = I40E_NVMUPD_CSUM_CON;
779 break;
780 case (I40E_NVM_CSUM|I40E_NVM_SA):
781 upd_cmd = I40E_NVMUPD_CSUM_SA;
782 break;
783 case (I40E_NVM_CSUM|I40E_NVM_LCB):
784 upd_cmd = I40E_NVMUPD_CSUM_LCB;
785 break;
786 }
787 break;
788 }
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000789 i40e_debug(hw, I40E_DEBUG_NVM, "%s\n",
790 i40e_nvm_update_state_str[upd_cmd]);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000791
792 if (upd_cmd == I40E_NVMUPD_INVALID) {
793 *errno = -EFAULT;
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000794 i40e_debug(hw, I40E_DEBUG_NVM,
795 "i40e_nvmupd_validate_command returns %d errno %d\n",
796 upd_cmd, *errno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000797 }
798 return upd_cmd;
799}
800
801/**
802 * i40e_nvmupd_nvm_read - Read NVM
803 * @hw: pointer to hardware structure
804 * @cmd: pointer to nvm update command buffer
805 * @bytes: pointer to the data buffer
806 * @errno: pointer to return error code
807 *
808 * cmd structure contains identifiers and data buffer
809 **/
810static i40e_status i40e_nvmupd_nvm_read(struct i40e_hw *hw,
811 struct i40e_nvm_access *cmd,
812 u8 *bytes, int *errno)
813{
814 i40e_status status;
815 u8 module, transaction;
816 bool last;
817
818 transaction = i40e_nvmupd_get_transaction(cmd->config);
819 module = i40e_nvmupd_get_module(cmd->config);
820 last = (transaction == I40E_NVM_LCB) || (transaction == I40E_NVM_SA);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000821
822 status = i40e_aq_read_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
823 bytes, last, NULL);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000824 if (status) {
825 i40e_debug(hw, I40E_DEBUG_NVM,
826 "i40e_nvmupd_nvm_read mod 0x%x off 0x%x len 0x%x\n",
827 module, cmd->offset, cmd->data_size);
828 i40e_debug(hw, I40E_DEBUG_NVM,
829 "i40e_nvmupd_nvm_read status %d aq %d\n",
830 status, hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000831 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000832 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000833
834 return status;
835}
836
837/**
838 * i40e_nvmupd_nvm_erase - Erase an NVM module
839 * @hw: pointer to hardware structure
840 * @cmd: pointer to nvm update command buffer
841 * @errno: pointer to return error code
842 *
843 * module, offset, data_size and data are in cmd structure
844 **/
845static i40e_status i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
846 struct i40e_nvm_access *cmd,
847 int *errno)
848{
849 i40e_status status = 0;
850 u8 module, transaction;
851 bool last;
852
853 transaction = i40e_nvmupd_get_transaction(cmd->config);
854 module = i40e_nvmupd_get_module(cmd->config);
855 last = (transaction & I40E_NVM_LCB);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000856 status = i40e_aq_erase_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
857 last, NULL);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000858 if (status) {
859 i40e_debug(hw, I40E_DEBUG_NVM,
860 "i40e_nvmupd_nvm_erase mod 0x%x off 0x%x len 0x%x\n",
861 module, cmd->offset, cmd->data_size);
862 i40e_debug(hw, I40E_DEBUG_NVM,
863 "i40e_nvmupd_nvm_erase status %d aq %d\n",
864 status, hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000865 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000866 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000867
868 return status;
869}
870
871/**
872 * i40e_nvmupd_nvm_write - Write NVM
873 * @hw: pointer to hardware structure
874 * @cmd: pointer to nvm update command buffer
875 * @bytes: pointer to the data buffer
876 * @errno: pointer to return error code
877 *
878 * module, offset, data_size and data are in cmd structure
879 **/
880static i40e_status i40e_nvmupd_nvm_write(struct i40e_hw *hw,
881 struct i40e_nvm_access *cmd,
882 u8 *bytes, int *errno)
883{
884 i40e_status status = 0;
885 u8 module, transaction;
886 bool last;
887
888 transaction = i40e_nvmupd_get_transaction(cmd->config);
889 module = i40e_nvmupd_get_module(cmd->config);
890 last = (transaction & I40E_NVM_LCB);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000891
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000892 status = i40e_aq_update_nvm(hw, module, cmd->offset,
893 (u16)cmd->data_size, bytes, last, NULL);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000894 if (status) {
895 i40e_debug(hw, I40E_DEBUG_NVM,
896 "i40e_nvmupd_nvm_write mod 0x%x off 0x%x len 0x%x\n",
897 module, cmd->offset, cmd->data_size);
898 i40e_debug(hw, I40E_DEBUG_NVM,
899 "i40e_nvmupd_nvm_write status %d aq %d\n",
900 status, hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000901 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000902 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000903
904 return status;
905}