4c709fd8665319d6d87009d4f7e9012bedc81ab5
[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 words
289  * @data: storage for the word read from the flash
290  *
291  * Read data from the specified flash module. The bank parameter indicates
292  * whether or not to read from the active bank or the inactive bank of that
293  * module.
294  *
295  * The word will be read using flat NVM access, and relies on the
296  * hw->flash.banks data being setup by ice_determine_active_flash_banks()
297  * during initialization.
298  */
299 static enum ice_status
300 ice_read_flash_module(struct ice_hw *hw, enum ice_bank_select bank, u16 module,
301                       u32 offset, u16 *data)
302 {
303         u32 bytes = sizeof(u16);
304         enum ice_status status;
305         __le16 data_local;
306         u32 start;
307
308         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
309
310         start = ice_get_flash_bank_offset(hw, bank, module);
311         if (!start) {
312                 ice_debug(hw, ICE_DBG_NVM, "Unable to calculate flash bank offset for module 0x%04x\n",
313                           module);
314                 return ICE_ERR_PARAM;
315         }
316
317         status = ice_acquire_nvm(hw, ICE_RES_READ);
318         if (status)
319                 return status;
320
321         status = ice_read_flat_nvm(hw, start + offset * sizeof(u16), &bytes,
322                                    (_FORCE_ u8 *)&data_local, false);
323         if (!status)
324                 *data = LE16_TO_CPU(data_local);
325
326         ice_release_nvm(hw);
327
328         return status;
329 }
330
331 /**
332  * ice_read_nvm_module - Read from the active main NVM module
333  * @hw: pointer to the HW structure
334  * @bank: whether to read from active or inactive NVM module
335  * @offset: offset into the NVM module to read, in words
336  * @data: storage for returned word value
337  *
338  * Read the specified word from the active NVM module. This includes the CSS
339  * header at the start of the NVM module.
340  */
341 static enum ice_status
342 ice_read_nvm_module(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
343 {
344         return ice_read_flash_module(hw, bank, ICE_SR_1ST_NVM_BANK_PTR, offset, data);
345 }
346
347 /**
348  * ice_read_orom_module - Read from the active Option ROM module
349  * @hw: pointer to the HW structure
350  * @bank: whether to read from active or inactive OROM module
351  * @offset: offset into the OROM module to read, in words
352  * @data: storage for returned word value
353  *
354  * Read the specified word from the active Option ROM module of the flash.
355  * Note that unlike the NVM module, the CSS data is stored at the end of the
356  * module instead of at the beginning.
357  */
358 static enum ice_status
359 ice_read_orom_module(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
360 {
361         return ice_read_flash_module(hw, bank, ICE_SR_1ST_OROM_BANK_PTR, offset, data);
362 }
363
364 /**
365  * ice_read_sr_word - Reads Shadow RAM word and acquire NVM if necessary
366  * @hw: pointer to the HW structure
367  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
368  * @data: word read from the Shadow RAM
369  *
370  * Reads one 16 bit word from the Shadow RAM using the ice_read_sr_word_aq.
371  */
372 enum ice_status ice_read_sr_word(struct ice_hw *hw, u16 offset, u16 *data)
373 {
374         enum ice_status status;
375
376         status = ice_acquire_nvm(hw, ICE_RES_READ);
377         if (!status) {
378                 status = ice_read_sr_word_aq(hw, offset, data);
379                 ice_release_nvm(hw);
380         }
381
382         return status;
383 }
384
385 /**
386  * ice_get_pfa_module_tlv - Reads sub module TLV from NVM PFA
387  * @hw: pointer to hardware structure
388  * @module_tlv: pointer to module TLV to return
389  * @module_tlv_len: pointer to module TLV length to return
390  * @module_type: module type requested
391  *
392  * Finds the requested sub module TLV type from the Preserved Field
393  * Area (PFA) and returns the TLV pointer and length. The caller can
394  * use these to read the variable length TLV value.
395  */
396 enum ice_status
397 ice_get_pfa_module_tlv(struct ice_hw *hw, u16 *module_tlv, u16 *module_tlv_len,
398                        u16 module_type)
399 {
400         enum ice_status status;
401         u16 pfa_len, pfa_ptr;
402         u16 next_tlv;
403
404         status = ice_read_sr_word(hw, ICE_SR_PFA_PTR, &pfa_ptr);
405         if (status != ICE_SUCCESS) {
406                 ice_debug(hw, ICE_DBG_INIT, "Preserved Field Array pointer.\n");
407                 return status;
408         }
409         status = ice_read_sr_word(hw, pfa_ptr, &pfa_len);
410         if (status != ICE_SUCCESS) {
411                 ice_debug(hw, ICE_DBG_INIT, "Failed to read PFA length.\n");
412                 return status;
413         }
414         /* Starting with first TLV after PFA length, iterate through the list
415          * of TLVs to find the requested one.
416          */
417         next_tlv = pfa_ptr + 1;
418         while (next_tlv < pfa_ptr + pfa_len) {
419                 u16 tlv_sub_module_type;
420                 u16 tlv_len;
421
422                 /* Read TLV type */
423                 status = ice_read_sr_word(hw, next_tlv, &tlv_sub_module_type);
424                 if (status != ICE_SUCCESS) {
425                         ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV type.\n");
426                         break;
427                 }
428                 /* Read TLV length */
429                 status = ice_read_sr_word(hw, next_tlv + 1, &tlv_len);
430                 if (status != ICE_SUCCESS) {
431                         ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV length.\n");
432                         break;
433                 }
434                 if (tlv_sub_module_type == module_type) {
435                         if (tlv_len) {
436                                 *module_tlv = next_tlv;
437                                 *module_tlv_len = tlv_len;
438                                 return ICE_SUCCESS;
439                         }
440                         return ICE_ERR_INVAL_SIZE;
441                 }
442                 /* Check next TLV, i.e. current TLV pointer + length + 2 words
443                  * (for current TLV's type and length)
444                  */
445                 next_tlv = next_tlv + tlv_len + 2;
446         }
447         /* Module does not exist */
448         return ICE_ERR_DOES_NOT_EXIST;
449 }
450
451 /**
452  * ice_read_pba_string - Reads part number string from NVM
453  * @hw: pointer to hardware structure
454  * @pba_num: stores the part number string from the NVM
455  * @pba_num_size: part number string buffer length
456  *
457  * Reads the part number string from the NVM.
458  */
459 enum ice_status
460 ice_read_pba_string(struct ice_hw *hw, u8 *pba_num, u32 pba_num_size)
461 {
462         u16 pba_tlv, pba_tlv_len;
463         enum ice_status status;
464         u16 pba_word, pba_size;
465         u16 i;
466
467         status = ice_get_pfa_module_tlv(hw, &pba_tlv, &pba_tlv_len,
468                                         ICE_SR_PBA_BLOCK_PTR);
469         if (status != ICE_SUCCESS) {
470                 ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block TLV.\n");
471                 return status;
472         }
473
474         /* pba_size is the next word */
475         status = ice_read_sr_word(hw, (pba_tlv + 2), &pba_size);
476         if (status != ICE_SUCCESS) {
477                 ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Section size.\n");
478                 return status;
479         }
480
481         if (pba_tlv_len < pba_size) {
482                 ice_debug(hw, ICE_DBG_INIT, "Invalid PBA Block TLV size.\n");
483                 return ICE_ERR_INVAL_SIZE;
484         }
485
486         /* Subtract one to get PBA word count (PBA Size word is included in
487          * total size)
488          */
489         pba_size--;
490         if (pba_num_size < (((u32)pba_size * 2) + 1)) {
491                 ice_debug(hw, ICE_DBG_INIT, "Buffer too small for PBA data.\n");
492                 return ICE_ERR_PARAM;
493         }
494
495         for (i = 0; i < pba_size; i++) {
496                 status = ice_read_sr_word(hw, (pba_tlv + 2 + 1) + i, &pba_word);
497                 if (status != ICE_SUCCESS) {
498                         ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block word %d.\n", i);
499                         return status;
500                 }
501
502                 pba_num[(i * 2)] = (pba_word >> 8) & 0xFF;
503                 pba_num[(i * 2) + 1] = pba_word & 0xFF;
504         }
505         pba_num[(pba_size * 2)] = '\0';
506
507         return status;
508 }
509
510 /**
511  * ice_get_nvm_srev - Read the security revision from the NVM CSS header
512  * @hw: pointer to the HW struct
513  * @bank: whether to read from the active or inactive flash bank
514  * @srev: storage for security revision
515  *
516  * Read the security revision out of the CSS header of the active NVM module
517  * bank.
518  */
519 static enum ice_status ice_get_nvm_srev(struct ice_hw *hw, enum ice_bank_select bank, u32 *srev)
520 {
521         enum ice_status status;
522         u16 srev_l, srev_h;
523
524         status = ice_read_nvm_module(hw, bank, ICE_NVM_CSS_SREV_L, &srev_l);
525         if (status)
526                 return status;
527
528         status = ice_read_nvm_module(hw, bank, ICE_NVM_CSS_SREV_H, &srev_h);
529         if (status)
530                 return status;
531
532         *srev = srev_h << 16 | srev_l;
533
534         return ICE_SUCCESS;
535 }
536
537 /**
538  * ice_get_nvm_ver_info - Read NVM version information
539  * @hw: pointer to the HW struct
540  * @nvm: pointer to NVM info structure
541  *
542  * Read the NVM EETRACK ID and map version of the main NVM image bank, filling
543  * in the nvm info structure.
544  */
545 static enum ice_status
546 ice_get_nvm_ver_info(struct ice_hw *hw, struct ice_nvm_info *nvm)
547 {
548         u16 eetrack_lo, eetrack_hi, ver;
549         enum ice_status status;
550
551         status = ice_read_sr_word(hw, ICE_SR_NVM_DEV_STARTER_VER, &ver);
552         if (status) {
553                 ice_debug(hw, ICE_DBG_NVM, "Failed to read DEV starter version.\n");
554                 return status;
555         }
556         nvm->major = (ver & ICE_NVM_VER_HI_MASK) >> ICE_NVM_VER_HI_SHIFT;
557         nvm->minor = (ver & ICE_NVM_VER_LO_MASK) >> ICE_NVM_VER_LO_SHIFT;
558
559         status = ice_read_sr_word(hw, ICE_SR_NVM_EETRACK_LO, &eetrack_lo);
560         if (status) {
561                 ice_debug(hw, ICE_DBG_NVM, "Failed to read EETRACK lo.\n");
562                 return status;
563         }
564         status = ice_read_sr_word(hw, ICE_SR_NVM_EETRACK_HI, &eetrack_hi);
565         if (status) {
566                 ice_debug(hw, ICE_DBG_NVM, "Failed to read EETRACK hi.\n");
567                 return status;
568         }
569
570         nvm->eetrack = (eetrack_hi << 16) | eetrack_lo;
571
572         status = ice_get_nvm_srev(hw, ICE_ACTIVE_FLASH_BANK, &nvm->srev);
573         if (status)
574                 ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM security revision.\n");
575
576         return ICE_SUCCESS;
577 }
578
579 /**
580  * ice_get_orom_srev - Read the security revision from the OROM CSS header
581  * @hw: pointer to the HW struct
582  * @bank: whether to read from active or inactive flash module
583  * @srev: storage for security revision
584  *
585  * Read the security revision out of the CSS header of the active OROM module
586  * bank.
587  */
588 static enum ice_status ice_get_orom_srev(struct ice_hw *hw, enum ice_bank_select bank, u32 *srev)
589 {
590         enum ice_status status;
591         u16 srev_l, srev_h;
592         u32 css_start;
593
594         if (hw->flash.banks.orom_size < ICE_NVM_OROM_TRAILER_LENGTH) {
595                 ice_debug(hw, ICE_DBG_NVM, "Unexpected Option ROM Size of %u\n",
596                           hw->flash.banks.orom_size);
597                 return ICE_ERR_CFG;
598         }
599
600         /* calculate how far into the Option ROM the CSS header starts. Note
601          * that ice_read_orom_module takes a word offset so we need to
602          * divide by 2 here.
603          */
604         css_start = (hw->flash.banks.orom_size - ICE_NVM_OROM_TRAILER_LENGTH) / 2;
605
606         status = ice_read_orom_module(hw, bank, css_start + ICE_NVM_CSS_SREV_L, &srev_l);
607         if (status)
608                 return status;
609
610         status = ice_read_orom_module(hw, bank, css_start + ICE_NVM_CSS_SREV_H, &srev_h);
611         if (status)
612                 return status;
613
614         *srev = srev_h << 16 | srev_l;
615
616         return ICE_SUCCESS;
617 }
618
619 /**
620  * ice_get_orom_ver_info - Read Option ROM version information
621  * @hw: pointer to the HW struct
622  * @orom: pointer to Option ROM info structure
623  *
624  * Read the Combo Image version data from the Boot Configuration TLV and fill
625  * in the option ROM version data.
626  */
627 static enum ice_status
628 ice_get_orom_ver_info(struct ice_hw *hw, struct ice_orom_info *orom)
629 {
630         u16 combo_hi, combo_lo, boot_cfg_tlv, boot_cfg_tlv_len;
631         enum ice_status status;
632         u32 combo_ver;
633
634         status = ice_get_pfa_module_tlv(hw, &boot_cfg_tlv, &boot_cfg_tlv_len,
635                                         ICE_SR_BOOT_CFG_PTR);
636         if (status) {
637                 ice_debug(hw, ICE_DBG_INIT, "Failed to read Boot Configuration Block TLV.\n");
638                 return status;
639         }
640
641         /* Boot Configuration Block must have length at least 2 words
642          * (Combo Image Version High and Combo Image Version Low)
643          */
644         if (boot_cfg_tlv_len < 2) {
645                 ice_debug(hw, ICE_DBG_INIT, "Invalid Boot Configuration Block TLV size.\n");
646                 return ICE_ERR_INVAL_SIZE;
647         }
648
649         status = ice_read_sr_word(hw, (boot_cfg_tlv + ICE_NVM_OROM_VER_OFF),
650                                   &combo_hi);
651         if (status) {
652                 ice_debug(hw, ICE_DBG_INIT, "Failed to read OROM_VER hi.\n");
653                 return status;
654         }
655
656         status = ice_read_sr_word(hw, (boot_cfg_tlv + ICE_NVM_OROM_VER_OFF + 1),
657                                   &combo_lo);
658         if (status) {
659                 ice_debug(hw, ICE_DBG_INIT, "Failed to read OROM_VER lo.\n");
660                 return status;
661         }
662
663         combo_ver = ((u32)combo_hi << 16) | combo_lo;
664
665         orom->major = (u8)((combo_ver & ICE_OROM_VER_MASK) >>
666                            ICE_OROM_VER_SHIFT);
667         orom->patch = (u8)(combo_ver & ICE_OROM_VER_PATCH_MASK);
668         orom->build = (u16)((combo_ver & ICE_OROM_VER_BUILD_MASK) >>
669                             ICE_OROM_VER_BUILD_SHIFT);
670
671         status = ice_get_orom_srev(hw, ICE_ACTIVE_FLASH_BANK, &orom->srev);
672         if (status)
673                 ice_debug(hw, ICE_DBG_NVM, "Failed to read Option ROM security revision.\n");
674
675         return ICE_SUCCESS;
676 }
677
678 /**
679  * ice_discover_flash_size - Discover the available flash size.
680  * @hw: pointer to the HW struct
681  *
682  * The device flash could be up to 16MB in size. However, it is possible that
683  * the actual size is smaller. Use bisection to determine the accessible size
684  * of flash memory.
685  */
686 static enum ice_status ice_discover_flash_size(struct ice_hw *hw)
687 {
688         u32 min_size = 0, max_size = ICE_AQC_NVM_MAX_OFFSET + 1;
689         enum ice_status status;
690
691         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
692
693         status = ice_acquire_nvm(hw, ICE_RES_READ);
694         if (status)
695                 return status;
696
697         while ((max_size - min_size) > 1) {
698                 u32 offset = (max_size + min_size) / 2;
699                 u32 len = 1;
700                 u8 data;
701
702                 status = ice_read_flat_nvm(hw, offset, &len, &data, false);
703                 if (status == ICE_ERR_AQ_ERROR &&
704                     hw->adminq.sq_last_status == ICE_AQ_RC_EINVAL) {
705                         ice_debug(hw, ICE_DBG_NVM, "%s: New upper bound of %u bytes\n",
706                                   __func__, offset);
707                         status = ICE_SUCCESS;
708                         max_size = offset;
709                 } else if (!status) {
710                         ice_debug(hw, ICE_DBG_NVM, "%s: New lower bound of %u bytes\n",
711                                   __func__, offset);
712                         min_size = offset;
713                 } else {
714                         /* an unexpected error occurred */
715                         goto err_read_flat_nvm;
716                 }
717         }
718
719         ice_debug(hw, ICE_DBG_NVM, "Predicted flash size is %u bytes\n", max_size);
720
721         hw->flash.flash_size = max_size;
722
723 err_read_flat_nvm:
724         ice_release_nvm(hw);
725
726         return status;
727 }
728
729 /**
730  * ice_read_sr_pointer - Read the value of a Shadow RAM pointer word
731  * @hw: pointer to the HW structure
732  * @offset: the word offset of the Shadow RAM word to read
733  * @pointer: pointer value read from Shadow RAM
734  *
735  * Read the given Shadow RAM word, and convert it to a pointer value specified
736  * in bytes. This function assumes the specified offset is a valid pointer
737  * word.
738  *
739  * Each pointer word specifies whether it is stored in word size or 4KB
740  * sector size by using the highest bit. The reported pointer value will be in
741  * bytes, intended for flat NVM reads.
742  */
743 static enum ice_status
744 ice_read_sr_pointer(struct ice_hw *hw, u16 offset, u32 *pointer)
745 {
746         enum ice_status status;
747         u16 value;
748
749         status = ice_read_sr_word(hw, offset, &value);
750         if (status)
751                 return status;
752
753         /* Determine if the pointer is in 4KB or word units */
754         if (value & ICE_SR_NVM_PTR_4KB_UNITS)
755                 *pointer = (value & ~ICE_SR_NVM_PTR_4KB_UNITS) * 4 * 1024;
756         else
757                 *pointer = value * 2;
758
759         return ICE_SUCCESS;
760 }
761
762 /**
763  * ice_read_sr_area_size - Read an area size from a Shadow RAM word
764  * @hw: pointer to the HW structure
765  * @offset: the word offset of the Shadow RAM to read
766  * @size: size value read from the Shadow RAM
767  *
768  * Read the given Shadow RAM word, and convert it to an area size value
769  * specified in bytes. This function assumes the specified offset is a valid
770  * area size word.
771  *
772  * Each area size word is specified in 4KB sector units. This function reports
773  * the size in bytes, intended for flat NVM reads.
774  */
775 static enum ice_status
776 ice_read_sr_area_size(struct ice_hw *hw, u16 offset, u32 *size)
777 {
778         enum ice_status status;
779         u16 value;
780
781         status = ice_read_sr_word(hw, offset, &value);
782         if (status)
783                 return status;
784
785         /* Area sizes are always specified in 4KB units */
786         *size = value * 4 * 1024;
787
788         return ICE_SUCCESS;
789 }
790
791 /**
792  * ice_determine_active_flash_banks - Discover active bank for each module
793  * @hw: pointer to the HW struct
794  *
795  * Read the Shadow RAM control word and determine which banks are active for
796  * the NVM, OROM, and Netlist modules. Also read and calculate the associated
797  * pointer and size. These values are then cached into the ice_flash_info
798  * structure for later use in order to calculate the correct offset to read
799  * from the active module.
800  */
801 static enum ice_status
802 ice_determine_active_flash_banks(struct ice_hw *hw)
803 {
804         struct ice_bank_info *banks = &hw->flash.banks;
805         enum ice_status status;
806         u16 ctrl_word;
807
808         status = ice_read_sr_word(hw, ICE_SR_NVM_CTRL_WORD, &ctrl_word);
809         if (status) {
810                 ice_debug(hw, ICE_DBG_NVM, "Failed to read the Shadow RAM control word\n");
811                 return status;
812         }
813
814         /* Check that the control word indicates validity */
815         if ((ctrl_word & ICE_SR_CTRL_WORD_1_M) >> ICE_SR_CTRL_WORD_1_S != ICE_SR_CTRL_WORD_VALID) {
816                 ice_debug(hw, ICE_DBG_NVM, "Shadow RAM control word is invalid\n");
817                 return ICE_ERR_CFG;
818         }
819
820         if (!(ctrl_word & ICE_SR_CTRL_WORD_NVM_BANK))
821                 banks->nvm_bank = ICE_1ST_FLASH_BANK;
822         else
823                 banks->nvm_bank = ICE_2ND_FLASH_BANK;
824
825         if (!(ctrl_word & ICE_SR_CTRL_WORD_OROM_BANK))
826                 banks->orom_bank = ICE_1ST_FLASH_BANK;
827         else
828                 banks->orom_bank = ICE_2ND_FLASH_BANK;
829
830         if (!(ctrl_word & ICE_SR_CTRL_WORD_NETLIST_BANK))
831                 banks->netlist_bank = ICE_1ST_FLASH_BANK;
832         else
833                 banks->netlist_bank = ICE_2ND_FLASH_BANK;
834
835         status = ice_read_sr_pointer(hw, ICE_SR_1ST_NVM_BANK_PTR, &banks->nvm_ptr);
836         if (status) {
837                 ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank pointer\n");
838                 return status;
839         }
840
841         status = ice_read_sr_area_size(hw, ICE_SR_NVM_BANK_SIZE, &banks->nvm_size);
842         if (status) {
843                 ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank area size\n");
844                 return status;
845         }
846
847         status = ice_read_sr_pointer(hw, ICE_SR_1ST_OROM_BANK_PTR, &banks->orom_ptr);
848         if (status) {
849                 ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank pointer\n");
850                 return status;
851         }
852
853         status = ice_read_sr_area_size(hw, ICE_SR_OROM_BANK_SIZE, &banks->orom_size);
854         if (status) {
855                 ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank area size\n");
856                 return status;
857         }
858
859         status = ice_read_sr_pointer(hw, ICE_SR_NETLIST_BANK_PTR, &banks->netlist_ptr);
860         if (status) {
861                 ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank pointer\n");
862                 return status;
863         }
864
865         status = ice_read_sr_area_size(hw, ICE_SR_NETLIST_BANK_SIZE, &banks->netlist_size);
866         if (status) {
867                 ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank area size\n");
868                 return status;
869         }
870
871         return ICE_SUCCESS;
872 }
873
874 /**
875  * ice_init_nvm - initializes NVM setting
876  * @hw: pointer to the HW struct
877  *
878  * This function reads and populates NVM settings such as Shadow RAM size,
879  * max_timeout, and blank_nvm_mode
880  */
881 enum ice_status ice_init_nvm(struct ice_hw *hw)
882 {
883         struct ice_flash_info *flash = &hw->flash;
884         enum ice_status status;
885         u32 fla, gens_stat;
886         u8 sr_size;
887
888         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
889
890         /* The SR size is stored regardless of the NVM programming mode
891          * as the blank mode may be used in the factory line.
892          */
893         gens_stat = rd32(hw, GLNVM_GENS);
894         sr_size = (gens_stat & GLNVM_GENS_SR_SIZE_M) >> GLNVM_GENS_SR_SIZE_S;
895
896         /* Switching to words (sr_size contains power of 2) */
897         flash->sr_words = BIT(sr_size) * ICE_SR_WORDS_IN_1KB;
898
899         /* Check if we are in the normal or blank NVM programming mode */
900         fla = rd32(hw, GLNVM_FLA);
901         if (fla & GLNVM_FLA_LOCKED_M) { /* Normal programming mode */
902                 flash->blank_nvm_mode = false;
903         } else {
904                 /* Blank programming mode */
905                 flash->blank_nvm_mode = true;
906                 ice_debug(hw, ICE_DBG_NVM, "NVM init error: unsupported blank mode.\n");
907                 return ICE_ERR_NVM_BLANK_MODE;
908         }
909
910         status = ice_discover_flash_size(hw);
911         if (status) {
912                 ice_debug(hw, ICE_DBG_NVM, "NVM init error: failed to discover flash size.\n");
913                 return status;
914         }
915
916         status = ice_determine_active_flash_banks(hw);
917         if (status) {
918                 ice_debug(hw, ICE_DBG_NVM, "Failed to determine active flash banks.\n");
919                 return status;
920         }
921
922         status = ice_get_nvm_ver_info(hw, &flash->nvm);
923         if (status) {
924                 ice_debug(hw, ICE_DBG_INIT, "Failed to read NVM info.\n");
925                 return status;
926         }
927
928         status = ice_get_orom_ver_info(hw, &flash->orom);
929         if (status) {
930                 ice_debug(hw, ICE_DBG_INIT, "Failed to read Option ROM info.\n");
931                 return status;
932         }
933
934         return ICE_SUCCESS;
935 }
936
937 /**
938  * ice_read_sr_buf - Reads Shadow RAM buf and acquire lock if necessary
939  * @hw: pointer to the HW structure
940  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
941  * @words: (in) number of words to read; (out) number of words actually read
942  * @data: words read from the Shadow RAM
943  *
944  * Reads 16 bit words (data buf) from the SR using the ice_read_nvm_buf_aq
945  * method. The buf read is preceded by the NVM ownership take
946  * and followed by the release.
947  */
948 enum ice_status
949 ice_read_sr_buf(struct ice_hw *hw, u16 offset, u16 *words, u16 *data)
950 {
951         enum ice_status status;
952
953         status = ice_acquire_nvm(hw, ICE_RES_READ);
954         if (!status) {
955                 status = ice_read_sr_buf_aq(hw, offset, words, data);
956                 ice_release_nvm(hw);
957         }
958
959         return status;
960 }
961
962 /**
963  * ice_nvm_validate_checksum
964  * @hw: pointer to the HW struct
965  *
966  * Verify NVM PFA checksum validity (0x0706)
967  */
968 enum ice_status ice_nvm_validate_checksum(struct ice_hw *hw)
969 {
970         struct ice_aqc_nvm_checksum *cmd;
971         struct ice_aq_desc desc;
972         enum ice_status status;
973
974         status = ice_acquire_nvm(hw, ICE_RES_READ);
975         if (status)
976                 return status;
977
978         cmd = &desc.params.nvm_checksum;
979
980         ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_checksum);
981         cmd->flags = ICE_AQC_NVM_CHECKSUM_VERIFY;
982
983         status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
984         ice_release_nvm(hw);
985
986         if (!status)
987                 if (LE16_TO_CPU(cmd->checksum) != ICE_AQC_NVM_CHECKSUM_CORRECT)
988                         status = ICE_ERR_NVM_CHECKSUM;
989
990         return status;
991 }
992
993 /**
994  * ice_nvm_access_get_features - Return the NVM access features structure
995  * @cmd: NVM access command to process
996  * @data: storage for the driver NVM features
997  *
998  * Fill in the data section of the NVM access request with a copy of the NVM
999  * features structure.
1000  */
1001 enum ice_status
1002 ice_nvm_access_get_features(struct ice_nvm_access_cmd *cmd,
1003                             union ice_nvm_access_data *data)
1004 {
1005         /* The provided data_size must be at least as large as our NVM
1006          * features structure. A larger size should not be treated as an
1007          * error, to allow future extensions to the features structure to
1008          * work on older drivers.
1009          */
1010         if (cmd->data_size < sizeof(struct ice_nvm_features))
1011                 return ICE_ERR_NO_MEMORY;
1012
1013         /* Initialize the data buffer to zeros */
1014         ice_memset(data, 0, cmd->data_size, ICE_NONDMA_MEM);
1015
1016         /* Fill in the features data */
1017         data->drv_features.major = ICE_NVM_ACCESS_MAJOR_VER;
1018         data->drv_features.minor = ICE_NVM_ACCESS_MINOR_VER;
1019         data->drv_features.size = sizeof(struct ice_nvm_features);
1020         data->drv_features.features[0] = ICE_NVM_FEATURES_0_REG_ACCESS;
1021
1022         return ICE_SUCCESS;
1023 }
1024
1025 /**
1026  * ice_nvm_access_get_module - Helper function to read module value
1027  * @cmd: NVM access command structure
1028  *
1029  * Reads the module value out of the NVM access config field.
1030  */
1031 u32 ice_nvm_access_get_module(struct ice_nvm_access_cmd *cmd)
1032 {
1033         return ((cmd->config & ICE_NVM_CFG_MODULE_M) >> ICE_NVM_CFG_MODULE_S);
1034 }
1035
1036 /**
1037  * ice_nvm_access_get_flags - Helper function to read flags value
1038  * @cmd: NVM access command structure
1039  *
1040  * Reads the flags value out of the NVM access config field.
1041  */
1042 u32 ice_nvm_access_get_flags(struct ice_nvm_access_cmd *cmd)
1043 {
1044         return ((cmd->config & ICE_NVM_CFG_FLAGS_M) >> ICE_NVM_CFG_FLAGS_S);
1045 }
1046
1047 /**
1048  * ice_nvm_access_get_adapter - Helper function to read adapter info
1049  * @cmd: NVM access command structure
1050  *
1051  * Read the adapter info value out of the NVM access config field.
1052  */
1053 u32 ice_nvm_access_get_adapter(struct ice_nvm_access_cmd *cmd)
1054 {
1055         return ((cmd->config & ICE_NVM_CFG_ADAPTER_INFO_M) >>
1056                 ICE_NVM_CFG_ADAPTER_INFO_S);
1057 }
1058
1059 /**
1060  * ice_validate_nvm_rw_reg - Check than an NVM access request is valid
1061  * @cmd: NVM access command structure
1062  *
1063  * Validates that an NVM access structure is request to read or write a valid
1064  * register offset. First validates that the module and flags are correct, and
1065  * then ensures that the register offset is one of the accepted registers.
1066  */
1067 static enum ice_status
1068 ice_validate_nvm_rw_reg(struct ice_nvm_access_cmd *cmd)
1069 {
1070         u32 module, flags, offset;
1071         u16 i;
1072
1073         module = ice_nvm_access_get_module(cmd);
1074         flags = ice_nvm_access_get_flags(cmd);
1075         offset = cmd->offset;
1076
1077         /* Make sure the module and flags indicate a read/write request */
1078         if (module != ICE_NVM_REG_RW_MODULE ||
1079             flags != ICE_NVM_REG_RW_FLAGS ||
1080             cmd->data_size != FIELD_SIZEOF(union ice_nvm_access_data, regval))
1081                 return ICE_ERR_PARAM;
1082
1083         switch (offset) {
1084         case GL_HICR:
1085         case GL_HICR_EN: /* Note, this register is read only */
1086         case GL_FWSTS:
1087         case GL_MNG_FWSM:
1088         case GLGEN_CSR_DEBUG_C:
1089         case GLGEN_RSTAT:
1090         case GLPCI_LBARCTRL:
1091         case GLNVM_GENS:
1092         case GLNVM_FLA:
1093         case PF_FUNC_RID:
1094                 return ICE_SUCCESS;
1095         default:
1096                 break;
1097         }
1098
1099         for (i = 0; i <= ICE_NVM_ACCESS_GL_HIDA_MAX; i++)
1100                 if (offset == (u32)GL_HIDA(i))
1101                         return ICE_SUCCESS;
1102
1103         for (i = 0; i <= ICE_NVM_ACCESS_GL_HIBA_MAX; i++)
1104                 if (offset == (u32)GL_HIBA(i))
1105                         return ICE_SUCCESS;
1106
1107         /* All other register offsets are not valid */
1108         return ICE_ERR_OUT_OF_RANGE;
1109 }
1110
1111 /**
1112  * ice_nvm_access_read - Handle an NVM read request
1113  * @hw: pointer to the HW struct
1114  * @cmd: NVM access command to process
1115  * @data: storage for the register value read
1116  *
1117  * Process an NVM access request to read a register.
1118  */
1119 enum ice_status
1120 ice_nvm_access_read(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
1121                     union ice_nvm_access_data *data)
1122 {
1123         enum ice_status status;
1124
1125         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1126
1127         /* Always initialize the output data, even on failure */
1128         ice_memset(data, 0, cmd->data_size, ICE_NONDMA_MEM);
1129
1130         /* Make sure this is a valid read/write access request */
1131         status = ice_validate_nvm_rw_reg(cmd);
1132         if (status)
1133                 return status;
1134
1135         ice_debug(hw, ICE_DBG_NVM, "NVM access: reading register %08x\n",
1136                   cmd->offset);
1137
1138         /* Read the register and store the contents in the data field */
1139         data->regval = rd32(hw, cmd->offset);
1140
1141         return ICE_SUCCESS;
1142 }
1143
1144 /**
1145  * ice_nvm_access_write - Handle an NVM write request
1146  * @hw: pointer to the HW struct
1147  * @cmd: NVM access command to process
1148  * @data: NVM access data to write
1149  *
1150  * Process an NVM access request to write a register.
1151  */
1152 enum ice_status
1153 ice_nvm_access_write(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
1154                      union ice_nvm_access_data *data)
1155 {
1156         enum ice_status status;
1157
1158         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1159
1160         /* Make sure this is a valid read/write access request */
1161         status = ice_validate_nvm_rw_reg(cmd);
1162         if (status)
1163                 return status;
1164
1165         /* Reject requests to write to read-only registers */
1166         switch (cmd->offset) {
1167         case GL_HICR_EN:
1168         case GLGEN_RSTAT:
1169                 return ICE_ERR_OUT_OF_RANGE;
1170         default:
1171                 break;
1172         }
1173
1174         ice_debug(hw, ICE_DBG_NVM, "NVM access: writing register %08x with value %08x\n",
1175                   cmd->offset, data->regval);
1176
1177         /* Write the data field to the specified register */
1178         wr32(hw, cmd->offset, data->regval);
1179
1180         return ICE_SUCCESS;
1181 }
1182
1183 /**
1184  * ice_handle_nvm_access - Handle an NVM access request
1185  * @hw: pointer to the HW struct
1186  * @cmd: NVM access command info
1187  * @data: pointer to read or return data
1188  *
1189  * Process an NVM access request. Read the command structure information and
1190  * determine if it is valid. If not, report an error indicating the command
1191  * was invalid.
1192  *
1193  * For valid commands, perform the necessary function, copying the data into
1194  * the provided data buffer.
1195  */
1196 enum ice_status
1197 ice_handle_nvm_access(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
1198                       union ice_nvm_access_data *data)
1199 {
1200         u32 module, flags, adapter_info;
1201
1202         ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1203
1204         /* Extended flags are currently reserved and must be zero */
1205         if ((cmd->config & ICE_NVM_CFG_EXT_FLAGS_M) != 0)
1206                 return ICE_ERR_PARAM;
1207
1208         /* Adapter info must match the HW device ID */
1209         adapter_info = ice_nvm_access_get_adapter(cmd);
1210         if (adapter_info != hw->device_id)
1211                 return ICE_ERR_PARAM;
1212
1213         switch (cmd->command) {
1214         case ICE_NVM_CMD_READ:
1215                 module = ice_nvm_access_get_module(cmd);
1216                 flags = ice_nvm_access_get_flags(cmd);
1217
1218                 /* Getting the driver's NVM features structure shares the same
1219                  * command type as reading a register. Read the config field
1220                  * to determine if this is a request to get features.
1221                  */
1222                 if (module == ICE_NVM_GET_FEATURES_MODULE &&
1223                     flags == ICE_NVM_GET_FEATURES_FLAGS &&
1224                     cmd->offset == 0)
1225                         return ice_nvm_access_get_features(cmd, data);
1226                 else
1227                         return ice_nvm_access_read(hw, cmd, data);
1228         case ICE_NVM_CMD_WRITE:
1229                 return ice_nvm_access_write(hw, cmd, data);
1230         default:
1231                 return ICE_ERR_PARAM;
1232         }
1233 }