blob: df429bb4eb44a2135121f9ae188e3b96e7affb3b [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 Brandeburg56a62fc2013-09-11 08:40:12 +000053 nvm->sr_size = (1 << sr_size) * I40E_SR_WORDS_IN_1KB;
54
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;
83 u64 time = 0;
84
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,
89 0, &time, 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 */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000094 hw->nvm.hw_semaphore_timeout = I40E_MS_TO_GTIME(time) + gtime;
95
96 if (ret_code) {
Shannon Nelson3e261862014-02-06 05:51:06 +000097 /* Set the polling timeout */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +000098 if (time > I40E_MAX_NVM_TIMEOUT)
99 timeout = I40E_MS_TO_GTIME(I40E_MAX_NVM_TIMEOUT)
100 + gtime;
101 else
102 timeout = hw->nvm.hw_semaphore_timeout;
Shannon Nelson3e261862014-02-06 05:51:06 +0000103 /* Poll until the current NVM owner timeouts */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000104 while (gtime < timeout) {
105 usleep_range(10000, 20000);
106 ret_code = i40e_aq_request_resource(hw,
107 I40E_NVM_RESOURCE_ID,
108 access, 0, &time,
109 NULL);
110 if (!ret_code) {
111 hw->nvm.hw_semaphore_timeout =
112 I40E_MS_TO_GTIME(time) + gtime;
113 break;
114 }
115 gtime = rd32(hw, I40E_GLVFGEN_TIMER);
116 }
117 if (ret_code) {
118 hw->nvm.hw_semaphore_timeout = 0;
119 hw->nvm.hw_semaphore_wait =
120 I40E_MS_TO_GTIME(time) + gtime;
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000121 i40e_debug(hw, I40E_DEBUG_NVM,
122 "NVM acquire timed out, wait %llu ms before trying again.\n",
123 time);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000124 }
125 }
126
127i40e_i40e_acquire_nvm_exit:
128 return ret_code;
129}
130
131/**
Shannon Nelson3e261862014-02-06 05:51:06 +0000132 * i40e_release_nvm - Generic request for releasing the NVM ownership
133 * @hw: pointer to the HW structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000134 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000135 * This function will release NVM resource via the proper Admin Command.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000136 **/
137void i40e_release_nvm(struct i40e_hw *hw)
138{
139 if (!hw->nvm.blank_nvm_mode)
140 i40e_aq_release_resource(hw, I40E_NVM_RESOURCE_ID, 0, NULL);
141}
142
143/**
Shannon Nelson3e261862014-02-06 05:51:06 +0000144 * i40e_poll_sr_srctl_done_bit - Polls the GLNVM_SRCTL done bit
145 * @hw: pointer to the HW structure
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000146 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000147 * Polls the SRCTL Shadow RAM register done bit.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000148 **/
149static i40e_status i40e_poll_sr_srctl_done_bit(struct i40e_hw *hw)
150{
151 i40e_status ret_code = I40E_ERR_TIMEOUT;
152 u32 srctl, wait_cnt;
153
Shannon Nelson3e261862014-02-06 05:51:06 +0000154 /* Poll the I40E_GLNVM_SRCTL until the done bit is set */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000155 for (wait_cnt = 0; wait_cnt < I40E_SRRD_SRCTL_ATTEMPTS; wait_cnt++) {
156 srctl = rd32(hw, I40E_GLNVM_SRCTL);
157 if (srctl & I40E_GLNVM_SRCTL_DONE_MASK) {
158 ret_code = 0;
159 break;
160 }
161 udelay(5);
162 }
163 if (ret_code == I40E_ERR_TIMEOUT)
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000164 i40e_debug(hw, I40E_DEBUG_NVM, "Done bit in GLNVM_SRCTL not set");
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000165 return ret_code;
166}
167
168/**
Shannon Nelson3e261862014-02-06 05:51:06 +0000169 * i40e_read_nvm_word - Reads Shadow RAM
170 * @hw: pointer to the HW structure
171 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
172 * @data: word read from the Shadow RAM
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000173 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000174 * Reads one 16 bit word from the Shadow RAM using the GLNVM_SRCTL register.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000175 **/
Shannon Nelsona4bcfbb2013-12-11 08:17:15 +0000176i40e_status i40e_read_nvm_word(struct i40e_hw *hw, u16 offset,
177 u16 *data)
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000178{
179 i40e_status ret_code = I40E_ERR_TIMEOUT;
180 u32 sr_reg;
181
182 if (offset >= hw->nvm.sr_size) {
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000183 i40e_debug(hw, I40E_DEBUG_NVM,
184 "NVM read error: offset %d beyond Shadow RAM limit %d\n",
185 offset, hw->nvm.sr_size);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000186 ret_code = I40E_ERR_PARAM;
187 goto read_nvm_exit;
188 }
189
Shannon Nelson3e261862014-02-06 05:51:06 +0000190 /* Poll the done bit first */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000191 ret_code = i40e_poll_sr_srctl_done_bit(hw);
192 if (!ret_code) {
Shannon Nelson3e261862014-02-06 05:51:06 +0000193 /* Write the address and start reading */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000194 sr_reg = (u32)(offset << I40E_GLNVM_SRCTL_ADDR_SHIFT) |
195 (1 << I40E_GLNVM_SRCTL_START_SHIFT);
196 wr32(hw, I40E_GLNVM_SRCTL, sr_reg);
197
Shannon Nelson3e261862014-02-06 05:51:06 +0000198 /* Poll I40E_GLNVM_SRCTL until the done bit is set */
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000199 ret_code = i40e_poll_sr_srctl_done_bit(hw);
200 if (!ret_code) {
201 sr_reg = rd32(hw, I40E_GLNVM_SRDATA);
202 *data = (u16)((sr_reg &
203 I40E_GLNVM_SRDATA_RDDATA_MASK)
204 >> I40E_GLNVM_SRDATA_RDDATA_SHIFT);
205 }
206 }
207 if (ret_code)
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000208 i40e_debug(hw, I40E_DEBUG_NVM,
209 "NVM read error: Couldn't access Shadow RAM address: 0x%x\n",
210 offset);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000211
212read_nvm_exit:
213 return ret_code;
214}
215
216/**
Shannon Nelson3e261862014-02-06 05:51:06 +0000217 * i40e_read_nvm_buffer - Reads Shadow RAM buffer
218 * @hw: pointer to the HW structure
219 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF).
220 * @words: (in) number of words to read; (out) number of words actually read
221 * @data: words read from the Shadow RAM
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000222 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000223 * Reads 16 bit words (data buffer) from the SR using the i40e_read_nvm_srrd()
224 * method. The buffer read is preceded by the NVM ownership take
225 * and followed by the release.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000226 **/
227i40e_status i40e_read_nvm_buffer(struct i40e_hw *hw, u16 offset,
228 u16 *words, u16 *data)
229{
230 i40e_status ret_code = 0;
231 u16 index, word;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000232
Shannon Nelson3e261862014-02-06 05:51:06 +0000233 /* Loop thru the selected region */
Shannon Nelsona4bcfbb2013-12-11 08:17:15 +0000234 for (word = 0; word < *words; word++) {
235 index = offset + word;
236 ret_code = i40e_read_nvm_word(hw, index, &data[word]);
237 if (ret_code)
238 break;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000239 }
240
Shannon Nelson3e261862014-02-06 05:51:06 +0000241 /* Update the number of words read from the Shadow RAM */
Shannon Nelsona4bcfbb2013-12-11 08:17:15 +0000242 *words = word;
243
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000244 return ret_code;
245}
246
247/**
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000248 * i40e_write_nvm_aq - Writes Shadow RAM.
249 * @hw: pointer to the HW structure.
250 * @module_pointer: module pointer location in words from the NVM beginning
251 * @offset: offset in words from module start
252 * @words: number of words to write
253 * @data: buffer with words to write to the Shadow RAM
254 * @last_command: tells the AdminQ that this is the last command
255 *
256 * Writes a 16 bit words buffer to the Shadow RAM using the admin command.
257 **/
Wei Yongjun952d9632014-07-30 09:02:53 +0000258static i40e_status i40e_write_nvm_aq(struct i40e_hw *hw, u8 module_pointer,
259 u32 offset, u16 words, void *data,
260 bool last_command)
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000261{
262 i40e_status ret_code = I40E_ERR_NVM;
263
264 /* Here we are checking the SR limit only for the flat memory model.
265 * We cannot do it for the module-based model, as we did not acquire
266 * the NVM resource yet (we cannot get the module pointer value).
267 * Firmware will check the module-based model.
268 */
269 if ((offset + words) > hw->nvm.sr_size)
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000270 i40e_debug(hw, I40E_DEBUG_NVM,
271 "NVM write error: offset %d beyond Shadow RAM limit %d\n",
272 (offset + words), hw->nvm.sr_size);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000273 else if (words > I40E_SR_SECTOR_SIZE_IN_WORDS)
274 /* We can write only up to 4KB (one sector), in one AQ write */
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000275 i40e_debug(hw, I40E_DEBUG_NVM,
276 "NVM write fail error: tried to write %d words, limit is %d.\n",
277 words, I40E_SR_SECTOR_SIZE_IN_WORDS);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000278 else if (((offset + (words - 1)) / I40E_SR_SECTOR_SIZE_IN_WORDS)
279 != (offset / I40E_SR_SECTOR_SIZE_IN_WORDS))
280 /* A single write cannot spread over two sectors */
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000281 i40e_debug(hw, I40E_DEBUG_NVM,
282 "NVM write error: cannot spread over two sectors in a single write offset=%d words=%d\n",
283 offset, words);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000284 else
285 ret_code = i40e_aq_update_nvm(hw, module_pointer,
286 2 * offset, /*bytes*/
287 2 * words, /*bytes*/
288 data, last_command, NULL);
289
290 return ret_code;
291}
292
293/**
Shannon Nelson3e261862014-02-06 05:51:06 +0000294 * i40e_calc_nvm_checksum - Calculates and returns the checksum
295 * @hw: pointer to hardware structure
296 * @checksum: pointer to the checksum
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000297 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000298 * This function calculates SW Checksum that covers the whole 64kB shadow RAM
299 * except the VPD and PCIe ALT Auto-load modules. The structure and size of VPD
300 * is customer specific and unknown. Therefore, this function skips all maximum
301 * possible size of VPD (1kB).
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000302 **/
303static i40e_status i40e_calc_nvm_checksum(struct i40e_hw *hw,
304 u16 *checksum)
305{
306 i40e_status ret_code = 0;
307 u16 pcie_alt_module = 0;
308 u16 checksum_local = 0;
309 u16 vpd_module = 0;
310 u16 word = 0;
311 u32 i = 0;
312
313 /* read pointer to VPD area */
Shannon Nelsona4bcfbb2013-12-11 08:17:15 +0000314 ret_code = i40e_read_nvm_word(hw, I40E_SR_VPD_PTR, &vpd_module);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000315 if (ret_code) {
316 ret_code = I40E_ERR_NVM_CHECKSUM;
317 goto i40e_calc_nvm_checksum_exit;
318 }
319
320 /* read pointer to PCIe Alt Auto-load module */
Shannon Nelsona4bcfbb2013-12-11 08:17:15 +0000321 ret_code = i40e_read_nvm_word(hw, I40E_SR_PCIE_ALT_AUTO_LOAD_PTR,
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000322 &pcie_alt_module);
323 if (ret_code) {
324 ret_code = I40E_ERR_NVM_CHECKSUM;
325 goto i40e_calc_nvm_checksum_exit;
326 }
327
328 /* Calculate SW checksum that covers the whole 64kB shadow RAM
329 * except the VPD and PCIe ALT Auto-load modules
330 */
331 for (i = 0; i < hw->nvm.sr_size; i++) {
332 /* Skip Checksum word */
333 if (i == I40E_SR_SW_CHECKSUM_WORD)
334 i++;
335 /* Skip VPD module (convert byte size to word count) */
336 if (i == (u32)vpd_module) {
337 i += (I40E_SR_VPD_MODULE_MAX_SIZE / 2);
338 if (i >= hw->nvm.sr_size)
339 break;
340 }
341 /* Skip PCIe ALT module (convert byte size to word count) */
342 if (i == (u32)pcie_alt_module) {
343 i += (I40E_SR_PCIE_ALT_MODULE_MAX_SIZE / 2);
344 if (i >= hw->nvm.sr_size)
345 break;
346 }
347
Shannon Nelsona4bcfbb2013-12-11 08:17:15 +0000348 ret_code = i40e_read_nvm_word(hw, (u16)i, &word);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000349 if (ret_code) {
350 ret_code = I40E_ERR_NVM_CHECKSUM;
351 goto i40e_calc_nvm_checksum_exit;
352 }
353 checksum_local += word;
354 }
355
356 *checksum = (u16)I40E_SR_SW_CHECKSUM_BASE - checksum_local;
357
358i40e_calc_nvm_checksum_exit:
359 return ret_code;
360}
361
362/**
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000363 * i40e_update_nvm_checksum - Updates the NVM checksum
364 * @hw: pointer to hardware structure
365 *
366 * NVM ownership must be acquired before calling this function and released
367 * on ARQ completion event reception by caller.
368 * This function will commit SR to NVM.
369 **/
370i40e_status i40e_update_nvm_checksum(struct i40e_hw *hw)
371{
372 i40e_status ret_code = 0;
373 u16 checksum;
374
375 ret_code = i40e_calc_nvm_checksum(hw, &checksum);
376 if (!ret_code)
377 ret_code = i40e_write_nvm_aq(hw, 0x00, I40E_SR_SW_CHECKSUM_WORD,
378 1, &checksum, true);
379
380 return ret_code;
381}
382
383/**
Shannon Nelson3e261862014-02-06 05:51:06 +0000384 * i40e_validate_nvm_checksum - Validate EEPROM checksum
385 * @hw: pointer to hardware structure
386 * @checksum: calculated checksum
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000387 *
Shannon Nelson3e261862014-02-06 05:51:06 +0000388 * Performs checksum calculation and validates the NVM SW checksum. If the
389 * caller does not need checksum, the value can be NULL.
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000390 **/
391i40e_status i40e_validate_nvm_checksum(struct i40e_hw *hw,
392 u16 *checksum)
393{
394 i40e_status ret_code = 0;
395 u16 checksum_sr = 0;
Jesse Brandeburge15c9fa2014-01-17 15:36:31 -0800396 u16 checksum_local = 0;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000397
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000398 ret_code = i40e_calc_nvm_checksum(hw, &checksum_local);
399 if (ret_code)
Kamil Krawczyk7a208e82014-06-04 04:22:36 +0000400 goto i40e_validate_nvm_checksum_exit;
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000401
402 /* Do not use i40e_read_nvm_word() because we do not want to take
403 * the synchronization semaphores twice here.
404 */
Shannon Nelsona4bcfbb2013-12-11 08:17:15 +0000405 i40e_read_nvm_word(hw, I40E_SR_SW_CHECKSUM_WORD, &checksum_sr);
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000406
407 /* Verify read checksum from EEPROM is the same as
408 * calculated checksum
409 */
410 if (checksum_local != checksum_sr)
411 ret_code = I40E_ERR_NVM_CHECKSUM;
412
413 /* If the user cares, return the calculated checksum */
414 if (checksum)
415 *checksum = checksum_local;
416
Jesse Brandeburg56a62fc2013-09-11 08:40:12 +0000417i40e_validate_nvm_checksum_exit:
418 return ret_code;
419}
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000420
421static i40e_status i40e_nvmupd_state_init(struct i40e_hw *hw,
422 struct i40e_nvm_access *cmd,
423 u8 *bytes, int *errno);
424static i40e_status i40e_nvmupd_state_reading(struct i40e_hw *hw,
425 struct i40e_nvm_access *cmd,
426 u8 *bytes, int *errno);
427static i40e_status i40e_nvmupd_state_writing(struct i40e_hw *hw,
428 struct i40e_nvm_access *cmd,
429 u8 *bytes, int *errno);
430static enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
431 struct i40e_nvm_access *cmd,
432 int *errno);
433static i40e_status i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
434 struct i40e_nvm_access *cmd,
435 int *errno);
436static i40e_status i40e_nvmupd_nvm_write(struct i40e_hw *hw,
437 struct i40e_nvm_access *cmd,
438 u8 *bytes, int *errno);
439static i40e_status i40e_nvmupd_nvm_read(struct i40e_hw *hw,
440 struct i40e_nvm_access *cmd,
441 u8 *bytes, int *errno);
442static inline u8 i40e_nvmupd_get_module(u32 val)
443{
444 return (u8)(val & I40E_NVM_MOD_PNT_MASK);
445}
446static inline u8 i40e_nvmupd_get_transaction(u32 val)
447{
448 return (u8)((val & I40E_NVM_TRANS_MASK) >> I40E_NVM_TRANS_SHIFT);
449}
450
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000451static char *i40e_nvm_update_state_str[] = {
452 "I40E_NVMUPD_INVALID",
453 "I40E_NVMUPD_READ_CON",
454 "I40E_NVMUPD_READ_SNT",
455 "I40E_NVMUPD_READ_LCB",
456 "I40E_NVMUPD_READ_SA",
457 "I40E_NVMUPD_WRITE_ERA",
458 "I40E_NVMUPD_WRITE_CON",
459 "I40E_NVMUPD_WRITE_SNT",
460 "I40E_NVMUPD_WRITE_LCB",
461 "I40E_NVMUPD_WRITE_SA",
462 "I40E_NVMUPD_CSUM_CON",
463 "I40E_NVMUPD_CSUM_SA",
464 "I40E_NVMUPD_CSUM_LCB",
465};
466
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000467/**
468 * i40e_nvmupd_command - Process an NVM update command
469 * @hw: pointer to hardware structure
470 * @cmd: pointer to nvm update command
471 * @bytes: pointer to the data buffer
472 * @errno: pointer to return error code
473 *
474 * Dispatches command depending on what update state is current
475 **/
476i40e_status i40e_nvmupd_command(struct i40e_hw *hw,
477 struct i40e_nvm_access *cmd,
478 u8 *bytes, int *errno)
479{
480 i40e_status status;
481
482 /* assume success */
483 *errno = 0;
484
485 switch (hw->nvmupd_state) {
486 case I40E_NVMUPD_STATE_INIT:
487 status = i40e_nvmupd_state_init(hw, cmd, bytes, errno);
488 break;
489
490 case I40E_NVMUPD_STATE_READING:
491 status = i40e_nvmupd_state_reading(hw, cmd, bytes, errno);
492 break;
493
494 case I40E_NVMUPD_STATE_WRITING:
495 status = i40e_nvmupd_state_writing(hw, cmd, bytes, errno);
496 break;
497
498 default:
499 /* invalid state, should never happen */
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000500 i40e_debug(hw, I40E_DEBUG_NVM,
501 "NVMUPD: no such state %d\n", hw->nvmupd_state);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000502 status = I40E_NOT_SUPPORTED;
503 *errno = -ESRCH;
504 break;
505 }
506 return status;
507}
508
509/**
510 * i40e_nvmupd_state_init - Handle NVM update state Init
511 * @hw: pointer to hardware structure
512 * @cmd: pointer to nvm update command buffer
513 * @bytes: pointer to the data buffer
514 * @errno: pointer to return error code
515 *
516 * Process legitimate commands of the Init state and conditionally set next
517 * state. Reject all other commands.
518 **/
519static i40e_status i40e_nvmupd_state_init(struct i40e_hw *hw,
520 struct i40e_nvm_access *cmd,
521 u8 *bytes, int *errno)
522{
523 i40e_status status = 0;
524 enum i40e_nvmupd_cmd upd_cmd;
525
526 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, errno);
527
528 switch (upd_cmd) {
529 case I40E_NVMUPD_READ_SA:
530 status = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
531 if (status) {
532 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
533 } else {
534 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, errno);
535 i40e_release_nvm(hw);
536 }
537 break;
538
539 case I40E_NVMUPD_READ_SNT:
540 status = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
541 if (status) {
542 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
543 } else {
544 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, errno);
545 hw->nvmupd_state = I40E_NVMUPD_STATE_READING;
546 }
547 break;
548
549 case I40E_NVMUPD_WRITE_ERA:
550 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
551 if (status) {
552 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
553 } else {
554 status = i40e_nvmupd_nvm_erase(hw, cmd, errno);
555 if (status)
556 i40e_release_nvm(hw);
557 else
558 hw->aq.nvm_release_on_done = true;
559 }
560 break;
561
562 case I40E_NVMUPD_WRITE_SA:
563 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
564 if (status) {
565 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
566 } else {
567 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, errno);
568 if (status)
569 i40e_release_nvm(hw);
570 else
571 hw->aq.nvm_release_on_done = true;
572 }
573 break;
574
575 case I40E_NVMUPD_WRITE_SNT:
576 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
577 if (status) {
578 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
579 } else {
580 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, errno);
581 hw->nvmupd_state = I40E_NVMUPD_STATE_WRITING;
582 }
583 break;
584
585 case I40E_NVMUPD_CSUM_SA:
586 status = i40e_acquire_nvm(hw, I40E_RESOURCE_WRITE);
587 if (status) {
588 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
589 } else {
590 status = i40e_update_nvm_checksum(hw);
591 if (status) {
592 *errno = hw->aq.asq_last_status ?
593 i40e_aq_rc_to_posix(hw->aq.asq_last_status) :
594 -EIO;
595 i40e_release_nvm(hw);
596 } else {
597 hw->aq.nvm_release_on_done = true;
598 }
599 }
600 break;
601
602 default:
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000603 i40e_debug(hw, I40E_DEBUG_NVM,
604 "NVMUPD: bad cmd %s in init state\n",
605 i40e_nvm_update_state_str[upd_cmd]);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000606 status = I40E_ERR_NVM;
607 *errno = -ESRCH;
608 break;
609 }
610 return status;
611}
612
613/**
614 * i40e_nvmupd_state_reading - Handle NVM update state Reading
615 * @hw: pointer to hardware structure
616 * @cmd: pointer to nvm update command buffer
617 * @bytes: pointer to the data buffer
618 * @errno: pointer to return error code
619 *
620 * NVM ownership is already held. Process legitimate commands and set any
621 * change in state; reject all other commands.
622 **/
623static i40e_status i40e_nvmupd_state_reading(struct i40e_hw *hw,
624 struct i40e_nvm_access *cmd,
625 u8 *bytes, int *errno)
626{
627 i40e_status status;
628 enum i40e_nvmupd_cmd upd_cmd;
629
630 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, errno);
631
632 switch (upd_cmd) {
633 case I40E_NVMUPD_READ_SA:
634 case I40E_NVMUPD_READ_CON:
635 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, errno);
636 break;
637
638 case I40E_NVMUPD_READ_LCB:
639 status = i40e_nvmupd_nvm_read(hw, cmd, bytes, errno);
640 i40e_release_nvm(hw);
641 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
642 break;
643
644 default:
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000645 i40e_debug(hw, I40E_DEBUG_NVM,
646 "NVMUPD: bad cmd %s in reading state.\n",
647 i40e_nvm_update_state_str[upd_cmd]);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000648 status = I40E_NOT_SUPPORTED;
649 *errno = -ESRCH;
650 break;
651 }
652 return status;
653}
654
655/**
656 * i40e_nvmupd_state_writing - Handle NVM update state Writing
657 * @hw: pointer to hardware structure
658 * @cmd: pointer to nvm update command buffer
659 * @bytes: pointer to the data buffer
660 * @errno: pointer to return error code
661 *
662 * NVM ownership is already held. Process legitimate commands and set any
663 * change in state; reject all other commands
664 **/
665static i40e_status i40e_nvmupd_state_writing(struct i40e_hw *hw,
666 struct i40e_nvm_access *cmd,
667 u8 *bytes, int *errno)
668{
669 i40e_status status;
670 enum i40e_nvmupd_cmd upd_cmd;
671
672 upd_cmd = i40e_nvmupd_validate_command(hw, cmd, errno);
673
674 switch (upd_cmd) {
675 case I40E_NVMUPD_WRITE_CON:
676 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, errno);
677 break;
678
679 case I40E_NVMUPD_WRITE_LCB:
680 status = i40e_nvmupd_nvm_write(hw, cmd, bytes, errno);
681 if (!status) {
682 hw->aq.nvm_release_on_done = true;
683 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
684 }
685 break;
686
687 case I40E_NVMUPD_CSUM_CON:
688 status = i40e_update_nvm_checksum(hw);
689 if (status)
690 *errno = hw->aq.asq_last_status ?
691 i40e_aq_rc_to_posix(hw->aq.asq_last_status) :
692 -EIO;
693 break;
694
695 case I40E_NVMUPD_CSUM_LCB:
696 status = i40e_update_nvm_checksum(hw);
697 if (status) {
698 *errno = hw->aq.asq_last_status ?
699 i40e_aq_rc_to_posix(hw->aq.asq_last_status) :
700 -EIO;
701 } else {
702 hw->aq.nvm_release_on_done = true;
703 hw->nvmupd_state = I40E_NVMUPD_STATE_INIT;
704 }
705 break;
706
707 default:
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000708 i40e_debug(hw, I40E_DEBUG_NVM,
709 "NVMUPD: bad cmd %s in writing state.\n",
710 i40e_nvm_update_state_str[upd_cmd]);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000711 status = I40E_NOT_SUPPORTED;
712 *errno = -ESRCH;
713 break;
714 }
715 return status;
716}
717
718/**
719 * i40e_nvmupd_validate_command - Validate given command
720 * @hw: pointer to hardware structure
721 * @cmd: pointer to nvm update command buffer
722 * @errno: pointer to return error code
723 *
724 * Return one of the valid command types or I40E_NVMUPD_INVALID
725 **/
726static enum i40e_nvmupd_cmd i40e_nvmupd_validate_command(struct i40e_hw *hw,
727 struct i40e_nvm_access *cmd,
728 int *errno)
729{
730 enum i40e_nvmupd_cmd upd_cmd;
731 u8 transaction, module;
732
733 /* anything that doesn't match a recognized case is an error */
734 upd_cmd = I40E_NVMUPD_INVALID;
735
736 transaction = i40e_nvmupd_get_transaction(cmd->config);
737 module = i40e_nvmupd_get_module(cmd->config);
738
739 /* limits on data size */
740 if ((cmd->data_size < 1) ||
741 (cmd->data_size > I40E_NVMUPD_MAX_DATA)) {
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000742 i40e_debug(hw, I40E_DEBUG_NVM,
743 "i40e_nvmupd_validate_command data_size %d\n",
744 cmd->data_size);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000745 *errno = -EFAULT;
746 return I40E_NVMUPD_INVALID;
747 }
748
749 switch (cmd->command) {
750 case I40E_NVM_READ:
751 switch (transaction) {
752 case I40E_NVM_CON:
753 upd_cmd = I40E_NVMUPD_READ_CON;
754 break;
755 case I40E_NVM_SNT:
756 upd_cmd = I40E_NVMUPD_READ_SNT;
757 break;
758 case I40E_NVM_LCB:
759 upd_cmd = I40E_NVMUPD_READ_LCB;
760 break;
761 case I40E_NVM_SA:
762 upd_cmd = I40E_NVMUPD_READ_SA;
763 break;
764 }
765 break;
766
767 case I40E_NVM_WRITE:
768 switch (transaction) {
769 case I40E_NVM_CON:
770 upd_cmd = I40E_NVMUPD_WRITE_CON;
771 break;
772 case I40E_NVM_SNT:
773 upd_cmd = I40E_NVMUPD_WRITE_SNT;
774 break;
775 case I40E_NVM_LCB:
776 upd_cmd = I40E_NVMUPD_WRITE_LCB;
777 break;
778 case I40E_NVM_SA:
779 upd_cmd = I40E_NVMUPD_WRITE_SA;
780 break;
781 case I40E_NVM_ERA:
782 upd_cmd = I40E_NVMUPD_WRITE_ERA;
783 break;
784 case I40E_NVM_CSUM:
785 upd_cmd = I40E_NVMUPD_CSUM_CON;
786 break;
787 case (I40E_NVM_CSUM|I40E_NVM_SA):
788 upd_cmd = I40E_NVMUPD_CSUM_SA;
789 break;
790 case (I40E_NVM_CSUM|I40E_NVM_LCB):
791 upd_cmd = I40E_NVMUPD_CSUM_LCB;
792 break;
793 }
794 break;
795 }
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000796 i40e_debug(hw, I40E_DEBUG_NVM, "%s\n",
797 i40e_nvm_update_state_str[upd_cmd]);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000798
799 if (upd_cmd == I40E_NVMUPD_INVALID) {
800 *errno = -EFAULT;
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000801 i40e_debug(hw, I40E_DEBUG_NVM,
802 "i40e_nvmupd_validate_command returns %d errno %d\n",
803 upd_cmd, *errno);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000804 }
805 return upd_cmd;
806}
807
808/**
809 * i40e_nvmupd_nvm_read - Read NVM
810 * @hw: pointer to hardware structure
811 * @cmd: pointer to nvm update command buffer
812 * @bytes: pointer to the data buffer
813 * @errno: pointer to return error code
814 *
815 * cmd structure contains identifiers and data buffer
816 **/
817static i40e_status i40e_nvmupd_nvm_read(struct i40e_hw *hw,
818 struct i40e_nvm_access *cmd,
819 u8 *bytes, int *errno)
820{
821 i40e_status status;
822 u8 module, transaction;
823 bool last;
824
825 transaction = i40e_nvmupd_get_transaction(cmd->config);
826 module = i40e_nvmupd_get_module(cmd->config);
827 last = (transaction == I40E_NVM_LCB) || (transaction == I40E_NVM_SA);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000828
829 status = i40e_aq_read_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
830 bytes, last, NULL);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000831 if (status) {
832 i40e_debug(hw, I40E_DEBUG_NVM,
833 "i40e_nvmupd_nvm_read mod 0x%x off 0x%x len 0x%x\n",
834 module, cmd->offset, cmd->data_size);
835 i40e_debug(hw, I40E_DEBUG_NVM,
836 "i40e_nvmupd_nvm_read status %d aq %d\n",
837 status, hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000838 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000839 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000840
841 return status;
842}
843
844/**
845 * i40e_nvmupd_nvm_erase - Erase an NVM module
846 * @hw: pointer to hardware structure
847 * @cmd: pointer to nvm update command buffer
848 * @errno: pointer to return error code
849 *
850 * module, offset, data_size and data are in cmd structure
851 **/
852static i40e_status i40e_nvmupd_nvm_erase(struct i40e_hw *hw,
853 struct i40e_nvm_access *cmd,
854 int *errno)
855{
856 i40e_status status = 0;
857 u8 module, transaction;
858 bool last;
859
860 transaction = i40e_nvmupd_get_transaction(cmd->config);
861 module = i40e_nvmupd_get_module(cmd->config);
862 last = (transaction & I40E_NVM_LCB);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000863 status = i40e_aq_erase_nvm(hw, module, cmd->offset, (u16)cmd->data_size,
864 last, NULL);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000865 if (status) {
866 i40e_debug(hw, I40E_DEBUG_NVM,
867 "i40e_nvmupd_nvm_erase mod 0x%x off 0x%x len 0x%x\n",
868 module, cmd->offset, cmd->data_size);
869 i40e_debug(hw, I40E_DEBUG_NVM,
870 "i40e_nvmupd_nvm_erase status %d aq %d\n",
871 status, hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000872 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000873 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000874
875 return status;
876}
877
878/**
879 * i40e_nvmupd_nvm_write - Write NVM
880 * @hw: pointer to hardware structure
881 * @cmd: pointer to nvm update command buffer
882 * @bytes: pointer to the data buffer
883 * @errno: pointer to return error code
884 *
885 * module, offset, data_size and data are in cmd structure
886 **/
887static i40e_status i40e_nvmupd_nvm_write(struct i40e_hw *hw,
888 struct i40e_nvm_access *cmd,
889 u8 *bytes, int *errno)
890{
891 i40e_status status = 0;
892 u8 module, transaction;
893 bool last;
894
895 transaction = i40e_nvmupd_get_transaction(cmd->config);
896 module = i40e_nvmupd_get_module(cmd->config);
897 last = (transaction & I40E_NVM_LCB);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000898
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000899 status = i40e_aq_update_nvm(hw, module, cmd->offset,
900 (u16)cmd->data_size, bytes, last, NULL);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000901 if (status) {
902 i40e_debug(hw, I40E_DEBUG_NVM,
903 "i40e_nvmupd_nvm_write mod 0x%x off 0x%x len 0x%x\n",
904 module, cmd->offset, cmd->data_size);
905 i40e_debug(hw, I40E_DEBUG_NVM,
906 "i40e_nvmupd_nvm_write status %d aq %d\n",
907 status, hw->aq.asq_last_status);
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000908 *errno = i40e_aq_rc_to_posix(hw->aq.asq_last_status);
Shannon Nelson74d0d0e2014-11-13 08:23:15 +0000909 }
Shannon Nelsoncd552cb2014-07-09 07:46:09 +0000910
911 return status;
912}