blob: ba9687c037950acd9dbe531f314f55c1480f4c8a [file] [log] [blame]
Jeff Kirsherae06c702018-03-22 10:08:48 -07001// SPDX-License-Identifier: GPL-2.0
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +00002/*******************************************************************************
3 *
4 * Intel Ethernet Controller XL710 Family Linux Driver
Greg Rosedc641b72013-12-18 13:45:51 +00005 * Copyright(c) 2013 - 2014 Intel Corporation.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +00006 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
Greg Rosedc641b72013-12-18 13:45:51 +000016 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000018 *
19 * The full GNU General Public License is included in this distribution in
20 * the file called "COPYING".
21 *
22 * Contact Information:
23 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25 *
26 ******************************************************************************/
27
28#include "i40e_prototype.h"
29
30/**
Shannon Nelson3e261862014-02-06 05:51:06 +000031 * i40e_init_nvm_ops - Initialize NVM function pointers
32 * @hw: pointer to the HW structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000033 *
Shannon Nelson3e261862014-02-06 05:51:06 +000034 * Setup the function pointers and the NVM info structure. Should be called
35 * once per NVM initialization, e.g. inside the i40e_init_shared_code().
36 * Please notice that the NVM term is used here (& in all methods covered
37 * in this file) as an equivalent of the FLASH part mapped into the SR.
38 * We are accessing FLASH always thru the Shadow RAM.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000039 **/
40i40e_status i40e_init_nvm(struct i40e_hw *hw)
41{
42 struct i40e_nvm_info *nvm = &hw->nvm;
43 i40e_status ret_code = 0;
44 u32 fla, gens;
45 u8 sr_size;
46
47 /* The SR size is stored regardless of the nvm programming mode
48 * as the blank mode may be used in the factory line.
49 */
50 gens = rd32(hw, I40E_GLNVM_GENS);
51 sr_size = ((gens & I40E_GLNVM_GENS_SR_SIZE_MASK) >>
52 I40E_GLNVM_GENS_SR_SIZE_SHIFT);
Shannon Nelson3e261862014-02-06 05:51:06 +000053 /* Switching to words (sr_size contains power of 2KB) */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -040054 nvm->sr_size = BIT(sr_size) * I40E_SR_WORDS_IN_1KB;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000055
Shannon Nelson3e261862014-02-06 05:51:06 +000056 /* Check if we are in the normal or blank NVM programming mode */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000057 fla = rd32(hw, I40E_GLNVM_FLA);
Shannon Nelson3e261862014-02-06 05:51:06 +000058 if (fla & I40E_GLNVM_FLA_LOCKED_MASK) { /* Normal programming mode */
59 /* Max NVM timeout */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000060 nvm->timeout = I40E_MAX_NVM_TIMEOUT;
61 nvm->blank_nvm_mode = false;
Shannon Nelson3e261862014-02-06 05:51:06 +000062 } else { /* Blank programming mode */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000063 nvm->blank_nvm_mode = true;
64 ret_code = I40E_ERR_NVM_BLANK_MODE;
Shannon Nelson74d0d0e2014-11-13 08:23:15 +000065 i40e_debug(hw, I40E_DEBUG_NVM, "NVM init error: unsupported blank mode.\n");
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000066 }
67
68 return ret_code;
69}
70
71/**
Shannon Nelson3e261862014-02-06 05:51:06 +000072 * i40e_acquire_nvm - Generic request for acquiring the NVM ownership
73 * @hw: pointer to the HW structure
74 * @access: NVM access type (read or write)
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000075 *
Shannon Nelson3e261862014-02-06 05:51:06 +000076 * This function will request NVM ownership for reading
77 * via the proper Admin Command.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000078 **/
79i40e_status i40e_acquire_nvm(struct i40e_hw *hw,
80 enum i40e_aq_resource_access_type access)
81{
82 i40e_status ret_code = 0;
83 u64 gtime, timeout;
Shannon Nelsonc509c1d2014-11-13 08:23:19 +000084 u64 time_left = 0;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000085
86 if (hw->nvm.blank_nvm_mode)
87 goto i40e_i40e_acquire_nvm_exit;
88
89 ret_code = i40e_aq_request_resource(hw, I40E_NVM_RESOURCE_ID, access,
Shannon Nelsonc509c1d2014-11-13 08:23:19 +000090 0, &time_left, NULL);
Shannon Nelson3e261862014-02-06 05:51:06 +000091 /* Reading the Global Device Timer */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000092 gtime = rd32(hw, I40E_GLVFGEN_TIMER);
93
Shannon Nelson3e261862014-02-06 05:51:06 +000094 /* Store the timeout */
Shannon Nelsonc509c1d2014-11-13 08:23:19 +000095 hw->nvm.hw_semaphore_timeout = I40E_MS_TO_GTIME(time_left) + gtime;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000096
Shannon Nelsona3f0b382014-11-13 08:23:21 +000097 if (ret_code)
98 i40e_debug(hw, I40E_DEBUG_NVM,
99 "NVM acquire type %d failed time_left=%llu ret=%d aq_err=%d\n",
100 access, time_left, ret_code, hw->aq.asq_last_status);
101
102 if (ret_code && time_left) {
Shannon Nelson3e261862014-02-06 05:51:06 +0000103 /* Poll until the current NVM owner timeouts */
Shannon Nelsonc509c1d2014-11-13 08:23:19 +0000104 timeout = I40E_MS_TO_GTIME(I40E_MAX_NVM_TIMEOUT) + gtime;
Shannon Nelsona3f0b382014-11-13 08:23:21 +0000105 while ((gtime < timeout) && time_left) {
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000106 usleep_range(10000, 20000);
Shannon Nelsonc509c1d2014-11-13 08:23:19 +0000107 gtime = rd32(hw, I40E_GLVFGEN_TIMER);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000108 ret_code = i40e_aq_request_resource(hw,
109 I40E_NVM_RESOURCE_ID,
Shannon Nelsonc509c1d2014-11-13 08:23:19 +0000110 access, 0, &time_left,
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000111 NULL);
112 if (!ret_code) {
113 hw->nvm.hw_semaphore_timeout =
Shannon Nelsonc509c1d2014-11-13 08:23:19 +0000114 I40E_MS_TO_GTIME(time_left) + gtime;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000115 break;
116 }
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000117 }
118 if (ret_code) {
119 hw->nvm.hw_semaphore_timeout = 0;
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000120 i40e_debug(hw, I40E_DEBUG_NVM,
Shannon Nelsona3f0b382014-11-13 08:23:21 +0000121 "NVM acquire timed out, wait %llu ms before trying again. status=%d aq_err=%d\n",
122 time_left, ret_code, hw->aq.asq_last_status);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000123 }
124 }
125
126i40e_i40e_acquire_nvm_exit:
127 return ret_code;
128}
129
130/**
Shannon Nelson3e261862014-02-06 05:51:06 +0000131 * i40e_release_nvm - Generic request for releasing the NVM ownership
132 * @hw: pointer to the HW structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000133 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000134 * This function will release NVM resource via the proper Admin Command.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000135 **/
136void i40e_release_nvm(struct i40e_hw *hw)
137{
Paul M Stillwell Jr981e25c2017-06-20 15:16:55 -0700138 i40e_status ret_code = I40E_SUCCESS;
139 u32 total_delay = 0;
140
141 if (hw->nvm.blank_nvm_mode)
142 return;
143
144 ret_code = i40e_aq_release_resource(hw, I40E_NVM_RESOURCE_ID, 0, NULL);
145
146 /* there are some rare cases when trying to release the resource
147 * results in an admin Q timeout, so handle them correctly
148 */
149 while ((ret_code == I40E_ERR_ADMIN_QUEUE_TIMEOUT) &&
150 (total_delay < hw->aq.asq_cmd_timeout)) {
151 usleep_range(1000, 2000);
152 ret_code = i40e_aq_release_resource(hw,
153 I40E_NVM_RESOURCE_ID,
154 0, NULL);
155 total_delay++;
156 }
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000157}
158
159/**
Shannon Nelson3e261862014-02-06 05:51:06 +0000160 * i40e_poll_sr_srctl_done_bit - Polls the GLNVM_SRCTL done bit
161 * @hw: pointer to the HW structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000162 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000163 * Polls the SRCTL Shadow RAM register done bit.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000164 **/
165static i40e_status i40e_poll_sr_srctl_done_bit(struct i40e_hw *hw)
166{
167 i40e_status ret_code = I40E_ERR_TIMEOUT;
168 u32 srctl, wait_cnt;
169
Shannon Nelson3e261862014-02-06 05:51:06 +0000170 /* Poll the I40E_GLNVM_SRCTL until the done bit is set */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000171 for (wait_cnt = 0; wait_cnt < I40E_SRRD_SRCTL_ATTEMPTS; wait_cnt++) {
172 srctl = rd32(hw, I40E_GLNVM_SRCTL);
173 if (srctl & I40E_GLNVM_SRCTL_DONE_MASK) {
174 ret_code = 0;
175 break;
176 }
177 udelay(5);
178 }
179 if (ret_code == I40E_ERR_TIMEOUT)
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000180 i40e_debug(hw, I40E_DEBUG_NVM, "Done bit in GLNVM_SRCTL not set");
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000181 return ret_code;
182}
183
184/**
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000185 * i40e_read_nvm_word_srctl - Reads Shadow RAM via SRCTL register
Shannon Nelson3e261862014-02-06 05:51:06 +0000186 * @hw: pointer to the HW structure
187 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
188 * @data: word read from the Shadow RAM
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000189 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000190 * Reads one 16 bit word from the Shadow RAM using the GLNVM_SRCTL register.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000191 **/
Shannon Nelson37a29732015-02-27 09:15:19 +0000192static i40e_status i40e_read_nvm_word_srctl(struct i40e_hw *hw, u16 offset,
193 u16 *data)
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000194{
195 i40e_status ret_code = I40E_ERR_TIMEOUT;
196 u32 sr_reg;
197
198 if (offset >= hw->nvm.sr_size) {
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000199 i40e_debug(hw, I40E_DEBUG_NVM,
200 "NVM read error: offset %d beyond Shadow RAM limit %d\n",
201 offset, hw->nvm.sr_size);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000202 ret_code = I40E_ERR_PARAM;
203 goto read_nvm_exit;
204 }
205
Shannon Nelson3e261862014-02-06 05:51:06 +0000206 /* Poll the done bit first */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000207 ret_code = i40e_poll_sr_srctl_done_bit(hw);
208 if (!ret_code) {
Shannon Nelson3e261862014-02-06 05:51:06 +0000209 /* Write the address and start reading */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400210 sr_reg = ((u32)offset << I40E_GLNVM_SRCTL_ADDR_SHIFT) |
211 BIT(I40E_GLNVM_SRCTL_START_SHIFT);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000212 wr32(hw, I40E_GLNVM_SRCTL, sr_reg);
213
Shannon Nelson3e261862014-02-06 05:51:06 +0000214 /* Poll I40E_GLNVM_SRCTL until the done bit is set */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000215 ret_code = i40e_poll_sr_srctl_done_bit(hw);
216 if (!ret_code) {
217 sr_reg = rd32(hw, I40E_GLNVM_SRDATA);
218 *data = (u16)((sr_reg &
219 I40E_GLNVM_SRDATA_RDDATA_MASK)
220 >> I40E_GLNVM_SRDATA_RDDATA_SHIFT);
221 }
222 }
223 if (ret_code)
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000224 i40e_debug(hw, I40E_DEBUG_NVM,
225 "NVM read error: Couldn't access Shadow RAM address: 0x%x\n",
226 offset);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000227
228read_nvm_exit:
229 return ret_code;
230}
231
232/**
Shannon Nelson7073f462015-06-05 12:20:34 -0400233 * i40e_read_nvm_aq - Read Shadow RAM.
234 * @hw: pointer to the HW structure.
235 * @module_pointer: module pointer location in words from the NVM beginning
236 * @offset: offset in words from module start
237 * @words: number of words to write
238 * @data: buffer with words to write to the Shadow RAM
239 * @last_command: tells the AdminQ that this is the last command
240 *
241 * Writes a 16 bit words buffer to the Shadow RAM using the admin command.
242 **/
Pawel Jablonskie3a5d6e2017-12-18 05:14:44 -0500243static i40e_status i40e_read_nvm_aq(struct i40e_hw *hw,
244 u8 module_pointer, u32 offset,
245 u16 words, void *data,
Shannon Nelson7073f462015-06-05 12:20:34 -0400246 bool last_command)
247{
248 i40e_status ret_code = I40E_ERR_NVM;
249 struct i40e_asq_cmd_details cmd_details;
250
251 memset(&cmd_details, 0, sizeof(cmd_details));
Jacob Keller3c8f3e92017-09-01 13:43:08 -0700252 cmd_details.wb_desc = &hw->nvm_wb_desc;
Shannon Nelson7073f462015-06-05 12:20:34 -0400253
254 /* Here we are checking the SR limit only for the flat memory model.
255 * We cannot do it for the module-based model, as we did not acquire
256 * the NVM resource yet (we cannot get the module pointer value).
257 * Firmware will check the module-based model.
258 */
259 if ((offset + words) > hw->nvm.sr_size)
260 i40e_debug(hw, I40E_DEBUG_NVM,
261 "NVM write error: offset %d beyond Shadow RAM limit %d\n",
262 (offset + words), hw->nvm.sr_size);
263 else if (words > I40E_SR_SECTOR_SIZE_IN_WORDS)
264 /* We can write only up to 4KB (one sector), in one AQ write */
265 i40e_debug(hw, I40E_DEBUG_NVM,
266 "NVM write fail error: tried to write %d words, limit is %d.\n",
267 words, I40E_SR_SECTOR_SIZE_IN_WORDS);
268 else if (((offset + (words - 1)) / I40E_SR_SECTOR_SIZE_IN_WORDS)
269 != (offset / I40E_SR_SECTOR_SIZE_IN_WORDS))
270 /* A single write cannot spread over two sectors */
271 i40e_debug(hw, I40E_DEBUG_NVM,
272 "NVM write error: cannot spread over two sectors in a single write offset=%d words=%d\n",
273 offset, words);
274 else
275 ret_code = i40e_aq_read_nvm(hw, module_pointer,
276 2 * offset, /*bytes*/
277 2 * words, /*bytes*/
278 data, last_command, &cmd_details);
279
280 return ret_code;
281}
282
283/**
284 * i40e_read_nvm_word_aq - Reads Shadow RAM via AQ
285 * @hw: pointer to the HW structure
286 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
287 * @data: word read from the Shadow RAM
288 *
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700289 * Reads one 16 bit word from the Shadow RAM using the AdminQ
Shannon Nelson7073f462015-06-05 12:20:34 -0400290 **/
291static i40e_status i40e_read_nvm_word_aq(struct i40e_hw *hw, u16 offset,
292 u16 *data)
293{
294 i40e_status ret_code = I40E_ERR_TIMEOUT;
295
296 ret_code = i40e_read_nvm_aq(hw, 0x0, offset, 1, data, true);
297 *data = le16_to_cpu(*(__le16 *)data);
298
299 return ret_code;
300}
301
302/**
Stefano Brivioe836e322017-09-06 10:11:38 +0200303 * __i40e_read_nvm_word - Reads nvm word, assumes caller does the locking
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000304 * @hw: pointer to the HW structure
305 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
306 * @data: word read from the Shadow RAM
307 *
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700308 * Reads one 16 bit word from the Shadow RAM.
309 *
310 * Do not use this function except in cases where the nvm lock is already
311 * taken via i40e_acquire_nvm().
312 **/
313static i40e_status __i40e_read_nvm_word(struct i40e_hw *hw,
314 u16 offset, u16 *data)
315{
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700316 if (hw->flags & I40E_HW_FLAG_AQ_SRCTL_ACCESS_ENABLE)
Stefano Brivio2c4d36b2017-09-06 10:11:39 +0200317 return i40e_read_nvm_word_aq(hw, offset, data);
318
319 return i40e_read_nvm_word_srctl(hw, offset, data);
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700320}
321
322/**
323 * i40e_read_nvm_word - Reads nvm word and acquire lock if necessary
324 * @hw: pointer to the HW structure
325 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
326 * @data: word read from the Shadow RAM
327 *
328 * Reads one 16 bit word from the Shadow RAM.
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000329 **/
330i40e_status i40e_read_nvm_word(struct i40e_hw *hw, u16 offset,
331 u16 *data)
332{
Jacob Keller3d72aeb2017-10-27 11:06:55 -0400333 i40e_status ret_code = 0;
Anjali Singhai07f89be2015-09-24 15:26:32 -0700334
Jacob Keller3d72aeb2017-10-27 11:06:55 -0400335 if (hw->flags & I40E_HW_FLAG_NVM_READ_REQUIRES_LOCK)
336 ret_code = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700337 if (ret_code)
338 return ret_code;
339
340 ret_code = __i40e_read_nvm_word(hw, offset, data);
341
Jacob Keller3d72aeb2017-10-27 11:06:55 -0400342 if (hw->flags & I40E_HW_FLAG_NVM_READ_REQUIRES_LOCK)
343 i40e_release_nvm(hw);
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700344
Anjali Singhai07f89be2015-09-24 15:26:32 -0700345 return ret_code;
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000346}
347
348/**
349 * i40e_read_nvm_buffer_srctl - Reads Shadow RAM buffer via SRCTL register
350 * @hw: pointer to the HW structure
351 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
352 * @words: (in) number of words to read; (out) number of words actually read
353 * @data: words read from the Shadow RAM
354 *
355 * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_srrd()
356 * method. The buffer read is preceded by the NVM ownership take
357 * and followed by the release.
358 **/
Shannon Nelson37a29732015-02-27 09:15:19 +0000359static i40e_status i40e_read_nvm_buffer_srctl(struct i40e_hw *hw, u16 offset,
360 u16 *words, u16 *data)
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000361{
362 i40e_status ret_code = 0;
363 u16 index, word;
364
365 /* Loop thru the selected region */
366 for (word = 0; word < *words; word++) {
367 index = offset + word;
368 ret_code = i40e_read_nvm_word_srctl(hw, index, &data[word]);
369 if (ret_code)
370 break;
371 }
372
373 /* Update the number of words read from the Shadow RAM */
374 *words = word;
375
376 return ret_code;
377}
378
379/**
Shannon Nelson7073f462015-06-05 12:20:34 -0400380 * i40e_read_nvm_buffer_aq - Reads Shadow RAM buffer via AQ
381 * @hw: pointer to the HW structure
382 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
383 * @words: (in) number of words to read; (out) number of words actually read
384 * @data: words read from the Shadow RAM
385 *
386 * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_aq()
387 * method. The buffer read is preceded by the NVM ownership take
388 * and followed by the release.
389 **/
390static i40e_status i40e_read_nvm_buffer_aq(struct i40e_hw *hw, u16 offset,
391 u16 *words, u16 *data)
392{
393 i40e_status ret_code;
Colin Ian King793c6f82017-11-05 13:04:29 +0000394 u16 read_size;
Shannon Nelson7073f462015-06-05 12:20:34 -0400395 bool last_cmd = false;
396 u16 words_read = 0;
397 u16 i = 0;
398
399 do {
400 /* Calculate number of bytes we should read in this step.
401 * FVL AQ do not allow to read more than one page at a time or
402 * to cross page boundaries.
403 */
404 if (offset % I40E_SR_SECTOR_SIZE_IN_WORDS)
405 read_size = min(*words,
406 (u16)(I40E_SR_SECTOR_SIZE_IN_WORDS -
407 (offset % I40E_SR_SECTOR_SIZE_IN_WORDS)));
408 else
409 read_size = min((*words - words_read),
410 I40E_SR_SECTOR_SIZE_IN_WORDS);
411
412 /* Check if this is last command, if so set proper flag */
413 if ((words_read + read_size) >= *words)
414 last_cmd = true;
415
416 ret_code = i40e_read_nvm_aq(hw, 0x0, offset, read_size,
417 data + words_read, last_cmd);
418 if (ret_code)
419 goto read_nvm_buffer_aq_exit;
420
421 /* Increment counter for words already read and move offset to
422 * new read location
423 */
424 words_read += read_size;
425 offset += read_size;
426 } while (words_read < *words);
427
428 for (i = 0; i < *words; i++)
429 data[i] = le16_to_cpu(((__le16 *)data)[i]);
430
431read_nvm_buffer_aq_exit:
432 *words = words_read;
433 return ret_code;
434}
435
436/**
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700437 * __i40e_read_nvm_buffer - Reads nvm buffer, caller must acquire lock
Shannon Nelson3e261862014-02-06 05:51:06 +0000438 * @hw: pointer to the HW structure
439 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
440 * @words: (in) number of words to read; (out) number of words actually read
441 * @data: words read from the Shadow RAM
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000442 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000443 * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_srrd()
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700444 * method.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000445 **/
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700446static i40e_status __i40e_read_nvm_buffer(struct i40e_hw *hw,
447 u16 offset, u16 *words,
448 u16 *data)
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000449{
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700450 if (hw->flags & I40E_HW_FLAG_AQ_SRCTL_ACCESS_ENABLE)
Stefano Brivio2c4d36b2017-09-06 10:11:39 +0200451 return i40e_read_nvm_buffer_aq(hw, offset, words, data);
452
453 return i40e_read_nvm_buffer_srctl(hw, offset, words, data);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000454}
455
456/**
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000457 * i40e_write_nvm_aq - Writes Shadow RAM.
458 * @hw: pointer to the HW structure.
459 * @module_pointer: module pointer location in words from the NVM beginning
460 * @offset: offset in words from module start
461 * @words: number of words to write
462 * @data: buffer with words to write to the Shadow RAM
463 * @last_command: tells the AdminQ that this is the last command
464 *
465 * Writes a 16 bit words buffer to the Shadow RAM using the admin command.
466 **/
Wei Yongjun952d9632014-07-30 09:02:53 +0000467static i40e_status i40e_write_nvm_aq(struct i40e_hw *hw, u8 module_pointer,
468 u32 offset, u16 words, void *data,
469 bool last_command)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000470{
471 i40e_status ret_code = I40E_ERR_NVM;
Shannon Nelson6b5c1b82015-08-28 17:55:47 -0400472 struct i40e_asq_cmd_details cmd_details;
473
474 memset(&cmd_details, 0, sizeof(cmd_details));
475 cmd_details.wb_desc = &hw->nvm_wb_desc;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000476
477 /* Here we are checking the SR limit only for the flat memory model.
478 * We cannot do it for the module-based model, as we did not acquire
479 * the NVM resource yet (we cannot get the module pointer value).
480 * Firmware will check the module-based model.
481 */
482 if ((offset + words) > hw->nvm.sr_size)
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000483 i40e_debug(hw, I40E_DEBUG_NVM,
484 "NVM write error: offset %d beyond Shadow RAM limit %d\n",
485 (offset + words), hw->nvm.sr_size);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000486 else if (words > I40E_SR_SECTOR_SIZE_IN_WORDS)
487 /* We can write only up to 4KB (one sector), in one AQ write */
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000488 i40e_debug(hw, I40E_DEBUG_NVM,
489 "NVM write fail error: tried to write %d words, limit is %d.\n",
490 words, I40E_SR_SECTOR_SIZE_IN_WORDS);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000491 else if (((offset + (words - 1)) / I40E_SR_SECTOR_SIZE_IN_WORDS)
492 != (offset / I40E_SR_SECTOR_SIZE_IN_WORDS))
493 /* A single write cannot spread over two sectors */
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000494 i40e_debug(hw, I40E_DEBUG_NVM,
495 "NVM write error: cannot spread over two sectors in a single write offset=%d words=%d\n",
496 offset, words);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000497 else
498 ret_code = i40e_aq_update_nvm(hw, module_pointer,
499 2 * offset, /*bytes*/
500 2 * words, /*bytes*/
Pawel Jablonskie3a5d6e2017-12-18 05:14:44 -0500501 data, last_command, 0,
502 &cmd_details);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000503
504 return ret_code;
505}
506
507/**
Shannon Nelson3e261862014-02-06 05:51:06 +0000508 * i40e_calc_nvm_checksum - Calculates and returns the checksum
509 * @hw: pointer to hardware structure
510 * @checksum: pointer to the checksum
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000511 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000512 * This function calculates SW Checksum that covers the whole 64kB shadow RAM
513 * except the VPD and PCIe ALT Auto-load modules. The structure and size of VPD
514 * is customer specific and unknown. Therefore, this function skips all maximum
515 * possible size of VPD (1kB).
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000516 **/
517static i40e_status i40e_calc_nvm_checksum(struct i40e_hw *hw,
518 u16 *checksum)
519{
Jean Sacren0e5229c2015-10-13 01:06:31 -0600520 i40e_status ret_code;
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000521 struct i40e_virt_mem vmem;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000522 u16 pcie_alt_module = 0;
523 u16 checksum_local = 0;
524 u16 vpd_module = 0;
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000525 u16 *data;
526 u16 i = 0;
527
528 ret_code = i40e_allocate_virt_mem(hw, &vmem,
529 I40E_SR_SECTOR_SIZE_IN_WORDS * sizeof(u16));
530 if (ret_code)
531 goto i40e_calc_nvm_checksum_exit;
532 data = (u16 *)vmem.va;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000533
534 /* read pointer to VPD area */
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700535 ret_code = __i40e_read_nvm_word(hw, I40E_SR_VPD_PTR, &vpd_module);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000536 if (ret_code) {
537 ret_code = I40E_ERR_NVM_CHECKSUM;
538 goto i40e_calc_nvm_checksum_exit;
539 }
540
541 /* read pointer to PCIe Alt Auto-load module */
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700542 ret_code = __i40e_read_nvm_word(hw, I40E_SR_PCIE_ALT_AUTO_LOAD_PTR,
543 &pcie_alt_module);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000544 if (ret_code) {
545 ret_code = I40E_ERR_NVM_CHECKSUM;
546 goto i40e_calc_nvm_checksum_exit;
547 }
548
549 /* Calculate SW checksum that covers the whole 64kB shadow RAM
550 * except the VPD and PCIe ALT Auto-load modules
551 */
552 for (i = 0; i < hw->nvm.sr_size; i++) {
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000553 /* Read SR page */
554 if ((i % I40E_SR_SECTOR_SIZE_IN_WORDS) == 0) {
555 u16 words = I40E_SR_SECTOR_SIZE_IN_WORDS;
556
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700557 ret_code = __i40e_read_nvm_buffer(hw, i, &words, data);
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000558 if (ret_code) {
559 ret_code = I40E_ERR_NVM_CHECKSUM;
560 goto i40e_calc_nvm_checksum_exit;
561 }
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000562 }
563
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000564 /* Skip Checksum word */
565 if (i == I40E_SR_SW_CHECKSUM_WORD)
566 continue;
567 /* Skip VPD module (convert byte size to word count) */
568 if ((i >= (u32)vpd_module) &&
569 (i < ((u32)vpd_module +
570 (I40E_SR_VPD_MODULE_MAX_SIZE / 2)))) {
571 continue;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000572 }
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000573 /* Skip PCIe ALT module (convert byte size to word count) */
574 if ((i >= (u32)pcie_alt_module) &&
575 (i < ((u32)pcie_alt_module +
576 (I40E_SR_PCIE_ALT_MODULE_MAX_SIZE / 2)))) {
577 continue;
578 }
579
580 checksum_local += data[i % I40E_SR_SECTOR_SIZE_IN_WORDS];
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000581 }
582
583 *checksum = (u16)I40E_SR_SW_CHECKSUM_BASE - checksum_local;
584
585i40e_calc_nvm_checksum_exit:
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000586 i40e_free_virt_mem(hw, &vmem);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000587 return ret_code;
588}
589
590/**
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000591 * i40e_update_nvm_checksum - Updates the NVM checksum
592 * @hw: pointer to hardware structure
593 *
594 * NVM ownership must be acquired before calling this function and released
595 * on ARQ completion event reception by caller.
596 * This function will commit SR to NVM.
597 **/
598i40e_status i40e_update_nvm_checksum(struct i40e_hw *hw)
599{
Jean Sacren0e5229c2015-10-13 01:06:31 -0600600 i40e_status ret_code;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000601 u16 checksum;
Jesse Brandeburgdd38c582015-08-26 15:14:18 -0400602 __le16 le_sum;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000603
604 ret_code = i40e_calc_nvm_checksum(hw, &checksum);
Jean Sacren2fc4cd52015-10-13 01:06:32 -0600605 if (!ret_code) {
606 le_sum = cpu_to_le16(checksum);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000607 ret_code = i40e_write_nvm_aq(hw, 0x00, I40E_SR_SW_CHECKSUM_WORD,
Jesse Brandeburgdd38c582015-08-26 15:14:18 -0400608 1, &le_sum, true);
Jean Sacren2fc4cd52015-10-13 01:06:32 -0600609 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000610
611 return ret_code;
612}
613
614/**
Shannon Nelson3e261862014-02-06 05:51:06 +0000615 * i40e_validate_nvm_checksum - Validate EEPROM checksum
616 * @hw: pointer to hardware structure
617 * @checksum: calculated checksum
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000618 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000619 * Performs checksum calculation and validates the NVM SW checksum. If the
620 * caller does not need checksum, the value can be NULL.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000621 **/
622i40e_status i40e_validate_nvm_checksum(struct i40e_hw *hw,
623 u16 *checksum)
624{
625 i40e_status ret_code = 0;
626 u16 checksum_sr = 0;
Jesse Brandeburge15c9fa2014-01-17 15:36:31 -0800627 u16 checksum_local = 0;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000628
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700629 /* We must acquire the NVM lock in order to correctly synchronize the
630 * NVM accesses across multiple PFs. Without doing so it is possible
631 * for one of the PFs to read invalid data potentially indicating that
632 * the checksum is invalid.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000633 */
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700634 ret_code = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
635 if (ret_code)
636 return ret_code;
637 ret_code = i40e_calc_nvm_checksum(hw, &checksum_local);
638 __i40e_read_nvm_word(hw, I40E_SR_SW_CHECKSUM_WORD, &checksum_sr);
639 i40e_release_nvm(hw);
640 if (ret_code)
641 return ret_code;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000642
643 /* Verify read checksum from EEPROM is the same as
644 * calculated checksum
645 */
646 if (checksum_local != checksum_sr)
647 ret_code = I40E_ERR_NVM_CHECKSUM;
648
649 /* If the user cares, return the calculated checksum */
650 if (checksum)
651 *checksum = checksum_local;
652
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000653 return ret_code;
654}
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000655
656static i40e_status i40e_nvmupd_state_init(struct i40e_hw *hw,
657 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400658 u8 *bytes, int *perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000659static i40e_status i40e_nvmupd_state_reading(struct i40e_hw *hw,
660 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400661 u8 *bytes, int *perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000662static i40e_status i40e_nvmupd_state_writing(struct i40e_hw *hw,
663 struct i40e_nvm_access *cmd,
664 u8 *bytes, int *errno);
665static enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
666 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400667 int *perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000668static i40e_status i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
669 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400670 int *perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000671static i40e_status i40e_nvmupd_nvm_write(struct i40e_hw *hw,
672 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400673 u8 *bytes, int *perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000674static i40e_status i40e_nvmupd_nvm_read(struct i40e_hw *hw,
675 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400676 u8 *bytes, int *perrno);
Shannon Nelsone4c83c22015-08-28 17:55:50 -0400677static i40e_status i40e_nvmupd_exec_aq(struct i40e_hw *hw,
678 struct i40e_nvm_access *cmd,
679 u8 *bytes, int *perrno);
Shannon Nelsonb72dc7b2015-08-28 17:55:51 -0400680static i40e_status i40e_nvmupd_get_aq_result(struct i40e_hw *hw,
681 struct i40e_nvm_access *cmd,
682 u8 *bytes, int *perrno);
Pawel Jablonskie3a5d6e2017-12-18 05:14:44 -0500683static i40e_status i40e_nvmupd_get_aq_event(struct i40e_hw *hw,
684 struct i40e_nvm_access *cmd,
685 u8 *bytes, int *perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000686static inline u8 i40e_nvmupd_get_module(u32 val)
687{
688 return (u8)(val & I40E_NVM_MOD_PNT_MASK);
689}
690static inline u8 i40e_nvmupd_get_transaction(u32 val)
691{
692 return (u8)((val & I40E_NVM_TRANS_MASK) >> I40E_NVM_TRANS_SHIFT);
693}
694
Pawel Jablonskie3a5d6e2017-12-18 05:14:44 -0500695static inline u8 i40e_nvmupd_get_preservation_flags(u32 val)
696{
697 return (u8)((val & I40E_NVM_PRESERVATION_FLAGS_MASK) >>
698 I40E_NVM_PRESERVATION_FLAGS_SHIFT);
699}
700
Jingjing Wu4e68adfe2015-09-28 14:12:31 -0400701static const char * const i40e_nvm_update_state_str[] = {
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000702 "I40E_NVMUPD_INVALID",
703 "I40E_NVMUPD_READ_CON",
704 "I40E_NVMUPD_READ_SNT",
705 "I40E_NVMUPD_READ_LCB",
706 "I40E_NVMUPD_READ_SA",
707 "I40E_NVMUPD_WRITE_ERA",
708 "I40E_NVMUPD_WRITE_CON",
709 "I40E_NVMUPD_WRITE_SNT",
710 "I40E_NVMUPD_WRITE_LCB",
711 "I40E_NVMUPD_WRITE_SA",
712 "I40E_NVMUPD_CSUM_CON",
713 "I40E_NVMUPD_CSUM_SA",
714 "I40E_NVMUPD_CSUM_LCB",
Shannon Nelson0af8e9d2015-08-28 17:55:48 -0400715 "I40E_NVMUPD_STATUS",
Shannon Nelsone4c83c22015-08-28 17:55:50 -0400716 "I40E_NVMUPD_EXEC_AQ",
Shannon Nelsonb72dc7b2015-08-28 17:55:51 -0400717 "I40E_NVMUPD_GET_AQ_RESULT",
Pawel Jablonskie3a5d6e2017-12-18 05:14:44 -0500718 "I40E_NVMUPD_GET_AQ_EVENT",
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000719};
720
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000721/**
722 * i40e_nvmupd_command - Process an NVM update command
723 * @hw: pointer to hardware structure
724 * @cmd: pointer to nvm update command
725 * @bytes: pointer to the data buffer
Shannon Nelson79afe832015-07-23 16:54:33 -0400726 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000727 *
728 * Dispatches command depending on what update state is current
729 **/
730i40e_status i40e_nvmupd_command(struct i40e_hw *hw,
731 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400732 u8 *bytes, int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000733{
734 i40e_status status;
Shannon Nelson0af8e9d2015-08-28 17:55:48 -0400735 enum i40e_nvmupd_cmd upd_cmd;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000736
737 /* assume success */
Shannon Nelson79afe832015-07-23 16:54:33 -0400738 *perrno = 0;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000739
Shannon Nelson0af8e9d2015-08-28 17:55:48 -0400740 /* early check for status command and debug msgs */
741 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
742
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700743 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",
Shannon Nelson0af8e9d2015-08-28 17:55:48 -0400744 i40e_nvm_update_state_str[upd_cmd],
745 hw->nvmupd_state,
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700746 hw->nvm_release_on_done, hw->nvm_wait_opcode,
Shannon Nelson1d73b2d2015-12-23 12:05:51 -0800747 cmd->command, cmd->config, cmd->offset, cmd->data_size);
Shannon Nelson0af8e9d2015-08-28 17:55:48 -0400748
749 if (upd_cmd == I40E_NVMUPD_INVALID) {
750 *perrno = -EFAULT;
751 i40e_debug(hw, I40E_DEBUG_NVM,
752 "i40e_nvmupd_validate_command returns %d errno %d\n",
753 upd_cmd, *perrno);
754 }
755
756 /* a status request returns immediately rather than
757 * going into the state machine
758 */
759 if (upd_cmd == I40E_NVMUPD_STATUS) {
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700760 if (!cmd->data_size) {
761 *perrno = -EFAULT;
762 return I40E_ERR_BUF_TOO_SHORT;
763 }
764
Shannon Nelson0af8e9d2015-08-28 17:55:48 -0400765 bytes[0] = hw->nvmupd_state;
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700766
767 if (cmd->data_size >= 4) {
768 bytes[1] = 0;
769 *((u16 *)&bytes[2]) = hw->nvm_wait_opcode;
770 }
771
Maciej Sosin81fa7c92016-10-11 15:26:57 -0700772 /* Clear error status on read */
773 if (hw->nvmupd_state == I40E_NVMUPD_STATE_ERROR)
774 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
775
Shannon Nelson0af8e9d2015-08-28 17:55:48 -0400776 return 0;
777 }
778
Maciej Sosin81fa7c92016-10-11 15:26:57 -0700779 /* Clear status even it is not read and log */
780 if (hw->nvmupd_state == I40E_NVMUPD_STATE_ERROR) {
781 i40e_debug(hw, I40E_DEBUG_NVM,
782 "Clearing I40E_NVMUPD_STATE_ERROR state without reading\n");
783 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
784 }
785
Sudheer Mogilappagari2bf01932017-07-12 05:46:07 -0400786 /* Acquire lock to prevent race condition where adminq_task
787 * can execute after i40e_nvmupd_nvm_read/write but before state
Sudheer Mogilappagari167d52e2017-08-27 15:07:47 -0700788 * variables (nvm_wait_opcode, nvm_release_on_done) are updated.
789 *
790 * During NVMUpdate, it is observed that lock could be held for
791 * ~5ms for most commands. However lock is held for ~60ms for
792 * NVMUPD_CSUM_LCB command.
Sudheer Mogilappagari2bf01932017-07-12 05:46:07 -0400793 */
794 mutex_lock(&hw->aq.arq_mutex);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000795 switch (hw->nvmupd_state) {
796 case I40E_NVMUPD_STATE_INIT:
Shannon Nelson79afe832015-07-23 16:54:33 -0400797 status = i40e_nvmupd_state_init(hw, cmd, bytes, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000798 break;
799
800 case I40E_NVMUPD_STATE_READING:
Shannon Nelson79afe832015-07-23 16:54:33 -0400801 status = i40e_nvmupd_state_reading(hw, cmd, bytes, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000802 break;
803
804 case I40E_NVMUPD_STATE_WRITING:
Shannon Nelson79afe832015-07-23 16:54:33 -0400805 status = i40e_nvmupd_state_writing(hw, cmd, bytes, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000806 break;
807
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400808 case I40E_NVMUPD_STATE_INIT_WAIT:
809 case I40E_NVMUPD_STATE_WRITE_WAIT:
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700810 /* if we need to stop waiting for an event, clear
811 * the wait info and return before doing anything else
812 */
813 if (cmd->offset == 0xffff) {
Pawel Jablonskie3a5d6e2017-12-18 05:14:44 -0500814 i40e_nvmupd_clear_wait_state(hw);
Sudheer Mogilappagari167d52e2017-08-27 15:07:47 -0700815 status = 0;
Pawel Jablonskie3a5d6e2017-12-18 05:14:44 -0500816 break;
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700817 }
818
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400819 status = I40E_ERR_NOT_READY;
820 *perrno = -EBUSY;
821 break;
822
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000823 default:
824 /* invalid state, should never happen */
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000825 i40e_debug(hw, I40E_DEBUG_NVM,
826 "NVMUPD: no such state %d\n", hw->nvmupd_state);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000827 status = I40E_NOT_SUPPORTED;
Shannon Nelson79afe832015-07-23 16:54:33 -0400828 *perrno = -ESRCH;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000829 break;
830 }
Pawel Jablonskie3a5d6e2017-12-18 05:14:44 -0500831
Sudheer Mogilappagari2bf01932017-07-12 05:46:07 -0400832 mutex_unlock(&hw->aq.arq_mutex);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000833 return status;
834}
835
836/**
837 * i40e_nvmupd_state_init - Handle NVM update state Init
838 * @hw: pointer to hardware structure
839 * @cmd: pointer to nvm update command buffer
840 * @bytes: pointer to the data buffer
Shannon Nelson79afe832015-07-23 16:54:33 -0400841 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000842 *
843 * Process legitimate commands of the Init state and conditionally set next
844 * state. Reject all other commands.
845 **/
846static i40e_status i40e_nvmupd_state_init(struct i40e_hw *hw,
847 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400848 u8 *bytes, int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000849{
850 i40e_status status = 0;
851 enum i40e_nvmupd_cmd upd_cmd;
852
Shannon Nelson79afe832015-07-23 16:54:33 -0400853 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000854
855 switch (upd_cmd) {
856 case I40E_NVMUPD_READ_SA:
857 status = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
858 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -0400859 *perrno = i40e_aq_rc_to_posix(status,
Shannon Nelsonbf848f32014-11-13 08:23:22 +0000860 hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000861 } else {
Shannon Nelson79afe832015-07-23 16:54:33 -0400862 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000863 i40e_release_nvm(hw);
864 }
865 break;
866
867 case I40E_NVMUPD_READ_SNT:
868 status = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
869 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -0400870 *perrno = i40e_aq_rc_to_posix(status,
Shannon Nelsonbf848f32014-11-13 08:23:22 +0000871 hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000872 } else {
Shannon Nelson79afe832015-07-23 16:54:33 -0400873 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
Shannon Nelson0fdd0522014-11-13 08:23:20 +0000874 if (status)
875 i40e_release_nvm(hw);
876 else
877 hw->nvmupd_state = I40E_NVMUPD_STATE_READING;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000878 }
879 break;
880
881 case I40E_NVMUPD_WRITE_ERA:
882 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
883 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -0400884 *perrno = i40e_aq_rc_to_posix(status,
Shannon Nelsonbf848f32014-11-13 08:23:22 +0000885 hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000886 } else {
Shannon Nelson79afe832015-07-23 16:54:33 -0400887 status = i40e_nvmupd_nvm_erase(hw, cmd, perrno);
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400888 if (status) {
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000889 i40e_release_nvm(hw);
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400890 } else {
Shannon Nelson437f82a2016-04-01 03:56:09 -0700891 hw->nvm_release_on_done = true;
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700892 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_erase;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400893 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
894 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000895 }
896 break;
897
898 case I40E_NVMUPD_WRITE_SA:
899 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
900 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -0400901 *perrno = i40e_aq_rc_to_posix(status,
Shannon Nelsonbf848f32014-11-13 08:23:22 +0000902 hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000903 } else {
Shannon Nelson79afe832015-07-23 16:54:33 -0400904 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400905 if (status) {
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000906 i40e_release_nvm(hw);
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400907 } else {
Shannon Nelson437f82a2016-04-01 03:56:09 -0700908 hw->nvm_release_on_done = true;
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700909 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400910 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
911 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000912 }
913 break;
914
915 case I40E_NVMUPD_WRITE_SNT:
916 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
917 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -0400918 *perrno = i40e_aq_rc_to_posix(status,
Shannon Nelsonbf848f32014-11-13 08:23:22 +0000919 hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000920 } else {
Shannon Nelson79afe832015-07-23 16:54:33 -0400921 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700922 if (status) {
Shannon Nelson0fdd0522014-11-13 08:23:20 +0000923 i40e_release_nvm(hw);
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700924 } else {
925 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400926 hw->nvmupd_state = I40E_NVMUPD_STATE_WRITE_WAIT;
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700927 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000928 }
929 break;
930
931 case I40E_NVMUPD_CSUM_SA:
932 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
933 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -0400934 *perrno = i40e_aq_rc_to_posix(status,
Shannon Nelsonbf848f32014-11-13 08:23:22 +0000935 hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000936 } else {
937 status = i40e_update_nvm_checksum(hw);
938 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -0400939 *perrno = hw->aq.asq_last_status ?
Shannon Nelsonbf848f32014-11-13 08:23:22 +0000940 i40e_aq_rc_to_posix(status,
941 hw->aq.asq_last_status) :
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000942 -EIO;
943 i40e_release_nvm(hw);
944 } else {
Shannon Nelson437f82a2016-04-01 03:56:09 -0700945 hw->nvm_release_on_done = true;
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700946 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400947 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000948 }
949 }
950 break;
951
Shannon Nelsone4c83c22015-08-28 17:55:50 -0400952 case I40E_NVMUPD_EXEC_AQ:
953 status = i40e_nvmupd_exec_aq(hw, cmd, bytes, perrno);
954 break;
955
Shannon Nelsonb72dc7b2015-08-28 17:55:51 -0400956 case I40E_NVMUPD_GET_AQ_RESULT:
957 status = i40e_nvmupd_get_aq_result(hw, cmd, bytes, perrno);
958 break;
959
Pawel Jablonskie3a5d6e2017-12-18 05:14:44 -0500960 case I40E_NVMUPD_GET_AQ_EVENT:
961 status = i40e_nvmupd_get_aq_event(hw, cmd, bytes, perrno);
962 break;
963
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000964 default:
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000965 i40e_debug(hw, I40E_DEBUG_NVM,
966 "NVMUPD: bad cmd %s in init state\n",
967 i40e_nvm_update_state_str[upd_cmd]);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000968 status = I40E_ERR_NVM;
Shannon Nelson79afe832015-07-23 16:54:33 -0400969 *perrno = -ESRCH;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000970 break;
971 }
972 return status;
973}
974
975/**
976 * i40e_nvmupd_state_reading - Handle NVM update state Reading
977 * @hw: pointer to hardware structure
978 * @cmd: pointer to nvm update command buffer
979 * @bytes: pointer to the data buffer
Shannon Nelson79afe832015-07-23 16:54:33 -0400980 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000981 *
982 * NVM ownership is already held. Process legitimate commands and set any
983 * change in state; reject all other commands.
984 **/
985static i40e_status i40e_nvmupd_state_reading(struct i40e_hw *hw,
986 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400987 u8 *bytes, int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000988{
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400989 i40e_status status = 0;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000990 enum i40e_nvmupd_cmd upd_cmd;
991
Shannon Nelson79afe832015-07-23 16:54:33 -0400992 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000993
994 switch (upd_cmd) {
995 case I40E_NVMUPD_READ_SA:
996 case I40E_NVMUPD_READ_CON:
Shannon Nelson79afe832015-07-23 16:54:33 -0400997 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000998 break;
999
1000 case I40E_NVMUPD_READ_LCB:
Shannon Nelson79afe832015-07-23 16:54:33 -04001001 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001002 i40e_release_nvm(hw);
1003 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
1004 break;
1005
1006 default:
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001007 i40e_debug(hw, I40E_DEBUG_NVM,
1008 "NVMUPD: bad cmd %s in reading state.\n",
1009 i40e_nvm_update_state_str[upd_cmd]);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001010 status = I40E_NOT_SUPPORTED;
Shannon Nelson79afe832015-07-23 16:54:33 -04001011 *perrno = -ESRCH;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001012 break;
1013 }
1014 return status;
1015}
1016
1017/**
1018 * i40e_nvmupd_state_writing - Handle NVM update state Writing
1019 * @hw: pointer to hardware structure
1020 * @cmd: pointer to nvm update command buffer
1021 * @bytes: pointer to the data buffer
Shannon Nelson79afe832015-07-23 16:54:33 -04001022 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001023 *
1024 * NVM ownership is already held. Process legitimate commands and set any
1025 * change in state; reject all other commands
1026 **/
1027static i40e_status i40e_nvmupd_state_writing(struct i40e_hw *hw,
1028 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -04001029 u8 *bytes, int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001030{
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001031 i40e_status status = 0;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001032 enum i40e_nvmupd_cmd upd_cmd;
Shannon Nelson2c47e352015-02-21 06:45:10 +00001033 bool retry_attempt = false;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001034
Shannon Nelson79afe832015-07-23 16:54:33 -04001035 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001036
Shannon Nelson2c47e352015-02-21 06:45:10 +00001037retry:
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001038 switch (upd_cmd) {
1039 case I40E_NVMUPD_WRITE_CON:
Shannon Nelson79afe832015-07-23 16:54:33 -04001040 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
Shannon Nelsonfed2db92016-04-12 08:30:43 -07001041 if (!status) {
1042 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001043 hw->nvmupd_state = I40E_NVMUPD_STATE_WRITE_WAIT;
Shannon Nelsonfed2db92016-04-12 08:30:43 -07001044 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001045 break;
1046
1047 case I40E_NVMUPD_WRITE_LCB:
Shannon Nelson79afe832015-07-23 16:54:33 -04001048 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001049 if (status) {
1050 *perrno = hw->aq.asq_last_status ?
1051 i40e_aq_rc_to_posix(status,
1052 hw->aq.asq_last_status) :
1053 -EIO;
1054 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
1055 } else {
Shannon Nelson437f82a2016-04-01 03:56:09 -07001056 hw->nvm_release_on_done = true;
Shannon Nelsonfed2db92016-04-12 08:30:43 -07001057 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001058 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
1059 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001060 break;
1061
1062 case I40E_NVMUPD_CSUM_CON:
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -07001063 /* Assumes the caller has acquired the nvm */
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001064 status = i40e_update_nvm_checksum(hw);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001065 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -04001066 *perrno = hw->aq.asq_last_status ?
Shannon Nelsonbf848f32014-11-13 08:23:22 +00001067 i40e_aq_rc_to_posix(status,
1068 hw->aq.asq_last_status) :
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001069 -EIO;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001070 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001071 } else {
Shannon Nelsonfed2db92016-04-12 08:30:43 -07001072 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001073 hw->nvmupd_state = I40E_NVMUPD_STATE_WRITE_WAIT;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001074 }
1075 break;
1076
Shannon Nelson0fdd0522014-11-13 08:23:20 +00001077 case I40E_NVMUPD_CSUM_LCB:
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -07001078 /* Assumes the caller has acquired the nvm */
Shannon Nelson0fdd0522014-11-13 08:23:20 +00001079 status = i40e_update_nvm_checksum(hw);
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001080 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -04001081 *perrno = hw->aq.asq_last_status ?
Shannon Nelsonbf848f32014-11-13 08:23:22 +00001082 i40e_aq_rc_to_posix(status,
1083 hw->aq.asq_last_status) :
Shannon Nelson0fdd0522014-11-13 08:23:20 +00001084 -EIO;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001085 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
1086 } else {
Shannon Nelson437f82a2016-04-01 03:56:09 -07001087 hw->nvm_release_on_done = true;
Shannon Nelsonfed2db92016-04-12 08:30:43 -07001088 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001089 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
1090 }
Shannon Nelson0fdd0522014-11-13 08:23:20 +00001091 break;
1092
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001093 default:
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001094 i40e_debug(hw, I40E_DEBUG_NVM,
1095 "NVMUPD: bad cmd %s in writing state.\n",
1096 i40e_nvm_update_state_str[upd_cmd]);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001097 status = I40E_NOT_SUPPORTED;
Shannon Nelson79afe832015-07-23 16:54:33 -04001098 *perrno = -ESRCH;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001099 break;
1100 }
Shannon Nelson2c47e352015-02-21 06:45:10 +00001101
1102 /* In some circumstances, a multi-write transaction takes longer
1103 * than the default 3 minute timeout on the write semaphore. If
1104 * the write failed with an EBUSY status, this is likely the problem,
1105 * so here we try to reacquire the semaphore then retry the write.
1106 * We only do one retry, then give up.
1107 */
1108 if (status && (hw->aq.asq_last_status == I40E_AQ_RC_EBUSY) &&
1109 !retry_attempt) {
1110 i40e_status old_status = status;
1111 u32 old_asq_status = hw->aq.asq_last_status;
1112 u32 gtime;
1113
1114 gtime = rd32(hw, I40E_GLVFGEN_TIMER);
1115 if (gtime >= hw->nvm.hw_semaphore_timeout) {
1116 i40e_debug(hw, I40E_DEBUG_ALL,
1117 "NVMUPD: write semaphore expired (%d >= %lld), retrying\n",
1118 gtime, hw->nvm.hw_semaphore_timeout);
1119 i40e_release_nvm(hw);
1120 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
1121 if (status) {
1122 i40e_debug(hw, I40E_DEBUG_ALL,
1123 "NVMUPD: write semaphore reacquire failed aq_err = %d\n",
1124 hw->aq.asq_last_status);
1125 status = old_status;
1126 hw->aq.asq_last_status = old_asq_status;
1127 } else {
1128 retry_attempt = true;
1129 goto retry;
1130 }
1131 }
1132 }
1133
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001134 return status;
1135}
1136
1137/**
Pawel Jablonskie3a5d6e2017-12-18 05:14:44 -05001138 * i40e_nvmupd_clear_wait_state - clear wait state on hw
1139 * @hw: pointer to the hardware structure
1140 **/
1141void i40e_nvmupd_clear_wait_state(struct i40e_hw *hw)
1142{
1143 i40e_debug(hw, I40E_DEBUG_NVM,
1144 "NVMUPD: clearing wait on opcode 0x%04x\n",
1145 hw->nvm_wait_opcode);
1146
1147 if (hw->nvm_release_on_done) {
1148 i40e_release_nvm(hw);
1149 hw->nvm_release_on_done = false;
1150 }
1151 hw->nvm_wait_opcode = 0;
1152
1153 if (hw->aq.arq_last_status) {
1154 hw->nvmupd_state = I40E_NVMUPD_STATE_ERROR;
1155 return;
1156 }
1157
1158 switch (hw->nvmupd_state) {
1159 case I40E_NVMUPD_STATE_INIT_WAIT:
1160 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
1161 break;
1162
1163 case I40E_NVMUPD_STATE_WRITE_WAIT:
1164 hw->nvmupd_state = I40E_NVMUPD_STATE_WRITING;
1165 break;
1166
1167 default:
1168 break;
1169 }
1170}
1171
1172/**
Shannon Nelsonbab2fb62016-04-01 03:56:11 -07001173 * i40e_nvmupd_check_wait_event - handle NVM update operation events
1174 * @hw: pointer to the hardware structure
1175 * @opcode: the event that just happened
1176 **/
Pawel Jablonskie3a5d6e2017-12-18 05:14:44 -05001177void i40e_nvmupd_check_wait_event(struct i40e_hw *hw, u16 opcode,
1178 struct i40e_aq_desc *desc)
Shannon Nelsonbab2fb62016-04-01 03:56:11 -07001179{
Pawel Jablonskie3a5d6e2017-12-18 05:14:44 -05001180 u32 aq_desc_len = sizeof(struct i40e_aq_desc);
1181
Shannon Nelsonfed2db92016-04-12 08:30:43 -07001182 if (opcode == hw->nvm_wait_opcode) {
Pawel Jablonskie3a5d6e2017-12-18 05:14:44 -05001183 memcpy(&hw->nvm_aq_event_desc, desc, aq_desc_len);
1184 i40e_nvmupd_clear_wait_state(hw);
Shannon Nelsonbab2fb62016-04-01 03:56:11 -07001185 }
1186}
1187
1188/**
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001189 * i40e_nvmupd_validate_command - Validate given command
1190 * @hw: pointer to hardware structure
1191 * @cmd: pointer to nvm update command buffer
Shannon Nelson79afe832015-07-23 16:54:33 -04001192 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001193 *
1194 * Return one of the valid command types or I40E_NVMUPD_INVALID
1195 **/
1196static enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
1197 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -04001198 int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001199{
1200 enum i40e_nvmupd_cmd upd_cmd;
Shannon Nelson0af8e9d2015-08-28 17:55:48 -04001201 u8 module, transaction;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001202
1203 /* anything that doesn't match a recognized case is an error */
1204 upd_cmd = I40E_NVMUPD_INVALID;
1205
1206 transaction = i40e_nvmupd_get_transaction(cmd->config);
Shannon Nelson0af8e9d2015-08-28 17:55:48 -04001207 module = i40e_nvmupd_get_module(cmd->config);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001208
1209 /* limits on data size */
1210 if ((cmd->data_size < 1) ||
1211 (cmd->data_size > I40E_NVMUPD_MAX_DATA)) {
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001212 i40e_debug(hw, I40E_DEBUG_NVM,
1213 "i40e_nvmupd_validate_command data_size %d\n",
1214 cmd->data_size);
Shannon Nelson79afe832015-07-23 16:54:33 -04001215 *perrno = -EFAULT;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001216 return I40E_NVMUPD_INVALID;
1217 }
1218
1219 switch (cmd->command) {
1220 case I40E_NVM_READ:
1221 switch (transaction) {
1222 case I40E_NVM_CON:
1223 upd_cmd = I40E_NVMUPD_READ_CON;
1224 break;
1225 case I40E_NVM_SNT:
1226 upd_cmd = I40E_NVMUPD_READ_SNT;
1227 break;
1228 case I40E_NVM_LCB:
1229 upd_cmd = I40E_NVMUPD_READ_LCB;
1230 break;
1231 case I40E_NVM_SA:
1232 upd_cmd = I40E_NVMUPD_READ_SA;
1233 break;
Shannon Nelson0af8e9d2015-08-28 17:55:48 -04001234 case I40E_NVM_EXEC:
1235 if (module == 0xf)
1236 upd_cmd = I40E_NVMUPD_STATUS;
Shannon Nelsonb72dc7b2015-08-28 17:55:51 -04001237 else if (module == 0)
1238 upd_cmd = I40E_NVMUPD_GET_AQ_RESULT;
Shannon Nelson0af8e9d2015-08-28 17:55:48 -04001239 break;
Pawel Jablonskie3a5d6e2017-12-18 05:14:44 -05001240 case I40E_NVM_AQE:
1241 upd_cmd = I40E_NVMUPD_GET_AQ_EVENT;
1242 break;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001243 }
1244 break;
1245
1246 case I40E_NVM_WRITE:
1247 switch (transaction) {
1248 case I40E_NVM_CON:
1249 upd_cmd = I40E_NVMUPD_WRITE_CON;
1250 break;
1251 case I40E_NVM_SNT:
1252 upd_cmd = I40E_NVMUPD_WRITE_SNT;
1253 break;
1254 case I40E_NVM_LCB:
1255 upd_cmd = I40E_NVMUPD_WRITE_LCB;
1256 break;
1257 case I40E_NVM_SA:
1258 upd_cmd = I40E_NVMUPD_WRITE_SA;
1259 break;
1260 case I40E_NVM_ERA:
1261 upd_cmd = I40E_NVMUPD_WRITE_ERA;
1262 break;
1263 case I40E_NVM_CSUM:
1264 upd_cmd = I40E_NVMUPD_CSUM_CON;
1265 break;
1266 case (I40E_NVM_CSUM|I40E_NVM_SA):
1267 upd_cmd = I40E_NVMUPD_CSUM_SA;
1268 break;
1269 case (I40E_NVM_CSUM|I40E_NVM_LCB):
1270 upd_cmd = I40E_NVMUPD_CSUM_LCB;
1271 break;
Shannon Nelsone4c83c22015-08-28 17:55:50 -04001272 case I40E_NVM_EXEC:
1273 if (module == 0)
1274 upd_cmd = I40E_NVMUPD_EXEC_AQ;
1275 break;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001276 }
1277 break;
1278 }
1279
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001280 return upd_cmd;
1281}
1282
1283/**
Shannon Nelsone4c83c22015-08-28 17:55:50 -04001284 * i40e_nvmupd_exec_aq - Run an AQ command
1285 * @hw: pointer to hardware structure
1286 * @cmd: pointer to nvm update command buffer
1287 * @bytes: pointer to the data buffer
1288 * @perrno: pointer to return error code
1289 *
1290 * cmd structure contains identifiers and data buffer
1291 **/
1292static i40e_status i40e_nvmupd_exec_aq(struct i40e_hw *hw,
1293 struct i40e_nvm_access *cmd,
1294 u8 *bytes, int *perrno)
1295{
1296 struct i40e_asq_cmd_details cmd_details;
1297 i40e_status status;
1298 struct i40e_aq_desc *aq_desc;
1299 u32 buff_size = 0;
1300 u8 *buff = NULL;
1301 u32 aq_desc_len;
1302 u32 aq_data_len;
1303
1304 i40e_debug(hw, I40E_DEBUG_NVM, "NVMUPD: %s\n", __func__);
Pawel Jablonskie3a5d6e2017-12-18 05:14:44 -05001305 if (cmd->offset == 0xffff)
1306 return 0;
1307
Shannon Nelsone4c83c22015-08-28 17:55:50 -04001308 memset(&cmd_details, 0, sizeof(cmd_details));
1309 cmd_details.wb_desc = &hw->nvm_wb_desc;
1310
1311 aq_desc_len = sizeof(struct i40e_aq_desc);
1312 memset(&hw->nvm_wb_desc, 0, aq_desc_len);
1313
1314 /* get the aq descriptor */
1315 if (cmd->data_size < aq_desc_len) {
1316 i40e_debug(hw, I40E_DEBUG_NVM,
1317 "NVMUPD: not enough aq desc bytes for exec, size %d < %d\n",
1318 cmd->data_size, aq_desc_len);
1319 *perrno = -EINVAL;
1320 return I40E_ERR_PARAM;
1321 }
1322 aq_desc = (struct i40e_aq_desc *)bytes;
1323
1324 /* if data buffer needed, make sure it's ready */
1325 aq_data_len = cmd->data_size - aq_desc_len;
1326 buff_size = max_t(u32, aq_data_len, le16_to_cpu(aq_desc->datalen));
1327 if (buff_size) {
1328 if (!hw->nvm_buff.va) {
1329 status = i40e_allocate_virt_mem(hw, &hw->nvm_buff,
1330 hw->aq.asq_buf_size);
1331 if (status)
1332 i40e_debug(hw, I40E_DEBUG_NVM,
1333 "NVMUPD: i40e_allocate_virt_mem for exec buff failed, %d\n",
1334 status);
1335 }
1336
1337 if (hw->nvm_buff.va) {
1338 buff = hw->nvm_buff.va;
1339 memcpy(buff, &bytes[aq_desc_len], aq_data_len);
1340 }
1341 }
1342
Pawel Jablonskie3a5d6e2017-12-18 05:14:44 -05001343 if (cmd->offset)
1344 memset(&hw->nvm_aq_event_desc, 0, aq_desc_len);
1345
Shannon Nelsone4c83c22015-08-28 17:55:50 -04001346 /* and away we go! */
1347 status = i40e_asq_send_command(hw, aq_desc, buff,
1348 buff_size, &cmd_details);
1349 if (status) {
1350 i40e_debug(hw, I40E_DEBUG_NVM,
1351 "i40e_nvmupd_exec_aq err %s aq_err %s\n",
1352 i40e_stat_str(hw, status),
1353 i40e_aq_str(hw, hw->aq.asq_last_status));
1354 *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
Pawel Jablonskie3a5d6e2017-12-18 05:14:44 -05001355 return status;
Shannon Nelsone4c83c22015-08-28 17:55:50 -04001356 }
1357
Shannon Nelsonfed2db92016-04-12 08:30:43 -07001358 /* should we wait for a followup event? */
1359 if (cmd->offset) {
1360 hw->nvm_wait_opcode = cmd->offset;
1361 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
1362 }
1363
Shannon Nelsone4c83c22015-08-28 17:55:50 -04001364 return status;
1365}
1366
1367/**
Shannon Nelsonb72dc7b2015-08-28 17:55:51 -04001368 * i40e_nvmupd_get_aq_result - Get the results from the previous exec_aq
1369 * @hw: pointer to hardware structure
1370 * @cmd: pointer to nvm update command buffer
1371 * @bytes: pointer to the data buffer
1372 * @perrno: pointer to return error code
1373 *
1374 * cmd structure contains identifiers and data buffer
1375 **/
1376static i40e_status i40e_nvmupd_get_aq_result(struct i40e_hw *hw,
1377 struct i40e_nvm_access *cmd,
1378 u8 *bytes, int *perrno)
1379{
1380 u32 aq_total_len;
1381 u32 aq_desc_len;
1382 int remainder;
1383 u8 *buff;
1384
1385 i40e_debug(hw, I40E_DEBUG_NVM, "NVMUPD: %s\n", __func__);
1386
1387 aq_desc_len = sizeof(struct i40e_aq_desc);
1388 aq_total_len = aq_desc_len + le16_to_cpu(hw->nvm_wb_desc.datalen);
1389
1390 /* check offset range */
1391 if (cmd->offset > aq_total_len) {
1392 i40e_debug(hw, I40E_DEBUG_NVM, "%s: offset too big %d > %d\n",
1393 __func__, cmd->offset, aq_total_len);
1394 *perrno = -EINVAL;
1395 return I40E_ERR_PARAM;
1396 }
1397
1398 /* check copylength range */
1399 if (cmd->data_size > (aq_total_len - cmd->offset)) {
1400 int new_len = aq_total_len - cmd->offset;
1401
1402 i40e_debug(hw, I40E_DEBUG_NVM, "%s: copy length %d too big, trimming to %d\n",
1403 __func__, cmd->data_size, new_len);
1404 cmd->data_size = new_len;
1405 }
1406
1407 remainder = cmd->data_size;
1408 if (cmd->offset < aq_desc_len) {
1409 u32 len = aq_desc_len - cmd->offset;
1410
1411 len = min(len, cmd->data_size);
1412 i40e_debug(hw, I40E_DEBUG_NVM, "%s: aq_desc bytes %d to %d\n",
1413 __func__, cmd->offset, cmd->offset + len);
1414
1415 buff = ((u8 *)&hw->nvm_wb_desc) + cmd->offset;
1416 memcpy(bytes, buff, len);
1417
1418 bytes += len;
1419 remainder -= len;
1420 buff = hw->nvm_buff.va;
1421 } else {
1422 buff = hw->nvm_buff.va + (cmd->offset - aq_desc_len);
1423 }
1424
1425 if (remainder > 0) {
1426 int start_byte = buff - (u8 *)hw->nvm_buff.va;
1427
1428 i40e_debug(hw, I40E_DEBUG_NVM, "%s: databuf bytes %d to %d\n",
1429 __func__, start_byte, start_byte + remainder);
1430 memcpy(bytes, buff, remainder);
1431 }
1432
1433 return 0;
1434}
1435
1436/**
Pawel Jablonskie3a5d6e2017-12-18 05:14:44 -05001437 * i40e_nvmupd_get_aq_event - Get the Admin Queue event from previous exec_aq
1438 * @hw: pointer to hardware structure
1439 * @cmd: pointer to nvm update command buffer
1440 * @bytes: pointer to the data buffer
1441 * @perrno: pointer to return error code
1442 *
1443 * cmd structure contains identifiers and data buffer
1444 **/
1445static i40e_status i40e_nvmupd_get_aq_event(struct i40e_hw *hw,
1446 struct i40e_nvm_access *cmd,
1447 u8 *bytes, int *perrno)
1448{
1449 u32 aq_total_len;
1450 u32 aq_desc_len;
1451
1452 i40e_debug(hw, I40E_DEBUG_NVM, "NVMUPD: %s\n", __func__);
1453
1454 aq_desc_len = sizeof(struct i40e_aq_desc);
1455 aq_total_len = aq_desc_len + le16_to_cpu(hw->nvm_aq_event_desc.datalen);
1456
1457 /* check copylength range */
1458 if (cmd->data_size > aq_total_len) {
1459 i40e_debug(hw, I40E_DEBUG_NVM,
1460 "%s: copy length %d too big, trimming to %d\n",
1461 __func__, cmd->data_size, aq_total_len);
1462 cmd->data_size = aq_total_len;
1463 }
1464
1465 memcpy(bytes, &hw->nvm_aq_event_desc, cmd->data_size);
1466
1467 return 0;
1468}
1469
1470/**
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001471 * i40e_nvmupd_nvm_read - Read NVM
1472 * @hw: pointer to hardware structure
1473 * @cmd: pointer to nvm update command buffer
1474 * @bytes: pointer to the data buffer
Shannon Nelson79afe832015-07-23 16:54:33 -04001475 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001476 *
1477 * cmd structure contains identifiers and data buffer
1478 **/
1479static i40e_status i40e_nvmupd_nvm_read(struct i40e_hw *hw,
1480 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -04001481 u8 *bytes, int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001482{
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001483 struct i40e_asq_cmd_details cmd_details;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001484 i40e_status status;
1485 u8 module, transaction;
1486 bool last;
1487
1488 transaction = i40e_nvmupd_get_transaction(cmd->config);
1489 module = i40e_nvmupd_get_module(cmd->config);
1490 last = (transaction == I40E_NVM_LCB) || (transaction == I40E_NVM_SA);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001491
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001492 memset(&cmd_details, 0, sizeof(cmd_details));
1493 cmd_details.wb_desc = &hw->nvm_wb_desc;
1494
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001495 status = i40e_aq_read_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001496 bytes, last, &cmd_details);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001497 if (status) {
1498 i40e_debug(hw, I40E_DEBUG_NVM,
1499 "i40e_nvmupd_nvm_read mod 0x%x off 0x%x len 0x%x\n",
1500 module, cmd->offset, cmd->data_size);
1501 i40e_debug(hw, I40E_DEBUG_NVM,
1502 "i40e_nvmupd_nvm_read status %d aq %d\n",
1503 status, hw->aq.asq_last_status);
Shannon Nelson79afe832015-07-23 16:54:33 -04001504 *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001505 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001506
1507 return status;
1508}
1509
1510/**
1511 * i40e_nvmupd_nvm_erase - Erase an NVM module
1512 * @hw: pointer to hardware structure
1513 * @cmd: pointer to nvm update command buffer
Shannon Nelson79afe832015-07-23 16:54:33 -04001514 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001515 *
1516 * module, offset, data_size and data are in cmd structure
1517 **/
1518static i40e_status i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
1519 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -04001520 int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001521{
1522 i40e_status status = 0;
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001523 struct i40e_asq_cmd_details cmd_details;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001524 u8 module, transaction;
1525 bool last;
1526
1527 transaction = i40e_nvmupd_get_transaction(cmd->config);
1528 module = i40e_nvmupd_get_module(cmd->config);
1529 last = (transaction & I40E_NVM_LCB);
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001530
1531 memset(&cmd_details, 0, sizeof(cmd_details));
1532 cmd_details.wb_desc = &hw->nvm_wb_desc;
1533
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001534 status = i40e_aq_erase_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001535 last, &cmd_details);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001536 if (status) {
1537 i40e_debug(hw, I40E_DEBUG_NVM,
1538 "i40e_nvmupd_nvm_erase mod 0x%x off 0x%x len 0x%x\n",
1539 module, cmd->offset, cmd->data_size);
1540 i40e_debug(hw, I40E_DEBUG_NVM,
1541 "i40e_nvmupd_nvm_erase status %d aq %d\n",
1542 status, hw->aq.asq_last_status);
Shannon Nelson79afe832015-07-23 16:54:33 -04001543 *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001544 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001545
1546 return status;
1547}
1548
1549/**
1550 * i40e_nvmupd_nvm_write - Write NVM
1551 * @hw: pointer to hardware structure
1552 * @cmd: pointer to nvm update command buffer
1553 * @bytes: pointer to the data buffer
Shannon Nelson79afe832015-07-23 16:54:33 -04001554 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001555 *
1556 * module, offset, data_size and data are in cmd structure
1557 **/
1558static i40e_status i40e_nvmupd_nvm_write(struct i40e_hw *hw,
1559 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -04001560 u8 *bytes, int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001561{
1562 i40e_status status = 0;
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001563 struct i40e_asq_cmd_details cmd_details;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001564 u8 module, transaction;
Pawel Jablonskie3a5d6e2017-12-18 05:14:44 -05001565 u8 preservation_flags;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001566 bool last;
1567
1568 transaction = i40e_nvmupd_get_transaction(cmd->config);
1569 module = i40e_nvmupd_get_module(cmd->config);
1570 last = (transaction & I40E_NVM_LCB);
Pawel Jablonskie3a5d6e2017-12-18 05:14:44 -05001571 preservation_flags = i40e_nvmupd_get_preservation_flags(cmd->config);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001572
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001573 memset(&cmd_details, 0, sizeof(cmd_details));
1574 cmd_details.wb_desc = &hw->nvm_wb_desc;
1575
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001576 status = i40e_aq_update_nvm(hw, module, cmd->offset,
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001577 (u16)cmd->data_size, bytes, last,
Pawel Jablonskie3a5d6e2017-12-18 05:14:44 -05001578 preservation_flags, &cmd_details);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001579 if (status) {
1580 i40e_debug(hw, I40E_DEBUG_NVM,
1581 "i40e_nvmupd_nvm_write mod 0x%x off 0x%x len 0x%x\n",
1582 module, cmd->offset, cmd->data_size);
1583 i40e_debug(hw, I40E_DEBUG_NVM,
1584 "i40e_nvmupd_nvm_write status %d aq %d\n",
1585 status, hw->aq.asq_last_status);
Shannon Nelson79afe832015-07-23 16:54:33 -04001586 *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001587 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001588
1589 return status;
1590}