1 /*******************************************************************************
3 Copyright (c) 2013 - 2015, Intel Corporation
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
9 1. Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
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.
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.
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.
32 ***************************************************************************/
34 #include "i40e_prototype.h"
37 * i40e_init_nvm_ops - Initialize NVM function pointers
38 * @hw: pointer to the HW structure
40 * Setup the function pointers and the NVM info structure. Should be called
41 * once per NVM initialization, e.g. inside the i40e_init_shared_code().
42 * Please notice that the NVM term is used here (& in all methods covered
43 * in this file) as an equivalent of the FLASH part mapped into the SR.
44 * We are accessing FLASH always through the Shadow RAM.
46 enum i40e_status_code i40e_init_nvm(struct i40e_hw *hw)
48 struct i40e_nvm_info *nvm = &hw->nvm;
49 enum i40e_status_code ret_code = I40E_SUCCESS;
53 DEBUGFUNC("i40e_init_nvm");
55 /* The SR size is stored regardless of the nvm programming mode
56 * as the blank mode may be used in the factory line.
58 gens = rd32(hw, I40E_GLNVM_GENS);
59 sr_size = ((gens & I40E_GLNVM_GENS_SR_SIZE_MASK) >>
60 I40E_GLNVM_GENS_SR_SIZE_SHIFT);
61 /* Switching to words (sr_size contains power of 2KB) */
62 nvm->sr_size = BIT(sr_size) * I40E_SR_WORDS_IN_1KB;
64 /* Check if we are in the normal or blank NVM programming mode */
65 fla = rd32(hw, I40E_GLNVM_FLA);
66 if (fla & I40E_GLNVM_FLA_LOCKED_MASK) { /* Normal programming mode */
68 nvm->timeout = I40E_MAX_NVM_TIMEOUT;
69 nvm->blank_nvm_mode = false;
70 } else { /* Blank programming mode */
71 nvm->blank_nvm_mode = true;
72 ret_code = I40E_ERR_NVM_BLANK_MODE;
73 i40e_debug(hw, I40E_DEBUG_NVM, "NVM init error: unsupported blank mode.\n");
80 * i40e_acquire_nvm - Generic request for acquiring the NVM ownership
81 * @hw: pointer to the HW structure
82 * @access: NVM access type (read or write)
84 * This function will request NVM ownership for reading
85 * via the proper Admin Command.
87 enum i40e_status_code i40e_acquire_nvm(struct i40e_hw *hw,
88 enum i40e_aq_resource_access_type access)
90 enum i40e_status_code ret_code = I40E_SUCCESS;
94 DEBUGFUNC("i40e_acquire_nvm");
96 if (hw->nvm.blank_nvm_mode)
97 goto i40e_i40e_acquire_nvm_exit;
99 ret_code = i40e_aq_request_resource(hw, I40E_NVM_RESOURCE_ID, access,
100 0, &time_left, NULL);
101 /* Reading the Global Device Timer */
102 gtime = rd32(hw, I40E_GLVFGEN_TIMER);
104 /* Store the timeout */
105 hw->nvm.hw_semaphore_timeout = I40E_MS_TO_GTIME(time_left) + gtime;
108 i40e_debug(hw, I40E_DEBUG_NVM,
109 "NVM acquire type %d failed time_left=%llu ret=%d aq_err=%d\n",
110 access, time_left, ret_code, hw->aq.asq_last_status);
112 if (ret_code && time_left) {
113 /* Poll until the current NVM owner timeouts */
114 timeout = I40E_MS_TO_GTIME(I40E_MAX_NVM_TIMEOUT) + gtime;
115 while ((gtime < timeout) && time_left) {
117 gtime = rd32(hw, I40E_GLVFGEN_TIMER);
118 ret_code = i40e_aq_request_resource(hw,
119 I40E_NVM_RESOURCE_ID,
120 access, 0, &time_left,
122 if (ret_code == I40E_SUCCESS) {
123 hw->nvm.hw_semaphore_timeout =
124 I40E_MS_TO_GTIME(time_left) + gtime;
128 if (ret_code != I40E_SUCCESS) {
129 hw->nvm.hw_semaphore_timeout = 0;
130 i40e_debug(hw, I40E_DEBUG_NVM,
131 "NVM acquire timed out, wait %llu ms before trying again. status=%d aq_err=%d\n",
132 time_left, ret_code, hw->aq.asq_last_status);
136 i40e_i40e_acquire_nvm_exit:
141 * i40e_release_nvm - Generic request for releasing the NVM ownership
142 * @hw: pointer to the HW structure
144 * This function will release NVM resource via the proper Admin Command.
146 void i40e_release_nvm(struct i40e_hw *hw)
148 enum i40e_status_code ret_code = I40E_SUCCESS;
151 DEBUGFUNC("i40e_release_nvm");
153 if (hw->nvm.blank_nvm_mode)
156 ret_code = i40e_aq_release_resource(hw, I40E_NVM_RESOURCE_ID, 0, NULL);
158 /* there are some rare cases when trying to release the resource
159 * results in an admin Q timeout, so handle them correctly
161 while ((ret_code == I40E_ERR_ADMIN_QUEUE_TIMEOUT) &&
162 (total_delay < hw->aq.asq_cmd_timeout)) {
164 ret_code = i40e_aq_release_resource(hw,
165 I40E_NVM_RESOURCE_ID, 0, NULL);
171 * i40e_poll_sr_srctl_done_bit - Polls the GLNVM_SRCTL done bit
172 * @hw: pointer to the HW structure
174 * Polls the SRCTL Shadow RAM register done bit.
176 static enum i40e_status_code i40e_poll_sr_srctl_done_bit(struct i40e_hw *hw)
178 enum i40e_status_code ret_code = I40E_ERR_TIMEOUT;
181 DEBUGFUNC("i40e_poll_sr_srctl_done_bit");
183 /* Poll the I40E_GLNVM_SRCTL until the done bit is set */
184 for (wait_cnt = 0; wait_cnt < I40E_SRRD_SRCTL_ATTEMPTS; wait_cnt++) {
185 srctl = rd32(hw, I40E_GLNVM_SRCTL);
186 if (srctl & I40E_GLNVM_SRCTL_DONE_MASK) {
187 ret_code = I40E_SUCCESS;
192 if (ret_code == I40E_ERR_TIMEOUT)
193 i40e_debug(hw, I40E_DEBUG_NVM, "Done bit in GLNVM_SRCTL not set");
198 * i40e_read_nvm_word_srctl - Reads Shadow RAM via SRCTL register
199 * @hw: pointer to the HW structure
200 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
201 * @data: word read from the Shadow RAM
203 * Reads one 16 bit word from the Shadow RAM using the GLNVM_SRCTL register.
205 STATIC enum i40e_status_code i40e_read_nvm_word_srctl(struct i40e_hw *hw,
209 enum i40e_status_code ret_code = I40E_ERR_TIMEOUT;
212 DEBUGFUNC("i40e_read_nvm_word_srctl");
214 if (offset >= hw->nvm.sr_size) {
215 i40e_debug(hw, I40E_DEBUG_NVM,
216 "NVM read error: Offset %d beyond Shadow RAM limit %d\n",
217 offset, hw->nvm.sr_size);
218 ret_code = I40E_ERR_PARAM;
222 /* Poll the done bit first */
223 ret_code = i40e_poll_sr_srctl_done_bit(hw);
224 if (ret_code == I40E_SUCCESS) {
225 /* Write the address and start reading */
226 sr_reg = ((u32)offset << I40E_GLNVM_SRCTL_ADDR_SHIFT) |
227 BIT(I40E_GLNVM_SRCTL_START_SHIFT);
228 wr32(hw, I40E_GLNVM_SRCTL, sr_reg);
230 /* Poll I40E_GLNVM_SRCTL until the done bit is set */
231 ret_code = i40e_poll_sr_srctl_done_bit(hw);
232 if (ret_code == I40E_SUCCESS) {
233 sr_reg = rd32(hw, I40E_GLNVM_SRDATA);
234 *data = (u16)((sr_reg &
235 I40E_GLNVM_SRDATA_RDDATA_MASK)
236 >> I40E_GLNVM_SRDATA_RDDATA_SHIFT);
239 if (ret_code != I40E_SUCCESS)
240 i40e_debug(hw, I40E_DEBUG_NVM,
241 "NVM read error: Couldn't access Shadow RAM address: 0x%x\n",
249 * i40e_read_nvm_aq - Read Shadow RAM.
250 * @hw: pointer to the HW structure.
251 * @module_pointer: module pointer location in words from the NVM beginning
252 * @offset: offset in words from module start
253 * @words: number of words to write
254 * @data: buffer with words to write to the Shadow RAM
255 * @last_command: tells the AdminQ that this is the last command
257 * Writes a 16 bit words buffer to the Shadow RAM using the admin command.
259 STATIC enum i40e_status_code i40e_read_nvm_aq(struct i40e_hw *hw,
260 u8 module_pointer, u32 offset,
261 u16 words, void *data,
264 enum i40e_status_code ret_code = I40E_ERR_NVM;
265 struct i40e_asq_cmd_details cmd_details;
267 DEBUGFUNC("i40e_read_nvm_aq");
269 memset(&cmd_details, 0, sizeof(cmd_details));
270 cmd_details.wb_desc = &hw->nvm_wb_desc;
272 /* Here we are checking the SR limit only for the flat memory model.
273 * We cannot do it for the module-based model, as we did not acquire
274 * the NVM resource yet (we cannot get the module pointer value).
275 * Firmware will check the module-based model.
277 if ((offset + words) > hw->nvm.sr_size)
278 i40e_debug(hw, I40E_DEBUG_NVM,
279 "NVM write error: offset %d beyond Shadow RAM limit %d\n",
280 (offset + words), hw->nvm.sr_size);
281 else if (words > I40E_SR_SECTOR_SIZE_IN_WORDS)
282 /* We can write only up to 4KB (one sector), in one AQ write */
283 i40e_debug(hw, I40E_DEBUG_NVM,
284 "NVM write fail error: tried to write %d words, limit is %d.\n",
285 words, I40E_SR_SECTOR_SIZE_IN_WORDS);
286 else if (((offset + (words - 1)) / I40E_SR_SECTOR_SIZE_IN_WORDS)
287 != (offset / I40E_SR_SECTOR_SIZE_IN_WORDS))
288 /* A single write cannot spread over two sectors */
289 i40e_debug(hw, I40E_DEBUG_NVM,
290 "NVM write error: cannot spread over two sectors in a single write offset=%d words=%d\n",
293 ret_code = i40e_aq_read_nvm(hw, module_pointer,
294 2 * offset, /*bytes*/
296 data, last_command, &cmd_details);
302 * i40e_read_nvm_word_aq - Reads Shadow RAM via AQ
303 * @hw: pointer to the HW structure
304 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
305 * @data: word read from the Shadow RAM
307 * Reads one 16 bit word from the Shadow RAM using the AdminQ
309 STATIC enum i40e_status_code i40e_read_nvm_word_aq(struct i40e_hw *hw, u16 offset,
312 enum i40e_status_code ret_code = I40E_ERR_TIMEOUT;
314 DEBUGFUNC("i40e_read_nvm_word_aq");
316 ret_code = i40e_read_nvm_aq(hw, 0x0, offset, 1, data, true);
317 *data = LE16_TO_CPU(*(__le16 *)data);
323 * __i40e_read_nvm_word - Reads NVM word, assumes caller does the locking
324 * @hw: pointer to the HW structure
325 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
326 * @data: word read from the Shadow RAM
328 * Reads one 16 bit word from the Shadow RAM.
330 * Do not use this function except in cases where the nvm lock is already
331 * taken via i40e_acquire_nvm().
333 enum i40e_status_code __i40e_read_nvm_word(struct i40e_hw *hw,
338 if (hw->flags & I40E_HW_FLAG_AQ_SRCTL_ACCESS_ENABLE)
339 return i40e_read_nvm_word_aq(hw, offset, data);
341 return i40e_read_nvm_word_srctl(hw, offset, data);
345 * i40e_read_nvm_word - Reads NVM word, acquires lock if necessary
346 * @hw: pointer to the HW structure
347 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
348 * @data: word read from the Shadow RAM
350 * Reads one 16 bit word from the Shadow RAM.
352 enum i40e_status_code i40e_read_nvm_word(struct i40e_hw *hw, u16 offset,
355 enum i40e_status_code ret_code = I40E_SUCCESS;
357 if (hw->flags & I40E_HW_FLAG_NVM_READ_REQUIRES_LOCK)
358 ret_code = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
362 ret_code = __i40e_read_nvm_word(hw, offset, data);
364 if (hw->flags & I40E_HW_FLAG_NVM_READ_REQUIRES_LOCK)
365 i40e_release_nvm(hw);
370 * i40e_read_nvm_buffer_srctl - Reads Shadow RAM buffer via SRCTL register
371 * @hw: pointer to the HW structure
372 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
373 * @words: (in) number of words to read; (out) number of words actually read
374 * @data: words read from the Shadow RAM
376 * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_srrd()
377 * method. The buffer read is preceded by the NVM ownership take
378 * and followed by the release.
380 STATIC enum i40e_status_code i40e_read_nvm_buffer_srctl(struct i40e_hw *hw, u16 offset,
381 u16 *words, u16 *data)
383 enum i40e_status_code ret_code = I40E_SUCCESS;
386 DEBUGFUNC("i40e_read_nvm_buffer_srctl");
388 /* Loop through the selected region */
389 for (word = 0; word < *words; word++) {
390 index = offset + word;
391 ret_code = i40e_read_nvm_word_srctl(hw, index, &data[word]);
392 if (ret_code != I40E_SUCCESS)
396 /* Update the number of words read from the Shadow RAM */
403 * i40e_read_nvm_buffer_aq - Reads Shadow RAM buffer via AQ
404 * @hw: pointer to the HW structure
405 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
406 * @words: (in) number of words to read; (out) number of words actually read
407 * @data: words read from the Shadow RAM
409 * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_aq()
410 * method. The buffer read is preceded by the NVM ownership take
411 * and followed by the release.
413 STATIC enum i40e_status_code i40e_read_nvm_buffer_aq(struct i40e_hw *hw, u16 offset,
414 u16 *words, u16 *data)
416 enum i40e_status_code ret_code;
417 u16 read_size = *words;
418 bool last_cmd = false;
422 DEBUGFUNC("i40e_read_nvm_buffer_aq");
425 /* Calculate number of bytes we should read in this step.
426 * FVL AQ do not allow to read more than one page at a time or
427 * to cross page boundaries.
429 if (offset % I40E_SR_SECTOR_SIZE_IN_WORDS)
430 read_size = min(*words,
431 (u16)(I40E_SR_SECTOR_SIZE_IN_WORDS -
432 (offset % I40E_SR_SECTOR_SIZE_IN_WORDS)));
434 read_size = min((*words - words_read),
435 I40E_SR_SECTOR_SIZE_IN_WORDS);
437 /* Check if this is last command, if so set proper flag */
438 if ((words_read + read_size) >= *words)
441 ret_code = i40e_read_nvm_aq(hw, 0x0, offset, read_size,
442 data + words_read, last_cmd);
443 if (ret_code != I40E_SUCCESS)
444 goto read_nvm_buffer_aq_exit;
446 /* Increment counter for words already read and move offset to
449 words_read += read_size;
451 } while (words_read < *words);
453 for (i = 0; i < *words; i++)
454 data[i] = LE16_TO_CPU(((__le16 *)data)[i]);
456 read_nvm_buffer_aq_exit:
462 * __i40e_read_nvm_buffer - Reads NVM buffer, caller must acquire lock
463 * @hw: pointer to the HW structure
464 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
465 * @words: (in) number of words to read; (out) number of words actually read
466 * @data: words read from the Shadow RAM
468 * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_srrd()
471 enum i40e_status_code __i40e_read_nvm_buffer(struct i40e_hw *hw,
473 u16 *words, u16 *data)
475 if (hw->flags & I40E_HW_FLAG_AQ_SRCTL_ACCESS_ENABLE)
476 return i40e_read_nvm_buffer_aq(hw, offset, words, data);
478 return i40e_read_nvm_buffer_srctl(hw, offset, words, data);
482 * i40e_read_nvm_buffer - Reads Shadow RAM buffer and acquire lock if necessary
483 * @hw: pointer to the HW structure
484 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
485 * @words: (in) number of words to read; (out) number of words actually read
486 * @data: words read from the Shadow RAM
488 * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_srrd()
489 * method. The buffer read is preceded by the NVM ownership take
490 * and followed by the release.
492 enum i40e_status_code i40e_read_nvm_buffer(struct i40e_hw *hw, u16 offset,
493 u16 *words, u16 *data)
495 enum i40e_status_code ret_code = I40E_SUCCESS;
497 if (hw->flags & I40E_HW_FLAG_AQ_SRCTL_ACCESS_ENABLE) {
498 ret_code = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
500 ret_code = i40e_read_nvm_buffer_aq(hw, offset, words,
502 i40e_release_nvm(hw);
505 ret_code = i40e_read_nvm_buffer_srctl(hw, offset, words, data);
512 * i40e_write_nvm_aq - Writes Shadow RAM.
513 * @hw: pointer to the HW structure.
514 * @module_pointer: module pointer location in words from the NVM beginning
515 * @offset: offset in words from module start
516 * @words: number of words to write
517 * @data: buffer with words to write to the Shadow RAM
518 * @last_command: tells the AdminQ that this is the last command
520 * Writes a 16 bit words buffer to the Shadow RAM using the admin command.
522 enum i40e_status_code i40e_write_nvm_aq(struct i40e_hw *hw, u8 module_pointer,
523 u32 offset, u16 words, void *data,
526 enum i40e_status_code ret_code = I40E_ERR_NVM;
527 struct i40e_asq_cmd_details cmd_details;
529 DEBUGFUNC("i40e_write_nvm_aq");
531 memset(&cmd_details, 0, sizeof(cmd_details));
532 cmd_details.wb_desc = &hw->nvm_wb_desc;
534 /* Here we are checking the SR limit only for the flat memory model.
535 * We cannot do it for the module-based model, as we did not acquire
536 * the NVM resource yet (we cannot get the module pointer value).
537 * Firmware will check the module-based model.
539 if ((offset + words) > hw->nvm.sr_size)
540 DEBUGOUT("NVM write error: offset beyond Shadow RAM limit.\n");
541 else if (words > I40E_SR_SECTOR_SIZE_IN_WORDS)
542 /* We can write only up to 4KB (one sector), in one AQ write */
543 DEBUGOUT("NVM write fail error: cannot write more than 4KB in a single write.\n");
544 else if (((offset + (words - 1)) / I40E_SR_SECTOR_SIZE_IN_WORDS)
545 != (offset / I40E_SR_SECTOR_SIZE_IN_WORDS))
546 /* A single write cannot spread over two sectors */
547 DEBUGOUT("NVM write error: cannot spread over two sectors in a single write.\n");
549 ret_code = i40e_aq_update_nvm(hw, module_pointer,
550 2 * offset, /*bytes*/
552 data, last_command, 0,
559 * __i40e_write_nvm_word - Writes Shadow RAM word
560 * @hw: pointer to the HW structure
561 * @offset: offset of the Shadow RAM word to write
562 * @data: word to write to the Shadow RAM
564 * Writes a 16 bit word to the SR using the i40e_write_nvm_aq() method.
565 * NVM ownership have to be acquired and released (on ARQ completion event
566 * reception) by caller. To commit SR to NVM update checksum function
569 enum i40e_status_code __i40e_write_nvm_word(struct i40e_hw *hw, u32 offset,
572 DEBUGFUNC("i40e_write_nvm_word");
574 *((__le16 *)data) = CPU_TO_LE16(*((u16 *)data));
576 /* Value 0x00 below means that we treat SR as a flat mem */
577 return i40e_write_nvm_aq(hw, 0x00, offset, 1, data, false);
581 * __i40e_write_nvm_buffer - Writes Shadow RAM buffer
582 * @hw: pointer to the HW structure
583 * @module_pointer: module pointer location in words from the NVM beginning
584 * @offset: offset of the Shadow RAM buffer to write
585 * @words: number of words to write
586 * @data: words to write to the Shadow RAM
588 * Writes a 16 bit words buffer to the Shadow RAM using the admin command.
589 * NVM ownership must be acquired before calling this function and released
590 * on ARQ completion event reception by caller. To commit SR to NVM update
591 * checksum function should be called.
593 enum i40e_status_code __i40e_write_nvm_buffer(struct i40e_hw *hw,
594 u8 module_pointer, u32 offset,
595 u16 words, void *data)
597 __le16 *le_word_ptr = (__le16 *)data;
598 u16 *word_ptr = (u16 *)data;
601 DEBUGFUNC("i40e_write_nvm_buffer");
603 for (i = 0; i < words; i++)
604 le_word_ptr[i] = CPU_TO_LE16(word_ptr[i]);
606 /* Here we will only write one buffer as the size of the modules
607 * mirrored in the Shadow RAM is always less than 4K.
609 return i40e_write_nvm_aq(hw, module_pointer, offset, words,
614 * i40e_calc_nvm_checksum - Calculates and returns the checksum
615 * @hw: pointer to hardware structure
616 * @checksum: pointer to the checksum
618 * This function calculates SW Checksum that covers the whole 64kB shadow RAM
619 * except the VPD and PCIe ALT Auto-load modules. The structure and size of VPD
620 * is customer specific and unknown. Therefore, this function skips all maximum
621 * possible size of VPD (1kB).
623 enum i40e_status_code i40e_calc_nvm_checksum(struct i40e_hw *hw, u16 *checksum)
625 enum i40e_status_code ret_code = I40E_SUCCESS;
626 struct i40e_virt_mem vmem;
627 u16 pcie_alt_module = 0;
628 u16 checksum_local = 0;
633 DEBUGFUNC("i40e_calc_nvm_checksum");
635 ret_code = i40e_allocate_virt_mem(hw, &vmem,
636 I40E_SR_SECTOR_SIZE_IN_WORDS * sizeof(u16));
638 goto i40e_calc_nvm_checksum_exit;
639 data = (u16 *)vmem.va;
641 /* read pointer to VPD area */
642 ret_code = __i40e_read_nvm_word(hw, I40E_SR_VPD_PTR, &vpd_module);
643 if (ret_code != I40E_SUCCESS) {
644 ret_code = I40E_ERR_NVM_CHECKSUM;
645 goto i40e_calc_nvm_checksum_exit;
648 /* read pointer to PCIe Alt Auto-load module */
649 ret_code = __i40e_read_nvm_word(hw, I40E_SR_PCIE_ALT_AUTO_LOAD_PTR,
651 if (ret_code != I40E_SUCCESS) {
652 ret_code = I40E_ERR_NVM_CHECKSUM;
653 goto i40e_calc_nvm_checksum_exit;
656 /* Calculate SW checksum that covers the whole 64kB shadow RAM
657 * except the VPD and PCIe ALT Auto-load modules
659 for (i = 0; i < hw->nvm.sr_size; i++) {
661 if ((i % I40E_SR_SECTOR_SIZE_IN_WORDS) == 0) {
662 u16 words = I40E_SR_SECTOR_SIZE_IN_WORDS;
664 ret_code = __i40e_read_nvm_buffer(hw, i, &words, data);
665 if (ret_code != I40E_SUCCESS) {
666 ret_code = I40E_ERR_NVM_CHECKSUM;
667 goto i40e_calc_nvm_checksum_exit;
671 /* Skip Checksum word */
672 if (i == I40E_SR_SW_CHECKSUM_WORD)
674 /* Skip VPD module (convert byte size to word count) */
675 if ((i >= (u32)vpd_module) &&
676 (i < ((u32)vpd_module +
677 (I40E_SR_VPD_MODULE_MAX_SIZE / 2)))) {
680 /* Skip PCIe ALT module (convert byte size to word count) */
681 if ((i >= (u32)pcie_alt_module) &&
682 (i < ((u32)pcie_alt_module +
683 (I40E_SR_PCIE_ALT_MODULE_MAX_SIZE / 2)))) {
687 checksum_local += data[i % I40E_SR_SECTOR_SIZE_IN_WORDS];
690 *checksum = (u16)I40E_SR_SW_CHECKSUM_BASE - checksum_local;
692 i40e_calc_nvm_checksum_exit:
693 i40e_free_virt_mem(hw, &vmem);
698 * i40e_update_nvm_checksum - Updates the NVM checksum
699 * @hw: pointer to hardware structure
701 * NVM ownership must be acquired before calling this function and released
702 * on ARQ completion event reception by caller.
703 * This function will commit SR to NVM.
705 enum i40e_status_code i40e_update_nvm_checksum(struct i40e_hw *hw)
707 enum i40e_status_code ret_code = I40E_SUCCESS;
711 DEBUGFUNC("i40e_update_nvm_checksum");
713 ret_code = i40e_calc_nvm_checksum(hw, &checksum);
714 le_sum = CPU_TO_LE16(checksum);
715 if (ret_code == I40E_SUCCESS)
716 ret_code = i40e_write_nvm_aq(hw, 0x00, I40E_SR_SW_CHECKSUM_WORD,
723 * i40e_validate_nvm_checksum - Validate EEPROM checksum
724 * @hw: pointer to hardware structure
725 * @checksum: calculated checksum
727 * Performs checksum calculation and validates the NVM SW checksum. If the
728 * caller does not need checksum, the value can be NULL.
730 enum i40e_status_code i40e_validate_nvm_checksum(struct i40e_hw *hw,
733 enum i40e_status_code ret_code = I40E_SUCCESS;
735 u16 checksum_local = 0;
737 DEBUGFUNC("i40e_validate_nvm_checksum");
739 /* We must acquire the NVM lock in order to correctly synchronize the
740 * NVM accesses across multiple PFs. Without doing so it is possible
741 * for one of the PFs to read invalid data potentially indicating that
742 * the checksum is invalid.
744 ret_code = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
747 ret_code = i40e_calc_nvm_checksum(hw, &checksum_local);
748 __i40e_read_nvm_word(hw, I40E_SR_SW_CHECKSUM_WORD, &checksum_sr);
749 i40e_release_nvm(hw);
753 /* Verify read checksum from EEPROM is the same as
754 * calculated checksum
756 if (checksum_local != checksum_sr)
757 ret_code = I40E_ERR_NVM_CHECKSUM;
759 /* If the user cares, return the calculated checksum */
761 *checksum = checksum_local;
766 STATIC enum i40e_status_code i40e_nvmupd_state_init(struct i40e_hw *hw,
767 struct i40e_nvm_access *cmd,
768 u8 *bytes, int *perrno);
769 STATIC enum i40e_status_code i40e_nvmupd_state_reading(struct i40e_hw *hw,
770 struct i40e_nvm_access *cmd,
771 u8 *bytes, int *perrno);
772 STATIC enum i40e_status_code i40e_nvmupd_state_writing(struct i40e_hw *hw,
773 struct i40e_nvm_access *cmd,
774 u8 *bytes, int *perrno);
775 STATIC enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
776 struct i40e_nvm_access *cmd,
778 STATIC enum i40e_status_code i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
779 struct i40e_nvm_access *cmd,
781 STATIC enum i40e_status_code i40e_nvmupd_nvm_write(struct i40e_hw *hw,
782 struct i40e_nvm_access *cmd,
783 u8 *bytes, int *perrno);
784 STATIC enum i40e_status_code i40e_nvmupd_nvm_read(struct i40e_hw *hw,
785 struct i40e_nvm_access *cmd,
786 u8 *bytes, int *perrno);
787 STATIC enum i40e_status_code i40e_nvmupd_exec_aq(struct i40e_hw *hw,
788 struct i40e_nvm_access *cmd,
789 u8 *bytes, int *perrno);
790 STATIC enum i40e_status_code i40e_nvmupd_get_aq_result(struct i40e_hw *hw,
791 struct i40e_nvm_access *cmd,
792 u8 *bytes, int *perrno);
793 STATIC INLINE u8 i40e_nvmupd_get_module(u32 val)
795 return (u8)(val & I40E_NVM_MOD_PNT_MASK);
797 STATIC INLINE u8 i40e_nvmupd_get_transaction(u32 val)
799 return (u8)((val & I40E_NVM_TRANS_MASK) >> I40E_NVM_TRANS_SHIFT);
802 STATIC INLINE u8 i40e_nvmupd_get_preservation_flags(u32 val)
804 return (u8)((val & I40E_NVM_PRESERVATION_FLAGS_MASK) >>
805 I40E_NVM_PRESERVATION_FLAGS_SHIFT);
808 STATIC const char *i40e_nvm_update_state_str[] = {
809 "I40E_NVMUPD_INVALID",
810 "I40E_NVMUPD_READ_CON",
811 "I40E_NVMUPD_READ_SNT",
812 "I40E_NVMUPD_READ_LCB",
813 "I40E_NVMUPD_READ_SA",
814 "I40E_NVMUPD_WRITE_ERA",
815 "I40E_NVMUPD_WRITE_CON",
816 "I40E_NVMUPD_WRITE_SNT",
817 "I40E_NVMUPD_WRITE_LCB",
818 "I40E_NVMUPD_WRITE_SA",
819 "I40E_NVMUPD_CSUM_CON",
820 "I40E_NVMUPD_CSUM_SA",
821 "I40E_NVMUPD_CSUM_LCB",
822 "I40E_NVMUPD_STATUS",
823 "I40E_NVMUPD_EXEC_AQ",
824 "I40E_NVMUPD_GET_AQ_RESULT",
828 * i40e_nvmupd_command - Process an NVM update command
829 * @hw: pointer to hardware structure
830 * @cmd: pointer to nvm update command
831 * @bytes: pointer to the data buffer
832 * @perrno: pointer to return error code
834 * Dispatches command depending on what update state is current
836 enum i40e_status_code i40e_nvmupd_command(struct i40e_hw *hw,
837 struct i40e_nvm_access *cmd,
838 u8 *bytes, int *perrno)
840 enum i40e_status_code status;
841 enum i40e_nvmupd_cmd upd_cmd;
843 DEBUGFUNC("i40e_nvmupd_command");
848 /* early check for status command and debug msgs */
849 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
851 i40e_debug(hw, I40E_DEBUG_NVM, "%s state %d nvm_release_on_hold %d opc 0x%04x cmd 0x%08x config 0x%08x offset 0x%08x data_size 0x%08x\n",
852 i40e_nvm_update_state_str[upd_cmd],
854 hw->nvm_release_on_done, hw->nvm_wait_opcode,
855 cmd->command, cmd->config, cmd->offset, cmd->data_size);
857 if (upd_cmd == I40E_NVMUPD_INVALID) {
859 i40e_debug(hw, I40E_DEBUG_NVM,
860 "i40e_nvmupd_validate_command returns %d errno %d\n",
864 /* a status request returns immediately rather than
865 * going into the state machine
867 if (upd_cmd == I40E_NVMUPD_STATUS) {
868 if (!cmd->data_size) {
870 return I40E_ERR_BUF_TOO_SHORT;
873 bytes[0] = hw->nvmupd_state;
875 if (cmd->data_size >= 4) {
877 *((u16 *)&bytes[2]) = hw->nvm_wait_opcode;
880 /* Clear error status on read */
881 if (hw->nvmupd_state == I40E_NVMUPD_STATE_ERROR)
882 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
887 /* Clear status even it is not read and log */
888 if (hw->nvmupd_state == I40E_NVMUPD_STATE_ERROR) {
889 i40e_debug(hw, I40E_DEBUG_NVM,
890 "Clearing I40E_NVMUPD_STATE_ERROR state without reading\n");
891 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
894 /* Acquire lock to prevent race condition where adminq_task
895 * can execute after i40e_nvmupd_nvm_read/write but before state
896 * variables (nvm_wait_opcode, nvm_release_on_done) are updated.
898 * During NVMUpdate, it is observed that lock could be held for
899 * ~5ms for most commands. However lock is held for ~60ms for
900 * NVMUPD_CSUM_LCB command.
902 i40e_acquire_spinlock(&hw->aq.arq_spinlock);
903 switch (hw->nvmupd_state) {
904 case I40E_NVMUPD_STATE_INIT:
905 status = i40e_nvmupd_state_init(hw, cmd, bytes, perrno);
908 case I40E_NVMUPD_STATE_READING:
909 status = i40e_nvmupd_state_reading(hw, cmd, bytes, perrno);
912 case I40E_NVMUPD_STATE_WRITING:
913 status = i40e_nvmupd_state_writing(hw, cmd, bytes, perrno);
916 case I40E_NVMUPD_STATE_INIT_WAIT:
917 case I40E_NVMUPD_STATE_WRITE_WAIT:
918 /* if we need to stop waiting for an event, clear
919 * the wait info and return before doing anything else
921 if (cmd->offset == 0xffff) {
922 i40e_nvmupd_check_wait_event(hw, hw->nvm_wait_opcode);
923 status = I40E_SUCCESS;
927 status = I40E_ERR_NOT_READY;
932 /* invalid state, should never happen */
933 i40e_debug(hw, I40E_DEBUG_NVM,
934 "NVMUPD: no such state %d\n", hw->nvmupd_state);
935 status = I40E_NOT_SUPPORTED;
940 i40e_release_spinlock(&hw->aq.arq_spinlock);
945 * i40e_nvmupd_state_init - Handle NVM update state Init
946 * @hw: pointer to hardware structure
947 * @cmd: pointer to nvm update command buffer
948 * @bytes: pointer to the data buffer
949 * @perrno: pointer to return error code
951 * Process legitimate commands of the Init state and conditionally set next
952 * state. Reject all other commands.
954 STATIC enum i40e_status_code i40e_nvmupd_state_init(struct i40e_hw *hw,
955 struct i40e_nvm_access *cmd,
956 u8 *bytes, int *perrno)
958 enum i40e_status_code status = I40E_SUCCESS;
959 enum i40e_nvmupd_cmd upd_cmd;
961 DEBUGFUNC("i40e_nvmupd_state_init");
963 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
966 case I40E_NVMUPD_READ_SA:
967 status = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
969 *perrno = i40e_aq_rc_to_posix(status,
970 hw->aq.asq_last_status);
972 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
973 i40e_release_nvm(hw);
977 case I40E_NVMUPD_READ_SNT:
978 status = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
980 *perrno = i40e_aq_rc_to_posix(status,
981 hw->aq.asq_last_status);
983 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
985 i40e_release_nvm(hw);
987 hw->nvmupd_state = I40E_NVMUPD_STATE_READING;
991 case I40E_NVMUPD_WRITE_ERA:
992 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
994 *perrno = i40e_aq_rc_to_posix(status,
995 hw->aq.asq_last_status);
997 status = i40e_nvmupd_nvm_erase(hw, cmd, perrno);
999 i40e_release_nvm(hw);
1001 hw->nvm_release_on_done = true;
1002 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_erase;
1003 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
1008 case I40E_NVMUPD_WRITE_SA:
1009 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
1011 *perrno = i40e_aq_rc_to_posix(status,
1012 hw->aq.asq_last_status);
1014 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
1016 i40e_release_nvm(hw);
1018 hw->nvm_release_on_done = true;
1019 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
1020 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
1025 case I40E_NVMUPD_WRITE_SNT:
1026 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
1028 *perrno = i40e_aq_rc_to_posix(status,
1029 hw->aq.asq_last_status);
1031 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
1033 i40e_release_nvm(hw);
1035 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
1036 hw->nvmupd_state = I40E_NVMUPD_STATE_WRITE_WAIT;
1041 case I40E_NVMUPD_CSUM_SA:
1042 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
1044 *perrno = i40e_aq_rc_to_posix(status,
1045 hw->aq.asq_last_status);
1047 status = i40e_update_nvm_checksum(hw);
1049 *perrno = hw->aq.asq_last_status ?
1050 i40e_aq_rc_to_posix(status,
1051 hw->aq.asq_last_status) :
1053 i40e_release_nvm(hw);
1055 hw->nvm_release_on_done = true;
1056 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
1057 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
1062 case I40E_NVMUPD_EXEC_AQ:
1063 status = i40e_nvmupd_exec_aq(hw, cmd, bytes, perrno);
1066 case I40E_NVMUPD_GET_AQ_RESULT:
1067 status = i40e_nvmupd_get_aq_result(hw, cmd, bytes, perrno);
1071 i40e_debug(hw, I40E_DEBUG_NVM,
1072 "NVMUPD: bad cmd %s in init state\n",
1073 i40e_nvm_update_state_str[upd_cmd]);
1074 status = I40E_ERR_NVM;
1082 * i40e_nvmupd_state_reading - Handle NVM update state Reading
1083 * @hw: pointer to hardware structure
1084 * @cmd: pointer to nvm update command buffer
1085 * @bytes: pointer to the data buffer
1086 * @perrno: pointer to return error code
1088 * NVM ownership is already held. Process legitimate commands and set any
1089 * change in state; reject all other commands.
1091 STATIC enum i40e_status_code i40e_nvmupd_state_reading(struct i40e_hw *hw,
1092 struct i40e_nvm_access *cmd,
1093 u8 *bytes, int *perrno)
1095 enum i40e_status_code status = I40E_SUCCESS;
1096 enum i40e_nvmupd_cmd upd_cmd;
1098 DEBUGFUNC("i40e_nvmupd_state_reading");
1100 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
1103 case I40E_NVMUPD_READ_SA:
1104 case I40E_NVMUPD_READ_CON:
1105 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
1108 case I40E_NVMUPD_READ_LCB:
1109 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
1110 i40e_release_nvm(hw);
1111 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
1115 i40e_debug(hw, I40E_DEBUG_NVM,
1116 "NVMUPD: bad cmd %s in reading state.\n",
1117 i40e_nvm_update_state_str[upd_cmd]);
1118 status = I40E_NOT_SUPPORTED;
1126 * i40e_nvmupd_state_writing - Handle NVM update state Writing
1127 * @hw: pointer to hardware structure
1128 * @cmd: pointer to nvm update command buffer
1129 * @bytes: pointer to the data buffer
1130 * @perrno: pointer to return error code
1132 * NVM ownership is already held. Process legitimate commands and set any
1133 * change in state; reject all other commands
1135 STATIC enum i40e_status_code i40e_nvmupd_state_writing(struct i40e_hw *hw,
1136 struct i40e_nvm_access *cmd,
1137 u8 *bytes, int *perrno)
1139 enum i40e_status_code status = I40E_SUCCESS;
1140 enum i40e_nvmupd_cmd upd_cmd;
1141 bool retry_attempt = false;
1143 DEBUGFUNC("i40e_nvmupd_state_writing");
1145 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
1149 case I40E_NVMUPD_WRITE_CON:
1150 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
1152 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
1153 hw->nvmupd_state = I40E_NVMUPD_STATE_WRITE_WAIT;
1157 case I40E_NVMUPD_WRITE_LCB:
1158 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
1160 *perrno = hw->aq.asq_last_status ?
1161 i40e_aq_rc_to_posix(status,
1162 hw->aq.asq_last_status) :
1164 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
1166 hw->nvm_release_on_done = true;
1167 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
1168 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
1172 case I40E_NVMUPD_CSUM_CON:
1173 /* Assumes the caller has acquired the nvm */
1174 status = i40e_update_nvm_checksum(hw);
1176 *perrno = hw->aq.asq_last_status ?
1177 i40e_aq_rc_to_posix(status,
1178 hw->aq.asq_last_status) :
1180 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
1182 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
1183 hw->nvmupd_state = I40E_NVMUPD_STATE_WRITE_WAIT;
1187 case I40E_NVMUPD_CSUM_LCB:
1188 /* Assumes the caller has acquired the nvm */
1189 status = i40e_update_nvm_checksum(hw);
1191 *perrno = hw->aq.asq_last_status ?
1192 i40e_aq_rc_to_posix(status,
1193 hw->aq.asq_last_status) :
1195 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
1197 hw->nvm_release_on_done = true;
1198 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
1199 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
1204 i40e_debug(hw, I40E_DEBUG_NVM,
1205 "NVMUPD: bad cmd %s in writing state.\n",
1206 i40e_nvm_update_state_str[upd_cmd]);
1207 status = I40E_NOT_SUPPORTED;
1212 /* In some circumstances, a multi-write transaction takes longer
1213 * than the default 3 minute timeout on the write semaphore. If
1214 * the write failed with an EBUSY status, this is likely the problem,
1215 * so here we try to reacquire the semaphore then retry the write.
1216 * We only do one retry, then give up.
1218 if (status && (hw->aq.asq_last_status == I40E_AQ_RC_EBUSY) &&
1220 enum i40e_status_code old_status = status;
1221 u32 old_asq_status = hw->aq.asq_last_status;
1224 gtime = rd32(hw, I40E_GLVFGEN_TIMER);
1225 if (gtime >= hw->nvm.hw_semaphore_timeout) {
1226 i40e_debug(hw, I40E_DEBUG_ALL,
1227 "NVMUPD: write semaphore expired (%d >= %lld), retrying\n",
1228 gtime, hw->nvm.hw_semaphore_timeout);
1229 i40e_release_nvm(hw);
1230 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
1232 i40e_debug(hw, I40E_DEBUG_ALL,
1233 "NVMUPD: write semaphore reacquire failed aq_err = %d\n",
1234 hw->aq.asq_last_status);
1235 status = old_status;
1236 hw->aq.asq_last_status = old_asq_status;
1238 retry_attempt = true;
1248 * i40e_nvmupd_check_wait_event - handle NVM update operation events
1249 * @hw: pointer to the hardware structure
1250 * @opcode: the event that just happened
1252 void i40e_nvmupd_check_wait_event(struct i40e_hw *hw, u16 opcode)
1254 if (opcode == hw->nvm_wait_opcode) {
1256 i40e_debug(hw, I40E_DEBUG_NVM,
1257 "NVMUPD: clearing wait on opcode 0x%04x\n", opcode);
1258 if (hw->nvm_release_on_done) {
1259 i40e_release_nvm(hw);
1260 hw->nvm_release_on_done = false;
1262 hw->nvm_wait_opcode = 0;
1264 if (hw->aq.arq_last_status) {
1265 hw->nvmupd_state = I40E_NVMUPD_STATE_ERROR;
1269 switch (hw->nvmupd_state) {
1270 case I40E_NVMUPD_STATE_INIT_WAIT:
1271 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
1274 case I40E_NVMUPD_STATE_WRITE_WAIT:
1275 hw->nvmupd_state = I40E_NVMUPD_STATE_WRITING;
1285 * i40e_nvmupd_validate_command - Validate given command
1286 * @hw: pointer to hardware structure
1287 * @cmd: pointer to nvm update command buffer
1288 * @perrno: pointer to return error code
1290 * Return one of the valid command types or I40E_NVMUPD_INVALID
1292 STATIC enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
1293 struct i40e_nvm_access *cmd,
1296 enum i40e_nvmupd_cmd upd_cmd;
1297 u8 module, transaction;
1299 DEBUGFUNC("i40e_nvmupd_validate_command\n");
1301 /* anything that doesn't match a recognized case is an error */
1302 upd_cmd = I40E_NVMUPD_INVALID;
1304 transaction = i40e_nvmupd_get_transaction(cmd->config);
1305 module = i40e_nvmupd_get_module(cmd->config);
1307 /* limits on data size */
1308 if ((cmd->data_size < 1) ||
1309 (cmd->data_size > I40E_NVMUPD_MAX_DATA)) {
1310 i40e_debug(hw, I40E_DEBUG_NVM,
1311 "i40e_nvmupd_validate_command data_size %d\n",
1314 return I40E_NVMUPD_INVALID;
1317 switch (cmd->command) {
1319 switch (transaction) {
1321 upd_cmd = I40E_NVMUPD_READ_CON;
1324 upd_cmd = I40E_NVMUPD_READ_SNT;
1327 upd_cmd = I40E_NVMUPD_READ_LCB;
1330 upd_cmd = I40E_NVMUPD_READ_SA;
1334 upd_cmd = I40E_NVMUPD_STATUS;
1335 else if (module == 0)
1336 upd_cmd = I40E_NVMUPD_GET_AQ_RESULT;
1341 case I40E_NVM_WRITE:
1342 switch (transaction) {
1344 upd_cmd = I40E_NVMUPD_WRITE_CON;
1347 upd_cmd = I40E_NVMUPD_WRITE_SNT;
1350 upd_cmd = I40E_NVMUPD_WRITE_LCB;
1353 upd_cmd = I40E_NVMUPD_WRITE_SA;
1356 upd_cmd = I40E_NVMUPD_WRITE_ERA;
1359 upd_cmd = I40E_NVMUPD_CSUM_CON;
1361 case (I40E_NVM_CSUM|I40E_NVM_SA):
1362 upd_cmd = I40E_NVMUPD_CSUM_SA;
1364 case (I40E_NVM_CSUM|I40E_NVM_LCB):
1365 upd_cmd = I40E_NVMUPD_CSUM_LCB;
1369 upd_cmd = I40E_NVMUPD_EXEC_AQ;
1379 * i40e_nvmupd_exec_aq - Run an AQ command
1380 * @hw: pointer to hardware structure
1381 * @cmd: pointer to nvm update command buffer
1382 * @bytes: pointer to the data buffer
1383 * @perrno: pointer to return error code
1385 * cmd structure contains identifiers and data buffer
1387 STATIC enum i40e_status_code i40e_nvmupd_exec_aq(struct i40e_hw *hw,
1388 struct i40e_nvm_access *cmd,
1389 u8 *bytes, int *perrno)
1391 struct i40e_asq_cmd_details cmd_details;
1392 enum i40e_status_code status;
1393 struct i40e_aq_desc *aq_desc;
1399 i40e_debug(hw, I40E_DEBUG_NVM, "NVMUPD: %s\n", __func__);
1400 memset(&cmd_details, 0, sizeof(cmd_details));
1401 cmd_details.wb_desc = &hw->nvm_wb_desc;
1403 aq_desc_len = sizeof(struct i40e_aq_desc);
1404 memset(&hw->nvm_wb_desc, 0, aq_desc_len);
1406 /* get the aq descriptor */
1407 if (cmd->data_size < aq_desc_len) {
1408 i40e_debug(hw, I40E_DEBUG_NVM,
1409 "NVMUPD: not enough aq desc bytes for exec, size %d < %d\n",
1410 cmd->data_size, aq_desc_len);
1412 return I40E_ERR_PARAM;
1414 aq_desc = (struct i40e_aq_desc *)bytes;
1416 /* if data buffer needed, make sure it's ready */
1417 aq_data_len = cmd->data_size - aq_desc_len;
1418 buff_size = max(aq_data_len, (u32)LE16_TO_CPU(aq_desc->datalen));
1420 if (!hw->nvm_buff.va) {
1421 status = i40e_allocate_virt_mem(hw, &hw->nvm_buff,
1422 hw->aq.asq_buf_size);
1424 i40e_debug(hw, I40E_DEBUG_NVM,
1425 "NVMUPD: i40e_allocate_virt_mem for exec buff failed, %d\n",
1429 if (hw->nvm_buff.va) {
1430 buff = hw->nvm_buff.va;
1431 i40e_memcpy(buff, &bytes[aq_desc_len], aq_data_len,
1432 I40E_NONDMA_TO_NONDMA);
1436 /* and away we go! */
1437 status = i40e_asq_send_command(hw, aq_desc, buff,
1438 buff_size, &cmd_details);
1440 i40e_debug(hw, I40E_DEBUG_NVM,
1441 "i40e_nvmupd_exec_aq err %s aq_err %s\n",
1442 i40e_stat_str(hw, status),
1443 i40e_aq_str(hw, hw->aq.asq_last_status));
1444 *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
1447 /* should we wait for a followup event? */
1449 hw->nvm_wait_opcode = cmd->offset;
1450 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
1457 * i40e_nvmupd_get_aq_result - Get the results from the previous exec_aq
1458 * @hw: pointer to hardware structure
1459 * @cmd: pointer to nvm update command buffer
1460 * @bytes: pointer to the data buffer
1461 * @perrno: pointer to return error code
1463 * cmd structure contains identifiers and data buffer
1465 STATIC enum i40e_status_code i40e_nvmupd_get_aq_result(struct i40e_hw *hw,
1466 struct i40e_nvm_access *cmd,
1467 u8 *bytes, int *perrno)
1474 i40e_debug(hw, I40E_DEBUG_NVM, "NVMUPD: %s\n", __func__);
1476 aq_desc_len = sizeof(struct i40e_aq_desc);
1477 aq_total_len = aq_desc_len + LE16_TO_CPU(hw->nvm_wb_desc.datalen);
1479 /* check offset range */
1480 if (cmd->offset > aq_total_len) {
1481 i40e_debug(hw, I40E_DEBUG_NVM, "%s: offset too big %d > %d\n",
1482 __func__, cmd->offset, aq_total_len);
1484 return I40E_ERR_PARAM;
1487 /* check copylength range */
1488 if (cmd->data_size > (aq_total_len - cmd->offset)) {
1489 int new_len = aq_total_len - cmd->offset;
1491 i40e_debug(hw, I40E_DEBUG_NVM, "%s: copy length %d too big, trimming to %d\n",
1492 __func__, cmd->data_size, new_len);
1493 cmd->data_size = new_len;
1496 remainder = cmd->data_size;
1497 if (cmd->offset < aq_desc_len) {
1498 u32 len = aq_desc_len - cmd->offset;
1500 len = min(len, cmd->data_size);
1501 i40e_debug(hw, I40E_DEBUG_NVM, "%s: aq_desc bytes %d to %d\n",
1502 __func__, cmd->offset, cmd->offset + len);
1504 buff = ((u8 *)&hw->nvm_wb_desc) + cmd->offset;
1505 i40e_memcpy(bytes, buff, len, I40E_NONDMA_TO_NONDMA);
1509 buff = hw->nvm_buff.va;
1511 buff = (u8 *)hw->nvm_buff.va + (cmd->offset - aq_desc_len);
1514 if (remainder > 0) {
1515 int start_byte = buff - (u8 *)hw->nvm_buff.va;
1517 i40e_debug(hw, I40E_DEBUG_NVM, "%s: databuf bytes %d to %d\n",
1518 __func__, start_byte, start_byte + remainder);
1519 i40e_memcpy(bytes, buff, remainder, I40E_NONDMA_TO_NONDMA);
1522 return I40E_SUCCESS;
1526 * i40e_nvmupd_nvm_read - Read NVM
1527 * @hw: pointer to hardware structure
1528 * @cmd: pointer to nvm update command buffer
1529 * @bytes: pointer to the data buffer
1530 * @perrno: pointer to return error code
1532 * cmd structure contains identifiers and data buffer
1534 STATIC enum i40e_status_code i40e_nvmupd_nvm_read(struct i40e_hw *hw,
1535 struct i40e_nvm_access *cmd,
1536 u8 *bytes, int *perrno)
1538 struct i40e_asq_cmd_details cmd_details;
1539 enum i40e_status_code status;
1540 u8 module, transaction;
1543 transaction = i40e_nvmupd_get_transaction(cmd->config);
1544 module = i40e_nvmupd_get_module(cmd->config);
1545 last = (transaction == I40E_NVM_LCB) || (transaction == I40E_NVM_SA);
1547 memset(&cmd_details, 0, sizeof(cmd_details));
1548 cmd_details.wb_desc = &hw->nvm_wb_desc;
1550 status = i40e_aq_read_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
1551 bytes, last, &cmd_details);
1553 i40e_debug(hw, I40E_DEBUG_NVM,
1554 "i40e_nvmupd_nvm_read mod 0x%x off 0x%x len 0x%x\n",
1555 module, cmd->offset, cmd->data_size);
1556 i40e_debug(hw, I40E_DEBUG_NVM,
1557 "i40e_nvmupd_nvm_read status %d aq %d\n",
1558 status, hw->aq.asq_last_status);
1559 *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
1566 * i40e_nvmupd_nvm_erase - Erase an NVM module
1567 * @hw: pointer to hardware structure
1568 * @cmd: pointer to nvm update command buffer
1569 * @perrno: pointer to return error code
1571 * module, offset, data_size and data are in cmd structure
1573 STATIC enum i40e_status_code i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
1574 struct i40e_nvm_access *cmd,
1577 enum i40e_status_code status = I40E_SUCCESS;
1578 struct i40e_asq_cmd_details cmd_details;
1579 u8 module, transaction;
1582 transaction = i40e_nvmupd_get_transaction(cmd->config);
1583 module = i40e_nvmupd_get_module(cmd->config);
1584 last = (transaction & I40E_NVM_LCB);
1586 memset(&cmd_details, 0, sizeof(cmd_details));
1587 cmd_details.wb_desc = &hw->nvm_wb_desc;
1589 status = i40e_aq_erase_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
1590 last, &cmd_details);
1592 i40e_debug(hw, I40E_DEBUG_NVM,
1593 "i40e_nvmupd_nvm_erase mod 0x%x off 0x%x len 0x%x\n",
1594 module, cmd->offset, cmd->data_size);
1595 i40e_debug(hw, I40E_DEBUG_NVM,
1596 "i40e_nvmupd_nvm_erase status %d aq %d\n",
1597 status, hw->aq.asq_last_status);
1598 *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
1605 * i40e_nvmupd_nvm_write - Write NVM
1606 * @hw: pointer to hardware structure
1607 * @cmd: pointer to nvm update command buffer
1608 * @bytes: pointer to the data buffer
1609 * @perrno: pointer to return error code
1611 * module, offset, data_size and data are in cmd structure
1613 STATIC enum i40e_status_code i40e_nvmupd_nvm_write(struct i40e_hw *hw,
1614 struct i40e_nvm_access *cmd,
1615 u8 *bytes, int *perrno)
1617 enum i40e_status_code status = I40E_SUCCESS;
1618 struct i40e_asq_cmd_details cmd_details;
1619 u8 module, transaction;
1620 u8 preservation_flags;
1623 transaction = i40e_nvmupd_get_transaction(cmd->config);
1624 module = i40e_nvmupd_get_module(cmd->config);
1625 last = (transaction & I40E_NVM_LCB);
1626 preservation_flags = i40e_nvmupd_get_preservation_flags(cmd->config);
1628 memset(&cmd_details, 0, sizeof(cmd_details));
1629 cmd_details.wb_desc = &hw->nvm_wb_desc;
1631 status = i40e_aq_update_nvm(hw, module, cmd->offset,
1632 (u16)cmd->data_size, bytes, last,
1633 preservation_flags, &cmd_details);
1635 i40e_debug(hw, I40E_DEBUG_NVM,
1636 "i40e_nvmupd_nvm_write mod 0x%x off 0x%x len 0x%x\n",
1637 module, cmd->offset, cmd->data_size);
1638 i40e_debug(hw, I40E_DEBUG_NVM,
1639 "i40e_nvmupd_nvm_write status %d aq %d\n",
1640 status, hw->aq.asq_last_status);
1641 *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);