xref: /freebsd/sys/dev/ixl/i40e_nvm.c (revision 71625ec9ad2a9bc8c09784fbd23b759830e0ee5f)
161ae650dSJack F Vogel /******************************************************************************
261ae650dSJack F Vogel 
3f4cc2d17SEric Joyner   Copyright (c) 2013-2018, Intel Corporation
461ae650dSJack F Vogel   All rights reserved.
561ae650dSJack F Vogel 
661ae650dSJack F Vogel   Redistribution and use in source and binary forms, with or without
761ae650dSJack F Vogel   modification, are permitted provided that the following conditions are met:
861ae650dSJack F Vogel 
961ae650dSJack F Vogel    1. Redistributions of source code must retain the above copyright notice,
1061ae650dSJack F Vogel       this list of conditions and the following disclaimer.
1161ae650dSJack F Vogel 
1261ae650dSJack F Vogel    2. Redistributions in binary form must reproduce the above copyright
1361ae650dSJack F Vogel       notice, this list of conditions and the following disclaimer in the
1461ae650dSJack F Vogel       documentation and/or other materials provided with the distribution.
1561ae650dSJack F Vogel 
1661ae650dSJack F Vogel    3. Neither the name of the Intel Corporation nor the names of its
1761ae650dSJack F Vogel       contributors may be used to endorse or promote products derived from
1861ae650dSJack F Vogel       this software without specific prior written permission.
1961ae650dSJack F Vogel 
2061ae650dSJack F Vogel   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2161ae650dSJack F Vogel   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2261ae650dSJack F Vogel   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2361ae650dSJack F Vogel   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2461ae650dSJack F Vogel   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2561ae650dSJack F Vogel   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2661ae650dSJack F Vogel   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2761ae650dSJack F Vogel   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2861ae650dSJack F Vogel   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2961ae650dSJack F Vogel   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3061ae650dSJack F Vogel   POSSIBILITY OF SUCH DAMAGE.
3161ae650dSJack F Vogel 
3261ae650dSJack F Vogel ******************************************************************************/
3361ae650dSJack F Vogel 
3461ae650dSJack F Vogel #include "i40e_prototype.h"
3561ae650dSJack F Vogel 
3661ae650dSJack F Vogel /**
37*abf77452SKrzysztof Galazka  * i40e_init_nvm - Initialize NVM function pointers
3861ae650dSJack F Vogel  * @hw: pointer to the HW structure
3961ae650dSJack F Vogel  *
4061ae650dSJack F Vogel  * Setup the function pointers and the NVM info structure. Should be called
4161ae650dSJack F Vogel  * once per NVM initialization, e.g. inside the i40e_init_shared_code().
4261ae650dSJack F Vogel  * Please notice that the NVM term is used here (& in all methods covered
4361ae650dSJack F Vogel  * in this file) as an equivalent of the FLASH part mapped into the SR.
441d767a8eSEric Joyner  * We are accessing FLASH always through the Shadow RAM.
4561ae650dSJack F Vogel  **/
i40e_init_nvm(struct i40e_hw * hw)4661ae650dSJack F Vogel enum i40e_status_code i40e_init_nvm(struct i40e_hw *hw)
4761ae650dSJack F Vogel {
4861ae650dSJack F Vogel 	struct i40e_nvm_info *nvm = &hw->nvm;
4961ae650dSJack F Vogel 	enum i40e_status_code ret_code = I40E_SUCCESS;
5061ae650dSJack F Vogel 	u32 fla, gens;
5161ae650dSJack F Vogel 	u8 sr_size;
5261ae650dSJack F Vogel 
5361ae650dSJack F Vogel 	DEBUGFUNC("i40e_init_nvm");
5461ae650dSJack F Vogel 
5561ae650dSJack F Vogel 	/* The SR size is stored regardless of the nvm programming mode
5661ae650dSJack F Vogel 	 * as the blank mode may be used in the factory line.
5761ae650dSJack F Vogel 	 */
5861ae650dSJack F Vogel 	gens = rd32(hw, I40E_GLNVM_GENS);
5961ae650dSJack F Vogel 	sr_size = ((gens & I40E_GLNVM_GENS_SR_SIZE_MASK) >>
6061ae650dSJack F Vogel 			   I40E_GLNVM_GENS_SR_SIZE_SHIFT);
6161ae650dSJack F Vogel 	/* Switching to words (sr_size contains power of 2KB) */
62be771cdaSJack F Vogel 	nvm->sr_size = BIT(sr_size) * I40E_SR_WORDS_IN_1KB;
6361ae650dSJack F Vogel 
6461ae650dSJack F Vogel 	/* Check if we are in the normal or blank NVM programming mode */
6561ae650dSJack F Vogel 	fla = rd32(hw, I40E_GLNVM_FLA);
6661ae650dSJack F Vogel 	if (fla & I40E_GLNVM_FLA_LOCKED_MASK) { /* Normal programming mode */
6761ae650dSJack F Vogel 		/* Max NVM timeout */
6861ae650dSJack F Vogel 		nvm->timeout = I40E_MAX_NVM_TIMEOUT;
6961ae650dSJack F Vogel 		nvm->blank_nvm_mode = FALSE;
7061ae650dSJack F Vogel 	} else { /* Blank programming mode */
7161ae650dSJack F Vogel 		nvm->blank_nvm_mode = TRUE;
7261ae650dSJack F Vogel 		ret_code = I40E_ERR_NVM_BLANK_MODE;
73f247dc25SJack F Vogel 		i40e_debug(hw, I40E_DEBUG_NVM, "NVM init error: unsupported blank mode.\n");
7461ae650dSJack F Vogel 	}
7561ae650dSJack F Vogel 
7661ae650dSJack F Vogel 	return ret_code;
7761ae650dSJack F Vogel }
7861ae650dSJack F Vogel 
7961ae650dSJack F Vogel /**
8061ae650dSJack F Vogel  * i40e_acquire_nvm - Generic request for acquiring the NVM ownership
8161ae650dSJack F Vogel  * @hw: pointer to the HW structure
8261ae650dSJack F Vogel  * @access: NVM access type (read or write)
8361ae650dSJack F Vogel  *
8461ae650dSJack F Vogel  * This function will request NVM ownership for reading
8561ae650dSJack F Vogel  * via the proper Admin Command.
8661ae650dSJack F Vogel  **/
i40e_acquire_nvm(struct i40e_hw * hw,enum i40e_aq_resource_access_type access)8761ae650dSJack F Vogel enum i40e_status_code i40e_acquire_nvm(struct i40e_hw *hw,
8861ae650dSJack F Vogel 				       enum i40e_aq_resource_access_type access)
8961ae650dSJack F Vogel {
9061ae650dSJack F Vogel 	enum i40e_status_code ret_code = I40E_SUCCESS;
9161ae650dSJack F Vogel 	u64 gtime, timeout;
92f247dc25SJack F Vogel 	u64 time_left = 0;
9361ae650dSJack F Vogel 
9461ae650dSJack F Vogel 	DEBUGFUNC("i40e_acquire_nvm");
9561ae650dSJack F Vogel 
9661ae650dSJack F Vogel 	if (hw->nvm.blank_nvm_mode)
9761ae650dSJack F Vogel 		goto i40e_i40e_acquire_nvm_exit;
9861ae650dSJack F Vogel 
9961ae650dSJack F Vogel 	ret_code = i40e_aq_request_resource(hw, I40E_NVM_RESOURCE_ID, access,
100f247dc25SJack F Vogel 					    0, &time_left, NULL);
10161ae650dSJack F Vogel 	/* Reading the Global Device Timer */
10261ae650dSJack F Vogel 	gtime = rd32(hw, I40E_GLVFGEN_TIMER);
10361ae650dSJack F Vogel 
10461ae650dSJack F Vogel 	/* Store the timeout */
105f247dc25SJack F Vogel 	hw->nvm.hw_semaphore_timeout = I40E_MS_TO_GTIME(time_left) + gtime;
10661ae650dSJack F Vogel 
107f247dc25SJack F Vogel 	if (ret_code)
108f247dc25SJack F Vogel 		i40e_debug(hw, I40E_DEBUG_NVM,
109f247dc25SJack F Vogel 			   "NVM acquire type %d failed time_left=%llu ret=%d aq_err=%d\n",
110*abf77452SKrzysztof Galazka 			   access, (unsigned long long)time_left, ret_code,
111*abf77452SKrzysztof Galazka 			   hw->aq.asq_last_status);
112f247dc25SJack F Vogel 
113f247dc25SJack F Vogel 	if (ret_code && time_left) {
11461ae650dSJack F Vogel 		/* Poll until the current NVM owner timeouts */
115f247dc25SJack F Vogel 		timeout = I40E_MS_TO_GTIME(I40E_MAX_NVM_TIMEOUT) + gtime;
116f247dc25SJack F Vogel 		while ((gtime < timeout) && time_left) {
11761ae650dSJack F Vogel 			i40e_msec_delay(10);
118f247dc25SJack F Vogel 			gtime = rd32(hw, I40E_GLVFGEN_TIMER);
11961ae650dSJack F Vogel 			ret_code = i40e_aq_request_resource(hw,
12061ae650dSJack F Vogel 							I40E_NVM_RESOURCE_ID,
121f247dc25SJack F Vogel 							access, 0, &time_left,
12261ae650dSJack F Vogel 							NULL);
12361ae650dSJack F Vogel 			if (ret_code == I40E_SUCCESS) {
12461ae650dSJack F Vogel 				hw->nvm.hw_semaphore_timeout =
125f247dc25SJack F Vogel 					    I40E_MS_TO_GTIME(time_left) + gtime;
12661ae650dSJack F Vogel 				break;
12761ae650dSJack F Vogel 			}
12861ae650dSJack F Vogel 		}
12961ae650dSJack F Vogel 		if (ret_code != I40E_SUCCESS) {
13061ae650dSJack F Vogel 			hw->nvm.hw_semaphore_timeout = 0;
131f247dc25SJack F Vogel 			i40e_debug(hw, I40E_DEBUG_NVM,
132f247dc25SJack F Vogel 				   "NVM acquire timed out, wait %llu ms before trying again. status=%d aq_err=%d\n",
133*abf77452SKrzysztof Galazka 				   (unsigned long long)time_left, ret_code,
134*abf77452SKrzysztof Galazka 				   hw->aq.asq_last_status);
13561ae650dSJack F Vogel 		}
13661ae650dSJack F Vogel 	}
13761ae650dSJack F Vogel 
13861ae650dSJack F Vogel i40e_i40e_acquire_nvm_exit:
13961ae650dSJack F Vogel 	return ret_code;
14061ae650dSJack F Vogel }
14161ae650dSJack F Vogel 
14261ae650dSJack F Vogel /**
14361ae650dSJack F Vogel  * i40e_release_nvm - Generic request for releasing the NVM ownership
14461ae650dSJack F Vogel  * @hw: pointer to the HW structure
14561ae650dSJack F Vogel  *
14661ae650dSJack F Vogel  * This function will release NVM resource via the proper Admin Command.
14761ae650dSJack F Vogel  **/
i40e_release_nvm(struct i40e_hw * hw)14861ae650dSJack F Vogel void i40e_release_nvm(struct i40e_hw *hw)
14961ae650dSJack F Vogel {
150be771cdaSJack F Vogel 	enum i40e_status_code ret_code = I40E_SUCCESS;
151be771cdaSJack F Vogel 	u32 total_delay = 0;
152be771cdaSJack F Vogel 
15361ae650dSJack F Vogel 	DEBUGFUNC("i40e_release_nvm");
15461ae650dSJack F Vogel 
155be771cdaSJack F Vogel 	if (hw->nvm.blank_nvm_mode)
156be771cdaSJack F Vogel 		return;
157be771cdaSJack F Vogel 
158be771cdaSJack F Vogel 	ret_code = i40e_aq_release_resource(hw, I40E_NVM_RESOURCE_ID, 0, NULL);
159be771cdaSJack F Vogel 
160be771cdaSJack F Vogel 	/* there are some rare cases when trying to release the resource
161be771cdaSJack F Vogel 	 * results in an admin Q timeout, so handle them correctly
162be771cdaSJack F Vogel 	 */
163be771cdaSJack F Vogel 	while ((ret_code == I40E_ERR_ADMIN_QUEUE_TIMEOUT) &&
164be771cdaSJack F Vogel 	       (total_delay < hw->aq.asq_cmd_timeout)) {
165be771cdaSJack F Vogel 			i40e_msec_delay(1);
166be771cdaSJack F Vogel 			ret_code = i40e_aq_release_resource(hw,
167be771cdaSJack F Vogel 						I40E_NVM_RESOURCE_ID, 0, NULL);
168be771cdaSJack F Vogel 			total_delay++;
169be771cdaSJack F Vogel 	}
17061ae650dSJack F Vogel }
17161ae650dSJack F Vogel 
17261ae650dSJack F Vogel /**
17361ae650dSJack F Vogel  * i40e_poll_sr_srctl_done_bit - Polls the GLNVM_SRCTL done bit
17461ae650dSJack F Vogel  * @hw: pointer to the HW structure
17561ae650dSJack F Vogel  *
17661ae650dSJack F Vogel  * Polls the SRCTL Shadow RAM register done bit.
17761ae650dSJack F Vogel  **/
i40e_poll_sr_srctl_done_bit(struct i40e_hw * hw)17861ae650dSJack F Vogel static enum i40e_status_code i40e_poll_sr_srctl_done_bit(struct i40e_hw *hw)
17961ae650dSJack F Vogel {
18061ae650dSJack F Vogel 	enum i40e_status_code ret_code = I40E_ERR_TIMEOUT;
18161ae650dSJack F Vogel 	u32 srctl, wait_cnt;
18261ae650dSJack F Vogel 
18361ae650dSJack F Vogel 	DEBUGFUNC("i40e_poll_sr_srctl_done_bit");
18461ae650dSJack F Vogel 
18561ae650dSJack F Vogel 	/* Poll the I40E_GLNVM_SRCTL until the done bit is set */
18661ae650dSJack F Vogel 	for (wait_cnt = 0; wait_cnt < I40E_SRRD_SRCTL_ATTEMPTS; wait_cnt++) {
18761ae650dSJack F Vogel 		srctl = rd32(hw, I40E_GLNVM_SRCTL);
18861ae650dSJack F Vogel 		if (srctl & I40E_GLNVM_SRCTL_DONE_MASK) {
18961ae650dSJack F Vogel 			ret_code = I40E_SUCCESS;
19061ae650dSJack F Vogel 			break;
19161ae650dSJack F Vogel 		}
19261ae650dSJack F Vogel 		i40e_usec_delay(5);
19361ae650dSJack F Vogel 	}
19461ae650dSJack F Vogel 	if (ret_code == I40E_ERR_TIMEOUT)
195f247dc25SJack F Vogel 		i40e_debug(hw, I40E_DEBUG_NVM, "Done bit in GLNVM_SRCTL not set");
19661ae650dSJack F Vogel 	return ret_code;
19761ae650dSJack F Vogel }
19861ae650dSJack F Vogel 
19961ae650dSJack F Vogel /**
200f247dc25SJack F Vogel  * i40e_read_nvm_word_srctl - Reads Shadow RAM via SRCTL register
201f247dc25SJack F Vogel  * @hw: pointer to the HW structure
202f247dc25SJack F Vogel  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
203f247dc25SJack F Vogel  * @data: word read from the Shadow RAM
204f247dc25SJack F Vogel  *
205f247dc25SJack F Vogel  * Reads one 16 bit word from the Shadow RAM using the GLNVM_SRCTL register.
206f247dc25SJack F Vogel  **/
i40e_read_nvm_word_srctl(struct i40e_hw * hw,u16 offset,u16 * data)207f247dc25SJack F Vogel enum i40e_status_code i40e_read_nvm_word_srctl(struct i40e_hw *hw, u16 offset,
208f247dc25SJack F Vogel 					       u16 *data)
209f247dc25SJack F Vogel {
21061ae650dSJack F Vogel 	enum i40e_status_code ret_code = I40E_ERR_TIMEOUT;
21161ae650dSJack F Vogel 	u32 sr_reg;
21261ae650dSJack F Vogel 
213f247dc25SJack F Vogel 	DEBUGFUNC("i40e_read_nvm_word_srctl");
21461ae650dSJack F Vogel 
21561ae650dSJack F Vogel 	if (offset >= hw->nvm.sr_size) {
216f247dc25SJack F Vogel 		i40e_debug(hw, I40E_DEBUG_NVM,
217f247dc25SJack F Vogel 			   "NVM read error: Offset %d beyond Shadow RAM limit %d\n",
218f247dc25SJack F Vogel 			   offset, hw->nvm.sr_size);
21961ae650dSJack F Vogel 		ret_code = I40E_ERR_PARAM;
22061ae650dSJack F Vogel 		goto read_nvm_exit;
22161ae650dSJack F Vogel 	}
22261ae650dSJack F Vogel 
22361ae650dSJack F Vogel 	/* Poll the done bit first */
22461ae650dSJack F Vogel 	ret_code = i40e_poll_sr_srctl_done_bit(hw);
22561ae650dSJack F Vogel 	if (ret_code == I40E_SUCCESS) {
22661ae650dSJack F Vogel 		/* Write the address and start reading */
227be771cdaSJack F Vogel 		sr_reg = ((u32)offset << I40E_GLNVM_SRCTL_ADDR_SHIFT) |
228be771cdaSJack F Vogel 			 BIT(I40E_GLNVM_SRCTL_START_SHIFT);
22961ae650dSJack F Vogel 		wr32(hw, I40E_GLNVM_SRCTL, sr_reg);
23061ae650dSJack F Vogel 
23161ae650dSJack F Vogel 		/* Poll I40E_GLNVM_SRCTL until the done bit is set */
23261ae650dSJack F Vogel 		ret_code = i40e_poll_sr_srctl_done_bit(hw);
23361ae650dSJack F Vogel 		if (ret_code == I40E_SUCCESS) {
23461ae650dSJack F Vogel 			sr_reg = rd32(hw, I40E_GLNVM_SRDATA);
23561ae650dSJack F Vogel 			*data = (u16)((sr_reg &
23661ae650dSJack F Vogel 				       I40E_GLNVM_SRDATA_RDDATA_MASK)
23761ae650dSJack F Vogel 				    >> I40E_GLNVM_SRDATA_RDDATA_SHIFT);
23861ae650dSJack F Vogel 		}
23961ae650dSJack F Vogel 	}
24061ae650dSJack F Vogel 	if (ret_code != I40E_SUCCESS)
241f247dc25SJack F Vogel 		i40e_debug(hw, I40E_DEBUG_NVM,
242f247dc25SJack F Vogel 			   "NVM read error: Couldn't access Shadow RAM address: 0x%x\n",
24361ae650dSJack F Vogel 			   offset);
24461ae650dSJack F Vogel 
24561ae650dSJack F Vogel read_nvm_exit:
24661ae650dSJack F Vogel 	return ret_code;
24761ae650dSJack F Vogel }
24861ae650dSJack F Vogel 
24961ae650dSJack F Vogel /**
250ceebc2f3SEric Joyner  * i40e_read_nvm_aq - Read Shadow RAM.
251ceebc2f3SEric Joyner  * @hw: pointer to the HW structure.
252ceebc2f3SEric Joyner  * @module_pointer: module pointer location in words from the NVM beginning
253ceebc2f3SEric Joyner  * @offset: offset in words from module start
254ceebc2f3SEric Joyner  * @words: number of words to write
255ceebc2f3SEric Joyner  * @data: buffer with words to write to the Shadow RAM
256ceebc2f3SEric Joyner  * @last_command: tells the AdminQ that this is the last command
257ceebc2f3SEric Joyner  *
258ceebc2f3SEric Joyner  * Writes a 16 bit words buffer to the Shadow RAM using the admin command.
259ceebc2f3SEric Joyner  **/
i40e_read_nvm_aq(struct i40e_hw * hw,u8 module_pointer,u32 offset,u16 words,void * data,bool last_command)260ceebc2f3SEric Joyner static enum i40e_status_code i40e_read_nvm_aq(struct i40e_hw *hw,
261ceebc2f3SEric Joyner 					      u8 module_pointer, u32 offset,
262ceebc2f3SEric Joyner 					      u16 words, void *data,
263ceebc2f3SEric Joyner 					      bool last_command)
264ceebc2f3SEric Joyner {
265ceebc2f3SEric Joyner 	enum i40e_status_code ret_code = I40E_ERR_NVM;
266ceebc2f3SEric Joyner 	struct i40e_asq_cmd_details cmd_details;
267ceebc2f3SEric Joyner 
268ceebc2f3SEric Joyner 	DEBUGFUNC("i40e_read_nvm_aq");
269ceebc2f3SEric Joyner 
270ceebc2f3SEric Joyner 	memset(&cmd_details, 0, sizeof(cmd_details));
271ceebc2f3SEric Joyner 	cmd_details.wb_desc = &hw->nvm_wb_desc;
272ceebc2f3SEric Joyner 
273ceebc2f3SEric Joyner 	/* Here we are checking the SR limit only for the flat memory model.
274ceebc2f3SEric Joyner 	 * We cannot do it for the module-based model, as we did not acquire
275ceebc2f3SEric Joyner 	 * the NVM resource yet (we cannot get the module pointer value).
276ceebc2f3SEric Joyner 	 * Firmware will check the module-based model.
277ceebc2f3SEric Joyner 	 */
278ceebc2f3SEric Joyner 	if ((offset + words) > hw->nvm.sr_size)
279ceebc2f3SEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM,
280ceebc2f3SEric Joyner 			   "NVM write error: offset %d beyond Shadow RAM limit %d\n",
281ceebc2f3SEric Joyner 			   (offset + words), hw->nvm.sr_size);
282ceebc2f3SEric Joyner 	else if (words > I40E_SR_SECTOR_SIZE_IN_WORDS)
283ceebc2f3SEric Joyner 		/* We can write only up to 4KB (one sector), in one AQ write */
284ceebc2f3SEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM,
285ceebc2f3SEric Joyner 			   "NVM write fail error: tried to write %d words, limit is %d.\n",
286ceebc2f3SEric Joyner 			   words, I40E_SR_SECTOR_SIZE_IN_WORDS);
287ceebc2f3SEric Joyner 	else if (((offset + (words - 1)) / I40E_SR_SECTOR_SIZE_IN_WORDS)
288ceebc2f3SEric Joyner 		 != (offset / I40E_SR_SECTOR_SIZE_IN_WORDS))
289ceebc2f3SEric Joyner 		/* A single write cannot spread over two sectors */
290ceebc2f3SEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM,
291ceebc2f3SEric Joyner 			   "NVM write error: cannot spread over two sectors in a single write offset=%d words=%d\n",
292ceebc2f3SEric Joyner 			   offset, words);
293ceebc2f3SEric Joyner 	else
294ceebc2f3SEric Joyner 		ret_code = i40e_aq_read_nvm(hw, module_pointer,
295ceebc2f3SEric Joyner 					    2 * offset,  /*bytes*/
296ceebc2f3SEric Joyner 					    2 * words,   /*bytes*/
297ceebc2f3SEric Joyner 					    data, last_command, &cmd_details);
298ceebc2f3SEric Joyner 
299ceebc2f3SEric Joyner 	return ret_code;
300ceebc2f3SEric Joyner }
301ceebc2f3SEric Joyner 
302ceebc2f3SEric Joyner /**
303f247dc25SJack F Vogel  * i40e_read_nvm_word_aq - Reads Shadow RAM via AQ
304f247dc25SJack F Vogel  * @hw: pointer to the HW structure
305f247dc25SJack F Vogel  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
306f247dc25SJack F Vogel  * @data: word read from the Shadow RAM
307f247dc25SJack F Vogel  *
308ceebc2f3SEric Joyner  * Reads one 16 bit word from the Shadow RAM using the AdminQ
309f247dc25SJack F Vogel  **/
i40e_read_nvm_word_aq(struct i40e_hw * hw,u16 offset,u16 * data)310ceebc2f3SEric Joyner static enum i40e_status_code i40e_read_nvm_word_aq(struct i40e_hw *hw, u16 offset,
311f247dc25SJack F Vogel 						   u16 *data)
312f247dc25SJack F Vogel {
313f247dc25SJack F Vogel 	enum i40e_status_code ret_code = I40E_ERR_TIMEOUT;
314f247dc25SJack F Vogel 
315f247dc25SJack F Vogel 	DEBUGFUNC("i40e_read_nvm_word_aq");
316f247dc25SJack F Vogel 
317f247dc25SJack F Vogel 	ret_code = i40e_read_nvm_aq(hw, 0x0, offset, 1, data, TRUE);
318f247dc25SJack F Vogel 	*data = LE16_TO_CPU(*(__le16 *)data);
319f247dc25SJack F Vogel 
320f247dc25SJack F Vogel 	return ret_code;
321f247dc25SJack F Vogel }
322f247dc25SJack F Vogel 
323f247dc25SJack F Vogel /**
324ceebc2f3SEric Joyner  * __i40e_read_nvm_word - Reads NVM word, assumes caller does the locking
325fdb6f38aSEric Joyner  * @hw: pointer to the HW structure
326ceebc2f3SEric Joyner  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
327ceebc2f3SEric Joyner  * @data: word read from the Shadow RAM
328fdb6f38aSEric Joyner  *
329ceebc2f3SEric Joyner  * Reads one 16 bit word from the Shadow RAM.
330ceebc2f3SEric Joyner  *
331ceebc2f3SEric Joyner  * Do not use this function except in cases where the nvm lock is already
332ceebc2f3SEric Joyner  * taken via i40e_acquire_nvm().
333fdb6f38aSEric Joyner  **/
__i40e_read_nvm_word(struct i40e_hw * hw,u16 offset,u16 * data)334ceebc2f3SEric Joyner enum i40e_status_code __i40e_read_nvm_word(struct i40e_hw *hw,
335fdb6f38aSEric Joyner 					   u16 offset,
336ceebc2f3SEric Joyner 					   u16 *data)
337fdb6f38aSEric Joyner {
338fdb6f38aSEric Joyner 
3394294f337SSean Bruno 	if (hw->flags & I40E_HW_FLAG_AQ_SRCTL_ACCESS_ENABLE)
340ceebc2f3SEric Joyner 		return i40e_read_nvm_word_aq(hw, offset, data);
341ceebc2f3SEric Joyner 
342ceebc2f3SEric Joyner 	return i40e_read_nvm_word_srctl(hw, offset, data);
343fdb6f38aSEric Joyner }
344fdb6f38aSEric Joyner 
345fdb6f38aSEric Joyner /**
346ceebc2f3SEric Joyner  * i40e_read_nvm_word - Reads NVM word, acquires lock if necessary
34761ae650dSJack F Vogel  * @hw: pointer to the HW structure
348ceebc2f3SEric Joyner  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
349ceebc2f3SEric Joyner  * @data: word read from the Shadow RAM
35061ae650dSJack F Vogel  *
351ceebc2f3SEric Joyner  * Reads one 16 bit word from the Shadow RAM.
35261ae650dSJack F Vogel  **/
i40e_read_nvm_word(struct i40e_hw * hw,u16 offset,u16 * data)353ceebc2f3SEric Joyner enum i40e_status_code i40e_read_nvm_word(struct i40e_hw *hw, u16 offset,
354ceebc2f3SEric Joyner 					 u16 *data)
35561ae650dSJack F Vogel {
356223d846dSEric Joyner 	enum i40e_status_code ret_code = I40E_SUCCESS;
357223d846dSEric Joyner 
358ceebc2f3SEric Joyner 	if (hw->flags & I40E_HW_FLAG_NVM_READ_REQUIRES_LOCK)
3594294f337SSean Bruno 		ret_code = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
360ceebc2f3SEric Joyner 
361ceebc2f3SEric Joyner 	if (ret_code)
362ceebc2f3SEric Joyner 		return ret_code;
363ceebc2f3SEric Joyner 	ret_code = __i40e_read_nvm_word(hw, offset, data);
364ceebc2f3SEric Joyner 
365ceebc2f3SEric Joyner 	if (hw->flags & I40E_HW_FLAG_NVM_READ_REQUIRES_LOCK)
3664294f337SSean Bruno 		i40e_release_nvm(hw);
367223d846dSEric Joyner 	return ret_code;
368f247dc25SJack F Vogel }
369f247dc25SJack F Vogel 
370f247dc25SJack F Vogel /**
371b4a7ce06SEric Joyner  * i40e_read_nvm_module_data - Reads NVM Buffer to specified memory location
372b4a7ce06SEric Joyner  * @hw: Pointer to the HW structure
373b4a7ce06SEric Joyner  * @module_ptr: Pointer to module in words with respect to NVM beginning
374b4a7ce06SEric Joyner  * @module_offset: Offset in words from module start
375b4a7ce06SEric Joyner  * @data_offset: Offset in words from reading data area start
376b4a7ce06SEric Joyner  * @words_data_size: Words to read from NVM
377b4a7ce06SEric Joyner  * @data_ptr: Pointer to memory location where resulting buffer will be stored
378b4a7ce06SEric Joyner  **/
379b4a7ce06SEric Joyner enum i40e_status_code
i40e_read_nvm_module_data(struct i40e_hw * hw,u8 module_ptr,u16 module_offset,u16 data_offset,u16 words_data_size,u16 * data_ptr)380b4a7ce06SEric Joyner i40e_read_nvm_module_data(struct i40e_hw *hw, u8 module_ptr, u16 module_offset,
381b4a7ce06SEric Joyner 			  u16 data_offset, u16 words_data_size, u16 *data_ptr)
382b4a7ce06SEric Joyner {
383b4a7ce06SEric Joyner 	enum i40e_status_code status;
384b4a7ce06SEric Joyner 	u16 specific_ptr = 0;
385b4a7ce06SEric Joyner 	u16 ptr_value = 0;
386b4a7ce06SEric Joyner 	u16 offset = 0;
387b4a7ce06SEric Joyner 
388b4a7ce06SEric Joyner 	if (module_ptr != 0) {
389b4a7ce06SEric Joyner 		status = i40e_read_nvm_word(hw, module_ptr, &ptr_value);
390b4a7ce06SEric Joyner 		if (status != I40E_SUCCESS) {
391b4a7ce06SEric Joyner 			i40e_debug(hw, I40E_DEBUG_ALL,
392b4a7ce06SEric Joyner 				   "Reading nvm word failed.Error code: %d.\n",
393b4a7ce06SEric Joyner 				   status);
394b4a7ce06SEric Joyner 			return I40E_ERR_NVM;
395b4a7ce06SEric Joyner 		}
396b4a7ce06SEric Joyner 	}
397b4a7ce06SEric Joyner #define I40E_NVM_INVALID_PTR_VAL 0x7FFF
398b4a7ce06SEric Joyner #define I40E_NVM_INVALID_VAL 0xFFFF
399b4a7ce06SEric Joyner 
400b4a7ce06SEric Joyner 	/* Pointer not initialized */
401b4a7ce06SEric Joyner 	if (ptr_value == I40E_NVM_INVALID_PTR_VAL ||
402b4a7ce06SEric Joyner 	    ptr_value == I40E_NVM_INVALID_VAL) {
403b4a7ce06SEric Joyner 		i40e_debug(hw, I40E_DEBUG_ALL, "Pointer not initialized.\n");
404b4a7ce06SEric Joyner 		return I40E_ERR_BAD_PTR;
405b4a7ce06SEric Joyner 	}
406b4a7ce06SEric Joyner 
407b4a7ce06SEric Joyner 	/* Check whether the module is in SR mapped area or outside */
408b4a7ce06SEric Joyner 	if (ptr_value & I40E_PTR_TYPE) {
409b4a7ce06SEric Joyner 		/* Pointer points outside of the Shared RAM mapped area */
410b4a7ce06SEric Joyner 		i40e_debug(hw, I40E_DEBUG_ALL,
411b4a7ce06SEric Joyner 			   "Reading nvm data failed. Pointer points outside of the Shared RAM mapped area.\n");
412b4a7ce06SEric Joyner 
413b4a7ce06SEric Joyner 		return I40E_ERR_PARAM;
414b4a7ce06SEric Joyner 	} else {
415b4a7ce06SEric Joyner 		/* Read from the Shadow RAM */
416b4a7ce06SEric Joyner 
417b4a7ce06SEric Joyner 		status = i40e_read_nvm_word(hw, ptr_value + module_offset,
418b4a7ce06SEric Joyner 					    &specific_ptr);
419b4a7ce06SEric Joyner 		if (status != I40E_SUCCESS) {
420b4a7ce06SEric Joyner 			i40e_debug(hw, I40E_DEBUG_ALL,
421b4a7ce06SEric Joyner 				   "Reading nvm word failed.Error code: %d.\n",
422b4a7ce06SEric Joyner 				   status);
423b4a7ce06SEric Joyner 			return I40E_ERR_NVM;
424b4a7ce06SEric Joyner 		}
425b4a7ce06SEric Joyner 
426b4a7ce06SEric Joyner 		offset = ptr_value + module_offset + specific_ptr +
427b4a7ce06SEric Joyner 			data_offset;
428b4a7ce06SEric Joyner 
429b4a7ce06SEric Joyner 		status = i40e_read_nvm_buffer(hw, offset, &words_data_size,
430b4a7ce06SEric Joyner 					      data_ptr);
431b4a7ce06SEric Joyner 		if (status != I40E_SUCCESS) {
432b4a7ce06SEric Joyner 			i40e_debug(hw, I40E_DEBUG_ALL,
433b4a7ce06SEric Joyner 				   "Reading nvm buffer failed.Error code: %d.\n",
434b4a7ce06SEric Joyner 				   status);
435b4a7ce06SEric Joyner 		}
436b4a7ce06SEric Joyner 	}
437b4a7ce06SEric Joyner 
438b4a7ce06SEric Joyner 	return status;
439b4a7ce06SEric Joyner }
440b4a7ce06SEric Joyner 
441b4a7ce06SEric Joyner /**
442f247dc25SJack F Vogel  * i40e_read_nvm_buffer_srctl - Reads Shadow RAM buffer via SRCTL register
443f247dc25SJack F Vogel  * @hw: pointer to the HW structure
444f247dc25SJack F Vogel  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
445f247dc25SJack F Vogel  * @words: (in) number of words to read; (out) number of words actually read
446f247dc25SJack F Vogel  * @data: words read from the Shadow RAM
447f247dc25SJack F Vogel  *
448f247dc25SJack F Vogel  * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_srrd()
449f247dc25SJack F Vogel  * method. The buffer read is preceded by the NVM ownership take
450f247dc25SJack F Vogel  * and followed by the release.
451f247dc25SJack F Vogel  **/
i40e_read_nvm_buffer_srctl(struct i40e_hw * hw,u16 offset,u16 * words,u16 * data)452ceebc2f3SEric Joyner static enum i40e_status_code i40e_read_nvm_buffer_srctl(struct i40e_hw *hw, u16 offset,
453f247dc25SJack F Vogel 							u16 *words, u16 *data)
454f247dc25SJack F Vogel {
45561ae650dSJack F Vogel 	enum i40e_status_code ret_code = I40E_SUCCESS;
45661ae650dSJack F Vogel 	u16 index, word;
45761ae650dSJack F Vogel 
458f247dc25SJack F Vogel 	DEBUGFUNC("i40e_read_nvm_buffer_srctl");
45961ae650dSJack F Vogel 
4601d767a8eSEric Joyner 	/* Loop through the selected region */
46161ae650dSJack F Vogel 	for (word = 0; word < *words; word++) {
46261ae650dSJack F Vogel 		index = offset + word;
463f247dc25SJack F Vogel 		ret_code = i40e_read_nvm_word_srctl(hw, index, &data[word]);
46461ae650dSJack F Vogel 		if (ret_code != I40E_SUCCESS)
46561ae650dSJack F Vogel 			break;
46661ae650dSJack F Vogel 	}
46761ae650dSJack F Vogel 
46861ae650dSJack F Vogel 	/* Update the number of words read from the Shadow RAM */
46961ae650dSJack F Vogel 	*words = word;
47061ae650dSJack F Vogel 
47161ae650dSJack F Vogel 	return ret_code;
47261ae650dSJack F Vogel }
473f247dc25SJack F Vogel 
474f247dc25SJack F Vogel /**
475f247dc25SJack F Vogel  * i40e_read_nvm_buffer_aq - Reads Shadow RAM buffer via AQ
476f247dc25SJack F Vogel  * @hw: pointer to the HW structure
477f247dc25SJack F Vogel  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
478f247dc25SJack F Vogel  * @words: (in) number of words to read; (out) number of words actually read
479f247dc25SJack F Vogel  * @data: words read from the Shadow RAM
480f247dc25SJack F Vogel  *
481f247dc25SJack F Vogel  * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_aq()
482f247dc25SJack F Vogel  * method. The buffer read is preceded by the NVM ownership take
483f247dc25SJack F Vogel  * and followed by the release.
484f247dc25SJack F Vogel  **/
i40e_read_nvm_buffer_aq(struct i40e_hw * hw,u16 offset,u16 * words,u16 * data)485ceebc2f3SEric Joyner static enum i40e_status_code i40e_read_nvm_buffer_aq(struct i40e_hw *hw, u16 offset,
486f247dc25SJack F Vogel 						     u16 *words, u16 *data)
487f247dc25SJack F Vogel {
488f247dc25SJack F Vogel 	enum i40e_status_code ret_code;
489f247dc25SJack F Vogel 	u16 read_size = *words;
490f247dc25SJack F Vogel 	bool last_cmd = FALSE;
491f247dc25SJack F Vogel 	u16 words_read = 0;
492f247dc25SJack F Vogel 	u16 i = 0;
493f247dc25SJack F Vogel 
494f247dc25SJack F Vogel 	DEBUGFUNC("i40e_read_nvm_buffer_aq");
495f247dc25SJack F Vogel 
496f247dc25SJack F Vogel 	do {
497f247dc25SJack F Vogel 		/* Calculate number of bytes we should read in this step.
498f247dc25SJack F Vogel 		 * FVL AQ do not allow to read more than one page at a time or
499f247dc25SJack F Vogel 		 * to cross page boundaries.
500f247dc25SJack F Vogel 		 */
501f247dc25SJack F Vogel 		if (offset % I40E_SR_SECTOR_SIZE_IN_WORDS)
502f247dc25SJack F Vogel 			read_size = min(*words,
503f247dc25SJack F Vogel 					(u16)(I40E_SR_SECTOR_SIZE_IN_WORDS -
504f247dc25SJack F Vogel 				      (offset % I40E_SR_SECTOR_SIZE_IN_WORDS)));
505f247dc25SJack F Vogel 		else
506f247dc25SJack F Vogel 			read_size = min((*words - words_read),
507f247dc25SJack F Vogel 					I40E_SR_SECTOR_SIZE_IN_WORDS);
508f247dc25SJack F Vogel 
509f247dc25SJack F Vogel 		/* Check if this is last command, if so set proper flag */
510f247dc25SJack F Vogel 		if ((words_read + read_size) >= *words)
511f247dc25SJack F Vogel 			last_cmd = TRUE;
512f247dc25SJack F Vogel 
513f247dc25SJack F Vogel 		ret_code = i40e_read_nvm_aq(hw, 0x0, offset, read_size,
514f247dc25SJack F Vogel 					    data + words_read, last_cmd);
515f247dc25SJack F Vogel 		if (ret_code != I40E_SUCCESS)
516f247dc25SJack F Vogel 			goto read_nvm_buffer_aq_exit;
517f247dc25SJack F Vogel 
518f247dc25SJack F Vogel 		/* Increment counter for words already read and move offset to
519f247dc25SJack F Vogel 		 * new read location
520f247dc25SJack F Vogel 		 */
521f247dc25SJack F Vogel 		words_read += read_size;
522f247dc25SJack F Vogel 		offset += read_size;
523f247dc25SJack F Vogel 	} while (words_read < *words);
524f247dc25SJack F Vogel 
525f247dc25SJack F Vogel 	for (i = 0; i < *words; i++)
526f247dc25SJack F Vogel 		data[i] = LE16_TO_CPU(((__le16 *)data)[i]);
527f247dc25SJack F Vogel 
528f247dc25SJack F Vogel read_nvm_buffer_aq_exit:
529f247dc25SJack F Vogel 	*words = words_read;
530f247dc25SJack F Vogel 	return ret_code;
531f247dc25SJack F Vogel }
532f247dc25SJack F Vogel 
533f247dc25SJack F Vogel /**
534ceebc2f3SEric Joyner  * __i40e_read_nvm_buffer - Reads NVM buffer, caller must acquire lock
535ceebc2f3SEric Joyner  * @hw: pointer to the HW structure
536ceebc2f3SEric Joyner  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
537ceebc2f3SEric Joyner  * @words: (in) number of words to read; (out) number of words actually read
538ceebc2f3SEric Joyner  * @data: words read from the Shadow RAM
539f247dc25SJack F Vogel  *
540ceebc2f3SEric Joyner  * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_srrd()
541ceebc2f3SEric Joyner  * method.
542f247dc25SJack F Vogel  **/
__i40e_read_nvm_buffer(struct i40e_hw * hw,u16 offset,u16 * words,u16 * data)543ceebc2f3SEric Joyner enum i40e_status_code __i40e_read_nvm_buffer(struct i40e_hw *hw,
544ceebc2f3SEric Joyner 					     u16 offset,
545ceebc2f3SEric Joyner 					     u16 *words, u16 *data)
546f247dc25SJack F Vogel {
547ceebc2f3SEric Joyner 	if (hw->flags & I40E_HW_FLAG_AQ_SRCTL_ACCESS_ENABLE)
548ceebc2f3SEric Joyner 		return i40e_read_nvm_buffer_aq(hw, offset, words, data);
549f247dc25SJack F Vogel 
550ceebc2f3SEric Joyner 	return i40e_read_nvm_buffer_srctl(hw, offset, words, data);
551ceebc2f3SEric Joyner }
552f247dc25SJack F Vogel 
553ceebc2f3SEric Joyner /**
554ceebc2f3SEric Joyner  * i40e_read_nvm_buffer - Reads Shadow RAM buffer and acquire lock if necessary
555ceebc2f3SEric Joyner  * @hw: pointer to the HW structure
556ceebc2f3SEric Joyner  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
557ceebc2f3SEric Joyner  * @words: (in) number of words to read; (out) number of words actually read
558ceebc2f3SEric Joyner  * @data: words read from the Shadow RAM
559ceebc2f3SEric Joyner  *
560ceebc2f3SEric Joyner  * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_srrd()
561ceebc2f3SEric Joyner  * method. The buffer read is preceded by the NVM ownership take
562ceebc2f3SEric Joyner  * and followed by the release.
563ceebc2f3SEric Joyner  **/
i40e_read_nvm_buffer(struct i40e_hw * hw,u16 offset,u16 * words,u16 * data)564ceebc2f3SEric Joyner enum i40e_status_code i40e_read_nvm_buffer(struct i40e_hw *hw, u16 offset,
565ceebc2f3SEric Joyner 					   u16 *words, u16 *data)
566ceebc2f3SEric Joyner {
567ceebc2f3SEric Joyner 	enum i40e_status_code ret_code = I40E_SUCCESS;
568be771cdaSJack F Vogel 
569ceebc2f3SEric Joyner 	if (hw->flags & I40E_HW_FLAG_AQ_SRCTL_ACCESS_ENABLE) {
570ceebc2f3SEric Joyner 		ret_code = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
571ceebc2f3SEric Joyner 		if (!ret_code) {
572ceebc2f3SEric Joyner 			ret_code = i40e_read_nvm_buffer_aq(hw, offset, words,
573ceebc2f3SEric Joyner 							 data);
574ceebc2f3SEric Joyner 			i40e_release_nvm(hw);
575ceebc2f3SEric Joyner 		}
576ceebc2f3SEric Joyner 	} else {
577ceebc2f3SEric Joyner 		ret_code = i40e_read_nvm_buffer_srctl(hw, offset, words, data);
578ceebc2f3SEric Joyner 	}
579b4a7ce06SEric Joyner 
580f247dc25SJack F Vogel 	return ret_code;
581f247dc25SJack F Vogel }
582f247dc25SJack F Vogel 
58361ae650dSJack F Vogel /**
58461ae650dSJack F Vogel  * i40e_write_nvm_aq - Writes Shadow RAM.
58561ae650dSJack F Vogel  * @hw: pointer to the HW structure.
58661ae650dSJack F Vogel  * @module_pointer: module pointer location in words from the NVM beginning
58761ae650dSJack F Vogel  * @offset: offset in words from module start
58861ae650dSJack F Vogel  * @words: number of words to write
58961ae650dSJack F Vogel  * @data: buffer with words to write to the Shadow RAM
59061ae650dSJack F Vogel  * @last_command: tells the AdminQ that this is the last command
59161ae650dSJack F Vogel  *
59261ae650dSJack F Vogel  * Writes a 16 bit words buffer to the Shadow RAM using the admin command.
59361ae650dSJack F Vogel  **/
i40e_write_nvm_aq(struct i40e_hw * hw,u8 module_pointer,u32 offset,u16 words,void * data,bool last_command)59461ae650dSJack F Vogel enum i40e_status_code i40e_write_nvm_aq(struct i40e_hw *hw, u8 module_pointer,
59561ae650dSJack F Vogel 					u32 offset, u16 words, void *data,
59661ae650dSJack F Vogel 					bool last_command)
59761ae650dSJack F Vogel {
59861ae650dSJack F Vogel 	enum i40e_status_code ret_code = I40E_ERR_NVM;
599be771cdaSJack F Vogel 	struct i40e_asq_cmd_details cmd_details;
60061ae650dSJack F Vogel 
60161ae650dSJack F Vogel 	DEBUGFUNC("i40e_write_nvm_aq");
60261ae650dSJack F Vogel 
603be771cdaSJack F Vogel 	memset(&cmd_details, 0, sizeof(cmd_details));
604be771cdaSJack F Vogel 	cmd_details.wb_desc = &hw->nvm_wb_desc;
605be771cdaSJack F Vogel 
60661ae650dSJack F Vogel 	/* Here we are checking the SR limit only for the flat memory model.
60761ae650dSJack F Vogel 	 * We cannot do it for the module-based model, as we did not acquire
60861ae650dSJack F Vogel 	 * the NVM resource yet (we cannot get the module pointer value).
60961ae650dSJack F Vogel 	 * Firmware will check the module-based model.
61061ae650dSJack F Vogel 	 */
61161ae650dSJack F Vogel 	if ((offset + words) > hw->nvm.sr_size)
61261ae650dSJack F Vogel 		DEBUGOUT("NVM write error: offset beyond Shadow RAM limit.\n");
61361ae650dSJack F Vogel 	else if (words > I40E_SR_SECTOR_SIZE_IN_WORDS)
61461ae650dSJack F Vogel 		/* We can write only up to 4KB (one sector), in one AQ write */
61561ae650dSJack F Vogel 		DEBUGOUT("NVM write fail error: cannot write more than 4KB in a single write.\n");
61661ae650dSJack F Vogel 	else if (((offset + (words - 1)) / I40E_SR_SECTOR_SIZE_IN_WORDS)
61761ae650dSJack F Vogel 		 != (offset / I40E_SR_SECTOR_SIZE_IN_WORDS))
61861ae650dSJack F Vogel 		/* A single write cannot spread over two sectors */
61961ae650dSJack F Vogel 		DEBUGOUT("NVM write error: cannot spread over two sectors in a single write.\n");
62061ae650dSJack F Vogel 	else
62161ae650dSJack F Vogel 		ret_code = i40e_aq_update_nvm(hw, module_pointer,
62261ae650dSJack F Vogel 					      2 * offset,  /*bytes*/
62361ae650dSJack F Vogel 					      2 * words,   /*bytes*/
624ceebc2f3SEric Joyner 					      data, last_command, 0,
625ceebc2f3SEric Joyner 					      &cmd_details);
62661ae650dSJack F Vogel 
62761ae650dSJack F Vogel 	return ret_code;
62861ae650dSJack F Vogel }
62961ae650dSJack F Vogel 
63061ae650dSJack F Vogel /**
631fdb6f38aSEric Joyner  * __i40e_write_nvm_word - Writes Shadow RAM word
63261ae650dSJack F Vogel  * @hw: pointer to the HW structure
63361ae650dSJack F Vogel  * @offset: offset of the Shadow RAM word to write
63461ae650dSJack F Vogel  * @data: word to write to the Shadow RAM
63561ae650dSJack F Vogel  *
63661ae650dSJack F Vogel  * Writes a 16 bit word to the SR using the i40e_write_nvm_aq() method.
63761ae650dSJack F Vogel  * NVM ownership have to be acquired and released (on ARQ completion event
63861ae650dSJack F Vogel  * reception) by caller. To commit SR to NVM update checksum function
63961ae650dSJack F Vogel  * should be called.
64061ae650dSJack F Vogel  **/
__i40e_write_nvm_word(struct i40e_hw * hw,u32 offset,void * data)641fdb6f38aSEric Joyner enum i40e_status_code __i40e_write_nvm_word(struct i40e_hw *hw, u32 offset,
64261ae650dSJack F Vogel 					    void *data)
64361ae650dSJack F Vogel {
64461ae650dSJack F Vogel 	DEBUGFUNC("i40e_write_nvm_word");
64561ae650dSJack F Vogel 
646f247dc25SJack F Vogel 	*((__le16 *)data) = CPU_TO_LE16(*((u16 *)data));
647f247dc25SJack F Vogel 
64861ae650dSJack F Vogel 	/* Value 0x00 below means that we treat SR as a flat mem */
64961ae650dSJack F Vogel 	return i40e_write_nvm_aq(hw, 0x00, offset, 1, data, FALSE);
65061ae650dSJack F Vogel }
65161ae650dSJack F Vogel 
65261ae650dSJack F Vogel /**
653fdb6f38aSEric Joyner  * __i40e_write_nvm_buffer - Writes Shadow RAM buffer
65461ae650dSJack F Vogel  * @hw: pointer to the HW structure
65561ae650dSJack F Vogel  * @module_pointer: module pointer location in words from the NVM beginning
65661ae650dSJack F Vogel  * @offset: offset of the Shadow RAM buffer to write
65761ae650dSJack F Vogel  * @words: number of words to write
65861ae650dSJack F Vogel  * @data: words to write to the Shadow RAM
65961ae650dSJack F Vogel  *
66061ae650dSJack F Vogel  * Writes a 16 bit words buffer to the Shadow RAM using the admin command.
66161ae650dSJack F Vogel  * NVM ownership must be acquired before calling this function and released
66261ae650dSJack F Vogel  * on ARQ completion event reception by caller. To commit SR to NVM update
66361ae650dSJack F Vogel  * checksum function should be called.
66461ae650dSJack F Vogel  **/
__i40e_write_nvm_buffer(struct i40e_hw * hw,u8 module_pointer,u32 offset,u16 words,void * data)665fdb6f38aSEric Joyner enum i40e_status_code __i40e_write_nvm_buffer(struct i40e_hw *hw,
66661ae650dSJack F Vogel 					      u8 module_pointer, u32 offset,
66761ae650dSJack F Vogel 					      u16 words, void *data)
66861ae650dSJack F Vogel {
669f247dc25SJack F Vogel 	__le16 *le_word_ptr = (__le16 *)data;
670f247dc25SJack F Vogel 	u16 *word_ptr = (u16 *)data;
671f247dc25SJack F Vogel 	u32 i = 0;
672f247dc25SJack F Vogel 
67361ae650dSJack F Vogel 	DEBUGFUNC("i40e_write_nvm_buffer");
67461ae650dSJack F Vogel 
675f247dc25SJack F Vogel 	for (i = 0; i < words; i++)
676f247dc25SJack F Vogel 		le_word_ptr[i] = CPU_TO_LE16(word_ptr[i]);
677f247dc25SJack F Vogel 
67861ae650dSJack F Vogel 	/* Here we will only write one buffer as the size of the modules
67961ae650dSJack F Vogel 	 * mirrored in the Shadow RAM is always less than 4K.
68061ae650dSJack F Vogel 	 */
68161ae650dSJack F Vogel 	return i40e_write_nvm_aq(hw, module_pointer, offset, words,
68261ae650dSJack F Vogel 				 data, FALSE);
68361ae650dSJack F Vogel }
68461ae650dSJack F Vogel 
68561ae650dSJack F Vogel /**
68661ae650dSJack F Vogel  * i40e_calc_nvm_checksum - Calculates and returns the checksum
68761ae650dSJack F Vogel  * @hw: pointer to hardware structure
68861ae650dSJack F Vogel  * @checksum: pointer to the checksum
68961ae650dSJack F Vogel  *
69061ae650dSJack F Vogel  * This function calculates SW Checksum that covers the whole 64kB shadow RAM
69161ae650dSJack F Vogel  * except the VPD and PCIe ALT Auto-load modules. The structure and size of VPD
69261ae650dSJack F Vogel  * is customer specific and unknown. Therefore, this function skips all maximum
69361ae650dSJack F Vogel  * possible size of VPD (1kB).
69461ae650dSJack F Vogel  **/
i40e_calc_nvm_checksum(struct i40e_hw * hw,u16 * checksum)69561ae650dSJack F Vogel enum i40e_status_code i40e_calc_nvm_checksum(struct i40e_hw *hw, u16 *checksum)
69661ae650dSJack F Vogel {
69761ae650dSJack F Vogel 	enum i40e_status_code ret_code = I40E_SUCCESS;
698f247dc25SJack F Vogel 	struct i40e_virt_mem vmem;
69961ae650dSJack F Vogel 	u16 pcie_alt_module = 0;
70061ae650dSJack F Vogel 	u16 checksum_local = 0;
70161ae650dSJack F Vogel 	u16 vpd_module = 0;
702f247dc25SJack F Vogel 	u16 *data;
703f247dc25SJack F Vogel 	u16 i = 0;
70461ae650dSJack F Vogel 
70561ae650dSJack F Vogel 	DEBUGFUNC("i40e_calc_nvm_checksum");
70661ae650dSJack F Vogel 
707f247dc25SJack F Vogel 	ret_code = i40e_allocate_virt_mem(hw, &vmem,
708f247dc25SJack F Vogel 				    I40E_SR_SECTOR_SIZE_IN_WORDS * sizeof(u16));
709f247dc25SJack F Vogel 	if (ret_code)
710f247dc25SJack F Vogel 		goto i40e_calc_nvm_checksum_exit;
711f247dc25SJack F Vogel 	data = (u16 *)vmem.va;
712f247dc25SJack F Vogel 
71361ae650dSJack F Vogel 	/* read pointer to VPD area */
714ceebc2f3SEric Joyner 	ret_code = __i40e_read_nvm_word(hw, I40E_SR_VPD_PTR, &vpd_module);
71561ae650dSJack F Vogel 	if (ret_code != I40E_SUCCESS) {
71661ae650dSJack F Vogel 		ret_code = I40E_ERR_NVM_CHECKSUM;
71761ae650dSJack F Vogel 		goto i40e_calc_nvm_checksum_exit;
71861ae650dSJack F Vogel 	}
71961ae650dSJack F Vogel 
72061ae650dSJack F Vogel 	/* read pointer to PCIe Alt Auto-load module */
721ceebc2f3SEric Joyner 	ret_code = __i40e_read_nvm_word(hw, I40E_SR_PCIE_ALT_AUTO_LOAD_PTR,
72261ae650dSJack F Vogel 					&pcie_alt_module);
72361ae650dSJack F Vogel 	if (ret_code != I40E_SUCCESS) {
72461ae650dSJack F Vogel 		ret_code = I40E_ERR_NVM_CHECKSUM;
72561ae650dSJack F Vogel 		goto i40e_calc_nvm_checksum_exit;
72661ae650dSJack F Vogel 	}
72761ae650dSJack F Vogel 
72861ae650dSJack F Vogel 	/* Calculate SW checksum that covers the whole 64kB shadow RAM
72961ae650dSJack F Vogel 	 * except the VPD and PCIe ALT Auto-load modules
73061ae650dSJack F Vogel 	 */
73161ae650dSJack F Vogel 	for (i = 0; i < hw->nvm.sr_size; i++) {
732f247dc25SJack F Vogel 		/* Read SR page */
733f247dc25SJack F Vogel 		if ((i % I40E_SR_SECTOR_SIZE_IN_WORDS) == 0) {
734f247dc25SJack F Vogel 			u16 words = I40E_SR_SECTOR_SIZE_IN_WORDS;
735be771cdaSJack F Vogel 
736fdb6f38aSEric Joyner 			ret_code = __i40e_read_nvm_buffer(hw, i, &words, data);
73761ae650dSJack F Vogel 			if (ret_code != I40E_SUCCESS) {
73861ae650dSJack F Vogel 				ret_code = I40E_ERR_NVM_CHECKSUM;
73961ae650dSJack F Vogel 				goto i40e_calc_nvm_checksum_exit;
74061ae650dSJack F Vogel 			}
741f247dc25SJack F Vogel 		}
742f247dc25SJack F Vogel 
743f247dc25SJack F Vogel 		/* Skip Checksum word */
744f247dc25SJack F Vogel 		if (i == I40E_SR_SW_CHECKSUM_WORD)
745f247dc25SJack F Vogel 			continue;
746f247dc25SJack F Vogel 		/* Skip VPD module (convert byte size to word count) */
747f247dc25SJack F Vogel 		if ((i >= (u32)vpd_module) &&
748f247dc25SJack F Vogel 		    (i < ((u32)vpd_module +
749f247dc25SJack F Vogel 		     (I40E_SR_VPD_MODULE_MAX_SIZE / 2)))) {
750f247dc25SJack F Vogel 			continue;
751f247dc25SJack F Vogel 		}
752f247dc25SJack F Vogel 		/* Skip PCIe ALT module (convert byte size to word count) */
753f247dc25SJack F Vogel 		if ((i >= (u32)pcie_alt_module) &&
754f247dc25SJack F Vogel 		    (i < ((u32)pcie_alt_module +
755f247dc25SJack F Vogel 		     (I40E_SR_PCIE_ALT_MODULE_MAX_SIZE / 2)))) {
756f247dc25SJack F Vogel 			continue;
757f247dc25SJack F Vogel 		}
758f247dc25SJack F Vogel 
759f247dc25SJack F Vogel 		checksum_local += data[i % I40E_SR_SECTOR_SIZE_IN_WORDS];
76061ae650dSJack F Vogel 	}
76161ae650dSJack F Vogel 
76261ae650dSJack F Vogel 	*checksum = (u16)I40E_SR_SW_CHECKSUM_BASE - checksum_local;
76361ae650dSJack F Vogel 
76461ae650dSJack F Vogel i40e_calc_nvm_checksum_exit:
765f247dc25SJack F Vogel 	i40e_free_virt_mem(hw, &vmem);
76661ae650dSJack F Vogel 	return ret_code;
76761ae650dSJack F Vogel }
76861ae650dSJack F Vogel 
76961ae650dSJack F Vogel /**
77061ae650dSJack F Vogel  * i40e_update_nvm_checksum - Updates the NVM checksum
77161ae650dSJack F Vogel  * @hw: pointer to hardware structure
77261ae650dSJack F Vogel  *
77361ae650dSJack F Vogel  * NVM ownership must be acquired before calling this function and released
77461ae650dSJack F Vogel  * on ARQ completion event reception by caller.
77561ae650dSJack F Vogel  * This function will commit SR to NVM.
77661ae650dSJack F Vogel  **/
i40e_update_nvm_checksum(struct i40e_hw * hw)77761ae650dSJack F Vogel enum i40e_status_code i40e_update_nvm_checksum(struct i40e_hw *hw)
77861ae650dSJack F Vogel {
77961ae650dSJack F Vogel 	enum i40e_status_code ret_code = I40E_SUCCESS;
78061ae650dSJack F Vogel 	u16 checksum;
781be771cdaSJack F Vogel 	__le16 le_sum;
78261ae650dSJack F Vogel 
78361ae650dSJack F Vogel 	DEBUGFUNC("i40e_update_nvm_checksum");
78461ae650dSJack F Vogel 
78561ae650dSJack F Vogel 	ret_code = i40e_calc_nvm_checksum(hw, &checksum);
786*abf77452SKrzysztof Galazka 	if (ret_code == I40E_SUCCESS) {
787be771cdaSJack F Vogel 		le_sum = CPU_TO_LE16(checksum);
78861ae650dSJack F Vogel 		ret_code = i40e_write_nvm_aq(hw, 0x00, I40E_SR_SW_CHECKSUM_WORD,
789be771cdaSJack F Vogel 					     1, &le_sum, TRUE);
790*abf77452SKrzysztof Galazka 	}
79161ae650dSJack F Vogel 
79261ae650dSJack F Vogel 	return ret_code;
79361ae650dSJack F Vogel }
79461ae650dSJack F Vogel 
79561ae650dSJack F Vogel /**
79661ae650dSJack F Vogel  * i40e_validate_nvm_checksum - Validate EEPROM checksum
79761ae650dSJack F Vogel  * @hw: pointer to hardware structure
79861ae650dSJack F Vogel  * @checksum: calculated checksum
79961ae650dSJack F Vogel  *
80061ae650dSJack F Vogel  * Performs checksum calculation and validates the NVM SW checksum. If the
80161ae650dSJack F Vogel  * caller does not need checksum, the value can be NULL.
80261ae650dSJack F Vogel  **/
i40e_validate_nvm_checksum(struct i40e_hw * hw,u16 * checksum)80361ae650dSJack F Vogel enum i40e_status_code i40e_validate_nvm_checksum(struct i40e_hw *hw,
80461ae650dSJack F Vogel 						 u16 *checksum)
80561ae650dSJack F Vogel {
80661ae650dSJack F Vogel 	enum i40e_status_code ret_code = I40E_SUCCESS;
80761ae650dSJack F Vogel 	u16 checksum_sr = 0;
80861ae650dSJack F Vogel 	u16 checksum_local = 0;
80961ae650dSJack F Vogel 
81061ae650dSJack F Vogel 	DEBUGFUNC("i40e_validate_nvm_checksum");
81161ae650dSJack F Vogel 
812ceebc2f3SEric Joyner 	/* We must acquire the NVM lock in order to correctly synchronize the
813ceebc2f3SEric Joyner 	 * NVM accesses across multiple PFs. Without doing so it is possible
814ceebc2f3SEric Joyner 	 * for one of the PFs to read invalid data potentially indicating that
815ceebc2f3SEric Joyner 	 * the checksum is invalid.
816ceebc2f3SEric Joyner 	 */
817fdb6f38aSEric Joyner 	ret_code = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
818ceebc2f3SEric Joyner 	if (ret_code)
819ceebc2f3SEric Joyner 		return ret_code;
82061ae650dSJack F Vogel 	ret_code = i40e_calc_nvm_checksum(hw, &checksum_local);
821ceebc2f3SEric Joyner 	__i40e_read_nvm_word(hw, I40E_SR_SW_CHECKSUM_WORD, &checksum_sr);
822fdb6f38aSEric Joyner 	i40e_release_nvm(hw);
823ceebc2f3SEric Joyner 	if (ret_code)
824ceebc2f3SEric Joyner 		return ret_code;
82561ae650dSJack F Vogel 
82661ae650dSJack F Vogel 	/* Verify read checksum from EEPROM is the same as
82761ae650dSJack F Vogel 	 * calculated checksum
82861ae650dSJack F Vogel 	 */
82961ae650dSJack F Vogel 	if (checksum_local != checksum_sr)
83061ae650dSJack F Vogel 		ret_code = I40E_ERR_NVM_CHECKSUM;
83161ae650dSJack F Vogel 
83261ae650dSJack F Vogel 	/* If the user cares, return the calculated checksum */
83361ae650dSJack F Vogel 	if (checksum)
83461ae650dSJack F Vogel 		*checksum = checksum_local;
83561ae650dSJack F Vogel 
83661ae650dSJack F Vogel 	return ret_code;
83761ae650dSJack F Vogel }
838223d846dSEric Joyner 
839223d846dSEric Joyner static enum i40e_status_code i40e_nvmupd_state_init(struct i40e_hw *hw,
840223d846dSEric Joyner 						    struct i40e_nvm_access *cmd,
841223d846dSEric Joyner 						    u8 *bytes, int *perrno);
842223d846dSEric Joyner static enum i40e_status_code i40e_nvmupd_state_reading(struct i40e_hw *hw,
843223d846dSEric Joyner 						    struct i40e_nvm_access *cmd,
844223d846dSEric Joyner 						    u8 *bytes, int *perrno);
845223d846dSEric Joyner static enum i40e_status_code i40e_nvmupd_state_writing(struct i40e_hw *hw,
846223d846dSEric Joyner 						    struct i40e_nvm_access *cmd,
847223d846dSEric Joyner 						    u8 *bytes, int *perrno);
848223d846dSEric Joyner static enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
849223d846dSEric Joyner 						    struct i40e_nvm_access *cmd,
850223d846dSEric Joyner 						    int *perrno);
851223d846dSEric Joyner static enum i40e_status_code i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
852223d846dSEric Joyner 						   struct i40e_nvm_access *cmd,
853223d846dSEric Joyner 						   int *perrno);
854223d846dSEric Joyner static enum i40e_status_code i40e_nvmupd_nvm_write(struct i40e_hw *hw,
855223d846dSEric Joyner 						   struct i40e_nvm_access *cmd,
856223d846dSEric Joyner 						   u8 *bytes, int *perrno);
857223d846dSEric Joyner static enum i40e_status_code i40e_nvmupd_nvm_read(struct i40e_hw *hw,
858223d846dSEric Joyner 						  struct i40e_nvm_access *cmd,
859223d846dSEric Joyner 						  u8 *bytes, int *perrno);
860223d846dSEric Joyner static enum i40e_status_code i40e_nvmupd_exec_aq(struct i40e_hw *hw,
861223d846dSEric Joyner 						 struct i40e_nvm_access *cmd,
862223d846dSEric Joyner 						 u8 *bytes, int *perrno);
863223d846dSEric Joyner static enum i40e_status_code i40e_nvmupd_get_aq_result(struct i40e_hw *hw,
864223d846dSEric Joyner 						    struct i40e_nvm_access *cmd,
865223d846dSEric Joyner 						    u8 *bytes, int *perrno);
866ceebc2f3SEric Joyner static enum i40e_status_code i40e_nvmupd_get_aq_event(struct i40e_hw *hw,
867ceebc2f3SEric Joyner 						    struct i40e_nvm_access *cmd,
868ceebc2f3SEric Joyner 						    u8 *bytes, int *perrno);
i40e_nvmupd_get_module(u32 val)869223d846dSEric Joyner static INLINE u8 i40e_nvmupd_get_module(u32 val)
870223d846dSEric Joyner {
871223d846dSEric Joyner 	return (u8)(val & I40E_NVM_MOD_PNT_MASK);
872223d846dSEric Joyner }
i40e_nvmupd_get_transaction(u32 val)873223d846dSEric Joyner static INLINE u8 i40e_nvmupd_get_transaction(u32 val)
874223d846dSEric Joyner {
875223d846dSEric Joyner 	return (u8)((val & I40E_NVM_TRANS_MASK) >> I40E_NVM_TRANS_SHIFT);
876223d846dSEric Joyner }
877223d846dSEric Joyner 
i40e_nvmupd_get_preservation_flags(u32 val)878ceebc2f3SEric Joyner static INLINE u8 i40e_nvmupd_get_preservation_flags(u32 val)
879ceebc2f3SEric Joyner {
880ceebc2f3SEric Joyner 	return (u8)((val & I40E_NVM_PRESERVATION_FLAGS_MASK) >>
881ceebc2f3SEric Joyner 		    I40E_NVM_PRESERVATION_FLAGS_SHIFT);
882ceebc2f3SEric Joyner }
883ceebc2f3SEric Joyner 
884223d846dSEric Joyner static const char *i40e_nvm_update_state_str[] = {
885223d846dSEric Joyner 	"I40E_NVMUPD_INVALID",
886223d846dSEric Joyner 	"I40E_NVMUPD_READ_CON",
887223d846dSEric Joyner 	"I40E_NVMUPD_READ_SNT",
888223d846dSEric Joyner 	"I40E_NVMUPD_READ_LCB",
889223d846dSEric Joyner 	"I40E_NVMUPD_READ_SA",
890223d846dSEric Joyner 	"I40E_NVMUPD_WRITE_ERA",
891223d846dSEric Joyner 	"I40E_NVMUPD_WRITE_CON",
892223d846dSEric Joyner 	"I40E_NVMUPD_WRITE_SNT",
893223d846dSEric Joyner 	"I40E_NVMUPD_WRITE_LCB",
894223d846dSEric Joyner 	"I40E_NVMUPD_WRITE_SA",
895223d846dSEric Joyner 	"I40E_NVMUPD_CSUM_CON",
896223d846dSEric Joyner 	"I40E_NVMUPD_CSUM_SA",
897223d846dSEric Joyner 	"I40E_NVMUPD_CSUM_LCB",
898223d846dSEric Joyner 	"I40E_NVMUPD_STATUS",
899223d846dSEric Joyner 	"I40E_NVMUPD_EXEC_AQ",
900223d846dSEric Joyner 	"I40E_NVMUPD_GET_AQ_RESULT",
901ceebc2f3SEric Joyner 	"I40E_NVMUPD_GET_AQ_EVENT",
902b4a7ce06SEric Joyner 	"I40E_NVMUPD_GET_FEATURES",
903223d846dSEric Joyner };
904223d846dSEric Joyner 
905223d846dSEric Joyner /**
906223d846dSEric Joyner  * i40e_nvmupd_command - Process an NVM update command
907223d846dSEric Joyner  * @hw: pointer to hardware structure
908223d846dSEric Joyner  * @cmd: pointer to nvm update command
909223d846dSEric Joyner  * @bytes: pointer to the data buffer
910223d846dSEric Joyner  * @perrno: pointer to return error code
911223d846dSEric Joyner  *
912223d846dSEric Joyner  * Dispatches command depending on what update state is current
913223d846dSEric Joyner  **/
i40e_nvmupd_command(struct i40e_hw * hw,struct i40e_nvm_access * cmd,u8 * bytes,int * perrno)914223d846dSEric Joyner enum i40e_status_code i40e_nvmupd_command(struct i40e_hw *hw,
915223d846dSEric Joyner 					  struct i40e_nvm_access *cmd,
916223d846dSEric Joyner 					  u8 *bytes, int *perrno)
917223d846dSEric Joyner {
918223d846dSEric Joyner 	enum i40e_status_code status;
919223d846dSEric Joyner 	enum i40e_nvmupd_cmd upd_cmd;
920223d846dSEric Joyner 
921223d846dSEric Joyner 	DEBUGFUNC("i40e_nvmupd_command");
922223d846dSEric Joyner 
923223d846dSEric Joyner 	/* assume success */
924223d846dSEric Joyner 	*perrno = 0;
925223d846dSEric Joyner 
926223d846dSEric Joyner 	/* early check for status command and debug msgs */
927223d846dSEric Joyner 	upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
928223d846dSEric Joyner 
9294294f337SSean Bruno 	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",
930223d846dSEric Joyner 		   i40e_nvm_update_state_str[upd_cmd],
931223d846dSEric Joyner 		   hw->nvmupd_state,
9324294f337SSean Bruno 		   hw->nvm_release_on_done, hw->nvm_wait_opcode,
933fdb6f38aSEric Joyner 		   cmd->command, cmd->config, cmd->offset, cmd->data_size);
934223d846dSEric Joyner 
935223d846dSEric Joyner 	if (upd_cmd == I40E_NVMUPD_INVALID) {
936223d846dSEric Joyner 		*perrno = -EFAULT;
937223d846dSEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM,
938223d846dSEric Joyner 			   "i40e_nvmupd_validate_command returns %d errno %d\n",
939223d846dSEric Joyner 			   upd_cmd, *perrno);
940223d846dSEric Joyner 	}
941223d846dSEric Joyner 
942223d846dSEric Joyner 	/* a status request returns immediately rather than
943223d846dSEric Joyner 	 * going into the state machine
944223d846dSEric Joyner 	 */
945223d846dSEric Joyner 	if (upd_cmd == I40E_NVMUPD_STATUS) {
9464294f337SSean Bruno 		if (!cmd->data_size) {
9474294f337SSean Bruno 			*perrno = -EFAULT;
9484294f337SSean Bruno 			return I40E_ERR_BUF_TOO_SHORT;
9494294f337SSean Bruno 		}
9504294f337SSean Bruno 
951223d846dSEric Joyner 		bytes[0] = hw->nvmupd_state;
9524294f337SSean Bruno 
9534294f337SSean Bruno 		if (cmd->data_size >= 4) {
9544294f337SSean Bruno 			bytes[1] = 0;
9554294f337SSean Bruno 			*((u16 *)&bytes[2]) = hw->nvm_wait_opcode;
9564294f337SSean Bruno 		}
9574294f337SSean Bruno 
958cb6b8299SEric Joyner 		/* Clear error status on read */
959cb6b8299SEric Joyner 		if (hw->nvmupd_state == I40E_NVMUPD_STATE_ERROR)
960cb6b8299SEric Joyner 			hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
961cb6b8299SEric Joyner 
962223d846dSEric Joyner 		return I40E_SUCCESS;
963223d846dSEric Joyner 	}
964223d846dSEric Joyner 
965b4a7ce06SEric Joyner 	/*
966b4a7ce06SEric Joyner 	 * A supported features request returns immediately
967b4a7ce06SEric Joyner 	 * rather than going into state machine
968b4a7ce06SEric Joyner 	 */
969b4a7ce06SEric Joyner 	if (upd_cmd == I40E_NVMUPD_FEATURES) {
970b4a7ce06SEric Joyner 		if (cmd->data_size < hw->nvmupd_features.size) {
971b4a7ce06SEric Joyner 			*perrno = -EFAULT;
972b4a7ce06SEric Joyner 			return I40E_ERR_BUF_TOO_SHORT;
973b4a7ce06SEric Joyner 		}
974b4a7ce06SEric Joyner 
975b4a7ce06SEric Joyner 		/*
976b4a7ce06SEric Joyner 		 * If buffer is bigger than i40e_nvmupd_features structure,
977b4a7ce06SEric Joyner 		 * make sure the trailing bytes are set to 0x0.
978b4a7ce06SEric Joyner 		 */
979b4a7ce06SEric Joyner 		if (cmd->data_size > hw->nvmupd_features.size)
980b4a7ce06SEric Joyner 			i40e_memset(bytes + hw->nvmupd_features.size, 0x0,
981b4a7ce06SEric Joyner 				    cmd->data_size - hw->nvmupd_features.size,
982b4a7ce06SEric Joyner 				    I40E_NONDMA_MEM);
983b4a7ce06SEric Joyner 
984b4a7ce06SEric Joyner 		i40e_memcpy(bytes, &hw->nvmupd_features,
985b4a7ce06SEric Joyner 			    hw->nvmupd_features.size, I40E_NONDMA_MEM);
986b4a7ce06SEric Joyner 
987b4a7ce06SEric Joyner 		return I40E_SUCCESS;
988b4a7ce06SEric Joyner 	}
989b4a7ce06SEric Joyner 
990cb6b8299SEric Joyner 	/* Clear status even it is not read and log */
991cb6b8299SEric Joyner 	if (hw->nvmupd_state == I40E_NVMUPD_STATE_ERROR) {
992cb6b8299SEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM,
993cb6b8299SEric Joyner 			   "Clearing I40E_NVMUPD_STATE_ERROR state without reading\n");
994cb6b8299SEric Joyner 		hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
995cb6b8299SEric Joyner 	}
996cb6b8299SEric Joyner 
997ceebc2f3SEric Joyner 	/* Acquire lock to prevent race condition where adminq_task
998ceebc2f3SEric Joyner 	 * can execute after i40e_nvmupd_nvm_read/write but before state
999ceebc2f3SEric Joyner 	 * variables (nvm_wait_opcode, nvm_release_on_done) are updated.
1000ceebc2f3SEric Joyner 	 *
1001ceebc2f3SEric Joyner 	 * During NVMUpdate, it is observed that lock could be held for
1002ceebc2f3SEric Joyner 	 * ~5ms for most commands. However lock is held for ~60ms for
1003ceebc2f3SEric Joyner 	 * NVMUPD_CSUM_LCB command.
1004ceebc2f3SEric Joyner 	 */
1005ceebc2f3SEric Joyner 	i40e_acquire_spinlock(&hw->aq.arq_spinlock);
1006223d846dSEric Joyner 	switch (hw->nvmupd_state) {
1007223d846dSEric Joyner 	case I40E_NVMUPD_STATE_INIT:
1008223d846dSEric Joyner 		status = i40e_nvmupd_state_init(hw, cmd, bytes, perrno);
1009223d846dSEric Joyner 		break;
1010223d846dSEric Joyner 
1011223d846dSEric Joyner 	case I40E_NVMUPD_STATE_READING:
1012223d846dSEric Joyner 		status = i40e_nvmupd_state_reading(hw, cmd, bytes, perrno);
1013223d846dSEric Joyner 		break;
1014223d846dSEric Joyner 
1015223d846dSEric Joyner 	case I40E_NVMUPD_STATE_WRITING:
1016223d846dSEric Joyner 		status = i40e_nvmupd_state_writing(hw, cmd, bytes, perrno);
1017223d846dSEric Joyner 		break;
1018223d846dSEric Joyner 
1019223d846dSEric Joyner 	case I40E_NVMUPD_STATE_INIT_WAIT:
1020223d846dSEric Joyner 	case I40E_NVMUPD_STATE_WRITE_WAIT:
10214294f337SSean Bruno 		/* if we need to stop waiting for an event, clear
10224294f337SSean Bruno 		 * the wait info and return before doing anything else
10234294f337SSean Bruno 		 */
10244294f337SSean Bruno 		if (cmd->offset == 0xffff) {
1025ceebc2f3SEric Joyner 			i40e_nvmupd_clear_wait_state(hw);
1026ceebc2f3SEric Joyner 			status = I40E_SUCCESS;
1027ceebc2f3SEric Joyner 			break;
10284294f337SSean Bruno 		}
10294294f337SSean Bruno 
1030223d846dSEric Joyner 		status = I40E_ERR_NOT_READY;
1031223d846dSEric Joyner 		*perrno = -EBUSY;
1032223d846dSEric Joyner 		break;
1033223d846dSEric Joyner 
1034223d846dSEric Joyner 	default:
1035223d846dSEric Joyner 		/* invalid state, should never happen */
1036223d846dSEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM,
1037223d846dSEric Joyner 			   "NVMUPD: no such state %d\n", hw->nvmupd_state);
1038223d846dSEric Joyner 		status = I40E_NOT_SUPPORTED;
1039223d846dSEric Joyner 		*perrno = -ESRCH;
1040223d846dSEric Joyner 		break;
1041223d846dSEric Joyner 	}
1042ceebc2f3SEric Joyner 
1043ceebc2f3SEric Joyner 	i40e_release_spinlock(&hw->aq.arq_spinlock);
1044223d846dSEric Joyner 	return status;
1045223d846dSEric Joyner }
1046223d846dSEric Joyner 
1047223d846dSEric Joyner /**
1048223d846dSEric Joyner  * i40e_nvmupd_state_init - Handle NVM update state Init
1049223d846dSEric Joyner  * @hw: pointer to hardware structure
1050223d846dSEric Joyner  * @cmd: pointer to nvm update command buffer
1051223d846dSEric Joyner  * @bytes: pointer to the data buffer
1052223d846dSEric Joyner  * @perrno: pointer to return error code
1053223d846dSEric Joyner  *
1054223d846dSEric Joyner  * Process legitimate commands of the Init state and conditionally set next
1055223d846dSEric Joyner  * state. Reject all other commands.
1056223d846dSEric Joyner  **/
i40e_nvmupd_state_init(struct i40e_hw * hw,struct i40e_nvm_access * cmd,u8 * bytes,int * perrno)1057223d846dSEric Joyner static enum i40e_status_code i40e_nvmupd_state_init(struct i40e_hw *hw,
1058223d846dSEric Joyner 						    struct i40e_nvm_access *cmd,
1059223d846dSEric Joyner 						    u8 *bytes, int *perrno)
1060223d846dSEric Joyner {
1061223d846dSEric Joyner 	enum i40e_status_code status = I40E_SUCCESS;
1062223d846dSEric Joyner 	enum i40e_nvmupd_cmd upd_cmd;
1063223d846dSEric Joyner 
1064223d846dSEric Joyner 	DEBUGFUNC("i40e_nvmupd_state_init");
1065223d846dSEric Joyner 
1066223d846dSEric Joyner 	upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
1067223d846dSEric Joyner 
1068223d846dSEric Joyner 	switch (upd_cmd) {
1069223d846dSEric Joyner 	case I40E_NVMUPD_READ_SA:
1070223d846dSEric Joyner 		status = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
1071223d846dSEric Joyner 		if (status) {
1072223d846dSEric Joyner 			*perrno = i40e_aq_rc_to_posix(status,
1073223d846dSEric Joyner 						     hw->aq.asq_last_status);
1074223d846dSEric Joyner 		} else {
1075223d846dSEric Joyner 			status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
1076223d846dSEric Joyner 			i40e_release_nvm(hw);
1077223d846dSEric Joyner 		}
1078223d846dSEric Joyner 		break;
1079223d846dSEric Joyner 
1080223d846dSEric Joyner 	case I40E_NVMUPD_READ_SNT:
1081223d846dSEric Joyner 		status = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
1082223d846dSEric Joyner 		if (status) {
1083223d846dSEric Joyner 			*perrno = i40e_aq_rc_to_posix(status,
1084223d846dSEric Joyner 						     hw->aq.asq_last_status);
1085223d846dSEric Joyner 		} else {
1086223d846dSEric Joyner 			status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
1087223d846dSEric Joyner 			if (status)
1088223d846dSEric Joyner 				i40e_release_nvm(hw);
1089223d846dSEric Joyner 			else
1090223d846dSEric Joyner 				hw->nvmupd_state = I40E_NVMUPD_STATE_READING;
1091223d846dSEric Joyner 		}
1092223d846dSEric Joyner 		break;
1093223d846dSEric Joyner 
1094223d846dSEric Joyner 	case I40E_NVMUPD_WRITE_ERA:
1095223d846dSEric Joyner 		status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
1096223d846dSEric Joyner 		if (status) {
1097223d846dSEric Joyner 			*perrno = i40e_aq_rc_to_posix(status,
1098223d846dSEric Joyner 						     hw->aq.asq_last_status);
1099223d846dSEric Joyner 		} else {
1100223d846dSEric Joyner 			status = i40e_nvmupd_nvm_erase(hw, cmd, perrno);
1101223d846dSEric Joyner 			if (status) {
1102223d846dSEric Joyner 				i40e_release_nvm(hw);
1103223d846dSEric Joyner 			} else {
11044294f337SSean Bruno 				hw->nvm_release_on_done = TRUE;
11054294f337SSean Bruno 				hw->nvm_wait_opcode = i40e_aqc_opc_nvm_erase;
1106223d846dSEric Joyner 				hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
1107223d846dSEric Joyner 			}
1108223d846dSEric Joyner 		}
1109223d846dSEric Joyner 		break;
1110223d846dSEric Joyner 
1111223d846dSEric Joyner 	case I40E_NVMUPD_WRITE_SA:
1112223d846dSEric Joyner 		status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
1113223d846dSEric Joyner 		if (status) {
1114223d846dSEric Joyner 			*perrno = i40e_aq_rc_to_posix(status,
1115223d846dSEric Joyner 						     hw->aq.asq_last_status);
1116223d846dSEric Joyner 		} else {
1117223d846dSEric Joyner 			status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
1118223d846dSEric Joyner 			if (status) {
1119223d846dSEric Joyner 				i40e_release_nvm(hw);
1120223d846dSEric Joyner 			} else {
11214294f337SSean Bruno 				hw->nvm_release_on_done = TRUE;
11224294f337SSean Bruno 				hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
1123223d846dSEric Joyner 				hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
1124223d846dSEric Joyner 			}
1125223d846dSEric Joyner 		}
1126223d846dSEric Joyner 		break;
1127223d846dSEric Joyner 
1128223d846dSEric Joyner 	case I40E_NVMUPD_WRITE_SNT:
1129223d846dSEric Joyner 		status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
1130223d846dSEric Joyner 		if (status) {
1131223d846dSEric Joyner 			*perrno = i40e_aq_rc_to_posix(status,
1132223d846dSEric Joyner 						     hw->aq.asq_last_status);
1133223d846dSEric Joyner 		} else {
1134223d846dSEric Joyner 			status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
11354294f337SSean Bruno 			if (status) {
1136223d846dSEric Joyner 				i40e_release_nvm(hw);
11374294f337SSean Bruno 			} else {
11384294f337SSean Bruno 				hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
1139223d846dSEric Joyner 				hw->nvmupd_state = I40E_NVMUPD_STATE_WRITE_WAIT;
1140223d846dSEric Joyner 			}
11414294f337SSean Bruno 		}
1142223d846dSEric Joyner 		break;
1143223d846dSEric Joyner 
1144223d846dSEric Joyner 	case I40E_NVMUPD_CSUM_SA:
1145223d846dSEric Joyner 		status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
1146223d846dSEric Joyner 		if (status) {
1147223d846dSEric Joyner 			*perrno = i40e_aq_rc_to_posix(status,
1148223d846dSEric Joyner 						     hw->aq.asq_last_status);
1149223d846dSEric Joyner 		} else {
1150223d846dSEric Joyner 			status = i40e_update_nvm_checksum(hw);
1151223d846dSEric Joyner 			if (status) {
1152223d846dSEric Joyner 				*perrno = hw->aq.asq_last_status ?
1153223d846dSEric Joyner 				   i40e_aq_rc_to_posix(status,
1154223d846dSEric Joyner 						       hw->aq.asq_last_status) :
1155223d846dSEric Joyner 				   -EIO;
1156223d846dSEric Joyner 				i40e_release_nvm(hw);
1157223d846dSEric Joyner 			} else {
11584294f337SSean Bruno 				hw->nvm_release_on_done = TRUE;
11594294f337SSean Bruno 				hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
1160223d846dSEric Joyner 				hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
1161223d846dSEric Joyner 			}
1162223d846dSEric Joyner 		}
1163223d846dSEric Joyner 		break;
1164223d846dSEric Joyner 
1165223d846dSEric Joyner 	case I40E_NVMUPD_EXEC_AQ:
1166223d846dSEric Joyner 		status = i40e_nvmupd_exec_aq(hw, cmd, bytes, perrno);
1167223d846dSEric Joyner 		break;
1168223d846dSEric Joyner 
1169223d846dSEric Joyner 	case I40E_NVMUPD_GET_AQ_RESULT:
1170223d846dSEric Joyner 		status = i40e_nvmupd_get_aq_result(hw, cmd, bytes, perrno);
1171223d846dSEric Joyner 		break;
1172223d846dSEric Joyner 
1173ceebc2f3SEric Joyner 	case I40E_NVMUPD_GET_AQ_EVENT:
1174ceebc2f3SEric Joyner 		status = i40e_nvmupd_get_aq_event(hw, cmd, bytes, perrno);
1175ceebc2f3SEric Joyner 		break;
1176ceebc2f3SEric Joyner 
1177223d846dSEric Joyner 	default:
1178223d846dSEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM,
1179223d846dSEric Joyner 			   "NVMUPD: bad cmd %s in init state\n",
1180223d846dSEric Joyner 			   i40e_nvm_update_state_str[upd_cmd]);
1181223d846dSEric Joyner 		status = I40E_ERR_NVM;
1182223d846dSEric Joyner 		*perrno = -ESRCH;
1183223d846dSEric Joyner 		break;
1184223d846dSEric Joyner 	}
1185223d846dSEric Joyner 	return status;
1186223d846dSEric Joyner }
1187223d846dSEric Joyner 
1188223d846dSEric Joyner /**
1189223d846dSEric Joyner  * i40e_nvmupd_state_reading - Handle NVM update state Reading
1190223d846dSEric Joyner  * @hw: pointer to hardware structure
1191223d846dSEric Joyner  * @cmd: pointer to nvm update command buffer
1192223d846dSEric Joyner  * @bytes: pointer to the data buffer
1193223d846dSEric Joyner  * @perrno: pointer to return error code
1194223d846dSEric Joyner  *
1195223d846dSEric Joyner  * NVM ownership is already held.  Process legitimate commands and set any
1196223d846dSEric Joyner  * change in state; reject all other commands.
1197223d846dSEric Joyner  **/
i40e_nvmupd_state_reading(struct i40e_hw * hw,struct i40e_nvm_access * cmd,u8 * bytes,int * perrno)1198223d846dSEric Joyner static enum i40e_status_code i40e_nvmupd_state_reading(struct i40e_hw *hw,
1199223d846dSEric Joyner 						    struct i40e_nvm_access *cmd,
1200223d846dSEric Joyner 						    u8 *bytes, int *perrno)
1201223d846dSEric Joyner {
1202223d846dSEric Joyner 	enum i40e_status_code status = I40E_SUCCESS;
1203223d846dSEric Joyner 	enum i40e_nvmupd_cmd upd_cmd;
1204223d846dSEric Joyner 
1205223d846dSEric Joyner 	DEBUGFUNC("i40e_nvmupd_state_reading");
1206223d846dSEric Joyner 
1207223d846dSEric Joyner 	upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
1208223d846dSEric Joyner 
1209223d846dSEric Joyner 	switch (upd_cmd) {
1210223d846dSEric Joyner 	case I40E_NVMUPD_READ_SA:
1211223d846dSEric Joyner 	case I40E_NVMUPD_READ_CON:
1212223d846dSEric Joyner 		status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
1213223d846dSEric Joyner 		break;
1214223d846dSEric Joyner 
1215223d846dSEric Joyner 	case I40E_NVMUPD_READ_LCB:
1216223d846dSEric Joyner 		status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
1217223d846dSEric Joyner 		i40e_release_nvm(hw);
1218223d846dSEric Joyner 		hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
1219223d846dSEric Joyner 		break;
1220223d846dSEric Joyner 
1221223d846dSEric Joyner 	default:
1222223d846dSEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM,
1223223d846dSEric Joyner 			   "NVMUPD: bad cmd %s in reading state.\n",
1224223d846dSEric Joyner 			   i40e_nvm_update_state_str[upd_cmd]);
1225223d846dSEric Joyner 		status = I40E_NOT_SUPPORTED;
1226223d846dSEric Joyner 		*perrno = -ESRCH;
1227223d846dSEric Joyner 		break;
1228223d846dSEric Joyner 	}
1229223d846dSEric Joyner 	return status;
1230223d846dSEric Joyner }
1231223d846dSEric Joyner 
1232223d846dSEric Joyner /**
1233223d846dSEric Joyner  * i40e_nvmupd_state_writing - Handle NVM update state Writing
1234223d846dSEric Joyner  * @hw: pointer to hardware structure
1235223d846dSEric Joyner  * @cmd: pointer to nvm update command buffer
1236223d846dSEric Joyner  * @bytes: pointer to the data buffer
1237223d846dSEric Joyner  * @perrno: pointer to return error code
1238223d846dSEric Joyner  *
1239223d846dSEric Joyner  * NVM ownership is already held.  Process legitimate commands and set any
1240223d846dSEric Joyner  * change in state; reject all other commands
1241223d846dSEric Joyner  **/
i40e_nvmupd_state_writing(struct i40e_hw * hw,struct i40e_nvm_access * cmd,u8 * bytes,int * perrno)1242223d846dSEric Joyner static enum i40e_status_code i40e_nvmupd_state_writing(struct i40e_hw *hw,
1243223d846dSEric Joyner 						    struct i40e_nvm_access *cmd,
1244223d846dSEric Joyner 						    u8 *bytes, int *perrno)
1245223d846dSEric Joyner {
1246223d846dSEric Joyner 	enum i40e_status_code status = I40E_SUCCESS;
1247223d846dSEric Joyner 	enum i40e_nvmupd_cmd upd_cmd;
1248223d846dSEric Joyner 	bool retry_attempt = FALSE;
1249223d846dSEric Joyner 
1250223d846dSEric Joyner 	DEBUGFUNC("i40e_nvmupd_state_writing");
1251223d846dSEric Joyner 
1252223d846dSEric Joyner 	upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
1253223d846dSEric Joyner 
1254223d846dSEric Joyner retry:
1255223d846dSEric Joyner 	switch (upd_cmd) {
1256223d846dSEric Joyner 	case I40E_NVMUPD_WRITE_CON:
1257223d846dSEric Joyner 		status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
12584294f337SSean Bruno 		if (!status) {
12594294f337SSean Bruno 			hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
1260223d846dSEric Joyner 			hw->nvmupd_state = I40E_NVMUPD_STATE_WRITE_WAIT;
12614294f337SSean Bruno 		}
1262223d846dSEric Joyner 		break;
1263223d846dSEric Joyner 
1264223d846dSEric Joyner 	case I40E_NVMUPD_WRITE_LCB:
1265223d846dSEric Joyner 		status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
1266223d846dSEric Joyner 		if (status) {
1267223d846dSEric Joyner 			*perrno = hw->aq.asq_last_status ?
1268223d846dSEric Joyner 				   i40e_aq_rc_to_posix(status,
1269223d846dSEric Joyner 						       hw->aq.asq_last_status) :
1270223d846dSEric Joyner 				   -EIO;
1271223d846dSEric Joyner 			hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
1272223d846dSEric Joyner 		} else {
12734294f337SSean Bruno 			hw->nvm_release_on_done = TRUE;
12744294f337SSean Bruno 			hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
1275223d846dSEric Joyner 			hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
1276223d846dSEric Joyner 		}
1277223d846dSEric Joyner 		break;
1278223d846dSEric Joyner 
1279223d846dSEric Joyner 	case I40E_NVMUPD_CSUM_CON:
1280fdb6f38aSEric Joyner 		/* Assumes the caller has acquired the nvm */
1281223d846dSEric Joyner 		status = i40e_update_nvm_checksum(hw);
1282223d846dSEric Joyner 		if (status) {
1283223d846dSEric Joyner 			*perrno = hw->aq.asq_last_status ?
1284223d846dSEric Joyner 				   i40e_aq_rc_to_posix(status,
1285223d846dSEric Joyner 						       hw->aq.asq_last_status) :
1286223d846dSEric Joyner 				   -EIO;
1287223d846dSEric Joyner 			hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
1288223d846dSEric Joyner 		} else {
12894294f337SSean Bruno 			hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
1290223d846dSEric Joyner 			hw->nvmupd_state = I40E_NVMUPD_STATE_WRITE_WAIT;
1291223d846dSEric Joyner 		}
1292223d846dSEric Joyner 		break;
1293223d846dSEric Joyner 
1294223d846dSEric Joyner 	case I40E_NVMUPD_CSUM_LCB:
1295fdb6f38aSEric Joyner 		/* Assumes the caller has acquired the nvm */
1296223d846dSEric Joyner 		status = i40e_update_nvm_checksum(hw);
1297223d846dSEric Joyner 		if (status) {
1298223d846dSEric Joyner 			*perrno = hw->aq.asq_last_status ?
1299223d846dSEric Joyner 				   i40e_aq_rc_to_posix(status,
1300223d846dSEric Joyner 						       hw->aq.asq_last_status) :
1301223d846dSEric Joyner 				   -EIO;
1302223d846dSEric Joyner 			hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
1303223d846dSEric Joyner 		} else {
13044294f337SSean Bruno 			hw->nvm_release_on_done = TRUE;
13054294f337SSean Bruno 			hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
1306223d846dSEric Joyner 			hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
1307223d846dSEric Joyner 		}
1308223d846dSEric Joyner 		break;
1309223d846dSEric Joyner 
1310223d846dSEric Joyner 	default:
1311223d846dSEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM,
1312223d846dSEric Joyner 			   "NVMUPD: bad cmd %s in writing state.\n",
1313223d846dSEric Joyner 			   i40e_nvm_update_state_str[upd_cmd]);
1314223d846dSEric Joyner 		status = I40E_NOT_SUPPORTED;
1315223d846dSEric Joyner 		*perrno = -ESRCH;
1316223d846dSEric Joyner 		break;
1317223d846dSEric Joyner 	}
1318223d846dSEric Joyner 
1319223d846dSEric Joyner 	/* In some circumstances, a multi-write transaction takes longer
1320223d846dSEric Joyner 	 * than the default 3 minute timeout on the write semaphore.  If
1321223d846dSEric Joyner 	 * the write failed with an EBUSY status, this is likely the problem,
1322223d846dSEric Joyner 	 * so here we try to reacquire the semaphore then retry the write.
1323223d846dSEric Joyner 	 * We only do one retry, then give up.
1324223d846dSEric Joyner 	 */
1325223d846dSEric Joyner 	if (status && (hw->aq.asq_last_status == I40E_AQ_RC_EBUSY) &&
1326223d846dSEric Joyner 	    !retry_attempt) {
1327223d846dSEric Joyner 		enum i40e_status_code old_status = status;
1328223d846dSEric Joyner 		u32 old_asq_status = hw->aq.asq_last_status;
1329223d846dSEric Joyner 		u32 gtime;
1330223d846dSEric Joyner 
1331223d846dSEric Joyner 		gtime = rd32(hw, I40E_GLVFGEN_TIMER);
1332223d846dSEric Joyner 		if (gtime >= hw->nvm.hw_semaphore_timeout) {
1333223d846dSEric Joyner 			i40e_debug(hw, I40E_DEBUG_ALL,
1334223d846dSEric Joyner 				   "NVMUPD: write semaphore expired (%d >= %lld), retrying\n",
1335223d846dSEric Joyner 				   gtime, hw->nvm.hw_semaphore_timeout);
1336223d846dSEric Joyner 			i40e_release_nvm(hw);
1337223d846dSEric Joyner 			status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
1338223d846dSEric Joyner 			if (status) {
1339223d846dSEric Joyner 				i40e_debug(hw, I40E_DEBUG_ALL,
1340223d846dSEric Joyner 					   "NVMUPD: write semaphore reacquire failed aq_err = %d\n",
1341223d846dSEric Joyner 					   hw->aq.asq_last_status);
1342223d846dSEric Joyner 				status = old_status;
1343223d846dSEric Joyner 				hw->aq.asq_last_status = old_asq_status;
1344223d846dSEric Joyner 			} else {
1345223d846dSEric Joyner 				retry_attempt = TRUE;
1346223d846dSEric Joyner 				goto retry;
1347223d846dSEric Joyner 			}
1348223d846dSEric Joyner 		}
1349223d846dSEric Joyner 	}
1350223d846dSEric Joyner 
1351223d846dSEric Joyner 	return status;
1352223d846dSEric Joyner }
1353223d846dSEric Joyner 
1354223d846dSEric Joyner /**
1355ceebc2f3SEric Joyner  * i40e_nvmupd_clear_wait_state - clear wait state on hw
13564294f337SSean Bruno  * @hw: pointer to the hardware structure
13574294f337SSean Bruno  **/
i40e_nvmupd_clear_wait_state(struct i40e_hw * hw)1358ceebc2f3SEric Joyner void i40e_nvmupd_clear_wait_state(struct i40e_hw *hw)
13594294f337SSean Bruno {
13604294f337SSean Bruno 	i40e_debug(hw, I40E_DEBUG_NVM,
1361ceebc2f3SEric Joyner 		   "NVMUPD: clearing wait on opcode 0x%04x\n",
1362ceebc2f3SEric Joyner 		   hw->nvm_wait_opcode);
1363ceebc2f3SEric Joyner 
13644294f337SSean Bruno 	if (hw->nvm_release_on_done) {
13654294f337SSean Bruno 		i40e_release_nvm(hw);
13664294f337SSean Bruno 		hw->nvm_release_on_done = FALSE;
13674294f337SSean Bruno 	}
13684294f337SSean Bruno 	hw->nvm_wait_opcode = 0;
13694294f337SSean Bruno 
1370cb6b8299SEric Joyner 	if (hw->aq.arq_last_status) {
1371cb6b8299SEric Joyner 		hw->nvmupd_state = I40E_NVMUPD_STATE_ERROR;
1372cb6b8299SEric Joyner 		return;
1373cb6b8299SEric Joyner 	}
1374cb6b8299SEric Joyner 
13754294f337SSean Bruno 	switch (hw->nvmupd_state) {
13764294f337SSean Bruno 	case I40E_NVMUPD_STATE_INIT_WAIT:
13774294f337SSean Bruno 		hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
13784294f337SSean Bruno 		break;
13794294f337SSean Bruno 
13804294f337SSean Bruno 	case I40E_NVMUPD_STATE_WRITE_WAIT:
13814294f337SSean Bruno 		hw->nvmupd_state = I40E_NVMUPD_STATE_WRITING;
13824294f337SSean Bruno 		break;
13834294f337SSean Bruno 
13844294f337SSean Bruno 	default:
13854294f337SSean Bruno 		break;
13864294f337SSean Bruno 	}
13874294f337SSean Bruno }
1388ceebc2f3SEric Joyner 
1389ceebc2f3SEric Joyner /**
1390ceebc2f3SEric Joyner  * i40e_nvmupd_check_wait_event - handle NVM update operation events
1391ceebc2f3SEric Joyner  * @hw: pointer to the hardware structure
1392ceebc2f3SEric Joyner  * @opcode: the event that just happened
1393ceebc2f3SEric Joyner  * @desc: AdminQ descriptor
1394ceebc2f3SEric Joyner  **/
i40e_nvmupd_check_wait_event(struct i40e_hw * hw,u16 opcode,struct i40e_aq_desc * desc)1395ceebc2f3SEric Joyner void i40e_nvmupd_check_wait_event(struct i40e_hw *hw, u16 opcode,
1396ceebc2f3SEric Joyner 				  struct i40e_aq_desc *desc)
1397ceebc2f3SEric Joyner {
1398ceebc2f3SEric Joyner 	u32 aq_desc_len = sizeof(struct i40e_aq_desc);
1399ceebc2f3SEric Joyner 
1400ceebc2f3SEric Joyner 	if (opcode == hw->nvm_wait_opcode) {
1401ceebc2f3SEric Joyner 		i40e_memcpy(&hw->nvm_aq_event_desc, desc,
1402ceebc2f3SEric Joyner 			    aq_desc_len, I40E_NONDMA_TO_NONDMA);
1403ceebc2f3SEric Joyner 		i40e_nvmupd_clear_wait_state(hw);
1404ceebc2f3SEric Joyner 	}
14054294f337SSean Bruno }
14064294f337SSean Bruno 
14074294f337SSean Bruno /**
1408223d846dSEric Joyner  * i40e_nvmupd_validate_command - Validate given command
1409223d846dSEric Joyner  * @hw: pointer to hardware structure
1410223d846dSEric Joyner  * @cmd: pointer to nvm update command buffer
1411223d846dSEric Joyner  * @perrno: pointer to return error code
1412223d846dSEric Joyner  *
1413223d846dSEric Joyner  * Return one of the valid command types or I40E_NVMUPD_INVALID
1414223d846dSEric Joyner  **/
i40e_nvmupd_validate_command(struct i40e_hw * hw,struct i40e_nvm_access * cmd,int * perrno)1415223d846dSEric Joyner static enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
1416223d846dSEric Joyner 						    struct i40e_nvm_access *cmd,
1417223d846dSEric Joyner 						    int *perrno)
1418223d846dSEric Joyner {
1419223d846dSEric Joyner 	enum i40e_nvmupd_cmd upd_cmd;
1420223d846dSEric Joyner 	u8 module, transaction;
1421223d846dSEric Joyner 
1422223d846dSEric Joyner 	DEBUGFUNC("i40e_nvmupd_validate_command\n");
1423223d846dSEric Joyner 
1424223d846dSEric Joyner 	/* anything that doesn't match a recognized case is an error */
1425223d846dSEric Joyner 	upd_cmd = I40E_NVMUPD_INVALID;
1426223d846dSEric Joyner 
1427223d846dSEric Joyner 	transaction = i40e_nvmupd_get_transaction(cmd->config);
1428223d846dSEric Joyner 	module = i40e_nvmupd_get_module(cmd->config);
1429223d846dSEric Joyner 
1430223d846dSEric Joyner 	/* limits on data size */
1431223d846dSEric Joyner 	if ((cmd->data_size < 1) ||
1432223d846dSEric Joyner 	    (cmd->data_size > I40E_NVMUPD_MAX_DATA)) {
1433223d846dSEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM,
1434223d846dSEric Joyner 			   "i40e_nvmupd_validate_command data_size %d\n",
1435223d846dSEric Joyner 			   cmd->data_size);
1436223d846dSEric Joyner 		*perrno = -EFAULT;
1437223d846dSEric Joyner 		return I40E_NVMUPD_INVALID;
1438223d846dSEric Joyner 	}
1439223d846dSEric Joyner 
1440223d846dSEric Joyner 	switch (cmd->command) {
1441223d846dSEric Joyner 	case I40E_NVM_READ:
1442223d846dSEric Joyner 		switch (transaction) {
1443223d846dSEric Joyner 		case I40E_NVM_CON:
1444223d846dSEric Joyner 			upd_cmd = I40E_NVMUPD_READ_CON;
1445223d846dSEric Joyner 			break;
1446223d846dSEric Joyner 		case I40E_NVM_SNT:
1447223d846dSEric Joyner 			upd_cmd = I40E_NVMUPD_READ_SNT;
1448223d846dSEric Joyner 			break;
1449223d846dSEric Joyner 		case I40E_NVM_LCB:
1450223d846dSEric Joyner 			upd_cmd = I40E_NVMUPD_READ_LCB;
1451223d846dSEric Joyner 			break;
1452223d846dSEric Joyner 		case I40E_NVM_SA:
1453223d846dSEric Joyner 			upd_cmd = I40E_NVMUPD_READ_SA;
1454223d846dSEric Joyner 			break;
1455223d846dSEric Joyner 		case I40E_NVM_EXEC:
1456b4a7ce06SEric Joyner 			switch (module) {
1457b4a7ce06SEric Joyner 			case I40E_NVM_EXEC_GET_AQ_RESULT:
1458223d846dSEric Joyner 				upd_cmd = I40E_NVMUPD_GET_AQ_RESULT;
1459223d846dSEric Joyner 				break;
1460b4a7ce06SEric Joyner 			case I40E_NVM_EXEC_FEATURES:
1461b4a7ce06SEric Joyner 				upd_cmd = I40E_NVMUPD_FEATURES;
1462b4a7ce06SEric Joyner 				break;
1463b4a7ce06SEric Joyner 			case I40E_NVM_EXEC_STATUS:
1464b4a7ce06SEric Joyner 				upd_cmd = I40E_NVMUPD_STATUS;
1465b4a7ce06SEric Joyner 				break;
1466b4a7ce06SEric Joyner 			default:
1467b4a7ce06SEric Joyner 				*perrno = -EFAULT;
1468b4a7ce06SEric Joyner 				return I40E_NVMUPD_INVALID;
1469b4a7ce06SEric Joyner 			}
1470b4a7ce06SEric Joyner 			break;
1471ceebc2f3SEric Joyner 		case I40E_NVM_AQE:
1472ceebc2f3SEric Joyner 			upd_cmd = I40E_NVMUPD_GET_AQ_EVENT;
1473ceebc2f3SEric Joyner 			break;
1474223d846dSEric Joyner 		}
1475223d846dSEric Joyner 		break;
1476223d846dSEric Joyner 
1477223d846dSEric Joyner 	case I40E_NVM_WRITE:
1478223d846dSEric Joyner 		switch (transaction) {
1479223d846dSEric Joyner 		case I40E_NVM_CON:
1480223d846dSEric Joyner 			upd_cmd = I40E_NVMUPD_WRITE_CON;
1481223d846dSEric Joyner 			break;
1482223d846dSEric Joyner 		case I40E_NVM_SNT:
1483223d846dSEric Joyner 			upd_cmd = I40E_NVMUPD_WRITE_SNT;
1484223d846dSEric Joyner 			break;
1485223d846dSEric Joyner 		case I40E_NVM_LCB:
1486223d846dSEric Joyner 			upd_cmd = I40E_NVMUPD_WRITE_LCB;
1487223d846dSEric Joyner 			break;
1488223d846dSEric Joyner 		case I40E_NVM_SA:
1489223d846dSEric Joyner 			upd_cmd = I40E_NVMUPD_WRITE_SA;
1490223d846dSEric Joyner 			break;
1491223d846dSEric Joyner 		case I40E_NVM_ERA:
1492223d846dSEric Joyner 			upd_cmd = I40E_NVMUPD_WRITE_ERA;
1493223d846dSEric Joyner 			break;
1494223d846dSEric Joyner 		case I40E_NVM_CSUM:
1495223d846dSEric Joyner 			upd_cmd = I40E_NVMUPD_CSUM_CON;
1496223d846dSEric Joyner 			break;
1497223d846dSEric Joyner 		case (I40E_NVM_CSUM|I40E_NVM_SA):
1498223d846dSEric Joyner 			upd_cmd = I40E_NVMUPD_CSUM_SA;
1499223d846dSEric Joyner 			break;
1500223d846dSEric Joyner 		case (I40E_NVM_CSUM|I40E_NVM_LCB):
1501223d846dSEric Joyner 			upd_cmd = I40E_NVMUPD_CSUM_LCB;
1502223d846dSEric Joyner 			break;
1503223d846dSEric Joyner 		case I40E_NVM_EXEC:
1504223d846dSEric Joyner 			if (module == 0)
1505223d846dSEric Joyner 				upd_cmd = I40E_NVMUPD_EXEC_AQ;
1506223d846dSEric Joyner 			break;
1507223d846dSEric Joyner 		}
1508223d846dSEric Joyner 		break;
1509223d846dSEric Joyner 	}
1510223d846dSEric Joyner 
1511223d846dSEric Joyner 	return upd_cmd;
1512223d846dSEric Joyner }
1513223d846dSEric Joyner 
1514223d846dSEric Joyner /**
1515223d846dSEric Joyner  * i40e_nvmupd_exec_aq - Run an AQ command
1516223d846dSEric Joyner  * @hw: pointer to hardware structure
1517223d846dSEric Joyner  * @cmd: pointer to nvm update command buffer
1518223d846dSEric Joyner  * @bytes: pointer to the data buffer
1519223d846dSEric Joyner  * @perrno: pointer to return error code
1520223d846dSEric Joyner  *
1521223d846dSEric Joyner  * cmd structure contains identifiers and data buffer
1522223d846dSEric Joyner  **/
i40e_nvmupd_exec_aq(struct i40e_hw * hw,struct i40e_nvm_access * cmd,u8 * bytes,int * perrno)1523223d846dSEric Joyner static enum i40e_status_code i40e_nvmupd_exec_aq(struct i40e_hw *hw,
1524223d846dSEric Joyner 						 struct i40e_nvm_access *cmd,
1525223d846dSEric Joyner 						 u8 *bytes, int *perrno)
1526223d846dSEric Joyner {
1527223d846dSEric Joyner 	struct i40e_asq_cmd_details cmd_details;
1528223d846dSEric Joyner 	enum i40e_status_code status;
1529223d846dSEric Joyner 	struct i40e_aq_desc *aq_desc;
1530223d846dSEric Joyner 	u32 buff_size = 0;
1531223d846dSEric Joyner 	u8 *buff = NULL;
1532223d846dSEric Joyner 	u32 aq_desc_len;
1533223d846dSEric Joyner 	u32 aq_data_len;
1534223d846dSEric Joyner 
1535223d846dSEric Joyner 	i40e_debug(hw, I40E_DEBUG_NVM, "NVMUPD: %s\n", __func__);
1536ceebc2f3SEric Joyner 	if (cmd->offset == 0xffff)
1537ceebc2f3SEric Joyner 		return I40E_SUCCESS;
1538ceebc2f3SEric Joyner 
1539223d846dSEric Joyner 	memset(&cmd_details, 0, sizeof(cmd_details));
1540223d846dSEric Joyner 	cmd_details.wb_desc = &hw->nvm_wb_desc;
1541223d846dSEric Joyner 
1542223d846dSEric Joyner 	aq_desc_len = sizeof(struct i40e_aq_desc);
1543223d846dSEric Joyner 	memset(&hw->nvm_wb_desc, 0, aq_desc_len);
1544223d846dSEric Joyner 
1545223d846dSEric Joyner 	/* get the aq descriptor */
1546223d846dSEric Joyner 	if (cmd->data_size < aq_desc_len) {
1547223d846dSEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM,
1548223d846dSEric Joyner 			   "NVMUPD: not enough aq desc bytes for exec, size %d < %d\n",
1549223d846dSEric Joyner 			   cmd->data_size, aq_desc_len);
1550223d846dSEric Joyner 		*perrno = -EINVAL;
1551223d846dSEric Joyner 		return I40E_ERR_PARAM;
1552223d846dSEric Joyner 	}
1553223d846dSEric Joyner 	aq_desc = (struct i40e_aq_desc *)bytes;
1554223d846dSEric Joyner 
1555223d846dSEric Joyner 	/* if data buffer needed, make sure it's ready */
1556223d846dSEric Joyner 	aq_data_len = cmd->data_size - aq_desc_len;
1557223d846dSEric Joyner 	buff_size = max(aq_data_len, (u32)LE16_TO_CPU(aq_desc->datalen));
1558223d846dSEric Joyner 	if (buff_size) {
1559223d846dSEric Joyner 		if (!hw->nvm_buff.va) {
1560223d846dSEric Joyner 			status = i40e_allocate_virt_mem(hw, &hw->nvm_buff,
1561223d846dSEric Joyner 							hw->aq.asq_buf_size);
1562223d846dSEric Joyner 			if (status)
1563223d846dSEric Joyner 				i40e_debug(hw, I40E_DEBUG_NVM,
1564223d846dSEric Joyner 					   "NVMUPD: i40e_allocate_virt_mem for exec buff failed, %d\n",
1565223d846dSEric Joyner 					   status);
1566223d846dSEric Joyner 		}
1567223d846dSEric Joyner 
1568223d846dSEric Joyner 		if (hw->nvm_buff.va) {
1569223d846dSEric Joyner 			buff = hw->nvm_buff.va;
1570cb6b8299SEric Joyner 			i40e_memcpy(buff, &bytes[aq_desc_len], aq_data_len,
1571cb6b8299SEric Joyner 				I40E_NONDMA_TO_NONDMA);
1572223d846dSEric Joyner 		}
1573223d846dSEric Joyner 	}
1574223d846dSEric Joyner 
1575ceebc2f3SEric Joyner 	if (cmd->offset)
1576ceebc2f3SEric Joyner 		memset(&hw->nvm_aq_event_desc, 0, aq_desc_len);
1577ceebc2f3SEric Joyner 
1578223d846dSEric Joyner 	/* and away we go! */
1579223d846dSEric Joyner 	status = i40e_asq_send_command(hw, aq_desc, buff,
1580223d846dSEric Joyner 				       buff_size, &cmd_details);
1581223d846dSEric Joyner 	if (status) {
1582223d846dSEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM,
1583223d846dSEric Joyner 			   "i40e_nvmupd_exec_aq err %s aq_err %s\n",
1584223d846dSEric Joyner 			   i40e_stat_str(hw, status),
1585223d846dSEric Joyner 			   i40e_aq_str(hw, hw->aq.asq_last_status));
1586223d846dSEric Joyner 		*perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
1587ceebc2f3SEric Joyner 		return status;
1588223d846dSEric Joyner 	}
1589223d846dSEric Joyner 
15904294f337SSean Bruno 	/* should we wait for a followup event? */
15914294f337SSean Bruno 	if (cmd->offset) {
15924294f337SSean Bruno 		hw->nvm_wait_opcode = cmd->offset;
15934294f337SSean Bruno 		hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
15944294f337SSean Bruno 	}
15954294f337SSean Bruno 
1596223d846dSEric Joyner 	return status;
1597223d846dSEric Joyner }
1598223d846dSEric Joyner 
1599223d846dSEric Joyner /**
1600223d846dSEric Joyner  * i40e_nvmupd_get_aq_result - Get the results from the previous exec_aq
1601223d846dSEric Joyner  * @hw: pointer to hardware structure
1602223d846dSEric Joyner  * @cmd: pointer to nvm update command buffer
1603223d846dSEric Joyner  * @bytes: pointer to the data buffer
1604223d846dSEric Joyner  * @perrno: pointer to return error code
1605223d846dSEric Joyner  *
1606223d846dSEric Joyner  * cmd structure contains identifiers and data buffer
1607223d846dSEric Joyner  **/
i40e_nvmupd_get_aq_result(struct i40e_hw * hw,struct i40e_nvm_access * cmd,u8 * bytes,int * perrno)1608223d846dSEric Joyner static enum i40e_status_code i40e_nvmupd_get_aq_result(struct i40e_hw *hw,
1609223d846dSEric Joyner 						    struct i40e_nvm_access *cmd,
1610223d846dSEric Joyner 						    u8 *bytes, int *perrno)
1611223d846dSEric Joyner {
1612223d846dSEric Joyner 	u32 aq_total_len;
1613223d846dSEric Joyner 	u32 aq_desc_len;
1614223d846dSEric Joyner 	int remainder;
1615223d846dSEric Joyner 	u8 *buff;
1616223d846dSEric Joyner 
1617223d846dSEric Joyner 	i40e_debug(hw, I40E_DEBUG_NVM, "NVMUPD: %s\n", __func__);
1618223d846dSEric Joyner 
1619223d846dSEric Joyner 	aq_desc_len = sizeof(struct i40e_aq_desc);
1620223d846dSEric Joyner 	aq_total_len = aq_desc_len + LE16_TO_CPU(hw->nvm_wb_desc.datalen);
1621223d846dSEric Joyner 
1622223d846dSEric Joyner 	/* check offset range */
1623223d846dSEric Joyner 	if (cmd->offset > aq_total_len) {
1624223d846dSEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM, "%s: offset too big %d > %d\n",
1625223d846dSEric Joyner 			   __func__, cmd->offset, aq_total_len);
1626223d846dSEric Joyner 		*perrno = -EINVAL;
1627223d846dSEric Joyner 		return I40E_ERR_PARAM;
1628223d846dSEric Joyner 	}
1629223d846dSEric Joyner 
1630223d846dSEric Joyner 	/* check copylength range */
1631223d846dSEric Joyner 	if (cmd->data_size > (aq_total_len - cmd->offset)) {
1632223d846dSEric Joyner 		int new_len = aq_total_len - cmd->offset;
1633223d846dSEric Joyner 
1634223d846dSEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM, "%s: copy length %d too big, trimming to %d\n",
1635223d846dSEric Joyner 			   __func__, cmd->data_size, new_len);
1636223d846dSEric Joyner 		cmd->data_size = new_len;
1637223d846dSEric Joyner 	}
1638223d846dSEric Joyner 
1639223d846dSEric Joyner 	remainder = cmd->data_size;
1640223d846dSEric Joyner 	if (cmd->offset < aq_desc_len) {
1641223d846dSEric Joyner 		u32 len = aq_desc_len - cmd->offset;
1642223d846dSEric Joyner 
1643223d846dSEric Joyner 		len = min(len, cmd->data_size);
1644223d846dSEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM, "%s: aq_desc bytes %d to %d\n",
1645223d846dSEric Joyner 			   __func__, cmd->offset, cmd->offset + len);
1646223d846dSEric Joyner 
1647223d846dSEric Joyner 		buff = ((u8 *)&hw->nvm_wb_desc) + cmd->offset;
1648cb6b8299SEric Joyner 		i40e_memcpy(bytes, buff, len, I40E_NONDMA_TO_NONDMA);
1649223d846dSEric Joyner 
1650223d846dSEric Joyner 		bytes += len;
1651223d846dSEric Joyner 		remainder -= len;
1652223d846dSEric Joyner 		buff = hw->nvm_buff.va;
1653223d846dSEric Joyner 	} else {
1654223d846dSEric Joyner 		buff = (u8 *)hw->nvm_buff.va + (cmd->offset - aq_desc_len);
1655223d846dSEric Joyner 	}
1656223d846dSEric Joyner 
1657223d846dSEric Joyner 	if (remainder > 0) {
1658223d846dSEric Joyner 		int start_byte = buff - (u8 *)hw->nvm_buff.va;
1659223d846dSEric Joyner 
1660223d846dSEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM, "%s: databuf bytes %d to %d\n",
1661223d846dSEric Joyner 			   __func__, start_byte, start_byte + remainder);
1662cb6b8299SEric Joyner 		i40e_memcpy(bytes, buff, remainder, I40E_NONDMA_TO_NONDMA);
1663223d846dSEric Joyner 	}
1664223d846dSEric Joyner 
1665223d846dSEric Joyner 	return I40E_SUCCESS;
1666223d846dSEric Joyner }
1667223d846dSEric Joyner 
1668223d846dSEric Joyner /**
1669ceebc2f3SEric Joyner  * i40e_nvmupd_get_aq_event - Get the Admin Queue event from previous exec_aq
1670ceebc2f3SEric Joyner  * @hw: pointer to hardware structure
1671ceebc2f3SEric Joyner  * @cmd: pointer to nvm update command buffer
1672ceebc2f3SEric Joyner  * @bytes: pointer to the data buffer
1673ceebc2f3SEric Joyner  * @perrno: pointer to return error code
1674ceebc2f3SEric Joyner  *
1675ceebc2f3SEric Joyner  * cmd structure contains identifiers and data buffer
1676ceebc2f3SEric Joyner  **/
i40e_nvmupd_get_aq_event(struct i40e_hw * hw,struct i40e_nvm_access * cmd,u8 * bytes,int * perrno)1677ceebc2f3SEric Joyner static enum i40e_status_code i40e_nvmupd_get_aq_event(struct i40e_hw *hw,
1678ceebc2f3SEric Joyner 						    struct i40e_nvm_access *cmd,
1679ceebc2f3SEric Joyner 						    u8 *bytes, int *perrno)
1680ceebc2f3SEric Joyner {
1681ceebc2f3SEric Joyner 	u32 aq_total_len;
1682ceebc2f3SEric Joyner 	u32 aq_desc_len;
1683ceebc2f3SEric Joyner 
1684ceebc2f3SEric Joyner 	i40e_debug(hw, I40E_DEBUG_NVM, "NVMUPD: %s\n", __func__);
1685ceebc2f3SEric Joyner 
1686ceebc2f3SEric Joyner 	aq_desc_len = sizeof(struct i40e_aq_desc);
1687ceebc2f3SEric Joyner 	aq_total_len = aq_desc_len + LE16_TO_CPU(hw->nvm_aq_event_desc.datalen);
1688ceebc2f3SEric Joyner 
1689ceebc2f3SEric Joyner 	/* check copylength range */
1690ceebc2f3SEric Joyner 	if (cmd->data_size > aq_total_len) {
1691ceebc2f3SEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM,
1692ceebc2f3SEric Joyner 			   "%s: copy length %d too big, trimming to %d\n",
1693ceebc2f3SEric Joyner 			   __func__, cmd->data_size, aq_total_len);
1694ceebc2f3SEric Joyner 		cmd->data_size = aq_total_len;
1695ceebc2f3SEric Joyner 	}
1696ceebc2f3SEric Joyner 
1697ceebc2f3SEric Joyner 	i40e_memcpy(bytes, &hw->nvm_aq_event_desc, cmd->data_size,
1698ceebc2f3SEric Joyner 		    I40E_NONDMA_TO_NONDMA);
1699ceebc2f3SEric Joyner 
1700ceebc2f3SEric Joyner 	return I40E_SUCCESS;
1701ceebc2f3SEric Joyner }
1702ceebc2f3SEric Joyner 
1703ceebc2f3SEric Joyner /**
1704223d846dSEric Joyner  * i40e_nvmupd_nvm_read - Read NVM
1705223d846dSEric Joyner  * @hw: pointer to hardware structure
1706223d846dSEric Joyner  * @cmd: pointer to nvm update command buffer
1707223d846dSEric Joyner  * @bytes: pointer to the data buffer
1708223d846dSEric Joyner  * @perrno: pointer to return error code
1709223d846dSEric Joyner  *
1710223d846dSEric Joyner  * cmd structure contains identifiers and data buffer
1711223d846dSEric Joyner  **/
i40e_nvmupd_nvm_read(struct i40e_hw * hw,struct i40e_nvm_access * cmd,u8 * bytes,int * perrno)1712223d846dSEric Joyner static enum i40e_status_code i40e_nvmupd_nvm_read(struct i40e_hw *hw,
1713223d846dSEric Joyner 						  struct i40e_nvm_access *cmd,
1714223d846dSEric Joyner 						  u8 *bytes, int *perrno)
1715223d846dSEric Joyner {
1716223d846dSEric Joyner 	struct i40e_asq_cmd_details cmd_details;
1717223d846dSEric Joyner 	enum i40e_status_code status;
1718223d846dSEric Joyner 	u8 module, transaction;
1719223d846dSEric Joyner 	bool last;
1720223d846dSEric Joyner 
1721223d846dSEric Joyner 	transaction = i40e_nvmupd_get_transaction(cmd->config);
1722223d846dSEric Joyner 	module = i40e_nvmupd_get_module(cmd->config);
1723223d846dSEric Joyner 	last = (transaction == I40E_NVM_LCB) || (transaction == I40E_NVM_SA);
1724223d846dSEric Joyner 
1725223d846dSEric Joyner 	memset(&cmd_details, 0, sizeof(cmd_details));
1726223d846dSEric Joyner 	cmd_details.wb_desc = &hw->nvm_wb_desc;
1727223d846dSEric Joyner 
1728223d846dSEric Joyner 	status = i40e_aq_read_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
1729223d846dSEric Joyner 				  bytes, last, &cmd_details);
1730223d846dSEric Joyner 	if (status) {
1731223d846dSEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM,
1732223d846dSEric Joyner 			   "i40e_nvmupd_nvm_read mod 0x%x  off 0x%x  len 0x%x\n",
1733223d846dSEric Joyner 			   module, cmd->offset, cmd->data_size);
1734223d846dSEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM,
1735223d846dSEric Joyner 			   "i40e_nvmupd_nvm_read status %d aq %d\n",
1736223d846dSEric Joyner 			   status, hw->aq.asq_last_status);
1737223d846dSEric Joyner 		*perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
1738223d846dSEric Joyner 	}
1739223d846dSEric Joyner 
1740223d846dSEric Joyner 	return status;
1741223d846dSEric Joyner }
1742223d846dSEric Joyner 
1743223d846dSEric Joyner /**
1744223d846dSEric Joyner  * i40e_nvmupd_nvm_erase - Erase an NVM module
1745223d846dSEric Joyner  * @hw: pointer to hardware structure
1746223d846dSEric Joyner  * @cmd: pointer to nvm update command buffer
1747223d846dSEric Joyner  * @perrno: pointer to return error code
1748223d846dSEric Joyner  *
1749223d846dSEric Joyner  * module, offset, data_size and data are in cmd structure
1750223d846dSEric Joyner  **/
i40e_nvmupd_nvm_erase(struct i40e_hw * hw,struct i40e_nvm_access * cmd,int * perrno)1751223d846dSEric Joyner static enum i40e_status_code i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
1752223d846dSEric Joyner 						   struct i40e_nvm_access *cmd,
1753223d846dSEric Joyner 						   int *perrno)
1754223d846dSEric Joyner {
1755223d846dSEric Joyner 	enum i40e_status_code status = I40E_SUCCESS;
1756223d846dSEric Joyner 	struct i40e_asq_cmd_details cmd_details;
1757223d846dSEric Joyner 	u8 module, transaction;
1758223d846dSEric Joyner 	bool last;
1759223d846dSEric Joyner 
1760223d846dSEric Joyner 	transaction = i40e_nvmupd_get_transaction(cmd->config);
1761223d846dSEric Joyner 	module = i40e_nvmupd_get_module(cmd->config);
1762223d846dSEric Joyner 	last = (transaction & I40E_NVM_LCB);
1763223d846dSEric Joyner 
1764223d846dSEric Joyner 	memset(&cmd_details, 0, sizeof(cmd_details));
1765223d846dSEric Joyner 	cmd_details.wb_desc = &hw->nvm_wb_desc;
1766223d846dSEric Joyner 
1767223d846dSEric Joyner 	status = i40e_aq_erase_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
1768223d846dSEric Joyner 				   last, &cmd_details);
1769223d846dSEric Joyner 	if (status) {
1770223d846dSEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM,
1771223d846dSEric Joyner 			   "i40e_nvmupd_nvm_erase mod 0x%x  off 0x%x len 0x%x\n",
1772223d846dSEric Joyner 			   module, cmd->offset, cmd->data_size);
1773223d846dSEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM,
1774223d846dSEric Joyner 			   "i40e_nvmupd_nvm_erase status %d aq %d\n",
1775223d846dSEric Joyner 			   status, hw->aq.asq_last_status);
1776223d846dSEric Joyner 		*perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
1777223d846dSEric Joyner 	}
1778223d846dSEric Joyner 
1779223d846dSEric Joyner 	return status;
1780223d846dSEric Joyner }
1781223d846dSEric Joyner 
1782223d846dSEric Joyner /**
1783223d846dSEric Joyner  * i40e_nvmupd_nvm_write - Write NVM
1784223d846dSEric Joyner  * @hw: pointer to hardware structure
1785223d846dSEric Joyner  * @cmd: pointer to nvm update command buffer
1786223d846dSEric Joyner  * @bytes: pointer to the data buffer
1787223d846dSEric Joyner  * @perrno: pointer to return error code
1788223d846dSEric Joyner  *
1789223d846dSEric Joyner  * module, offset, data_size and data are in cmd structure
1790223d846dSEric Joyner  **/
i40e_nvmupd_nvm_write(struct i40e_hw * hw,struct i40e_nvm_access * cmd,u8 * bytes,int * perrno)1791223d846dSEric Joyner static enum i40e_status_code i40e_nvmupd_nvm_write(struct i40e_hw *hw,
1792223d846dSEric Joyner 						   struct i40e_nvm_access *cmd,
1793223d846dSEric Joyner 						   u8 *bytes, int *perrno)
1794223d846dSEric Joyner {
1795223d846dSEric Joyner 	enum i40e_status_code status = I40E_SUCCESS;
1796223d846dSEric Joyner 	struct i40e_asq_cmd_details cmd_details;
1797223d846dSEric Joyner 	u8 module, transaction;
1798ceebc2f3SEric Joyner 	u8 preservation_flags;
1799223d846dSEric Joyner 	bool last;
1800223d846dSEric Joyner 
1801223d846dSEric Joyner 	transaction = i40e_nvmupd_get_transaction(cmd->config);
1802223d846dSEric Joyner 	module = i40e_nvmupd_get_module(cmd->config);
1803223d846dSEric Joyner 	last = (transaction & I40E_NVM_LCB);
1804ceebc2f3SEric Joyner 	preservation_flags = i40e_nvmupd_get_preservation_flags(cmd->config);
1805223d846dSEric Joyner 
1806223d846dSEric Joyner 	memset(&cmd_details, 0, sizeof(cmd_details));
1807223d846dSEric Joyner 	cmd_details.wb_desc = &hw->nvm_wb_desc;
1808223d846dSEric Joyner 
1809223d846dSEric Joyner 	status = i40e_aq_update_nvm(hw, module, cmd->offset,
1810223d846dSEric Joyner 				    (u16)cmd->data_size, bytes, last,
1811ceebc2f3SEric Joyner 				    preservation_flags, &cmd_details);
1812223d846dSEric Joyner 	if (status) {
1813223d846dSEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM,
1814223d846dSEric Joyner 			   "i40e_nvmupd_nvm_write mod 0x%x off 0x%x len 0x%x\n",
1815223d846dSEric Joyner 			   module, cmd->offset, cmd->data_size);
1816223d846dSEric Joyner 		i40e_debug(hw, I40E_DEBUG_NVM,
1817223d846dSEric Joyner 			   "i40e_nvmupd_nvm_write status %d aq %d\n",
1818223d846dSEric Joyner 			   status, hw->aq.asq_last_status);
1819223d846dSEric Joyner 		*perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
1820223d846dSEric Joyner 	}
1821223d846dSEric Joyner 
1822223d846dSEric Joyner 	return status;
1823223d846dSEric Joyner }
1824