1f31e4b6fSAnirudh Venkataramanan // SPDX-License-Identifier: GPL-2.0
2f31e4b6fSAnirudh Venkataramanan /* Copyright (c) 2018, Intel Corporation. */
3f31e4b6fSAnirudh Venkataramanan 
4f31e4b6fSAnirudh Venkataramanan #include "ice_common.h"
5f31e4b6fSAnirudh Venkataramanan 
6f31e4b6fSAnirudh Venkataramanan /**
7f31e4b6fSAnirudh Venkataramanan  * ice_aq_read_nvm
8f9867df6SAnirudh Venkataramanan  * @hw: pointer to the HW struct
9f31e4b6fSAnirudh Venkataramanan  * @module_typeid: module pointer location in words from the NVM beginning
10f31e4b6fSAnirudh Venkataramanan  * @offset: byte offset from the module beginning
11f31e4b6fSAnirudh Venkataramanan  * @length: length of the section to be read (in bytes from the offset)
12f31e4b6fSAnirudh Venkataramanan  * @data: command buffer (size [bytes] = length)
13f31e4b6fSAnirudh Venkataramanan  * @last_command: tells if this is the last command in a series
14e9450990SJacob Keller  * @read_shadow_ram: tell if this is a shadow RAM read
15f31e4b6fSAnirudh Venkataramanan  * @cd: pointer to command details structure or NULL
16f31e4b6fSAnirudh Venkataramanan  *
17f31e4b6fSAnirudh Venkataramanan  * Read the NVM using the admin queue commands (0x0701)
18f31e4b6fSAnirudh Venkataramanan  */
195e24d598STony Nguyen static int
2043c89b16SAnirudh Venkataramanan ice_aq_read_nvm(struct ice_hw *hw, u16 module_typeid, u32 offset, u16 length,
21e9450990SJacob Keller 		void *data, bool last_command, bool read_shadow_ram,
22e9450990SJacob Keller 		struct ice_sq_cd *cd)
23f31e4b6fSAnirudh Venkataramanan {
24f31e4b6fSAnirudh Venkataramanan 	struct ice_aq_desc desc;
25f31e4b6fSAnirudh Venkataramanan 	struct ice_aqc_nvm *cmd;
26f31e4b6fSAnirudh Venkataramanan 
27f31e4b6fSAnirudh Venkataramanan 	cmd = &desc.params.nvm;
28f31e4b6fSAnirudh Venkataramanan 
2981f07491SJacob Keller 	if (offset > ICE_AQC_NVM_MAX_OFFSET)
30d54699e2STony Nguyen 		return -EINVAL;
31f31e4b6fSAnirudh Venkataramanan 
32f31e4b6fSAnirudh Venkataramanan 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_read);
33f31e4b6fSAnirudh Venkataramanan 
34e9450990SJacob Keller 	if (!read_shadow_ram && module_typeid == ICE_AQC_NVM_START_POINT)
35e9450990SJacob Keller 		cmd->cmd_flags |= ICE_AQC_NVM_FLASH_ONLY;
36e9450990SJacob Keller 
37f31e4b6fSAnirudh Venkataramanan 	/* If this is the last command in a series, set the proper flag. */
38f31e4b6fSAnirudh Venkataramanan 	if (last_command)
39f31e4b6fSAnirudh Venkataramanan 		cmd->cmd_flags |= ICE_AQC_NVM_LAST_CMD;
4043c89b16SAnirudh Venkataramanan 	cmd->module_typeid = cpu_to_le16(module_typeid);
4143c89b16SAnirudh Venkataramanan 	cmd->offset_low = cpu_to_le16(offset & 0xFFFF);
4243c89b16SAnirudh Venkataramanan 	cmd->offset_high = (offset >> 16) & 0xFF;
43f31e4b6fSAnirudh Venkataramanan 	cmd->length = cpu_to_le16(length);
44f31e4b6fSAnirudh Venkataramanan 
45f31e4b6fSAnirudh Venkataramanan 	return ice_aq_send_cmd(hw, &desc, data, length, cd);
46f31e4b6fSAnirudh Venkataramanan }
47f31e4b6fSAnirudh Venkataramanan 
48f31e4b6fSAnirudh Venkataramanan /**
49e9450990SJacob Keller  * ice_read_flat_nvm - Read portion of NVM by flat offset
50e9450990SJacob Keller  * @hw: pointer to the HW struct
51e9450990SJacob Keller  * @offset: offset from beginning of NVM
52e9450990SJacob Keller  * @length: (in) number of bytes to read; (out) number of bytes actually read
53e9450990SJacob Keller  * @data: buffer to return data in (sized to fit the specified length)
54e9450990SJacob Keller  * @read_shadow_ram: if true, read from shadow RAM instead of NVM
55e9450990SJacob Keller  *
56e9450990SJacob Keller  * Reads a portion of the NVM, as a flat memory space. This function correctly
57e9450990SJacob Keller  * breaks read requests across Shadow RAM sectors and ensures that no single
5832e6deb2SBruce Allan  * read request exceeds the maximum 4KB read for a single AdminQ command.
59e9450990SJacob Keller  *
60e9450990SJacob Keller  * Returns a status code on failure. Note that the data pointer may be
61e9450990SJacob Keller  * partially updated if some reads succeed before a failure.
62e9450990SJacob Keller  */
635e24d598STony Nguyen int
64e9450990SJacob Keller ice_read_flat_nvm(struct ice_hw *hw, u32 offset, u32 *length, u8 *data,
65e9450990SJacob Keller 		  bool read_shadow_ram)
66e9450990SJacob Keller {
67e9450990SJacob Keller 	u32 inlen = *length;
68e9450990SJacob Keller 	u32 bytes_read = 0;
69e9450990SJacob Keller 	bool last_cmd;
705518ac2aSTony Nguyen 	int status;
71e9450990SJacob Keller 
72e9450990SJacob Keller 	*length = 0;
73e9450990SJacob Keller 
74e9450990SJacob Keller 	/* Verify the length of the read if this is for the Shadow RAM */
759af368faSJacob Keller 	if (read_shadow_ram && ((offset + inlen) > (hw->flash.sr_words * 2u))) {
769228d8b2SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "NVM error: requested offset is beyond Shadow RAM limit\n");
77d54699e2STony Nguyen 		return -EINVAL;
78e9450990SJacob Keller 	}
79e9450990SJacob Keller 
80e9450990SJacob Keller 	do {
81e9450990SJacob Keller 		u32 read_size, sector_offset;
82e9450990SJacob Keller 
8332e6deb2SBruce Allan 		/* ice_aq_read_nvm cannot read more than 4KB at a time.
84e9450990SJacob Keller 		 * Additionally, a read from the Shadow RAM may not cross over
85e9450990SJacob Keller 		 * a sector boundary. Conveniently, the sector size is also
8632e6deb2SBruce Allan 		 * 4KB.
87e9450990SJacob Keller 		 */
88e9450990SJacob Keller 		sector_offset = offset % ICE_AQ_MAX_BUF_LEN;
89e9450990SJacob Keller 		read_size = min_t(u32, ICE_AQ_MAX_BUF_LEN - sector_offset,
90e9450990SJacob Keller 				  inlen - bytes_read);
91e9450990SJacob Keller 
92e9450990SJacob Keller 		last_cmd = !(bytes_read + read_size < inlen);
93e9450990SJacob Keller 
94e9450990SJacob Keller 		status = ice_aq_read_nvm(hw, ICE_AQC_NVM_START_POINT,
95e9450990SJacob Keller 					 offset, read_size,
96e9450990SJacob Keller 					 data + bytes_read, last_cmd,
97e9450990SJacob Keller 					 read_shadow_ram, NULL);
98e9450990SJacob Keller 		if (status)
99e9450990SJacob Keller 			break;
100e9450990SJacob Keller 
101e9450990SJacob Keller 		bytes_read += read_size;
102e9450990SJacob Keller 		offset += read_size;
103e9450990SJacob Keller 	} while (!last_cmd);
104e9450990SJacob Keller 
105e9450990SJacob Keller 	*length = bytes_read;
106e9450990SJacob Keller 	return status;
107e9450990SJacob Keller }
108e9450990SJacob Keller 
109e9450990SJacob Keller /**
110544cd2acSCudzilo, Szymon T  * ice_aq_update_nvm
111544cd2acSCudzilo, Szymon T  * @hw: pointer to the HW struct
112544cd2acSCudzilo, Szymon T  * @module_typeid: module pointer location in words from the NVM beginning
113544cd2acSCudzilo, Szymon T  * @offset: byte offset from the module beginning
114544cd2acSCudzilo, Szymon T  * @length: length of the section to be written (in bytes from the offset)
115544cd2acSCudzilo, Szymon T  * @data: command buffer (size [bytes] = length)
116544cd2acSCudzilo, Szymon T  * @last_command: tells if this is the last command in a series
117544cd2acSCudzilo, Szymon T  * @command_flags: command parameters
118544cd2acSCudzilo, Szymon T  * @cd: pointer to command details structure or NULL
119544cd2acSCudzilo, Szymon T  *
120544cd2acSCudzilo, Szymon T  * Update the NVM using the admin queue commands (0x0703)
121544cd2acSCudzilo, Szymon T  */
1225e24d598STony Nguyen int
123544cd2acSCudzilo, Szymon T ice_aq_update_nvm(struct ice_hw *hw, u16 module_typeid, u32 offset,
124544cd2acSCudzilo, Szymon T 		  u16 length, void *data, bool last_command, u8 command_flags,
125544cd2acSCudzilo, Szymon T 		  struct ice_sq_cd *cd)
126544cd2acSCudzilo, Szymon T {
127544cd2acSCudzilo, Szymon T 	struct ice_aq_desc desc;
128544cd2acSCudzilo, Szymon T 	struct ice_aqc_nvm *cmd;
129544cd2acSCudzilo, Szymon T 
130544cd2acSCudzilo, Szymon T 	cmd = &desc.params.nvm;
131544cd2acSCudzilo, Szymon T 
132544cd2acSCudzilo, Szymon T 	/* In offset the highest byte must be zeroed. */
133544cd2acSCudzilo, Szymon T 	if (offset & 0xFF000000)
134d54699e2STony Nguyen 		return -EINVAL;
135544cd2acSCudzilo, Szymon T 
136544cd2acSCudzilo, Szymon T 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_write);
137544cd2acSCudzilo, Szymon T 
138544cd2acSCudzilo, Szymon T 	cmd->cmd_flags |= command_flags;
139544cd2acSCudzilo, Szymon T 
140544cd2acSCudzilo, Szymon T 	/* If this is the last command in a series, set the proper flag. */
141544cd2acSCudzilo, Szymon T 	if (last_command)
142544cd2acSCudzilo, Szymon T 		cmd->cmd_flags |= ICE_AQC_NVM_LAST_CMD;
143544cd2acSCudzilo, Szymon T 	cmd->module_typeid = cpu_to_le16(module_typeid);
144544cd2acSCudzilo, Szymon T 	cmd->offset_low = cpu_to_le16(offset & 0xFFFF);
145544cd2acSCudzilo, Szymon T 	cmd->offset_high = (offset >> 16) & 0xFF;
146544cd2acSCudzilo, Szymon T 	cmd->length = cpu_to_le16(length);
147544cd2acSCudzilo, Szymon T 
148544cd2acSCudzilo, Szymon T 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
149544cd2acSCudzilo, Szymon T 
150544cd2acSCudzilo, Szymon T 	return ice_aq_send_cmd(hw, &desc, data, length, cd);
151544cd2acSCudzilo, Szymon T }
152544cd2acSCudzilo, Szymon T 
153544cd2acSCudzilo, Szymon T /**
154544cd2acSCudzilo, Szymon T  * ice_aq_erase_nvm
155544cd2acSCudzilo, Szymon T  * @hw: pointer to the HW struct
156544cd2acSCudzilo, Szymon T  * @module_typeid: module pointer location in words from the NVM beginning
157544cd2acSCudzilo, Szymon T  * @cd: pointer to command details structure or NULL
158544cd2acSCudzilo, Szymon T  *
159544cd2acSCudzilo, Szymon T  * Erase the NVM sector using the admin queue commands (0x0702)
160544cd2acSCudzilo, Szymon T  */
1615518ac2aSTony Nguyen int ice_aq_erase_nvm(struct ice_hw *hw, u16 module_typeid, struct ice_sq_cd *cd)
162544cd2acSCudzilo, Szymon T {
163544cd2acSCudzilo, Szymon T 	struct ice_aq_desc desc;
164544cd2acSCudzilo, Szymon T 	struct ice_aqc_nvm *cmd;
165544cd2acSCudzilo, Szymon T 
166544cd2acSCudzilo, Szymon T 	cmd = &desc.params.nvm;
167544cd2acSCudzilo, Szymon T 
168544cd2acSCudzilo, Szymon T 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_erase);
169544cd2acSCudzilo, Szymon T 
170544cd2acSCudzilo, Szymon T 	cmd->module_typeid = cpu_to_le16(module_typeid);
171544cd2acSCudzilo, Szymon T 	cmd->length = cpu_to_le16(ICE_AQC_NVM_ERASE_LEN);
172544cd2acSCudzilo, Szymon T 	cmd->offset_low = 0;
173544cd2acSCudzilo, Szymon T 	cmd->offset_high = 0;
174544cd2acSCudzilo, Szymon T 
175544cd2acSCudzilo, Szymon T 	return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
176544cd2acSCudzilo, Szymon T }
177544cd2acSCudzilo, Szymon T 
178544cd2acSCudzilo, Szymon T /**
179f31e4b6fSAnirudh Venkataramanan  * ice_read_sr_word_aq - Reads Shadow RAM via AQ
180f31e4b6fSAnirudh Venkataramanan  * @hw: pointer to the HW structure
181f31e4b6fSAnirudh Venkataramanan  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
182f31e4b6fSAnirudh Venkataramanan  * @data: word read from the Shadow RAM
183f31e4b6fSAnirudh Venkataramanan  *
184e9450990SJacob Keller  * Reads one 16 bit word from the Shadow RAM using ice_read_flat_nvm.
185f31e4b6fSAnirudh Venkataramanan  */
1865518ac2aSTony Nguyen static int ice_read_sr_word_aq(struct ice_hw *hw, u16 offset, u16 *data)
187f31e4b6fSAnirudh Venkataramanan {
188e9450990SJacob Keller 	u32 bytes = sizeof(u16);
1892efefb56SJacob Keller 	__le16 data_local;
1905518ac2aSTony Nguyen 	int status;
191f31e4b6fSAnirudh Venkataramanan 
192e9450990SJacob Keller 	/* Note that ice_read_flat_nvm takes into account the 4Kb AdminQ and
193e9450990SJacob Keller 	 * Shadow RAM sector restrictions necessary when reading from the NVM.
194e9450990SJacob Keller 	 */
195e9450990SJacob Keller 	status = ice_read_flat_nvm(hw, offset * sizeof(u16), &bytes,
19634d8461aSBruce Allan 				   (__force u8 *)&data_local, true);
197e9450990SJacob Keller 	if (status)
198f31e4b6fSAnirudh Venkataramanan 		return status;
199e9450990SJacob Keller 
200e9450990SJacob Keller 	*data = le16_to_cpu(data_local);
201e9450990SJacob Keller 	return 0;
202f31e4b6fSAnirudh Venkataramanan }
203f31e4b6fSAnirudh Venkataramanan 
204f31e4b6fSAnirudh Venkataramanan /**
205f31e4b6fSAnirudh Venkataramanan  * ice_acquire_nvm - Generic request for acquiring the NVM ownership
206f31e4b6fSAnirudh Venkataramanan  * @hw: pointer to the HW structure
207f31e4b6fSAnirudh Venkataramanan  * @access: NVM access type (read or write)
208f31e4b6fSAnirudh Venkataramanan  *
209f31e4b6fSAnirudh Venkataramanan  * This function will request NVM ownership.
210f31e4b6fSAnirudh Venkataramanan  */
2115518ac2aSTony Nguyen int ice_acquire_nvm(struct ice_hw *hw, enum ice_aq_res_access_type access)
212f31e4b6fSAnirudh Venkataramanan {
2139af368faSJacob Keller 	if (hw->flash.blank_nvm_mode)
214f31e4b6fSAnirudh Venkataramanan 		return 0;
215f31e4b6fSAnirudh Venkataramanan 
216ff2b1321SDan Nowlin 	return ice_acquire_res(hw, ICE_NVM_RES_ID, access, ICE_NVM_TIMEOUT);
217f31e4b6fSAnirudh Venkataramanan }
218f31e4b6fSAnirudh Venkataramanan 
219f31e4b6fSAnirudh Venkataramanan /**
220f31e4b6fSAnirudh Venkataramanan  * ice_release_nvm - Generic request for releasing the NVM ownership
221f31e4b6fSAnirudh Venkataramanan  * @hw: pointer to the HW structure
222f31e4b6fSAnirudh Venkataramanan  *
223f31e4b6fSAnirudh Venkataramanan  * This function will release NVM ownership.
224f31e4b6fSAnirudh Venkataramanan  */
22584a24798SJesse Brandeburg void ice_release_nvm(struct ice_hw *hw)
226f31e4b6fSAnirudh Venkataramanan {
2279af368faSJacob Keller 	if (hw->flash.blank_nvm_mode)
228f31e4b6fSAnirudh Venkataramanan 		return;
229f31e4b6fSAnirudh Venkataramanan 
230f31e4b6fSAnirudh Venkataramanan 	ice_release_res(hw, ICE_NVM_RES_ID);
231f31e4b6fSAnirudh Venkataramanan }
232f31e4b6fSAnirudh Venkataramanan 
233f31e4b6fSAnirudh Venkataramanan /**
2340ce50c70SJacob Keller  * ice_get_flash_bank_offset - Get offset into requested flash bank
2350ce50c70SJacob Keller  * @hw: pointer to the HW structure
2360ce50c70SJacob Keller  * @bank: whether to read from the active or inactive flash bank
2370ce50c70SJacob Keller  * @module: the module to read from
2380ce50c70SJacob Keller  *
2390ce50c70SJacob Keller  * Based on the module, lookup the module offset from the beginning of the
2400ce50c70SJacob Keller  * flash.
2410ce50c70SJacob Keller  *
2420ce50c70SJacob Keller  * Returns the flash offset. Note that a value of zero is invalid and must be
2430ce50c70SJacob Keller  * treated as an error.
2440ce50c70SJacob Keller  */
2450ce50c70SJacob Keller static u32 ice_get_flash_bank_offset(struct ice_hw *hw, enum ice_bank_select bank, u16 module)
2460ce50c70SJacob Keller {
2470ce50c70SJacob Keller 	struct ice_bank_info *banks = &hw->flash.banks;
2480ce50c70SJacob Keller 	enum ice_flash_bank active_bank;
2490ce50c70SJacob Keller 	bool second_bank_active;
2500ce50c70SJacob Keller 	u32 offset, size;
2510ce50c70SJacob Keller 
2520ce50c70SJacob Keller 	switch (module) {
2530ce50c70SJacob Keller 	case ICE_SR_1ST_NVM_BANK_PTR:
2540ce50c70SJacob Keller 		offset = banks->nvm_ptr;
2550ce50c70SJacob Keller 		size = banks->nvm_size;
2560ce50c70SJacob Keller 		active_bank = banks->nvm_bank;
2570ce50c70SJacob Keller 		break;
2580ce50c70SJacob Keller 	case ICE_SR_1ST_OROM_BANK_PTR:
2590ce50c70SJacob Keller 		offset = banks->orom_ptr;
2600ce50c70SJacob Keller 		size = banks->orom_size;
2610ce50c70SJacob Keller 		active_bank = banks->orom_bank;
2620ce50c70SJacob Keller 		break;
2630ce50c70SJacob Keller 	case ICE_SR_NETLIST_BANK_PTR:
2640ce50c70SJacob Keller 		offset = banks->netlist_ptr;
2650ce50c70SJacob Keller 		size = banks->netlist_size;
2660ce50c70SJacob Keller 		active_bank = banks->netlist_bank;
2670ce50c70SJacob Keller 		break;
2680ce50c70SJacob Keller 	default:
2690ce50c70SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Unexpected value for flash module: 0x%04x\n", module);
2700ce50c70SJacob Keller 		return 0;
2710ce50c70SJacob Keller 	}
2720ce50c70SJacob Keller 
2730ce50c70SJacob Keller 	switch (active_bank) {
2740ce50c70SJacob Keller 	case ICE_1ST_FLASH_BANK:
2750ce50c70SJacob Keller 		second_bank_active = false;
2760ce50c70SJacob Keller 		break;
2770ce50c70SJacob Keller 	case ICE_2ND_FLASH_BANK:
2780ce50c70SJacob Keller 		second_bank_active = true;
2790ce50c70SJacob Keller 		break;
2800ce50c70SJacob Keller 	default:
2810ce50c70SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Unexpected value for active flash bank: %u\n",
2820ce50c70SJacob Keller 			  active_bank);
2830ce50c70SJacob Keller 		return 0;
2840ce50c70SJacob Keller 	}
2850ce50c70SJacob Keller 
2860ce50c70SJacob Keller 	/* The second flash bank is stored immediately following the first
2870ce50c70SJacob Keller 	 * bank. Based on whether the 1st or 2nd bank is active, and whether
2880ce50c70SJacob Keller 	 * we want the active or inactive bank, calculate the desired offset.
2890ce50c70SJacob Keller 	 */
2900ce50c70SJacob Keller 	switch (bank) {
2910ce50c70SJacob Keller 	case ICE_ACTIVE_FLASH_BANK:
2920ce50c70SJacob Keller 		return offset + (second_bank_active ? size : 0);
2930ce50c70SJacob Keller 	case ICE_INACTIVE_FLASH_BANK:
2940ce50c70SJacob Keller 		return offset + (second_bank_active ? 0 : size);
2950ce50c70SJacob Keller 	}
2960ce50c70SJacob Keller 
2970ce50c70SJacob Keller 	ice_debug(hw, ICE_DBG_NVM, "Unexpected value for flash bank selection: %u\n", bank);
2980ce50c70SJacob Keller 	return 0;
2990ce50c70SJacob Keller }
3000ce50c70SJacob Keller 
3010ce50c70SJacob Keller /**
3020ce50c70SJacob Keller  * ice_read_flash_module - Read a word from one of the main NVM modules
3030ce50c70SJacob Keller  * @hw: pointer to the HW structure
3040ce50c70SJacob Keller  * @bank: which bank of the module to read
3050ce50c70SJacob Keller  * @module: the module to read
3060ce50c70SJacob Keller  * @offset: the offset into the module in bytes
3070ce50c70SJacob Keller  * @data: storage for the word read from the flash
3080ce50c70SJacob Keller  * @length: bytes of data to read
3090ce50c70SJacob Keller  *
3100ce50c70SJacob Keller  * Read data from the specified flash module. The bank parameter indicates
3110ce50c70SJacob Keller  * whether or not to read from the active bank or the inactive bank of that
3120ce50c70SJacob Keller  * module.
3130ce50c70SJacob Keller  *
3140ce50c70SJacob Keller  * The word will be read using flat NVM access, and relies on the
3150ce50c70SJacob Keller  * hw->flash.banks data being setup by ice_determine_active_flash_banks()
3160ce50c70SJacob Keller  * during initialization.
3170ce50c70SJacob Keller  */
3185e24d598STony Nguyen static int
3190ce50c70SJacob Keller ice_read_flash_module(struct ice_hw *hw, enum ice_bank_select bank, u16 module,
3200ce50c70SJacob Keller 		      u32 offset, u8 *data, u32 length)
3210ce50c70SJacob Keller {
3225e24d598STony Nguyen 	int status;
3230ce50c70SJacob Keller 	u32 start;
3240ce50c70SJacob Keller 
3250ce50c70SJacob Keller 	start = ice_get_flash_bank_offset(hw, bank, module);
3260ce50c70SJacob Keller 	if (!start) {
3270ce50c70SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Unable to calculate flash bank offset for module 0x%04x\n",
3280ce50c70SJacob Keller 			  module);
329d54699e2STony Nguyen 		return -EINVAL;
3300ce50c70SJacob Keller 	}
3310ce50c70SJacob Keller 
3320ce50c70SJacob Keller 	status = ice_acquire_nvm(hw, ICE_RES_READ);
3330ce50c70SJacob Keller 	if (status)
3340ce50c70SJacob Keller 		return status;
3350ce50c70SJacob Keller 
3360ce50c70SJacob Keller 	status = ice_read_flat_nvm(hw, start + offset, &length, data, false);
3370ce50c70SJacob Keller 
3380ce50c70SJacob Keller 	ice_release_nvm(hw);
3390ce50c70SJacob Keller 
3400ce50c70SJacob Keller 	return status;
3410ce50c70SJacob Keller }
3420ce50c70SJacob Keller 
3430ce50c70SJacob Keller /**
3440ce50c70SJacob Keller  * ice_read_nvm_module - Read from the active main NVM module
3450ce50c70SJacob Keller  * @hw: pointer to the HW structure
3460ce50c70SJacob Keller  * @bank: whether to read from active or inactive NVM module
3470ce50c70SJacob Keller  * @offset: offset into the NVM module to read, in words
3480ce50c70SJacob Keller  * @data: storage for returned word value
3490ce50c70SJacob Keller  *
3500ce50c70SJacob Keller  * Read the specified word from the active NVM module. This includes the CSS
3510ce50c70SJacob Keller  * header at the start of the NVM module.
3520ce50c70SJacob Keller  */
3535e24d598STony Nguyen static int
3540ce50c70SJacob Keller ice_read_nvm_module(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
3550ce50c70SJacob Keller {
3560ce50c70SJacob Keller 	__le16 data_local;
3575518ac2aSTony Nguyen 	int status;
3580ce50c70SJacob Keller 
3590ce50c70SJacob Keller 	status = ice_read_flash_module(hw, bank, ICE_SR_1ST_NVM_BANK_PTR, offset * sizeof(u16),
3600ce50c70SJacob Keller 				       (__force u8 *)&data_local, sizeof(u16));
3610ce50c70SJacob Keller 	if (!status)
3620ce50c70SJacob Keller 		*data = le16_to_cpu(data_local);
3630ce50c70SJacob Keller 
3640ce50c70SJacob Keller 	return status;
3650ce50c70SJacob Keller }
3660ce50c70SJacob Keller 
3670ce50c70SJacob Keller /**
3680ce50c70SJacob Keller  * ice_read_nvm_sr_copy - Read a word from the Shadow RAM copy in the NVM bank
3690ce50c70SJacob Keller  * @hw: pointer to the HW structure
3700ce50c70SJacob Keller  * @bank: whether to read from the active or inactive NVM module
3710ce50c70SJacob Keller  * @offset: offset into the Shadow RAM copy to read, in words
3720ce50c70SJacob Keller  * @data: storage for returned word value
3730ce50c70SJacob Keller  *
3740ce50c70SJacob Keller  * Read the specified word from the copy of the Shadow RAM found in the
3750ce50c70SJacob Keller  * specified NVM module.
3760ce50c70SJacob Keller  */
3775e24d598STony Nguyen static int
3780ce50c70SJacob Keller ice_read_nvm_sr_copy(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
3790ce50c70SJacob Keller {
3800ce50c70SJacob Keller 	return ice_read_nvm_module(hw, bank, ICE_NVM_SR_COPY_WORD_OFFSET + offset, data);
3810ce50c70SJacob Keller }
3820ce50c70SJacob Keller 
3830ce50c70SJacob Keller /**
384e120a9abSJacob Keller  * ice_read_netlist_module - Read data from the netlist module area
385e120a9abSJacob Keller  * @hw: pointer to the HW structure
386e120a9abSJacob Keller  * @bank: whether to read from the active or inactive module
387e120a9abSJacob Keller  * @offset: offset into the netlist to read from
388e120a9abSJacob Keller  * @data: storage for returned word value
389e120a9abSJacob Keller  *
390e120a9abSJacob Keller  * Read a word from the specified netlist bank.
391e120a9abSJacob Keller  */
3925e24d598STony Nguyen static int
393e120a9abSJacob Keller ice_read_netlist_module(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
394e120a9abSJacob Keller {
395e120a9abSJacob Keller 	__le16 data_local;
3965518ac2aSTony Nguyen 	int status;
397e120a9abSJacob Keller 
398e120a9abSJacob Keller 	status = ice_read_flash_module(hw, bank, ICE_SR_NETLIST_BANK_PTR, offset * sizeof(u16),
399e120a9abSJacob Keller 				       (__force u8 *)&data_local, sizeof(u16));
400e120a9abSJacob Keller 	if (!status)
401e120a9abSJacob Keller 		*data = le16_to_cpu(data_local);
402e120a9abSJacob Keller 
403e120a9abSJacob Keller 	return status;
404e120a9abSJacob Keller }
405e120a9abSJacob Keller 
406e120a9abSJacob Keller /**
407f31e4b6fSAnirudh Venkataramanan  * ice_read_sr_word - Reads Shadow RAM word and acquire NVM if necessary
408f31e4b6fSAnirudh Venkataramanan  * @hw: pointer to the HW structure
409f31e4b6fSAnirudh Venkataramanan  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
410f31e4b6fSAnirudh Venkataramanan  * @data: word read from the Shadow RAM
411f31e4b6fSAnirudh Venkataramanan  *
412f31e4b6fSAnirudh Venkataramanan  * Reads one 16 bit word from the Shadow RAM using the ice_read_sr_word_aq.
413f31e4b6fSAnirudh Venkataramanan  */
4145e24d598STony Nguyen int ice_read_sr_word(struct ice_hw *hw, u16 offset, u16 *data)
415f31e4b6fSAnirudh Venkataramanan {
4165e24d598STony Nguyen 	int status;
417f31e4b6fSAnirudh Venkataramanan 
418f31e4b6fSAnirudh Venkataramanan 	status = ice_acquire_nvm(hw, ICE_RES_READ);
419f31e4b6fSAnirudh Venkataramanan 	if (!status) {
420f31e4b6fSAnirudh Venkataramanan 		status = ice_read_sr_word_aq(hw, offset, data);
421f31e4b6fSAnirudh Venkataramanan 		ice_release_nvm(hw);
422f31e4b6fSAnirudh Venkataramanan 	}
423f31e4b6fSAnirudh Venkataramanan 
424f31e4b6fSAnirudh Venkataramanan 	return status;
425f31e4b6fSAnirudh Venkataramanan }
426f31e4b6fSAnirudh Venkataramanan 
427f31e4b6fSAnirudh Venkataramanan /**
428e961b679SJacob Keller  * ice_get_pfa_module_tlv - Reads sub module TLV from NVM PFA
429e961b679SJacob Keller  * @hw: pointer to hardware structure
430e961b679SJacob Keller  * @module_tlv: pointer to module TLV to return
431e961b679SJacob Keller  * @module_tlv_len: pointer to module TLV length to return
432e961b679SJacob Keller  * @module_type: module type requested
433e961b679SJacob Keller  *
434e961b679SJacob Keller  * Finds the requested sub module TLV type from the Preserved Field
435e961b679SJacob Keller  * Area (PFA) and returns the TLV pointer and length. The caller can
436e961b679SJacob Keller  * use these to read the variable length TLV value.
437e961b679SJacob Keller  */
4385e24d598STony Nguyen int
439e961b679SJacob Keller ice_get_pfa_module_tlv(struct ice_hw *hw, u16 *module_tlv, u16 *module_tlv_len,
440e961b679SJacob Keller 		       u16 module_type)
441e961b679SJacob Keller {
442e961b679SJacob Keller 	u16 pfa_len, pfa_ptr;
443e961b679SJacob Keller 	u16 next_tlv;
4445518ac2aSTony Nguyen 	int status;
445e961b679SJacob Keller 
446e961b679SJacob Keller 	status = ice_read_sr_word(hw, ICE_SR_PFA_PTR, &pfa_ptr);
447e961b679SJacob Keller 	if (status) {
448e961b679SJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Preserved Field Array pointer.\n");
449e961b679SJacob Keller 		return status;
450e961b679SJacob Keller 	}
451e961b679SJacob Keller 	status = ice_read_sr_word(hw, pfa_ptr, &pfa_len);
452e961b679SJacob Keller 	if (status) {
453e961b679SJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PFA length.\n");
454e961b679SJacob Keller 		return status;
455e961b679SJacob Keller 	}
456e961b679SJacob Keller 	/* Starting with first TLV after PFA length, iterate through the list
457e961b679SJacob Keller 	 * of TLVs to find the requested one.
458e961b679SJacob Keller 	 */
459e961b679SJacob Keller 	next_tlv = pfa_ptr + 1;
460e961b679SJacob Keller 	while (next_tlv < pfa_ptr + pfa_len) {
461e961b679SJacob Keller 		u16 tlv_sub_module_type;
462e961b679SJacob Keller 		u16 tlv_len;
463e961b679SJacob Keller 
464e961b679SJacob Keller 		/* Read TLV type */
465e961b679SJacob Keller 		status = ice_read_sr_word(hw, next_tlv, &tlv_sub_module_type);
466e961b679SJacob Keller 		if (status) {
467e961b679SJacob Keller 			ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV type.\n");
468e961b679SJacob Keller 			break;
469e961b679SJacob Keller 		}
470e961b679SJacob Keller 		/* Read TLV length */
471e961b679SJacob Keller 		status = ice_read_sr_word(hw, next_tlv + 1, &tlv_len);
472e961b679SJacob Keller 		if (status) {
473e961b679SJacob Keller 			ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV length.\n");
474e961b679SJacob Keller 			break;
475e961b679SJacob Keller 		}
476e961b679SJacob Keller 		if (tlv_sub_module_type == module_type) {
477e961b679SJacob Keller 			if (tlv_len) {
478e961b679SJacob Keller 				*module_tlv = next_tlv;
479e961b679SJacob Keller 				*module_tlv_len = tlv_len;
480e961b679SJacob Keller 				return 0;
481e961b679SJacob Keller 			}
482d54699e2STony Nguyen 			return -EINVAL;
483e961b679SJacob Keller 		}
484e961b679SJacob Keller 		/* Check next TLV, i.e. current TLV pointer + length + 2 words
485e961b679SJacob Keller 		 * (for current TLV's type and length)
486e961b679SJacob Keller 		 */
487e961b679SJacob Keller 		next_tlv = next_tlv + tlv_len + 2;
488e961b679SJacob Keller 	}
489e961b679SJacob Keller 	/* Module does not exist */
490d54699e2STony Nguyen 	return -ENOENT;
491e961b679SJacob Keller }
492e961b679SJacob Keller 
493e961b679SJacob Keller /**
494e961b679SJacob Keller  * ice_read_pba_string - Reads part number string from NVM
495e961b679SJacob Keller  * @hw: pointer to hardware structure
496e961b679SJacob Keller  * @pba_num: stores the part number string from the NVM
497e961b679SJacob Keller  * @pba_num_size: part number string buffer length
498e961b679SJacob Keller  *
499e961b679SJacob Keller  * Reads the part number string from the NVM.
500e961b679SJacob Keller  */
5015518ac2aSTony Nguyen int ice_read_pba_string(struct ice_hw *hw, u8 *pba_num, u32 pba_num_size)
502e961b679SJacob Keller {
503e961b679SJacob Keller 	u16 pba_tlv, pba_tlv_len;
504e961b679SJacob Keller 	u16 pba_word, pba_size;
5055518ac2aSTony Nguyen 	int status;
506e961b679SJacob Keller 	u16 i;
507e961b679SJacob Keller 
508e961b679SJacob Keller 	status = ice_get_pfa_module_tlv(hw, &pba_tlv, &pba_tlv_len,
509e961b679SJacob Keller 					ICE_SR_PBA_BLOCK_PTR);
510e961b679SJacob Keller 	if (status) {
511e961b679SJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block TLV.\n");
512e961b679SJacob Keller 		return status;
513e961b679SJacob Keller 	}
514e961b679SJacob Keller 
515e961b679SJacob Keller 	/* pba_size is the next word */
516e961b679SJacob Keller 	status = ice_read_sr_word(hw, (pba_tlv + 2), &pba_size);
517e961b679SJacob Keller 	if (status) {
518e961b679SJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Section size.\n");
519e961b679SJacob Keller 		return status;
520e961b679SJacob Keller 	}
521e961b679SJacob Keller 
522e961b679SJacob Keller 	if (pba_tlv_len < pba_size) {
523e961b679SJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Invalid PBA Block TLV size.\n");
524d54699e2STony Nguyen 		return -EINVAL;
525e961b679SJacob Keller 	}
526e961b679SJacob Keller 
527e961b679SJacob Keller 	/* Subtract one to get PBA word count (PBA Size word is included in
528e961b679SJacob Keller 	 * total size)
529e961b679SJacob Keller 	 */
530e961b679SJacob Keller 	pba_size--;
531e961b679SJacob Keller 	if (pba_num_size < (((u32)pba_size * 2) + 1)) {
532e961b679SJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Buffer too small for PBA data.\n");
533d54699e2STony Nguyen 		return -EINVAL;
534e961b679SJacob Keller 	}
535e961b679SJacob Keller 
536e961b679SJacob Keller 	for (i = 0; i < pba_size; i++) {
537e961b679SJacob Keller 		status = ice_read_sr_word(hw, (pba_tlv + 2 + 1) + i, &pba_word);
538e961b679SJacob Keller 		if (status) {
539e961b679SJacob Keller 			ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block word %d.\n", i);
540e961b679SJacob Keller 			return status;
541e961b679SJacob Keller 		}
542e961b679SJacob Keller 
543e961b679SJacob Keller 		pba_num[(i * 2)] = (pba_word >> 8) & 0xFF;
544e961b679SJacob Keller 		pba_num[(i * 2) + 1] = pba_word & 0xFF;
545e961b679SJacob Keller 	}
546e961b679SJacob Keller 	pba_num[(pba_size * 2)] = '\0';
547e961b679SJacob Keller 
548e961b679SJacob Keller 	return status;
549e961b679SJacob Keller }
550e961b679SJacob Keller 
551e961b679SJacob Keller /**
5529af368faSJacob Keller  * ice_get_nvm_ver_info - Read NVM version information
5539af368faSJacob Keller  * @hw: pointer to the HW struct
5540ce50c70SJacob Keller  * @bank: whether to read from the active or inactive flash bank
5559af368faSJacob Keller  * @nvm: pointer to NVM info structure
5569af368faSJacob Keller  *
5579af368faSJacob Keller  * Read the NVM EETRACK ID and map version of the main NVM image bank, filling
5589af368faSJacob Keller  * in the NVM info structure.
5599af368faSJacob Keller  */
5605e24d598STony Nguyen static int
5610ce50c70SJacob Keller ice_get_nvm_ver_info(struct ice_hw *hw, enum ice_bank_select bank, struct ice_nvm_info *nvm)
5629af368faSJacob Keller {
5639af368faSJacob Keller 	u16 eetrack_lo, eetrack_hi, ver;
5645e24d598STony Nguyen 	int status;
5659af368faSJacob Keller 
5660ce50c70SJacob Keller 	status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_DEV_STARTER_VER, &ver);
5679af368faSJacob Keller 	if (status) {
5689af368faSJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read DEV starter version.\n");
5699af368faSJacob Keller 		return status;
5709af368faSJacob Keller 	}
5710ce50c70SJacob Keller 
5729af368faSJacob Keller 	nvm->major = (ver & ICE_NVM_VER_HI_MASK) >> ICE_NVM_VER_HI_SHIFT;
5739af368faSJacob Keller 	nvm->minor = (ver & ICE_NVM_VER_LO_MASK) >> ICE_NVM_VER_LO_SHIFT;
5749af368faSJacob Keller 
5750ce50c70SJacob Keller 	status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_EETRACK_LO, &eetrack_lo);
5769af368faSJacob Keller 	if (status) {
5779af368faSJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read EETRACK lo.\n");
5789af368faSJacob Keller 		return status;
5799af368faSJacob Keller 	}
5800ce50c70SJacob Keller 	status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_EETRACK_HI, &eetrack_hi);
5819af368faSJacob Keller 	if (status) {
5829af368faSJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read EETRACK hi.\n");
5839af368faSJacob Keller 		return status;
5849af368faSJacob Keller 	}
5859af368faSJacob Keller 
5869af368faSJacob Keller 	nvm->eetrack = (eetrack_hi << 16) | eetrack_lo;
5879af368faSJacob Keller 
5889af368faSJacob Keller 	return 0;
5899af368faSJacob Keller }
5909af368faSJacob Keller 
5919af368faSJacob Keller /**
5922c4fe41dSJacob Keller  * ice_get_inactive_nvm_ver - Read Option ROM version from the inactive bank
5932c4fe41dSJacob Keller  * @hw: pointer to the HW structure
5942c4fe41dSJacob Keller  * @nvm: storage for Option ROM version information
5952c4fe41dSJacob Keller  *
5962c4fe41dSJacob Keller  * Reads the NVM EETRACK ID, Map version, and security revision of the
5972c4fe41dSJacob Keller  * inactive NVM bank. Used to access version data for a pending update that
5982c4fe41dSJacob Keller  * has not yet been activated.
5992c4fe41dSJacob Keller  */
6005e24d598STony Nguyen int ice_get_inactive_nvm_ver(struct ice_hw *hw, struct ice_nvm_info *nvm)
6012c4fe41dSJacob Keller {
6022c4fe41dSJacob Keller 	return ice_get_nvm_ver_info(hw, ICE_INACTIVE_FLASH_BANK, nvm);
6032c4fe41dSJacob Keller }
6042c4fe41dSJacob Keller 
6052c4fe41dSJacob Keller /**
606e67fbcfbSJacob Keller  * ice_get_orom_civd_data - Get the combo version information from Option ROM
607d4e87444SJacob Keller  * @hw: pointer to the HW struct
608e67fbcfbSJacob Keller  * @bank: whether to read from the active or inactive flash module
609e67fbcfbSJacob Keller  * @civd: storage for the Option ROM CIVD data.
610d4e87444SJacob Keller  *
611e67fbcfbSJacob Keller  * Searches through the Option ROM flash contents to locate the CIVD data for
612e67fbcfbSJacob Keller  * the image.
613d4e87444SJacob Keller  */
6145e24d598STony Nguyen static int
615e67fbcfbSJacob Keller ice_get_orom_civd_data(struct ice_hw *hw, enum ice_bank_select bank,
616e67fbcfbSJacob Keller 		       struct ice_orom_civd_info *civd)
617d4e87444SJacob Keller {
618*af18d886SJacob Keller 	u8 *orom_data;
6195e24d598STony Nguyen 	int status;
620e67fbcfbSJacob Keller 	u32 offset;
621e67fbcfbSJacob Keller 
622e67fbcfbSJacob Keller 	/* The CIVD section is located in the Option ROM aligned to 512 bytes.
623e67fbcfbSJacob Keller 	 * The first 4 bytes must contain the ASCII characters "$CIV".
624e67fbcfbSJacob Keller 	 * A simple modulo 256 sum of all of the bytes of the structure must
625e67fbcfbSJacob Keller 	 * equal 0.
626*af18d886SJacob Keller 	 *
627*af18d886SJacob Keller 	 * The exact location is unknown and varies between images but is
628*af18d886SJacob Keller 	 * usually somewhere in the middle of the bank. We need to scan the
629*af18d886SJacob Keller 	 * Option ROM bank to locate it.
630*af18d886SJacob Keller 	 *
631*af18d886SJacob Keller 	 * It's significantly faster to read the entire Option ROM up front
632*af18d886SJacob Keller 	 * using the maximum page size, than to read each possible location
633*af18d886SJacob Keller 	 * with a separate firmware command.
634e67fbcfbSJacob Keller 	 */
635*af18d886SJacob Keller 	orom_data = vzalloc(hw->flash.banks.orom_size);
636*af18d886SJacob Keller 	if (!orom_data)
637*af18d886SJacob Keller 		return -ENOMEM;
638e67fbcfbSJacob Keller 
639*af18d886SJacob Keller 	status = ice_read_flash_module(hw, bank, ICE_SR_1ST_OROM_BANK_PTR, 0,
640*af18d886SJacob Keller 				       orom_data, hw->flash.banks.orom_size);
641e67fbcfbSJacob Keller 	if (status) {
642*af18d886SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Unable to read Option ROM data\n");
643e67fbcfbSJacob Keller 		return status;
644e67fbcfbSJacob Keller 	}
645e67fbcfbSJacob Keller 
646*af18d886SJacob Keller 	/* Scan the memory buffer to locate the CIVD data section */
647*af18d886SJacob Keller 	for (offset = 0; (offset + 512) <= hw->flash.banks.orom_size; offset += 512) {
648*af18d886SJacob Keller 		struct ice_orom_civd_info *tmp;
649*af18d886SJacob Keller 		u8 sum = 0, i;
650*af18d886SJacob Keller 
651*af18d886SJacob Keller 		tmp = (struct ice_orom_civd_info *)&orom_data[offset];
652*af18d886SJacob Keller 
653e67fbcfbSJacob Keller 		/* Skip forward until we find a matching signature */
654*af18d886SJacob Keller 		if (memcmp("$CIV", tmp->signature, sizeof(tmp->signature)) != 0)
655e67fbcfbSJacob Keller 			continue;
656e67fbcfbSJacob Keller 
657*af18d886SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Found CIVD section at offset %u\n",
658*af18d886SJacob Keller 			  offset);
659*af18d886SJacob Keller 
660e67fbcfbSJacob Keller 		/* Verify that the simple checksum is zero */
661*af18d886SJacob Keller 		for (i = 0; i < sizeof(*tmp); i++)
662b370245bSBruce Allan 			/* cppcheck-suppress objectIndex */
663*af18d886SJacob Keller 			sum += ((u8 *)tmp)[i];
664e67fbcfbSJacob Keller 
665e67fbcfbSJacob Keller 		if (sum) {
666e67fbcfbSJacob Keller 			ice_debug(hw, ICE_DBG_NVM, "Found CIVD data with invalid checksum of %u\n",
667e67fbcfbSJacob Keller 				  sum);
668*af18d886SJacob Keller 			goto err_invalid_checksum;
669e67fbcfbSJacob Keller 		}
670e67fbcfbSJacob Keller 
671*af18d886SJacob Keller 		*civd = *tmp;
672*af18d886SJacob Keller 		vfree(orom_data);
673e67fbcfbSJacob Keller 		return 0;
674e67fbcfbSJacob Keller 	}
675e67fbcfbSJacob Keller 
676*af18d886SJacob Keller 	ice_debug(hw, ICE_DBG_NVM, "Unable to locate CIVD data within the Option ROM\n");
677*af18d886SJacob Keller 
678*af18d886SJacob Keller err_invalid_checksum:
679*af18d886SJacob Keller 	vfree(orom_data);
680d54699e2STony Nguyen 	return -EIO;
681e67fbcfbSJacob Keller }
682e67fbcfbSJacob Keller 
683e67fbcfbSJacob Keller /**
684e67fbcfbSJacob Keller  * ice_get_orom_ver_info - Read Option ROM version information
685e67fbcfbSJacob Keller  * @hw: pointer to the HW struct
686e67fbcfbSJacob Keller  * @bank: whether to read from the active or inactive flash module
687e67fbcfbSJacob Keller  * @orom: pointer to Option ROM info structure
688e67fbcfbSJacob Keller  *
689e67fbcfbSJacob Keller  * Read Option ROM version and security revision from the Option ROM flash
690e67fbcfbSJacob Keller  * section.
691e67fbcfbSJacob Keller  */
6925e24d598STony Nguyen static int
693e67fbcfbSJacob Keller ice_get_orom_ver_info(struct ice_hw *hw, enum ice_bank_select bank, struct ice_orom_info *orom)
694e67fbcfbSJacob Keller {
695e67fbcfbSJacob Keller 	struct ice_orom_civd_info civd;
696d4e87444SJacob Keller 	u32 combo_ver;
6975518ac2aSTony Nguyen 	int status;
698d4e87444SJacob Keller 
699e67fbcfbSJacob Keller 	status = ice_get_orom_civd_data(hw, bank, &civd);
700d4e87444SJacob Keller 	if (status) {
701e67fbcfbSJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to locate valid Option ROM CIVD data\n");
702d4e87444SJacob Keller 		return status;
703d4e87444SJacob Keller 	}
704d4e87444SJacob Keller 
705e67fbcfbSJacob Keller 	combo_ver = le32_to_cpu(civd.combo_ver);
706d4e87444SJacob Keller 
707e67fbcfbSJacob Keller 	orom->major = (u8)((combo_ver & ICE_OROM_VER_MASK) >> ICE_OROM_VER_SHIFT);
708d4e87444SJacob Keller 	orom->patch = (u8)(combo_ver & ICE_OROM_VER_PATCH_MASK);
709e67fbcfbSJacob Keller 	orom->build = (u16)((combo_ver & ICE_OROM_VER_BUILD_MASK) >> ICE_OROM_VER_BUILD_SHIFT);
710d4e87444SJacob Keller 
711d4e87444SJacob Keller 	return 0;
712d4e87444SJacob Keller }
713d4e87444SJacob Keller 
714d4e87444SJacob Keller /**
715e67fbcfbSJacob Keller  * ice_get_inactive_orom_ver - Read Option ROM version from the inactive bank
716e67fbcfbSJacob Keller  * @hw: pointer to the HW structure
717e67fbcfbSJacob Keller  * @orom: storage for Option ROM version information
718e67fbcfbSJacob Keller  *
719e67fbcfbSJacob Keller  * Reads the Option ROM version and security revision data for the inactive
720e67fbcfbSJacob Keller  * section of flash. Used to access version data for a pending update that has
721e67fbcfbSJacob Keller  * not yet been activated.
722e67fbcfbSJacob Keller  */
7235e24d598STony Nguyen int ice_get_inactive_orom_ver(struct ice_hw *hw, struct ice_orom_info *orom)
724e67fbcfbSJacob Keller {
725e67fbcfbSJacob Keller 	return ice_get_orom_ver_info(hw, ICE_INACTIVE_FLASH_BANK, orom);
726e67fbcfbSJacob Keller }
727e67fbcfbSJacob Keller 
728e67fbcfbSJacob Keller /**
729e120a9abSJacob Keller  * ice_get_netlist_info
730f45a645fSJacob Keller  * @hw: pointer to the HW struct
731e120a9abSJacob Keller  * @bank: whether to read from the active or inactive flash bank
732e120a9abSJacob Keller  * @netlist: pointer to netlist version info structure
733f45a645fSJacob Keller  *
734e120a9abSJacob Keller  * Get the netlist version information from the requested bank. Reads the Link
735e120a9abSJacob Keller  * Topology section to find the Netlist ID block and extract the relevant
736e120a9abSJacob Keller  * information into the netlist version structure.
737f45a645fSJacob Keller  */
7385e24d598STony Nguyen static int
739e120a9abSJacob Keller ice_get_netlist_info(struct ice_hw *hw, enum ice_bank_select bank,
740e120a9abSJacob Keller 		     struct ice_netlist_info *netlist)
741f45a645fSJacob Keller {
742e120a9abSJacob Keller 	u16 module_id, length, node_count, i;
743e120a9abSJacob Keller 	u16 *id_blk;
7445518ac2aSTony Nguyen 	int status;
745f45a645fSJacob Keller 
746e120a9abSJacob Keller 	status = ice_read_netlist_module(hw, bank, ICE_NETLIST_TYPE_OFFSET, &module_id);
747e120a9abSJacob Keller 	if (status)
748e120a9abSJacob Keller 		return status;
749e120a9abSJacob Keller 
750e120a9abSJacob Keller 	if (module_id != ICE_NETLIST_LINK_TOPO_MOD_ID) {
751e120a9abSJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Expected netlist module_id ID of 0x%04x, but got 0x%04x\n",
752e120a9abSJacob Keller 			  ICE_NETLIST_LINK_TOPO_MOD_ID, module_id);
753d54699e2STony Nguyen 		return -EIO;
754f45a645fSJacob Keller 	}
755f45a645fSJacob Keller 
756e120a9abSJacob Keller 	status = ice_read_netlist_module(hw, bank, ICE_LINK_TOPO_MODULE_LEN, &length);
757e120a9abSJacob Keller 	if (status)
758e120a9abSJacob Keller 		return status;
759e120a9abSJacob Keller 
760e120a9abSJacob Keller 	/* sanity check that we have at least enough words to store the netlist ID block */
761e120a9abSJacob Keller 	if (length < ICE_NETLIST_ID_BLK_SIZE) {
762e120a9abSJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Netlist Link Topology module too small. Expected at least %u words, but got %u words.\n",
763e120a9abSJacob Keller 			  ICE_NETLIST_ID_BLK_SIZE, length);
764d54699e2STony Nguyen 		return -EIO;
765e120a9abSJacob Keller 	}
766e120a9abSJacob Keller 
767e120a9abSJacob Keller 	status = ice_read_netlist_module(hw, bank, ICE_LINK_TOPO_NODE_COUNT, &node_count);
768e120a9abSJacob Keller 	if (status)
769e120a9abSJacob Keller 		return status;
770e120a9abSJacob Keller 	node_count &= ICE_LINK_TOPO_NODE_COUNT_M;
771e120a9abSJacob Keller 
772e120a9abSJacob Keller 	id_blk = kcalloc(ICE_NETLIST_ID_BLK_SIZE, sizeof(*id_blk), GFP_KERNEL);
773e120a9abSJacob Keller 	if (!id_blk)
774d54699e2STony Nguyen 		return -ENOMEM;
775e120a9abSJacob Keller 
776e120a9abSJacob Keller 	/* Read out the entire Netlist ID Block at once. */
777e120a9abSJacob Keller 	status = ice_read_flash_module(hw, bank, ICE_SR_NETLIST_BANK_PTR,
778e120a9abSJacob Keller 				       ICE_NETLIST_ID_BLK_OFFSET(node_count) * sizeof(u16),
779e120a9abSJacob Keller 				       (u8 *)id_blk, ICE_NETLIST_ID_BLK_SIZE * sizeof(u16));
780e120a9abSJacob Keller 	if (status)
781f45a645fSJacob Keller 		goto exit_error;
782f45a645fSJacob Keller 
783e120a9abSJacob Keller 	for (i = 0; i < ICE_NETLIST_ID_BLK_SIZE; i++)
784e120a9abSJacob Keller 		id_blk[i] = le16_to_cpu(((__force __le16 *)id_blk)[i]);
785f45a645fSJacob Keller 
786e120a9abSJacob Keller 	netlist->major = id_blk[ICE_NETLIST_ID_BLK_MAJOR_VER_HIGH] << 16 |
787e120a9abSJacob Keller 			 id_blk[ICE_NETLIST_ID_BLK_MAJOR_VER_LOW];
788e120a9abSJacob Keller 	netlist->minor = id_blk[ICE_NETLIST_ID_BLK_MINOR_VER_HIGH] << 16 |
789e120a9abSJacob Keller 			 id_blk[ICE_NETLIST_ID_BLK_MINOR_VER_LOW];
790e120a9abSJacob Keller 	netlist->type = id_blk[ICE_NETLIST_ID_BLK_TYPE_HIGH] << 16 |
791e120a9abSJacob Keller 			id_blk[ICE_NETLIST_ID_BLK_TYPE_LOW];
792e120a9abSJacob Keller 	netlist->rev = id_blk[ICE_NETLIST_ID_BLK_REV_HIGH] << 16 |
793e120a9abSJacob Keller 		       id_blk[ICE_NETLIST_ID_BLK_REV_LOW];
794e120a9abSJacob Keller 	netlist->cust_ver = id_blk[ICE_NETLIST_ID_BLK_CUST_VER];
795f45a645fSJacob Keller 	/* Read the left most 4 bytes of SHA */
796e120a9abSJacob Keller 	netlist->hash = id_blk[ICE_NETLIST_ID_BLK_SHA_HASH_WORD(15)] << 16 |
797e120a9abSJacob Keller 			id_blk[ICE_NETLIST_ID_BLK_SHA_HASH_WORD(14)];
798f45a645fSJacob Keller 
799f45a645fSJacob Keller exit_error:
800e120a9abSJacob Keller 	kfree(id_blk);
801e120a9abSJacob Keller 
802e120a9abSJacob Keller 	return status;
803e120a9abSJacob Keller }
804e120a9abSJacob Keller 
805e120a9abSJacob Keller /**
806e120a9abSJacob Keller  * ice_get_inactive_netlist_ver
807e120a9abSJacob Keller  * @hw: pointer to the HW struct
808e120a9abSJacob Keller  * @netlist: pointer to netlist version info structure
809e120a9abSJacob Keller  *
810e120a9abSJacob Keller  * Read the netlist version data from the inactive netlist bank. Used to
811e120a9abSJacob Keller  * extract version data of a pending flash update in order to display the
812e120a9abSJacob Keller  * version data.
813e120a9abSJacob Keller  */
8145e24d598STony Nguyen int ice_get_inactive_netlist_ver(struct ice_hw *hw, struct ice_netlist_info *netlist)
815e120a9abSJacob Keller {
816e120a9abSJacob Keller 	return ice_get_netlist_info(hw, ICE_INACTIVE_FLASH_BANK, netlist);
817f45a645fSJacob Keller }
818f45a645fSJacob Keller 
819f45a645fSJacob Keller /**
82081f07491SJacob Keller  * ice_discover_flash_size - Discover the available flash size.
82181f07491SJacob Keller  * @hw: pointer to the HW struct
82281f07491SJacob Keller  *
82381f07491SJacob Keller  * The device flash could be up to 16MB in size. However, it is possible that
82481f07491SJacob Keller  * the actual size is smaller. Use bisection to determine the accessible size
82581f07491SJacob Keller  * of flash memory.
82681f07491SJacob Keller  */
8275e24d598STony Nguyen static int ice_discover_flash_size(struct ice_hw *hw)
82881f07491SJacob Keller {
82981f07491SJacob Keller 	u32 min_size = 0, max_size = ICE_AQC_NVM_MAX_OFFSET + 1;
8305e24d598STony Nguyen 	int status;
83181f07491SJacob Keller 
83281f07491SJacob Keller 	status = ice_acquire_nvm(hw, ICE_RES_READ);
83381f07491SJacob Keller 	if (status)
83481f07491SJacob Keller 		return status;
83581f07491SJacob Keller 
83681f07491SJacob Keller 	while ((max_size - min_size) > 1) {
83781f07491SJacob Keller 		u32 offset = (max_size + min_size) / 2;
83881f07491SJacob Keller 		u32 len = 1;
83981f07491SJacob Keller 		u8 data;
84081f07491SJacob Keller 
84181f07491SJacob Keller 		status = ice_read_flat_nvm(hw, offset, &len, &data, false);
842d54699e2STony Nguyen 		if (status == -EIO &&
84381f07491SJacob Keller 		    hw->adminq.sq_last_status == ICE_AQ_RC_EINVAL) {
8449228d8b2SJacob Keller 			ice_debug(hw, ICE_DBG_NVM, "%s: New upper bound of %u bytes\n",
84581f07491SJacob Keller 				  __func__, offset);
84681f07491SJacob Keller 			status = 0;
84781f07491SJacob Keller 			max_size = offset;
84881f07491SJacob Keller 		} else if (!status) {
8499228d8b2SJacob Keller 			ice_debug(hw, ICE_DBG_NVM, "%s: New lower bound of %u bytes\n",
85081f07491SJacob Keller 				  __func__, offset);
85181f07491SJacob Keller 			min_size = offset;
85281f07491SJacob Keller 		} else {
85381f07491SJacob Keller 			/* an unexpected error occurred */
85481f07491SJacob Keller 			goto err_read_flat_nvm;
85581f07491SJacob Keller 		}
85681f07491SJacob Keller 	}
85781f07491SJacob Keller 
8589228d8b2SJacob Keller 	ice_debug(hw, ICE_DBG_NVM, "Predicted flash size is %u bytes\n", max_size);
85981f07491SJacob Keller 
8609af368faSJacob Keller 	hw->flash.flash_size = max_size;
86181f07491SJacob Keller 
86281f07491SJacob Keller err_read_flat_nvm:
86381f07491SJacob Keller 	ice_release_nvm(hw);
86481f07491SJacob Keller 
86581f07491SJacob Keller 	return status;
86681f07491SJacob Keller }
86781f07491SJacob Keller 
86881f07491SJacob Keller /**
8691fa95e01SJacob Keller  * ice_read_sr_pointer - Read the value of a Shadow RAM pointer word
8701fa95e01SJacob Keller  * @hw: pointer to the HW structure
8711fa95e01SJacob Keller  * @offset: the word offset of the Shadow RAM word to read
8721fa95e01SJacob Keller  * @pointer: pointer value read from Shadow RAM
8731fa95e01SJacob Keller  *
8741fa95e01SJacob Keller  * Read the given Shadow RAM word, and convert it to a pointer value specified
8751fa95e01SJacob Keller  * in bytes. This function assumes the specified offset is a valid pointer
8761fa95e01SJacob Keller  * word.
8771fa95e01SJacob Keller  *
8781fa95e01SJacob Keller  * Each pointer word specifies whether it is stored in word size or 4KB
8791fa95e01SJacob Keller  * sector size by using the highest bit. The reported pointer value will be in
8801fa95e01SJacob Keller  * bytes, intended for flat NVM reads.
8811fa95e01SJacob Keller  */
8825518ac2aSTony Nguyen static int ice_read_sr_pointer(struct ice_hw *hw, u16 offset, u32 *pointer)
8831fa95e01SJacob Keller {
8845e24d598STony Nguyen 	int status;
8851fa95e01SJacob Keller 	u16 value;
8861fa95e01SJacob Keller 
8871fa95e01SJacob Keller 	status = ice_read_sr_word(hw, offset, &value);
8881fa95e01SJacob Keller 	if (status)
8891fa95e01SJacob Keller 		return status;
8901fa95e01SJacob Keller 
8911fa95e01SJacob Keller 	/* Determine if the pointer is in 4KB or word units */
8921fa95e01SJacob Keller 	if (value & ICE_SR_NVM_PTR_4KB_UNITS)
8931fa95e01SJacob Keller 		*pointer = (value & ~ICE_SR_NVM_PTR_4KB_UNITS) * 4 * 1024;
8941fa95e01SJacob Keller 	else
8951fa95e01SJacob Keller 		*pointer = value * 2;
8961fa95e01SJacob Keller 
8971fa95e01SJacob Keller 	return 0;
8981fa95e01SJacob Keller }
8991fa95e01SJacob Keller 
9001fa95e01SJacob Keller /**
9011fa95e01SJacob Keller  * ice_read_sr_area_size - Read an area size from a Shadow RAM word
9021fa95e01SJacob Keller  * @hw: pointer to the HW structure
9031fa95e01SJacob Keller  * @offset: the word offset of the Shadow RAM to read
9041fa95e01SJacob Keller  * @size: size value read from the Shadow RAM
9051fa95e01SJacob Keller  *
9061fa95e01SJacob Keller  * Read the given Shadow RAM word, and convert it to an area size value
9071fa95e01SJacob Keller  * specified in bytes. This function assumes the specified offset is a valid
9081fa95e01SJacob Keller  * area size word.
9091fa95e01SJacob Keller  *
9101fa95e01SJacob Keller  * Each area size word is specified in 4KB sector units. This function reports
9111fa95e01SJacob Keller  * the size in bytes, intended for flat NVM reads.
9121fa95e01SJacob Keller  */
9135518ac2aSTony Nguyen static int ice_read_sr_area_size(struct ice_hw *hw, u16 offset, u32 *size)
9141fa95e01SJacob Keller {
9155e24d598STony Nguyen 	int status;
9161fa95e01SJacob Keller 	u16 value;
9171fa95e01SJacob Keller 
9181fa95e01SJacob Keller 	status = ice_read_sr_word(hw, offset, &value);
9191fa95e01SJacob Keller 	if (status)
9201fa95e01SJacob Keller 		return status;
9211fa95e01SJacob Keller 
9221fa95e01SJacob Keller 	/* Area sizes are always specified in 4KB units */
9231fa95e01SJacob Keller 	*size = value * 4 * 1024;
9241fa95e01SJacob Keller 
9251fa95e01SJacob Keller 	return 0;
9261fa95e01SJacob Keller }
9271fa95e01SJacob Keller 
9281fa95e01SJacob Keller /**
9291fa95e01SJacob Keller  * ice_determine_active_flash_banks - Discover active bank for each module
9301fa95e01SJacob Keller  * @hw: pointer to the HW struct
9311fa95e01SJacob Keller  *
9321fa95e01SJacob Keller  * Read the Shadow RAM control word and determine which banks are active for
9331fa95e01SJacob Keller  * the NVM, OROM, and Netlist modules. Also read and calculate the associated
9341fa95e01SJacob Keller  * pointer and size. These values are then cached into the ice_flash_info
9351fa95e01SJacob Keller  * structure for later use in order to calculate the correct offset to read
9361fa95e01SJacob Keller  * from the active module.
9371fa95e01SJacob Keller  */
9385518ac2aSTony Nguyen static int ice_determine_active_flash_banks(struct ice_hw *hw)
9391fa95e01SJacob Keller {
9401fa95e01SJacob Keller 	struct ice_bank_info *banks = &hw->flash.banks;
9411fa95e01SJacob Keller 	u16 ctrl_word;
9425518ac2aSTony Nguyen 	int status;
9431fa95e01SJacob Keller 
9441fa95e01SJacob Keller 	status = ice_read_sr_word(hw, ICE_SR_NVM_CTRL_WORD, &ctrl_word);
9451fa95e01SJacob Keller 	if (status) {
9461fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read the Shadow RAM control word\n");
9471fa95e01SJacob Keller 		return status;
9481fa95e01SJacob Keller 	}
9491fa95e01SJacob Keller 
9501fa95e01SJacob Keller 	/* Check that the control word indicates validity */
9511fa95e01SJacob Keller 	if ((ctrl_word & ICE_SR_CTRL_WORD_1_M) >> ICE_SR_CTRL_WORD_1_S != ICE_SR_CTRL_WORD_VALID) {
9521fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Shadow RAM control word is invalid\n");
953d54699e2STony Nguyen 		return -EIO;
9541fa95e01SJacob Keller 	}
9551fa95e01SJacob Keller 
9561fa95e01SJacob Keller 	if (!(ctrl_word & ICE_SR_CTRL_WORD_NVM_BANK))
9571fa95e01SJacob Keller 		banks->nvm_bank = ICE_1ST_FLASH_BANK;
9581fa95e01SJacob Keller 	else
9591fa95e01SJacob Keller 		banks->nvm_bank = ICE_2ND_FLASH_BANK;
9601fa95e01SJacob Keller 
9611fa95e01SJacob Keller 	if (!(ctrl_word & ICE_SR_CTRL_WORD_OROM_BANK))
9621fa95e01SJacob Keller 		banks->orom_bank = ICE_1ST_FLASH_BANK;
9631fa95e01SJacob Keller 	else
9641fa95e01SJacob Keller 		banks->orom_bank = ICE_2ND_FLASH_BANK;
9651fa95e01SJacob Keller 
9661fa95e01SJacob Keller 	if (!(ctrl_word & ICE_SR_CTRL_WORD_NETLIST_BANK))
9671fa95e01SJacob Keller 		banks->netlist_bank = ICE_1ST_FLASH_BANK;
9681fa95e01SJacob Keller 	else
9691fa95e01SJacob Keller 		banks->netlist_bank = ICE_2ND_FLASH_BANK;
9701fa95e01SJacob Keller 
9711fa95e01SJacob Keller 	status = ice_read_sr_pointer(hw, ICE_SR_1ST_NVM_BANK_PTR, &banks->nvm_ptr);
9721fa95e01SJacob Keller 	if (status) {
9731fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank pointer\n");
9741fa95e01SJacob Keller 		return status;
9751fa95e01SJacob Keller 	}
9761fa95e01SJacob Keller 
9771fa95e01SJacob Keller 	status = ice_read_sr_area_size(hw, ICE_SR_NVM_BANK_SIZE, &banks->nvm_size);
9781fa95e01SJacob Keller 	if (status) {
9791fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank area size\n");
9801fa95e01SJacob Keller 		return status;
9811fa95e01SJacob Keller 	}
9821fa95e01SJacob Keller 
9831fa95e01SJacob Keller 	status = ice_read_sr_pointer(hw, ICE_SR_1ST_OROM_BANK_PTR, &banks->orom_ptr);
9841fa95e01SJacob Keller 	if (status) {
9851fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank pointer\n");
9861fa95e01SJacob Keller 		return status;
9871fa95e01SJacob Keller 	}
9881fa95e01SJacob Keller 
9891fa95e01SJacob Keller 	status = ice_read_sr_area_size(hw, ICE_SR_OROM_BANK_SIZE, &banks->orom_size);
9901fa95e01SJacob Keller 	if (status) {
9911fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank area size\n");
9921fa95e01SJacob Keller 		return status;
9931fa95e01SJacob Keller 	}
9941fa95e01SJacob Keller 
9951fa95e01SJacob Keller 	status = ice_read_sr_pointer(hw, ICE_SR_NETLIST_BANK_PTR, &banks->netlist_ptr);
9961fa95e01SJacob Keller 	if (status) {
9971fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank pointer\n");
9981fa95e01SJacob Keller 		return status;
9991fa95e01SJacob Keller 	}
10001fa95e01SJacob Keller 
10011fa95e01SJacob Keller 	status = ice_read_sr_area_size(hw, ICE_SR_NETLIST_BANK_SIZE, &banks->netlist_size);
10021fa95e01SJacob Keller 	if (status) {
10031fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank area size\n");
10041fa95e01SJacob Keller 		return status;
10051fa95e01SJacob Keller 	}
10061fa95e01SJacob Keller 
10071fa95e01SJacob Keller 	return 0;
10081fa95e01SJacob Keller }
10091fa95e01SJacob Keller 
10101fa95e01SJacob Keller /**
1011f31e4b6fSAnirudh Venkataramanan  * ice_init_nvm - initializes NVM setting
1012f9867df6SAnirudh Venkataramanan  * @hw: pointer to the HW struct
1013f31e4b6fSAnirudh Venkataramanan  *
1014f31e4b6fSAnirudh Venkataramanan  * This function reads and populates NVM settings such as Shadow RAM size,
1015f31e4b6fSAnirudh Venkataramanan  * max_timeout, and blank_nvm_mode
1016f31e4b6fSAnirudh Venkataramanan  */
10175e24d598STony Nguyen int ice_init_nvm(struct ice_hw *hw)
1018f31e4b6fSAnirudh Venkataramanan {
10199af368faSJacob Keller 	struct ice_flash_info *flash = &hw->flash;
1020f31e4b6fSAnirudh Venkataramanan 	u32 fla, gens_stat;
1021f31e4b6fSAnirudh Venkataramanan 	u8 sr_size;
10225518ac2aSTony Nguyen 	int status;
1023f31e4b6fSAnirudh Venkataramanan 
1024f9867df6SAnirudh Venkataramanan 	/* The SR size is stored regardless of the NVM programming mode
1025f31e4b6fSAnirudh Venkataramanan 	 * as the blank mode may be used in the factory line.
1026f31e4b6fSAnirudh Venkataramanan 	 */
1027f31e4b6fSAnirudh Venkataramanan 	gens_stat = rd32(hw, GLNVM_GENS);
1028f31e4b6fSAnirudh Venkataramanan 	sr_size = (gens_stat & GLNVM_GENS_SR_SIZE_M) >> GLNVM_GENS_SR_SIZE_S;
1029f31e4b6fSAnirudh Venkataramanan 
1030f31e4b6fSAnirudh Venkataramanan 	/* Switching to words (sr_size contains power of 2) */
10319af368faSJacob Keller 	flash->sr_words = BIT(sr_size) * ICE_SR_WORDS_IN_1KB;
1032f31e4b6fSAnirudh Venkataramanan 
1033f31e4b6fSAnirudh Venkataramanan 	/* Check if we are in the normal or blank NVM programming mode */
1034f31e4b6fSAnirudh Venkataramanan 	fla = rd32(hw, GLNVM_FLA);
1035f31e4b6fSAnirudh Venkataramanan 	if (fla & GLNVM_FLA_LOCKED_M) { /* Normal programming mode */
10369af368faSJacob Keller 		flash->blank_nvm_mode = false;
1037031f2147SMd Fahad Iqbal Polash 	} else {
1038031f2147SMd Fahad Iqbal Polash 		/* Blank programming mode */
10399af368faSJacob Keller 		flash->blank_nvm_mode = true;
10409228d8b2SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "NVM init error: unsupported blank mode.\n");
1041d54699e2STony Nguyen 		return -EIO;
1042f31e4b6fSAnirudh Venkataramanan 	}
1043f31e4b6fSAnirudh Venkataramanan 
104481f07491SJacob Keller 	status = ice_discover_flash_size(hw);
104581f07491SJacob Keller 	if (status) {
10469228d8b2SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "NVM init error: failed to discover flash size.\n");
104781f07491SJacob Keller 		return status;
104881f07491SJacob Keller 	}
104981f07491SJacob Keller 
10501fa95e01SJacob Keller 	status = ice_determine_active_flash_banks(hw);
10511fa95e01SJacob Keller 	if (status) {
10521fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to determine active flash banks.\n");
10531fa95e01SJacob Keller 		return status;
10541fa95e01SJacob Keller 	}
10551fa95e01SJacob Keller 
10560ce50c70SJacob Keller 	status = ice_get_nvm_ver_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->nvm);
10579af368faSJacob Keller 	if (status) {
10589af368faSJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Failed to read NVM info.\n");
10599af368faSJacob Keller 		return status;
10609af368faSJacob Keller 	}
10619af368faSJacob Keller 
1062e67fbcfbSJacob Keller 	status = ice_get_orom_ver_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->orom);
1063e67fbcfbSJacob Keller 	if (status)
1064d4e87444SJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Failed to read Option ROM info.\n");
10654c98ab55SBruce Allan 
1066f45a645fSJacob Keller 	/* read the netlist version information */
1067e120a9abSJacob Keller 	status = ice_get_netlist_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->netlist);
1068f45a645fSJacob Keller 	if (status)
1069f45a645fSJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Failed to read netlist info.\n");
1070f45a645fSJacob Keller 
1071031f2147SMd Fahad Iqbal Polash 	return 0;
1072031f2147SMd Fahad Iqbal Polash }
1073031f2147SMd Fahad Iqbal Polash 
10744c98ab55SBruce Allan /**
10750e674aebSAnirudh Venkataramanan  * ice_nvm_validate_checksum
10760e674aebSAnirudh Venkataramanan  * @hw: pointer to the HW struct
10770e674aebSAnirudh Venkataramanan  *
10780e674aebSAnirudh Venkataramanan  * Verify NVM PFA checksum validity (0x0706)
10790e674aebSAnirudh Venkataramanan  */
10805e24d598STony Nguyen int ice_nvm_validate_checksum(struct ice_hw *hw)
10810e674aebSAnirudh Venkataramanan {
10820e674aebSAnirudh Venkataramanan 	struct ice_aqc_nvm_checksum *cmd;
10830e674aebSAnirudh Venkataramanan 	struct ice_aq_desc desc;
10845e24d598STony Nguyen 	int status;
10850e674aebSAnirudh Venkataramanan 
10860e674aebSAnirudh Venkataramanan 	status = ice_acquire_nvm(hw, ICE_RES_READ);
10870e674aebSAnirudh Venkataramanan 	if (status)
10880e674aebSAnirudh Venkataramanan 		return status;
10890e674aebSAnirudh Venkataramanan 
10900e674aebSAnirudh Venkataramanan 	cmd = &desc.params.nvm_checksum;
10910e674aebSAnirudh Venkataramanan 
10920e674aebSAnirudh Venkataramanan 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_checksum);
10930e674aebSAnirudh Venkataramanan 	cmd->flags = ICE_AQC_NVM_CHECKSUM_VERIFY;
10940e674aebSAnirudh Venkataramanan 
10950e674aebSAnirudh Venkataramanan 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
10960e674aebSAnirudh Venkataramanan 	ice_release_nvm(hw);
10970e674aebSAnirudh Venkataramanan 
10980e674aebSAnirudh Venkataramanan 	if (!status)
10990e674aebSAnirudh Venkataramanan 		if (le16_to_cpu(cmd->checksum) != ICE_AQC_NVM_CHECKSUM_CORRECT)
1100d54699e2STony Nguyen 			status = -EIO;
11010e674aebSAnirudh Venkataramanan 
11020e674aebSAnirudh Venkataramanan 	return status;
11030e674aebSAnirudh Venkataramanan }
1104544cd2acSCudzilo, Szymon T 
1105544cd2acSCudzilo, Szymon T /**
1106544cd2acSCudzilo, Szymon T  * ice_nvm_write_activate
1107544cd2acSCudzilo, Szymon T  * @hw: pointer to the HW struct
1108544cd2acSCudzilo, Szymon T  * @cmd_flags: NVM activate admin command bits (banks to be validated)
1109544cd2acSCudzilo, Szymon T  *
1110544cd2acSCudzilo, Szymon T  * Update the control word with the required banks' validity bits
1111544cd2acSCudzilo, Szymon T  * and dumps the Shadow RAM to flash (0x0707)
1112544cd2acSCudzilo, Szymon T  */
11135e24d598STony Nguyen int ice_nvm_write_activate(struct ice_hw *hw, u8 cmd_flags)
1114544cd2acSCudzilo, Szymon T {
1115544cd2acSCudzilo, Szymon T 	struct ice_aqc_nvm *cmd;
1116544cd2acSCudzilo, Szymon T 	struct ice_aq_desc desc;
1117544cd2acSCudzilo, Szymon T 
1118544cd2acSCudzilo, Szymon T 	cmd = &desc.params.nvm;
1119544cd2acSCudzilo, Szymon T 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_write_activate);
1120544cd2acSCudzilo, Szymon T 
1121544cd2acSCudzilo, Szymon T 	cmd->cmd_flags = cmd_flags;
1122544cd2acSCudzilo, Szymon T 
1123544cd2acSCudzilo, Szymon T 	return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1124544cd2acSCudzilo, Szymon T }
1125544cd2acSCudzilo, Szymon T 
1126544cd2acSCudzilo, Szymon T /**
1127544cd2acSCudzilo, Szymon T  * ice_aq_nvm_update_empr
1128544cd2acSCudzilo, Szymon T  * @hw: pointer to the HW struct
1129544cd2acSCudzilo, Szymon T  *
1130544cd2acSCudzilo, Szymon T  * Update empr (0x0709). This command allows SW to
1131544cd2acSCudzilo, Szymon T  * request an EMPR to activate new FW.
1132544cd2acSCudzilo, Szymon T  */
11335e24d598STony Nguyen int ice_aq_nvm_update_empr(struct ice_hw *hw)
1134544cd2acSCudzilo, Szymon T {
1135544cd2acSCudzilo, Szymon T 	struct ice_aq_desc desc;
1136544cd2acSCudzilo, Szymon T 
1137544cd2acSCudzilo, Szymon T 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_update_empr);
1138544cd2acSCudzilo, Szymon T 
1139544cd2acSCudzilo, Szymon T 	return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1140544cd2acSCudzilo, Szymon T }
1141544cd2acSCudzilo, Szymon T 
1142544cd2acSCudzilo, Szymon T /* ice_nvm_set_pkg_data
1143544cd2acSCudzilo, Szymon T  * @hw: pointer to the HW struct
1144544cd2acSCudzilo, Szymon T  * @del_pkg_data_flag: If is set then the current pkg_data store by FW
1145544cd2acSCudzilo, Szymon T  *		       is deleted.
1146544cd2acSCudzilo, Szymon T  *		       If bit is set to 1, then buffer should be size 0.
1147544cd2acSCudzilo, Szymon T  * @data: pointer to buffer
1148544cd2acSCudzilo, Szymon T  * @length: length of the buffer
1149544cd2acSCudzilo, Szymon T  * @cd: pointer to command details structure or NULL
1150544cd2acSCudzilo, Szymon T  *
1151544cd2acSCudzilo, Szymon T  * Set package data (0x070A). This command is equivalent to the reception
1152544cd2acSCudzilo, Szymon T  * of a PLDM FW Update GetPackageData cmd. This command should be sent
1153544cd2acSCudzilo, Szymon T  * as part of the NVM update as the first cmd in the flow.
1154544cd2acSCudzilo, Szymon T  */
1155544cd2acSCudzilo, Szymon T 
11565e24d598STony Nguyen int
1157544cd2acSCudzilo, Szymon T ice_nvm_set_pkg_data(struct ice_hw *hw, bool del_pkg_data_flag, u8 *data,
1158544cd2acSCudzilo, Szymon T 		     u16 length, struct ice_sq_cd *cd)
1159544cd2acSCudzilo, Szymon T {
1160544cd2acSCudzilo, Szymon T 	struct ice_aqc_nvm_pkg_data *cmd;
1161544cd2acSCudzilo, Szymon T 	struct ice_aq_desc desc;
1162544cd2acSCudzilo, Szymon T 
1163544cd2acSCudzilo, Szymon T 	if (length != 0 && !data)
1164d54699e2STony Nguyen 		return -EINVAL;
1165544cd2acSCudzilo, Szymon T 
1166544cd2acSCudzilo, Szymon T 	cmd = &desc.params.pkg_data;
1167544cd2acSCudzilo, Szymon T 
1168544cd2acSCudzilo, Szymon T 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_pkg_data);
1169544cd2acSCudzilo, Szymon T 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1170544cd2acSCudzilo, Szymon T 
1171544cd2acSCudzilo, Szymon T 	if (del_pkg_data_flag)
1172544cd2acSCudzilo, Szymon T 		cmd->cmd_flags |= ICE_AQC_NVM_PKG_DELETE;
1173544cd2acSCudzilo, Szymon T 
1174544cd2acSCudzilo, Szymon T 	return ice_aq_send_cmd(hw, &desc, data, length, cd);
1175544cd2acSCudzilo, Szymon T }
1176544cd2acSCudzilo, Szymon T 
1177544cd2acSCudzilo, Szymon T /* ice_nvm_pass_component_tbl
1178544cd2acSCudzilo, Szymon T  * @hw: pointer to the HW struct
1179544cd2acSCudzilo, Szymon T  * @data: pointer to buffer
1180544cd2acSCudzilo, Szymon T  * @length: length of the buffer
1181544cd2acSCudzilo, Szymon T  * @transfer_flag: parameter for determining stage of the update
1182544cd2acSCudzilo, Szymon T  * @comp_response: a pointer to the response from the 0x070B AQC.
1183544cd2acSCudzilo, Szymon T  * @comp_response_code: a pointer to the response code from the 0x070B AQC.
1184544cd2acSCudzilo, Szymon T  * @cd: pointer to command details structure or NULL
1185544cd2acSCudzilo, Szymon T  *
1186544cd2acSCudzilo, Szymon T  * Pass component table (0x070B). This command is equivalent to the reception
1187544cd2acSCudzilo, Szymon T  * of a PLDM FW Update PassComponentTable cmd. This command should be sent once
1188544cd2acSCudzilo, Szymon T  * per component. It can be only sent after Set Package Data cmd and before
1189544cd2acSCudzilo, Szymon T  * actual update. FW will assume these commands are going to be sent until
1190544cd2acSCudzilo, Szymon T  * the TransferFlag is set to End or StartAndEnd.
1191544cd2acSCudzilo, Szymon T  */
1192544cd2acSCudzilo, Szymon T 
11935e24d598STony Nguyen int
1194544cd2acSCudzilo, Szymon T ice_nvm_pass_component_tbl(struct ice_hw *hw, u8 *data, u16 length,
1195544cd2acSCudzilo, Szymon T 			   u8 transfer_flag, u8 *comp_response,
1196544cd2acSCudzilo, Szymon T 			   u8 *comp_response_code, struct ice_sq_cd *cd)
1197544cd2acSCudzilo, Szymon T {
1198544cd2acSCudzilo, Szymon T 	struct ice_aqc_nvm_pass_comp_tbl *cmd;
1199544cd2acSCudzilo, Szymon T 	struct ice_aq_desc desc;
12005e24d598STony Nguyen 	int status;
1201544cd2acSCudzilo, Szymon T 
1202544cd2acSCudzilo, Szymon T 	if (!data || !comp_response || !comp_response_code)
1203d54699e2STony Nguyen 		return -EINVAL;
1204544cd2acSCudzilo, Szymon T 
1205544cd2acSCudzilo, Szymon T 	cmd = &desc.params.pass_comp_tbl;
1206544cd2acSCudzilo, Szymon T 
1207544cd2acSCudzilo, Szymon T 	ice_fill_dflt_direct_cmd_desc(&desc,
1208544cd2acSCudzilo, Szymon T 				      ice_aqc_opc_nvm_pass_component_tbl);
1209544cd2acSCudzilo, Szymon T 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1210544cd2acSCudzilo, Szymon T 
1211544cd2acSCudzilo, Szymon T 	cmd->transfer_flag = transfer_flag;
1212544cd2acSCudzilo, Szymon T 	status = ice_aq_send_cmd(hw, &desc, data, length, cd);
1213544cd2acSCudzilo, Szymon T 
1214544cd2acSCudzilo, Szymon T 	if (!status) {
1215544cd2acSCudzilo, Szymon T 		*comp_response = cmd->component_response;
1216544cd2acSCudzilo, Szymon T 		*comp_response_code = cmd->component_response_code;
1217544cd2acSCudzilo, Szymon T 	}
1218544cd2acSCudzilo, Szymon T 	return status;
1219544cd2acSCudzilo, Szymon T }
1220