1f31e4b6fSAnirudh Venkataramanan // SPDX-License-Identifier: GPL-2.0
2f31e4b6fSAnirudh Venkataramanan /* Copyright (c) 2018, Intel Corporation. */
3f31e4b6fSAnirudh Venkataramanan 
47d714ff1SJakub Kicinski #include <linux/vmalloc.h>
57d714ff1SJakub Kicinski 
6f31e4b6fSAnirudh Venkataramanan #include "ice_common.h"
7f31e4b6fSAnirudh Venkataramanan 
8f31e4b6fSAnirudh Venkataramanan /**
9f31e4b6fSAnirudh Venkataramanan  * ice_aq_read_nvm
10f9867df6SAnirudh Venkataramanan  * @hw: pointer to the HW struct
11f31e4b6fSAnirudh Venkataramanan  * @module_typeid: module pointer location in words from the NVM beginning
12f31e4b6fSAnirudh Venkataramanan  * @offset: byte offset from the module beginning
13f31e4b6fSAnirudh Venkataramanan  * @length: length of the section to be read (in bytes from the offset)
14f31e4b6fSAnirudh Venkataramanan  * @data: command buffer (size [bytes] = length)
15f31e4b6fSAnirudh Venkataramanan  * @last_command: tells if this is the last command in a series
16e9450990SJacob Keller  * @read_shadow_ram: tell if this is a shadow RAM read
17f31e4b6fSAnirudh Venkataramanan  * @cd: pointer to command details structure or NULL
18f31e4b6fSAnirudh Venkataramanan  *
19f31e4b6fSAnirudh Venkataramanan  * Read the NVM using the admin queue commands (0x0701)
20f31e4b6fSAnirudh Venkataramanan  */
215e24d598STony Nguyen static int
2243c89b16SAnirudh Venkataramanan ice_aq_read_nvm(struct ice_hw *hw, u16 module_typeid, u32 offset, u16 length,
23e9450990SJacob Keller 		void *data, bool last_command, bool read_shadow_ram,
24e9450990SJacob Keller 		struct ice_sq_cd *cd)
25f31e4b6fSAnirudh Venkataramanan {
26f31e4b6fSAnirudh Venkataramanan 	struct ice_aq_desc desc;
27f31e4b6fSAnirudh Venkataramanan 	struct ice_aqc_nvm *cmd;
28f31e4b6fSAnirudh Venkataramanan 
29f31e4b6fSAnirudh Venkataramanan 	cmd = &desc.params.nvm;
30f31e4b6fSAnirudh Venkataramanan 
3181f07491SJacob Keller 	if (offset > ICE_AQC_NVM_MAX_OFFSET)
32d54699e2STony Nguyen 		return -EINVAL;
33f31e4b6fSAnirudh Venkataramanan 
34f31e4b6fSAnirudh Venkataramanan 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_read);
35f31e4b6fSAnirudh Venkataramanan 
36e9450990SJacob Keller 	if (!read_shadow_ram && module_typeid == ICE_AQC_NVM_START_POINT)
37e9450990SJacob Keller 		cmd->cmd_flags |= ICE_AQC_NVM_FLASH_ONLY;
38e9450990SJacob Keller 
39f31e4b6fSAnirudh Venkataramanan 	/* If this is the last command in a series, set the proper flag. */
40f31e4b6fSAnirudh Venkataramanan 	if (last_command)
41f31e4b6fSAnirudh Venkataramanan 		cmd->cmd_flags |= ICE_AQC_NVM_LAST_CMD;
4243c89b16SAnirudh Venkataramanan 	cmd->module_typeid = cpu_to_le16(module_typeid);
4343c89b16SAnirudh Venkataramanan 	cmd->offset_low = cpu_to_le16(offset & 0xFFFF);
4443c89b16SAnirudh Venkataramanan 	cmd->offset_high = (offset >> 16) & 0xFF;
45f31e4b6fSAnirudh Venkataramanan 	cmd->length = cpu_to_le16(length);
46f31e4b6fSAnirudh Venkataramanan 
47f31e4b6fSAnirudh Venkataramanan 	return ice_aq_send_cmd(hw, &desc, data, length, cd);
48f31e4b6fSAnirudh Venkataramanan }
49f31e4b6fSAnirudh Venkataramanan 
50f31e4b6fSAnirudh Venkataramanan /**
51e9450990SJacob Keller  * ice_read_flat_nvm - Read portion of NVM by flat offset
52e9450990SJacob Keller  * @hw: pointer to the HW struct
53e9450990SJacob Keller  * @offset: offset from beginning of NVM
54e9450990SJacob Keller  * @length: (in) number of bytes to read; (out) number of bytes actually read
55e9450990SJacob Keller  * @data: buffer to return data in (sized to fit the specified length)
56e9450990SJacob Keller  * @read_shadow_ram: if true, read from shadow RAM instead of NVM
57e9450990SJacob Keller  *
58e9450990SJacob Keller  * Reads a portion of the NVM, as a flat memory space. This function correctly
59e9450990SJacob Keller  * breaks read requests across Shadow RAM sectors and ensures that no single
6032e6deb2SBruce Allan  * read request exceeds the maximum 4KB read for a single AdminQ command.
61e9450990SJacob Keller  *
62e9450990SJacob Keller  * Returns a status code on failure. Note that the data pointer may be
63e9450990SJacob Keller  * partially updated if some reads succeed before a failure.
64e9450990SJacob Keller  */
655e24d598STony Nguyen int
66e9450990SJacob Keller ice_read_flat_nvm(struct ice_hw *hw, u32 offset, u32 *length, u8 *data,
67e9450990SJacob Keller 		  bool read_shadow_ram)
68e9450990SJacob Keller {
69e9450990SJacob Keller 	u32 inlen = *length;
70e9450990SJacob Keller 	u32 bytes_read = 0;
71e9450990SJacob Keller 	bool last_cmd;
725518ac2aSTony Nguyen 	int status;
73e9450990SJacob Keller 
74e9450990SJacob Keller 	*length = 0;
75e9450990SJacob Keller 
76e9450990SJacob Keller 	/* Verify the length of the read if this is for the Shadow RAM */
779af368faSJacob Keller 	if (read_shadow_ram && ((offset + inlen) > (hw->flash.sr_words * 2u))) {
789228d8b2SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "NVM error: requested offset is beyond Shadow RAM limit\n");
79d54699e2STony Nguyen 		return -EINVAL;
80e9450990SJacob Keller 	}
81e9450990SJacob Keller 
82e9450990SJacob Keller 	do {
83e9450990SJacob Keller 		u32 read_size, sector_offset;
84e9450990SJacob Keller 
8532e6deb2SBruce Allan 		/* ice_aq_read_nvm cannot read more than 4KB at a time.
86e9450990SJacob Keller 		 * Additionally, a read from the Shadow RAM may not cross over
87e9450990SJacob Keller 		 * a sector boundary. Conveniently, the sector size is also
8832e6deb2SBruce Allan 		 * 4KB.
89e9450990SJacob Keller 		 */
90e9450990SJacob Keller 		sector_offset = offset % ICE_AQ_MAX_BUF_LEN;
91e9450990SJacob Keller 		read_size = min_t(u32, ICE_AQ_MAX_BUF_LEN - sector_offset,
92e9450990SJacob Keller 				  inlen - bytes_read);
93e9450990SJacob Keller 
94e9450990SJacob Keller 		last_cmd = !(bytes_read + read_size < inlen);
95e9450990SJacob Keller 
96e9450990SJacob Keller 		status = ice_aq_read_nvm(hw, ICE_AQC_NVM_START_POINT,
97e9450990SJacob Keller 					 offset, read_size,
98e9450990SJacob Keller 					 data + bytes_read, last_cmd,
99e9450990SJacob Keller 					 read_shadow_ram, NULL);
100e9450990SJacob Keller 		if (status)
101e9450990SJacob Keller 			break;
102e9450990SJacob Keller 
103e9450990SJacob Keller 		bytes_read += read_size;
104e9450990SJacob Keller 		offset += read_size;
105e9450990SJacob Keller 	} while (!last_cmd);
106e9450990SJacob Keller 
107e9450990SJacob Keller 	*length = bytes_read;
108e9450990SJacob Keller 	return status;
109e9450990SJacob Keller }
110e9450990SJacob Keller 
111e9450990SJacob Keller /**
112544cd2acSCudzilo, Szymon T  * ice_aq_update_nvm
113544cd2acSCudzilo, Szymon T  * @hw: pointer to the HW struct
114544cd2acSCudzilo, Szymon T  * @module_typeid: module pointer location in words from the NVM beginning
115544cd2acSCudzilo, Szymon T  * @offset: byte offset from the module beginning
116544cd2acSCudzilo, Szymon T  * @length: length of the section to be written (in bytes from the offset)
117544cd2acSCudzilo, Szymon T  * @data: command buffer (size [bytes] = length)
118544cd2acSCudzilo, Szymon T  * @last_command: tells if this is the last command in a series
119544cd2acSCudzilo, Szymon T  * @command_flags: command parameters
120544cd2acSCudzilo, Szymon T  * @cd: pointer to command details structure or NULL
121544cd2acSCudzilo, Szymon T  *
122544cd2acSCudzilo, Szymon T  * Update the NVM using the admin queue commands (0x0703)
123544cd2acSCudzilo, Szymon T  */
1245e24d598STony Nguyen int
125544cd2acSCudzilo, Szymon T ice_aq_update_nvm(struct ice_hw *hw, u16 module_typeid, u32 offset,
126544cd2acSCudzilo, Szymon T 		  u16 length, void *data, bool last_command, u8 command_flags,
127544cd2acSCudzilo, Szymon T 		  struct ice_sq_cd *cd)
128544cd2acSCudzilo, Szymon T {
129544cd2acSCudzilo, Szymon T 	struct ice_aq_desc desc;
130544cd2acSCudzilo, Szymon T 	struct ice_aqc_nvm *cmd;
131544cd2acSCudzilo, Szymon T 
132544cd2acSCudzilo, Szymon T 	cmd = &desc.params.nvm;
133544cd2acSCudzilo, Szymon T 
134544cd2acSCudzilo, Szymon T 	/* In offset the highest byte must be zeroed. */
135544cd2acSCudzilo, Szymon T 	if (offset & 0xFF000000)
136d54699e2STony Nguyen 		return -EINVAL;
137544cd2acSCudzilo, Szymon T 
138544cd2acSCudzilo, Szymon T 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_write);
139544cd2acSCudzilo, Szymon T 
140544cd2acSCudzilo, Szymon T 	cmd->cmd_flags |= command_flags;
141544cd2acSCudzilo, Szymon T 
142544cd2acSCudzilo, Szymon T 	/* If this is the last command in a series, set the proper flag. */
143544cd2acSCudzilo, Szymon T 	if (last_command)
144544cd2acSCudzilo, Szymon T 		cmd->cmd_flags |= ICE_AQC_NVM_LAST_CMD;
145544cd2acSCudzilo, Szymon T 	cmd->module_typeid = cpu_to_le16(module_typeid);
146544cd2acSCudzilo, Szymon T 	cmd->offset_low = cpu_to_le16(offset & 0xFFFF);
147544cd2acSCudzilo, Szymon T 	cmd->offset_high = (offset >> 16) & 0xFF;
148544cd2acSCudzilo, Szymon T 	cmd->length = cpu_to_le16(length);
149544cd2acSCudzilo, Szymon T 
150544cd2acSCudzilo, Szymon T 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
151544cd2acSCudzilo, Szymon T 
152544cd2acSCudzilo, Szymon T 	return ice_aq_send_cmd(hw, &desc, data, length, cd);
153544cd2acSCudzilo, Szymon T }
154544cd2acSCudzilo, Szymon T 
155544cd2acSCudzilo, Szymon T /**
156544cd2acSCudzilo, Szymon T  * ice_aq_erase_nvm
157544cd2acSCudzilo, Szymon T  * @hw: pointer to the HW struct
158544cd2acSCudzilo, Szymon T  * @module_typeid: module pointer location in words from the NVM beginning
159544cd2acSCudzilo, Szymon T  * @cd: pointer to command details structure or NULL
160544cd2acSCudzilo, Szymon T  *
161544cd2acSCudzilo, Szymon T  * Erase the NVM sector using the admin queue commands (0x0702)
162544cd2acSCudzilo, Szymon T  */
1635518ac2aSTony Nguyen int ice_aq_erase_nvm(struct ice_hw *hw, u16 module_typeid, struct ice_sq_cd *cd)
164544cd2acSCudzilo, Szymon T {
165544cd2acSCudzilo, Szymon T 	struct ice_aq_desc desc;
166544cd2acSCudzilo, Szymon T 	struct ice_aqc_nvm *cmd;
167544cd2acSCudzilo, Szymon T 
168544cd2acSCudzilo, Szymon T 	cmd = &desc.params.nvm;
169544cd2acSCudzilo, Szymon T 
170544cd2acSCudzilo, Szymon T 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_erase);
171544cd2acSCudzilo, Szymon T 
172544cd2acSCudzilo, Szymon T 	cmd->module_typeid = cpu_to_le16(module_typeid);
173544cd2acSCudzilo, Szymon T 	cmd->length = cpu_to_le16(ICE_AQC_NVM_ERASE_LEN);
174544cd2acSCudzilo, Szymon T 	cmd->offset_low = 0;
175544cd2acSCudzilo, Szymon T 	cmd->offset_high = 0;
176544cd2acSCudzilo, Szymon T 
177544cd2acSCudzilo, Szymon T 	return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
178544cd2acSCudzilo, Szymon T }
179544cd2acSCudzilo, Szymon T 
180544cd2acSCudzilo, Szymon T /**
181f31e4b6fSAnirudh Venkataramanan  * ice_read_sr_word_aq - Reads Shadow RAM via AQ
182f31e4b6fSAnirudh Venkataramanan  * @hw: pointer to the HW structure
183f31e4b6fSAnirudh Venkataramanan  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
184f31e4b6fSAnirudh Venkataramanan  * @data: word read from the Shadow RAM
185f31e4b6fSAnirudh Venkataramanan  *
186e9450990SJacob Keller  * Reads one 16 bit word from the Shadow RAM using ice_read_flat_nvm.
187f31e4b6fSAnirudh Venkataramanan  */
1885518ac2aSTony Nguyen static int ice_read_sr_word_aq(struct ice_hw *hw, u16 offset, u16 *data)
189f31e4b6fSAnirudh Venkataramanan {
190e9450990SJacob Keller 	u32 bytes = sizeof(u16);
1912efefb56SJacob Keller 	__le16 data_local;
1925518ac2aSTony Nguyen 	int status;
193f31e4b6fSAnirudh Venkataramanan 
194e9450990SJacob Keller 	/* Note that ice_read_flat_nvm takes into account the 4Kb AdminQ and
195e9450990SJacob Keller 	 * Shadow RAM sector restrictions necessary when reading from the NVM.
196e9450990SJacob Keller 	 */
197e9450990SJacob Keller 	status = ice_read_flat_nvm(hw, offset * sizeof(u16), &bytes,
19834d8461aSBruce Allan 				   (__force u8 *)&data_local, true);
199e9450990SJacob Keller 	if (status)
200f31e4b6fSAnirudh Venkataramanan 		return status;
201e9450990SJacob Keller 
202e9450990SJacob Keller 	*data = le16_to_cpu(data_local);
203e9450990SJacob Keller 	return 0;
204f31e4b6fSAnirudh Venkataramanan }
205f31e4b6fSAnirudh Venkataramanan 
206f31e4b6fSAnirudh Venkataramanan /**
207f31e4b6fSAnirudh Venkataramanan  * ice_acquire_nvm - Generic request for acquiring the NVM ownership
208f31e4b6fSAnirudh Venkataramanan  * @hw: pointer to the HW structure
209f31e4b6fSAnirudh Venkataramanan  * @access: NVM access type (read or write)
210f31e4b6fSAnirudh Venkataramanan  *
211f31e4b6fSAnirudh Venkataramanan  * This function will request NVM ownership.
212f31e4b6fSAnirudh Venkataramanan  */
2135518ac2aSTony Nguyen int ice_acquire_nvm(struct ice_hw *hw, enum ice_aq_res_access_type access)
214f31e4b6fSAnirudh Venkataramanan {
2159af368faSJacob Keller 	if (hw->flash.blank_nvm_mode)
216f31e4b6fSAnirudh Venkataramanan 		return 0;
217f31e4b6fSAnirudh Venkataramanan 
218ff2b1321SDan Nowlin 	return ice_acquire_res(hw, ICE_NVM_RES_ID, access, ICE_NVM_TIMEOUT);
219f31e4b6fSAnirudh Venkataramanan }
220f31e4b6fSAnirudh Venkataramanan 
221f31e4b6fSAnirudh Venkataramanan /**
222f31e4b6fSAnirudh Venkataramanan  * ice_release_nvm - Generic request for releasing the NVM ownership
223f31e4b6fSAnirudh Venkataramanan  * @hw: pointer to the HW structure
224f31e4b6fSAnirudh Venkataramanan  *
225f31e4b6fSAnirudh Venkataramanan  * This function will release NVM ownership.
226f31e4b6fSAnirudh Venkataramanan  */
22784a24798SJesse Brandeburg void ice_release_nvm(struct ice_hw *hw)
228f31e4b6fSAnirudh Venkataramanan {
2299af368faSJacob Keller 	if (hw->flash.blank_nvm_mode)
230f31e4b6fSAnirudh Venkataramanan 		return;
231f31e4b6fSAnirudh Venkataramanan 
232f31e4b6fSAnirudh Venkataramanan 	ice_release_res(hw, ICE_NVM_RES_ID);
233f31e4b6fSAnirudh Venkataramanan }
234f31e4b6fSAnirudh Venkataramanan 
235f31e4b6fSAnirudh Venkataramanan /**
2360ce50c70SJacob Keller  * ice_get_flash_bank_offset - Get offset into requested flash bank
2370ce50c70SJacob Keller  * @hw: pointer to the HW structure
2380ce50c70SJacob Keller  * @bank: whether to read from the active or inactive flash bank
2390ce50c70SJacob Keller  * @module: the module to read from
2400ce50c70SJacob Keller  *
2410ce50c70SJacob Keller  * Based on the module, lookup the module offset from the beginning of the
2420ce50c70SJacob Keller  * flash.
2430ce50c70SJacob Keller  *
2440ce50c70SJacob Keller  * Returns the flash offset. Note that a value of zero is invalid and must be
2450ce50c70SJacob Keller  * treated as an error.
2460ce50c70SJacob Keller  */
2470ce50c70SJacob Keller static u32 ice_get_flash_bank_offset(struct ice_hw *hw, enum ice_bank_select bank, u16 module)
2480ce50c70SJacob Keller {
2490ce50c70SJacob Keller 	struct ice_bank_info *banks = &hw->flash.banks;
2500ce50c70SJacob Keller 	enum ice_flash_bank active_bank;
2510ce50c70SJacob Keller 	bool second_bank_active;
2520ce50c70SJacob Keller 	u32 offset, size;
2530ce50c70SJacob Keller 
2540ce50c70SJacob Keller 	switch (module) {
2550ce50c70SJacob Keller 	case ICE_SR_1ST_NVM_BANK_PTR:
2560ce50c70SJacob Keller 		offset = banks->nvm_ptr;
2570ce50c70SJacob Keller 		size = banks->nvm_size;
2580ce50c70SJacob Keller 		active_bank = banks->nvm_bank;
2590ce50c70SJacob Keller 		break;
2600ce50c70SJacob Keller 	case ICE_SR_1ST_OROM_BANK_PTR:
2610ce50c70SJacob Keller 		offset = banks->orom_ptr;
2620ce50c70SJacob Keller 		size = banks->orom_size;
2630ce50c70SJacob Keller 		active_bank = banks->orom_bank;
2640ce50c70SJacob Keller 		break;
2650ce50c70SJacob Keller 	case ICE_SR_NETLIST_BANK_PTR:
2660ce50c70SJacob Keller 		offset = banks->netlist_ptr;
2670ce50c70SJacob Keller 		size = banks->netlist_size;
2680ce50c70SJacob Keller 		active_bank = banks->netlist_bank;
2690ce50c70SJacob Keller 		break;
2700ce50c70SJacob Keller 	default:
2710ce50c70SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Unexpected value for flash module: 0x%04x\n", module);
2720ce50c70SJacob Keller 		return 0;
2730ce50c70SJacob Keller 	}
2740ce50c70SJacob Keller 
2750ce50c70SJacob Keller 	switch (active_bank) {
2760ce50c70SJacob Keller 	case ICE_1ST_FLASH_BANK:
2770ce50c70SJacob Keller 		second_bank_active = false;
2780ce50c70SJacob Keller 		break;
2790ce50c70SJacob Keller 	case ICE_2ND_FLASH_BANK:
2800ce50c70SJacob Keller 		second_bank_active = true;
2810ce50c70SJacob Keller 		break;
2820ce50c70SJacob Keller 	default:
2830ce50c70SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Unexpected value for active flash bank: %u\n",
2840ce50c70SJacob Keller 			  active_bank);
2850ce50c70SJacob Keller 		return 0;
2860ce50c70SJacob Keller 	}
2870ce50c70SJacob Keller 
2880ce50c70SJacob Keller 	/* The second flash bank is stored immediately following the first
2890ce50c70SJacob Keller 	 * bank. Based on whether the 1st or 2nd bank is active, and whether
2900ce50c70SJacob Keller 	 * we want the active or inactive bank, calculate the desired offset.
2910ce50c70SJacob Keller 	 */
2920ce50c70SJacob Keller 	switch (bank) {
2930ce50c70SJacob Keller 	case ICE_ACTIVE_FLASH_BANK:
2940ce50c70SJacob Keller 		return offset + (second_bank_active ? size : 0);
2950ce50c70SJacob Keller 	case ICE_INACTIVE_FLASH_BANK:
2960ce50c70SJacob Keller 		return offset + (second_bank_active ? 0 : size);
2970ce50c70SJacob Keller 	}
2980ce50c70SJacob Keller 
2990ce50c70SJacob Keller 	ice_debug(hw, ICE_DBG_NVM, "Unexpected value for flash bank selection: %u\n", bank);
3000ce50c70SJacob Keller 	return 0;
3010ce50c70SJacob Keller }
3020ce50c70SJacob Keller 
3030ce50c70SJacob Keller /**
3040ce50c70SJacob Keller  * ice_read_flash_module - Read a word from one of the main NVM modules
3050ce50c70SJacob Keller  * @hw: pointer to the HW structure
3060ce50c70SJacob Keller  * @bank: which bank of the module to read
3070ce50c70SJacob Keller  * @module: the module to read
3080ce50c70SJacob Keller  * @offset: the offset into the module in bytes
3090ce50c70SJacob Keller  * @data: storage for the word read from the flash
3100ce50c70SJacob Keller  * @length: bytes of data to read
3110ce50c70SJacob Keller  *
3120ce50c70SJacob Keller  * Read data from the specified flash module. The bank parameter indicates
3130ce50c70SJacob Keller  * whether or not to read from the active bank or the inactive bank of that
3140ce50c70SJacob Keller  * module.
3150ce50c70SJacob Keller  *
3160ce50c70SJacob Keller  * The word will be read using flat NVM access, and relies on the
3170ce50c70SJacob Keller  * hw->flash.banks data being setup by ice_determine_active_flash_banks()
3180ce50c70SJacob Keller  * during initialization.
3190ce50c70SJacob Keller  */
3205e24d598STony Nguyen static int
3210ce50c70SJacob Keller ice_read_flash_module(struct ice_hw *hw, enum ice_bank_select bank, u16 module,
3220ce50c70SJacob Keller 		      u32 offset, u8 *data, u32 length)
3230ce50c70SJacob Keller {
3245e24d598STony Nguyen 	int status;
3250ce50c70SJacob Keller 	u32 start;
3260ce50c70SJacob Keller 
3270ce50c70SJacob Keller 	start = ice_get_flash_bank_offset(hw, bank, module);
3280ce50c70SJacob Keller 	if (!start) {
3290ce50c70SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Unable to calculate flash bank offset for module 0x%04x\n",
3300ce50c70SJacob Keller 			  module);
331d54699e2STony Nguyen 		return -EINVAL;
3320ce50c70SJacob Keller 	}
3330ce50c70SJacob Keller 
3340ce50c70SJacob Keller 	status = ice_acquire_nvm(hw, ICE_RES_READ);
3350ce50c70SJacob Keller 	if (status)
3360ce50c70SJacob Keller 		return status;
3370ce50c70SJacob Keller 
3380ce50c70SJacob Keller 	status = ice_read_flat_nvm(hw, start + offset, &length, data, false);
3390ce50c70SJacob Keller 
3400ce50c70SJacob Keller 	ice_release_nvm(hw);
3410ce50c70SJacob Keller 
3420ce50c70SJacob Keller 	return status;
3430ce50c70SJacob Keller }
3440ce50c70SJacob Keller 
3450ce50c70SJacob Keller /**
3460ce50c70SJacob Keller  * ice_read_nvm_module - Read from the active main NVM module
3470ce50c70SJacob Keller  * @hw: pointer to the HW structure
3480ce50c70SJacob Keller  * @bank: whether to read from active or inactive NVM module
3490ce50c70SJacob Keller  * @offset: offset into the NVM module to read, in words
3500ce50c70SJacob Keller  * @data: storage for returned word value
3510ce50c70SJacob Keller  *
3520ce50c70SJacob Keller  * Read the specified word from the active NVM module. This includes the CSS
3530ce50c70SJacob Keller  * header at the start of the NVM module.
3540ce50c70SJacob Keller  */
3555e24d598STony Nguyen static int
3560ce50c70SJacob Keller ice_read_nvm_module(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
3570ce50c70SJacob Keller {
3580ce50c70SJacob Keller 	__le16 data_local;
3595518ac2aSTony Nguyen 	int status;
3600ce50c70SJacob Keller 
3610ce50c70SJacob Keller 	status = ice_read_flash_module(hw, bank, ICE_SR_1ST_NVM_BANK_PTR, offset * sizeof(u16),
3620ce50c70SJacob Keller 				       (__force u8 *)&data_local, sizeof(u16));
3630ce50c70SJacob Keller 	if (!status)
3640ce50c70SJacob Keller 		*data = le16_to_cpu(data_local);
3650ce50c70SJacob Keller 
3660ce50c70SJacob Keller 	return status;
3670ce50c70SJacob Keller }
3680ce50c70SJacob Keller 
3690ce50c70SJacob Keller /**
3700ce50c70SJacob Keller  * ice_read_nvm_sr_copy - Read a word from the Shadow RAM copy in the NVM bank
3710ce50c70SJacob Keller  * @hw: pointer to the HW structure
3720ce50c70SJacob Keller  * @bank: whether to read from the active or inactive NVM module
3730ce50c70SJacob Keller  * @offset: offset into the Shadow RAM copy to read, in words
3740ce50c70SJacob Keller  * @data: storage for returned word value
3750ce50c70SJacob Keller  *
3760ce50c70SJacob Keller  * Read the specified word from the copy of the Shadow RAM found in the
3770ce50c70SJacob Keller  * specified NVM module.
3780ce50c70SJacob Keller  */
3795e24d598STony Nguyen static int
3800ce50c70SJacob Keller ice_read_nvm_sr_copy(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
3810ce50c70SJacob Keller {
3820ce50c70SJacob Keller 	return ice_read_nvm_module(hw, bank, ICE_NVM_SR_COPY_WORD_OFFSET + offset, data);
3830ce50c70SJacob Keller }
3840ce50c70SJacob Keller 
3850ce50c70SJacob Keller /**
386e120a9abSJacob Keller  * ice_read_netlist_module - Read data from the netlist module area
387e120a9abSJacob Keller  * @hw: pointer to the HW structure
388e120a9abSJacob Keller  * @bank: whether to read from the active or inactive module
389e120a9abSJacob Keller  * @offset: offset into the netlist to read from
390e120a9abSJacob Keller  * @data: storage for returned word value
391e120a9abSJacob Keller  *
392e120a9abSJacob Keller  * Read a word from the specified netlist bank.
393e120a9abSJacob Keller  */
3945e24d598STony Nguyen static int
395e120a9abSJacob Keller ice_read_netlist_module(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
396e120a9abSJacob Keller {
397e120a9abSJacob Keller 	__le16 data_local;
3985518ac2aSTony Nguyen 	int status;
399e120a9abSJacob Keller 
400e120a9abSJacob Keller 	status = ice_read_flash_module(hw, bank, ICE_SR_NETLIST_BANK_PTR, offset * sizeof(u16),
401e120a9abSJacob Keller 				       (__force u8 *)&data_local, sizeof(u16));
402e120a9abSJacob Keller 	if (!status)
403e120a9abSJacob Keller 		*data = le16_to_cpu(data_local);
404e120a9abSJacob Keller 
405e120a9abSJacob Keller 	return status;
406e120a9abSJacob Keller }
407e120a9abSJacob Keller 
408e120a9abSJacob Keller /**
409f31e4b6fSAnirudh Venkataramanan  * ice_read_sr_word - Reads Shadow RAM word and acquire NVM if necessary
410f31e4b6fSAnirudh Venkataramanan  * @hw: pointer to the HW structure
411f31e4b6fSAnirudh Venkataramanan  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
412f31e4b6fSAnirudh Venkataramanan  * @data: word read from the Shadow RAM
413f31e4b6fSAnirudh Venkataramanan  *
414f31e4b6fSAnirudh Venkataramanan  * Reads one 16 bit word from the Shadow RAM using the ice_read_sr_word_aq.
415f31e4b6fSAnirudh Venkataramanan  */
4165e24d598STony Nguyen int ice_read_sr_word(struct ice_hw *hw, u16 offset, u16 *data)
417f31e4b6fSAnirudh Venkataramanan {
4185e24d598STony Nguyen 	int status;
419f31e4b6fSAnirudh Venkataramanan 
420f31e4b6fSAnirudh Venkataramanan 	status = ice_acquire_nvm(hw, ICE_RES_READ);
421f31e4b6fSAnirudh Venkataramanan 	if (!status) {
422f31e4b6fSAnirudh Venkataramanan 		status = ice_read_sr_word_aq(hw, offset, data);
423f31e4b6fSAnirudh Venkataramanan 		ice_release_nvm(hw);
424f31e4b6fSAnirudh Venkataramanan 	}
425f31e4b6fSAnirudh Venkataramanan 
426f31e4b6fSAnirudh Venkataramanan 	return status;
427f31e4b6fSAnirudh Venkataramanan }
428f31e4b6fSAnirudh Venkataramanan 
429f31e4b6fSAnirudh Venkataramanan /**
430e961b679SJacob Keller  * ice_get_pfa_module_tlv - Reads sub module TLV from NVM PFA
431e961b679SJacob Keller  * @hw: pointer to hardware structure
432e961b679SJacob Keller  * @module_tlv: pointer to module TLV to return
433e961b679SJacob Keller  * @module_tlv_len: pointer to module TLV length to return
434e961b679SJacob Keller  * @module_type: module type requested
435e961b679SJacob Keller  *
436e961b679SJacob Keller  * Finds the requested sub module TLV type from the Preserved Field
437e961b679SJacob Keller  * Area (PFA) and returns the TLV pointer and length. The caller can
438e961b679SJacob Keller  * use these to read the variable length TLV value.
439e961b679SJacob Keller  */
4405e24d598STony Nguyen int
441e961b679SJacob Keller ice_get_pfa_module_tlv(struct ice_hw *hw, u16 *module_tlv, u16 *module_tlv_len,
442e961b679SJacob Keller 		       u16 module_type)
443e961b679SJacob Keller {
444e961b679SJacob Keller 	u16 pfa_len, pfa_ptr;
445e961b679SJacob Keller 	u16 next_tlv;
4465518ac2aSTony Nguyen 	int status;
447e961b679SJacob Keller 
448e961b679SJacob Keller 	status = ice_read_sr_word(hw, ICE_SR_PFA_PTR, &pfa_ptr);
449e961b679SJacob Keller 	if (status) {
450e961b679SJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Preserved Field Array pointer.\n");
451e961b679SJacob Keller 		return status;
452e961b679SJacob Keller 	}
453e961b679SJacob Keller 	status = ice_read_sr_word(hw, pfa_ptr, &pfa_len);
454e961b679SJacob Keller 	if (status) {
455e961b679SJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PFA length.\n");
456e961b679SJacob Keller 		return status;
457e961b679SJacob Keller 	}
458e961b679SJacob Keller 	/* Starting with first TLV after PFA length, iterate through the list
459e961b679SJacob Keller 	 * of TLVs to find the requested one.
460e961b679SJacob Keller 	 */
461e961b679SJacob Keller 	next_tlv = pfa_ptr + 1;
462e961b679SJacob Keller 	while (next_tlv < pfa_ptr + pfa_len) {
463e961b679SJacob Keller 		u16 tlv_sub_module_type;
464e961b679SJacob Keller 		u16 tlv_len;
465e961b679SJacob Keller 
466e961b679SJacob Keller 		/* Read TLV type */
467e961b679SJacob Keller 		status = ice_read_sr_word(hw, next_tlv, &tlv_sub_module_type);
468e961b679SJacob Keller 		if (status) {
469e961b679SJacob Keller 			ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV type.\n");
470e961b679SJacob Keller 			break;
471e961b679SJacob Keller 		}
472e961b679SJacob Keller 		/* Read TLV length */
473e961b679SJacob Keller 		status = ice_read_sr_word(hw, next_tlv + 1, &tlv_len);
474e961b679SJacob Keller 		if (status) {
475e961b679SJacob Keller 			ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV length.\n");
476e961b679SJacob Keller 			break;
477e961b679SJacob Keller 		}
478e961b679SJacob Keller 		if (tlv_sub_module_type == module_type) {
479e961b679SJacob Keller 			if (tlv_len) {
480e961b679SJacob Keller 				*module_tlv = next_tlv;
481e961b679SJacob Keller 				*module_tlv_len = tlv_len;
482e961b679SJacob Keller 				return 0;
483e961b679SJacob Keller 			}
484d54699e2STony Nguyen 			return -EINVAL;
485e961b679SJacob Keller 		}
486e961b679SJacob Keller 		/* Check next TLV, i.e. current TLV pointer + length + 2 words
487e961b679SJacob Keller 		 * (for current TLV's type and length)
488e961b679SJacob Keller 		 */
489e961b679SJacob Keller 		next_tlv = next_tlv + tlv_len + 2;
490e961b679SJacob Keller 	}
491e961b679SJacob Keller 	/* Module does not exist */
492d54699e2STony Nguyen 	return -ENOENT;
493e961b679SJacob Keller }
494e961b679SJacob Keller 
495e961b679SJacob Keller /**
496e961b679SJacob Keller  * ice_read_pba_string - Reads part number string from NVM
497e961b679SJacob Keller  * @hw: pointer to hardware structure
498e961b679SJacob Keller  * @pba_num: stores the part number string from the NVM
499e961b679SJacob Keller  * @pba_num_size: part number string buffer length
500e961b679SJacob Keller  *
501e961b679SJacob Keller  * Reads the part number string from the NVM.
502e961b679SJacob Keller  */
5035518ac2aSTony Nguyen int ice_read_pba_string(struct ice_hw *hw, u8 *pba_num, u32 pba_num_size)
504e961b679SJacob Keller {
505e961b679SJacob Keller 	u16 pba_tlv, pba_tlv_len;
506e961b679SJacob Keller 	u16 pba_word, pba_size;
5075518ac2aSTony Nguyen 	int status;
508e961b679SJacob Keller 	u16 i;
509e961b679SJacob Keller 
510e961b679SJacob Keller 	status = ice_get_pfa_module_tlv(hw, &pba_tlv, &pba_tlv_len,
511e961b679SJacob Keller 					ICE_SR_PBA_BLOCK_PTR);
512e961b679SJacob Keller 	if (status) {
513e961b679SJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block TLV.\n");
514e961b679SJacob Keller 		return status;
515e961b679SJacob Keller 	}
516e961b679SJacob Keller 
517e961b679SJacob Keller 	/* pba_size is the next word */
518e961b679SJacob Keller 	status = ice_read_sr_word(hw, (pba_tlv + 2), &pba_size);
519e961b679SJacob Keller 	if (status) {
520e961b679SJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Section size.\n");
521e961b679SJacob Keller 		return status;
522e961b679SJacob Keller 	}
523e961b679SJacob Keller 
524e961b679SJacob Keller 	if (pba_tlv_len < pba_size) {
525e961b679SJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Invalid PBA Block TLV size.\n");
526d54699e2STony Nguyen 		return -EINVAL;
527e961b679SJacob Keller 	}
528e961b679SJacob Keller 
529e961b679SJacob Keller 	/* Subtract one to get PBA word count (PBA Size word is included in
530e961b679SJacob Keller 	 * total size)
531e961b679SJacob Keller 	 */
532e961b679SJacob Keller 	pba_size--;
533e961b679SJacob Keller 	if (pba_num_size < (((u32)pba_size * 2) + 1)) {
534e961b679SJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Buffer too small for PBA data.\n");
535d54699e2STony Nguyen 		return -EINVAL;
536e961b679SJacob Keller 	}
537e961b679SJacob Keller 
538e961b679SJacob Keller 	for (i = 0; i < pba_size; i++) {
539e961b679SJacob Keller 		status = ice_read_sr_word(hw, (pba_tlv + 2 + 1) + i, &pba_word);
540e961b679SJacob Keller 		if (status) {
541e961b679SJacob Keller 			ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block word %d.\n", i);
542e961b679SJacob Keller 			return status;
543e961b679SJacob Keller 		}
544e961b679SJacob Keller 
545e961b679SJacob Keller 		pba_num[(i * 2)] = (pba_word >> 8) & 0xFF;
546e961b679SJacob Keller 		pba_num[(i * 2) + 1] = pba_word & 0xFF;
547e961b679SJacob Keller 	}
548e961b679SJacob Keller 	pba_num[(pba_size * 2)] = '\0';
549e961b679SJacob Keller 
550e961b679SJacob Keller 	return status;
551e961b679SJacob Keller }
552e961b679SJacob Keller 
553e961b679SJacob Keller /**
5549af368faSJacob Keller  * ice_get_nvm_ver_info - Read NVM version information
5559af368faSJacob Keller  * @hw: pointer to the HW struct
5560ce50c70SJacob Keller  * @bank: whether to read from the active or inactive flash bank
5579af368faSJacob Keller  * @nvm: pointer to NVM info structure
5589af368faSJacob Keller  *
5599af368faSJacob Keller  * Read the NVM EETRACK ID and map version of the main NVM image bank, filling
5609af368faSJacob Keller  * in the NVM info structure.
5619af368faSJacob Keller  */
5625e24d598STony Nguyen static int
5630ce50c70SJacob Keller ice_get_nvm_ver_info(struct ice_hw *hw, enum ice_bank_select bank, struct ice_nvm_info *nvm)
5649af368faSJacob Keller {
5659af368faSJacob Keller 	u16 eetrack_lo, eetrack_hi, ver;
5665e24d598STony Nguyen 	int status;
5679af368faSJacob Keller 
5680ce50c70SJacob Keller 	status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_DEV_STARTER_VER, &ver);
5699af368faSJacob Keller 	if (status) {
5709af368faSJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read DEV starter version.\n");
5719af368faSJacob Keller 		return status;
5729af368faSJacob Keller 	}
5730ce50c70SJacob Keller 
5749af368faSJacob Keller 	nvm->major = (ver & ICE_NVM_VER_HI_MASK) >> ICE_NVM_VER_HI_SHIFT;
5759af368faSJacob Keller 	nvm->minor = (ver & ICE_NVM_VER_LO_MASK) >> ICE_NVM_VER_LO_SHIFT;
5769af368faSJacob Keller 
5770ce50c70SJacob Keller 	status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_EETRACK_LO, &eetrack_lo);
5789af368faSJacob Keller 	if (status) {
5799af368faSJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read EETRACK lo.\n");
5809af368faSJacob Keller 		return status;
5819af368faSJacob Keller 	}
5820ce50c70SJacob Keller 	status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_EETRACK_HI, &eetrack_hi);
5839af368faSJacob Keller 	if (status) {
5849af368faSJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read EETRACK hi.\n");
5859af368faSJacob Keller 		return status;
5869af368faSJacob Keller 	}
5879af368faSJacob Keller 
5889af368faSJacob Keller 	nvm->eetrack = (eetrack_hi << 16) | eetrack_lo;
5899af368faSJacob Keller 
5909af368faSJacob Keller 	return 0;
5919af368faSJacob Keller }
5929af368faSJacob Keller 
5939af368faSJacob Keller /**
5942c4fe41dSJacob Keller  * ice_get_inactive_nvm_ver - Read Option ROM version from the inactive bank
5952c4fe41dSJacob Keller  * @hw: pointer to the HW structure
5962c4fe41dSJacob Keller  * @nvm: storage for Option ROM version information
5972c4fe41dSJacob Keller  *
5982c4fe41dSJacob Keller  * Reads the NVM EETRACK ID, Map version, and security revision of the
5992c4fe41dSJacob Keller  * inactive NVM bank. Used to access version data for a pending update that
6002c4fe41dSJacob Keller  * has not yet been activated.
6012c4fe41dSJacob Keller  */
6025e24d598STony Nguyen int ice_get_inactive_nvm_ver(struct ice_hw *hw, struct ice_nvm_info *nvm)
6032c4fe41dSJacob Keller {
6042c4fe41dSJacob Keller 	return ice_get_nvm_ver_info(hw, ICE_INACTIVE_FLASH_BANK, nvm);
6052c4fe41dSJacob Keller }
6062c4fe41dSJacob Keller 
6072c4fe41dSJacob Keller /**
608e67fbcfbSJacob Keller  * ice_get_orom_civd_data - Get the combo version information from Option ROM
609d4e87444SJacob Keller  * @hw: pointer to the HW struct
610e67fbcfbSJacob Keller  * @bank: whether to read from the active or inactive flash module
611e67fbcfbSJacob Keller  * @civd: storage for the Option ROM CIVD data.
612d4e87444SJacob Keller  *
613e67fbcfbSJacob Keller  * Searches through the Option ROM flash contents to locate the CIVD data for
614e67fbcfbSJacob Keller  * the image.
615d4e87444SJacob Keller  */
6165e24d598STony Nguyen static int
617e67fbcfbSJacob Keller ice_get_orom_civd_data(struct ice_hw *hw, enum ice_bank_select bank,
618e67fbcfbSJacob Keller 		       struct ice_orom_civd_info *civd)
619d4e87444SJacob Keller {
620af18d886SJacob Keller 	u8 *orom_data;
6215e24d598STony Nguyen 	int status;
622e67fbcfbSJacob Keller 	u32 offset;
623e67fbcfbSJacob Keller 
624e67fbcfbSJacob Keller 	/* The CIVD section is located in the Option ROM aligned to 512 bytes.
625e67fbcfbSJacob Keller 	 * The first 4 bytes must contain the ASCII characters "$CIV".
626e67fbcfbSJacob Keller 	 * A simple modulo 256 sum of all of the bytes of the structure must
627e67fbcfbSJacob Keller 	 * equal 0.
628af18d886SJacob Keller 	 *
629af18d886SJacob Keller 	 * The exact location is unknown and varies between images but is
630af18d886SJacob Keller 	 * usually somewhere in the middle of the bank. We need to scan the
631af18d886SJacob Keller 	 * Option ROM bank to locate it.
632af18d886SJacob Keller 	 *
633af18d886SJacob Keller 	 * It's significantly faster to read the entire Option ROM up front
634af18d886SJacob Keller 	 * using the maximum page size, than to read each possible location
635af18d886SJacob Keller 	 * with a separate firmware command.
636e67fbcfbSJacob Keller 	 */
637af18d886SJacob Keller 	orom_data = vzalloc(hw->flash.banks.orom_size);
638af18d886SJacob Keller 	if (!orom_data)
639af18d886SJacob Keller 		return -ENOMEM;
640e67fbcfbSJacob Keller 
641af18d886SJacob Keller 	status = ice_read_flash_module(hw, bank, ICE_SR_1ST_OROM_BANK_PTR, 0,
642af18d886SJacob Keller 				       orom_data, hw->flash.banks.orom_size);
643e67fbcfbSJacob Keller 	if (status) {
6447c8881b7SJianglei Nie 		vfree(orom_data);
645af18d886SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Unable to read Option ROM data\n");
646e67fbcfbSJacob Keller 		return status;
647e67fbcfbSJacob Keller 	}
648e67fbcfbSJacob Keller 
649af18d886SJacob Keller 	/* Scan the memory buffer to locate the CIVD data section */
650af18d886SJacob Keller 	for (offset = 0; (offset + 512) <= hw->flash.banks.orom_size; offset += 512) {
651af18d886SJacob Keller 		struct ice_orom_civd_info *tmp;
652af18d886SJacob Keller 		u8 sum = 0, i;
653af18d886SJacob Keller 
654af18d886SJacob Keller 		tmp = (struct ice_orom_civd_info *)&orom_data[offset];
655af18d886SJacob Keller 
656e67fbcfbSJacob Keller 		/* Skip forward until we find a matching signature */
657af18d886SJacob Keller 		if (memcmp("$CIV", tmp->signature, sizeof(tmp->signature)) != 0)
658e67fbcfbSJacob Keller 			continue;
659e67fbcfbSJacob Keller 
660af18d886SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Found CIVD section at offset %u\n",
661af18d886SJacob Keller 			  offset);
662af18d886SJacob Keller 
663e67fbcfbSJacob Keller 		/* Verify that the simple checksum is zero */
664af18d886SJacob Keller 		for (i = 0; i < sizeof(*tmp); i++)
665b370245bSBruce Allan 			/* cppcheck-suppress objectIndex */
666af18d886SJacob Keller 			sum += ((u8 *)tmp)[i];
667e67fbcfbSJacob Keller 
668e67fbcfbSJacob Keller 		if (sum) {
669e67fbcfbSJacob Keller 			ice_debug(hw, ICE_DBG_NVM, "Found CIVD data with invalid checksum of %u\n",
670e67fbcfbSJacob Keller 				  sum);
671af18d886SJacob Keller 			goto err_invalid_checksum;
672e67fbcfbSJacob Keller 		}
673e67fbcfbSJacob Keller 
674af18d886SJacob Keller 		*civd = *tmp;
675af18d886SJacob Keller 		vfree(orom_data);
676e67fbcfbSJacob Keller 		return 0;
677e67fbcfbSJacob Keller 	}
678e67fbcfbSJacob Keller 
679af18d886SJacob Keller 	ice_debug(hw, ICE_DBG_NVM, "Unable to locate CIVD data within the Option ROM\n");
680af18d886SJacob Keller 
681af18d886SJacob Keller err_invalid_checksum:
682af18d886SJacob Keller 	vfree(orom_data);
683d54699e2STony Nguyen 	return -EIO;
684e67fbcfbSJacob Keller }
685e67fbcfbSJacob Keller 
686e67fbcfbSJacob Keller /**
687e67fbcfbSJacob Keller  * ice_get_orom_ver_info - Read Option ROM version information
688e67fbcfbSJacob Keller  * @hw: pointer to the HW struct
689e67fbcfbSJacob Keller  * @bank: whether to read from the active or inactive flash module
690e67fbcfbSJacob Keller  * @orom: pointer to Option ROM info structure
691e67fbcfbSJacob Keller  *
692e67fbcfbSJacob Keller  * Read Option ROM version and security revision from the Option ROM flash
693e67fbcfbSJacob Keller  * section.
694e67fbcfbSJacob Keller  */
6955e24d598STony Nguyen static int
696e67fbcfbSJacob Keller ice_get_orom_ver_info(struct ice_hw *hw, enum ice_bank_select bank, struct ice_orom_info *orom)
697e67fbcfbSJacob Keller {
698e67fbcfbSJacob Keller 	struct ice_orom_civd_info civd;
699d4e87444SJacob Keller 	u32 combo_ver;
7005518ac2aSTony Nguyen 	int status;
701d4e87444SJacob Keller 
702e67fbcfbSJacob Keller 	status = ice_get_orom_civd_data(hw, bank, &civd);
703d4e87444SJacob Keller 	if (status) {
704e67fbcfbSJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to locate valid Option ROM CIVD data\n");
705d4e87444SJacob Keller 		return status;
706d4e87444SJacob Keller 	}
707d4e87444SJacob Keller 
708e67fbcfbSJacob Keller 	combo_ver = le32_to_cpu(civd.combo_ver);
709d4e87444SJacob Keller 
710e67fbcfbSJacob Keller 	orom->major = (u8)((combo_ver & ICE_OROM_VER_MASK) >> ICE_OROM_VER_SHIFT);
711d4e87444SJacob Keller 	orom->patch = (u8)(combo_ver & ICE_OROM_VER_PATCH_MASK);
712e67fbcfbSJacob Keller 	orom->build = (u16)((combo_ver & ICE_OROM_VER_BUILD_MASK) >> ICE_OROM_VER_BUILD_SHIFT);
713d4e87444SJacob Keller 
714d4e87444SJacob Keller 	return 0;
715d4e87444SJacob Keller }
716d4e87444SJacob Keller 
717d4e87444SJacob Keller /**
718e67fbcfbSJacob Keller  * ice_get_inactive_orom_ver - Read Option ROM version from the inactive bank
719e67fbcfbSJacob Keller  * @hw: pointer to the HW structure
720e67fbcfbSJacob Keller  * @orom: storage for Option ROM version information
721e67fbcfbSJacob Keller  *
722e67fbcfbSJacob Keller  * Reads the Option ROM version and security revision data for the inactive
723e67fbcfbSJacob Keller  * section of flash. Used to access version data for a pending update that has
724e67fbcfbSJacob Keller  * not yet been activated.
725e67fbcfbSJacob Keller  */
7265e24d598STony Nguyen int ice_get_inactive_orom_ver(struct ice_hw *hw, struct ice_orom_info *orom)
727e67fbcfbSJacob Keller {
728e67fbcfbSJacob Keller 	return ice_get_orom_ver_info(hw, ICE_INACTIVE_FLASH_BANK, orom);
729e67fbcfbSJacob Keller }
730e67fbcfbSJacob Keller 
731e67fbcfbSJacob Keller /**
732e120a9abSJacob Keller  * ice_get_netlist_info
733f45a645fSJacob Keller  * @hw: pointer to the HW struct
734e120a9abSJacob Keller  * @bank: whether to read from the active or inactive flash bank
735e120a9abSJacob Keller  * @netlist: pointer to netlist version info structure
736f45a645fSJacob Keller  *
737e120a9abSJacob Keller  * Get the netlist version information from the requested bank. Reads the Link
738e120a9abSJacob Keller  * Topology section to find the Netlist ID block and extract the relevant
739e120a9abSJacob Keller  * information into the netlist version structure.
740f45a645fSJacob Keller  */
7415e24d598STony Nguyen static int
742e120a9abSJacob Keller ice_get_netlist_info(struct ice_hw *hw, enum ice_bank_select bank,
743e120a9abSJacob Keller 		     struct ice_netlist_info *netlist)
744f45a645fSJacob Keller {
745e120a9abSJacob Keller 	u16 module_id, length, node_count, i;
746e120a9abSJacob Keller 	u16 *id_blk;
7475518ac2aSTony Nguyen 	int status;
748f45a645fSJacob Keller 
749e120a9abSJacob Keller 	status = ice_read_netlist_module(hw, bank, ICE_NETLIST_TYPE_OFFSET, &module_id);
750e120a9abSJacob Keller 	if (status)
751e120a9abSJacob Keller 		return status;
752e120a9abSJacob Keller 
753e120a9abSJacob Keller 	if (module_id != ICE_NETLIST_LINK_TOPO_MOD_ID) {
754e120a9abSJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Expected netlist module_id ID of 0x%04x, but got 0x%04x\n",
755e120a9abSJacob Keller 			  ICE_NETLIST_LINK_TOPO_MOD_ID, module_id);
756d54699e2STony Nguyen 		return -EIO;
757f45a645fSJacob Keller 	}
758f45a645fSJacob Keller 
759e120a9abSJacob Keller 	status = ice_read_netlist_module(hw, bank, ICE_LINK_TOPO_MODULE_LEN, &length);
760e120a9abSJacob Keller 	if (status)
761e120a9abSJacob Keller 		return status;
762e120a9abSJacob Keller 
763e120a9abSJacob Keller 	/* sanity check that we have at least enough words to store the netlist ID block */
764e120a9abSJacob Keller 	if (length < ICE_NETLIST_ID_BLK_SIZE) {
765e120a9abSJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Netlist Link Topology module too small. Expected at least %u words, but got %u words.\n",
766e120a9abSJacob Keller 			  ICE_NETLIST_ID_BLK_SIZE, length);
767d54699e2STony Nguyen 		return -EIO;
768e120a9abSJacob Keller 	}
769e120a9abSJacob Keller 
770e120a9abSJacob Keller 	status = ice_read_netlist_module(hw, bank, ICE_LINK_TOPO_NODE_COUNT, &node_count);
771e120a9abSJacob Keller 	if (status)
772e120a9abSJacob Keller 		return status;
773e120a9abSJacob Keller 	node_count &= ICE_LINK_TOPO_NODE_COUNT_M;
774e120a9abSJacob Keller 
775e120a9abSJacob Keller 	id_blk = kcalloc(ICE_NETLIST_ID_BLK_SIZE, sizeof(*id_blk), GFP_KERNEL);
776e120a9abSJacob Keller 	if (!id_blk)
777d54699e2STony Nguyen 		return -ENOMEM;
778e120a9abSJacob Keller 
779e120a9abSJacob Keller 	/* Read out the entire Netlist ID Block at once. */
780e120a9abSJacob Keller 	status = ice_read_flash_module(hw, bank, ICE_SR_NETLIST_BANK_PTR,
781e120a9abSJacob Keller 				       ICE_NETLIST_ID_BLK_OFFSET(node_count) * sizeof(u16),
782e120a9abSJacob Keller 				       (u8 *)id_blk, ICE_NETLIST_ID_BLK_SIZE * sizeof(u16));
783e120a9abSJacob Keller 	if (status)
784f45a645fSJacob Keller 		goto exit_error;
785f45a645fSJacob Keller 
786e120a9abSJacob Keller 	for (i = 0; i < ICE_NETLIST_ID_BLK_SIZE; i++)
787e120a9abSJacob Keller 		id_blk[i] = le16_to_cpu(((__force __le16 *)id_blk)[i]);
788f45a645fSJacob Keller 
789e120a9abSJacob Keller 	netlist->major = id_blk[ICE_NETLIST_ID_BLK_MAJOR_VER_HIGH] << 16 |
790e120a9abSJacob Keller 			 id_blk[ICE_NETLIST_ID_BLK_MAJOR_VER_LOW];
791e120a9abSJacob Keller 	netlist->minor = id_blk[ICE_NETLIST_ID_BLK_MINOR_VER_HIGH] << 16 |
792e120a9abSJacob Keller 			 id_blk[ICE_NETLIST_ID_BLK_MINOR_VER_LOW];
793e120a9abSJacob Keller 	netlist->type = id_blk[ICE_NETLIST_ID_BLK_TYPE_HIGH] << 16 |
794e120a9abSJacob Keller 			id_blk[ICE_NETLIST_ID_BLK_TYPE_LOW];
795e120a9abSJacob Keller 	netlist->rev = id_blk[ICE_NETLIST_ID_BLK_REV_HIGH] << 16 |
796e120a9abSJacob Keller 		       id_blk[ICE_NETLIST_ID_BLK_REV_LOW];
797e120a9abSJacob Keller 	netlist->cust_ver = id_blk[ICE_NETLIST_ID_BLK_CUST_VER];
798f45a645fSJacob Keller 	/* Read the left most 4 bytes of SHA */
799e120a9abSJacob Keller 	netlist->hash = id_blk[ICE_NETLIST_ID_BLK_SHA_HASH_WORD(15)] << 16 |
800e120a9abSJacob Keller 			id_blk[ICE_NETLIST_ID_BLK_SHA_HASH_WORD(14)];
801f45a645fSJacob Keller 
802f45a645fSJacob Keller exit_error:
803e120a9abSJacob Keller 	kfree(id_blk);
804e120a9abSJacob Keller 
805e120a9abSJacob Keller 	return status;
806e120a9abSJacob Keller }
807e120a9abSJacob Keller 
808e120a9abSJacob Keller /**
809e120a9abSJacob Keller  * ice_get_inactive_netlist_ver
810e120a9abSJacob Keller  * @hw: pointer to the HW struct
811e120a9abSJacob Keller  * @netlist: pointer to netlist version info structure
812e120a9abSJacob Keller  *
813e120a9abSJacob Keller  * Read the netlist version data from the inactive netlist bank. Used to
814e120a9abSJacob Keller  * extract version data of a pending flash update in order to display the
815e120a9abSJacob Keller  * version data.
816e120a9abSJacob Keller  */
8175e24d598STony Nguyen int ice_get_inactive_netlist_ver(struct ice_hw *hw, struct ice_netlist_info *netlist)
818e120a9abSJacob Keller {
819e120a9abSJacob Keller 	return ice_get_netlist_info(hw, ICE_INACTIVE_FLASH_BANK, netlist);
820f45a645fSJacob Keller }
821f45a645fSJacob Keller 
822f45a645fSJacob Keller /**
82381f07491SJacob Keller  * ice_discover_flash_size - Discover the available flash size.
82481f07491SJacob Keller  * @hw: pointer to the HW struct
82581f07491SJacob Keller  *
82681f07491SJacob Keller  * The device flash could be up to 16MB in size. However, it is possible that
82781f07491SJacob Keller  * the actual size is smaller. Use bisection to determine the accessible size
82881f07491SJacob Keller  * of flash memory.
82981f07491SJacob Keller  */
8305e24d598STony Nguyen static int ice_discover_flash_size(struct ice_hw *hw)
83181f07491SJacob Keller {
83281f07491SJacob Keller 	u32 min_size = 0, max_size = ICE_AQC_NVM_MAX_OFFSET + 1;
8335e24d598STony Nguyen 	int status;
83481f07491SJacob Keller 
83581f07491SJacob Keller 	status = ice_acquire_nvm(hw, ICE_RES_READ);
83681f07491SJacob Keller 	if (status)
83781f07491SJacob Keller 		return status;
83881f07491SJacob Keller 
83981f07491SJacob Keller 	while ((max_size - min_size) > 1) {
84081f07491SJacob Keller 		u32 offset = (max_size + min_size) / 2;
84181f07491SJacob Keller 		u32 len = 1;
84281f07491SJacob Keller 		u8 data;
84381f07491SJacob Keller 
84481f07491SJacob Keller 		status = ice_read_flat_nvm(hw, offset, &len, &data, false);
845d54699e2STony Nguyen 		if (status == -EIO &&
84681f07491SJacob Keller 		    hw->adminq.sq_last_status == ICE_AQ_RC_EINVAL) {
8479228d8b2SJacob Keller 			ice_debug(hw, ICE_DBG_NVM, "%s: New upper bound of %u bytes\n",
84881f07491SJacob Keller 				  __func__, offset);
84981f07491SJacob Keller 			status = 0;
85081f07491SJacob Keller 			max_size = offset;
85181f07491SJacob Keller 		} else if (!status) {
8529228d8b2SJacob Keller 			ice_debug(hw, ICE_DBG_NVM, "%s: New lower bound of %u bytes\n",
85381f07491SJacob Keller 				  __func__, offset);
85481f07491SJacob Keller 			min_size = offset;
85581f07491SJacob Keller 		} else {
85681f07491SJacob Keller 			/* an unexpected error occurred */
85781f07491SJacob Keller 			goto err_read_flat_nvm;
85881f07491SJacob Keller 		}
85981f07491SJacob Keller 	}
86081f07491SJacob Keller 
8619228d8b2SJacob Keller 	ice_debug(hw, ICE_DBG_NVM, "Predicted flash size is %u bytes\n", max_size);
86281f07491SJacob Keller 
8639af368faSJacob Keller 	hw->flash.flash_size = max_size;
86481f07491SJacob Keller 
86581f07491SJacob Keller err_read_flat_nvm:
86681f07491SJacob Keller 	ice_release_nvm(hw);
86781f07491SJacob Keller 
86881f07491SJacob Keller 	return status;
86981f07491SJacob Keller }
87081f07491SJacob Keller 
87181f07491SJacob Keller /**
8721fa95e01SJacob Keller  * ice_read_sr_pointer - Read the value of a Shadow RAM pointer word
8731fa95e01SJacob Keller  * @hw: pointer to the HW structure
8741fa95e01SJacob Keller  * @offset: the word offset of the Shadow RAM word to read
8751fa95e01SJacob Keller  * @pointer: pointer value read from Shadow RAM
8761fa95e01SJacob Keller  *
8771fa95e01SJacob Keller  * Read the given Shadow RAM word, and convert it to a pointer value specified
8781fa95e01SJacob Keller  * in bytes. This function assumes the specified offset is a valid pointer
8791fa95e01SJacob Keller  * word.
8801fa95e01SJacob Keller  *
8811fa95e01SJacob Keller  * Each pointer word specifies whether it is stored in word size or 4KB
8821fa95e01SJacob Keller  * sector size by using the highest bit. The reported pointer value will be in
8831fa95e01SJacob Keller  * bytes, intended for flat NVM reads.
8841fa95e01SJacob Keller  */
8855518ac2aSTony Nguyen static int ice_read_sr_pointer(struct ice_hw *hw, u16 offset, u32 *pointer)
8861fa95e01SJacob Keller {
8875e24d598STony Nguyen 	int status;
8881fa95e01SJacob Keller 	u16 value;
8891fa95e01SJacob Keller 
8901fa95e01SJacob Keller 	status = ice_read_sr_word(hw, offset, &value);
8911fa95e01SJacob Keller 	if (status)
8921fa95e01SJacob Keller 		return status;
8931fa95e01SJacob Keller 
8941fa95e01SJacob Keller 	/* Determine if the pointer is in 4KB or word units */
8951fa95e01SJacob Keller 	if (value & ICE_SR_NVM_PTR_4KB_UNITS)
8961fa95e01SJacob Keller 		*pointer = (value & ~ICE_SR_NVM_PTR_4KB_UNITS) * 4 * 1024;
8971fa95e01SJacob Keller 	else
8981fa95e01SJacob Keller 		*pointer = value * 2;
8991fa95e01SJacob Keller 
9001fa95e01SJacob Keller 	return 0;
9011fa95e01SJacob Keller }
9021fa95e01SJacob Keller 
9031fa95e01SJacob Keller /**
9041fa95e01SJacob Keller  * ice_read_sr_area_size - Read an area size from a Shadow RAM word
9051fa95e01SJacob Keller  * @hw: pointer to the HW structure
9061fa95e01SJacob Keller  * @offset: the word offset of the Shadow RAM to read
9071fa95e01SJacob Keller  * @size: size value read from the Shadow RAM
9081fa95e01SJacob Keller  *
9091fa95e01SJacob Keller  * Read the given Shadow RAM word, and convert it to an area size value
9101fa95e01SJacob Keller  * specified in bytes. This function assumes the specified offset is a valid
9111fa95e01SJacob Keller  * area size word.
9121fa95e01SJacob Keller  *
9131fa95e01SJacob Keller  * Each area size word is specified in 4KB sector units. This function reports
9141fa95e01SJacob Keller  * the size in bytes, intended for flat NVM reads.
9151fa95e01SJacob Keller  */
9165518ac2aSTony Nguyen static int ice_read_sr_area_size(struct ice_hw *hw, u16 offset, u32 *size)
9171fa95e01SJacob Keller {
9185e24d598STony Nguyen 	int status;
9191fa95e01SJacob Keller 	u16 value;
9201fa95e01SJacob Keller 
9211fa95e01SJacob Keller 	status = ice_read_sr_word(hw, offset, &value);
9221fa95e01SJacob Keller 	if (status)
9231fa95e01SJacob Keller 		return status;
9241fa95e01SJacob Keller 
9251fa95e01SJacob Keller 	/* Area sizes are always specified in 4KB units */
9261fa95e01SJacob Keller 	*size = value * 4 * 1024;
9271fa95e01SJacob Keller 
9281fa95e01SJacob Keller 	return 0;
9291fa95e01SJacob Keller }
9301fa95e01SJacob Keller 
9311fa95e01SJacob Keller /**
9321fa95e01SJacob Keller  * ice_determine_active_flash_banks - Discover active bank for each module
9331fa95e01SJacob Keller  * @hw: pointer to the HW struct
9341fa95e01SJacob Keller  *
9351fa95e01SJacob Keller  * Read the Shadow RAM control word and determine which banks are active for
9361fa95e01SJacob Keller  * the NVM, OROM, and Netlist modules. Also read and calculate the associated
9371fa95e01SJacob Keller  * pointer and size. These values are then cached into the ice_flash_info
9381fa95e01SJacob Keller  * structure for later use in order to calculate the correct offset to read
9391fa95e01SJacob Keller  * from the active module.
9401fa95e01SJacob Keller  */
9415518ac2aSTony Nguyen static int ice_determine_active_flash_banks(struct ice_hw *hw)
9421fa95e01SJacob Keller {
9431fa95e01SJacob Keller 	struct ice_bank_info *banks = &hw->flash.banks;
9441fa95e01SJacob Keller 	u16 ctrl_word;
9455518ac2aSTony Nguyen 	int status;
9461fa95e01SJacob Keller 
9471fa95e01SJacob Keller 	status = ice_read_sr_word(hw, ICE_SR_NVM_CTRL_WORD, &ctrl_word);
9481fa95e01SJacob Keller 	if (status) {
9491fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read the Shadow RAM control word\n");
9501fa95e01SJacob Keller 		return status;
9511fa95e01SJacob Keller 	}
9521fa95e01SJacob Keller 
9531fa95e01SJacob Keller 	/* Check that the control word indicates validity */
9541fa95e01SJacob Keller 	if ((ctrl_word & ICE_SR_CTRL_WORD_1_M) >> ICE_SR_CTRL_WORD_1_S != ICE_SR_CTRL_WORD_VALID) {
9551fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Shadow RAM control word is invalid\n");
956d54699e2STony Nguyen 		return -EIO;
9571fa95e01SJacob Keller 	}
9581fa95e01SJacob Keller 
9591fa95e01SJacob Keller 	if (!(ctrl_word & ICE_SR_CTRL_WORD_NVM_BANK))
9601fa95e01SJacob Keller 		banks->nvm_bank = ICE_1ST_FLASH_BANK;
9611fa95e01SJacob Keller 	else
9621fa95e01SJacob Keller 		banks->nvm_bank = ICE_2ND_FLASH_BANK;
9631fa95e01SJacob Keller 
9641fa95e01SJacob Keller 	if (!(ctrl_word & ICE_SR_CTRL_WORD_OROM_BANK))
9651fa95e01SJacob Keller 		banks->orom_bank = ICE_1ST_FLASH_BANK;
9661fa95e01SJacob Keller 	else
9671fa95e01SJacob Keller 		banks->orom_bank = ICE_2ND_FLASH_BANK;
9681fa95e01SJacob Keller 
9691fa95e01SJacob Keller 	if (!(ctrl_word & ICE_SR_CTRL_WORD_NETLIST_BANK))
9701fa95e01SJacob Keller 		banks->netlist_bank = ICE_1ST_FLASH_BANK;
9711fa95e01SJacob Keller 	else
9721fa95e01SJacob Keller 		banks->netlist_bank = ICE_2ND_FLASH_BANK;
9731fa95e01SJacob Keller 
9741fa95e01SJacob Keller 	status = ice_read_sr_pointer(hw, ICE_SR_1ST_NVM_BANK_PTR, &banks->nvm_ptr);
9751fa95e01SJacob Keller 	if (status) {
9761fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank pointer\n");
9771fa95e01SJacob Keller 		return status;
9781fa95e01SJacob Keller 	}
9791fa95e01SJacob Keller 
9801fa95e01SJacob Keller 	status = ice_read_sr_area_size(hw, ICE_SR_NVM_BANK_SIZE, &banks->nvm_size);
9811fa95e01SJacob Keller 	if (status) {
9821fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank area size\n");
9831fa95e01SJacob Keller 		return status;
9841fa95e01SJacob Keller 	}
9851fa95e01SJacob Keller 
9861fa95e01SJacob Keller 	status = ice_read_sr_pointer(hw, ICE_SR_1ST_OROM_BANK_PTR, &banks->orom_ptr);
9871fa95e01SJacob Keller 	if (status) {
9881fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank pointer\n");
9891fa95e01SJacob Keller 		return status;
9901fa95e01SJacob Keller 	}
9911fa95e01SJacob Keller 
9921fa95e01SJacob Keller 	status = ice_read_sr_area_size(hw, ICE_SR_OROM_BANK_SIZE, &banks->orom_size);
9931fa95e01SJacob Keller 	if (status) {
9941fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank area size\n");
9951fa95e01SJacob Keller 		return status;
9961fa95e01SJacob Keller 	}
9971fa95e01SJacob Keller 
9981fa95e01SJacob Keller 	status = ice_read_sr_pointer(hw, ICE_SR_NETLIST_BANK_PTR, &banks->netlist_ptr);
9991fa95e01SJacob Keller 	if (status) {
10001fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank pointer\n");
10011fa95e01SJacob Keller 		return status;
10021fa95e01SJacob Keller 	}
10031fa95e01SJacob Keller 
10041fa95e01SJacob Keller 	status = ice_read_sr_area_size(hw, ICE_SR_NETLIST_BANK_SIZE, &banks->netlist_size);
10051fa95e01SJacob Keller 	if (status) {
10061fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank area size\n");
10071fa95e01SJacob Keller 		return status;
10081fa95e01SJacob Keller 	}
10091fa95e01SJacob Keller 
10101fa95e01SJacob Keller 	return 0;
10111fa95e01SJacob Keller }
10121fa95e01SJacob Keller 
10131fa95e01SJacob Keller /**
1014f31e4b6fSAnirudh Venkataramanan  * ice_init_nvm - initializes NVM setting
1015f9867df6SAnirudh Venkataramanan  * @hw: pointer to the HW struct
1016f31e4b6fSAnirudh Venkataramanan  *
1017f31e4b6fSAnirudh Venkataramanan  * This function reads and populates NVM settings such as Shadow RAM size,
1018f31e4b6fSAnirudh Venkataramanan  * max_timeout, and blank_nvm_mode
1019f31e4b6fSAnirudh Venkataramanan  */
10205e24d598STony Nguyen int ice_init_nvm(struct ice_hw *hw)
1021f31e4b6fSAnirudh Venkataramanan {
10229af368faSJacob Keller 	struct ice_flash_info *flash = &hw->flash;
1023f31e4b6fSAnirudh Venkataramanan 	u32 fla, gens_stat;
1024f31e4b6fSAnirudh Venkataramanan 	u8 sr_size;
10255518ac2aSTony Nguyen 	int status;
1026f31e4b6fSAnirudh Venkataramanan 
1027f9867df6SAnirudh Venkataramanan 	/* The SR size is stored regardless of the NVM programming mode
1028f31e4b6fSAnirudh Venkataramanan 	 * as the blank mode may be used in the factory line.
1029f31e4b6fSAnirudh Venkataramanan 	 */
1030f31e4b6fSAnirudh Venkataramanan 	gens_stat = rd32(hw, GLNVM_GENS);
1031f31e4b6fSAnirudh Venkataramanan 	sr_size = (gens_stat & GLNVM_GENS_SR_SIZE_M) >> GLNVM_GENS_SR_SIZE_S;
1032f31e4b6fSAnirudh Venkataramanan 
1033f31e4b6fSAnirudh Venkataramanan 	/* Switching to words (sr_size contains power of 2) */
10349af368faSJacob Keller 	flash->sr_words = BIT(sr_size) * ICE_SR_WORDS_IN_1KB;
1035f31e4b6fSAnirudh Venkataramanan 
1036f31e4b6fSAnirudh Venkataramanan 	/* Check if we are in the normal or blank NVM programming mode */
1037f31e4b6fSAnirudh Venkataramanan 	fla = rd32(hw, GLNVM_FLA);
1038f31e4b6fSAnirudh Venkataramanan 	if (fla & GLNVM_FLA_LOCKED_M) { /* Normal programming mode */
10399af368faSJacob Keller 		flash->blank_nvm_mode = false;
1040031f2147SMd Fahad Iqbal Polash 	} else {
1041031f2147SMd Fahad Iqbal Polash 		/* Blank programming mode */
10429af368faSJacob Keller 		flash->blank_nvm_mode = true;
10439228d8b2SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "NVM init error: unsupported blank mode.\n");
1044d54699e2STony Nguyen 		return -EIO;
1045f31e4b6fSAnirudh Venkataramanan 	}
1046f31e4b6fSAnirudh Venkataramanan 
104781f07491SJacob Keller 	status = ice_discover_flash_size(hw);
104881f07491SJacob Keller 	if (status) {
10499228d8b2SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "NVM init error: failed to discover flash size.\n");
105081f07491SJacob Keller 		return status;
105181f07491SJacob Keller 	}
105281f07491SJacob Keller 
10531fa95e01SJacob Keller 	status = ice_determine_active_flash_banks(hw);
10541fa95e01SJacob Keller 	if (status) {
10551fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to determine active flash banks.\n");
10561fa95e01SJacob Keller 		return status;
10571fa95e01SJacob Keller 	}
10581fa95e01SJacob Keller 
10590ce50c70SJacob Keller 	status = ice_get_nvm_ver_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->nvm);
10609af368faSJacob Keller 	if (status) {
10619af368faSJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Failed to read NVM info.\n");
10629af368faSJacob Keller 		return status;
10639af368faSJacob Keller 	}
10649af368faSJacob Keller 
1065e67fbcfbSJacob Keller 	status = ice_get_orom_ver_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->orom);
1066e67fbcfbSJacob Keller 	if (status)
1067d4e87444SJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Failed to read Option ROM info.\n");
10684c98ab55SBruce Allan 
1069f45a645fSJacob Keller 	/* read the netlist version information */
1070e120a9abSJacob Keller 	status = ice_get_netlist_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->netlist);
1071f45a645fSJacob Keller 	if (status)
1072f45a645fSJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Failed to read netlist info.\n");
1073f45a645fSJacob Keller 
1074031f2147SMd Fahad Iqbal Polash 	return 0;
1075031f2147SMd Fahad Iqbal Polash }
1076031f2147SMd Fahad Iqbal Polash 
10774c98ab55SBruce Allan /**
10780e674aebSAnirudh Venkataramanan  * ice_nvm_validate_checksum
10790e674aebSAnirudh Venkataramanan  * @hw: pointer to the HW struct
10800e674aebSAnirudh Venkataramanan  *
10810e674aebSAnirudh Venkataramanan  * Verify NVM PFA checksum validity (0x0706)
10820e674aebSAnirudh Venkataramanan  */
10835e24d598STony Nguyen int ice_nvm_validate_checksum(struct ice_hw *hw)
10840e674aebSAnirudh Venkataramanan {
10850e674aebSAnirudh Venkataramanan 	struct ice_aqc_nvm_checksum *cmd;
10860e674aebSAnirudh Venkataramanan 	struct ice_aq_desc desc;
10875e24d598STony Nguyen 	int status;
10880e674aebSAnirudh Venkataramanan 
10890e674aebSAnirudh Venkataramanan 	status = ice_acquire_nvm(hw, ICE_RES_READ);
10900e674aebSAnirudh Venkataramanan 	if (status)
10910e674aebSAnirudh Venkataramanan 		return status;
10920e674aebSAnirudh Venkataramanan 
10930e674aebSAnirudh Venkataramanan 	cmd = &desc.params.nvm_checksum;
10940e674aebSAnirudh Venkataramanan 
10950e674aebSAnirudh Venkataramanan 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_checksum);
10960e674aebSAnirudh Venkataramanan 	cmd->flags = ICE_AQC_NVM_CHECKSUM_VERIFY;
10970e674aebSAnirudh Venkataramanan 
10980e674aebSAnirudh Venkataramanan 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
10990e674aebSAnirudh Venkataramanan 	ice_release_nvm(hw);
11000e674aebSAnirudh Venkataramanan 
11010e674aebSAnirudh Venkataramanan 	if (!status)
11020e674aebSAnirudh Venkataramanan 		if (le16_to_cpu(cmd->checksum) != ICE_AQC_NVM_CHECKSUM_CORRECT)
1103d54699e2STony Nguyen 			status = -EIO;
11040e674aebSAnirudh Venkataramanan 
11050e674aebSAnirudh Venkataramanan 	return status;
11060e674aebSAnirudh Venkataramanan }
1107544cd2acSCudzilo, Szymon T 
1108544cd2acSCudzilo, Szymon T /**
1109544cd2acSCudzilo, Szymon T  * ice_nvm_write_activate
1110544cd2acSCudzilo, Szymon T  * @hw: pointer to the HW struct
1111399e27dbSJacob Keller  * @cmd_flags: flags for write activate command
1112399e27dbSJacob Keller  * @response_flags: response indicators from firmware
1113544cd2acSCudzilo, Szymon T  *
1114544cd2acSCudzilo, Szymon T  * Update the control word with the required banks' validity bits
1115544cd2acSCudzilo, Szymon T  * and dumps the Shadow RAM to flash (0x0707)
1116399e27dbSJacob Keller  *
1117*da02ee9cSJacob Keller  * cmd_flags controls which banks to activate, the preservation level to use
1118*da02ee9cSJacob Keller  * when activating the NVM bank, and whether an EMP reset is required for
1119*da02ee9cSJacob Keller  * activation.
1120*da02ee9cSJacob Keller  *
1121*da02ee9cSJacob Keller  * Note that the 16bit cmd_flags value is split between two separate 1 byte
1122*da02ee9cSJacob Keller  * flag values in the descriptor.
1123399e27dbSJacob Keller  *
1124399e27dbSJacob Keller  * On successful return of the firmware command, the response_flags variable
1125399e27dbSJacob Keller  * is updated with the flags reported by firmware indicating certain status,
1126399e27dbSJacob Keller  * such as whether EMP reset is enabled.
1127544cd2acSCudzilo, Szymon T  */
1128*da02ee9cSJacob Keller int ice_nvm_write_activate(struct ice_hw *hw, u16 cmd_flags, u8 *response_flags)
1129544cd2acSCudzilo, Szymon T {
1130544cd2acSCudzilo, Szymon T 	struct ice_aqc_nvm *cmd;
1131544cd2acSCudzilo, Szymon T 	struct ice_aq_desc desc;
1132399e27dbSJacob Keller 	int err;
1133544cd2acSCudzilo, Szymon T 
1134544cd2acSCudzilo, Szymon T 	cmd = &desc.params.nvm;
1135544cd2acSCudzilo, Szymon T 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_write_activate);
1136544cd2acSCudzilo, Szymon T 
1137*da02ee9cSJacob Keller 	cmd->cmd_flags = (u8)(cmd_flags & 0xFF);
1138*da02ee9cSJacob Keller 	cmd->offset_high = (u8)((cmd_flags >> 8) & 0xFF);
1139544cd2acSCudzilo, Szymon T 
1140399e27dbSJacob Keller 	err = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1141399e27dbSJacob Keller 	if (!err && response_flags)
1142399e27dbSJacob Keller 		*response_flags = cmd->cmd_flags;
1143399e27dbSJacob Keller 
1144399e27dbSJacob Keller 	return err;
1145544cd2acSCudzilo, Szymon T }
1146544cd2acSCudzilo, Szymon T 
1147544cd2acSCudzilo, Szymon T /**
1148544cd2acSCudzilo, Szymon T  * ice_aq_nvm_update_empr
1149544cd2acSCudzilo, Szymon T  * @hw: pointer to the HW struct
1150544cd2acSCudzilo, Szymon T  *
1151544cd2acSCudzilo, Szymon T  * Update empr (0x0709). This command allows SW to
1152544cd2acSCudzilo, Szymon T  * request an EMPR to activate new FW.
1153544cd2acSCudzilo, Szymon T  */
11545e24d598STony Nguyen int ice_aq_nvm_update_empr(struct ice_hw *hw)
1155544cd2acSCudzilo, Szymon T {
1156544cd2acSCudzilo, Szymon T 	struct ice_aq_desc desc;
1157544cd2acSCudzilo, Szymon T 
1158544cd2acSCudzilo, Szymon T 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_update_empr);
1159544cd2acSCudzilo, Szymon T 
1160544cd2acSCudzilo, Szymon T 	return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1161544cd2acSCudzilo, Szymon T }
1162544cd2acSCudzilo, Szymon T 
1163544cd2acSCudzilo, Szymon T /* ice_nvm_set_pkg_data
1164544cd2acSCudzilo, Szymon T  * @hw: pointer to the HW struct
1165544cd2acSCudzilo, Szymon T  * @del_pkg_data_flag: If is set then the current pkg_data store by FW
1166544cd2acSCudzilo, Szymon T  *		       is deleted.
1167544cd2acSCudzilo, Szymon T  *		       If bit is set to 1, then buffer should be size 0.
1168544cd2acSCudzilo, Szymon T  * @data: pointer to buffer
1169544cd2acSCudzilo, Szymon T  * @length: length of the buffer
1170544cd2acSCudzilo, Szymon T  * @cd: pointer to command details structure or NULL
1171544cd2acSCudzilo, Szymon T  *
1172544cd2acSCudzilo, Szymon T  * Set package data (0x070A). This command is equivalent to the reception
1173544cd2acSCudzilo, Szymon T  * of a PLDM FW Update GetPackageData cmd. This command should be sent
1174544cd2acSCudzilo, Szymon T  * as part of the NVM update as the first cmd in the flow.
1175544cd2acSCudzilo, Szymon T  */
1176544cd2acSCudzilo, Szymon T 
11775e24d598STony Nguyen int
1178544cd2acSCudzilo, Szymon T ice_nvm_set_pkg_data(struct ice_hw *hw, bool del_pkg_data_flag, u8 *data,
1179544cd2acSCudzilo, Szymon T 		     u16 length, struct ice_sq_cd *cd)
1180544cd2acSCudzilo, Szymon T {
1181544cd2acSCudzilo, Szymon T 	struct ice_aqc_nvm_pkg_data *cmd;
1182544cd2acSCudzilo, Szymon T 	struct ice_aq_desc desc;
1183544cd2acSCudzilo, Szymon T 
1184544cd2acSCudzilo, Szymon T 	if (length != 0 && !data)
1185d54699e2STony Nguyen 		return -EINVAL;
1186544cd2acSCudzilo, Szymon T 
1187544cd2acSCudzilo, Szymon T 	cmd = &desc.params.pkg_data;
1188544cd2acSCudzilo, Szymon T 
1189544cd2acSCudzilo, Szymon T 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_pkg_data);
1190544cd2acSCudzilo, Szymon T 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1191544cd2acSCudzilo, Szymon T 
1192544cd2acSCudzilo, Szymon T 	if (del_pkg_data_flag)
1193544cd2acSCudzilo, Szymon T 		cmd->cmd_flags |= ICE_AQC_NVM_PKG_DELETE;
1194544cd2acSCudzilo, Szymon T 
1195544cd2acSCudzilo, Szymon T 	return ice_aq_send_cmd(hw, &desc, data, length, cd);
1196544cd2acSCudzilo, Szymon T }
1197544cd2acSCudzilo, Szymon T 
1198544cd2acSCudzilo, Szymon T /* ice_nvm_pass_component_tbl
1199544cd2acSCudzilo, Szymon T  * @hw: pointer to the HW struct
1200544cd2acSCudzilo, Szymon T  * @data: pointer to buffer
1201544cd2acSCudzilo, Szymon T  * @length: length of the buffer
1202544cd2acSCudzilo, Szymon T  * @transfer_flag: parameter for determining stage of the update
1203544cd2acSCudzilo, Szymon T  * @comp_response: a pointer to the response from the 0x070B AQC.
1204544cd2acSCudzilo, Szymon T  * @comp_response_code: a pointer to the response code from the 0x070B AQC.
1205544cd2acSCudzilo, Szymon T  * @cd: pointer to command details structure or NULL
1206544cd2acSCudzilo, Szymon T  *
1207544cd2acSCudzilo, Szymon T  * Pass component table (0x070B). This command is equivalent to the reception
1208544cd2acSCudzilo, Szymon T  * of a PLDM FW Update PassComponentTable cmd. This command should be sent once
1209544cd2acSCudzilo, Szymon T  * per component. It can be only sent after Set Package Data cmd and before
1210544cd2acSCudzilo, Szymon T  * actual update. FW will assume these commands are going to be sent until
1211544cd2acSCudzilo, Szymon T  * the TransferFlag is set to End or StartAndEnd.
1212544cd2acSCudzilo, Szymon T  */
1213544cd2acSCudzilo, Szymon T 
12145e24d598STony Nguyen int
1215544cd2acSCudzilo, Szymon T ice_nvm_pass_component_tbl(struct ice_hw *hw, u8 *data, u16 length,
1216544cd2acSCudzilo, Szymon T 			   u8 transfer_flag, u8 *comp_response,
1217544cd2acSCudzilo, Szymon T 			   u8 *comp_response_code, struct ice_sq_cd *cd)
1218544cd2acSCudzilo, Szymon T {
1219544cd2acSCudzilo, Szymon T 	struct ice_aqc_nvm_pass_comp_tbl *cmd;
1220544cd2acSCudzilo, Szymon T 	struct ice_aq_desc desc;
12215e24d598STony Nguyen 	int status;
1222544cd2acSCudzilo, Szymon T 
1223544cd2acSCudzilo, Szymon T 	if (!data || !comp_response || !comp_response_code)
1224d54699e2STony Nguyen 		return -EINVAL;
1225544cd2acSCudzilo, Szymon T 
1226544cd2acSCudzilo, Szymon T 	cmd = &desc.params.pass_comp_tbl;
1227544cd2acSCudzilo, Szymon T 
1228544cd2acSCudzilo, Szymon T 	ice_fill_dflt_direct_cmd_desc(&desc,
1229544cd2acSCudzilo, Szymon T 				      ice_aqc_opc_nvm_pass_component_tbl);
1230544cd2acSCudzilo, Szymon T 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1231544cd2acSCudzilo, Szymon T 
1232544cd2acSCudzilo, Szymon T 	cmd->transfer_flag = transfer_flag;
1233544cd2acSCudzilo, Szymon T 	status = ice_aq_send_cmd(hw, &desc, data, length, cd);
1234544cd2acSCudzilo, Szymon T 
1235544cd2acSCudzilo, Szymon T 	if (!status) {
1236544cd2acSCudzilo, Szymon T 		*comp_response = cmd->component_response;
1237544cd2acSCudzilo, Szymon T 		*comp_response_code = cmd->component_response_code;
1238544cd2acSCudzilo, Szymon T 	}
1239544cd2acSCudzilo, Szymon T 	return status;
1240544cd2acSCudzilo, Szymon T }
1241