blob: 96afef98a08f5f3d4c5970ed065cde196657aa85 [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{
Paul M Stillwell Jr981e25c2017-06-20 15:16:55 -0700137 i40e_status ret_code = I40E_SUCCESS;
138 u32 total_delay = 0;
139
140 if (hw->nvm.blank_nvm_mode)
141 return;
142
143 ret_code = i40e_aq_release_resource(hw, I40E_NVM_RESOURCE_ID, 0, NULL);
144
145 /* there are some rare cases when trying to release the resource
146 * results in an admin Q timeout, so handle them correctly
147 */
148 while ((ret_code == I40E_ERR_ADMIN_QUEUE_TIMEOUT) &&
149 (total_delay < hw->aq.asq_cmd_timeout)) {
150 usleep_range(1000, 2000);
151 ret_code = i40e_aq_release_resource(hw,
152 I40E_NVM_RESOURCE_ID,
153 0, NULL);
154 total_delay++;
155 }
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000156}
157
158/**
Shannon Nelson3e261862014-02-06 05:51:06 +0000159 * i40e_poll_sr_srctl_done_bit - Polls the GLNVM_SRCTL done bit
160 * @hw: pointer to the HW structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000161 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000162 * Polls the SRCTL Shadow RAM register done bit.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000163 **/
164static i40e_status i40e_poll_sr_srctl_done_bit(struct i40e_hw *hw)
165{
166 i40e_status ret_code = I40E_ERR_TIMEOUT;
167 u32 srctl, wait_cnt;
168
Shannon Nelson3e261862014-02-06 05:51:06 +0000169 /* Poll the I40E_GLNVM_SRCTL until the done bit is set */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000170 for (wait_cnt = 0; wait_cnt < I40E_SRRD_SRCTL_ATTEMPTS; wait_cnt++) {
171 srctl = rd32(hw, I40E_GLNVM_SRCTL);
172 if (srctl & I40E_GLNVM_SRCTL_DONE_MASK) {
173 ret_code = 0;
174 break;
175 }
176 udelay(5);
177 }
178 if (ret_code == I40E_ERR_TIMEOUT)
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000179 i40e_debug(hw, I40E_DEBUG_NVM, "Done bit in GLNVM_SRCTL not set");
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000180 return ret_code;
181}
182
183/**
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000184 * i40e_read_nvm_word_srctl - Reads Shadow RAM via SRCTL register
Shannon Nelson3e261862014-02-06 05:51:06 +0000185 * @hw: pointer to the HW structure
186 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
187 * @data: word read from the Shadow RAM
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000188 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000189 * Reads one 16 bit word from the Shadow RAM using the GLNVM_SRCTL register.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000190 **/
Shannon Nelson37a29732015-02-27 09:15:19 +0000191static i40e_status i40e_read_nvm_word_srctl(struct i40e_hw *hw, u16 offset,
192 u16 *data)
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000193{
194 i40e_status ret_code = I40E_ERR_TIMEOUT;
195 u32 sr_reg;
196
197 if (offset >= hw->nvm.sr_size) {
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000198 i40e_debug(hw, I40E_DEBUG_NVM,
199 "NVM read error: offset %d beyond Shadow RAM limit %d\n",
200 offset, hw->nvm.sr_size);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000201 ret_code = I40E_ERR_PARAM;
202 goto read_nvm_exit;
203 }
204
Shannon Nelson3e261862014-02-06 05:51:06 +0000205 /* Poll the done bit first */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000206 ret_code = i40e_poll_sr_srctl_done_bit(hw);
207 if (!ret_code) {
Shannon Nelson3e261862014-02-06 05:51:06 +0000208 /* Write the address and start reading */
Jesse Brandeburg41a1d042015-06-04 16:24:02 -0400209 sr_reg = ((u32)offset << I40E_GLNVM_SRCTL_ADDR_SHIFT) |
210 BIT(I40E_GLNVM_SRCTL_START_SHIFT);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000211 wr32(hw, I40E_GLNVM_SRCTL, sr_reg);
212
Shannon Nelson3e261862014-02-06 05:51:06 +0000213 /* Poll I40E_GLNVM_SRCTL until the done bit is set */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000214 ret_code = i40e_poll_sr_srctl_done_bit(hw);
215 if (!ret_code) {
216 sr_reg = rd32(hw, I40E_GLNVM_SRDATA);
217 *data = (u16)((sr_reg &
218 I40E_GLNVM_SRDATA_RDDATA_MASK)
219 >> I40E_GLNVM_SRDATA_RDDATA_SHIFT);
220 }
221 }
222 if (ret_code)
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000223 i40e_debug(hw, I40E_DEBUG_NVM,
224 "NVM read error: Couldn't access Shadow RAM address: 0x%x\n",
225 offset);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000226
227read_nvm_exit:
228 return ret_code;
229}
230
231/**
Shannon Nelson7073f462015-06-05 12:20:34 -0400232 * i40e_read_nvm_aq - Read Shadow RAM.
233 * @hw: pointer to the HW structure.
234 * @module_pointer: module pointer location in words from the NVM beginning
235 * @offset: offset in words from module start
236 * @words: number of words to write
237 * @data: buffer with words to write to the Shadow RAM
238 * @last_command: tells the AdminQ that this is the last command
239 *
240 * Writes a 16 bit words buffer to the Shadow RAM using the admin command.
241 **/
242static i40e_status i40e_read_nvm_aq(struct i40e_hw *hw, u8 module_pointer,
243 u32 offset, u16 words, void *data,
244 bool last_command)
245{
246 i40e_status ret_code = I40E_ERR_NVM;
247 struct i40e_asq_cmd_details cmd_details;
248
249 memset(&cmd_details, 0, sizeof(cmd_details));
250
251 /* Here we are checking the SR limit only for the flat memory model.
252 * We cannot do it for the module-based model, as we did not acquire
253 * the NVM resource yet (we cannot get the module pointer value).
254 * Firmware will check the module-based model.
255 */
256 if ((offset + words) > hw->nvm.sr_size)
257 i40e_debug(hw, I40E_DEBUG_NVM,
258 "NVM write error: offset %d beyond Shadow RAM limit %d\n",
259 (offset + words), hw->nvm.sr_size);
260 else if (words > I40E_SR_SECTOR_SIZE_IN_WORDS)
261 /* We can write only up to 4KB (one sector), in one AQ write */
262 i40e_debug(hw, I40E_DEBUG_NVM,
263 "NVM write fail error: tried to write %d words, limit is %d.\n",
264 words, I40E_SR_SECTOR_SIZE_IN_WORDS);
265 else if (((offset + (words - 1)) / I40E_SR_SECTOR_SIZE_IN_WORDS)
266 != (offset / I40E_SR_SECTOR_SIZE_IN_WORDS))
267 /* A single write cannot spread over two sectors */
268 i40e_debug(hw, I40E_DEBUG_NVM,
269 "NVM write error: cannot spread over two sectors in a single write offset=%d words=%d\n",
270 offset, words);
271 else
272 ret_code = i40e_aq_read_nvm(hw, module_pointer,
273 2 * offset, /*bytes*/
274 2 * words, /*bytes*/
275 data, last_command, &cmd_details);
276
277 return ret_code;
278}
279
280/**
281 * i40e_read_nvm_word_aq - Reads Shadow RAM via AQ
282 * @hw: pointer to the HW structure
283 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
284 * @data: word read from the Shadow RAM
285 *
286 * Reads one 16 bit word from the Shadow RAM using the GLNVM_SRCTL register.
287 **/
288static i40e_status i40e_read_nvm_word_aq(struct i40e_hw *hw, u16 offset,
289 u16 *data)
290{
291 i40e_status ret_code = I40E_ERR_TIMEOUT;
292
293 ret_code = i40e_read_nvm_aq(hw, 0x0, offset, 1, data, true);
294 *data = le16_to_cpu(*(__le16 *)data);
295
296 return ret_code;
297}
298
299/**
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000300 * i40e_read_nvm_word - Reads Shadow RAM
301 * @hw: pointer to the HW structure
302 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
303 * @data: word read from the Shadow RAM
304 *
305 * Reads one 16 bit word from the Shadow RAM using the GLNVM_SRCTL register.
306 **/
307i40e_status i40e_read_nvm_word(struct i40e_hw *hw, u16 offset,
308 u16 *data)
309{
Anjali Singhai07f89be2015-09-24 15:26:32 -0700310 enum i40e_status_code ret_code = 0;
311
Aaron Salter96a39ae2016-12-02 12:33:02 -0800312 ret_code = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
313 if (!ret_code) {
314 if (hw->flags & I40E_HW_FLAG_AQ_SRCTL_ACCESS_ENABLE) {
Anjali Singhai07f89be2015-09-24 15:26:32 -0700315 ret_code = i40e_read_nvm_word_aq(hw, offset, data);
Aaron Salter96a39ae2016-12-02 12:33:02 -0800316 } else {
317 ret_code = i40e_read_nvm_word_srctl(hw, offset, data);
Anjali Singhai07f89be2015-09-24 15:26:32 -0700318 }
Aaron Salter96a39ae2016-12-02 12:33:02 -0800319 i40e_release_nvm(hw);
Anjali Singhai07f89be2015-09-24 15:26:32 -0700320 }
321 return ret_code;
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000322}
323
324/**
325 * i40e_read_nvm_buffer_srctl - Reads Shadow RAM buffer via SRCTL register
326 * @hw: pointer to the HW structure
327 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
328 * @words: (in) number of words to read; (out) number of words actually read
329 * @data: words read from the Shadow RAM
330 *
331 * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_srrd()
332 * method. The buffer read is preceded by the NVM ownership take
333 * and followed by the release.
334 **/
Shannon Nelson37a29732015-02-27 09:15:19 +0000335static i40e_status i40e_read_nvm_buffer_srctl(struct i40e_hw *hw, u16 offset,
336 u16 *words, u16 *data)
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000337{
338 i40e_status ret_code = 0;
339 u16 index, word;
340
341 /* Loop thru the selected region */
342 for (word = 0; word < *words; word++) {
343 index = offset + word;
344 ret_code = i40e_read_nvm_word_srctl(hw, index, &data[word]);
345 if (ret_code)
346 break;
347 }
348
349 /* Update the number of words read from the Shadow RAM */
350 *words = word;
351
352 return ret_code;
353}
354
355/**
Shannon Nelson7073f462015-06-05 12:20:34 -0400356 * i40e_read_nvm_buffer_aq - Reads Shadow RAM buffer via AQ
357 * @hw: pointer to the HW structure
358 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
359 * @words: (in) number of words to read; (out) number of words actually read
360 * @data: words read from the Shadow RAM
361 *
362 * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_aq()
363 * method. The buffer read is preceded by the NVM ownership take
364 * and followed by the release.
365 **/
366static i40e_status i40e_read_nvm_buffer_aq(struct i40e_hw *hw, u16 offset,
367 u16 *words, u16 *data)
368{
369 i40e_status ret_code;
370 u16 read_size = *words;
371 bool last_cmd = false;
372 u16 words_read = 0;
373 u16 i = 0;
374
375 do {
376 /* Calculate number of bytes we should read in this step.
377 * FVL AQ do not allow to read more than one page at a time or
378 * to cross page boundaries.
379 */
380 if (offset % I40E_SR_SECTOR_SIZE_IN_WORDS)
381 read_size = min(*words,
382 (u16)(I40E_SR_SECTOR_SIZE_IN_WORDS -
383 (offset % I40E_SR_SECTOR_SIZE_IN_WORDS)));
384 else
385 read_size = min((*words - words_read),
386 I40E_SR_SECTOR_SIZE_IN_WORDS);
387
388 /* Check if this is last command, if so set proper flag */
389 if ((words_read + read_size) >= *words)
390 last_cmd = true;
391
392 ret_code = i40e_read_nvm_aq(hw, 0x0, offset, read_size,
393 data + words_read, last_cmd);
394 if (ret_code)
395 goto read_nvm_buffer_aq_exit;
396
397 /* Increment counter for words already read and move offset to
398 * new read location
399 */
400 words_read += read_size;
401 offset += read_size;
402 } while (words_read < *words);
403
404 for (i = 0; i < *words; i++)
405 data[i] = le16_to_cpu(((__le16 *)data)[i]);
406
407read_nvm_buffer_aq_exit:
408 *words = words_read;
409 return ret_code;
410}
411
412/**
Shannon Nelson3e261862014-02-06 05:51:06 +0000413 * i40e_read_nvm_buffer - Reads Shadow RAM buffer
414 * @hw: pointer to the HW structure
415 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
416 * @words: (in) number of words to read; (out) number of words actually read
417 * @data: words read from the Shadow RAM
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000418 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000419 * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_srrd()
420 * method. The buffer read is preceded by the NVM ownership take
421 * and followed by the release.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000422 **/
423i40e_status i40e_read_nvm_buffer(struct i40e_hw *hw, u16 offset,
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000424 u16 *words, u16 *data)
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000425{
Anjali Singhai07f89be2015-09-24 15:26:32 -0700426 enum i40e_status_code ret_code = 0;
427
428 if (hw->flags & I40E_HW_FLAG_AQ_SRCTL_ACCESS_ENABLE) {
429 ret_code = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
430 if (!ret_code) {
431 ret_code = i40e_read_nvm_buffer_aq(hw, offset, words,
432 data);
433 i40e_release_nvm(hw);
434 }
435 } else {
436 ret_code = i40e_read_nvm_buffer_srctl(hw, offset, words, data);
437 }
438 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 */
Shannon Nelsona4bcfbb2013-12-11 08:17:15 +0000519 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 */
Shannon Nelsona4bcfbb2013-12-11 08:17:15 +0000526 ret_code = i40e_read_nvm_word(hw, I40E_SR_PCIE_ALT_AUTO_LOAD_PTR,
Kamil Krawczykd1bbe0e2015-01-24 09:58:33 +0000527 &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
541 ret_code = i40e_read_nvm_buffer(hw, i, &words, data);
542 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
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000613 ret_code = i40e_calc_nvm_checksum(hw, &checksum_local);
614 if (ret_code)
Kamil Krawczyk7a208e82014-06-04 04:22:36 +0000615 goto i40e_validate_nvm_checksum_exit;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000616
617 /* Do not use i40e_read_nvm_word() because we do not want to take
618 * the synchronization semaphores twice here.
619 */
Shannon Nelsona4bcfbb2013-12-11 08:17:15 +0000620 i40e_read_nvm_word(hw, I40E_SR_SW_CHECKSUM_WORD, &checksum_sr);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000621
622 /* Verify read checksum from EEPROM is the same as
623 * calculated checksum
624 */
625 if (checksum_local != checksum_sr)
626 ret_code = I40E_ERR_NVM_CHECKSUM;
627
628 /* If the user cares, return the calculated checksum */
629 if (checksum)
630 *checksum = checksum_local;
631
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000632i40e_validate_nvm_checksum_exit:
633 return ret_code;
634}
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000635
636static i40e_status i40e_nvmupd_state_init(struct i40e_hw *hw,
637 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400638 u8 *bytes, int *perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000639static i40e_status i40e_nvmupd_state_reading(struct i40e_hw *hw,
640 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400641 u8 *bytes, int *perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000642static i40e_status i40e_nvmupd_state_writing(struct i40e_hw *hw,
643 struct i40e_nvm_access *cmd,
644 u8 *bytes, int *errno);
645static enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
646 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400647 int *perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000648static i40e_status i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
649 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400650 int *perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000651static i40e_status i40e_nvmupd_nvm_write(struct i40e_hw *hw,
652 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400653 u8 *bytes, int *perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000654static i40e_status i40e_nvmupd_nvm_read(struct i40e_hw *hw,
655 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400656 u8 *bytes, int *perrno);
Shannon Nelsone4c83c22015-08-28 17:55:50 -0400657static i40e_status i40e_nvmupd_exec_aq(struct i40e_hw *hw,
658 struct i40e_nvm_access *cmd,
659 u8 *bytes, int *perrno);
Shannon Nelsonb72dc7b2015-08-28 17:55:51 -0400660static i40e_status i40e_nvmupd_get_aq_result(struct i40e_hw *hw,
661 struct i40e_nvm_access *cmd,
662 u8 *bytes, int *perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000663static inline u8 i40e_nvmupd_get_module(u32 val)
664{
665 return (u8)(val & I40E_NVM_MOD_PNT_MASK);
666}
667static inline u8 i40e_nvmupd_get_transaction(u32 val)
668{
669 return (u8)((val & I40E_NVM_TRANS_MASK) >> I40E_NVM_TRANS_SHIFT);
670}
671
Jingjing Wu4e68adfe2015-09-28 14:12:31 -0400672static const char * const i40e_nvm_update_state_str[] = {
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000673 "I40E_NVMUPD_INVALID",
674 "I40E_NVMUPD_READ_CON",
675 "I40E_NVMUPD_READ_SNT",
676 "I40E_NVMUPD_READ_LCB",
677 "I40E_NVMUPD_READ_SA",
678 "I40E_NVMUPD_WRITE_ERA",
679 "I40E_NVMUPD_WRITE_CON",
680 "I40E_NVMUPD_WRITE_SNT",
681 "I40E_NVMUPD_WRITE_LCB",
682 "I40E_NVMUPD_WRITE_SA",
683 "I40E_NVMUPD_CSUM_CON",
684 "I40E_NVMUPD_CSUM_SA",
685 "I40E_NVMUPD_CSUM_LCB",
Shannon Nelson0af8e9d2015-08-28 17:55:48 -0400686 "I40E_NVMUPD_STATUS",
Shannon Nelsone4c83c22015-08-28 17:55:50 -0400687 "I40E_NVMUPD_EXEC_AQ",
Shannon Nelsonb72dc7b2015-08-28 17:55:51 -0400688 "I40E_NVMUPD_GET_AQ_RESULT",
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000689};
690
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000691/**
692 * i40e_nvmupd_command - Process an NVM update command
693 * @hw: pointer to hardware structure
694 * @cmd: pointer to nvm update command
695 * @bytes: pointer to the data buffer
Shannon Nelson79afe832015-07-23 16:54:33 -0400696 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000697 *
698 * Dispatches command depending on what update state is current
699 **/
700i40e_status i40e_nvmupd_command(struct i40e_hw *hw,
701 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400702 u8 *bytes, int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000703{
704 i40e_status status;
Shannon Nelson0af8e9d2015-08-28 17:55:48 -0400705 enum i40e_nvmupd_cmd upd_cmd;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000706
707 /* assume success */
Shannon Nelson79afe832015-07-23 16:54:33 -0400708 *perrno = 0;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000709
Shannon Nelson0af8e9d2015-08-28 17:55:48 -0400710 /* early check for status command and debug msgs */
711 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
712
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700713 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 -0400714 i40e_nvm_update_state_str[upd_cmd],
715 hw->nvmupd_state,
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700716 hw->nvm_release_on_done, hw->nvm_wait_opcode,
Shannon Nelson1d73b2d2015-12-23 12:05:51 -0800717 cmd->command, cmd->config, cmd->offset, cmd->data_size);
Shannon Nelson0af8e9d2015-08-28 17:55:48 -0400718
719 if (upd_cmd == I40E_NVMUPD_INVALID) {
720 *perrno = -EFAULT;
721 i40e_debug(hw, I40E_DEBUG_NVM,
722 "i40e_nvmupd_validate_command returns %d errno %d\n",
723 upd_cmd, *perrno);
724 }
725
726 /* a status request returns immediately rather than
727 * going into the state machine
728 */
729 if (upd_cmd == I40E_NVMUPD_STATUS) {
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700730 if (!cmd->data_size) {
731 *perrno = -EFAULT;
732 return I40E_ERR_BUF_TOO_SHORT;
733 }
734
Shannon Nelson0af8e9d2015-08-28 17:55:48 -0400735 bytes[0] = hw->nvmupd_state;
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700736
737 if (cmd->data_size >= 4) {
738 bytes[1] = 0;
739 *((u16 *)&bytes[2]) = hw->nvm_wait_opcode;
740 }
741
Maciej Sosin81fa7c92016-10-11 15:26:57 -0700742 /* Clear error status on read */
743 if (hw->nvmupd_state == I40E_NVMUPD_STATE_ERROR)
744 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
745
Shannon Nelson0af8e9d2015-08-28 17:55:48 -0400746 return 0;
747 }
748
Maciej Sosin81fa7c92016-10-11 15:26:57 -0700749 /* Clear status even it is not read and log */
750 if (hw->nvmupd_state == I40E_NVMUPD_STATE_ERROR) {
751 i40e_debug(hw, I40E_DEBUG_NVM,
752 "Clearing I40E_NVMUPD_STATE_ERROR state without reading\n");
753 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
754 }
755
Sudheer Mogilappagari2bf01932017-07-12 05:46:07 -0400756 /* Acquire lock to prevent race condition where adminq_task
757 * can execute after i40e_nvmupd_nvm_read/write but before state
Sudheer Mogilappagari167d52e2017-08-27 15:07:47 -0700758 * variables (nvm_wait_opcode, nvm_release_on_done) are updated.
759 *
760 * During NVMUpdate, it is observed that lock could be held for
761 * ~5ms for most commands. However lock is held for ~60ms for
762 * NVMUPD_CSUM_LCB command.
Sudheer Mogilappagari2bf01932017-07-12 05:46:07 -0400763 */
764 mutex_lock(&hw->aq.arq_mutex);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000765 switch (hw->nvmupd_state) {
766 case I40E_NVMUPD_STATE_INIT:
Shannon Nelson79afe832015-07-23 16:54:33 -0400767 status = i40e_nvmupd_state_init(hw, cmd, bytes, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000768 break;
769
770 case I40E_NVMUPD_STATE_READING:
Shannon Nelson79afe832015-07-23 16:54:33 -0400771 status = i40e_nvmupd_state_reading(hw, cmd, bytes, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000772 break;
773
774 case I40E_NVMUPD_STATE_WRITING:
Shannon Nelson79afe832015-07-23 16:54:33 -0400775 status = i40e_nvmupd_state_writing(hw, cmd, bytes, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000776 break;
777
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400778 case I40E_NVMUPD_STATE_INIT_WAIT:
779 case I40E_NVMUPD_STATE_WRITE_WAIT:
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700780 /* if we need to stop waiting for an event, clear
781 * the wait info and return before doing anything else
782 */
783 if (cmd->offset == 0xffff) {
784 i40e_nvmupd_check_wait_event(hw, hw->nvm_wait_opcode);
Sudheer Mogilappagari167d52e2017-08-27 15:07:47 -0700785 status = 0;
786 goto exit;
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700787 }
788
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400789 status = I40E_ERR_NOT_READY;
790 *perrno = -EBUSY;
791 break;
792
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000793 default:
794 /* invalid state, should never happen */
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000795 i40e_debug(hw, I40E_DEBUG_NVM,
796 "NVMUPD: no such state %d\n", hw->nvmupd_state);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000797 status = I40E_NOT_SUPPORTED;
Shannon Nelson79afe832015-07-23 16:54:33 -0400798 *perrno = -ESRCH;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000799 break;
800 }
Sudheer Mogilappagari167d52e2017-08-27 15:07:47 -0700801exit:
Sudheer Mogilappagari2bf01932017-07-12 05:46:07 -0400802 mutex_unlock(&hw->aq.arq_mutex);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000803 return status;
804}
805
806/**
807 * i40e_nvmupd_state_init - Handle NVM update state Init
808 * @hw: pointer to hardware structure
809 * @cmd: pointer to nvm update command buffer
810 * @bytes: pointer to the data buffer
Shannon Nelson79afe832015-07-23 16:54:33 -0400811 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000812 *
813 * Process legitimate commands of the Init state and conditionally set next
814 * state. Reject all other commands.
815 **/
816static i40e_status i40e_nvmupd_state_init(struct i40e_hw *hw,
817 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400818 u8 *bytes, int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000819{
820 i40e_status status = 0;
821 enum i40e_nvmupd_cmd upd_cmd;
822
Shannon Nelson79afe832015-07-23 16:54:33 -0400823 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000824
825 switch (upd_cmd) {
826 case I40E_NVMUPD_READ_SA:
827 status = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
828 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -0400829 *perrno = i40e_aq_rc_to_posix(status,
Shannon Nelsonbf848f32014-11-13 08:23:22 +0000830 hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000831 } else {
Shannon Nelson79afe832015-07-23 16:54:33 -0400832 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000833 i40e_release_nvm(hw);
834 }
835 break;
836
837 case I40E_NVMUPD_READ_SNT:
838 status = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
839 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -0400840 *perrno = i40e_aq_rc_to_posix(status,
Shannon Nelsonbf848f32014-11-13 08:23:22 +0000841 hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000842 } else {
Shannon Nelson79afe832015-07-23 16:54:33 -0400843 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
Shannon Nelson0fdd0522014-11-13 08:23:20 +0000844 if (status)
845 i40e_release_nvm(hw);
846 else
847 hw->nvmupd_state = I40E_NVMUPD_STATE_READING;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000848 }
849 break;
850
851 case I40E_NVMUPD_WRITE_ERA:
852 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
853 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -0400854 *perrno = i40e_aq_rc_to_posix(status,
Shannon Nelsonbf848f32014-11-13 08:23:22 +0000855 hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000856 } else {
Shannon Nelson79afe832015-07-23 16:54:33 -0400857 status = i40e_nvmupd_nvm_erase(hw, cmd, perrno);
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400858 if (status) {
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000859 i40e_release_nvm(hw);
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400860 } else {
Shannon Nelson437f82a2016-04-01 03:56:09 -0700861 hw->nvm_release_on_done = true;
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700862 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_erase;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400863 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
864 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000865 }
866 break;
867
868 case I40E_NVMUPD_WRITE_SA:
869 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
870 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -0400871 *perrno = i40e_aq_rc_to_posix(status,
Shannon Nelsonbf848f32014-11-13 08:23:22 +0000872 hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000873 } else {
Shannon Nelson79afe832015-07-23 16:54:33 -0400874 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400875 if (status) {
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000876 i40e_release_nvm(hw);
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400877 } else {
Shannon Nelson437f82a2016-04-01 03:56:09 -0700878 hw->nvm_release_on_done = true;
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700879 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400880 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
881 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000882 }
883 break;
884
885 case I40E_NVMUPD_WRITE_SNT:
886 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
887 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -0400888 *perrno = i40e_aq_rc_to_posix(status,
Shannon Nelsonbf848f32014-11-13 08:23:22 +0000889 hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000890 } else {
Shannon Nelson79afe832015-07-23 16:54:33 -0400891 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700892 if (status) {
Shannon Nelson0fdd0522014-11-13 08:23:20 +0000893 i40e_release_nvm(hw);
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700894 } else {
895 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400896 hw->nvmupd_state = I40E_NVMUPD_STATE_WRITE_WAIT;
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700897 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000898 }
899 break;
900
901 case I40E_NVMUPD_CSUM_SA:
902 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
903 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -0400904 *perrno = i40e_aq_rc_to_posix(status,
Shannon Nelsonbf848f32014-11-13 08:23:22 +0000905 hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000906 } else {
907 status = i40e_update_nvm_checksum(hw);
908 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -0400909 *perrno = hw->aq.asq_last_status ?
Shannon Nelsonbf848f32014-11-13 08:23:22 +0000910 i40e_aq_rc_to_posix(status,
911 hw->aq.asq_last_status) :
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000912 -EIO;
913 i40e_release_nvm(hw);
914 } else {
Shannon Nelson437f82a2016-04-01 03:56:09 -0700915 hw->nvm_release_on_done = true;
Shannon Nelsonfed2db92016-04-12 08:30:43 -0700916 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400917 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000918 }
919 }
920 break;
921
Shannon Nelsone4c83c22015-08-28 17:55:50 -0400922 case I40E_NVMUPD_EXEC_AQ:
923 status = i40e_nvmupd_exec_aq(hw, cmd, bytes, perrno);
924 break;
925
Shannon Nelsonb72dc7b2015-08-28 17:55:51 -0400926 case I40E_NVMUPD_GET_AQ_RESULT:
927 status = i40e_nvmupd_get_aq_result(hw, cmd, bytes, perrno);
928 break;
929
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000930 default:
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000931 i40e_debug(hw, I40E_DEBUG_NVM,
932 "NVMUPD: bad cmd %s in init state\n",
933 i40e_nvm_update_state_str[upd_cmd]);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000934 status = I40E_ERR_NVM;
Shannon Nelson79afe832015-07-23 16:54:33 -0400935 *perrno = -ESRCH;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000936 break;
937 }
938 return status;
939}
940
941/**
942 * i40e_nvmupd_state_reading - Handle NVM update state Reading
943 * @hw: pointer to hardware structure
944 * @cmd: pointer to nvm update command buffer
945 * @bytes: pointer to the data buffer
Shannon Nelson79afe832015-07-23 16:54:33 -0400946 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000947 *
948 * NVM ownership is already held. Process legitimate commands and set any
949 * change in state; reject all other commands.
950 **/
951static i40e_status i40e_nvmupd_state_reading(struct i40e_hw *hw,
952 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400953 u8 *bytes, int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000954{
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400955 i40e_status status = 0;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000956 enum i40e_nvmupd_cmd upd_cmd;
957
Shannon Nelson79afe832015-07-23 16:54:33 -0400958 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000959
960 switch (upd_cmd) {
961 case I40E_NVMUPD_READ_SA:
962 case I40E_NVMUPD_READ_CON:
Shannon Nelson79afe832015-07-23 16:54:33 -0400963 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000964 break;
965
966 case I40E_NVMUPD_READ_LCB:
Shannon Nelson79afe832015-07-23 16:54:33 -0400967 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000968 i40e_release_nvm(hw);
969 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
970 break;
971
972 default:
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000973 i40e_debug(hw, I40E_DEBUG_NVM,
974 "NVMUPD: bad cmd %s in reading state.\n",
975 i40e_nvm_update_state_str[upd_cmd]);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000976 status = I40E_NOT_SUPPORTED;
Shannon Nelson79afe832015-07-23 16:54:33 -0400977 *perrno = -ESRCH;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000978 break;
979 }
980 return status;
981}
982
983/**
984 * i40e_nvmupd_state_writing - Handle NVM update state Writing
985 * @hw: pointer to hardware structure
986 * @cmd: pointer to nvm update command buffer
987 * @bytes: pointer to the data buffer
Shannon Nelson79afe832015-07-23 16:54:33 -0400988 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000989 *
990 * NVM ownership is already held. Process legitimate commands and set any
991 * change in state; reject all other commands
992 **/
993static i40e_status i40e_nvmupd_state_writing(struct i40e_hw *hw,
994 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -0400995 u8 *bytes, int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000996{
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -0400997 i40e_status status = 0;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000998 enum i40e_nvmupd_cmd upd_cmd;
Shannon Nelson2c47e352015-02-21 06:45:10 +0000999 bool retry_attempt = false;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001000
Shannon Nelson79afe832015-07-23 16:54:33 -04001001 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, perrno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001002
Shannon Nelson2c47e352015-02-21 06:45:10 +00001003retry:
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001004 switch (upd_cmd) {
1005 case I40E_NVMUPD_WRITE_CON:
Shannon Nelson79afe832015-07-23 16:54:33 -04001006 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
Shannon Nelsonfed2db92016-04-12 08:30:43 -07001007 if (!status) {
1008 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001009 hw->nvmupd_state = I40E_NVMUPD_STATE_WRITE_WAIT;
Shannon Nelsonfed2db92016-04-12 08:30:43 -07001010 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001011 break;
1012
1013 case I40E_NVMUPD_WRITE_LCB:
Shannon Nelson79afe832015-07-23 16:54:33 -04001014 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, perrno);
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001015 if (status) {
1016 *perrno = hw->aq.asq_last_status ?
1017 i40e_aq_rc_to_posix(status,
1018 hw->aq.asq_last_status) :
1019 -EIO;
1020 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
1021 } else {
Shannon Nelson437f82a2016-04-01 03:56:09 -07001022 hw->nvm_release_on_done = true;
Shannon Nelsonfed2db92016-04-12 08:30:43 -07001023 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001024 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
1025 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001026 break;
1027
1028 case I40E_NVMUPD_CSUM_CON:
1029 status = i40e_update_nvm_checksum(hw);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001030 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -04001031 *perrno = hw->aq.asq_last_status ?
Shannon Nelsonbf848f32014-11-13 08:23:22 +00001032 i40e_aq_rc_to_posix(status,
1033 hw->aq.asq_last_status) :
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001034 -EIO;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001035 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001036 } else {
Shannon Nelsonfed2db92016-04-12 08:30:43 -07001037 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001038 hw->nvmupd_state = I40E_NVMUPD_STATE_WRITE_WAIT;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001039 }
1040 break;
1041
Shannon Nelson0fdd0522014-11-13 08:23:20 +00001042 case I40E_NVMUPD_CSUM_LCB:
1043 status = i40e_update_nvm_checksum(hw);
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001044 if (status) {
Shannon Nelson79afe832015-07-23 16:54:33 -04001045 *perrno = hw->aq.asq_last_status ?
Shannon Nelsonbf848f32014-11-13 08:23:22 +00001046 i40e_aq_rc_to_posix(status,
1047 hw->aq.asq_last_status) :
Shannon Nelson0fdd0522014-11-13 08:23:20 +00001048 -EIO;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001049 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
1050 } else {
Shannon Nelson437f82a2016-04-01 03:56:09 -07001051 hw->nvm_release_on_done = true;
Shannon Nelsonfed2db92016-04-12 08:30:43 -07001052 hw->nvm_wait_opcode = i40e_aqc_opc_nvm_update;
Shannon Nelson2f1b5bc2015-08-28 17:55:49 -04001053 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
1054 }
Shannon Nelson0fdd0522014-11-13 08:23:20 +00001055 break;
1056
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001057 default:
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001058 i40e_debug(hw, I40E_DEBUG_NVM,
1059 "NVMUPD: bad cmd %s in writing state.\n",
1060 i40e_nvm_update_state_str[upd_cmd]);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001061 status = I40E_NOT_SUPPORTED;
Shannon Nelson79afe832015-07-23 16:54:33 -04001062 *perrno = -ESRCH;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001063 break;
1064 }
Shannon Nelson2c47e352015-02-21 06:45:10 +00001065
1066 /* In some circumstances, a multi-write transaction takes longer
1067 * than the default 3 minute timeout on the write semaphore. If
1068 * the write failed with an EBUSY status, this is likely the problem,
1069 * so here we try to reacquire the semaphore then retry the write.
1070 * We only do one retry, then give up.
1071 */
1072 if (status && (hw->aq.asq_last_status == I40E_AQ_RC_EBUSY) &&
1073 !retry_attempt) {
1074 i40e_status old_status = status;
1075 u32 old_asq_status = hw->aq.asq_last_status;
1076 u32 gtime;
1077
1078 gtime = rd32(hw, I40E_GLVFGEN_TIMER);
1079 if (gtime >= hw->nvm.hw_semaphore_timeout) {
1080 i40e_debug(hw, I40E_DEBUG_ALL,
1081 "NVMUPD: write semaphore expired (%d >= %lld), retrying\n",
1082 gtime, hw->nvm.hw_semaphore_timeout);
1083 i40e_release_nvm(hw);
1084 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
1085 if (status) {
1086 i40e_debug(hw, I40E_DEBUG_ALL,
1087 "NVMUPD: write semaphore reacquire failed aq_err = %d\n",
1088 hw->aq.asq_last_status);
1089 status = old_status;
1090 hw->aq.asq_last_status = old_asq_status;
1091 } else {
1092 retry_attempt = true;
1093 goto retry;
1094 }
1095 }
1096 }
1097
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001098 return status;
1099}
1100
1101/**
Shannon Nelsonbab2fb62016-04-01 03:56:11 -07001102 * i40e_nvmupd_check_wait_event - handle NVM update operation events
1103 * @hw: pointer to the hardware structure
1104 * @opcode: the event that just happened
1105 **/
1106void i40e_nvmupd_check_wait_event(struct i40e_hw *hw, u16 opcode)
1107{
Shannon Nelsonfed2db92016-04-12 08:30:43 -07001108 if (opcode == hw->nvm_wait_opcode) {
Shannon Nelsonbab2fb62016-04-01 03:56:11 -07001109 i40e_debug(hw, I40E_DEBUG_NVM,
1110 "NVMUPD: clearing wait on opcode 0x%04x\n", opcode);
1111 if (hw->nvm_release_on_done) {
1112 i40e_release_nvm(hw);
1113 hw->nvm_release_on_done = false;
1114 }
Shannon Nelsonfed2db92016-04-12 08:30:43 -07001115 hw->nvm_wait_opcode = 0;
Shannon Nelsonbab2fb62016-04-01 03:56:11 -07001116
Maciej Sosin81fa7c92016-10-11 15:26:57 -07001117 if (hw->aq.arq_last_status) {
1118 hw->nvmupd_state = I40E_NVMUPD_STATE_ERROR;
1119 return;
1120 }
1121
Shannon Nelsonbab2fb62016-04-01 03:56:11 -07001122 switch (hw->nvmupd_state) {
1123 case I40E_NVMUPD_STATE_INIT_WAIT:
1124 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
1125 break;
1126
1127 case I40E_NVMUPD_STATE_WRITE_WAIT:
1128 hw->nvmupd_state = I40E_NVMUPD_STATE_WRITING;
1129 break;
1130
1131 default:
1132 break;
1133 }
1134 }
1135}
1136
1137/**
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001138 * i40e_nvmupd_validate_command - Validate given command
1139 * @hw: pointer to hardware structure
1140 * @cmd: pointer to nvm update command buffer
Shannon Nelson79afe832015-07-23 16:54:33 -04001141 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001142 *
1143 * Return one of the valid command types or I40E_NVMUPD_INVALID
1144 **/
1145static enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
1146 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -04001147 int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001148{
1149 enum i40e_nvmupd_cmd upd_cmd;
Shannon Nelson0af8e9d2015-08-28 17:55:48 -04001150 u8 module, transaction;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001151
1152 /* anything that doesn't match a recognized case is an error */
1153 upd_cmd = I40E_NVMUPD_INVALID;
1154
1155 transaction = i40e_nvmupd_get_transaction(cmd->config);
Shannon Nelson0af8e9d2015-08-28 17:55:48 -04001156 module = i40e_nvmupd_get_module(cmd->config);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001157
1158 /* limits on data size */
1159 if ((cmd->data_size < 1) ||
1160 (cmd->data_size > I40E_NVMUPD_MAX_DATA)) {
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001161 i40e_debug(hw, I40E_DEBUG_NVM,
1162 "i40e_nvmupd_validate_command data_size %d\n",
1163 cmd->data_size);
Shannon Nelson79afe832015-07-23 16:54:33 -04001164 *perrno = -EFAULT;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001165 return I40E_NVMUPD_INVALID;
1166 }
1167
1168 switch (cmd->command) {
1169 case I40E_NVM_READ:
1170 switch (transaction) {
1171 case I40E_NVM_CON:
1172 upd_cmd = I40E_NVMUPD_READ_CON;
1173 break;
1174 case I40E_NVM_SNT:
1175 upd_cmd = I40E_NVMUPD_READ_SNT;
1176 break;
1177 case I40E_NVM_LCB:
1178 upd_cmd = I40E_NVMUPD_READ_LCB;
1179 break;
1180 case I40E_NVM_SA:
1181 upd_cmd = I40E_NVMUPD_READ_SA;
1182 break;
Shannon Nelson0af8e9d2015-08-28 17:55:48 -04001183 case I40E_NVM_EXEC:
1184 if (module == 0xf)
1185 upd_cmd = I40E_NVMUPD_STATUS;
Shannon Nelsonb72dc7b2015-08-28 17:55:51 -04001186 else if (module == 0)
1187 upd_cmd = I40E_NVMUPD_GET_AQ_RESULT;
Shannon Nelson0af8e9d2015-08-28 17:55:48 -04001188 break;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001189 }
1190 break;
1191
1192 case I40E_NVM_WRITE:
1193 switch (transaction) {
1194 case I40E_NVM_CON:
1195 upd_cmd = I40E_NVMUPD_WRITE_CON;
1196 break;
1197 case I40E_NVM_SNT:
1198 upd_cmd = I40E_NVMUPD_WRITE_SNT;
1199 break;
1200 case I40E_NVM_LCB:
1201 upd_cmd = I40E_NVMUPD_WRITE_LCB;
1202 break;
1203 case I40E_NVM_SA:
1204 upd_cmd = I40E_NVMUPD_WRITE_SA;
1205 break;
1206 case I40E_NVM_ERA:
1207 upd_cmd = I40E_NVMUPD_WRITE_ERA;
1208 break;
1209 case I40E_NVM_CSUM:
1210 upd_cmd = I40E_NVMUPD_CSUM_CON;
1211 break;
1212 case (I40E_NVM_CSUM|I40E_NVM_SA):
1213 upd_cmd = I40E_NVMUPD_CSUM_SA;
1214 break;
1215 case (I40E_NVM_CSUM|I40E_NVM_LCB):
1216 upd_cmd = I40E_NVMUPD_CSUM_LCB;
1217 break;
Shannon Nelsone4c83c22015-08-28 17:55:50 -04001218 case I40E_NVM_EXEC:
1219 if (module == 0)
1220 upd_cmd = I40E_NVMUPD_EXEC_AQ;
1221 break;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001222 }
1223 break;
1224 }
1225
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001226 return upd_cmd;
1227}
1228
1229/**
Shannon Nelsone4c83c22015-08-28 17:55:50 -04001230 * i40e_nvmupd_exec_aq - Run an AQ command
1231 * @hw: pointer to hardware structure
1232 * @cmd: pointer to nvm update command buffer
1233 * @bytes: pointer to the data buffer
1234 * @perrno: pointer to return error code
1235 *
1236 * cmd structure contains identifiers and data buffer
1237 **/
1238static i40e_status i40e_nvmupd_exec_aq(struct i40e_hw *hw,
1239 struct i40e_nvm_access *cmd,
1240 u8 *bytes, int *perrno)
1241{
1242 struct i40e_asq_cmd_details cmd_details;
1243 i40e_status status;
1244 struct i40e_aq_desc *aq_desc;
1245 u32 buff_size = 0;
1246 u8 *buff = NULL;
1247 u32 aq_desc_len;
1248 u32 aq_data_len;
1249
1250 i40e_debug(hw, I40E_DEBUG_NVM, "NVMUPD: %s\n", __func__);
1251 memset(&cmd_details, 0, sizeof(cmd_details));
1252 cmd_details.wb_desc = &hw->nvm_wb_desc;
1253
1254 aq_desc_len = sizeof(struct i40e_aq_desc);
1255 memset(&hw->nvm_wb_desc, 0, aq_desc_len);
1256
1257 /* get the aq descriptor */
1258 if (cmd->data_size < aq_desc_len) {
1259 i40e_debug(hw, I40E_DEBUG_NVM,
1260 "NVMUPD: not enough aq desc bytes for exec, size %d < %d\n",
1261 cmd->data_size, aq_desc_len);
1262 *perrno = -EINVAL;
1263 return I40E_ERR_PARAM;
1264 }
1265 aq_desc = (struct i40e_aq_desc *)bytes;
1266
1267 /* if data buffer needed, make sure it's ready */
1268 aq_data_len = cmd->data_size - aq_desc_len;
1269 buff_size = max_t(u32, aq_data_len, le16_to_cpu(aq_desc->datalen));
1270 if (buff_size) {
1271 if (!hw->nvm_buff.va) {
1272 status = i40e_allocate_virt_mem(hw, &hw->nvm_buff,
1273 hw->aq.asq_buf_size);
1274 if (status)
1275 i40e_debug(hw, I40E_DEBUG_NVM,
1276 "NVMUPD: i40e_allocate_virt_mem for exec buff failed, %d\n",
1277 status);
1278 }
1279
1280 if (hw->nvm_buff.va) {
1281 buff = hw->nvm_buff.va;
1282 memcpy(buff, &bytes[aq_desc_len], aq_data_len);
1283 }
1284 }
1285
1286 /* and away we go! */
1287 status = i40e_asq_send_command(hw, aq_desc, buff,
1288 buff_size, &cmd_details);
1289 if (status) {
1290 i40e_debug(hw, I40E_DEBUG_NVM,
1291 "i40e_nvmupd_exec_aq err %s aq_err %s\n",
1292 i40e_stat_str(hw, status),
1293 i40e_aq_str(hw, hw->aq.asq_last_status));
1294 *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
1295 }
1296
Shannon Nelsonfed2db92016-04-12 08:30:43 -07001297 /* should we wait for a followup event? */
1298 if (cmd->offset) {
1299 hw->nvm_wait_opcode = cmd->offset;
1300 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT_WAIT;
1301 }
1302
Shannon Nelsone4c83c22015-08-28 17:55:50 -04001303 return status;
1304}
1305
1306/**
Shannon Nelsonb72dc7b2015-08-28 17:55:51 -04001307 * i40e_nvmupd_get_aq_result - Get the results from the previous exec_aq
1308 * @hw: pointer to hardware structure
1309 * @cmd: pointer to nvm update command buffer
1310 * @bytes: pointer to the data buffer
1311 * @perrno: pointer to return error code
1312 *
1313 * cmd structure contains identifiers and data buffer
1314 **/
1315static i40e_status i40e_nvmupd_get_aq_result(struct i40e_hw *hw,
1316 struct i40e_nvm_access *cmd,
1317 u8 *bytes, int *perrno)
1318{
1319 u32 aq_total_len;
1320 u32 aq_desc_len;
1321 int remainder;
1322 u8 *buff;
1323
1324 i40e_debug(hw, I40E_DEBUG_NVM, "NVMUPD: %s\n", __func__);
1325
1326 aq_desc_len = sizeof(struct i40e_aq_desc);
1327 aq_total_len = aq_desc_len + le16_to_cpu(hw->nvm_wb_desc.datalen);
1328
1329 /* check offset range */
1330 if (cmd->offset > aq_total_len) {
1331 i40e_debug(hw, I40E_DEBUG_NVM, "%s: offset too big %d > %d\n",
1332 __func__, cmd->offset, aq_total_len);
1333 *perrno = -EINVAL;
1334 return I40E_ERR_PARAM;
1335 }
1336
1337 /* check copylength range */
1338 if (cmd->data_size > (aq_total_len - cmd->offset)) {
1339 int new_len = aq_total_len - cmd->offset;
1340
1341 i40e_debug(hw, I40E_DEBUG_NVM, "%s: copy length %d too big, trimming to %d\n",
1342 __func__, cmd->data_size, new_len);
1343 cmd->data_size = new_len;
1344 }
1345
1346 remainder = cmd->data_size;
1347 if (cmd->offset < aq_desc_len) {
1348 u32 len = aq_desc_len - cmd->offset;
1349
1350 len = min(len, cmd->data_size);
1351 i40e_debug(hw, I40E_DEBUG_NVM, "%s: aq_desc bytes %d to %d\n",
1352 __func__, cmd->offset, cmd->offset + len);
1353
1354 buff = ((u8 *)&hw->nvm_wb_desc) + cmd->offset;
1355 memcpy(bytes, buff, len);
1356
1357 bytes += len;
1358 remainder -= len;
1359 buff = hw->nvm_buff.va;
1360 } else {
1361 buff = hw->nvm_buff.va + (cmd->offset - aq_desc_len);
1362 }
1363
1364 if (remainder > 0) {
1365 int start_byte = buff - (u8 *)hw->nvm_buff.va;
1366
1367 i40e_debug(hw, I40E_DEBUG_NVM, "%s: databuf bytes %d to %d\n",
1368 __func__, start_byte, start_byte + remainder);
1369 memcpy(bytes, buff, remainder);
1370 }
1371
1372 return 0;
1373}
1374
1375/**
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001376 * i40e_nvmupd_nvm_read - Read NVM
1377 * @hw: pointer to hardware structure
1378 * @cmd: pointer to nvm update command buffer
1379 * @bytes: pointer to the data buffer
Shannon Nelson79afe832015-07-23 16:54:33 -04001380 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001381 *
1382 * cmd structure contains identifiers and data buffer
1383 **/
1384static i40e_status i40e_nvmupd_nvm_read(struct i40e_hw *hw,
1385 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -04001386 u8 *bytes, int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001387{
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001388 struct i40e_asq_cmd_details cmd_details;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001389 i40e_status status;
1390 u8 module, transaction;
1391 bool last;
1392
1393 transaction = i40e_nvmupd_get_transaction(cmd->config);
1394 module = i40e_nvmupd_get_module(cmd->config);
1395 last = (transaction == I40E_NVM_LCB) || (transaction == I40E_NVM_SA);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001396
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001397 memset(&cmd_details, 0, sizeof(cmd_details));
1398 cmd_details.wb_desc = &hw->nvm_wb_desc;
1399
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001400 status = i40e_aq_read_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001401 bytes, last, &cmd_details);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001402 if (status) {
1403 i40e_debug(hw, I40E_DEBUG_NVM,
1404 "i40e_nvmupd_nvm_read mod 0x%x off 0x%x len 0x%x\n",
1405 module, cmd->offset, cmd->data_size);
1406 i40e_debug(hw, I40E_DEBUG_NVM,
1407 "i40e_nvmupd_nvm_read status %d aq %d\n",
1408 status, hw->aq.asq_last_status);
Shannon Nelson79afe832015-07-23 16:54:33 -04001409 *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001410 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001411
1412 return status;
1413}
1414
1415/**
1416 * i40e_nvmupd_nvm_erase - Erase an NVM module
1417 * @hw: pointer to hardware structure
1418 * @cmd: pointer to nvm update command buffer
Shannon Nelson79afe832015-07-23 16:54:33 -04001419 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001420 *
1421 * module, offset, data_size and data are in cmd structure
1422 **/
1423static i40e_status i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
1424 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -04001425 int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001426{
1427 i40e_status status = 0;
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001428 struct i40e_asq_cmd_details cmd_details;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001429 u8 module, transaction;
1430 bool last;
1431
1432 transaction = i40e_nvmupd_get_transaction(cmd->config);
1433 module = i40e_nvmupd_get_module(cmd->config);
1434 last = (transaction & I40E_NVM_LCB);
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001435
1436 memset(&cmd_details, 0, sizeof(cmd_details));
1437 cmd_details.wb_desc = &hw->nvm_wb_desc;
1438
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001439 status = i40e_aq_erase_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001440 last, &cmd_details);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001441 if (status) {
1442 i40e_debug(hw, I40E_DEBUG_NVM,
1443 "i40e_nvmupd_nvm_erase mod 0x%x off 0x%x len 0x%x\n",
1444 module, cmd->offset, cmd->data_size);
1445 i40e_debug(hw, I40E_DEBUG_NVM,
1446 "i40e_nvmupd_nvm_erase status %d aq %d\n",
1447 status, hw->aq.asq_last_status);
Shannon Nelson79afe832015-07-23 16:54:33 -04001448 *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001449 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001450
1451 return status;
1452}
1453
1454/**
1455 * i40e_nvmupd_nvm_write - Write NVM
1456 * @hw: pointer to hardware structure
1457 * @cmd: pointer to nvm update command buffer
1458 * @bytes: pointer to the data buffer
Shannon Nelson79afe832015-07-23 16:54:33 -04001459 * @perrno: pointer to return error code
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001460 *
1461 * module, offset, data_size and data are in cmd structure
1462 **/
1463static i40e_status i40e_nvmupd_nvm_write(struct i40e_hw *hw,
1464 struct i40e_nvm_access *cmd,
Shannon Nelson79afe832015-07-23 16:54:33 -04001465 u8 *bytes, int *perrno)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001466{
1467 i40e_status status = 0;
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001468 struct i40e_asq_cmd_details cmd_details;
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001469 u8 module, transaction;
1470 bool last;
1471
1472 transaction = i40e_nvmupd_get_transaction(cmd->config);
1473 module = i40e_nvmupd_get_module(cmd->config);
1474 last = (transaction & I40E_NVM_LCB);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001475
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001476 memset(&cmd_details, 0, sizeof(cmd_details));
1477 cmd_details.wb_desc = &hw->nvm_wb_desc;
1478
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001479 status = i40e_aq_update_nvm(hw, module, cmd->offset,
Shannon Nelson6b5c1b82015-08-28 17:55:47 -04001480 (u16)cmd->data_size, bytes, last,
1481 &cmd_details);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001482 if (status) {
1483 i40e_debug(hw, I40E_DEBUG_NVM,
1484 "i40e_nvmupd_nvm_write mod 0x%x off 0x%x len 0x%x\n",
1485 module, cmd->offset, cmd->data_size);
1486 i40e_debug(hw, I40E_DEBUG_NVM,
1487 "i40e_nvmupd_nvm_write status %d aq %d\n",
1488 status, hw->aq.asq_last_status);
Shannon Nelson79afe832015-07-23 16:54:33 -04001489 *perrno = i40e_aq_rc_to_posix(status, hw->aq.asq_last_status);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +00001490 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +00001491
1492 return status;
1493}