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
ice_aq_read_nvm(struct ice_hw * hw,u16 module_typeid,u32 offset,u16 length,void * data,bool last_command,bool read_shadow_ram,struct ice_sq_cd * cd)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
ice_read_flat_nvm(struct ice_hw * hw,u32 offset,u32 * length,u8 * data,bool read_shadow_ram)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
ice_aq_update_nvm(struct ice_hw * hw,u16 module_typeid,u32 offset,u16 length,void * data,bool last_command,u8 command_flags,struct ice_sq_cd * cd)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  */
ice_aq_erase_nvm(struct ice_hw * hw,u16 module_typeid,struct ice_sq_cd * cd)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  */
ice_read_sr_word_aq(struct ice_hw * hw,u16 offset,u16 * data)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  */
ice_acquire_nvm(struct ice_hw * hw,enum ice_aq_res_access_type access)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  */
ice_release_nvm(struct ice_hw * hw)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  */
ice_get_flash_bank_offset(struct ice_hw * hw,enum ice_bank_select bank,u16 module)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
ice_read_flash_module(struct ice_hw * hw,enum ice_bank_select bank,u16 module,u32 offset,u8 * data,u32 length)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
ice_read_nvm_module(struct ice_hw * hw,enum ice_bank_select bank,u32 offset,u16 * data)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
ice_read_nvm_sr_copy(struct ice_hw * hw,enum ice_bank_select bank,u32 offset,u16 * data)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
ice_read_netlist_module(struct ice_hw * hw,enum ice_bank_select bank,u32 offset,u16 * data)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  */
ice_read_sr_word(struct ice_hw * hw,u16 offset,u16 * data)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
ice_get_pfa_module_tlv(struct ice_hw * hw,u16 * module_tlv,u16 * module_tlv_len,u16 module_type)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 {
4443201ba7dSJacob Keller 	u16 pfa_len, pfa_ptr, next_tlv, max_tlv;
4455518ac2aSTony Nguyen 	int status;
446e961b679SJacob Keller 
447e961b679SJacob Keller 	status = ice_read_sr_word(hw, ICE_SR_PFA_PTR, &pfa_ptr);
448e961b679SJacob Keller 	if (status) {
449e961b679SJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Preserved Field Array pointer.\n");
450e961b679SJacob Keller 		return status;
451e961b679SJacob Keller 	}
452e961b679SJacob Keller 	status = ice_read_sr_word(hw, pfa_ptr, &pfa_len);
453e961b679SJacob Keller 	if (status) {
454e961b679SJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PFA length.\n");
455e961b679SJacob Keller 		return status;
456e961b679SJacob Keller 	}
4573201ba7dSJacob Keller 
4583201ba7dSJacob Keller 	/* The Preserved Fields Area contains a sequence of Type-Length-Value
4593201ba7dSJacob Keller 	 * structures which define its contents. The PFA length includes all
4603201ba7dSJacob Keller 	 * of the TLVs, plus the initial length word itself, *and* one final
4613201ba7dSJacob Keller 	 * word at the end after all of the TLVs.
4623201ba7dSJacob Keller 	 */
4633201ba7dSJacob Keller 	if (check_add_overflow(pfa_ptr, pfa_len - 1, &max_tlv)) {
4643201ba7dSJacob Keller 		dev_warn(ice_hw_to_dev(hw), "PFA starts at offset %u. PFA length of %u caused 16-bit arithmetic overflow.\n",
4653201ba7dSJacob Keller 			 pfa_ptr, pfa_len);
4663201ba7dSJacob Keller 		return -EINVAL;
4673201ba7dSJacob Keller 	}
4683201ba7dSJacob Keller 
469e961b679SJacob Keller 	/* Starting with first TLV after PFA length, iterate through the list
470e961b679SJacob Keller 	 * of TLVs to find the requested one.
471e961b679SJacob Keller 	 */
472e961b679SJacob Keller 	next_tlv = pfa_ptr + 1;
4733201ba7dSJacob Keller 	while (next_tlv < max_tlv) {
474e961b679SJacob Keller 		u16 tlv_sub_module_type;
475e961b679SJacob Keller 		u16 tlv_len;
476e961b679SJacob Keller 
477e961b679SJacob Keller 		/* Read TLV type */
478e961b679SJacob Keller 		status = ice_read_sr_word(hw, next_tlv, &tlv_sub_module_type);
479e961b679SJacob Keller 		if (status) {
480e961b679SJacob Keller 			ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV type.\n");
481e961b679SJacob Keller 			break;
482e961b679SJacob Keller 		}
483e961b679SJacob Keller 		/* Read TLV length */
484e961b679SJacob Keller 		status = ice_read_sr_word(hw, next_tlv + 1, &tlv_len);
485e961b679SJacob Keller 		if (status) {
486e961b679SJacob Keller 			ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV length.\n");
487e961b679SJacob Keller 			break;
488e961b679SJacob Keller 		}
489e961b679SJacob Keller 		if (tlv_sub_module_type == module_type) {
490e961b679SJacob Keller 			if (tlv_len) {
491e961b679SJacob Keller 				*module_tlv = next_tlv;
492e961b679SJacob Keller 				*module_tlv_len = tlv_len;
493e961b679SJacob Keller 				return 0;
494e961b679SJacob Keller 			}
495d54699e2STony Nguyen 			return -EINVAL;
496e961b679SJacob Keller 		}
4973201ba7dSJacob Keller 
4983201ba7dSJacob Keller 		if (check_add_overflow(next_tlv, 2, &next_tlv) ||
4993201ba7dSJacob Keller 		    check_add_overflow(next_tlv, tlv_len, &next_tlv)) {
5003201ba7dSJacob Keller 			dev_warn(ice_hw_to_dev(hw), "TLV of type %u and length 0x%04x caused 16-bit arithmetic overflow. The PFA starts at 0x%04x and has length of 0x%04x\n",
5013201ba7dSJacob Keller 				 tlv_sub_module_type, tlv_len, pfa_ptr, pfa_len);
5023201ba7dSJacob Keller 			return -EINVAL;
5033201ba7dSJacob Keller 		}
504e961b679SJacob Keller 	}
505e961b679SJacob Keller 	/* Module does not exist */
506d54699e2STony Nguyen 	return -ENOENT;
507e961b679SJacob Keller }
508e961b679SJacob Keller 
509e961b679SJacob Keller /**
510e961b679SJacob Keller  * ice_read_pba_string - Reads part number string from NVM
511e961b679SJacob Keller  * @hw: pointer to hardware structure
512e961b679SJacob Keller  * @pba_num: stores the part number string from the NVM
513e961b679SJacob Keller  * @pba_num_size: part number string buffer length
514e961b679SJacob Keller  *
515e961b679SJacob Keller  * Reads the part number string from the NVM.
516e961b679SJacob Keller  */
ice_read_pba_string(struct ice_hw * hw,u8 * pba_num,u32 pba_num_size)5175518ac2aSTony Nguyen int ice_read_pba_string(struct ice_hw *hw, u8 *pba_num, u32 pba_num_size)
518e961b679SJacob Keller {
519e961b679SJacob Keller 	u16 pba_tlv, pba_tlv_len;
520e961b679SJacob Keller 	u16 pba_word, pba_size;
5215518ac2aSTony Nguyen 	int status;
522e961b679SJacob Keller 	u16 i;
523e961b679SJacob Keller 
524e961b679SJacob Keller 	status = ice_get_pfa_module_tlv(hw, &pba_tlv, &pba_tlv_len,
525e961b679SJacob Keller 					ICE_SR_PBA_BLOCK_PTR);
526e961b679SJacob Keller 	if (status) {
527e961b679SJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block TLV.\n");
528e961b679SJacob Keller 		return status;
529e961b679SJacob Keller 	}
530e961b679SJacob Keller 
531e961b679SJacob Keller 	/* pba_size is the next word */
532e961b679SJacob Keller 	status = ice_read_sr_word(hw, (pba_tlv + 2), &pba_size);
533e961b679SJacob Keller 	if (status) {
534e961b679SJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Section size.\n");
535e961b679SJacob Keller 		return status;
536e961b679SJacob Keller 	}
537e961b679SJacob Keller 
538e961b679SJacob Keller 	if (pba_tlv_len < pba_size) {
539e961b679SJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Invalid PBA Block TLV size.\n");
540d54699e2STony Nguyen 		return -EINVAL;
541e961b679SJacob Keller 	}
542e961b679SJacob Keller 
543e961b679SJacob Keller 	/* Subtract one to get PBA word count (PBA Size word is included in
544e961b679SJacob Keller 	 * total size)
545e961b679SJacob Keller 	 */
546e961b679SJacob Keller 	pba_size--;
547e961b679SJacob Keller 	if (pba_num_size < (((u32)pba_size * 2) + 1)) {
548e961b679SJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Buffer too small for PBA data.\n");
549d54699e2STony Nguyen 		return -EINVAL;
550e961b679SJacob Keller 	}
551e961b679SJacob Keller 
552e961b679SJacob Keller 	for (i = 0; i < pba_size; i++) {
553e961b679SJacob Keller 		status = ice_read_sr_word(hw, (pba_tlv + 2 + 1) + i, &pba_word);
554e961b679SJacob Keller 		if (status) {
555e961b679SJacob Keller 			ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block word %d.\n", i);
556e961b679SJacob Keller 			return status;
557e961b679SJacob Keller 		}
558e961b679SJacob Keller 
559e961b679SJacob Keller 		pba_num[(i * 2)] = (pba_word >> 8) & 0xFF;
560e961b679SJacob Keller 		pba_num[(i * 2) + 1] = pba_word & 0xFF;
561e961b679SJacob Keller 	}
562e961b679SJacob Keller 	pba_num[(pba_size * 2)] = '\0';
563e961b679SJacob Keller 
564e961b679SJacob Keller 	return status;
565e961b679SJacob Keller }
566e961b679SJacob Keller 
567e961b679SJacob Keller /**
5689af368faSJacob Keller  * ice_get_nvm_ver_info - Read NVM version information
5699af368faSJacob Keller  * @hw: pointer to the HW struct
5700ce50c70SJacob Keller  * @bank: whether to read from the active or inactive flash bank
5719af368faSJacob Keller  * @nvm: pointer to NVM info structure
5729af368faSJacob Keller  *
5739af368faSJacob Keller  * Read the NVM EETRACK ID and map version of the main NVM image bank, filling
5749af368faSJacob Keller  * in the NVM info structure.
5759af368faSJacob Keller  */
5765e24d598STony Nguyen static int
ice_get_nvm_ver_info(struct ice_hw * hw,enum ice_bank_select bank,struct ice_nvm_info * nvm)5770ce50c70SJacob Keller ice_get_nvm_ver_info(struct ice_hw *hw, enum ice_bank_select bank, struct ice_nvm_info *nvm)
5789af368faSJacob Keller {
5799af368faSJacob Keller 	u16 eetrack_lo, eetrack_hi, ver;
5805e24d598STony Nguyen 	int status;
5819af368faSJacob Keller 
5820ce50c70SJacob Keller 	status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_DEV_STARTER_VER, &ver);
5839af368faSJacob Keller 	if (status) {
5849af368faSJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read DEV starter version.\n");
5859af368faSJacob Keller 		return status;
5869af368faSJacob Keller 	}
5870ce50c70SJacob Keller 
5889af368faSJacob Keller 	nvm->major = (ver & ICE_NVM_VER_HI_MASK) >> ICE_NVM_VER_HI_SHIFT;
5899af368faSJacob Keller 	nvm->minor = (ver & ICE_NVM_VER_LO_MASK) >> ICE_NVM_VER_LO_SHIFT;
5909af368faSJacob Keller 
5910ce50c70SJacob Keller 	status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_EETRACK_LO, &eetrack_lo);
5929af368faSJacob Keller 	if (status) {
5939af368faSJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read EETRACK lo.\n");
5949af368faSJacob Keller 		return status;
5959af368faSJacob Keller 	}
5960ce50c70SJacob Keller 	status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_EETRACK_HI, &eetrack_hi);
5979af368faSJacob Keller 	if (status) {
5989af368faSJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read EETRACK hi.\n");
5999af368faSJacob Keller 		return status;
6009af368faSJacob Keller 	}
6019af368faSJacob Keller 
6029af368faSJacob Keller 	nvm->eetrack = (eetrack_hi << 16) | eetrack_lo;
6039af368faSJacob Keller 
6049af368faSJacob Keller 	return 0;
6059af368faSJacob Keller }
6069af368faSJacob Keller 
6079af368faSJacob Keller /**
6082c4fe41dSJacob Keller  * ice_get_inactive_nvm_ver - Read Option ROM version from the inactive bank
6092c4fe41dSJacob Keller  * @hw: pointer to the HW structure
6102c4fe41dSJacob Keller  * @nvm: storage for Option ROM version information
6112c4fe41dSJacob Keller  *
6122c4fe41dSJacob Keller  * Reads the NVM EETRACK ID, Map version, and security revision of the
6132c4fe41dSJacob Keller  * inactive NVM bank. Used to access version data for a pending update that
6142c4fe41dSJacob Keller  * has not yet been activated.
6152c4fe41dSJacob Keller  */
ice_get_inactive_nvm_ver(struct ice_hw * hw,struct ice_nvm_info * nvm)6165e24d598STony Nguyen int ice_get_inactive_nvm_ver(struct ice_hw *hw, struct ice_nvm_info *nvm)
6172c4fe41dSJacob Keller {
6182c4fe41dSJacob Keller 	return ice_get_nvm_ver_info(hw, ICE_INACTIVE_FLASH_BANK, nvm);
6192c4fe41dSJacob Keller }
6202c4fe41dSJacob Keller 
6212c4fe41dSJacob Keller /**
622e67fbcfbSJacob Keller  * ice_get_orom_civd_data - Get the combo version information from Option ROM
623d4e87444SJacob Keller  * @hw: pointer to the HW struct
624e67fbcfbSJacob Keller  * @bank: whether to read from the active or inactive flash module
625e67fbcfbSJacob Keller  * @civd: storage for the Option ROM CIVD data.
626d4e87444SJacob Keller  *
627e67fbcfbSJacob Keller  * Searches through the Option ROM flash contents to locate the CIVD data for
628e67fbcfbSJacob Keller  * the image.
629d4e87444SJacob Keller  */
6305e24d598STony Nguyen static int
ice_get_orom_civd_data(struct ice_hw * hw,enum ice_bank_select bank,struct ice_orom_civd_info * civd)631e67fbcfbSJacob Keller ice_get_orom_civd_data(struct ice_hw *hw, enum ice_bank_select bank,
632e67fbcfbSJacob Keller 		       struct ice_orom_civd_info *civd)
633d4e87444SJacob Keller {
634af18d886SJacob Keller 	u8 *orom_data;
6355e24d598STony Nguyen 	int status;
636e67fbcfbSJacob Keller 	u32 offset;
637e67fbcfbSJacob Keller 
638e67fbcfbSJacob Keller 	/* The CIVD section is located in the Option ROM aligned to 512 bytes.
639e67fbcfbSJacob Keller 	 * The first 4 bytes must contain the ASCII characters "$CIV".
640e67fbcfbSJacob Keller 	 * A simple modulo 256 sum of all of the bytes of the structure must
641e67fbcfbSJacob Keller 	 * equal 0.
642af18d886SJacob Keller 	 *
643af18d886SJacob Keller 	 * The exact location is unknown and varies between images but is
644af18d886SJacob Keller 	 * usually somewhere in the middle of the bank. We need to scan the
645af18d886SJacob Keller 	 * Option ROM bank to locate it.
646af18d886SJacob Keller 	 *
647af18d886SJacob Keller 	 * It's significantly faster to read the entire Option ROM up front
648af18d886SJacob Keller 	 * using the maximum page size, than to read each possible location
649af18d886SJacob Keller 	 * with a separate firmware command.
650e67fbcfbSJacob Keller 	 */
651af18d886SJacob Keller 	orom_data = vzalloc(hw->flash.banks.orom_size);
652af18d886SJacob Keller 	if (!orom_data)
653af18d886SJacob Keller 		return -ENOMEM;
654e67fbcfbSJacob Keller 
655af18d886SJacob Keller 	status = ice_read_flash_module(hw, bank, ICE_SR_1ST_OROM_BANK_PTR, 0,
656af18d886SJacob Keller 				       orom_data, hw->flash.banks.orom_size);
657e67fbcfbSJacob Keller 	if (status) {
6587c8881b7SJianglei Nie 		vfree(orom_data);
659af18d886SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Unable to read Option ROM data\n");
660e67fbcfbSJacob Keller 		return status;
661e67fbcfbSJacob Keller 	}
662e67fbcfbSJacob Keller 
663af18d886SJacob Keller 	/* Scan the memory buffer to locate the CIVD data section */
664af18d886SJacob Keller 	for (offset = 0; (offset + 512) <= hw->flash.banks.orom_size; offset += 512) {
665af18d886SJacob Keller 		struct ice_orom_civd_info *tmp;
666af18d886SJacob Keller 		u8 sum = 0, i;
667af18d886SJacob Keller 
668af18d886SJacob Keller 		tmp = (struct ice_orom_civd_info *)&orom_data[offset];
669af18d886SJacob Keller 
670e67fbcfbSJacob Keller 		/* Skip forward until we find a matching signature */
671af18d886SJacob Keller 		if (memcmp("$CIV", tmp->signature, sizeof(tmp->signature)) != 0)
672e67fbcfbSJacob Keller 			continue;
673e67fbcfbSJacob Keller 
674af18d886SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Found CIVD section at offset %u\n",
675af18d886SJacob Keller 			  offset);
676af18d886SJacob Keller 
677e67fbcfbSJacob Keller 		/* Verify that the simple checksum is zero */
678af18d886SJacob Keller 		for (i = 0; i < sizeof(*tmp); i++)
679af18d886SJacob Keller 			sum += ((u8 *)tmp)[i];
680e67fbcfbSJacob Keller 
681e67fbcfbSJacob Keller 		if (sum) {
682e67fbcfbSJacob Keller 			ice_debug(hw, ICE_DBG_NVM, "Found CIVD data with invalid checksum of %u\n",
683e67fbcfbSJacob Keller 				  sum);
684af18d886SJacob Keller 			goto err_invalid_checksum;
685e67fbcfbSJacob Keller 		}
686e67fbcfbSJacob Keller 
687af18d886SJacob Keller 		*civd = *tmp;
688af18d886SJacob Keller 		vfree(orom_data);
689e67fbcfbSJacob Keller 		return 0;
690e67fbcfbSJacob Keller 	}
691e67fbcfbSJacob Keller 
692af18d886SJacob Keller 	ice_debug(hw, ICE_DBG_NVM, "Unable to locate CIVD data within the Option ROM\n");
693af18d886SJacob Keller 
694af18d886SJacob Keller err_invalid_checksum:
695af18d886SJacob Keller 	vfree(orom_data);
696d54699e2STony Nguyen 	return -EIO;
697e67fbcfbSJacob Keller }
698e67fbcfbSJacob Keller 
699e67fbcfbSJacob Keller /**
700e67fbcfbSJacob Keller  * ice_get_orom_ver_info - Read Option ROM version information
701e67fbcfbSJacob Keller  * @hw: pointer to the HW struct
702e67fbcfbSJacob Keller  * @bank: whether to read from the active or inactive flash module
703e67fbcfbSJacob Keller  * @orom: pointer to Option ROM info structure
704e67fbcfbSJacob Keller  *
705e67fbcfbSJacob Keller  * Read Option ROM version and security revision from the Option ROM flash
706e67fbcfbSJacob Keller  * section.
707e67fbcfbSJacob Keller  */
7085e24d598STony Nguyen static int
ice_get_orom_ver_info(struct ice_hw * hw,enum ice_bank_select bank,struct ice_orom_info * orom)709e67fbcfbSJacob Keller ice_get_orom_ver_info(struct ice_hw *hw, enum ice_bank_select bank, struct ice_orom_info *orom)
710e67fbcfbSJacob Keller {
711e67fbcfbSJacob Keller 	struct ice_orom_civd_info civd;
712d4e87444SJacob Keller 	u32 combo_ver;
7135518ac2aSTony Nguyen 	int status;
714d4e87444SJacob Keller 
715e67fbcfbSJacob Keller 	status = ice_get_orom_civd_data(hw, bank, &civd);
716d4e87444SJacob Keller 	if (status) {
717e67fbcfbSJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to locate valid Option ROM CIVD data\n");
718d4e87444SJacob Keller 		return status;
719d4e87444SJacob Keller 	}
720d4e87444SJacob Keller 
721e67fbcfbSJacob Keller 	combo_ver = le32_to_cpu(civd.combo_ver);
722d4e87444SJacob Keller 
723e67fbcfbSJacob Keller 	orom->major = (u8)((combo_ver & ICE_OROM_VER_MASK) >> ICE_OROM_VER_SHIFT);
724d4e87444SJacob Keller 	orom->patch = (u8)(combo_ver & ICE_OROM_VER_PATCH_MASK);
725e67fbcfbSJacob Keller 	orom->build = (u16)((combo_ver & ICE_OROM_VER_BUILD_MASK) >> ICE_OROM_VER_BUILD_SHIFT);
726d4e87444SJacob Keller 
727d4e87444SJacob Keller 	return 0;
728d4e87444SJacob Keller }
729d4e87444SJacob Keller 
730d4e87444SJacob Keller /**
731e67fbcfbSJacob Keller  * ice_get_inactive_orom_ver - Read Option ROM version from the inactive bank
732e67fbcfbSJacob Keller  * @hw: pointer to the HW structure
733e67fbcfbSJacob Keller  * @orom: storage for Option ROM version information
734e67fbcfbSJacob Keller  *
735e67fbcfbSJacob Keller  * Reads the Option ROM version and security revision data for the inactive
736e67fbcfbSJacob Keller  * section of flash. Used to access version data for a pending update that has
737e67fbcfbSJacob Keller  * not yet been activated.
738e67fbcfbSJacob Keller  */
ice_get_inactive_orom_ver(struct ice_hw * hw,struct ice_orom_info * orom)7395e24d598STony Nguyen int ice_get_inactive_orom_ver(struct ice_hw *hw, struct ice_orom_info *orom)
740e67fbcfbSJacob Keller {
741e67fbcfbSJacob Keller 	return ice_get_orom_ver_info(hw, ICE_INACTIVE_FLASH_BANK, orom);
742e67fbcfbSJacob Keller }
743e67fbcfbSJacob Keller 
744e67fbcfbSJacob Keller /**
745e120a9abSJacob Keller  * ice_get_netlist_info
746f45a645fSJacob Keller  * @hw: pointer to the HW struct
747e120a9abSJacob Keller  * @bank: whether to read from the active or inactive flash bank
748e120a9abSJacob Keller  * @netlist: pointer to netlist version info structure
749f45a645fSJacob Keller  *
750e120a9abSJacob Keller  * Get the netlist version information from the requested bank. Reads the Link
751e120a9abSJacob Keller  * Topology section to find the Netlist ID block and extract the relevant
752e120a9abSJacob Keller  * information into the netlist version structure.
753f45a645fSJacob Keller  */
7545e24d598STony Nguyen static int
ice_get_netlist_info(struct ice_hw * hw,enum ice_bank_select bank,struct ice_netlist_info * netlist)755e120a9abSJacob Keller ice_get_netlist_info(struct ice_hw *hw, enum ice_bank_select bank,
756e120a9abSJacob Keller 		     struct ice_netlist_info *netlist)
757f45a645fSJacob Keller {
758e120a9abSJacob Keller 	u16 module_id, length, node_count, i;
759e120a9abSJacob Keller 	u16 *id_blk;
7605518ac2aSTony Nguyen 	int status;
761f45a645fSJacob Keller 
762e120a9abSJacob Keller 	status = ice_read_netlist_module(hw, bank, ICE_NETLIST_TYPE_OFFSET, &module_id);
763e120a9abSJacob Keller 	if (status)
764e120a9abSJacob Keller 		return status;
765e120a9abSJacob Keller 
766e120a9abSJacob Keller 	if (module_id != ICE_NETLIST_LINK_TOPO_MOD_ID) {
767e120a9abSJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Expected netlist module_id ID of 0x%04x, but got 0x%04x\n",
768e120a9abSJacob Keller 			  ICE_NETLIST_LINK_TOPO_MOD_ID, module_id);
769d54699e2STony Nguyen 		return -EIO;
770f45a645fSJacob Keller 	}
771f45a645fSJacob Keller 
772e120a9abSJacob Keller 	status = ice_read_netlist_module(hw, bank, ICE_LINK_TOPO_MODULE_LEN, &length);
773e120a9abSJacob Keller 	if (status)
774e120a9abSJacob Keller 		return status;
775e120a9abSJacob Keller 
776e120a9abSJacob Keller 	/* sanity check that we have at least enough words to store the netlist ID block */
777e120a9abSJacob Keller 	if (length < ICE_NETLIST_ID_BLK_SIZE) {
778e120a9abSJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Netlist Link Topology module too small. Expected at least %u words, but got %u words.\n",
779e120a9abSJacob Keller 			  ICE_NETLIST_ID_BLK_SIZE, length);
780d54699e2STony Nguyen 		return -EIO;
781e120a9abSJacob Keller 	}
782e120a9abSJacob Keller 
783e120a9abSJacob Keller 	status = ice_read_netlist_module(hw, bank, ICE_LINK_TOPO_NODE_COUNT, &node_count);
784e120a9abSJacob Keller 	if (status)
785e120a9abSJacob Keller 		return status;
786e120a9abSJacob Keller 	node_count &= ICE_LINK_TOPO_NODE_COUNT_M;
787e120a9abSJacob Keller 
788e120a9abSJacob Keller 	id_blk = kcalloc(ICE_NETLIST_ID_BLK_SIZE, sizeof(*id_blk), GFP_KERNEL);
789e120a9abSJacob Keller 	if (!id_blk)
790d54699e2STony Nguyen 		return -ENOMEM;
791e120a9abSJacob Keller 
792e120a9abSJacob Keller 	/* Read out the entire Netlist ID Block at once. */
793e120a9abSJacob Keller 	status = ice_read_flash_module(hw, bank, ICE_SR_NETLIST_BANK_PTR,
794e120a9abSJacob Keller 				       ICE_NETLIST_ID_BLK_OFFSET(node_count) * sizeof(u16),
795e120a9abSJacob Keller 				       (u8 *)id_blk, ICE_NETLIST_ID_BLK_SIZE * sizeof(u16));
796e120a9abSJacob Keller 	if (status)
797f45a645fSJacob Keller 		goto exit_error;
798f45a645fSJacob Keller 
799e120a9abSJacob Keller 	for (i = 0; i < ICE_NETLIST_ID_BLK_SIZE; i++)
800e120a9abSJacob Keller 		id_blk[i] = le16_to_cpu(((__force __le16 *)id_blk)[i]);
801f45a645fSJacob Keller 
802e120a9abSJacob Keller 	netlist->major = id_blk[ICE_NETLIST_ID_BLK_MAJOR_VER_HIGH] << 16 |
803e120a9abSJacob Keller 			 id_blk[ICE_NETLIST_ID_BLK_MAJOR_VER_LOW];
804e120a9abSJacob Keller 	netlist->minor = id_blk[ICE_NETLIST_ID_BLK_MINOR_VER_HIGH] << 16 |
805e120a9abSJacob Keller 			 id_blk[ICE_NETLIST_ID_BLK_MINOR_VER_LOW];
806e120a9abSJacob Keller 	netlist->type = id_blk[ICE_NETLIST_ID_BLK_TYPE_HIGH] << 16 |
807e120a9abSJacob Keller 			id_blk[ICE_NETLIST_ID_BLK_TYPE_LOW];
808e120a9abSJacob Keller 	netlist->rev = id_blk[ICE_NETLIST_ID_BLK_REV_HIGH] << 16 |
809e120a9abSJacob Keller 		       id_blk[ICE_NETLIST_ID_BLK_REV_LOW];
810e120a9abSJacob Keller 	netlist->cust_ver = id_blk[ICE_NETLIST_ID_BLK_CUST_VER];
811f45a645fSJacob Keller 	/* Read the left most 4 bytes of SHA */
812e120a9abSJacob Keller 	netlist->hash = id_blk[ICE_NETLIST_ID_BLK_SHA_HASH_WORD(15)] << 16 |
813e120a9abSJacob Keller 			id_blk[ICE_NETLIST_ID_BLK_SHA_HASH_WORD(14)];
814f45a645fSJacob Keller 
815f45a645fSJacob Keller exit_error:
816e120a9abSJacob Keller 	kfree(id_blk);
817e120a9abSJacob Keller 
818e120a9abSJacob Keller 	return status;
819e120a9abSJacob Keller }
820e120a9abSJacob Keller 
821e120a9abSJacob Keller /**
822e120a9abSJacob Keller  * ice_get_inactive_netlist_ver
823e120a9abSJacob Keller  * @hw: pointer to the HW struct
824e120a9abSJacob Keller  * @netlist: pointer to netlist version info structure
825e120a9abSJacob Keller  *
826e120a9abSJacob Keller  * Read the netlist version data from the inactive netlist bank. Used to
827e120a9abSJacob Keller  * extract version data of a pending flash update in order to display the
828e120a9abSJacob Keller  * version data.
829e120a9abSJacob Keller  */
ice_get_inactive_netlist_ver(struct ice_hw * hw,struct ice_netlist_info * netlist)8305e24d598STony Nguyen int ice_get_inactive_netlist_ver(struct ice_hw *hw, struct ice_netlist_info *netlist)
831e120a9abSJacob Keller {
832e120a9abSJacob Keller 	return ice_get_netlist_info(hw, ICE_INACTIVE_FLASH_BANK, netlist);
833f45a645fSJacob Keller }
834f45a645fSJacob Keller 
835f45a645fSJacob Keller /**
83681f07491SJacob Keller  * ice_discover_flash_size - Discover the available flash size.
83781f07491SJacob Keller  * @hw: pointer to the HW struct
83881f07491SJacob Keller  *
83981f07491SJacob Keller  * The device flash could be up to 16MB in size. However, it is possible that
84081f07491SJacob Keller  * the actual size is smaller. Use bisection to determine the accessible size
84181f07491SJacob Keller  * of flash memory.
84281f07491SJacob Keller  */
ice_discover_flash_size(struct ice_hw * hw)8435e24d598STony Nguyen static int ice_discover_flash_size(struct ice_hw *hw)
84481f07491SJacob Keller {
84581f07491SJacob Keller 	u32 min_size = 0, max_size = ICE_AQC_NVM_MAX_OFFSET + 1;
8465e24d598STony Nguyen 	int status;
84781f07491SJacob Keller 
84881f07491SJacob Keller 	status = ice_acquire_nvm(hw, ICE_RES_READ);
84981f07491SJacob Keller 	if (status)
85081f07491SJacob Keller 		return status;
85181f07491SJacob Keller 
85281f07491SJacob Keller 	while ((max_size - min_size) > 1) {
85381f07491SJacob Keller 		u32 offset = (max_size + min_size) / 2;
85481f07491SJacob Keller 		u32 len = 1;
85581f07491SJacob Keller 		u8 data;
85681f07491SJacob Keller 
85781f07491SJacob Keller 		status = ice_read_flat_nvm(hw, offset, &len, &data, false);
858d54699e2STony Nguyen 		if (status == -EIO &&
85981f07491SJacob Keller 		    hw->adminq.sq_last_status == ICE_AQ_RC_EINVAL) {
8609228d8b2SJacob Keller 			ice_debug(hw, ICE_DBG_NVM, "%s: New upper bound of %u bytes\n",
86181f07491SJacob Keller 				  __func__, offset);
86281f07491SJacob Keller 			status = 0;
86381f07491SJacob Keller 			max_size = offset;
86481f07491SJacob Keller 		} else if (!status) {
8659228d8b2SJacob Keller 			ice_debug(hw, ICE_DBG_NVM, "%s: New lower bound of %u bytes\n",
86681f07491SJacob Keller 				  __func__, offset);
86781f07491SJacob Keller 			min_size = offset;
86881f07491SJacob Keller 		} else {
86981f07491SJacob Keller 			/* an unexpected error occurred */
87081f07491SJacob Keller 			goto err_read_flat_nvm;
87181f07491SJacob Keller 		}
87281f07491SJacob Keller 	}
87381f07491SJacob Keller 
8749228d8b2SJacob Keller 	ice_debug(hw, ICE_DBG_NVM, "Predicted flash size is %u bytes\n", max_size);
87581f07491SJacob Keller 
8769af368faSJacob Keller 	hw->flash.flash_size = max_size;
87781f07491SJacob Keller 
87881f07491SJacob Keller err_read_flat_nvm:
87981f07491SJacob Keller 	ice_release_nvm(hw);
88081f07491SJacob Keller 
88181f07491SJacob Keller 	return status;
88281f07491SJacob Keller }
88381f07491SJacob Keller 
88481f07491SJacob Keller /**
8851fa95e01SJacob Keller  * ice_read_sr_pointer - Read the value of a Shadow RAM pointer word
8861fa95e01SJacob Keller  * @hw: pointer to the HW structure
8871fa95e01SJacob Keller  * @offset: the word offset of the Shadow RAM word to read
8881fa95e01SJacob Keller  * @pointer: pointer value read from Shadow RAM
8891fa95e01SJacob Keller  *
8901fa95e01SJacob Keller  * Read the given Shadow RAM word, and convert it to a pointer value specified
8911fa95e01SJacob Keller  * in bytes. This function assumes the specified offset is a valid pointer
8921fa95e01SJacob Keller  * word.
8931fa95e01SJacob Keller  *
8941fa95e01SJacob Keller  * Each pointer word specifies whether it is stored in word size or 4KB
8951fa95e01SJacob Keller  * sector size by using the highest bit. The reported pointer value will be in
8961fa95e01SJacob Keller  * bytes, intended for flat NVM reads.
8971fa95e01SJacob Keller  */
ice_read_sr_pointer(struct ice_hw * hw,u16 offset,u32 * pointer)8985518ac2aSTony Nguyen static int ice_read_sr_pointer(struct ice_hw *hw, u16 offset, u32 *pointer)
8991fa95e01SJacob Keller {
9005e24d598STony Nguyen 	int status;
9011fa95e01SJacob Keller 	u16 value;
9021fa95e01SJacob Keller 
9031fa95e01SJacob Keller 	status = ice_read_sr_word(hw, offset, &value);
9041fa95e01SJacob Keller 	if (status)
9051fa95e01SJacob Keller 		return status;
9061fa95e01SJacob Keller 
9071fa95e01SJacob Keller 	/* Determine if the pointer is in 4KB or word units */
9081fa95e01SJacob Keller 	if (value & ICE_SR_NVM_PTR_4KB_UNITS)
9091fa95e01SJacob Keller 		*pointer = (value & ~ICE_SR_NVM_PTR_4KB_UNITS) * 4 * 1024;
9101fa95e01SJacob Keller 	else
9111fa95e01SJacob Keller 		*pointer = value * 2;
9121fa95e01SJacob Keller 
9131fa95e01SJacob Keller 	return 0;
9141fa95e01SJacob Keller }
9151fa95e01SJacob Keller 
9161fa95e01SJacob Keller /**
9171fa95e01SJacob Keller  * ice_read_sr_area_size - Read an area size from a Shadow RAM word
9181fa95e01SJacob Keller  * @hw: pointer to the HW structure
9191fa95e01SJacob Keller  * @offset: the word offset of the Shadow RAM to read
9201fa95e01SJacob Keller  * @size: size value read from the Shadow RAM
9211fa95e01SJacob Keller  *
9221fa95e01SJacob Keller  * Read the given Shadow RAM word, and convert it to an area size value
9231fa95e01SJacob Keller  * specified in bytes. This function assumes the specified offset is a valid
9241fa95e01SJacob Keller  * area size word.
9251fa95e01SJacob Keller  *
9261fa95e01SJacob Keller  * Each area size word is specified in 4KB sector units. This function reports
9271fa95e01SJacob Keller  * the size in bytes, intended for flat NVM reads.
9281fa95e01SJacob Keller  */
ice_read_sr_area_size(struct ice_hw * hw,u16 offset,u32 * size)9295518ac2aSTony Nguyen static int ice_read_sr_area_size(struct ice_hw *hw, u16 offset, u32 *size)
9301fa95e01SJacob Keller {
9315e24d598STony Nguyen 	int status;
9321fa95e01SJacob Keller 	u16 value;
9331fa95e01SJacob Keller 
9341fa95e01SJacob Keller 	status = ice_read_sr_word(hw, offset, &value);
9351fa95e01SJacob Keller 	if (status)
9361fa95e01SJacob Keller 		return status;
9371fa95e01SJacob Keller 
9381fa95e01SJacob Keller 	/* Area sizes are always specified in 4KB units */
9391fa95e01SJacob Keller 	*size = value * 4 * 1024;
9401fa95e01SJacob Keller 
9411fa95e01SJacob Keller 	return 0;
9421fa95e01SJacob Keller }
9431fa95e01SJacob Keller 
9441fa95e01SJacob Keller /**
9451fa95e01SJacob Keller  * ice_determine_active_flash_banks - Discover active bank for each module
9461fa95e01SJacob Keller  * @hw: pointer to the HW struct
9471fa95e01SJacob Keller  *
9481fa95e01SJacob Keller  * Read the Shadow RAM control word and determine which banks are active for
9491fa95e01SJacob Keller  * the NVM, OROM, and Netlist modules. Also read and calculate the associated
9501fa95e01SJacob Keller  * pointer and size. These values are then cached into the ice_flash_info
9511fa95e01SJacob Keller  * structure for later use in order to calculate the correct offset to read
9521fa95e01SJacob Keller  * from the active module.
9531fa95e01SJacob Keller  */
ice_determine_active_flash_banks(struct ice_hw * hw)9545518ac2aSTony Nguyen static int ice_determine_active_flash_banks(struct ice_hw *hw)
9551fa95e01SJacob Keller {
9561fa95e01SJacob Keller 	struct ice_bank_info *banks = &hw->flash.banks;
9571fa95e01SJacob Keller 	u16 ctrl_word;
9585518ac2aSTony Nguyen 	int status;
9591fa95e01SJacob Keller 
9601fa95e01SJacob Keller 	status = ice_read_sr_word(hw, ICE_SR_NVM_CTRL_WORD, &ctrl_word);
9611fa95e01SJacob Keller 	if (status) {
9621fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read the Shadow RAM control word\n");
9631fa95e01SJacob Keller 		return status;
9641fa95e01SJacob Keller 	}
9651fa95e01SJacob Keller 
9661fa95e01SJacob Keller 	/* Check that the control word indicates validity */
9671fa95e01SJacob Keller 	if ((ctrl_word & ICE_SR_CTRL_WORD_1_M) >> ICE_SR_CTRL_WORD_1_S != ICE_SR_CTRL_WORD_VALID) {
9681fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Shadow RAM control word is invalid\n");
969d54699e2STony Nguyen 		return -EIO;
9701fa95e01SJacob Keller 	}
9711fa95e01SJacob Keller 
9721fa95e01SJacob Keller 	if (!(ctrl_word & ICE_SR_CTRL_WORD_NVM_BANK))
9731fa95e01SJacob Keller 		banks->nvm_bank = ICE_1ST_FLASH_BANK;
9741fa95e01SJacob Keller 	else
9751fa95e01SJacob Keller 		banks->nvm_bank = ICE_2ND_FLASH_BANK;
9761fa95e01SJacob Keller 
9771fa95e01SJacob Keller 	if (!(ctrl_word & ICE_SR_CTRL_WORD_OROM_BANK))
9781fa95e01SJacob Keller 		banks->orom_bank = ICE_1ST_FLASH_BANK;
9791fa95e01SJacob Keller 	else
9801fa95e01SJacob Keller 		banks->orom_bank = ICE_2ND_FLASH_BANK;
9811fa95e01SJacob Keller 
9821fa95e01SJacob Keller 	if (!(ctrl_word & ICE_SR_CTRL_WORD_NETLIST_BANK))
9831fa95e01SJacob Keller 		banks->netlist_bank = ICE_1ST_FLASH_BANK;
9841fa95e01SJacob Keller 	else
9851fa95e01SJacob Keller 		banks->netlist_bank = ICE_2ND_FLASH_BANK;
9861fa95e01SJacob Keller 
9871fa95e01SJacob Keller 	status = ice_read_sr_pointer(hw, ICE_SR_1ST_NVM_BANK_PTR, &banks->nvm_ptr);
9881fa95e01SJacob Keller 	if (status) {
9891fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank pointer\n");
9901fa95e01SJacob Keller 		return status;
9911fa95e01SJacob Keller 	}
9921fa95e01SJacob Keller 
9931fa95e01SJacob Keller 	status = ice_read_sr_area_size(hw, ICE_SR_NVM_BANK_SIZE, &banks->nvm_size);
9941fa95e01SJacob Keller 	if (status) {
9951fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank area size\n");
9961fa95e01SJacob Keller 		return status;
9971fa95e01SJacob Keller 	}
9981fa95e01SJacob Keller 
9991fa95e01SJacob Keller 	status = ice_read_sr_pointer(hw, ICE_SR_1ST_OROM_BANK_PTR, &banks->orom_ptr);
10001fa95e01SJacob Keller 	if (status) {
10011fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank pointer\n");
10021fa95e01SJacob Keller 		return status;
10031fa95e01SJacob Keller 	}
10041fa95e01SJacob Keller 
10051fa95e01SJacob Keller 	status = ice_read_sr_area_size(hw, ICE_SR_OROM_BANK_SIZE, &banks->orom_size);
10061fa95e01SJacob Keller 	if (status) {
10071fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank area size\n");
10081fa95e01SJacob Keller 		return status;
10091fa95e01SJacob Keller 	}
10101fa95e01SJacob Keller 
10111fa95e01SJacob Keller 	status = ice_read_sr_pointer(hw, ICE_SR_NETLIST_BANK_PTR, &banks->netlist_ptr);
10121fa95e01SJacob Keller 	if (status) {
10131fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank pointer\n");
10141fa95e01SJacob Keller 		return status;
10151fa95e01SJacob Keller 	}
10161fa95e01SJacob Keller 
10171fa95e01SJacob Keller 	status = ice_read_sr_area_size(hw, ICE_SR_NETLIST_BANK_SIZE, &banks->netlist_size);
10181fa95e01SJacob Keller 	if (status) {
10191fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank area size\n");
10201fa95e01SJacob Keller 		return status;
10211fa95e01SJacob Keller 	}
10221fa95e01SJacob Keller 
10231fa95e01SJacob Keller 	return 0;
10241fa95e01SJacob Keller }
10251fa95e01SJacob Keller 
10261fa95e01SJacob Keller /**
1027f31e4b6fSAnirudh Venkataramanan  * ice_init_nvm - initializes NVM setting
1028f9867df6SAnirudh Venkataramanan  * @hw: pointer to the HW struct
1029f31e4b6fSAnirudh Venkataramanan  *
1030f31e4b6fSAnirudh Venkataramanan  * This function reads and populates NVM settings such as Shadow RAM size,
1031f31e4b6fSAnirudh Venkataramanan  * max_timeout, and blank_nvm_mode
1032f31e4b6fSAnirudh Venkataramanan  */
ice_init_nvm(struct ice_hw * hw)10335e24d598STony Nguyen int ice_init_nvm(struct ice_hw *hw)
1034f31e4b6fSAnirudh Venkataramanan {
10359af368faSJacob Keller 	struct ice_flash_info *flash = &hw->flash;
1036f31e4b6fSAnirudh Venkataramanan 	u32 fla, gens_stat;
1037f31e4b6fSAnirudh Venkataramanan 	u8 sr_size;
10385518ac2aSTony Nguyen 	int status;
1039f31e4b6fSAnirudh Venkataramanan 
1040f9867df6SAnirudh Venkataramanan 	/* The SR size is stored regardless of the NVM programming mode
1041f31e4b6fSAnirudh Venkataramanan 	 * as the blank mode may be used in the factory line.
1042f31e4b6fSAnirudh Venkataramanan 	 */
1043f31e4b6fSAnirudh Venkataramanan 	gens_stat = rd32(hw, GLNVM_GENS);
1044f31e4b6fSAnirudh Venkataramanan 	sr_size = (gens_stat & GLNVM_GENS_SR_SIZE_M) >> GLNVM_GENS_SR_SIZE_S;
1045f31e4b6fSAnirudh Venkataramanan 
1046f31e4b6fSAnirudh Venkataramanan 	/* Switching to words (sr_size contains power of 2) */
10479af368faSJacob Keller 	flash->sr_words = BIT(sr_size) * ICE_SR_WORDS_IN_1KB;
1048f31e4b6fSAnirudh Venkataramanan 
1049f31e4b6fSAnirudh Venkataramanan 	/* Check if we are in the normal or blank NVM programming mode */
1050f31e4b6fSAnirudh Venkataramanan 	fla = rd32(hw, GLNVM_FLA);
1051f31e4b6fSAnirudh Venkataramanan 	if (fla & GLNVM_FLA_LOCKED_M) { /* Normal programming mode */
10529af368faSJacob Keller 		flash->blank_nvm_mode = false;
1053031f2147SMd Fahad Iqbal Polash 	} else {
1054031f2147SMd Fahad Iqbal Polash 		/* Blank programming mode */
10559af368faSJacob Keller 		flash->blank_nvm_mode = true;
10569228d8b2SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "NVM init error: unsupported blank mode.\n");
1057d54699e2STony Nguyen 		return -EIO;
1058f31e4b6fSAnirudh Venkataramanan 	}
1059f31e4b6fSAnirudh Venkataramanan 
106081f07491SJacob Keller 	status = ice_discover_flash_size(hw);
106181f07491SJacob Keller 	if (status) {
10629228d8b2SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "NVM init error: failed to discover flash size.\n");
106381f07491SJacob Keller 		return status;
106481f07491SJacob Keller 	}
106581f07491SJacob Keller 
10661fa95e01SJacob Keller 	status = ice_determine_active_flash_banks(hw);
10671fa95e01SJacob Keller 	if (status) {
10681fa95e01SJacob Keller 		ice_debug(hw, ICE_DBG_NVM, "Failed to determine active flash banks.\n");
10691fa95e01SJacob Keller 		return status;
10701fa95e01SJacob Keller 	}
10711fa95e01SJacob Keller 
10720ce50c70SJacob Keller 	status = ice_get_nvm_ver_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->nvm);
10739af368faSJacob Keller 	if (status) {
10749af368faSJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Failed to read NVM info.\n");
10759af368faSJacob Keller 		return status;
10769af368faSJacob Keller 	}
10779af368faSJacob Keller 
1078e67fbcfbSJacob Keller 	status = ice_get_orom_ver_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->orom);
1079e67fbcfbSJacob Keller 	if (status)
1080d4e87444SJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Failed to read Option ROM info.\n");
10814c98ab55SBruce Allan 
1082f45a645fSJacob Keller 	/* read the netlist version information */
1083e120a9abSJacob Keller 	status = ice_get_netlist_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->netlist);
1084f45a645fSJacob Keller 	if (status)
1085f45a645fSJacob Keller 		ice_debug(hw, ICE_DBG_INIT, "Failed to read netlist info.\n");
1086f45a645fSJacob Keller 
1087031f2147SMd Fahad Iqbal Polash 	return 0;
1088031f2147SMd Fahad Iqbal Polash }
1089031f2147SMd Fahad Iqbal Polash 
10904c98ab55SBruce Allan /**
10910e674aebSAnirudh Venkataramanan  * ice_nvm_validate_checksum
10920e674aebSAnirudh Venkataramanan  * @hw: pointer to the HW struct
10930e674aebSAnirudh Venkataramanan  *
10940e674aebSAnirudh Venkataramanan  * Verify NVM PFA checksum validity (0x0706)
10950e674aebSAnirudh Venkataramanan  */
ice_nvm_validate_checksum(struct ice_hw * hw)10965e24d598STony Nguyen int ice_nvm_validate_checksum(struct ice_hw *hw)
10970e674aebSAnirudh Venkataramanan {
10980e674aebSAnirudh Venkataramanan 	struct ice_aqc_nvm_checksum *cmd;
10990e674aebSAnirudh Venkataramanan 	struct ice_aq_desc desc;
11005e24d598STony Nguyen 	int status;
11010e674aebSAnirudh Venkataramanan 
11020e674aebSAnirudh Venkataramanan 	status = ice_acquire_nvm(hw, ICE_RES_READ);
11030e674aebSAnirudh Venkataramanan 	if (status)
11040e674aebSAnirudh Venkataramanan 		return status;
11050e674aebSAnirudh Venkataramanan 
11060e674aebSAnirudh Venkataramanan 	cmd = &desc.params.nvm_checksum;
11070e674aebSAnirudh Venkataramanan 
11080e674aebSAnirudh Venkataramanan 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_checksum);
11090e674aebSAnirudh Venkataramanan 	cmd->flags = ICE_AQC_NVM_CHECKSUM_VERIFY;
11100e674aebSAnirudh Venkataramanan 
11110e674aebSAnirudh Venkataramanan 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
11120e674aebSAnirudh Venkataramanan 	ice_release_nvm(hw);
11130e674aebSAnirudh Venkataramanan 
11140e674aebSAnirudh Venkataramanan 	if (!status)
11150e674aebSAnirudh Venkataramanan 		if (le16_to_cpu(cmd->checksum) != ICE_AQC_NVM_CHECKSUM_CORRECT)
1116d54699e2STony Nguyen 			status = -EIO;
11170e674aebSAnirudh Venkataramanan 
11180e674aebSAnirudh Venkataramanan 	return status;
11190e674aebSAnirudh Venkataramanan }
1120544cd2acSCudzilo, Szymon T 
1121544cd2acSCudzilo, Szymon T /**
1122544cd2acSCudzilo, Szymon T  * ice_nvm_write_activate
1123544cd2acSCudzilo, Szymon T  * @hw: pointer to the HW struct
1124399e27dbSJacob Keller  * @cmd_flags: flags for write activate command
1125399e27dbSJacob Keller  * @response_flags: response indicators from firmware
1126544cd2acSCudzilo, Szymon T  *
1127544cd2acSCudzilo, Szymon T  * Update the control word with the required banks' validity bits
1128544cd2acSCudzilo, Szymon T  * and dumps the Shadow RAM to flash (0x0707)
1129399e27dbSJacob Keller  *
1130da02ee9cSJacob Keller  * cmd_flags controls which banks to activate, the preservation level to use
1131da02ee9cSJacob Keller  * when activating the NVM bank, and whether an EMP reset is required for
1132da02ee9cSJacob Keller  * activation.
1133da02ee9cSJacob Keller  *
1134da02ee9cSJacob Keller  * Note that the 16bit cmd_flags value is split between two separate 1 byte
1135da02ee9cSJacob Keller  * flag values in the descriptor.
1136399e27dbSJacob Keller  *
1137399e27dbSJacob Keller  * On successful return of the firmware command, the response_flags variable
1138399e27dbSJacob Keller  * is updated with the flags reported by firmware indicating certain status,
1139399e27dbSJacob Keller  * such as whether EMP reset is enabled.
1140544cd2acSCudzilo, Szymon T  */
ice_nvm_write_activate(struct ice_hw * hw,u16 cmd_flags,u8 * response_flags)1141da02ee9cSJacob Keller int ice_nvm_write_activate(struct ice_hw *hw, u16 cmd_flags, u8 *response_flags)
1142544cd2acSCudzilo, Szymon T {
1143544cd2acSCudzilo, Szymon T 	struct ice_aqc_nvm *cmd;
1144544cd2acSCudzilo, Szymon T 	struct ice_aq_desc desc;
1145399e27dbSJacob Keller 	int err;
1146544cd2acSCudzilo, Szymon T 
1147544cd2acSCudzilo, Szymon T 	cmd = &desc.params.nvm;
1148544cd2acSCudzilo, Szymon T 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_write_activate);
1149544cd2acSCudzilo, Szymon T 
1150da02ee9cSJacob Keller 	cmd->cmd_flags = (u8)(cmd_flags & 0xFF);
1151da02ee9cSJacob Keller 	cmd->offset_high = (u8)((cmd_flags >> 8) & 0xFF);
1152544cd2acSCudzilo, Szymon T 
1153399e27dbSJacob Keller 	err = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1154399e27dbSJacob Keller 	if (!err && response_flags)
1155399e27dbSJacob Keller 		*response_flags = cmd->cmd_flags;
1156399e27dbSJacob Keller 
1157399e27dbSJacob Keller 	return err;
1158544cd2acSCudzilo, Szymon T }
1159544cd2acSCudzilo, Szymon T 
1160544cd2acSCudzilo, Szymon T /**
1161544cd2acSCudzilo, Szymon T  * ice_aq_nvm_update_empr
1162544cd2acSCudzilo, Szymon T  * @hw: pointer to the HW struct
1163544cd2acSCudzilo, Szymon T  *
1164544cd2acSCudzilo, Szymon T  * Update empr (0x0709). This command allows SW to
1165544cd2acSCudzilo, Szymon T  * request an EMPR to activate new FW.
1166544cd2acSCudzilo, Szymon T  */
ice_aq_nvm_update_empr(struct ice_hw * hw)11675e24d598STony Nguyen int ice_aq_nvm_update_empr(struct ice_hw *hw)
1168544cd2acSCudzilo, Szymon T {
1169544cd2acSCudzilo, Szymon T 	struct ice_aq_desc desc;
1170544cd2acSCudzilo, Szymon T 
1171544cd2acSCudzilo, Szymon T 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_update_empr);
1172544cd2acSCudzilo, Szymon T 
1173544cd2acSCudzilo, Szymon T 	return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1174544cd2acSCudzilo, Szymon T }
1175544cd2acSCudzilo, Szymon T 
1176544cd2acSCudzilo, Szymon T /* ice_nvm_set_pkg_data
1177544cd2acSCudzilo, Szymon T  * @hw: pointer to the HW struct
1178544cd2acSCudzilo, Szymon T  * @del_pkg_data_flag: If is set then the current pkg_data store by FW
1179544cd2acSCudzilo, Szymon T  *		       is deleted.
1180544cd2acSCudzilo, Szymon T  *		       If bit is set to 1, then buffer should be size 0.
1181544cd2acSCudzilo, Szymon T  * @data: pointer to buffer
1182544cd2acSCudzilo, Szymon T  * @length: length of the buffer
1183544cd2acSCudzilo, Szymon T  * @cd: pointer to command details structure or NULL
1184544cd2acSCudzilo, Szymon T  *
1185544cd2acSCudzilo, Szymon T  * Set package data (0x070A). This command is equivalent to the reception
1186544cd2acSCudzilo, Szymon T  * of a PLDM FW Update GetPackageData cmd. This command should be sent
1187544cd2acSCudzilo, Szymon T  * as part of the NVM update as the first cmd in the flow.
1188544cd2acSCudzilo, Szymon T  */
1189544cd2acSCudzilo, Szymon T 
11905e24d598STony Nguyen int
ice_nvm_set_pkg_data(struct ice_hw * hw,bool del_pkg_data_flag,u8 * data,u16 length,struct ice_sq_cd * cd)1191544cd2acSCudzilo, Szymon T ice_nvm_set_pkg_data(struct ice_hw *hw, bool del_pkg_data_flag, u8 *data,
1192544cd2acSCudzilo, Szymon T 		     u16 length, struct ice_sq_cd *cd)
1193544cd2acSCudzilo, Szymon T {
1194544cd2acSCudzilo, Szymon T 	struct ice_aqc_nvm_pkg_data *cmd;
1195544cd2acSCudzilo, Szymon T 	struct ice_aq_desc desc;
1196544cd2acSCudzilo, Szymon T 
1197544cd2acSCudzilo, Szymon T 	if (length != 0 && !data)
1198d54699e2STony Nguyen 		return -EINVAL;
1199544cd2acSCudzilo, Szymon T 
1200544cd2acSCudzilo, Szymon T 	cmd = &desc.params.pkg_data;
1201544cd2acSCudzilo, Szymon T 
1202544cd2acSCudzilo, Szymon T 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_pkg_data);
1203544cd2acSCudzilo, Szymon T 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1204544cd2acSCudzilo, Szymon T 
1205544cd2acSCudzilo, Szymon T 	if (del_pkg_data_flag)
1206544cd2acSCudzilo, Szymon T 		cmd->cmd_flags |= ICE_AQC_NVM_PKG_DELETE;
1207544cd2acSCudzilo, Szymon T 
1208544cd2acSCudzilo, Szymon T 	return ice_aq_send_cmd(hw, &desc, data, length, cd);
1209544cd2acSCudzilo, Szymon T }
1210544cd2acSCudzilo, Szymon T 
1211544cd2acSCudzilo, Szymon T /* ice_nvm_pass_component_tbl
1212544cd2acSCudzilo, Szymon T  * @hw: pointer to the HW struct
1213544cd2acSCudzilo, Szymon T  * @data: pointer to buffer
1214544cd2acSCudzilo, Szymon T  * @length: length of the buffer
1215544cd2acSCudzilo, Szymon T  * @transfer_flag: parameter for determining stage of the update
1216544cd2acSCudzilo, Szymon T  * @comp_response: a pointer to the response from the 0x070B AQC.
1217544cd2acSCudzilo, Szymon T  * @comp_response_code: a pointer to the response code from the 0x070B AQC.
1218544cd2acSCudzilo, Szymon T  * @cd: pointer to command details structure or NULL
1219544cd2acSCudzilo, Szymon T  *
1220544cd2acSCudzilo, Szymon T  * Pass component table (0x070B). This command is equivalent to the reception
1221544cd2acSCudzilo, Szymon T  * of a PLDM FW Update PassComponentTable cmd. This command should be sent once
1222544cd2acSCudzilo, Szymon T  * per component. It can be only sent after Set Package Data cmd and before
1223544cd2acSCudzilo, Szymon T  * actual update. FW will assume these commands are going to be sent until
1224544cd2acSCudzilo, Szymon T  * the TransferFlag is set to End or StartAndEnd.
1225544cd2acSCudzilo, Szymon T  */
1226544cd2acSCudzilo, Szymon T 
12275e24d598STony Nguyen int
ice_nvm_pass_component_tbl(struct ice_hw * hw,u8 * data,u16 length,u8 transfer_flag,u8 * comp_response,u8 * comp_response_code,struct ice_sq_cd * cd)1228544cd2acSCudzilo, Szymon T ice_nvm_pass_component_tbl(struct ice_hw *hw, u8 *data, u16 length,
1229544cd2acSCudzilo, Szymon T 			   u8 transfer_flag, u8 *comp_response,
1230544cd2acSCudzilo, Szymon T 			   u8 *comp_response_code, struct ice_sq_cd *cd)
1231544cd2acSCudzilo, Szymon T {
1232544cd2acSCudzilo, Szymon T 	struct ice_aqc_nvm_pass_comp_tbl *cmd;
1233544cd2acSCudzilo, Szymon T 	struct ice_aq_desc desc;
12345e24d598STony Nguyen 	int status;
1235544cd2acSCudzilo, Szymon T 
1236544cd2acSCudzilo, Szymon T 	if (!data || !comp_response || !comp_response_code)
1237d54699e2STony Nguyen 		return -EINVAL;
1238544cd2acSCudzilo, Szymon T 
1239544cd2acSCudzilo, Szymon T 	cmd = &desc.params.pass_comp_tbl;
1240544cd2acSCudzilo, Szymon T 
1241544cd2acSCudzilo, Szymon T 	ice_fill_dflt_direct_cmd_desc(&desc,
1242544cd2acSCudzilo, Szymon T 				      ice_aqc_opc_nvm_pass_component_tbl);
1243544cd2acSCudzilo, Szymon T 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1244544cd2acSCudzilo, Szymon T 
1245544cd2acSCudzilo, Szymon T 	cmd->transfer_flag = transfer_flag;
1246544cd2acSCudzilo, Szymon T 	status = ice_aq_send_cmd(hw, &desc, data, length, cd);
1247544cd2acSCudzilo, Szymon T 
1248544cd2acSCudzilo, Szymon T 	if (!status) {
1249544cd2acSCudzilo, Szymon T 		*comp_response = cmd->component_response;
1250544cd2acSCudzilo, Szymon T 		*comp_response_code = cmd->component_response_code;
1251544cd2acSCudzilo, Szymon T 	}
1252544cd2acSCudzilo, Szymon T 	return status;
1253544cd2acSCudzilo, Szymon T }
1254