71affd18123d0b24ee919d279af5e4408681aeaa
[dpdk.git] / drivers / net / ice / base / ice_nvm.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2001-2020 Intel Corporation
3  */
4
5 #include "ice_common.h"
6
7 /**
8  * ice_aq_read_nvm
9  * @hw: pointer to the HW struct
10  * @module_typeid: module pointer location in words from the NVM beginning
11  * @offset: byte offset from the module beginning
12  * @length: length of the section to be read (in bytes from the offset)
13  * @data: command buffer (size [bytes] = length)
14  * @last_command: tells if this is the last command in a series
15  * @read_shadow_ram: tell if this is a shadow RAM read
16  * @cd: pointer to command details structure or NULL
17  *
18  * Read the NVM using the admin queue commands (0x0701)
19  */
20 enum ice_status
21 ice_aq_read_nvm(struct ice_hw *hw, u16 module_typeid, u32 offset, u16 length,
22                 void *data, bool last_command, bool read_shadow_ram,
23                 struct ice_sq_cd *cd)
24 {
25         struct ice_aq_desc desc;
26         struct ice_aqc_nvm *cmd;
27
28         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
29
30         cmd = &desc.params.nvm;
31
32         if (offset > ICE_AQC_NVM_MAX_OFFSET)
33                 return ICE_ERR_PARAM;
34
35         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_read);
36
37         if (!read_shadow_ram && module_typeid == ICE_AQC_NVM_START_POINT)
38                 cmd->cmd_flags |= ICE_AQC_NVM_FLASH_ONLY;
39
40         /* If this is the last command in a series, set the proper flag. */
41         if (last_command)
42                 cmd->cmd_flags |= ICE_AQC_NVM_LAST_CMD;
43         cmd->module_typeid = CPU_TO_LE16(module_typeid);
44         cmd->offset_low = CPU_TO_LE16(offset & 0xFFFF);
45         cmd->offset_high = (offset >> 16) & 0xFF;
46         cmd->length = CPU_TO_LE16(length);
47
48         return ice_aq_send_cmd(hw, &desc, data, length, cd);
49 }
50
51 /**
52  * ice_read_flat_nvm - Read portion of NVM by flat offset
53  * @hw: pointer to the HW struct
54  * @offset: offset from beginning of NVM
55  * @length: (in) number of bytes to read; (out) number of bytes actually read
56  * @data: buffer to return data in (sized to fit the specified length)
57  * @read_shadow_ram: if true, read from shadow RAM instead of NVM
58  *
59  * Reads a portion of the NVM, as a flat memory space. This function correctly
60  * breaks read requests across Shadow RAM sectors and ensures that no single
61  * read request exceeds the maximum 4KB read for a single AdminQ command.
62  *
63  * Returns a status code on failure. Note that the data pointer may be
64  * partially updated if some reads succeed before a failure.
65  */
66 enum ice_status
67 ice_read_flat_nvm(struct ice_hw *hw, u32 offset, u32 *length, u8 *data,
68                   bool read_shadow_ram)
69 {
70         enum ice_status status;
71         u32 inlen = *length;
72         u32 bytes_read = 0;
73         bool last_cmd;
74
75         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
76
77         *length = 0;
78
79         /* Verify the length of the read if this is for the Shadow RAM */
80         if (read_shadow_ram && ((offset + inlen) > (hw->flash.sr_words * 2u))) {
81                 ice_debug(hw, ICE_DBG_NVM, "NVM error: requested data is beyond Shadow RAM limit\n");
82                 return ICE_ERR_PARAM;
83         }
84
85         do {
86                 u32 read_size, sector_offset;
87
88                 /* ice_aq_read_nvm cannot read more than 4KB at a time.
89                  * Additionally, a read from the Shadow RAM may not cross over
90                  * a sector boundary. Conveniently, the sector size is also
91                  * 4KB.
92                  */
93                 sector_offset = offset % ICE_AQ_MAX_BUF_LEN;
94                 read_size = MIN_T(u32, ICE_AQ_MAX_BUF_LEN - sector_offset,
95                                   inlen - bytes_read);
96
97                 last_cmd = !(bytes_read + read_size < inlen);
98
99                 /* ice_aq_read_nvm takes the length as a u16. Our read_size is
100                  * calculated using a u32, but the ICE_AQ_MAX_BUF_LEN maximum
101                  * size guarantees that it will fit within the 2 bytes.
102                  */
103                 status = ice_aq_read_nvm(hw, ICE_AQC_NVM_START_POINT,
104                                          offset, (u16)read_size,
105                                          data + bytes_read, last_cmd,
106                                          read_shadow_ram, NULL);
107                 if (status)
108                         break;
109
110                 bytes_read += read_size;
111                 offset += read_size;
112         } while (!last_cmd);
113
114         *length = bytes_read;
115         return status;
116 }
117
118 /**
119  * ice_read_sr_word_aq - Reads Shadow RAM via AQ
120  * @hw: pointer to the HW structure
121  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
122  * @data: word read from the Shadow RAM
123  *
124  * Reads one 16 bit word from the Shadow RAM using ice_read_flat_nvm.
125  */
126 static enum ice_status
127 ice_read_sr_word_aq(struct ice_hw *hw, u16 offset, u16 *data)
128 {
129         u32 bytes = sizeof(u16);
130         enum ice_status status;
131         __le16 data_local;
132
133         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
134
135         /* Note that ice_read_flat_nvm checks if the read is past the Shadow
136          * RAM size, and ensures we don't read across a Shadow RAM sector
137          * boundary
138          */
139         status = ice_read_flat_nvm(hw, offset * sizeof(u16), &bytes,
140                                    (_FORCE_ u8 *)&data_local, true);
141         if (status)
142                 return status;
143
144         *data = LE16_TO_CPU(data_local);
145         return ICE_SUCCESS;
146 }
147
148 /**
149  * ice_read_sr_buf_aq - Reads Shadow RAM buf via AQ
150  * @hw: pointer to the HW structure
151  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
152  * @words: (in) number of words to read; (out) number of words actually read
153  * @data: words read from the Shadow RAM
154  *
155  * Reads 16 bit words (data buf) from the Shadow RAM. Ownership of the NVM is
156  * taken before reading the buffer and later released.
157  */
158 static enum ice_status
159 ice_read_sr_buf_aq(struct ice_hw *hw, u16 offset, u16 *words, u16 *data)
160 {
161         u32 bytes = *words * 2, i;
162         enum ice_status status;
163
164         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
165
166         /* ice_read_flat_nvm takes into account the 4KB AdminQ and Shadow RAM
167          * sector restrictions necessary when reading from the NVM.
168          */
169         status = ice_read_flat_nvm(hw, offset * 2, &bytes, (u8 *)data, true);
170
171         /* Report the number of words successfully read */
172         *words = bytes / 2;
173
174         /* Byte swap the words up to the amount we actually read */
175         for (i = 0; i < *words; i++)
176                 data[i] = LE16_TO_CPU(((_FORCE_ __le16 *)data)[i]);
177
178         return status;
179 }
180
181 /**
182  * ice_acquire_nvm - Generic request for acquiring the NVM ownership
183  * @hw: pointer to the HW structure
184  * @access: NVM access type (read or write)
185  *
186  * This function will request NVM ownership.
187  */
188 enum ice_status
189 ice_acquire_nvm(struct ice_hw *hw, enum ice_aq_res_access_type access)
190 {
191         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
192
193         if (hw->flash.blank_nvm_mode)
194                 return ICE_SUCCESS;
195
196         return ice_acquire_res(hw, ICE_NVM_RES_ID, access, ICE_NVM_TIMEOUT);
197 }
198
199 /**
200  * ice_release_nvm - Generic request for releasing the NVM ownership
201  * @hw: pointer to the HW structure
202  *
203  * This function will release NVM ownership.
204  */
205 void ice_release_nvm(struct ice_hw *hw)
206 {
207         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
208
209         if (hw->flash.blank_nvm_mode)
210                 return;
211
212         ice_release_res(hw, ICE_NVM_RES_ID);
213 }
214
215 /**
216  * ice_get_flash_bank_offset - Get offset into requested flash bank
217  * @hw: pointer to the HW structure
218  * @bank: whether to read from the active or inactive flash bank
219  * @module: the module to read from
220  *
221  * Based on the module, lookup the module offset from the beginning of the
222  * flash.
223  *
224  * Returns the flash offset. Note that a value of zero is invalid and must be
225  * treated as an error.
226  */
227 static u32 ice_get_flash_bank_offset(struct ice_hw *hw, enum ice_bank_select bank, u16 module)
228 {
229         struct ice_bank_info *banks = &hw->flash.banks;
230         enum ice_flash_bank active_bank;
231         bool second_bank_active;
232         u32 offset, size;
233
234         switch (module) {
235         case ICE_SR_1ST_NVM_BANK_PTR:
236                 offset = banks->nvm_ptr;
237                 size = banks->nvm_size;
238                 active_bank = banks->nvm_bank;
239                 break;
240         case ICE_SR_1ST_OROM_BANK_PTR:
241                 offset = banks->orom_ptr;
242                 size = banks->orom_size;
243                 active_bank = banks->orom_bank;
244                 break;
245         case ICE_SR_NETLIST_BANK_PTR:
246                 offset = banks->netlist_ptr;
247                 size = banks->netlist_size;
248                 active_bank = banks->netlist_bank;
249                 break;
250         default:
251                 ice_debug(hw, ICE_DBG_NVM, "Unexpected value for flash module: 0x%04x\n", module);
252                 return 0;
253         }
254
255         switch (active_bank) {
256         case ICE_1ST_FLASH_BANK:
257                 second_bank_active = false;
258                 break;
259         case ICE_2ND_FLASH_BANK:
260                 second_bank_active = true;
261                 break;
262         default:
263                 ice_debug(hw, ICE_DBG_NVM, "Unexpected value for active flash bank: %u\n",
264                           active_bank);
265                 return 0;
266         }
267
268         /* The second flash bank is stored immediately following the first
269          * bank. Based on whether the 1st or 2nd bank is active, and whether
270          * we want the active or inactive bank, calculate the desired offset.
271          */
272         switch (bank) {
273         case ICE_ACTIVE_FLASH_BANK:
274                 return offset + (second_bank_active ? size : 0);
275         case ICE_INACTIVE_FLASH_BANK:
276                 return offset + (second_bank_active ? 0 : size);
277         }
278
279         ice_debug(hw, ICE_DBG_NVM, "Unexpected value for flash bank selection: %u\n", bank);
280         return 0;
281 }
282
283 /**
284  * ice_read_flash_module - Read a word from one of the main NVM modules
285  * @hw: pointer to the HW structure
286  * @bank: which bank of the module to read
287  * @module: the module to read
288  * @offset: the offset into the module in bytes
289  * @data: storage for the word read from the flash
290  * @length: bytes of data to read
291  *
292  * Read data from the specified flash module. The bank parameter indicates
293  * whether or not to read from the active bank or the inactive bank of that
294  * module.
295  *
296  * The word will be read using flat NVM access, and relies on the
297  * hw->flash.banks data being setup by ice_determine_active_flash_banks()
298  * during initialization.
299  */
300 static enum ice_status
301 ice_read_flash_module(struct ice_hw *hw, enum ice_bank_select bank, u16 module,
302                       u32 offset, u8 *data, u32 length)
303 {
304         enum ice_status status;
305         u32 start;
306
307         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
308
309         start = ice_get_flash_bank_offset(hw, bank, module);
310         if (!start) {
311                 ice_debug(hw, ICE_DBG_NVM, "Unable to calculate flash bank offset for module 0x%04x\n",
312                           module);
313                 return ICE_ERR_PARAM;
314         }
315
316         status = ice_acquire_nvm(hw, ICE_RES_READ);
317         if (status)
318                 return status;
319
320         status = ice_read_flat_nvm(hw, start + offset, &length, data, false);
321
322         ice_release_nvm(hw);
323
324         return status;
325 }
326
327 /**
328  * ice_read_nvm_module - Read from the active main NVM module
329  * @hw: pointer to the HW structure
330  * @bank: whether to read from active or inactive NVM module
331  * @offset: offset into the NVM module to read, in words
332  * @data: storage for returned word value
333  *
334  * Read the specified word from the active NVM module. This includes the CSS
335  * header at the start of the NVM module.
336  */
337 static enum ice_status
338 ice_read_nvm_module(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
339 {
340         enum ice_status status;
341         __le16 data_local;
342
343         status = ice_read_flash_module(hw, bank, ICE_SR_1ST_NVM_BANK_PTR, offset * sizeof(u16),
344                                        (_FORCE_ u8 *)&data_local, sizeof(u16));
345         if (!status)
346                 *data = LE16_TO_CPU(data_local);
347
348         return status;
349 }
350
351 /**
352  * ice_read_orom_module - Read from the active Option ROM module
353  * @hw: pointer to the HW structure
354  * @bank: whether to read from active or inactive OROM module
355  * @offset: offset into the OROM module to read, in words
356  * @data: storage for returned word value
357  *
358  * Read the specified word from the active Option ROM module of the flash.
359  * Note that unlike the NVM module, the CSS data is stored at the end of the
360  * module instead of at the beginning.
361  */
362 static enum ice_status
363 ice_read_orom_module(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
364 {
365         enum ice_status status;
366         __le16 data_local;
367
368         status = ice_read_flash_module(hw, bank, ICE_SR_1ST_OROM_BANK_PTR, offset * sizeof(u16),
369                                        (_FORCE_ u8 *)&data_local, sizeof(u16));
370         if (!status)
371                 *data = LE16_TO_CPU(data_local);
372
373         return status;
374 }
375
376 /**
377  * ice_read_sr_word - Reads Shadow RAM word and acquire NVM if necessary
378  * @hw: pointer to the HW structure
379  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
380  * @data: word read from the Shadow RAM
381  *
382  * Reads one 16 bit word from the Shadow RAM using the ice_read_sr_word_aq.
383  */
384 enum ice_status ice_read_sr_word(struct ice_hw *hw, u16 offset, u16 *data)
385 {
386         enum ice_status status;
387
388         status = ice_acquire_nvm(hw, ICE_RES_READ);
389         if (!status) {
390                 status = ice_read_sr_word_aq(hw, offset, data);
391                 ice_release_nvm(hw);
392         }
393
394         return status;
395 }
396
397 /**
398  * ice_get_pfa_module_tlv - Reads sub module TLV from NVM PFA
399  * @hw: pointer to hardware structure
400  * @module_tlv: pointer to module TLV to return
401  * @module_tlv_len: pointer to module TLV length to return
402  * @module_type: module type requested
403  *
404  * Finds the requested sub module TLV type from the Preserved Field
405  * Area (PFA) and returns the TLV pointer and length. The caller can
406  * use these to read the variable length TLV value.
407  */
408 enum ice_status
409 ice_get_pfa_module_tlv(struct ice_hw *hw, u16 *module_tlv, u16 *module_tlv_len,
410                        u16 module_type)
411 {
412         enum ice_status status;
413         u16 pfa_len, pfa_ptr;
414         u16 next_tlv;
415
416         status = ice_read_sr_word(hw, ICE_SR_PFA_PTR, &pfa_ptr);
417         if (status != ICE_SUCCESS) {
418                 ice_debug(hw, ICE_DBG_INIT, "Preserved Field Array pointer.\n");
419                 return status;
420         }
421         status = ice_read_sr_word(hw, pfa_ptr, &pfa_len);
422         if (status != ICE_SUCCESS) {
423                 ice_debug(hw, ICE_DBG_INIT, "Failed to read PFA length.\n");
424                 return status;
425         }
426         /* Starting with first TLV after PFA length, iterate through the list
427          * of TLVs to find the requested one.
428          */
429         next_tlv = pfa_ptr + 1;
430         while (next_tlv < pfa_ptr + pfa_len) {
431                 u16 tlv_sub_module_type;
432                 u16 tlv_len;
433
434                 /* Read TLV type */
435                 status = ice_read_sr_word(hw, next_tlv, &tlv_sub_module_type);
436                 if (status != ICE_SUCCESS) {
437                         ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV type.\n");
438                         break;
439                 }
440                 /* Read TLV length */
441                 status = ice_read_sr_word(hw, next_tlv + 1, &tlv_len);
442                 if (status != ICE_SUCCESS) {
443                         ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV length.\n");
444                         break;
445                 }
446                 if (tlv_sub_module_type == module_type) {
447                         if (tlv_len) {
448                                 *module_tlv = next_tlv;
449                                 *module_tlv_len = tlv_len;
450                                 return ICE_SUCCESS;
451                         }
452                         return ICE_ERR_INVAL_SIZE;
453                 }
454                 /* Check next TLV, i.e. current TLV pointer + length + 2 words
455                  * (for current TLV's type and length)
456                  */
457                 next_tlv = next_tlv + tlv_len + 2;
458         }
459         /* Module does not exist */
460         return ICE_ERR_DOES_NOT_EXIST;
461 }
462
463 /**
464  * ice_read_pba_string - Reads part number string from NVM
465  * @hw: pointer to hardware structure
466  * @pba_num: stores the part number string from the NVM
467  * @pba_num_size: part number string buffer length
468  *
469  * Reads the part number string from the NVM.
470  */
471 enum ice_status
472 ice_read_pba_string(struct ice_hw *hw, u8 *pba_num, u32 pba_num_size)
473 {
474         u16 pba_tlv, pba_tlv_len;
475         enum ice_status status;
476         u16 pba_word, pba_size;
477         u16 i;
478
479         status = ice_get_pfa_module_tlv(hw, &pba_tlv, &pba_tlv_len,
480                                         ICE_SR_PBA_BLOCK_PTR);
481         if (status != ICE_SUCCESS) {
482                 ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block TLV.\n");
483                 return status;
484         }
485
486         /* pba_size is the next word */
487         status = ice_read_sr_word(hw, (pba_tlv + 2), &pba_size);
488         if (status != ICE_SUCCESS) {
489                 ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Section size.\n");
490                 return status;
491         }
492
493         if (pba_tlv_len < pba_size) {
494                 ice_debug(hw, ICE_DBG_INIT, "Invalid PBA Block TLV size.\n");
495                 return ICE_ERR_INVAL_SIZE;
496         }
497
498         /* Subtract one to get PBA word count (PBA Size word is included in
499          * total size)
500          */
501         pba_size--;
502         if (pba_num_size < (((u32)pba_size * 2) + 1)) {
503                 ice_debug(hw, ICE_DBG_INIT, "Buffer too small for PBA data.\n");
504                 return ICE_ERR_PARAM;
505         }
506
507         for (i = 0; i < pba_size; i++) {
508                 status = ice_read_sr_word(hw, (pba_tlv + 2 + 1) + i, &pba_word);
509                 if (status != ICE_SUCCESS) {
510                         ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block word %d.\n", i);
511                         return status;
512                 }
513
514                 pba_num[(i * 2)] = (pba_word >> 8) & 0xFF;
515                 pba_num[(i * 2) + 1] = pba_word & 0xFF;
516         }
517         pba_num[(pba_size * 2)] = '\0';
518
519         return status;
520 }
521
522 /**
523  * ice_get_nvm_srev - Read the security revision from the NVM CSS header
524  * @hw: pointer to the HW struct
525  * @bank: whether to read from the active or inactive flash bank
526  * @srev: storage for security revision
527  *
528  * Read the security revision out of the CSS header of the active NVM module
529  * bank.
530  */
531 static enum ice_status ice_get_nvm_srev(struct ice_hw *hw, enum ice_bank_select bank, u32 *srev)
532 {
533         enum ice_status status;
534         u16 srev_l, srev_h;
535
536         status = ice_read_nvm_module(hw, bank, ICE_NVM_CSS_SREV_L, &srev_l);
537         if (status)
538                 return status;
539
540         status = ice_read_nvm_module(hw, bank, ICE_NVM_CSS_SREV_H, &srev_h);
541         if (status)
542                 return status;
543
544         *srev = srev_h << 16 | srev_l;
545
546         return ICE_SUCCESS;
547 }
548
549 /**
550  * ice_get_nvm_ver_info - Read NVM version information
551  * @hw: pointer to the HW struct
552  * @nvm: pointer to NVM info structure
553  *
554  * Read the NVM EETRACK ID and map version of the main NVM image bank, filling
555  * in the nvm info structure.
556  */
557 static enum ice_status
558 ice_get_nvm_ver_info(struct ice_hw *hw, struct ice_nvm_info *nvm)
559 {
560         u16 eetrack_lo, eetrack_hi, ver;
561         enum ice_status status;
562
563         status = ice_read_sr_word(hw, ICE_SR_NVM_DEV_STARTER_VER, &ver);
564         if (status) {
565                 ice_debug(hw, ICE_DBG_NVM, "Failed to read DEV starter version.\n");
566                 return status;
567         }
568         nvm->major = (ver & ICE_NVM_VER_HI_MASK) >> ICE_NVM_VER_HI_SHIFT;
569         nvm->minor = (ver & ICE_NVM_VER_LO_MASK) >> ICE_NVM_VER_LO_SHIFT;
570
571         status = ice_read_sr_word(hw, ICE_SR_NVM_EETRACK_LO, &eetrack_lo);
572         if (status) {
573                 ice_debug(hw, ICE_DBG_NVM, "Failed to read EETRACK lo.\n");
574                 return status;
575         }
576         status = ice_read_sr_word(hw, ICE_SR_NVM_EETRACK_HI, &eetrack_hi);
577         if (status) {
578                 ice_debug(hw, ICE_DBG_NVM, "Failed to read EETRACK hi.\n");
579                 return status;
580         }
581
582         nvm->eetrack = (eetrack_hi << 16) | eetrack_lo;
583
584         status = ice_get_nvm_srev(hw, ICE_ACTIVE_FLASH_BANK, &nvm->srev);
585         if (status)
586                 ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM security revision.\n");
587
588         return ICE_SUCCESS;
589 }
590
591 /**
592  * ice_get_orom_srev - Read the security revision from the OROM CSS header
593  * @hw: pointer to the HW struct
594  * @bank: whether to read from active or inactive flash module
595  * @srev: storage for security revision
596  *
597  * Read the security revision out of the CSS header of the active OROM module
598  * bank.
599  */
600 static enum ice_status ice_get_orom_srev(struct ice_hw *hw, enum ice_bank_select bank, u32 *srev)
601 {
602         enum ice_status status;
603         u16 srev_l, srev_h;
604         u32 css_start;
605
606         if (hw->flash.banks.orom_size < ICE_NVM_OROM_TRAILER_LENGTH) {
607                 ice_debug(hw, ICE_DBG_NVM, "Unexpected Option ROM Size of %u\n",
608                           hw->flash.banks.orom_size);
609                 return ICE_ERR_CFG;
610         }
611
612         /* calculate how far into the Option ROM the CSS header starts. Note
613          * that ice_read_orom_module takes a word offset so we need to
614          * divide by 2 here.
615          */
616         css_start = (hw->flash.banks.orom_size - ICE_NVM_OROM_TRAILER_LENGTH) / 2;
617
618         status = ice_read_orom_module(hw, bank, css_start + ICE_NVM_CSS_SREV_L, &srev_l);
619         if (status)
620                 return status;
621
622         status = ice_read_orom_module(hw, bank, css_start + ICE_NVM_CSS_SREV_H, &srev_h);
623         if (status)
624                 return status;
625
626         *srev = srev_h << 16 | srev_l;
627
628         return ICE_SUCCESS;
629 }
630
631 /**
632  * ice_get_orom_civd_data - Get the combo version information from Option ROM
633  * @hw: pointer to the HW struct
634  * @bank: whether to read from the active or inactive flash module
635  * @civd: storage for the Option ROM CIVD data.
636  *
637  * Searches through the Option ROM flash contents to locate the CIVD data for
638  * the image.
639  */
640 static enum ice_status
641 ice_get_orom_civd_data(struct ice_hw *hw, enum ice_bank_select bank,
642                        struct ice_orom_civd_info *civd)
643 {
644         struct ice_orom_civd_info tmp;
645         enum ice_status status;
646         u32 offset;
647
648         /* The CIVD section is located in the Option ROM aligned to 512 bytes.
649          * The first 4 bytes must contain the ASCII characters "$CIV".
650          * A simple modulo 256 sum of all of the bytes of the structure must
651          * equal 0.
652          */
653         for (offset = 0; (offset + 512) <= hw->flash.banks.orom_size; offset += 512) {
654                 u8 sum = 0, i;
655
656                 status = ice_read_flash_module(hw, bank, ICE_SR_1ST_OROM_BANK_PTR,
657                                                offset, (u8 *)&tmp, sizeof(tmp));
658                 if (status) {
659                         ice_debug(hw, ICE_DBG_NVM, "Unable to read Option ROM CIVD data\n");
660                         return status;
661                 }
662
663                 /* Skip forward until we find a matching signature */
664                 if (memcmp("$CIV", tmp.signature, sizeof(tmp.signature)) != 0)
665                         continue;
666
667                 /* Verify that the simple checksum is zero */
668                 for (i = 0; i < sizeof(tmp); i++)
669                         sum += ((u8 *)&tmp)[i];
670
671                 if (sum) {
672                         ice_debug(hw, ICE_DBG_NVM, "Found CIVD data with invalid checksum of %u\n",
673                                   sum);
674                         return ICE_ERR_NVM;
675                 }
676
677                 *civd = tmp;
678                 return ICE_SUCCESS;
679         }
680
681         return ICE_ERR_NVM;
682 }
683
684 /**
685  * ice_get_orom_ver_info - Read Option ROM version information
686  * @hw: pointer to the HW struct
687  * @bank: whether to read from the active or inactive flash module
688  * @orom: pointer to Option ROM info structure
689  *
690  * Read Option ROM version and security revision from the Option ROM flash
691  * section.
692  */
693 static enum ice_status
694 ice_get_orom_ver_info(struct ice_hw *hw, enum ice_bank_select bank, struct ice_orom_info *orom)
695 {
696         struct ice_orom_civd_info civd;
697         enum ice_status status;
698         u32 combo_ver;
699
700         status = ice_get_orom_civd_data(hw, bank, &civd);
701         if (status) {
702                 ice_debug(hw, ICE_DBG_NVM, "Failed to locate valid Option ROM CIVD data\n");
703                 return status;
704         }
705
706         combo_ver = LE32_TO_CPU(civd.combo_ver);
707
708         orom->major = (u8)((combo_ver & ICE_OROM_VER_MASK) >> ICE_OROM_VER_SHIFT);
709         orom->patch = (u8)(combo_ver & ICE_OROM_VER_PATCH_MASK);
710         orom->build = (u16)((combo_ver & ICE_OROM_VER_BUILD_MASK) >> ICE_OROM_VER_BUILD_SHIFT);
711
712         status = ice_get_orom_srev(hw, bank, &orom->srev);
713         if (status) {
714                 ice_debug(hw, ICE_DBG_NVM, "Failed to read Option ROM security revision.\n");
715                 return status;
716         }
717
718         return ICE_SUCCESS;
719 }
720
721 /**
722  * ice_get_inactive_orom_ver - Read Option ROM version from the inactive bank
723  * @hw: pointer to the HW structure
724  * @orom: storage for Option ROM version information
725  *
726  * Reads the Option ROM version and security revision data for the inactive
727  * section of flash. Used to access version data for a pending update that has
728  * not yet been activated.
729  */
730 enum ice_status ice_get_inactive_orom_ver(struct ice_hw *hw, struct ice_orom_info *orom)
731 {
732         return ice_get_orom_ver_info(hw, ICE_INACTIVE_FLASH_BANK, orom);
733 }
734
735 /**
736  * ice_discover_flash_size - Discover the available flash size.
737  * @hw: pointer to the HW struct
738  *
739  * The device flash could be up to 16MB in size. However, it is possible that
740  * the actual size is smaller. Use bisection to determine the accessible size
741  * of flash memory.
742  */
743 static enum ice_status ice_discover_flash_size(struct ice_hw *hw)
744 {
745         u32 min_size = 0, max_size = ICE_AQC_NVM_MAX_OFFSET + 1;
746         enum ice_status status;
747
748         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
749
750         status = ice_acquire_nvm(hw, ICE_RES_READ);
751         if (status)
752                 return status;
753
754         while ((max_size - min_size) > 1) {
755                 u32 offset = (max_size + min_size) / 2;
756                 u32 len = 1;
757                 u8 data;
758
759                 status = ice_read_flat_nvm(hw, offset, &len, &data, false);
760                 if (status == ICE_ERR_AQ_ERROR &&
761                     hw->adminq.sq_last_status == ICE_AQ_RC_EINVAL) {
762                         ice_debug(hw, ICE_DBG_NVM, "%s: New upper bound of %u bytes\n",
763                                   __func__, offset);
764                         status = ICE_SUCCESS;
765                         max_size = offset;
766                 } else if (!status) {
767                         ice_debug(hw, ICE_DBG_NVM, "%s: New lower bound of %u bytes\n",
768                                   __func__, offset);
769                         min_size = offset;
770                 } else {
771                         /* an unexpected error occurred */
772                         goto err_read_flat_nvm;
773                 }
774         }
775
776         ice_debug(hw, ICE_DBG_NVM, "Predicted flash size is %u bytes\n", max_size);
777
778         hw->flash.flash_size = max_size;
779
780 err_read_flat_nvm:
781         ice_release_nvm(hw);
782
783         return status;
784 }
785
786 /**
787  * ice_read_sr_pointer - Read the value of a Shadow RAM pointer word
788  * @hw: pointer to the HW structure
789  * @offset: the word offset of the Shadow RAM word to read
790  * @pointer: pointer value read from Shadow RAM
791  *
792  * Read the given Shadow RAM word, and convert it to a pointer value specified
793  * in bytes. This function assumes the specified offset is a valid pointer
794  * word.
795  *
796  * Each pointer word specifies whether it is stored in word size or 4KB
797  * sector size by using the highest bit. The reported pointer value will be in
798  * bytes, intended for flat NVM reads.
799  */
800 static enum ice_status
801 ice_read_sr_pointer(struct ice_hw *hw, u16 offset, u32 *pointer)
802 {
803         enum ice_status status;
804         u16 value;
805
806         status = ice_read_sr_word(hw, offset, &value);
807         if (status)
808                 return status;
809
810         /* Determine if the pointer is in 4KB or word units */
811         if (value & ICE_SR_NVM_PTR_4KB_UNITS)
812                 *pointer = (value & ~ICE_SR_NVM_PTR_4KB_UNITS) * 4 * 1024;
813         else
814                 *pointer = value * 2;
815
816         return ICE_SUCCESS;
817 }
818
819 /**
820  * ice_read_sr_area_size - Read an area size from a Shadow RAM word
821  * @hw: pointer to the HW structure
822  * @offset: the word offset of the Shadow RAM to read
823  * @size: size value read from the Shadow RAM
824  *
825  * Read the given Shadow RAM word, and convert it to an area size value
826  * specified in bytes. This function assumes the specified offset is a valid
827  * area size word.
828  *
829  * Each area size word is specified in 4KB sector units. This function reports
830  * the size in bytes, intended for flat NVM reads.
831  */
832 static enum ice_status
833 ice_read_sr_area_size(struct ice_hw *hw, u16 offset, u32 *size)
834 {
835         enum ice_status status;
836         u16 value;
837
838         status = ice_read_sr_word(hw, offset, &value);
839         if (status)
840                 return status;
841
842         /* Area sizes are always specified in 4KB units */
843         *size = value * 4 * 1024;
844
845         return ICE_SUCCESS;
846 }
847
848 /**
849  * ice_determine_active_flash_banks - Discover active bank for each module
850  * @hw: pointer to the HW struct
851  *
852  * Read the Shadow RAM control word and determine which banks are active for
853  * the NVM, OROM, and Netlist modules. Also read and calculate the associated
854  * pointer and size. These values are then cached into the ice_flash_info
855  * structure for later use in order to calculate the correct offset to read
856  * from the active module.
857  */
858 static enum ice_status
859 ice_determine_active_flash_banks(struct ice_hw *hw)
860 {
861         struct ice_bank_info *banks = &hw->flash.banks;
862         enum ice_status status;
863         u16 ctrl_word;
864
865         status = ice_read_sr_word(hw, ICE_SR_NVM_CTRL_WORD, &ctrl_word);
866         if (status) {
867                 ice_debug(hw, ICE_DBG_NVM, "Failed to read the Shadow RAM control word\n");
868                 return status;
869         }
870
871         /* Check that the control word indicates validity */
872         if ((ctrl_word & ICE_SR_CTRL_WORD_1_M) >> ICE_SR_CTRL_WORD_1_S != ICE_SR_CTRL_WORD_VALID) {
873                 ice_debug(hw, ICE_DBG_NVM, "Shadow RAM control word is invalid\n");
874                 return ICE_ERR_CFG;
875         }
876
877         if (!(ctrl_word & ICE_SR_CTRL_WORD_NVM_BANK))
878                 banks->nvm_bank = ICE_1ST_FLASH_BANK;
879         else
880                 banks->nvm_bank = ICE_2ND_FLASH_BANK;
881
882         if (!(ctrl_word & ICE_SR_CTRL_WORD_OROM_BANK))
883                 banks->orom_bank = ICE_1ST_FLASH_BANK;
884         else
885                 banks->orom_bank = ICE_2ND_FLASH_BANK;
886
887         if (!(ctrl_word & ICE_SR_CTRL_WORD_NETLIST_BANK))
888                 banks->netlist_bank = ICE_1ST_FLASH_BANK;
889         else
890                 banks->netlist_bank = ICE_2ND_FLASH_BANK;
891
892         status = ice_read_sr_pointer(hw, ICE_SR_1ST_NVM_BANK_PTR, &banks->nvm_ptr);
893         if (status) {
894                 ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank pointer\n");
895                 return status;
896         }
897
898         status = ice_read_sr_area_size(hw, ICE_SR_NVM_BANK_SIZE, &banks->nvm_size);
899         if (status) {
900                 ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank area size\n");
901                 return status;
902         }
903
904         status = ice_read_sr_pointer(hw, ICE_SR_1ST_OROM_BANK_PTR, &banks->orom_ptr);
905         if (status) {
906                 ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank pointer\n");
907                 return status;
908         }
909
910         status = ice_read_sr_area_size(hw, ICE_SR_OROM_BANK_SIZE, &banks->orom_size);
911         if (status) {
912                 ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank area size\n");
913                 return status;
914         }
915
916         status = ice_read_sr_pointer(hw, ICE_SR_NETLIST_BANK_PTR, &banks->netlist_ptr);
917         if (status) {
918                 ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank pointer\n");
919                 return status;
920         }
921
922         status = ice_read_sr_area_size(hw, ICE_SR_NETLIST_BANK_SIZE, &banks->netlist_size);
923         if (status) {
924                 ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank area size\n");
925                 return status;
926         }
927
928         return ICE_SUCCESS;
929 }
930
931 /**
932  * ice_init_nvm - initializes NVM setting
933  * @hw: pointer to the HW struct
934  *
935  * This function reads and populates NVM settings such as Shadow RAM size,
936  * max_timeout, and blank_nvm_mode
937  */
938 enum ice_status ice_init_nvm(struct ice_hw *hw)
939 {
940         struct ice_flash_info *flash = &hw->flash;
941         enum ice_status status;
942         u32 fla, gens_stat;
943         u8 sr_size;
944
945         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
946
947         /* The SR size is stored regardless of the NVM programming mode
948          * as the blank mode may be used in the factory line.
949          */
950         gens_stat = rd32(hw, GLNVM_GENS);
951         sr_size = (gens_stat & GLNVM_GENS_SR_SIZE_M) >> GLNVM_GENS_SR_SIZE_S;
952
953         /* Switching to words (sr_size contains power of 2) */
954         flash->sr_words = BIT(sr_size) * ICE_SR_WORDS_IN_1KB;
955
956         /* Check if we are in the normal or blank NVM programming mode */
957         fla = rd32(hw, GLNVM_FLA);
958         if (fla & GLNVM_FLA_LOCKED_M) { /* Normal programming mode */
959                 flash->blank_nvm_mode = false;
960         } else {
961                 /* Blank programming mode */
962                 flash->blank_nvm_mode = true;
963                 ice_debug(hw, ICE_DBG_NVM, "NVM init error: unsupported blank mode.\n");
964                 return ICE_ERR_NVM_BLANK_MODE;
965         }
966
967         status = ice_discover_flash_size(hw);
968         if (status) {
969                 ice_debug(hw, ICE_DBG_NVM, "NVM init error: failed to discover flash size.\n");
970                 return status;
971         }
972
973         status = ice_determine_active_flash_banks(hw);
974         if (status) {
975                 ice_debug(hw, ICE_DBG_NVM, "Failed to determine active flash banks.\n");
976                 return status;
977         }
978
979         status = ice_get_nvm_ver_info(hw, &flash->nvm);
980         if (status) {
981                 ice_debug(hw, ICE_DBG_INIT, "Failed to read NVM info.\n");
982                 return status;
983         }
984
985         status = ice_get_orom_ver_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->orom);
986         if (status)
987                 ice_debug(hw, ICE_DBG_INIT, "Failed to read Option ROM info.\n");
988
989         return ICE_SUCCESS;
990 }
991
992 /**
993  * ice_read_sr_buf - Reads Shadow RAM buf and acquire lock if necessary
994  * @hw: pointer to the HW structure
995  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
996  * @words: (in) number of words to read; (out) number of words actually read
997  * @data: words read from the Shadow RAM
998  *
999  * Reads 16 bit words (data buf) from the SR using the ice_read_nvm_buf_aq
1000  * method. The buf read is preceded by the NVM ownership take
1001  * and followed by the release.
1002  */
1003 enum ice_status
1004 ice_read_sr_buf(struct ice_hw *hw, u16 offset, u16 *words, u16 *data)
1005 {
1006         enum ice_status status;
1007
1008         status = ice_acquire_nvm(hw, ICE_RES_READ);
1009         if (!status) {
1010                 status = ice_read_sr_buf_aq(hw, offset, words, data);
1011                 ice_release_nvm(hw);
1012         }
1013
1014         return status;
1015 }
1016
1017 /**
1018  * ice_nvm_validate_checksum
1019  * @hw: pointer to the HW struct
1020  *
1021  * Verify NVM PFA checksum validity (0x0706)
1022  */
1023 enum ice_status ice_nvm_validate_checksum(struct ice_hw *hw)
1024 {
1025         struct ice_aqc_nvm_checksum *cmd;
1026         struct ice_aq_desc desc;
1027         enum ice_status status;
1028
1029         status = ice_acquire_nvm(hw, ICE_RES_READ);
1030         if (status)
1031                 return status;
1032
1033         cmd = &desc.params.nvm_checksum;
1034
1035         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_checksum);
1036         cmd->flags = ICE_AQC_NVM_CHECKSUM_VERIFY;
1037
1038         status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1039         ice_release_nvm(hw);
1040
1041         if (!status)
1042                 if (LE16_TO_CPU(cmd->checksum) != ICE_AQC_NVM_CHECKSUM_CORRECT)
1043                         status = ICE_ERR_NVM_CHECKSUM;
1044
1045         return status;
1046 }
1047
1048 /**
1049  * ice_nvm_access_get_features - Return the NVM access features structure
1050  * @cmd: NVM access command to process
1051  * @data: storage for the driver NVM features
1052  *
1053  * Fill in the data section of the NVM access request with a copy of the NVM
1054  * features structure.
1055  */
1056 enum ice_status
1057 ice_nvm_access_get_features(struct ice_nvm_access_cmd *cmd,
1058                             union ice_nvm_access_data *data)
1059 {
1060         /* The provided data_size must be at least as large as our NVM
1061          * features structure. A larger size should not be treated as an
1062          * error, to allow future extensions to the features structure to
1063          * work on older drivers.
1064          */
1065         if (cmd->data_size < sizeof(struct ice_nvm_features))
1066                 return ICE_ERR_NO_MEMORY;
1067
1068         /* Initialize the data buffer to zeros */
1069         ice_memset(data, 0, cmd->data_size, ICE_NONDMA_MEM);
1070
1071         /* Fill in the features data */
1072         data->drv_features.major = ICE_NVM_ACCESS_MAJOR_VER;
1073         data->drv_features.minor = ICE_NVM_ACCESS_MINOR_VER;
1074         data->drv_features.size = sizeof(struct ice_nvm_features);
1075         data->drv_features.features[0] = ICE_NVM_FEATURES_0_REG_ACCESS;
1076
1077         return ICE_SUCCESS;
1078 }
1079
1080 /**
1081  * ice_nvm_access_get_module - Helper function to read module value
1082  * @cmd: NVM access command structure
1083  *
1084  * Reads the module value out of the NVM access config field.
1085  */
1086 u32 ice_nvm_access_get_module(struct ice_nvm_access_cmd *cmd)
1087 {
1088         return ((cmd->config & ICE_NVM_CFG_MODULE_M) >> ICE_NVM_CFG_MODULE_S);
1089 }
1090
1091 /**
1092  * ice_nvm_access_get_flags - Helper function to read flags value
1093  * @cmd: NVM access command structure
1094  *
1095  * Reads the flags value out of the NVM access config field.
1096  */
1097 u32 ice_nvm_access_get_flags(struct ice_nvm_access_cmd *cmd)
1098 {
1099         return ((cmd->config & ICE_NVM_CFG_FLAGS_M) >> ICE_NVM_CFG_FLAGS_S);
1100 }
1101
1102 /**
1103  * ice_nvm_access_get_adapter - Helper function to read adapter info
1104  * @cmd: NVM access command structure
1105  *
1106  * Read the adapter info value out of the NVM access config field.
1107  */
1108 u32 ice_nvm_access_get_adapter(struct ice_nvm_access_cmd *cmd)
1109 {
1110         return ((cmd->config & ICE_NVM_CFG_ADAPTER_INFO_M) >>
1111                 ICE_NVM_CFG_ADAPTER_INFO_S);
1112 }
1113
1114 /**
1115  * ice_validate_nvm_rw_reg - Check than an NVM access request is valid
1116  * @cmd: NVM access command structure
1117  *
1118  * Validates that an NVM access structure is request to read or write a valid
1119  * register offset. First validates that the module and flags are correct, and
1120  * then ensures that the register offset is one of the accepted registers.
1121  */
1122 static enum ice_status
1123 ice_validate_nvm_rw_reg(struct ice_nvm_access_cmd *cmd)
1124 {
1125         u32 module, flags, offset;
1126         u16 i;
1127
1128         module = ice_nvm_access_get_module(cmd);
1129         flags = ice_nvm_access_get_flags(cmd);
1130         offset = cmd->offset;
1131
1132         /* Make sure the module and flags indicate a read/write request */
1133         if (module != ICE_NVM_REG_RW_MODULE ||
1134             flags != ICE_NVM_REG_RW_FLAGS ||
1135             cmd->data_size != FIELD_SIZEOF(union ice_nvm_access_data, regval))
1136                 return ICE_ERR_PARAM;
1137
1138         switch (offset) {
1139         case GL_HICR:
1140         case GL_HICR_EN: /* Note, this register is read only */
1141         case GL_FWSTS:
1142         case GL_MNG_FWSM:
1143         case GLGEN_CSR_DEBUG_C:
1144         case GLGEN_RSTAT:
1145         case GLPCI_LBARCTRL:
1146         case GLNVM_GENS:
1147         case GLNVM_FLA:
1148         case PF_FUNC_RID:
1149                 return ICE_SUCCESS;
1150         default:
1151                 break;
1152         }
1153
1154         for (i = 0; i <= ICE_NVM_ACCESS_GL_HIDA_MAX; i++)
1155                 if (offset == (u32)GL_HIDA(i))
1156                         return ICE_SUCCESS;
1157
1158         for (i = 0; i <= ICE_NVM_ACCESS_GL_HIBA_MAX; i++)
1159                 if (offset == (u32)GL_HIBA(i))
1160                         return ICE_SUCCESS;
1161
1162         /* All other register offsets are not valid */
1163         return ICE_ERR_OUT_OF_RANGE;
1164 }
1165
1166 /**
1167  * ice_nvm_access_read - Handle an NVM read request
1168  * @hw: pointer to the HW struct
1169  * @cmd: NVM access command to process
1170  * @data: storage for the register value read
1171  *
1172  * Process an NVM access request to read a register.
1173  */
1174 enum ice_status
1175 ice_nvm_access_read(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
1176                     union ice_nvm_access_data *data)
1177 {
1178         enum ice_status status;
1179
1180         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1181
1182         /* Always initialize the output data, even on failure */
1183         ice_memset(data, 0, cmd->data_size, ICE_NONDMA_MEM);
1184
1185         /* Make sure this is a valid read/write access request */
1186         status = ice_validate_nvm_rw_reg(cmd);
1187         if (status)
1188                 return status;
1189
1190         ice_debug(hw, ICE_DBG_NVM, "NVM access: reading register %08x\n",
1191                   cmd->offset);
1192
1193         /* Read the register and store the contents in the data field */
1194         data->regval = rd32(hw, cmd->offset);
1195
1196         return ICE_SUCCESS;
1197 }
1198
1199 /**
1200  * ice_nvm_access_write - Handle an NVM write request
1201  * @hw: pointer to the HW struct
1202  * @cmd: NVM access command to process
1203  * @data: NVM access data to write
1204  *
1205  * Process an NVM access request to write a register.
1206  */
1207 enum ice_status
1208 ice_nvm_access_write(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
1209                      union ice_nvm_access_data *data)
1210 {
1211         enum ice_status status;
1212
1213         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1214
1215         /* Make sure this is a valid read/write access request */
1216         status = ice_validate_nvm_rw_reg(cmd);
1217         if (status)
1218                 return status;
1219
1220         /* Reject requests to write to read-only registers */
1221         switch (cmd->offset) {
1222         case GL_HICR_EN:
1223         case GLGEN_RSTAT:
1224                 return ICE_ERR_OUT_OF_RANGE;
1225         default:
1226                 break;
1227         }
1228
1229         ice_debug(hw, ICE_DBG_NVM, "NVM access: writing register %08x with value %08x\n",
1230                   cmd->offset, data->regval);
1231
1232         /* Write the data field to the specified register */
1233         wr32(hw, cmd->offset, data->regval);
1234
1235         return ICE_SUCCESS;
1236 }
1237
1238 /**
1239  * ice_handle_nvm_access - Handle an NVM access request
1240  * @hw: pointer to the HW struct
1241  * @cmd: NVM access command info
1242  * @data: pointer to read or return data
1243  *
1244  * Process an NVM access request. Read the command structure information and
1245  * determine if it is valid. If not, report an error indicating the command
1246  * was invalid.
1247  *
1248  * For valid commands, perform the necessary function, copying the data into
1249  * the provided data buffer.
1250  */
1251 enum ice_status
1252 ice_handle_nvm_access(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
1253                       union ice_nvm_access_data *data)
1254 {
1255         u32 module, flags, adapter_info;
1256
1257         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1258
1259         /* Extended flags are currently reserved and must be zero */
1260         if ((cmd->config & ICE_NVM_CFG_EXT_FLAGS_M) != 0)
1261                 return ICE_ERR_PARAM;
1262
1263         /* Adapter info must match the HW device ID */
1264         adapter_info = ice_nvm_access_get_adapter(cmd);
1265         if (adapter_info != hw->device_id)
1266                 return ICE_ERR_PARAM;
1267
1268         switch (cmd->command) {
1269         case ICE_NVM_CMD_READ:
1270                 module = ice_nvm_access_get_module(cmd);
1271                 flags = ice_nvm_access_get_flags(cmd);
1272
1273                 /* Getting the driver's NVM features structure shares the same
1274                  * command type as reading a register. Read the config field
1275                  * to determine if this is a request to get features.
1276                  */
1277                 if (module == ICE_NVM_GET_FEATURES_MODULE &&
1278                     flags == ICE_NVM_GET_FEATURES_FLAGS &&
1279                     cmd->offset == 0)
1280                         return ice_nvm_access_get_features(cmd, data);
1281                 else
1282                         return ice_nvm_access_read(hw, cmd, data);
1283         case ICE_NVM_CMD_WRITE:
1284                 return ice_nvm_access_write(hw, cmd, data);
1285         default:
1286                 return ICE_ERR_PARAM;
1287         }
1288 }