1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
4 #include <linux/vmalloc.h>
6 #include "ice_common.h"
10 * @hw: pointer to the HW struct
11 * @module_typeid: module pointer location in words from the NVM beginning
12 * @offset: byte offset from the module beginning
13 * @length: length of the section to be read (in bytes from the offset)
14 * @data: command buffer (size [bytes] = length)
15 * @last_command: tells if this is the last command in a series
16 * @read_shadow_ram: tell if this is a shadow RAM read
17 * @cd: pointer to command details structure or NULL
19 * Read the NVM using the admin queue commands (0x0701)
21 int ice_aq_read_nvm(struct ice_hw *hw, u16 module_typeid, u32 offset,
22 u16 length, void *data, bool last_command,
23 bool read_shadow_ram, struct ice_sq_cd *cd)
25 struct ice_aq_desc desc;
26 struct ice_aqc_nvm *cmd;
28 cmd = &desc.params.nvm;
30 if (offset > ICE_AQC_NVM_MAX_OFFSET)
33 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_read);
35 if (!read_shadow_ram && module_typeid == ICE_AQC_NVM_START_POINT)
36 cmd->cmd_flags |= ICE_AQC_NVM_FLASH_ONLY;
38 /* If this is the last command in a series, set the proper flag. */
40 cmd->cmd_flags |= ICE_AQC_NVM_LAST_CMD;
41 cmd->module_typeid = cpu_to_le16(module_typeid);
42 cmd->offset_low = cpu_to_le16(offset & 0xFFFF);
43 cmd->offset_high = (offset >> 16) & 0xFF;
44 cmd->length = cpu_to_le16(length);
46 return ice_aq_send_cmd(hw, &desc, data, length, cd);
50 * ice_read_flat_nvm - Read portion of NVM by flat offset
51 * @hw: pointer to the HW struct
52 * @offset: offset from beginning of NVM
53 * @length: (in) number of bytes to read; (out) number of bytes actually read
54 * @data: buffer to return data in (sized to fit the specified length)
55 * @read_shadow_ram: if true, read from shadow RAM instead of NVM
57 * Reads a portion of the NVM, as a flat memory space. This function correctly
58 * breaks read requests across Shadow RAM sectors and ensures that no single
59 * read request exceeds the maximum 4KB read for a single AdminQ command.
61 * Returns a status code on failure. Note that the data pointer may be
62 * partially updated if some reads succeed before a failure.
65 ice_read_flat_nvm(struct ice_hw *hw, u32 offset, u32 *length, u8 *data,
75 /* Verify the length of the read if this is for the Shadow RAM */
76 if (read_shadow_ram && ((offset + inlen) > (hw->flash.sr_words * 2u))) {
77 ice_debug(hw, ICE_DBG_NVM, "NVM error: requested offset is beyond Shadow RAM limit\n");
82 u32 read_size, sector_offset;
84 /* ice_aq_read_nvm cannot read more than 4KB at a time.
85 * Additionally, a read from the Shadow RAM may not cross over
86 * a sector boundary. Conveniently, the sector size is also
89 sector_offset = offset % ICE_AQ_MAX_BUF_LEN;
90 read_size = min_t(u32, ICE_AQ_MAX_BUF_LEN - sector_offset,
93 last_cmd = !(bytes_read + read_size < inlen);
95 status = ice_aq_read_nvm(hw, ICE_AQC_NVM_START_POINT,
97 data + bytes_read, last_cmd,
98 read_shadow_ram, NULL);
102 bytes_read += read_size;
106 *length = bytes_read;
112 * @hw: pointer to the HW struct
113 * @module_typeid: module pointer location in words from the NVM beginning
114 * @offset: byte offset from the module beginning
115 * @length: length of the section to be written (in bytes from the offset)
116 * @data: command buffer (size [bytes] = length)
117 * @last_command: tells if this is the last command in a series
118 * @command_flags: command parameters
119 * @cd: pointer to command details structure or NULL
121 * Update the NVM using the admin queue commands (0x0703)
124 ice_aq_update_nvm(struct ice_hw *hw, u16 module_typeid, u32 offset,
125 u16 length, void *data, bool last_command, u8 command_flags,
126 struct ice_sq_cd *cd)
128 struct ice_aq_desc desc;
129 struct ice_aqc_nvm *cmd;
131 cmd = &desc.params.nvm;
133 /* In offset the highest byte must be zeroed. */
134 if (offset & 0xFF000000)
137 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_write);
139 cmd->cmd_flags |= command_flags;
141 /* If this is the last command in a series, set the proper flag. */
143 cmd->cmd_flags |= ICE_AQC_NVM_LAST_CMD;
144 cmd->module_typeid = cpu_to_le16(module_typeid);
145 cmd->offset_low = cpu_to_le16(offset & 0xFFFF);
146 cmd->offset_high = (offset >> 16) & 0xFF;
147 cmd->length = cpu_to_le16(length);
149 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
151 return ice_aq_send_cmd(hw, &desc, data, length, cd);
156 * @hw: pointer to the HW struct
157 * @module_typeid: module pointer location in words from the NVM beginning
158 * @cd: pointer to command details structure or NULL
160 * Erase the NVM sector using the admin queue commands (0x0702)
162 int ice_aq_erase_nvm(struct ice_hw *hw, u16 module_typeid, struct ice_sq_cd *cd)
164 struct ice_aq_desc desc;
165 struct ice_aqc_nvm *cmd;
167 cmd = &desc.params.nvm;
169 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_erase);
171 cmd->module_typeid = cpu_to_le16(module_typeid);
172 cmd->length = cpu_to_le16(ICE_AQC_NVM_ERASE_LEN);
174 cmd->offset_high = 0;
176 return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
180 * ice_read_sr_word_aq - Reads Shadow RAM via AQ
181 * @hw: pointer to the HW structure
182 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
183 * @data: word read from the Shadow RAM
185 * Reads one 16 bit word from the Shadow RAM using ice_read_flat_nvm.
187 static int ice_read_sr_word_aq(struct ice_hw *hw, u16 offset, u16 *data)
189 u32 bytes = sizeof(u16);
193 /* Note that ice_read_flat_nvm takes into account the 4Kb AdminQ and
194 * Shadow RAM sector restrictions necessary when reading from the NVM.
196 status = ice_read_flat_nvm(hw, offset * sizeof(u16), &bytes,
197 (__force u8 *)&data_local, true);
201 *data = le16_to_cpu(data_local);
206 * ice_acquire_nvm - Generic request for acquiring the NVM ownership
207 * @hw: pointer to the HW structure
208 * @access: NVM access type (read or write)
210 * This function will request NVM ownership.
212 int ice_acquire_nvm(struct ice_hw *hw, enum ice_aq_res_access_type access)
214 if (hw->flash.blank_nvm_mode)
217 return ice_acquire_res(hw, ICE_NVM_RES_ID, access, ICE_NVM_TIMEOUT);
221 * ice_release_nvm - Generic request for releasing the NVM ownership
222 * @hw: pointer to the HW structure
224 * This function will release NVM ownership.
226 void ice_release_nvm(struct ice_hw *hw)
228 if (hw->flash.blank_nvm_mode)
231 ice_release_res(hw, ICE_NVM_RES_ID);
235 * ice_get_flash_bank_offset - Get offset into requested flash bank
236 * @hw: pointer to the HW structure
237 * @bank: whether to read from the active or inactive flash bank
238 * @module: the module to read from
240 * Based on the module, lookup the module offset from the beginning of the
243 * Returns the flash offset. Note that a value of zero is invalid and must be
244 * treated as an error.
246 static u32 ice_get_flash_bank_offset(struct ice_hw *hw, enum ice_bank_select bank, u16 module)
248 struct ice_bank_info *banks = &hw->flash.banks;
249 enum ice_flash_bank active_bank;
250 bool second_bank_active;
254 case ICE_SR_1ST_NVM_BANK_PTR:
255 offset = banks->nvm_ptr;
256 size = banks->nvm_size;
257 active_bank = banks->nvm_bank;
259 case ICE_SR_1ST_OROM_BANK_PTR:
260 offset = banks->orom_ptr;
261 size = banks->orom_size;
262 active_bank = banks->orom_bank;
264 case ICE_SR_NETLIST_BANK_PTR:
265 offset = banks->netlist_ptr;
266 size = banks->netlist_size;
267 active_bank = banks->netlist_bank;
270 ice_debug(hw, ICE_DBG_NVM, "Unexpected value for flash module: 0x%04x\n", module);
274 switch (active_bank) {
275 case ICE_1ST_FLASH_BANK:
276 second_bank_active = false;
278 case ICE_2ND_FLASH_BANK:
279 second_bank_active = true;
282 ice_debug(hw, ICE_DBG_NVM, "Unexpected value for active flash bank: %u\n",
287 /* The second flash bank is stored immediately following the first
288 * bank. Based on whether the 1st or 2nd bank is active, and whether
289 * we want the active or inactive bank, calculate the desired offset.
292 case ICE_ACTIVE_FLASH_BANK:
293 return offset + (second_bank_active ? size : 0);
294 case ICE_INACTIVE_FLASH_BANK:
295 return offset + (second_bank_active ? 0 : size);
298 ice_debug(hw, ICE_DBG_NVM, "Unexpected value for flash bank selection: %u\n", bank);
303 * ice_read_flash_module - Read a word from one of the main NVM modules
304 * @hw: pointer to the HW structure
305 * @bank: which bank of the module to read
306 * @module: the module to read
307 * @offset: the offset into the module in bytes
308 * @data: storage for the word read from the flash
309 * @length: bytes of data to read
311 * Read data from the specified flash module. The bank parameter indicates
312 * whether or not to read from the active bank or the inactive bank of that
315 * The word will be read using flat NVM access, and relies on the
316 * hw->flash.banks data being setup by ice_determine_active_flash_banks()
317 * during initialization.
320 ice_read_flash_module(struct ice_hw *hw, enum ice_bank_select bank, u16 module,
321 u32 offset, u8 *data, u32 length)
326 start = ice_get_flash_bank_offset(hw, bank, module);
328 ice_debug(hw, ICE_DBG_NVM, "Unable to calculate flash bank offset for module 0x%04x\n",
333 status = ice_acquire_nvm(hw, ICE_RES_READ);
337 status = ice_read_flat_nvm(hw, start + offset, &length, data, false);
345 * ice_read_nvm_module - Read from the active main NVM module
346 * @hw: pointer to the HW structure
347 * @bank: whether to read from active or inactive NVM module
348 * @offset: offset into the NVM module to read, in words
349 * @data: storage for returned word value
351 * Read the specified word from the active NVM module. This includes the CSS
352 * header at the start of the NVM module.
355 ice_read_nvm_module(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
360 status = ice_read_flash_module(hw, bank, ICE_SR_1ST_NVM_BANK_PTR, offset * sizeof(u16),
361 (__force u8 *)&data_local, sizeof(u16));
363 *data = le16_to_cpu(data_local);
369 * ice_read_nvm_sr_copy - Read a word from the Shadow RAM copy in the NVM bank
370 * @hw: pointer to the HW structure
371 * @bank: whether to read from the active or inactive NVM module
372 * @offset: offset into the Shadow RAM copy to read, in words
373 * @data: storage for returned word value
375 * Read the specified word from the copy of the Shadow RAM found in the
376 * specified NVM module.
378 * Note that the Shadow RAM copy is always located after the CSS header, and
379 * is aligned to 64-byte (32-word) offsets.
382 ice_read_nvm_sr_copy(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
387 case ICE_ACTIVE_FLASH_BANK:
388 sr_copy = roundup(hw->flash.banks.active_css_hdr_len, 32);
390 case ICE_INACTIVE_FLASH_BANK:
391 sr_copy = roundup(hw->flash.banks.inactive_css_hdr_len, 32);
395 return ice_read_nvm_module(hw, bank, sr_copy + offset, data);
399 * ice_read_netlist_module - Read data from the netlist module area
400 * @hw: pointer to the HW structure
401 * @bank: whether to read from the active or inactive module
402 * @offset: offset into the netlist to read from
403 * @data: storage for returned word value
405 * Read a word from the specified netlist bank.
408 ice_read_netlist_module(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
413 status = ice_read_flash_module(hw, bank, ICE_SR_NETLIST_BANK_PTR, offset * sizeof(u16),
414 (__force u8 *)&data_local, sizeof(u16));
416 *data = le16_to_cpu(data_local);
422 * ice_read_sr_word - Reads Shadow RAM word and acquire NVM if necessary
423 * @hw: pointer to the HW structure
424 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
425 * @data: word read from the Shadow RAM
427 * Reads one 16 bit word from the Shadow RAM using the ice_read_sr_word_aq.
429 int ice_read_sr_word(struct ice_hw *hw, u16 offset, u16 *data)
433 status = ice_acquire_nvm(hw, ICE_RES_READ);
435 status = ice_read_sr_word_aq(hw, offset, data);
443 * ice_get_pfa_module_tlv - Reads sub module TLV from NVM PFA
444 * @hw: pointer to hardware structure
445 * @module_tlv: pointer to module TLV to return
446 * @module_tlv_len: pointer to module TLV length to return
447 * @module_type: module type requested
449 * Finds the requested sub module TLV type from the Preserved Field
450 * Area (PFA) and returns the TLV pointer and length. The caller can
451 * use these to read the variable length TLV value.
454 ice_get_pfa_module_tlv(struct ice_hw *hw, u16 *module_tlv, u16 *module_tlv_len,
457 u16 pfa_len, pfa_ptr, next_tlv, max_tlv;
460 status = ice_read_sr_word(hw, ICE_SR_PFA_PTR, &pfa_ptr);
462 ice_debug(hw, ICE_DBG_INIT, "Preserved Field Array pointer.\n");
465 status = ice_read_sr_word(hw, pfa_ptr, &pfa_len);
467 ice_debug(hw, ICE_DBG_INIT, "Failed to read PFA length.\n");
471 /* The Preserved Fields Area contains a sequence of Type-Length-Value
472 * structures which define its contents. The PFA length includes all
473 * of the TLVs, plus the initial length word itself, *and* one final
474 * word at the end after all of the TLVs.
476 if (check_add_overflow(pfa_ptr, pfa_len - 1, &max_tlv)) {
477 dev_warn(ice_hw_to_dev(hw), "PFA starts at offset %u. PFA length of %u caused 16-bit arithmetic overflow.\n",
482 /* Starting with first TLV after PFA length, iterate through the list
483 * of TLVs to find the requested one.
485 next_tlv = pfa_ptr + 1;
486 while (next_tlv < max_tlv) {
487 u16 tlv_sub_module_type;
491 status = ice_read_sr_word(hw, next_tlv, &tlv_sub_module_type);
493 ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV type.\n");
496 /* Read TLV length */
497 status = ice_read_sr_word(hw, next_tlv + 1, &tlv_len);
499 ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV length.\n");
502 if (tlv_sub_module_type == module_type) {
504 *module_tlv = next_tlv;
505 *module_tlv_len = tlv_len;
511 if (check_add_overflow(next_tlv, 2, &next_tlv) ||
512 check_add_overflow(next_tlv, tlv_len, &next_tlv)) {
513 dev_warn(ice_hw_to_dev(hw), "TLV of type %u and length 0x%04x caused 16-bit arithmetic overflow. The PFA starts at 0x%04x and has length of 0x%04x\n",
514 tlv_sub_module_type, tlv_len, pfa_ptr, pfa_len);
518 /* Module does not exist */
523 * ice_read_pba_string - Reads part number string from NVM
524 * @hw: pointer to hardware structure
525 * @pba_num: stores the part number string from the NVM
526 * @pba_num_size: part number string buffer length
528 * Reads the part number string from the NVM.
530 int ice_read_pba_string(struct ice_hw *hw, u8 *pba_num, u32 pba_num_size)
532 u16 pba_tlv, pba_tlv_len;
533 u16 pba_word, pba_size;
537 status = ice_get_pfa_module_tlv(hw, &pba_tlv, &pba_tlv_len,
538 ICE_SR_PBA_BLOCK_PTR);
540 ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block TLV.\n");
544 /* pba_size is the next word */
545 status = ice_read_sr_word(hw, (pba_tlv + 2), &pba_size);
547 ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Section size.\n");
551 if (pba_tlv_len < pba_size) {
552 ice_debug(hw, ICE_DBG_INIT, "Invalid PBA Block TLV size.\n");
556 /* Subtract one to get PBA word count (PBA Size word is included in
560 if (pba_num_size < (((u32)pba_size * 2) + 1)) {
561 ice_debug(hw, ICE_DBG_INIT, "Buffer too small for PBA data.\n");
565 for (i = 0; i < pba_size; i++) {
566 status = ice_read_sr_word(hw, (pba_tlv + 2 + 1) + i, &pba_word);
568 ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block word %d.\n", i);
572 pba_num[(i * 2)] = (pba_word >> 8) & 0xFF;
573 pba_num[(i * 2) + 1] = pba_word & 0xFF;
575 pba_num[(pba_size * 2)] = '\0';
581 * ice_get_nvm_ver_info - Read NVM version information
582 * @hw: pointer to the HW struct
583 * @bank: whether to read from the active or inactive flash bank
584 * @nvm: pointer to NVM info structure
586 * Read the NVM EETRACK ID and map version of the main NVM image bank, filling
587 * in the NVM info structure.
590 ice_get_nvm_ver_info(struct ice_hw *hw, enum ice_bank_select bank, struct ice_nvm_info *nvm)
592 u16 eetrack_lo, eetrack_hi, ver;
595 status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_DEV_STARTER_VER, &ver);
597 ice_debug(hw, ICE_DBG_NVM, "Failed to read DEV starter version.\n");
601 nvm->major = FIELD_GET(ICE_NVM_VER_HI_MASK, ver);
602 nvm->minor = FIELD_GET(ICE_NVM_VER_LO_MASK, ver);
604 status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_EETRACK_LO, &eetrack_lo);
606 ice_debug(hw, ICE_DBG_NVM, "Failed to read EETRACK lo.\n");
609 status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_EETRACK_HI, &eetrack_hi);
611 ice_debug(hw, ICE_DBG_NVM, "Failed to read EETRACK hi.\n");
615 nvm->eetrack = (eetrack_hi << 16) | eetrack_lo;
621 * ice_get_inactive_nvm_ver - Read Option ROM version from the inactive bank
622 * @hw: pointer to the HW structure
623 * @nvm: storage for Option ROM version information
625 * Reads the NVM EETRACK ID, Map version, and security revision of the
626 * inactive NVM bank. Used to access version data for a pending update that
627 * has not yet been activated.
629 int ice_get_inactive_nvm_ver(struct ice_hw *hw, struct ice_nvm_info *nvm)
631 return ice_get_nvm_ver_info(hw, ICE_INACTIVE_FLASH_BANK, nvm);
635 * ice_get_orom_civd_data - Get the combo version information from Option ROM
636 * @hw: pointer to the HW struct
637 * @bank: whether to read from the active or inactive flash module
638 * @civd: storage for the Option ROM CIVD data.
640 * Searches through the Option ROM flash contents to locate the CIVD data for
644 ice_get_orom_civd_data(struct ice_hw *hw, enum ice_bank_select bank,
645 struct ice_orom_civd_info *civd)
651 /* The CIVD section is located in the Option ROM aligned to 512 bytes.
652 * The first 4 bytes must contain the ASCII characters "$CIV".
653 * A simple modulo 256 sum of all of the bytes of the structure must
656 * The exact location is unknown and varies between images but is
657 * usually somewhere in the middle of the bank. We need to scan the
658 * Option ROM bank to locate it.
660 * It's significantly faster to read the entire Option ROM up front
661 * using the maximum page size, than to read each possible location
662 * with a separate firmware command.
664 orom_data = vzalloc(hw->flash.banks.orom_size);
668 status = ice_read_flash_module(hw, bank, ICE_SR_1ST_OROM_BANK_PTR, 0,
669 orom_data, hw->flash.banks.orom_size);
672 ice_debug(hw, ICE_DBG_NVM, "Unable to read Option ROM data\n");
676 /* Scan the memory buffer to locate the CIVD data section */
677 for (offset = 0; (offset + 512) <= hw->flash.banks.orom_size; offset += 512) {
678 struct ice_orom_civd_info *tmp;
681 tmp = (struct ice_orom_civd_info *)&orom_data[offset];
683 /* Skip forward until we find a matching signature */
684 if (memcmp("$CIV", tmp->signature, sizeof(tmp->signature)) != 0)
687 ice_debug(hw, ICE_DBG_NVM, "Found CIVD section at offset %u\n",
690 /* Verify that the simple checksum is zero */
691 for (i = 0; i < sizeof(*tmp); i++)
692 sum += ((u8 *)tmp)[i];
695 ice_debug(hw, ICE_DBG_NVM, "Found CIVD data with invalid checksum of %u\n",
697 goto err_invalid_checksum;
705 ice_debug(hw, ICE_DBG_NVM, "Unable to locate CIVD data within the Option ROM\n");
707 err_invalid_checksum:
713 * ice_get_orom_ver_info - Read Option ROM version information
714 * @hw: pointer to the HW struct
715 * @bank: whether to read from the active or inactive flash module
716 * @orom: pointer to Option ROM info structure
718 * Read Option ROM version and security revision from the Option ROM flash
722 ice_get_orom_ver_info(struct ice_hw *hw, enum ice_bank_select bank, struct ice_orom_info *orom)
724 struct ice_orom_civd_info civd;
728 status = ice_get_orom_civd_data(hw, bank, &civd);
730 ice_debug(hw, ICE_DBG_NVM, "Failed to locate valid Option ROM CIVD data\n");
734 combo_ver = le32_to_cpu(civd.combo_ver);
736 orom->major = FIELD_GET(ICE_OROM_VER_MASK, combo_ver);
737 orom->patch = FIELD_GET(ICE_OROM_VER_PATCH_MASK, combo_ver);
738 orom->build = FIELD_GET(ICE_OROM_VER_BUILD_MASK, combo_ver);
744 * ice_get_inactive_orom_ver - Read Option ROM version from the inactive bank
745 * @hw: pointer to the HW structure
746 * @orom: storage for Option ROM version information
748 * Reads the Option ROM version and security revision data for the inactive
749 * section of flash. Used to access version data for a pending update that has
750 * not yet been activated.
752 int ice_get_inactive_orom_ver(struct ice_hw *hw, struct ice_orom_info *orom)
754 return ice_get_orom_ver_info(hw, ICE_INACTIVE_FLASH_BANK, orom);
758 * ice_get_netlist_info
759 * @hw: pointer to the HW struct
760 * @bank: whether to read from the active or inactive flash bank
761 * @netlist: pointer to netlist version info structure
763 * Get the netlist version information from the requested bank. Reads the Link
764 * Topology section to find the Netlist ID block and extract the relevant
765 * information into the netlist version structure.
768 ice_get_netlist_info(struct ice_hw *hw, enum ice_bank_select bank,
769 struct ice_netlist_info *netlist)
771 u16 module_id, length, node_count, i;
775 status = ice_read_netlist_module(hw, bank, ICE_NETLIST_TYPE_OFFSET, &module_id);
779 if (module_id != ICE_NETLIST_LINK_TOPO_MOD_ID) {
780 ice_debug(hw, ICE_DBG_NVM, "Expected netlist module_id ID of 0x%04x, but got 0x%04x\n",
781 ICE_NETLIST_LINK_TOPO_MOD_ID, module_id);
785 status = ice_read_netlist_module(hw, bank, ICE_LINK_TOPO_MODULE_LEN, &length);
789 /* sanity check that we have at least enough words to store the netlist ID block */
790 if (length < ICE_NETLIST_ID_BLK_SIZE) {
791 ice_debug(hw, ICE_DBG_NVM, "Netlist Link Topology module too small. Expected at least %u words, but got %u words.\n",
792 ICE_NETLIST_ID_BLK_SIZE, length);
796 status = ice_read_netlist_module(hw, bank, ICE_LINK_TOPO_NODE_COUNT, &node_count);
799 node_count &= ICE_LINK_TOPO_NODE_COUNT_M;
801 id_blk = kcalloc(ICE_NETLIST_ID_BLK_SIZE, sizeof(*id_blk), GFP_KERNEL);
805 /* Read out the entire Netlist ID Block at once. */
806 status = ice_read_flash_module(hw, bank, ICE_SR_NETLIST_BANK_PTR,
807 ICE_NETLIST_ID_BLK_OFFSET(node_count) * sizeof(u16),
808 (u8 *)id_blk, ICE_NETLIST_ID_BLK_SIZE * sizeof(u16));
812 for (i = 0; i < ICE_NETLIST_ID_BLK_SIZE; i++)
813 id_blk[i] = le16_to_cpu(((__force __le16 *)id_blk)[i]);
815 netlist->major = id_blk[ICE_NETLIST_ID_BLK_MAJOR_VER_HIGH] << 16 |
816 id_blk[ICE_NETLIST_ID_BLK_MAJOR_VER_LOW];
817 netlist->minor = id_blk[ICE_NETLIST_ID_BLK_MINOR_VER_HIGH] << 16 |
818 id_blk[ICE_NETLIST_ID_BLK_MINOR_VER_LOW];
819 netlist->type = id_blk[ICE_NETLIST_ID_BLK_TYPE_HIGH] << 16 |
820 id_blk[ICE_NETLIST_ID_BLK_TYPE_LOW];
821 netlist->rev = id_blk[ICE_NETLIST_ID_BLK_REV_HIGH] << 16 |
822 id_blk[ICE_NETLIST_ID_BLK_REV_LOW];
823 netlist->cust_ver = id_blk[ICE_NETLIST_ID_BLK_CUST_VER];
824 /* Read the left most 4 bytes of SHA */
825 netlist->hash = id_blk[ICE_NETLIST_ID_BLK_SHA_HASH_WORD(15)] << 16 |
826 id_blk[ICE_NETLIST_ID_BLK_SHA_HASH_WORD(14)];
835 * ice_get_inactive_netlist_ver
836 * @hw: pointer to the HW struct
837 * @netlist: pointer to netlist version info structure
839 * Read the netlist version data from the inactive netlist bank. Used to
840 * extract version data of a pending flash update in order to display the
843 int ice_get_inactive_netlist_ver(struct ice_hw *hw, struct ice_netlist_info *netlist)
845 return ice_get_netlist_info(hw, ICE_INACTIVE_FLASH_BANK, netlist);
849 * ice_discover_flash_size - Discover the available flash size.
850 * @hw: pointer to the HW struct
852 * The device flash could be up to 16MB in size. However, it is possible that
853 * the actual size is smaller. Use bisection to determine the accessible size
856 static int ice_discover_flash_size(struct ice_hw *hw)
858 u32 min_size = 0, max_size = ICE_AQC_NVM_MAX_OFFSET + 1;
861 status = ice_acquire_nvm(hw, ICE_RES_READ);
865 while ((max_size - min_size) > 1) {
866 u32 offset = (max_size + min_size) / 2;
870 status = ice_read_flat_nvm(hw, offset, &len, &data, false);
871 if (status == -EIO &&
872 hw->adminq.sq_last_status == ICE_AQ_RC_EINVAL) {
873 ice_debug(hw, ICE_DBG_NVM, "%s: New upper bound of %u bytes\n",
877 } else if (!status) {
878 ice_debug(hw, ICE_DBG_NVM, "%s: New lower bound of %u bytes\n",
882 /* an unexpected error occurred */
883 goto err_read_flat_nvm;
887 ice_debug(hw, ICE_DBG_NVM, "Predicted flash size is %u bytes\n", max_size);
889 hw->flash.flash_size = max_size;
898 * ice_read_sr_pointer - Read the value of a Shadow RAM pointer word
899 * @hw: pointer to the HW structure
900 * @offset: the word offset of the Shadow RAM word to read
901 * @pointer: pointer value read from Shadow RAM
903 * Read the given Shadow RAM word, and convert it to a pointer value specified
904 * in bytes. This function assumes the specified offset is a valid pointer
907 * Each pointer word specifies whether it is stored in word size or 4KB
908 * sector size by using the highest bit. The reported pointer value will be in
909 * bytes, intended for flat NVM reads.
911 static int ice_read_sr_pointer(struct ice_hw *hw, u16 offset, u32 *pointer)
916 status = ice_read_sr_word(hw, offset, &value);
920 /* Determine if the pointer is in 4KB or word units */
921 if (value & ICE_SR_NVM_PTR_4KB_UNITS)
922 *pointer = (value & ~ICE_SR_NVM_PTR_4KB_UNITS) * 4 * 1024;
924 *pointer = value * 2;
930 * ice_read_sr_area_size - Read an area size from a Shadow RAM word
931 * @hw: pointer to the HW structure
932 * @offset: the word offset of the Shadow RAM to read
933 * @size: size value read from the Shadow RAM
935 * Read the given Shadow RAM word, and convert it to an area size value
936 * specified in bytes. This function assumes the specified offset is a valid
939 * Each area size word is specified in 4KB sector units. This function reports
940 * the size in bytes, intended for flat NVM reads.
942 static int ice_read_sr_area_size(struct ice_hw *hw, u16 offset, u32 *size)
947 status = ice_read_sr_word(hw, offset, &value);
951 /* Area sizes are always specified in 4KB units */
952 *size = value * 4 * 1024;
958 * ice_determine_active_flash_banks - Discover active bank for each module
959 * @hw: pointer to the HW struct
961 * Read the Shadow RAM control word and determine which banks are active for
962 * the NVM, OROM, and Netlist modules. Also read and calculate the associated
963 * pointer and size. These values are then cached into the ice_flash_info
964 * structure for later use in order to calculate the correct offset to read
965 * from the active module.
967 static int ice_determine_active_flash_banks(struct ice_hw *hw)
969 struct ice_bank_info *banks = &hw->flash.banks;
973 status = ice_read_sr_word(hw, ICE_SR_NVM_CTRL_WORD, &ctrl_word);
975 ice_debug(hw, ICE_DBG_NVM, "Failed to read the Shadow RAM control word\n");
979 /* Check that the control word indicates validity */
980 if (FIELD_GET(ICE_SR_CTRL_WORD_1_M, ctrl_word) !=
981 ICE_SR_CTRL_WORD_VALID) {
982 ice_debug(hw, ICE_DBG_NVM, "Shadow RAM control word is invalid\n");
986 if (!(ctrl_word & ICE_SR_CTRL_WORD_NVM_BANK))
987 banks->nvm_bank = ICE_1ST_FLASH_BANK;
989 banks->nvm_bank = ICE_2ND_FLASH_BANK;
991 if (!(ctrl_word & ICE_SR_CTRL_WORD_OROM_BANK))
992 banks->orom_bank = ICE_1ST_FLASH_BANK;
994 banks->orom_bank = ICE_2ND_FLASH_BANK;
996 if (!(ctrl_word & ICE_SR_CTRL_WORD_NETLIST_BANK))
997 banks->netlist_bank = ICE_1ST_FLASH_BANK;
999 banks->netlist_bank = ICE_2ND_FLASH_BANK;
1001 status = ice_read_sr_pointer(hw, ICE_SR_1ST_NVM_BANK_PTR, &banks->nvm_ptr);
1003 ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank pointer\n");
1007 status = ice_read_sr_area_size(hw, ICE_SR_NVM_BANK_SIZE, &banks->nvm_size);
1009 ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank area size\n");
1013 status = ice_read_sr_pointer(hw, ICE_SR_1ST_OROM_BANK_PTR, &banks->orom_ptr);
1015 ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank pointer\n");
1019 status = ice_read_sr_area_size(hw, ICE_SR_OROM_BANK_SIZE, &banks->orom_size);
1021 ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank area size\n");
1025 status = ice_read_sr_pointer(hw, ICE_SR_NETLIST_BANK_PTR, &banks->netlist_ptr);
1027 ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank pointer\n");
1031 status = ice_read_sr_area_size(hw, ICE_SR_NETLIST_BANK_SIZE, &banks->netlist_size);
1033 ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank area size\n");
1041 * ice_get_nvm_css_hdr_len - Read the CSS header length from the NVM CSS header
1042 * @hw: pointer to the HW struct
1043 * @bank: whether to read from the active or inactive flash bank
1044 * @hdr_len: storage for header length in words
1046 * Read the CSS header length from the NVM CSS header and add the Authentication
1047 * header size, and then convert to words.
1049 * Return: zero on success, or a negative error code on failure.
1052 ice_get_nvm_css_hdr_len(struct ice_hw *hw, enum ice_bank_select bank,
1055 u16 hdr_len_l, hdr_len_h;
1059 status = ice_read_nvm_module(hw, bank, ICE_NVM_CSS_HDR_LEN_L,
1064 status = ice_read_nvm_module(hw, bank, ICE_NVM_CSS_HDR_LEN_H,
1069 /* CSS header length is in DWORD, so convert to words and add
1070 * authentication header size
1072 hdr_len_dword = hdr_len_h << 16 | hdr_len_l;
1073 *hdr_len = (hdr_len_dword * 2) + ICE_NVM_AUTH_HEADER_LEN;
1079 * ice_determine_css_hdr_len - Discover CSS header length for the device
1080 * @hw: pointer to the HW struct
1082 * Determine the size of the CSS header at the start of the NVM module. This
1083 * is useful for locating the Shadow RAM copy in the NVM, as the Shadow RAM is
1084 * always located just after the CSS header.
1086 * Return: zero on success, or a negative error code on failure.
1088 static int ice_determine_css_hdr_len(struct ice_hw *hw)
1090 struct ice_bank_info *banks = &hw->flash.banks;
1093 status = ice_get_nvm_css_hdr_len(hw, ICE_ACTIVE_FLASH_BANK,
1094 &banks->active_css_hdr_len);
1098 status = ice_get_nvm_css_hdr_len(hw, ICE_INACTIVE_FLASH_BANK,
1099 &banks->inactive_css_hdr_len);
1107 * ice_init_nvm - initializes NVM setting
1108 * @hw: pointer to the HW struct
1110 * This function reads and populates NVM settings such as Shadow RAM size,
1111 * max_timeout, and blank_nvm_mode
1113 int ice_init_nvm(struct ice_hw *hw)
1115 struct ice_flash_info *flash = &hw->flash;
1120 /* The SR size is stored regardless of the NVM programming mode
1121 * as the blank mode may be used in the factory line.
1123 gens_stat = rd32(hw, GLNVM_GENS);
1124 sr_size = FIELD_GET(GLNVM_GENS_SR_SIZE_M, gens_stat);
1126 /* Switching to words (sr_size contains power of 2) */
1127 flash->sr_words = BIT(sr_size) * ICE_SR_WORDS_IN_1KB;
1129 /* Check if we are in the normal or blank NVM programming mode */
1130 fla = rd32(hw, GLNVM_FLA);
1131 if (fla & GLNVM_FLA_LOCKED_M) { /* Normal programming mode */
1132 flash->blank_nvm_mode = false;
1134 /* Blank programming mode */
1135 flash->blank_nvm_mode = true;
1136 ice_debug(hw, ICE_DBG_NVM, "NVM init error: unsupported blank mode.\n");
1140 status = ice_discover_flash_size(hw);
1142 ice_debug(hw, ICE_DBG_NVM, "NVM init error: failed to discover flash size.\n");
1146 status = ice_determine_active_flash_banks(hw);
1148 ice_debug(hw, ICE_DBG_NVM, "Failed to determine active flash banks.\n");
1152 status = ice_determine_css_hdr_len(hw);
1154 ice_debug(hw, ICE_DBG_NVM, "Failed to determine Shadow RAM copy offsets.\n");
1158 status = ice_get_nvm_ver_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->nvm);
1160 ice_debug(hw, ICE_DBG_INIT, "Failed to read NVM info.\n");
1164 status = ice_get_orom_ver_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->orom);
1166 ice_debug(hw, ICE_DBG_INIT, "Failed to read Option ROM info.\n");
1168 /* read the netlist version information */
1169 status = ice_get_netlist_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->netlist);
1171 ice_debug(hw, ICE_DBG_INIT, "Failed to read netlist info.\n");
1177 * ice_nvm_validate_checksum
1178 * @hw: pointer to the HW struct
1180 * Verify NVM PFA checksum validity (0x0706)
1182 int ice_nvm_validate_checksum(struct ice_hw *hw)
1184 struct ice_aqc_nvm_checksum *cmd;
1185 struct ice_aq_desc desc;
1188 status = ice_acquire_nvm(hw, ICE_RES_READ);
1192 cmd = &desc.params.nvm_checksum;
1194 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_checksum);
1195 cmd->flags = ICE_AQC_NVM_CHECKSUM_VERIFY;
1197 status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1198 ice_release_nvm(hw);
1201 if (le16_to_cpu(cmd->checksum) != ICE_AQC_NVM_CHECKSUM_CORRECT)
1208 * ice_nvm_write_activate
1209 * @hw: pointer to the HW struct
1210 * @cmd_flags: flags for write activate command
1211 * @response_flags: response indicators from firmware
1213 * Update the control word with the required banks' validity bits
1214 * and dumps the Shadow RAM to flash (0x0707)
1216 * cmd_flags controls which banks to activate, the preservation level to use
1217 * when activating the NVM bank, and whether an EMP reset is required for
1220 * Note that the 16bit cmd_flags value is split between two separate 1 byte
1221 * flag values in the descriptor.
1223 * On successful return of the firmware command, the response_flags variable
1224 * is updated with the flags reported by firmware indicating certain status,
1225 * such as whether EMP reset is enabled.
1227 int ice_nvm_write_activate(struct ice_hw *hw, u16 cmd_flags, u8 *response_flags)
1229 struct ice_aqc_nvm *cmd;
1230 struct ice_aq_desc desc;
1233 cmd = &desc.params.nvm;
1234 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_write_activate);
1236 cmd->cmd_flags = (u8)(cmd_flags & 0xFF);
1237 cmd->offset_high = (u8)((cmd_flags >> 8) & 0xFF);
1239 err = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1240 if (!err && response_flags)
1241 *response_flags = cmd->cmd_flags;
1247 * ice_aq_nvm_update_empr
1248 * @hw: pointer to the HW struct
1250 * Update empr (0x0709). This command allows SW to
1251 * request an EMPR to activate new FW.
1253 int ice_aq_nvm_update_empr(struct ice_hw *hw)
1255 struct ice_aq_desc desc;
1257 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_update_empr);
1259 return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1262 /* ice_nvm_set_pkg_data
1263 * @hw: pointer to the HW struct
1264 * @del_pkg_data_flag: If is set then the current pkg_data store by FW
1266 * If bit is set to 1, then buffer should be size 0.
1267 * @data: pointer to buffer
1268 * @length: length of the buffer
1269 * @cd: pointer to command details structure or NULL
1271 * Set package data (0x070A). This command is equivalent to the reception
1272 * of a PLDM FW Update GetPackageData cmd. This command should be sent
1273 * as part of the NVM update as the first cmd in the flow.
1277 ice_nvm_set_pkg_data(struct ice_hw *hw, bool del_pkg_data_flag, u8 *data,
1278 u16 length, struct ice_sq_cd *cd)
1280 struct ice_aqc_nvm_pkg_data *cmd;
1281 struct ice_aq_desc desc;
1283 if (length != 0 && !data)
1286 cmd = &desc.params.pkg_data;
1288 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_pkg_data);
1289 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1291 if (del_pkg_data_flag)
1292 cmd->cmd_flags |= ICE_AQC_NVM_PKG_DELETE;
1294 return ice_aq_send_cmd(hw, &desc, data, length, cd);
1297 /* ice_nvm_pass_component_tbl
1298 * @hw: pointer to the HW struct
1299 * @data: pointer to buffer
1300 * @length: length of the buffer
1301 * @transfer_flag: parameter for determining stage of the update
1302 * @comp_response: a pointer to the response from the 0x070B AQC.
1303 * @comp_response_code: a pointer to the response code from the 0x070B AQC.
1304 * @cd: pointer to command details structure or NULL
1306 * Pass component table (0x070B). This command is equivalent to the reception
1307 * of a PLDM FW Update PassComponentTable cmd. This command should be sent once
1308 * per component. It can be only sent after Set Package Data cmd and before
1309 * actual update. FW will assume these commands are going to be sent until
1310 * the TransferFlag is set to End or StartAndEnd.
1314 ice_nvm_pass_component_tbl(struct ice_hw *hw, u8 *data, u16 length,
1315 u8 transfer_flag, u8 *comp_response,
1316 u8 *comp_response_code, struct ice_sq_cd *cd)
1318 struct ice_aqc_nvm_pass_comp_tbl *cmd;
1319 struct ice_aq_desc desc;
1322 if (!data || !comp_response || !comp_response_code)
1325 cmd = &desc.params.pass_comp_tbl;
1327 ice_fill_dflt_direct_cmd_desc(&desc,
1328 ice_aqc_opc_nvm_pass_component_tbl);
1329 desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1331 cmd->transfer_flag = transfer_flag;
1332 status = ice_aq_send_cmd(hw, &desc, data, length, cd);
1335 *comp_response = cmd->component_response;
1336 *comp_response_code = cmd->component_response_code;