blob: 26d7e9fe6220fb1a073b1c6d302273551fd1de9c [file] [log] [blame]
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +00001/*******************************************************************************
2 *
3 * Intel Ethernet Controller XL710 Family Linux Driver
Greg Rosedc641b72013-12-18 13:45:51 +00004 * Copyright(c) 2013 - 2014 Intel Corporation.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +00005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
Greg Rosedc641b72013-12-18 13:45:51 +000015 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000017 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 *
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 ******************************************************************************/
26
27#include "i40e_prototype.h"
28
29/**
Shannon Nelson3e261862014-02-06 05:51:06 +000030 * i40e_init_nvm_ops - Initialize NVM function pointers
31 * @hw: pointer to the HW structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000032 *
Shannon Nelson3e261862014-02-06 05:51:06 +000033 * Setup the function pointers and the NVM info structure. Should be called
34 * once per NVM initialization, e.g. inside the i40e_init_shared_code().
35 * Please notice that the NVM term is used here (& in all methods covered
36 * in this file) as an equivalent of the FLASH part mapped into the SR.
37 * We are accessing FLASH always thru the Shadow RAM.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000038 **/
39i40e_status i40e_init_nvm(struct i40e_hw *hw)
40{
41 struct i40e_nvm_info *nvm = &hw->nvm;
42 i40e_status ret_code = 0;
43 u32 fla, gens;
44 u8 sr_size;
45
46 /* The SR size is stored regardless of the nvm programming mode
47 * as the blank mode may be used in the factory line.
48 */
49 gens = rd32(hw, I40E_GLNVM_GENS);
50 sr_size = ((gens & I40E_GLNVM_GENS_SR_SIZE_MASK) >>
51 I40E_GLNVM_GENS_SR_SIZE_SHIFT);
Shannon Nelson3e261862014-02-06 05:51:06 +000052 /* Switching to words (sr_size contains power of 2KB) */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -040053 nvm->sr_size = BIT(sr_size) * I40E_SR_WORDS_IN_1KB;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000054
Shannon Nelson3e261862014-02-06 05:51:06 +000055 /* Check if we are in the normal or blank NVM programming mode */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000056 fla = rd32(hw, I40E_GLNVM_FLA);
Shannon Nelson3e261862014-02-06 05:51:06 +000057 if (fla & I40E_GLNVM_FLA_LOCKED_MASK) { /* Normal programming mode */
58 /* Max NVM timeout */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000059 nvm->timeout = I40E_MAX_NVM_TIMEOUT;
60 nvm->blank_nvm_mode = false;
Shannon Nelson3e261862014-02-06 05:51:06 +000061 } else { /* Blank programming mode */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000062 nvm->blank_nvm_mode = true;
63 ret_code = I40E_ERR_NVM_BLANK_MODE;
Shannon Nelson74d0d0e2014-11-13 08:23:15 +000064 i40e_debug(hw, I40E_DEBUG_NVM, "NVM init error: unsupported blank mode.\n");
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000065 }
66
67 return ret_code;
68}
69
70/**
Shannon Nelson3e261862014-02-06 05:51:06 +000071 * i40e_acquire_nvm - Generic request for acquiring the NVM ownership
72 * @hw: pointer to the HW structure
73 * @access: NVM access type (read or write)
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000074 *
Shannon Nelson3e261862014-02-06 05:51:06 +000075 * This function will request NVM ownership for reading
76 * via the proper Admin Command.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000077 **/
78i40e_status i40e_acquire_nvm(struct i40e_hw *hw,
79 enum i40e_aq_resource_access_type access)
80{
81 i40e_status ret_code = 0;
82 u64 gtime, timeout;
Shannon Nelsonc509c1d2014-11-13 08:23:19 +000083 u64 time_left = 0;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000084
85 if (hw->nvm.blank_nvm_mode)
86 goto i40e_i40e_acquire_nvm_exit;
87
88 ret_code = i40e_aq_request_resource(hw, I40E_NVM_RESOURCE_ID, access,
Shannon Nelsonc509c1d2014-11-13 08:23:19 +000089 0, &time_left, NULL);
Shannon Nelson3e261862014-02-06 05:51:06 +000090 /* Reading the Global Device Timer */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000091 gtime = rd32(hw, I40E_GLVFGEN_TIMER);
92
Shannon Nelson3e261862014-02-06 05:51:06 +000093 /* Store the timeout */
Shannon Nelsonc509c1d2014-11-13 08:23:19 +000094 hw->nvm.hw_semaphore_timeout = I40E_MS_TO_GTIME(time_left) + gtime;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000095
Shannon Nelsona3f0b382014-11-13 08:23:21 +000096 if (ret_code)
97 i40e_debug(hw, I40E_DEBUG_NVM,
98 "NVM acquire type %d failed time_left=%llu ret=%d aq_err=%d\n",
99 access, time_left, ret_code, hw->aq.asq_last_status);
100
101 if (ret_code && time_left) {
Shannon Nelson3e261862014-02-06 05:51:06 +0000102 /* Poll until the current NVM owner timeouts */
Shannon Nelsonc509c1d2014-11-13 08:23:19 +0000103 timeout = I40E_MS_TO_GTIME(I40E_MAX_NVM_TIMEOUT) + gtime;
Shannon Nelsona3f0b382014-11-13 08:23:21 +0000104 while ((gtime < timeout) && time_left) {
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000105 usleep_range(10000, 20000);
Shannon Nelsonc509c1d2014-11-13 08:23:19 +0000106 gtime = rd32(hw, I40E_GLVFGEN_TIMER);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000107 ret_code = i40e_aq_request_resource(hw,
108 I40E_NVM_RESOURCE_ID,
Shannon Nelsonc509c1d2014-11-13 08:23:19 +0000109 access, 0, &time_left,
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000110 NULL);
111 if (!ret_code) {
112 hw->nvm.hw_semaphore_timeout =
Shannon Nelsonc509c1d2014-11-13 08:23:19 +0000113 I40E_MS_TO_GTIME(time_left) + gtime;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000114 break;
115 }
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000116 }
117 if (ret_code) {
118 hw->nvm.hw_semaphore_timeout = 0;
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000119 i40e_debug(hw, I40E_DEBUG_NVM,
Shannon Nelsona3f0b382014-11-13 08:23:21 +0000120 "NVM acquire timed out, wait %llu ms before trying again. status=%d aq_err=%d\n",
121 time_left, ret_code, hw->aq.asq_last_status);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000122 }
123 }
124
125i40e_i40e_acquire_nvm_exit:
126 return ret_code;
127}
128
129/**
Shannon Nelson3e261862014-02-06 05:51:06 +0000130 * i40e_release_nvm - Generic request for releasing the NVM ownership
131 * @hw: pointer to the HW structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000132 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000133 * This function will release NVM resource via the proper Admin Command.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000134 **/
135void i40e_release_nvm(struct i40e_hw *hw)
136{
137 if (!hw->nvm.blank_nvm_mode)
138 i40e_aq_release_resource(hw, I40E_NVM_RESOURCE_ID, 0, NULL);
139}
140
141/**
Shannon Nelson3e261862014-02-06 05:51:06 +0000142 * i40e_poll_sr_srctl_done_bit - Polls the GLNVM_SRCTL done bit
143 * @hw: pointer to the HW structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000144 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000145 * Polls the SRCTL Shadow RAM register done bit.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000146 **/
147static i40e_status i40e_poll_sr_srctl_done_bit(struct i40e_hw *hw)
148{
149 i40e_status ret_code = I40E_ERR_TIMEOUT;
150 u32 srctl, wait_cnt;
151
Shannon Nelson3e261862014-02-06 05:51:06 +0000152 /* Poll the I40E_GLNVM_SRCTL until the done bit is set */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000153 for (wait_cnt = 0; wait_cnt < I40E_SRRD_SRCTL_ATTEMPTS; wait_cnt++) {
154 srctl = rd32(hw, I40E_GLNVM_SRCTL);
155 if (srctl & I40E_GLNVM_SRCTL_DONE_MASK) {
156 ret_code = 0;
157 break;
158 }
159 udelay(5);
160 }
161 if (ret_code == I40E_ERR_TIMEOUT)
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000162 i40e_debug(hw, I40E_DEBUG_NVM, "Done bit in GLNVM_SRCTL not set");
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000163 return ret_code;
164}
165
166/**
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000167 * i40e_read_nvm_word_srctl - Reads Shadow RAM via SRCTL register
Shannon Nelson3e261862014-02-06 05:51:06 +0000168 * @hw: pointer to the HW structure
169 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
170 * @data: word read from the Shadow RAM
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000171 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000172 * Reads one 16 bit word from the Shadow RAM using the GLNVM_SRCTL register.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000173 **/
Shannon Nelson37a29732015-02-27 09:15:19 +0000174static i40e_status i40e_read_nvm_word_srctl(struct i40e_hw *hw, u16 offset,
175 u16 *data)
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000176{
177 i40e_status ret_code = I40E_ERR_TIMEOUT;
178 u32 sr_reg;
179
180 if (offset >= hw->nvm.sr_size) {
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000181 i40e_debug(hw, I40E_DEBUG_NVM,
182 "NVM read error: offset %d beyond Shadow RAM limit %d\n",
183 offset, hw->nvm.sr_size);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000184 ret_code = I40E_ERR_PARAM;
185 goto read_nvm_exit;
186 }
187
Shannon Nelson3e261862014-02-06 05:51:06 +0000188 /* Poll the done bit first */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000189 ret_code = i40e_poll_sr_srctl_done_bit(hw);
190 if (!ret_code) {
Shannon Nelson3e261862014-02-06 05:51:06 +0000191 /* Write the address and start reading */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400192 sr_reg = ((u32)offset << I40E_GLNVM_SRCTL_ADDR_SHIFT) |
193 BIT(I40E_GLNVM_SRCTL_START_SHIFT);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000194 wr32(hw, I40E_GLNVM_SRCTL, sr_reg);
195
Shannon Nelson3e261862014-02-06 05:51:06 +0000196 /* Poll I40E_GLNVM_SRCTL until the done bit is set */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000197 ret_code = i40e_poll_sr_srctl_done_bit(hw);
198 if (!ret_code) {
199 sr_reg = rd32(hw, I40E_GLNVM_SRDATA);
200 *data = (u16)((sr_reg &
201 I40E_GLNVM_SRDATA_RDDATA_MASK)
202 >> I40E_GLNVM_SRDATA_RDDATA_SHIFT);
203 }
204 }
205 if (ret_code)
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000206 i40e_debug(hw, I40E_DEBUG_NVM,
207 "NVM read error: Couldn't access Shadow RAM address: 0x%x\n",
208 offset);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000209
210read_nvm_exit:
211 return ret_code;
212}
213
214/**
Shannon Nelson7073f462015-06-05 12:20:34 -0400215 * i40e_read_nvm_aq - Read Shadow RAM.
216 * @hw: pointer to the HW structure.
217 * @module_pointer: module pointer location in words from the NVM beginning
218 * @offset: offset in words from module start
219 * @words: number of words to write
220 * @data: buffer with words to write to the Shadow RAM
221 * @last_command: tells the AdminQ that this is the last command
222 *
223 * Writes a 16 bit words buffer to the Shadow RAM using the admin command.
224 **/
225static i40e_status i40e_read_nvm_aq(struct i40e_hw *hw, u8 module_pointer,
226 u32 offset, u16 words, void *data,
227 bool last_command)
228{
229 i40e_status ret_code = I40E_ERR_NVM;
230 struct i40e_asq_cmd_details cmd_details;
231
232 memset(&cmd_details, 0, sizeof(cmd_details));
Jacob Keller3c8f3e92017-09-01 13:43:08 -0700233 cmd_details.wb_desc = &hw->nvm_wb_desc;
Shannon Nelson7073f462015-06-05 12:20:34 -0400234
235 /* Here we are checking the SR limit only for the flat memory model.
236 * We cannot do it for the module-based model, as we did not acquire
237 * the NVM resource yet (we cannot get the module pointer value).
238 * Firmware will check the module-based model.
239 */
240 if ((offset + words) > hw->nvm.sr_size)
241 i40e_debug(hw, I40E_DEBUG_NVM,
242 "NVM write error: offset %d beyond Shadow RAM limit %d\n",
243 (offset + words), hw->nvm.sr_size);
244 else if (words > I40E_SR_SECTOR_SIZE_IN_WORDS)
245 /* We can write only up to 4KB (one sector), in one AQ write */
246 i40e_debug(hw, I40E_DEBUG_NVM,
247 "NVM write fail error: tried to write %d words, limit is %d.\n",
248 words, I40E_SR_SECTOR_SIZE_IN_WORDS);
249 else if (((offset + (words - 1)) / I40E_SR_SECTOR_SIZE_IN_WORDS)
250 != (offset / I40E_SR_SECTOR_SIZE_IN_WORDS))
251 /* A single write cannot spread over two sectors */
252 i40e_debug(hw, I40E_DEBUG_NVM,
253 "NVM write error: cannot spread over two sectors in a single write offset=%d words=%d\n",
254 offset, words);
255 else
256 ret_code = i40e_aq_read_nvm(hw, module_pointer,
257 2 * offset, /*bytes*/
258 2 * words, /*bytes*/
259 data, last_command, &cmd_details);
260
261 return ret_code;
262}
263
264/**
265 * i40e_read_nvm_word_aq - Reads Shadow RAM via AQ
266 * @hw: pointer to the HW structure
267 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
268 * @data: word read from the Shadow RAM
269 *
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700270 * Reads one 16 bit word from the Shadow RAM using the AdminQ
Shannon Nelson7073f462015-06-05 12:20:34 -0400271 **/
272static i40e_status i40e_read_nvm_word_aq(struct i40e_hw *hw, u16 offset,
273 u16 *data)
274{
275 i40e_status ret_code = I40E_ERR_TIMEOUT;
276
277 ret_code = i40e_read_nvm_aq(hw, 0x0, offset, 1, data, true);
278 *data = le16_to_cpu(*(__le16 *)data);
279
280 return ret_code;
281}
282
283/**
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700284 * __i40e_read_nvm_word - Reads nvm word, assumes called does the locking
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000285 * @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.
290 *
291 * Do not use this function except in cases where the nvm lock is already
292 * taken via i40e_acquire_nvm().
293 **/
294static i40e_status __i40e_read_nvm_word(struct i40e_hw *hw,
295 u16 offset, u16 *data)
296{
297 i40e_status ret_code = 0;
298
299 if (hw->flags & I40E_HW_FLAG_AQ_SRCTL_ACCESS_ENABLE)
300 ret_code = i40e_read_nvm_word_aq(hw, offset, data);
301 else
302 ret_code = i40e_read_nvm_word_srctl(hw, offset, data);
303 return ret_code;
304}
305
306/**
307 * i40e_read_nvm_word - Reads nvm word and acquire lock if necessary
308 * @hw: pointer to the HW structure
309 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
310 * @data: word read from the Shadow RAM
311 *
312 * Reads one 16 bit word from the Shadow RAM.
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000313 **/
314i40e_status i40e_read_nvm_word(struct i40e_hw *hw, u16 offset,
315 u16 *data)
316{
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700317 i40e_status ret_code = 0;
Anjali Singhai07f89be2015-09-24 15:26:32 -0700318
Aaron Salter96a39ae2016-12-02 12:33:02 -0800319 ret_code = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700320 if (ret_code)
321 return ret_code;
322
323 ret_code = __i40e_read_nvm_word(hw, offset, data);
324
325 i40e_release_nvm(hw);
326
Anjali Singhai07f89be2015-09-24 15:26:32 -0700327 return ret_code;
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000328}
329
330/**
331 * i40e_read_nvm_buffer_srctl - Reads Shadow RAM buffer via SRCTL register
332 * @hw: pointer to the HW structure
333 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
334 * @words: (in) number of words to read; (out) number of words actually read
335 * @data: words read from the Shadow RAM
336 *
337 * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_srrd()
338 * method. The buffer read is preceded by the NVM ownership take
339 * and followed by the release.
340 **/
Shannon Nelson37a29732015-02-27 09:15:19 +0000341static i40e_status i40e_read_nvm_buffer_srctl(struct i40e_hw *hw, u16 offset,
342 u16 *words, u16 *data)
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000343{
344 i40e_status ret_code = 0;
345 u16 index, word;
346
347 /* Loop thru the selected region */
348 for (word = 0; word < *words; word++) {
349 index = offset + word;
350 ret_code = i40e_read_nvm_word_srctl(hw, index, &data[word]);
351 if (ret_code)
352 break;
353 }
354
355 /* Update the number of words read from the Shadow RAM */
356 *words = word;
357
358 return ret_code;
359}
360
361/**
Shannon Nelson7073f462015-06-05 12:20:34 -0400362 * i40e_read_nvm_buffer_aq - Reads Shadow RAM buffer via AQ
363 * @hw: pointer to the HW structure
364 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
365 * @words: (in) number of words to read; (out) number of words actually read
366 * @data: words read from the Shadow RAM
367 *
368 * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_aq()
369 * method. The buffer read is preceded by the NVM ownership take
370 * and followed by the release.
371 **/
372static i40e_status i40e_read_nvm_buffer_aq(struct i40e_hw *hw, u16 offset,
373 u16 *words, u16 *data)
374{
375 i40e_status ret_code;
376 u16 read_size = *words;
377 bool last_cmd = false;
378 u16 words_read = 0;
379 u16 i = 0;
380
381 do {
382 /* Calculate number of bytes we should read in this step.
383 * FVL AQ do not allow to read more than one page at a time or
384 * to cross page boundaries.
385 */
386 if (offset % I40E_SR_SECTOR_SIZE_IN_WORDS)
387 read_size = min(*words,
388 (u16)(I40E_SR_SECTOR_SIZE_IN_WORDS -
389 (offset % I40E_SR_SECTOR_SIZE_IN_WORDS)));
390 else
391 read_size = min((*words - words_read),
392 I40E_SR_SECTOR_SIZE_IN_WORDS);
393
394 /* Check if this is last command, if so set proper flag */
395 if ((words_read + read_size) >= *words)
396 last_cmd = true;
397
398 ret_code = i40e_read_nvm_aq(hw, 0x0, offset, read_size,
399 data + words_read, last_cmd);
400 if (ret_code)
401 goto read_nvm_buffer_aq_exit;
402
403 /* Increment counter for words already read and move offset to
404 * new read location
405 */
406 words_read += read_size;
407 offset += read_size;
408 } while (words_read < *words);
409
410 for (i = 0; i < *words; i++)
411 data[i] = le16_to_cpu(((__le16 *)data)[i]);
412
413read_nvm_buffer_aq_exit:
414 *words = words_read;
415 return ret_code;
416}
417
418/**
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700419 * __i40e_read_nvm_buffer - Reads nvm buffer, caller must acquire lock
Shannon Nelson3e261862014-02-06 05:51:06 +0000420 * @hw: pointer to the HW structure
421 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
422 * @words: (in) number of words to read; (out) number of words actually read
423 * @data: words read from the Shadow RAM
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000424 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000425 * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_srrd()
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700426 * method.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000427 **/
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700428static i40e_status __i40e_read_nvm_buffer(struct i40e_hw *hw,
429 u16 offset, u16 *words,
430 u16 *data)
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000431{
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700432 i40e_status ret_code = 0;
Anjali Singhai07f89be2015-09-24 15:26:32 -0700433
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700434 if (hw->flags & I40E_HW_FLAG_AQ_SRCTL_ACCESS_ENABLE)
435 ret_code = i40e_read_nvm_buffer_aq(hw, offset, words, data);
436 else
Anjali Singhai07f89be2015-09-24 15:26:32 -0700437 ret_code = i40e_read_nvm_buffer_srctl(hw, offset, words, data);
Anjali Singhai07f89be2015-09-24 15:26:32 -0700438 return ret_code;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000439}
440
441/**
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000442 * i40e_write_nvm_aq - Writes Shadow RAM.
443 * @hw: pointer to the HW structure.
444 * @module_pointer: module pointer location in words from the NVM beginning
445 * @offset: offset in words from module start
446 * @words: number of words to write
447 * @data: buffer with words to write to the Shadow RAM
448 * @last_command: tells the AdminQ that this is the last command
449 *
450 * Writes a 16 bit words buffer to the Shadow RAM using the admin command.
451 **/
Wei Yongjun952d9632014-07-30 09:02:53 +0000452static i40e_status i40e_write_nvm_aq(struct i40e_hw *hw, u8 module_pointer,
453 u32 offset, u16 words, void *data,
454 bool last_command)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000455{
456 i40e_status ret_code = I40E_ERR_NVM;
Shannon Nelson6b5c1b82015-08-28 17:55:47 -0400457 struct i40e_asq_cmd_details cmd_details;
458
459 memset(&cmd_details, 0, sizeof(cmd_details));
460 cmd_details.wb_desc = &hw->nvm_wb_desc;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000461
462 /* Here we are checking the SR limit only for the flat memory model.
463 * We cannot do it for the module-based model, as we did not acquire
464 * the NVM resource yet (we cannot get the module pointer value).
465 * Firmware will check the module-based model.
466 */
467 if ((offset + words) > hw->nvm.sr_size)
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000468 i40e_debug(hw, I40E_DEBUG_NVM,
469 "NVM write error: offset %d beyond Shadow RAM limit %d\n",
470 (offset + words), hw->nvm.sr_size);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000471 else if (words > I40E_SR_SECTOR_SIZE_IN_WORDS)
472 /* We can write only up to 4KB (one sector), in one AQ write */
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000473 i40e_debug(hw, I40E_DEBUG_NVM,
474 "NVM write fail error: tried to write %d words, limit is %d.\n",
475 words, I40E_SR_SECTOR_SIZE_IN_WORDS);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000476 else if (((offset + (words - 1)) / I40E_SR_SECTOR_SIZE_IN_WORDS)
477 != (offset / I40E_SR_SECTOR_SIZE_IN_WORDS))
478 /* A single write cannot spread over two sectors */
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000479 i40e_debug(hw, I40E_DEBUG_NVM,
480 "NVM write error: cannot spread over two sectors in a single write offset=%d words=%d\n",
481 offset, words);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000482 else
483 ret_code = i40e_aq_update_nvm(hw, module_pointer,
484 2 * offset, /*bytes*/
485 2 * words, /*bytes*/
Shannon Nelson6b5c1b82015-08-28 17:55:47 -0400486 data, last_command, &cmd_details);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000487
488 return ret_code;
489}
490
491/**
Shannon Nelson3e261862014-02-06 05:51:06 +0000492 * i40e_calc_nvm_checksum - Calculates and returns the checksum
493 * @hw: pointer to hardware structure
494 * @checksum: pointer to the checksum
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000495 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000496 * This function calculates SW Checksum that covers the whole 64kB shadow RAM
497 * except the VPD and PCIe ALT Auto-load modules. The structure and size of VPD
498 * is customer specific and unknown. Therefore, this function skips all maximum
499 * possible size of VPD (1kB).
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000500 **/
501static i40e_status i40e_calc_nvm_checksum(struct i40e_hw *hw,
502 u16 *checksum)
503{
Jean Sacren0e5229c2015-10-13 01:06:31 -0600504 i40e_status ret_code;
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000505 struct i40e_virt_mem vmem;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000506 u16 pcie_alt_module = 0;
507 u16 checksum_local = 0;
508 u16 vpd_module = 0;
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000509 u16 *data;
510 u16 i = 0;
511
512 ret_code = i40e_allocate_virt_mem(hw, &vmem,
513 I40E_SR_SECTOR_SIZE_IN_WORDS * sizeof(u16));
514 if (ret_code)
515 goto i40e_calc_nvm_checksum_exit;
516 data = (u16 *)vmem.va;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000517
518 /* read pointer to VPD area */
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700519 ret_code = __i40e_read_nvm_word(hw, I40E_SR_VPD_PTR, &vpd_module);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000520 if (ret_code) {
521 ret_code = I40E_ERR_NVM_CHECKSUM;
522 goto i40e_calc_nvm_checksum_exit;
523 }
524
525 /* read pointer to PCIe Alt Auto-load module */
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700526 ret_code = __i40e_read_nvm_word(hw, I40E_SR_PCIE_ALT_AUTO_LOAD_PTR,
527 &pcie_alt_module);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000528 if (ret_code) {
529 ret_code = I40E_ERR_NVM_CHECKSUM;
530 goto i40e_calc_nvm_checksum_exit;
531 }
532
533 /* Calculate SW checksum that covers the whole 64kB shadow RAM
534 * except the VPD and PCIe ALT Auto-load modules
535 */
536 for (i = 0; i < hw->nvm.sr_size; i++) {
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000537 /* Read SR page */
538 if ((i % I40E_SR_SECTOR_SIZE_IN_WORDS) == 0) {
539 u16 words = I40E_SR_SECTOR_SIZE_IN_WORDS;
540
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700541 ret_code = __i40e_read_nvm_buffer(hw, i, &words, data);
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000542 if (ret_code) {
543 ret_code = I40E_ERR_NVM_CHECKSUM;
544 goto i40e_calc_nvm_checksum_exit;
545 }
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000546 }
547
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000548 /* Skip Checksum word */
549 if (i == I40E_SR_SW_CHECKSUM_WORD)
550 continue;
551 /* Skip VPD module (convert byte size to word count) */
552 if ((i >= (u32)vpd_module) &&
553 (i < ((u32)vpd_module +
554 (I40E_SR_VPD_MODULE_MAX_SIZE / 2)))) {
555 continue;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000556 }
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000557 /* Skip PCIe ALT module (convert byte size to word count) */
558 if ((i >= (u32)pcie_alt_module) &&
559 (i < ((u32)pcie_alt_module +
560 (I40E_SR_PCIE_ALT_MODULE_MAX_SIZE / 2)))) {
561 continue;
562 }
563
564 checksum_local += data[i % I40E_SR_SECTOR_SIZE_IN_WORDS];
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000565 }
566
567 *checksum = (u16)I40E_SR_SW_CHECKSUM_BASE - checksum_local;
568
569i40e_calc_nvm_checksum_exit:
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000570 i40e_free_virt_mem(hw, &vmem);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000571 return ret_code;
572}
573
574/**
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000575 * i40e_update_nvm_checksum - Updates the NVM checksum
576 * @hw: pointer to hardware structure
577 *
578 * NVM ownership must be acquired before calling this function and released
579 * on ARQ completion event reception by caller.
580 * This function will commit SR to NVM.
581 **/
582i40e_status i40e_update_nvm_checksum(struct i40e_hw *hw)
583{
Jean Sacren0e5229c2015-10-13 01:06:31 -0600584 i40e_status ret_code;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000585 u16 checksum;
Jesse Brandeburgdd38c582015-08-26 15:14:18 -0400586 __le16 le_sum;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000587
588 ret_code = i40e_calc_nvm_checksum(hw, &checksum);
Jean Sacren2fc4cd52015-10-13 01:06:32 -0600589 if (!ret_code) {
590 le_sum = cpu_to_le16(checksum);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000591 ret_code = i40e_write_nvm_aq(hw, 0x00, I40E_SR_SW_CHECKSUM_WORD,
Jesse Brandeburgdd38c582015-08-26 15:14:18 -0400592 1, &le_sum, true);
Jean Sacren2fc4cd52015-10-13 01:06:32 -0600593 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000594
595 return ret_code;
596}
597
598/**
Shannon Nelson3e261862014-02-06 05:51:06 +0000599 * i40e_validate_nvm_checksum - Validate EEPROM checksum
600 * @hw: pointer to hardware structure
601 * @checksum: calculated checksum
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000602 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000603 * Performs checksum calculation and validates the NVM SW checksum. If the
604 * caller does not need checksum, the value can be NULL.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000605 **/
606i40e_status i40e_validate_nvm_checksum(struct i40e_hw *hw,
607 u16 *checksum)
608{
609 i40e_status ret_code = 0;
610 u16 checksum_sr = 0;
Jesse Brandeburge15c9fa2014-01-17 15:36:31 -0800611 u16 checksum_local = 0;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000612
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700613 /* We must acquire the NVM lock in order to correctly synchronize the
614 * NVM accesses across multiple PFs. Without doing so it is possible
615 * for one of the PFs to read invalid data potentially indicating that
616 * the checksum is invalid.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000617 */
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -0700618 ret_code = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
619 if (ret_code)
620 return ret_code;
621 ret_code = i40e_calc_nvm_checksum(hw, &checksum_local);
622 __i40e_read_nvm_word(hw, I40E_SR_SW_CHECKSUM_WORD, &checksum_sr);
623 i40e_release_nvm(hw);
624 if (ret_code)
625 return ret_code;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000626
627 /* Verify read checksum from EEPROM is the same as
628 * calculated checksum
629 */
630 if (checksum_local != checksum_sr)
631 ret_code = I40E_ERR_NVM_CHECKSUM;
632
633 /* If the user cares, return the calculated checksum */
634 if (checksum)
635 *checksum = checksum_local;
636
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000637 return ret_code;
638}
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000639
640static i40e_status i40e_nvmupd_state_init(struct i40e_hw *hw,
641 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400642 u8 *bytes, int *perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000643static i40e_status i40e_nvmupd_state_reading(struct i40e_hw *hw,
644 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400645 u8 *bytes, int *perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000646static i40e_status i40e_nvmupd_state_writing(struct i40e_hw *hw,
647 struct i40e_nvm_access *cmd,
648 u8 *bytes, int *errno);
649static enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
650 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400651 int *perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000652static i40e_status i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
653 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400654 int *perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000655static i40e_status i40e_nvmupd_nvm_write(struct i40e_hw *hw,
656 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400657 u8 *bytes, int *perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000658static i40e_status i40e_nvmupd_nvm_read(struct i40e_hw *hw,
659 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400660 u8 *bytes, int *perrno);
Shannon Nelsone4c83c22015-08-28 17:55:50 -0400661static i40e_status i40e_nvmupd_exec_aq(struct i40e_hw *hw,
662 struct i40e_nvm_access *cmd,
663 u8 *bytes, int *perrno);
Shannon Nelsonb72dc7b2015-08-28 17:55:51 -0400664static i40e_status i40e_nvmupd_get_aq_result(struct i40e_hw *hw,
665 struct i40e_nvm_access *cmd,
666 u8 *bytes, int *perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000667static inline u8 i40e_nvmupd_get_module(u32 val)
668{
669 return (u8)(val & I40E_NVM_MOD_PNT_MASK);
670}
671static inline u8 i40e_nvmupd_get_transaction(u32 val)
672{
673 return (u8)((val & I40E_NVM_TRANS_MASK) >> I40E_NVM_TRANS_SHIFT);
674}
675
Jingjing Wu4e68adfe2015-09-28 14:12:31 -0400676static const char * const i40e_nvm_update_state_str[] = {
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000677 "I40E_NVMUPD_INVALID",
678 "I40E_NVMUPD_READ_CON",
679 "I40E_NVMUPD_READ_SNT",
680 "I40E_NVMUPD_READ_LCB",
681 "I40E_NVMUPD_READ_SA",
682 "I40E_NVMUPD_WRITE_ERA",
683 "I40E_NVMUPD_WRITE_CON",
684 "I40E_NVMUPD_WRITE_SNT",
685 "I40E_NVMUPD_WRITE_LCB",
686 "I40E_NVMUPD_WRITE_SA",
687 "I40E_NVMUPD_CSUM_CON",
688 "I40E_NVMUPD_CSUM_SA",
689 "I40E_NVMUPD_CSUM_LCB",
Shannon Nelson0af8e9d2015-08-28 17:55:48 -0400690 "I40E_NVMUPD_STATUS",
Shannon Nelsone4c83c22015-08-28 17:55:50 -0400691 "I40E_NVMUPD_EXEC_AQ",
Shannon Nelsonb72dc7b2015-08-28 17:55:51 -0400692 "I40E_NVMUPD_GET_AQ_RESULT",
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000693};
694
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000695/**
696 * i40e_nvmupd_command - Process an NVM update command
697 * @hw: pointer to hardware structure
698 * @cmd: pointer to nvm update command
699 * @bytes: pointer to the data buffer
Shannon Nelson79afe832015-07-23 16:54:33 -0400700 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000701 *
702 * Dispatches command depending on what update state is current
703 **/
704i40e_status i40e_nvmupd_command(struct i40e_hw *hw,
705 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400706 u8 *bytes, int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000707{
708 i40e_status status;
Shannon Nelson0af8e9d2015-08-28 17:55:48 -0400709 enum i40e_nvmupd_cmd upd_cmd;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000710
711 /* assume success */
Shannon Nelson79afe832015-07-23 16:54:33 -0400712 *perrno = 0;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000713
Shannon Nelson0af8e9d2015-08-28 17:55:48 -0400714 /* early check for status command and debug msgs */
715 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
716
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700717 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 -0400718 i40e_nvm_update_state_str[upd_cmd],
719 hw->nvmupd_state,
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700720 hw->nvm_release_on_done, hw->nvm_wait_opcode,
Shannon Nelson1d73b2d2015-12-23 12:05:51 -0800721 cmd->command, cmd->config, cmd->offset, cmd->data_size);
Shannon Nelson0af8e9d2015-08-28 17:55:48 -0400722
723 if (upd_cmd == I40E_NVMUPD_INVALID) {
724 *perrno = -EFAULT;
725 i40e_debug(hw, I40E_DEBUG_NVM,
726 "i40e_nvmupd_validate_command returns %d errno %d\n",
727 upd_cmd, *perrno);
728 }
729
730 /* a status request returns immediately rather than
731 * going into the state machine
732 */
733 if (upd_cmd == I40E_NVMUPD_STATUS) {
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700734 if (!cmd->data_size) {
735 *perrno = -EFAULT;
736 return I40E_ERR_BUF_TOO_SHORT;
737 }
738
Shannon Nelson0af8e9d2015-08-28 17:55:48 -0400739 bytes[0] = hw->nvmupd_state;
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700740
741 if (cmd->data_size >= 4) {
742 bytes[1] = 0;
743 *((u16 *)&bytes[2]) = hw->nvm_wait_opcode;
744 }
745
Maciej Sosin81fa7c92016-10-11 15:26:57 -0700746 /* Clear error status on read */
747 if (hw->nvmupd_state == I40E_NVMUPD_STATE_ERROR)
748 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
749
Shannon Nelson0af8e9d2015-08-28 17:55:48 -0400750 return 0;
751 }
752
Maciej Sosin81fa7c92016-10-11 15:26:57 -0700753 /* Clear status even it is not read and log */
754 if (hw->nvmupd_state == I40E_NVMUPD_STATE_ERROR) {
755 i40e_debug(hw, I40E_DEBUG_NVM,
756 "Clearing I40E_NVMUPD_STATE_ERROR state without reading\n");
757 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
758 }
759
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000760 switch (hw->nvmupd_state) {
761 case I40E_NVMUPD_STATE_INIT:
Shannon Nelson79afe832015-07-23 16:54:33 -0400762 status = i40e_nvmupd_state_init(hw, cmd, bytes, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000763 break;
764
765 case I40E_NVMUPD_STATE_READING:
Shannon Nelson79afe832015-07-23 16:54:33 -0400766 status = i40e_nvmupd_state_reading(hw, cmd, bytes, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000767 break;
768
769 case I40E_NVMUPD_STATE_WRITING:
Shannon Nelson79afe832015-07-23 16:54:33 -0400770 status = i40e_nvmupd_state_writing(hw, cmd, bytes, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000771 break;
772
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400773 case I40E_NVMUPD_STATE_INIT_WAIT:
774 case I40E_NVMUPD_STATE_WRITE_WAIT:
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700775 /* if we need to stop waiting for an event, clear
776 * the wait info and return before doing anything else
777 */
778 if (cmd->offset == 0xffff) {
779 i40e_nvmupd_check_wait_event(hw, hw->nvm_wait_opcode);
780 return 0;
781 }
782
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400783 status = I40E_ERR_NOT_READY;
784 *perrno = -EBUSY;
785 break;
786
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000787 default:
788 /* invalid state, should never happen */
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000789 i40e_debug(hw, I40E_DEBUG_NVM,
790 "NVMUPD: no such state %d\n", hw->nvmupd_state);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000791 status = I40E_NOT_SUPPORTED;
Shannon Nelson79afe832015-07-23 16:54:33 -0400792 *perrno = -ESRCH;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000793 break;
794 }
795 return status;
796}
797
798/**
799 * i40e_nvmupd_state_init - Handle NVM update state Init
800 * @hw: pointer to hardware structure
801 * @cmd: pointer to nvm update command buffer
802 * @bytes: pointer to the data buffer
Shannon Nelson79afe832015-07-23 16:54:33 -0400803 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000804 *
805 * Process legitimate commands of the Init state and conditionally set next
806 * state. Reject all other commands.
807 **/
808static i40e_status i40e_nvmupd_state_init(struct i40e_hw *hw,
809 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400810 u8 *bytes, int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000811{
812 i40e_status status = 0;
813 enum i40e_nvmupd_cmd upd_cmd;
814
Shannon Nelson79afe832015-07-23 16:54:33 -0400815 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000816
817 switch (upd_cmd) {
818 case I40E_NVMUPD_READ_SA:
819 status = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
820 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -0400821 *perrno = i40e_aq_rc_to_posix(status,
Shannon Nelsonbf848f32014-11-13 08:23:22 +0000822 hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000823 } else {
Shannon Nelson79afe832015-07-23 16:54:33 -0400824 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000825 i40e_release_nvm(hw);
826 }
827 break;
828
829 case I40E_NVMUPD_READ_SNT:
830 status = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
831 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -0400832 *perrno = i40e_aq_rc_to_posix(status,
Shannon Nelsonbf848f32014-11-13 08:23:22 +0000833 hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000834 } else {
Shannon Nelson79afe832015-07-23 16:54:33 -0400835 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
Shannon Nelson0fdd0522014-11-13 08:23:20 +0000836 if (status)
837 i40e_release_nvm(hw);
838 else
839 hw->nvmupd_state = I40E_NVMUPD_STATE_READING;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000840 }
841 break;
842
843 case I40E_NVMUPD_WRITE_ERA:
844 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
845 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -0400846 *perrno = i40e_aq_rc_to_posix(status,
Shannon Nelsonbf848f32014-11-13 08:23:22 +0000847 hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000848 } else {
Shannon Nelson79afe832015-07-23 16:54:33 -0400849 status = i40e_nvmupd_nvm_erase(hw, cmd, perrno);
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400850 if (status) {
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000851 i40e_release_nvm(hw);
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400852 } else {
Shannon Nelson437f82a2016-04-01 03:56:09 -0700853 hw->nvm_release_on_done = true;
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700854 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_erase;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400855 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
856 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000857 }
858 break;
859
860 case I40E_NVMUPD_WRITE_SA:
861 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
862 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -0400863 *perrno = i40e_aq_rc_to_posix(status,
Shannon Nelsonbf848f32014-11-13 08:23:22 +0000864 hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000865 } else {
Shannon Nelson79afe832015-07-23 16:54:33 -0400866 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400867 if (status) {
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000868 i40e_release_nvm(hw);
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400869 } else {
Shannon Nelson437f82a2016-04-01 03:56:09 -0700870 hw->nvm_release_on_done = true;
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700871 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400872 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
873 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000874 }
875 break;
876
877 case I40E_NVMUPD_WRITE_SNT:
878 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
879 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -0400880 *perrno = i40e_aq_rc_to_posix(status,
Shannon Nelsonbf848f32014-11-13 08:23:22 +0000881 hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000882 } else {
Shannon Nelson79afe832015-07-23 16:54:33 -0400883 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700884 if (status) {
Shannon Nelson0fdd0522014-11-13 08:23:20 +0000885 i40e_release_nvm(hw);
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700886 } else {
887 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400888 hw->nvmupd_state = I40E_NVMUPD_STATE_WRITE_WAIT;
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700889 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000890 }
891 break;
892
893 case I40E_NVMUPD_CSUM_SA:
894 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
895 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -0400896 *perrno = i40e_aq_rc_to_posix(status,
Shannon Nelsonbf848f32014-11-13 08:23:22 +0000897 hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000898 } else {
899 status = i40e_update_nvm_checksum(hw);
900 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -0400901 *perrno = hw->aq.asq_last_status ?
Shannon Nelsonbf848f32014-11-13 08:23:22 +0000902 i40e_aq_rc_to_posix(status,
903 hw->aq.asq_last_status) :
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000904 -EIO;
905 i40e_release_nvm(hw);
906 } else {
Shannon Nelson437f82a2016-04-01 03:56:09 -0700907 hw->nvm_release_on_done = true;
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700908 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400909 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000910 }
911 }
912 break;
913
Shannon Nelsone4c83c22015-08-28 17:55:50 -0400914 case I40E_NVMUPD_EXEC_AQ:
915 status = i40e_nvmupd_exec_aq(hw, cmd, bytes, perrno);
916 break;
917
Shannon Nelsonb72dc7b2015-08-28 17:55:51 -0400918 case I40E_NVMUPD_GET_AQ_RESULT:
919 status = i40e_nvmupd_get_aq_result(hw, cmd, bytes, perrno);
920 break;
921
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000922 default:
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000923 i40e_debug(hw, I40E_DEBUG_NVM,
924 "NVMUPD: bad cmd %s in init state\n",
925 i40e_nvm_update_state_str[upd_cmd]);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000926 status = I40E_ERR_NVM;
Shannon Nelson79afe832015-07-23 16:54:33 -0400927 *perrno = -ESRCH;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000928 break;
929 }
930 return status;
931}
932
933/**
934 * i40e_nvmupd_state_reading - Handle NVM update state Reading
935 * @hw: pointer to hardware structure
936 * @cmd: pointer to nvm update command buffer
937 * @bytes: pointer to the data buffer
Shannon Nelson79afe832015-07-23 16:54:33 -0400938 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000939 *
940 * NVM ownership is already held. Process legitimate commands and set any
941 * change in state; reject all other commands.
942 **/
943static i40e_status i40e_nvmupd_state_reading(struct i40e_hw *hw,
944 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400945 u8 *bytes, int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000946{
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400947 i40e_status status = 0;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000948 enum i40e_nvmupd_cmd upd_cmd;
949
Shannon Nelson79afe832015-07-23 16:54:33 -0400950 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000951
952 switch (upd_cmd) {
953 case I40E_NVMUPD_READ_SA:
954 case I40E_NVMUPD_READ_CON:
Shannon Nelson79afe832015-07-23 16:54:33 -0400955 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000956 break;
957
958 case I40E_NVMUPD_READ_LCB:
Shannon Nelson79afe832015-07-23 16:54:33 -0400959 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000960 i40e_release_nvm(hw);
961 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
962 break;
963
964 default:
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000965 i40e_debug(hw, I40E_DEBUG_NVM,
966 "NVMUPD: bad cmd %s in reading state.\n",
967 i40e_nvm_update_state_str[upd_cmd]);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000968 status = I40E_NOT_SUPPORTED;
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_writing - Handle NVM update state Writing
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_writing(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;
Shannon Nelson2c47e352015-02-21 06:45:10 +0000991 bool retry_attempt = false;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000992
Shannon Nelson79afe832015-07-23 16:54:33 -0400993 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000994
Shannon Nelson2c47e352015-02-21 06:45:10 +0000995retry:
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000996 switch (upd_cmd) {
997 case I40E_NVMUPD_WRITE_CON:
Shannon Nelson79afe832015-07-23 16:54:33 -0400998 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700999 if (!status) {
1000 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001001 hw->nvmupd_state = I40E_NVMUPD_STATE_WRITE_WAIT;
Shannon Nelsonfed2db92016-04-12 08:30:43 -07001002 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001003 break;
1004
1005 case I40E_NVMUPD_WRITE_LCB:
Shannon Nelson79afe832015-07-23 16:54:33 -04001006 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001007 if (status) {
1008 *perrno = hw->aq.asq_last_status ?
1009 i40e_aq_rc_to_posix(status,
1010 hw->aq.asq_last_status) :
1011 -EIO;
1012 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
1013 } else {
Shannon Nelson437f82a2016-04-01 03:56:09 -07001014 hw->nvm_release_on_done = true;
Shannon Nelsonfed2db92016-04-12 08:30:43 -07001015 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001016 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
1017 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001018 break;
1019
1020 case I40E_NVMUPD_CSUM_CON:
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -07001021 /* Assumes the caller has acquired the nvm */
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001022 status = i40e_update_nvm_checksum(hw);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001023 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -04001024 *perrno = hw->aq.asq_last_status ?
Shannon Nelsonbf848f32014-11-13 08:23:22 +00001025 i40e_aq_rc_to_posix(status,
1026 hw->aq.asq_last_status) :
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001027 -EIO;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001028 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001029 } else {
Shannon Nelsonfed2db92016-04-12 08:30:43 -07001030 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001031 hw->nvmupd_state = I40E_NVMUPD_STATE_WRITE_WAIT;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001032 }
1033 break;
1034
Shannon Nelson0fdd0522014-11-13 08:23:20 +00001035 case I40E_NVMUPD_CSUM_LCB:
Anjali Singhai Jain09f79fd2017-09-01 13:42:49 -07001036 /* Assumes the caller has acquired the nvm */
Shannon Nelson0fdd0522014-11-13 08:23:20 +00001037 status = i40e_update_nvm_checksum(hw);
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001038 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -04001039 *perrno = hw->aq.asq_last_status ?
Shannon Nelsonbf848f32014-11-13 08:23:22 +00001040 i40e_aq_rc_to_posix(status,
1041 hw->aq.asq_last_status) :
Shannon Nelson0fdd0522014-11-13 08:23:20 +00001042 -EIO;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001043 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
1044 } else {
Shannon Nelson437f82a2016-04-01 03:56:09 -07001045 hw->nvm_release_on_done = true;
Shannon Nelsonfed2db92016-04-12 08:30:43 -07001046 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001047 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
1048 }
Shannon Nelson0fdd0522014-11-13 08:23:20 +00001049 break;
1050
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001051 default:
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001052 i40e_debug(hw, I40E_DEBUG_NVM,
1053 "NVMUPD: bad cmd %s in writing state.\n",
1054 i40e_nvm_update_state_str[upd_cmd]);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001055 status = I40E_NOT_SUPPORTED;
Shannon Nelson79afe832015-07-23 16:54:33 -04001056 *perrno = -ESRCH;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001057 break;
1058 }
Shannon Nelson2c47e352015-02-21 06:45:10 +00001059
1060 /* In some circumstances, a multi-write transaction takes longer
1061 * than the default 3 minute timeout on the write semaphore. If
1062 * the write failed with an EBUSY status, this is likely the problem,
1063 * so here we try to reacquire the semaphore then retry the write.
1064 * We only do one retry, then give up.
1065 */
1066 if (status && (hw->aq.asq_last_status == I40E_AQ_RC_EBUSY) &&
1067 !retry_attempt) {
1068 i40e_status old_status = status;
1069 u32 old_asq_status = hw->aq.asq_last_status;
1070 u32 gtime;
1071
1072 gtime = rd32(hw, I40E_GLVFGEN_TIMER);
1073 if (gtime >= hw->nvm.hw_semaphore_timeout) {
1074 i40e_debug(hw, I40E_DEBUG_ALL,
1075 "NVMUPD: write semaphore expired (%d >= %lld), retrying\n",
1076 gtime, hw->nvm.hw_semaphore_timeout);
1077 i40e_release_nvm(hw);
1078 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
1079 if (status) {
1080 i40e_debug(hw, I40E_DEBUG_ALL,
1081 "NVMUPD: write semaphore reacquire failed aq_err = %d\n",
1082 hw->aq.asq_last_status);
1083 status = old_status;
1084 hw->aq.asq_last_status = old_asq_status;
1085 } else {
1086 retry_attempt = true;
1087 goto retry;
1088 }
1089 }
1090 }
1091
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001092 return status;
1093}
1094
1095/**
Shannon Nelsonbab2fb62016-04-01 03:56:11 -07001096 * i40e_nvmupd_check_wait_event - handle NVM update operation events
1097 * @hw: pointer to the hardware structure
1098 * @opcode: the event that just happened
1099 **/
1100void i40e_nvmupd_check_wait_event(struct i40e_hw *hw, u16 opcode)
1101{
Shannon Nelsonfed2db92016-04-12 08:30:43 -07001102 if (opcode == hw->nvm_wait_opcode) {
Shannon Nelsonbab2fb62016-04-01 03:56:11 -07001103 i40e_debug(hw, I40E_DEBUG_NVM,
1104 "NVMUPD: clearing wait on opcode 0x%04x\n", opcode);
1105 if (hw->nvm_release_on_done) {
1106 i40e_release_nvm(hw);
1107 hw->nvm_release_on_done = false;
1108 }
Shannon Nelsonfed2db92016-04-12 08:30:43 -07001109 hw->nvm_wait_opcode = 0;
Shannon Nelsonbab2fb62016-04-01 03:56:11 -07001110
Maciej Sosin81fa7c92016-10-11 15:26:57 -07001111 if (hw->aq.arq_last_status) {
1112 hw->nvmupd_state = I40E_NVMUPD_STATE_ERROR;
1113 return;
1114 }
1115
Shannon Nelsonbab2fb62016-04-01 03:56:11 -07001116 switch (hw->nvmupd_state) {
1117 case I40E_NVMUPD_STATE_INIT_WAIT:
1118 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
1119 break;
1120
1121 case I40E_NVMUPD_STATE_WRITE_WAIT:
1122 hw->nvmupd_state = I40E_NVMUPD_STATE_WRITING;
1123 break;
1124
1125 default:
1126 break;
1127 }
1128 }
1129}
1130
1131/**
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001132 * i40e_nvmupd_validate_command - Validate given command
1133 * @hw: pointer to hardware structure
1134 * @cmd: pointer to nvm update command buffer
Shannon Nelson79afe832015-07-23 16:54:33 -04001135 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001136 *
1137 * Return one of the valid command types or I40E_NVMUPD_INVALID
1138 **/
1139static enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
1140 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -04001141 int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001142{
1143 enum i40e_nvmupd_cmd upd_cmd;
Shannon Nelson0af8e9d2015-08-28 17:55:48 -04001144 u8 module, transaction;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001145
1146 /* anything that doesn't match a recognized case is an error */
1147 upd_cmd = I40E_NVMUPD_INVALID;
1148
1149 transaction = i40e_nvmupd_get_transaction(cmd->config);
Shannon Nelson0af8e9d2015-08-28 17:55:48 -04001150 module = i40e_nvmupd_get_module(cmd->config);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001151
1152 /* limits on data size */
1153 if ((cmd->data_size < 1) ||
1154 (cmd->data_size > I40E_NVMUPD_MAX_DATA)) {
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001155 i40e_debug(hw, I40E_DEBUG_NVM,
1156 "i40e_nvmupd_validate_command data_size %d\n",
1157 cmd->data_size);
Shannon Nelson79afe832015-07-23 16:54:33 -04001158 *perrno = -EFAULT;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001159 return I40E_NVMUPD_INVALID;
1160 }
1161
1162 switch (cmd->command) {
1163 case I40E_NVM_READ:
1164 switch (transaction) {
1165 case I40E_NVM_CON:
1166 upd_cmd = I40E_NVMUPD_READ_CON;
1167 break;
1168 case I40E_NVM_SNT:
1169 upd_cmd = I40E_NVMUPD_READ_SNT;
1170 break;
1171 case I40E_NVM_LCB:
1172 upd_cmd = I40E_NVMUPD_READ_LCB;
1173 break;
1174 case I40E_NVM_SA:
1175 upd_cmd = I40E_NVMUPD_READ_SA;
1176 break;
Shannon Nelson0af8e9d2015-08-28 17:55:48 -04001177 case I40E_NVM_EXEC:
1178 if (module == 0xf)
1179 upd_cmd = I40E_NVMUPD_STATUS;
Shannon Nelsonb72dc7b2015-08-28 17:55:51 -04001180 else if (module == 0)
1181 upd_cmd = I40E_NVMUPD_GET_AQ_RESULT;
Shannon Nelson0af8e9d2015-08-28 17:55:48 -04001182 break;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001183 }
1184 break;
1185
1186 case I40E_NVM_WRITE:
1187 switch (transaction) {
1188 case I40E_NVM_CON:
1189 upd_cmd = I40E_NVMUPD_WRITE_CON;
1190 break;
1191 case I40E_NVM_SNT:
1192 upd_cmd = I40E_NVMUPD_WRITE_SNT;
1193 break;
1194 case I40E_NVM_LCB:
1195 upd_cmd = I40E_NVMUPD_WRITE_LCB;
1196 break;
1197 case I40E_NVM_SA:
1198 upd_cmd = I40E_NVMUPD_WRITE_SA;
1199 break;
1200 case I40E_NVM_ERA:
1201 upd_cmd = I40E_NVMUPD_WRITE_ERA;
1202 break;
1203 case I40E_NVM_CSUM:
1204 upd_cmd = I40E_NVMUPD_CSUM_CON;
1205 break;
1206 case (I40E_NVM_CSUM|I40E_NVM_SA):
1207 upd_cmd = I40E_NVMUPD_CSUM_SA;
1208 break;
1209 case (I40E_NVM_CSUM|I40E_NVM_LCB):
1210 upd_cmd = I40E_NVMUPD_CSUM_LCB;
1211 break;
Shannon Nelsone4c83c22015-08-28 17:55:50 -04001212 case I40E_NVM_EXEC:
1213 if (module == 0)
1214 upd_cmd = I40E_NVMUPD_EXEC_AQ;
1215 break;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001216 }
1217 break;
1218 }
1219
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001220 return upd_cmd;
1221}
1222
1223/**
Shannon Nelsone4c83c22015-08-28 17:55:50 -04001224 * i40e_nvmupd_exec_aq - Run an AQ command
1225 * @hw: pointer to hardware structure
1226 * @cmd: pointer to nvm update command buffer
1227 * @bytes: pointer to the data buffer
1228 * @perrno: pointer to return error code
1229 *
1230 * cmd structure contains identifiers and data buffer
1231 **/
1232static i40e_status i40e_nvmupd_exec_aq(struct i40e_hw *hw,
1233 struct i40e_nvm_access *cmd,
1234 u8 *bytes, int *perrno)
1235{
1236 struct i40e_asq_cmd_details cmd_details;
1237 i40e_status status;
1238 struct i40e_aq_desc *aq_desc;
1239 u32 buff_size = 0;
1240 u8 *buff = NULL;
1241 u32 aq_desc_len;
1242 u32 aq_data_len;
1243
1244 i40e_debug(hw, I40E_DEBUG_NVM, "NVMUPD: %s\n", __func__);
1245 memset(&cmd_details, 0, sizeof(cmd_details));
1246 cmd_details.wb_desc = &hw->nvm_wb_desc;
1247
1248 aq_desc_len = sizeof(struct i40e_aq_desc);
1249 memset(&hw->nvm_wb_desc, 0, aq_desc_len);
1250
1251 /* get the aq descriptor */
1252 if (cmd->data_size < aq_desc_len) {
1253 i40e_debug(hw, I40E_DEBUG_NVM,
1254 "NVMUPD: not enough aq desc bytes for exec, size %d < %d\n",
1255 cmd->data_size, aq_desc_len);
1256 *perrno = -EINVAL;
1257 return I40E_ERR_PARAM;
1258 }
1259 aq_desc = (struct i40e_aq_desc *)bytes;
1260
1261 /* if data buffer needed, make sure it's ready */
1262 aq_data_len = cmd->data_size - aq_desc_len;
1263 buff_size = max_t(u32, aq_data_len, le16_to_cpu(aq_desc->datalen));
1264 if (buff_size) {
1265 if (!hw->nvm_buff.va) {
1266 status = i40e_allocate_virt_mem(hw, &hw->nvm_buff,
1267 hw->aq.asq_buf_size);
1268 if (status)
1269 i40e_debug(hw, I40E_DEBUG_NVM,
1270 "NVMUPD: i40e_allocate_virt_mem for exec buff failed, %d\n",
1271 status);
1272 }
1273
1274 if (hw->nvm_buff.va) {
1275 buff = hw->nvm_buff.va;
1276 memcpy(buff, &bytes[aq_desc_len], aq_data_len);
1277 }
1278 }
1279
1280 /* and away we go! */
1281 status = i40e_asq_send_command(hw, aq_desc, buff,
1282 buff_size, &cmd_details);
1283 if (status) {
1284 i40e_debug(hw, I40E_DEBUG_NVM,
1285 "i40e_nvmupd_exec_aq err %s aq_err %s\n",
1286 i40e_stat_str(hw, status),
1287 i40e_aq_str(hw, hw->aq.asq_last_status));
1288 *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
1289 }
1290
Shannon Nelsonfed2db92016-04-12 08:30:43 -07001291 /* should we wait for a followup event? */
1292 if (cmd->offset) {
1293 hw->nvm_wait_opcode = cmd->offset;
1294 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
1295 }
1296
Shannon Nelsone4c83c22015-08-28 17:55:50 -04001297 return status;
1298}
1299
1300/**
Shannon Nelsonb72dc7b2015-08-28 17:55:51 -04001301 * i40e_nvmupd_get_aq_result - Get the results from the previous exec_aq
1302 * @hw: pointer to hardware structure
1303 * @cmd: pointer to nvm update command buffer
1304 * @bytes: pointer to the data buffer
1305 * @perrno: pointer to return error code
1306 *
1307 * cmd structure contains identifiers and data buffer
1308 **/
1309static i40e_status i40e_nvmupd_get_aq_result(struct i40e_hw *hw,
1310 struct i40e_nvm_access *cmd,
1311 u8 *bytes, int *perrno)
1312{
1313 u32 aq_total_len;
1314 u32 aq_desc_len;
1315 int remainder;
1316 u8 *buff;
1317
1318 i40e_debug(hw, I40E_DEBUG_NVM, "NVMUPD: %s\n", __func__);
1319
1320 aq_desc_len = sizeof(struct i40e_aq_desc);
1321 aq_total_len = aq_desc_len + le16_to_cpu(hw->nvm_wb_desc.datalen);
1322
1323 /* check offset range */
1324 if (cmd->offset > aq_total_len) {
1325 i40e_debug(hw, I40E_DEBUG_NVM, "%s: offset too big %d > %d\n",
1326 __func__, cmd->offset, aq_total_len);
1327 *perrno = -EINVAL;
1328 return I40E_ERR_PARAM;
1329 }
1330
1331 /* check copylength range */
1332 if (cmd->data_size > (aq_total_len - cmd->offset)) {
1333 int new_len = aq_total_len - cmd->offset;
1334
1335 i40e_debug(hw, I40E_DEBUG_NVM, "%s: copy length %d too big, trimming to %d\n",
1336 __func__, cmd->data_size, new_len);
1337 cmd->data_size = new_len;
1338 }
1339
1340 remainder = cmd->data_size;
1341 if (cmd->offset < aq_desc_len) {
1342 u32 len = aq_desc_len - cmd->offset;
1343
1344 len = min(len, cmd->data_size);
1345 i40e_debug(hw, I40E_DEBUG_NVM, "%s: aq_desc bytes %d to %d\n",
1346 __func__, cmd->offset, cmd->offset + len);
1347
1348 buff = ((u8 *)&hw->nvm_wb_desc) + cmd->offset;
1349 memcpy(bytes, buff, len);
1350
1351 bytes += len;
1352 remainder -= len;
1353 buff = hw->nvm_buff.va;
1354 } else {
1355 buff = hw->nvm_buff.va + (cmd->offset - aq_desc_len);
1356 }
1357
1358 if (remainder > 0) {
1359 int start_byte = buff - (u8 *)hw->nvm_buff.va;
1360
1361 i40e_debug(hw, I40E_DEBUG_NVM, "%s: databuf bytes %d to %d\n",
1362 __func__, start_byte, start_byte + remainder);
1363 memcpy(bytes, buff, remainder);
1364 }
1365
1366 return 0;
1367}
1368
1369/**
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001370 * i40e_nvmupd_nvm_read - Read NVM
1371 * @hw: pointer to hardware structure
1372 * @cmd: pointer to nvm update command buffer
1373 * @bytes: pointer to the data buffer
Shannon Nelson79afe832015-07-23 16:54:33 -04001374 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001375 *
1376 * cmd structure contains identifiers and data buffer
1377 **/
1378static i40e_status i40e_nvmupd_nvm_read(struct i40e_hw *hw,
1379 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -04001380 u8 *bytes, int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001381{
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001382 struct i40e_asq_cmd_details cmd_details;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001383 i40e_status status;
1384 u8 module, transaction;
1385 bool last;
1386
1387 transaction = i40e_nvmupd_get_transaction(cmd->config);
1388 module = i40e_nvmupd_get_module(cmd->config);
1389 last = (transaction == I40E_NVM_LCB) || (transaction == I40E_NVM_SA);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001390
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001391 memset(&cmd_details, 0, sizeof(cmd_details));
1392 cmd_details.wb_desc = &hw->nvm_wb_desc;
1393
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001394 status = i40e_aq_read_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001395 bytes, last, &cmd_details);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001396 if (status) {
1397 i40e_debug(hw, I40E_DEBUG_NVM,
1398 "i40e_nvmupd_nvm_read mod 0x%x off 0x%x len 0x%x\n",
1399 module, cmd->offset, cmd->data_size);
1400 i40e_debug(hw, I40E_DEBUG_NVM,
1401 "i40e_nvmupd_nvm_read status %d aq %d\n",
1402 status, hw->aq.asq_last_status);
Shannon Nelson79afe832015-07-23 16:54:33 -04001403 *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001404 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001405
1406 return status;
1407}
1408
1409/**
1410 * i40e_nvmupd_nvm_erase - Erase an NVM module
1411 * @hw: pointer to hardware structure
1412 * @cmd: pointer to nvm update command buffer
Shannon Nelson79afe832015-07-23 16:54:33 -04001413 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001414 *
1415 * module, offset, data_size and data are in cmd structure
1416 **/
1417static i40e_status i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
1418 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -04001419 int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001420{
1421 i40e_status status = 0;
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001422 struct i40e_asq_cmd_details cmd_details;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001423 u8 module, transaction;
1424 bool last;
1425
1426 transaction = i40e_nvmupd_get_transaction(cmd->config);
1427 module = i40e_nvmupd_get_module(cmd->config);
1428 last = (transaction & I40E_NVM_LCB);
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001429
1430 memset(&cmd_details, 0, sizeof(cmd_details));
1431 cmd_details.wb_desc = &hw->nvm_wb_desc;
1432
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001433 status = i40e_aq_erase_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001434 last, &cmd_details);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001435 if (status) {
1436 i40e_debug(hw, I40E_DEBUG_NVM,
1437 "i40e_nvmupd_nvm_erase mod 0x%x off 0x%x len 0x%x\n",
1438 module, cmd->offset, cmd->data_size);
1439 i40e_debug(hw, I40E_DEBUG_NVM,
1440 "i40e_nvmupd_nvm_erase status %d aq %d\n",
1441 status, hw->aq.asq_last_status);
Shannon Nelson79afe832015-07-23 16:54:33 -04001442 *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001443 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001444
1445 return status;
1446}
1447
1448/**
1449 * i40e_nvmupd_nvm_write - Write NVM
1450 * @hw: pointer to hardware structure
1451 * @cmd: pointer to nvm update command buffer
1452 * @bytes: pointer to the data buffer
Shannon Nelson79afe832015-07-23 16:54:33 -04001453 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001454 *
1455 * module, offset, data_size and data are in cmd structure
1456 **/
1457static i40e_status i40e_nvmupd_nvm_write(struct i40e_hw *hw,
1458 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -04001459 u8 *bytes, int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001460{
1461 i40e_status status = 0;
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001462 struct i40e_asq_cmd_details cmd_details;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001463 u8 module, transaction;
1464 bool last;
1465
1466 transaction = i40e_nvmupd_get_transaction(cmd->config);
1467 module = i40e_nvmupd_get_module(cmd->config);
1468 last = (transaction & I40E_NVM_LCB);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001469
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001470 memset(&cmd_details, 0, sizeof(cmd_details));
1471 cmd_details.wb_desc = &hw->nvm_wb_desc;
1472
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001473 status = i40e_aq_update_nvm(hw, module, cmd->offset,
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001474 (u16)cmd->data_size, bytes, last,
1475 &cmd_details);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001476 if (status) {
1477 i40e_debug(hw, I40E_DEBUG_NVM,
1478 "i40e_nvmupd_nvm_write mod 0x%x off 0x%x len 0x%x\n",
1479 module, cmd->offset, cmd->data_size);
1480 i40e_debug(hw, I40E_DEBUG_NVM,
1481 "i40e_nvmupd_nvm_write status %d aq %d\n",
1482 status, hw->aq.asq_last_status);
Shannon Nelson79afe832015-07-23 16:54:33 -04001483 *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001484 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001485
1486 return status;
1487}