xref: /freebsd/sys/dev/ice/ice_nvm.c (revision 2a63c3be158216222d89a073dcbd6a72ee4aab5a)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*  Copyright (c) 2023, Intel Corporation
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above copyright notice,
9  *      this list of conditions and the following disclaimer.
10  *
11  *   2. Redistributions in binary form must reproduce the above copyright
12  *      notice, this list of conditions and the following disclaimer in the
13  *      documentation and/or other materials provided with the distribution.
14  *
15  *   3. Neither the name of the Intel Corporation nor the names of its
16  *      contributors may be used to endorse or promote products derived from
17  *      this software without specific prior written permission.
18  *
19  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  *  POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "ice_common.h"
33 
34 #define GL_MNG_DEF_DEVID 0x000B611C
35 
36 /**
37  * ice_aq_read_nvm
38  * @hw: pointer to the HW struct
39  * @module_typeid: module pointer location in words from the NVM beginning
40  * @offset: byte offset from the module beginning
41  * @length: length of the section to be read (in bytes from the offset)
42  * @data: command buffer (size [bytes] = length)
43  * @last_command: tells if this is the last command in a series
44  * @read_shadow_ram: tell if this is a shadow RAM read
45  * @cd: pointer to command details structure or NULL
46  *
47  * Read the NVM using the admin queue commands (0x0701)
48  */
49 enum ice_status
50 ice_aq_read_nvm(struct ice_hw *hw, u16 module_typeid, u32 offset, u16 length,
51 		void *data, bool last_command, bool read_shadow_ram,
52 		struct ice_sq_cd *cd)
53 {
54 	struct ice_aq_desc desc;
55 	struct ice_aqc_nvm *cmd;
56 
57 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
58 
59 	cmd = &desc.params.nvm;
60 
61 	if (offset > ICE_AQC_NVM_MAX_OFFSET)
62 		return ICE_ERR_PARAM;
63 
64 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_read);
65 
66 	if (!read_shadow_ram && module_typeid == ICE_AQC_NVM_START_POINT)
67 		cmd->cmd_flags |= ICE_AQC_NVM_FLASH_ONLY;
68 
69 	/* If this is the last command in a series, set the proper flag. */
70 	if (last_command)
71 		cmd->cmd_flags |= ICE_AQC_NVM_LAST_CMD;
72 	cmd->module_typeid = CPU_TO_LE16(module_typeid);
73 	cmd->offset_low = CPU_TO_LE16(offset & 0xFFFF);
74 	cmd->offset_high = (offset >> 16) & 0xFF;
75 	cmd->length = CPU_TO_LE16(length);
76 
77 	return ice_aq_send_cmd(hw, &desc, data, length, cd);
78 }
79 
80 /**
81  * ice_read_flat_nvm - Read portion of NVM by flat offset
82  * @hw: pointer to the HW struct
83  * @offset: offset from beginning of NVM
84  * @length: (in) number of bytes to read; (out) number of bytes actually read
85  * @data: buffer to return data in (sized to fit the specified length)
86  * @read_shadow_ram: if true, read from shadow RAM instead of NVM
87  *
88  * Reads a portion of the NVM, as a flat memory space. This function correctly
89  * breaks read requests across Shadow RAM sectors and ensures that no single
90  * read request exceeds the maximum 4KB read for a single AdminQ command.
91  *
92  * Returns a status code on failure. Note that the data pointer may be
93  * partially updated if some reads succeed before a failure.
94  */
95 enum ice_status
96 ice_read_flat_nvm(struct ice_hw *hw, u32 offset, u32 *length, u8 *data,
97 		  bool read_shadow_ram)
98 {
99 	enum ice_status status;
100 	u32 inlen = *length;
101 	u32 bytes_read = 0;
102 	bool last_cmd;
103 
104 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
105 
106 	*length = 0;
107 
108 	/* Verify the length of the read if this is for the Shadow RAM */
109 	if (read_shadow_ram && ((offset + inlen) > (hw->flash.sr_words * 2u))) {
110 		ice_debug(hw, ICE_DBG_NVM, "NVM error: requested data is beyond Shadow RAM limit\n");
111 		return ICE_ERR_PARAM;
112 	}
113 
114 	do {
115 		u32 read_size, sector_offset;
116 
117 		/* ice_aq_read_nvm cannot read more than 4KB at a time.
118 		 * Additionally, a read from the Shadow RAM may not cross over
119 		 * a sector boundary. Conveniently, the sector size is also
120 		 * 4KB.
121 		 */
122 		sector_offset = offset % ICE_AQ_MAX_BUF_LEN;
123 		read_size = MIN_T(u32, ICE_AQ_MAX_BUF_LEN - sector_offset,
124 				  inlen - bytes_read);
125 
126 		last_cmd = !(bytes_read + read_size < inlen);
127 
128 		/* ice_aq_read_nvm takes the length as a u16. Our read_size is
129 		 * calculated using a u32, but the ICE_AQ_MAX_BUF_LEN maximum
130 		 * size guarantees that it will fit within the 2 bytes.
131 		 */
132 		status = ice_aq_read_nvm(hw, ICE_AQC_NVM_START_POINT,
133 					 offset, (u16)read_size,
134 					 data + bytes_read, last_cmd,
135 					 read_shadow_ram, NULL);
136 		if (status)
137 			break;
138 
139 		bytes_read += read_size;
140 		offset += read_size;
141 	} while (!last_cmd);
142 
143 	*length = bytes_read;
144 	return status;
145 }
146 
147 /**
148  * ice_aq_update_nvm
149  * @hw: pointer to the HW struct
150  * @module_typeid: module pointer location in words from the NVM beginning
151  * @offset: byte offset from the module beginning
152  * @length: length of the section to be written (in bytes from the offset)
153  * @data: command buffer (size [bytes] = length)
154  * @last_command: tells if this is the last command in a series
155  * @command_flags: command parameters
156  * @cd: pointer to command details structure or NULL
157  *
158  * Update the NVM using the admin queue commands (0x0703)
159  */
160 enum ice_status
161 ice_aq_update_nvm(struct ice_hw *hw, u16 module_typeid, u32 offset,
162 		  u16 length, void *data, bool last_command, u8 command_flags,
163 		  struct ice_sq_cd *cd)
164 {
165 	struct ice_aq_desc desc;
166 	struct ice_aqc_nvm *cmd;
167 
168 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
169 
170 	cmd = &desc.params.nvm;
171 
172 	/* In offset the highest byte must be zeroed. */
173 	if (offset & 0xFF000000)
174 		return ICE_ERR_PARAM;
175 
176 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_write);
177 
178 	cmd->cmd_flags |= command_flags;
179 
180 	/* If this is the last command in a series, set the proper flag. */
181 	if (last_command)
182 		cmd->cmd_flags |= ICE_AQC_NVM_LAST_CMD;
183 	cmd->module_typeid = CPU_TO_LE16(module_typeid);
184 	cmd->offset_low = CPU_TO_LE16(offset & 0xFFFF);
185 	cmd->offset_high = (offset >> 16) & 0xFF;
186 	cmd->length = CPU_TO_LE16(length);
187 
188 	desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
189 
190 	return ice_aq_send_cmd(hw, &desc, data, length, cd);
191 }
192 
193 /**
194  * ice_aq_erase_nvm
195  * @hw: pointer to the HW struct
196  * @module_typeid: module pointer location in words from the NVM beginning
197  * @cd: pointer to command details structure or NULL
198  *
199  * Erase the NVM sector using the admin queue commands (0x0702)
200  */
201 enum ice_status
202 ice_aq_erase_nvm(struct ice_hw *hw, u16 module_typeid, struct ice_sq_cd *cd)
203 {
204 	struct ice_aq_desc desc;
205 	struct ice_aqc_nvm *cmd;
206 	enum ice_status status;
207 	__le16 len;
208 
209 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
210 
211 	/* read a length value from SR, so module_typeid is equal to 0 */
212 	/* calculate offset where module size is placed from bytes to words */
213 	/* set last command and read from SR values to true */
214 	status = ice_aq_read_nvm(hw, 0, 2 * module_typeid + 2, 2, &len, true,
215 				 true, NULL);
216 	if (status)
217 		return status;
218 
219 	cmd = &desc.params.nvm;
220 
221 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_erase);
222 
223 	cmd->module_typeid = CPU_TO_LE16(module_typeid);
224 	cmd->length = len;
225 	cmd->offset_low = 0;
226 	cmd->offset_high = 0;
227 
228 	return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
229 }
230 
231 /**
232  * ice_aq_read_nvm_cfg - read an NVM config block
233  * @hw: pointer to the HW struct
234  * @cmd_flags: NVM access admin command bits
235  * @field_id: field or feature ID
236  * @data: buffer for result
237  * @buf_size: buffer size
238  * @elem_count: pointer to count of elements read by FW
239  * @cd: pointer to command details structure or NULL
240  *
241  * Reads single or multiple feature/field ID and data (0x0704)
242  */
243 enum ice_status
244 ice_aq_read_nvm_cfg(struct ice_hw *hw, u8 cmd_flags, u16 field_id, void *data,
245 		    u16 buf_size, u16 *elem_count, struct ice_sq_cd *cd)
246 {
247 	struct ice_aqc_nvm_cfg *cmd;
248 	struct ice_aq_desc desc;
249 	enum ice_status status;
250 
251 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
252 
253 	cmd = &desc.params.nvm_cfg;
254 
255 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_cfg_read);
256 
257 	cmd->cmd_flags = cmd_flags;
258 	cmd->id = CPU_TO_LE16(field_id);
259 
260 	status = ice_aq_send_cmd(hw, &desc, data, buf_size, cd);
261 	if (!status && elem_count)
262 		*elem_count = LE16_TO_CPU(cmd->count);
263 
264 	return status;
265 }
266 
267 /**
268  * ice_aq_write_nvm_cfg - write an NVM config block
269  * @hw: pointer to the HW struct
270  * @cmd_flags: NVM access admin command bits
271  * @data: buffer for result
272  * @buf_size: buffer size
273  * @elem_count: count of elements to be written
274  * @cd: pointer to command details structure or NULL
275  *
276  * Writes single or multiple feature/field ID and data (0x0705)
277  */
278 enum ice_status
279 ice_aq_write_nvm_cfg(struct ice_hw *hw, u8 cmd_flags, void *data, u16 buf_size,
280 		     u16 elem_count, struct ice_sq_cd *cd)
281 {
282 	struct ice_aqc_nvm_cfg *cmd;
283 	struct ice_aq_desc desc;
284 
285 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
286 
287 	cmd = &desc.params.nvm_cfg;
288 
289 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_cfg_write);
290 	desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
291 
292 	cmd->count = CPU_TO_LE16(elem_count);
293 	cmd->cmd_flags = cmd_flags;
294 
295 	return ice_aq_send_cmd(hw, &desc, data, buf_size, cd);
296 }
297 
298 /**
299  * ice_check_sr_access_params - verify params for Shadow RAM R/W operations.
300  * @hw: pointer to the HW structure
301  * @offset: offset in words from module start
302  * @words: number of words to access
303  */
304 static enum ice_status
305 ice_check_sr_access_params(struct ice_hw *hw, u32 offset, u16 words)
306 {
307 	if ((offset + words) > hw->flash.sr_words) {
308 		ice_debug(hw, ICE_DBG_NVM, "NVM error: offset beyond SR lmt.\n");
309 		return ICE_ERR_PARAM;
310 	}
311 
312 	if (words > ICE_SR_SECTOR_SIZE_IN_WORDS) {
313 		/* We can access only up to 4KB (one sector), in one AQ write */
314 		ice_debug(hw, ICE_DBG_NVM, "NVM error: tried to access %d words, limit is %d.\n",
315 			  words, ICE_SR_SECTOR_SIZE_IN_WORDS);
316 		return ICE_ERR_PARAM;
317 	}
318 
319 	if (((offset + (words - 1)) / ICE_SR_SECTOR_SIZE_IN_WORDS) !=
320 	    (offset / ICE_SR_SECTOR_SIZE_IN_WORDS)) {
321 		/* A single access cannot spread over two sectors */
322 		ice_debug(hw, ICE_DBG_NVM, "NVM error: cannot spread over two sectors.\n");
323 		return ICE_ERR_PARAM;
324 	}
325 
326 	return ICE_SUCCESS;
327 }
328 
329 /**
330  * ice_read_sr_word_aq - Reads Shadow RAM via AQ
331  * @hw: pointer to the HW structure
332  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
333  * @data: word read from the Shadow RAM
334  *
335  * Reads one 16 bit word from the Shadow RAM using ice_read_flat_nvm.
336  */
337 enum ice_status ice_read_sr_word_aq(struct ice_hw *hw, u16 offset, u16 *data)
338 {
339 	u32 bytes = sizeof(u16);
340 	enum ice_status status;
341 	__le16 data_local;
342 
343 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
344 
345 	/* Note that ice_read_flat_nvm checks if the read is past the Shadow
346 	 * RAM size, and ensures we don't read across a Shadow RAM sector
347 	 * boundary
348 	 */
349 	status = ice_read_flat_nvm(hw, offset * sizeof(u16), &bytes,
350 				   (_FORCE_ u8 *)&data_local, true);
351 	if (status)
352 		return status;
353 
354 	*data = LE16_TO_CPU(data_local);
355 	return ICE_SUCCESS;
356 }
357 
358 /**
359  * ice_write_sr_aq - Writes Shadow RAM.
360  * @hw: pointer to the HW structure
361  * @offset: offset in words from module start
362  * @words: number of words to write
363  * @data: buffer with words to write to the Shadow RAM
364  * @last_command: tells the AdminQ that this is the last command
365  *
366  * Writes a 16 bit words buffer to the Shadow RAM using the admin command.
367  */
368 static enum ice_status
369 ice_write_sr_aq(struct ice_hw *hw, u32 offset, u16 words, __le16 *data,
370 		bool last_command)
371 {
372 	enum ice_status status;
373 
374 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
375 
376 	status = ice_check_sr_access_params(hw, offset, words);
377 	if (!status)
378 		status = ice_aq_update_nvm(hw, 0, 2 * offset, 2 * words, data,
379 					   last_command, 0, NULL);
380 
381 	return status;
382 }
383 
384 /**
385  * ice_read_sr_buf_aq - Reads Shadow RAM buf via AQ
386  * @hw: pointer to the HW structure
387  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
388  * @words: (in) number of words to read; (out) number of words actually read
389  * @data: words read from the Shadow RAM
390  *
391  * Reads 16 bit words (data buf) from the Shadow RAM. Ownership of the NVM is
392  * taken before reading the buffer and later released.
393  */
394 static enum ice_status
395 ice_read_sr_buf_aq(struct ice_hw *hw, u16 offset, u16 *words, u16 *data)
396 {
397 	u32 bytes = *words * 2, i;
398 	enum ice_status status;
399 
400 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
401 
402 	/* ice_read_flat_nvm takes into account the 4KB AdminQ and Shadow RAM
403 	 * sector restrictions necessary when reading from the NVM.
404 	 */
405 	status = ice_read_flat_nvm(hw, offset * 2, &bytes, (u8 *)data, true);
406 
407 	/* Report the number of words successfully read */
408 	*words = (u16)(bytes / 2);
409 
410 	/* Byte swap the words up to the amount we actually read */
411 	for (i = 0; i < *words; i++)
412 		data[i] = LE16_TO_CPU(((_FORCE_ __le16 *)data)[i]);
413 
414 	return status;
415 }
416 
417 /**
418  * ice_acquire_nvm - Generic request for acquiring the NVM ownership
419  * @hw: pointer to the HW structure
420  * @access: NVM access type (read or write)
421  *
422  * This function will request NVM ownership.
423  */
424 enum ice_status
425 ice_acquire_nvm(struct ice_hw *hw, enum ice_aq_res_access_type access)
426 {
427 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
428 
429 	if (hw->flash.blank_nvm_mode)
430 		return ICE_SUCCESS;
431 
432 	return ice_acquire_res(hw, ICE_NVM_RES_ID, access, ICE_NVM_TIMEOUT);
433 }
434 
435 /**
436  * ice_release_nvm - Generic request for releasing the NVM ownership
437  * @hw: pointer to the HW structure
438  *
439  * This function will release NVM ownership.
440  */
441 void ice_release_nvm(struct ice_hw *hw)
442 {
443 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
444 
445 	if (hw->flash.blank_nvm_mode)
446 		return;
447 
448 	ice_release_res(hw, ICE_NVM_RES_ID);
449 }
450 
451 /**
452  * ice_get_flash_bank_offset - Get offset into requested flash bank
453  * @hw: pointer to the HW structure
454  * @bank: whether to read from the active or inactive flash bank
455  * @module: the module to read from
456  *
457  * Based on the module, lookup the module offset from the beginning of the
458  * flash.
459  *
460  * Returns the flash offset. Note that a value of zero is invalid and must be
461  * treated as an error.
462  */
463 static u32 ice_get_flash_bank_offset(struct ice_hw *hw, enum ice_bank_select bank, u16 module)
464 {
465 	struct ice_bank_info *banks = &hw->flash.banks;
466 	enum ice_flash_bank active_bank;
467 	bool second_bank_active;
468 	u32 offset, size;
469 
470 	switch (module) {
471 	case ICE_SR_1ST_NVM_BANK_PTR:
472 		offset = banks->nvm_ptr;
473 		size = banks->nvm_size;
474 		active_bank = banks->nvm_bank;
475 		break;
476 	case ICE_SR_1ST_OROM_BANK_PTR:
477 		offset = banks->orom_ptr;
478 		size = banks->orom_size;
479 		active_bank = banks->orom_bank;
480 		break;
481 	case ICE_SR_NETLIST_BANK_PTR:
482 		offset = banks->netlist_ptr;
483 		size = banks->netlist_size;
484 		active_bank = banks->netlist_bank;
485 		break;
486 	default:
487 		ice_debug(hw, ICE_DBG_NVM, "Unexpected value for flash module: 0x%04x\n", module);
488 		return 0;
489 	}
490 
491 	switch (active_bank) {
492 	case ICE_1ST_FLASH_BANK:
493 		second_bank_active = false;
494 		break;
495 	case ICE_2ND_FLASH_BANK:
496 		second_bank_active = true;
497 		break;
498 	default:
499 		ice_debug(hw, ICE_DBG_NVM, "Unexpected value for active flash bank: %u\n",
500 			  active_bank);
501 		return 0;
502 	}
503 
504 	/* The second flash bank is stored immediately following the first
505 	 * bank. Based on whether the 1st or 2nd bank is active, and whether
506 	 * we want the active or inactive bank, calculate the desired offset.
507 	 */
508 	switch (bank) {
509 	case ICE_ACTIVE_FLASH_BANK:
510 		return offset + (second_bank_active ? size : 0);
511 	case ICE_INACTIVE_FLASH_BANK:
512 		return offset + (second_bank_active ? 0 : size);
513 	}
514 
515 	ice_debug(hw, ICE_DBG_NVM, "Unexpected value for flash bank selection: %u\n", bank);
516 	return 0;
517 }
518 
519 /**
520  * ice_read_flash_module - Read a word from one of the main NVM modules
521  * @hw: pointer to the HW structure
522  * @bank: which bank of the module to read
523  * @module: the module to read
524  * @offset: the offset into the module in bytes
525  * @data: storage for the word read from the flash
526  * @length: bytes of data to read
527  *
528  * Read data from the specified flash module. The bank parameter indicates
529  * whether or not to read from the active bank or the inactive bank of that
530  * module.
531  *
532  * The word will be read using flat NVM access, and relies on the
533  * hw->flash.banks data being setup by ice_determine_active_flash_banks()
534  * during initialization.
535  */
536 static enum ice_status
537 ice_read_flash_module(struct ice_hw *hw, enum ice_bank_select bank, u16 module,
538 		      u32 offset, u8 *data, u32 length)
539 {
540 	enum ice_status status;
541 	u32 start;
542 
543 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
544 
545 	start = ice_get_flash_bank_offset(hw, bank, module);
546 	if (!start) {
547 		ice_debug(hw, ICE_DBG_NVM, "Unable to calculate flash bank offset for module 0x%04x\n",
548 			  module);
549 		return ICE_ERR_PARAM;
550 	}
551 
552 	status = ice_acquire_nvm(hw, ICE_RES_READ);
553 	if (status)
554 		return status;
555 
556 	status = ice_read_flat_nvm(hw, start + offset, &length, data, false);
557 
558 	ice_release_nvm(hw);
559 
560 	return status;
561 }
562 
563 /**
564  * ice_read_nvm_module - Read from the active main NVM module
565  * @hw: pointer to the HW structure
566  * @bank: whether to read from active or inactive NVM module
567  * @offset: offset into the NVM module to read, in words
568  * @data: storage for returned word value
569  *
570  * Read the specified word from the active NVM module. This includes the CSS
571  * header at the start of the NVM module.
572  */
573 static enum ice_status
574 ice_read_nvm_module(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
575 {
576 	enum ice_status status;
577 	__le16 data_local;
578 
579 	status = ice_read_flash_module(hw, bank, ICE_SR_1ST_NVM_BANK_PTR, offset * sizeof(u16),
580 				       (_FORCE_ u8 *)&data_local, sizeof(u16));
581 	if (!status)
582 		*data = LE16_TO_CPU(data_local);
583 
584 	return status;
585 }
586 
587 /**
588  * ice_get_nvm_css_hdr_len - Read the CSS header length from the NVM CSS header
589  * @hw: pointer to the HW struct
590  * @bank: whether to read from the active or inactive flash bank
591  * @hdr_len: storage for header length in words
592  *
593  * Read the CSS header length from the NVM CSS header and add the Authentication
594  * header size, and then convert to words.
595  */
596 static enum ice_status
597 ice_get_nvm_css_hdr_len(struct ice_hw *hw, enum ice_bank_select bank,
598 			u32 *hdr_len)
599 {
600 	u16 hdr_len_l, hdr_len_h;
601 	enum ice_status status;
602 	u32 hdr_len_dword;
603 
604 	status = ice_read_nvm_module(hw, bank, ICE_NVM_CSS_HDR_LEN_L,
605 				     &hdr_len_l);
606 	if (status)
607 		return status;
608 
609 	status = ice_read_nvm_module(hw, bank, ICE_NVM_CSS_HDR_LEN_H,
610 				     &hdr_len_h);
611 	if (status)
612 		return status;
613 
614 	/* CSS header length is in DWORD, so convert to words and add
615 	 * authentication header size
616 	 */
617 	hdr_len_dword = hdr_len_h << 16 | hdr_len_l;
618 	*hdr_len = (hdr_len_dword * 2) + ICE_NVM_AUTH_HEADER_LEN;
619 
620 	return ICE_SUCCESS;
621 }
622 
623 /**
624  * ice_read_nvm_sr_copy - Read a word from the Shadow RAM copy in the NVM bank
625  * @hw: pointer to the HW structure
626  * @bank: whether to read from the active or inactive NVM module
627  * @offset: offset into the Shadow RAM copy to read, in words
628  * @data: storage for returned word value
629  *
630  * Read the specified word from the copy of the Shadow RAM found in the
631  * specified NVM module.
632  */
633 static enum ice_status
634 ice_read_nvm_sr_copy(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
635 {
636 	enum ice_status status;
637 	u32 hdr_len;
638 
639 	status = ice_get_nvm_css_hdr_len(hw, bank, &hdr_len);
640 	if (status)
641 		return status;
642 
643 	hdr_len = ROUND_UP(hdr_len, 32);
644 
645 	return ice_read_nvm_module(hw, bank, hdr_len + offset, data);
646 }
647 
648 /**
649  * ice_read_orom_module - Read from the active Option ROM module
650  * @hw: pointer to the HW structure
651  * @bank: whether to read from active or inactive OROM module
652  * @offset: offset into the OROM module to read, in words
653  * @data: storage for returned word value
654  *
655  * Read the specified word from the active Option ROM module of the flash.
656  * Note that unlike the NVM module, the CSS data is stored at the end of the
657  * module instead of at the beginning.
658  */
659 static enum ice_status
660 ice_read_orom_module(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
661 {
662 	enum ice_status status;
663 	__le16 data_local;
664 
665 	status = ice_read_flash_module(hw, bank, ICE_SR_1ST_OROM_BANK_PTR, offset * sizeof(u16),
666 				       (_FORCE_ u8 *)&data_local, sizeof(u16));
667 	if (!status)
668 		*data = LE16_TO_CPU(data_local);
669 
670 	return status;
671 }
672 
673 /**
674  * ice_read_netlist_module - Read data from the netlist module area
675  * @hw: pointer to the HW structure
676  * @bank: whether to read from the active or inactive module
677  * @offset: offset into the netlist to read from
678  * @data: storage for returned word value
679  *
680  * Read a word from the specified netlist bank.
681  */
682 static enum ice_status
683 ice_read_netlist_module(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
684 {
685 	enum ice_status status;
686 	__le16 data_local;
687 
688 	status = ice_read_flash_module(hw, bank, ICE_SR_NETLIST_BANK_PTR, offset * sizeof(u16),
689 				       (_FORCE_ u8 *)&data_local, sizeof(u16));
690 	if (!status)
691 		*data = LE16_TO_CPU(data_local);
692 
693 	return status;
694 }
695 
696 /**
697  * ice_read_sr_word - Reads Shadow RAM word and acquire NVM if necessary
698  * @hw: pointer to the HW structure
699  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
700  * @data: word read from the Shadow RAM
701  *
702  * Reads one 16 bit word from the Shadow RAM using the ice_read_sr_word_aq.
703  */
704 enum ice_status ice_read_sr_word(struct ice_hw *hw, u16 offset, u16 *data)
705 {
706 	enum ice_status status;
707 
708 	status = ice_acquire_nvm(hw, ICE_RES_READ);
709 	if (!status) {
710 		status = ice_read_sr_word_aq(hw, offset, data);
711 		ice_release_nvm(hw);
712 	}
713 
714 	return status;
715 }
716 
717 /**
718  * ice_get_pfa_module_tlv - Reads sub module TLV from NVM PFA
719  * @hw: pointer to hardware structure
720  * @module_tlv: pointer to module TLV to return
721  * @module_tlv_len: pointer to module TLV length to return
722  * @module_type: module type requested
723  *
724  * Finds the requested sub module TLV type from the Preserved Field
725  * Area (PFA) and returns the TLV pointer and length. The caller can
726  * use these to read the variable length TLV value.
727  */
728 enum ice_status
729 ice_get_pfa_module_tlv(struct ice_hw *hw, u16 *module_tlv, u16 *module_tlv_len,
730 		       u16 module_type)
731 {
732 	enum ice_status status;
733 	u16 pfa_len, pfa_ptr;
734 	u16 next_tlv;
735 
736 	status = ice_read_sr_word(hw, ICE_SR_PFA_PTR, &pfa_ptr);
737 	if (status != ICE_SUCCESS) {
738 		ice_debug(hw, ICE_DBG_INIT, "Preserved Field Array pointer.\n");
739 		return status;
740 	}
741 	status = ice_read_sr_word(hw, pfa_ptr, &pfa_len);
742 	if (status != ICE_SUCCESS) {
743 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PFA length.\n");
744 		return status;
745 	}
746 	/* Starting with first TLV after PFA length, iterate through the list
747 	 * of TLVs to find the requested one.
748 	 */
749 	next_tlv = pfa_ptr + 1;
750 	while (next_tlv < pfa_ptr + pfa_len) {
751 		u16 tlv_sub_module_type;
752 		u16 tlv_len;
753 
754 		/* Read TLV type */
755 		status = ice_read_sr_word(hw, next_tlv, &tlv_sub_module_type);
756 		if (status != ICE_SUCCESS) {
757 			ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV type.\n");
758 			break;
759 		}
760 		/* Read TLV length */
761 		status = ice_read_sr_word(hw, next_tlv + 1, &tlv_len);
762 		if (status != ICE_SUCCESS) {
763 			ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV length.\n");
764 			break;
765 		}
766 		if (tlv_sub_module_type == module_type) {
767 			if (tlv_len) {
768 				*module_tlv = next_tlv;
769 				*module_tlv_len = tlv_len;
770 				return ICE_SUCCESS;
771 			}
772 			return ICE_ERR_INVAL_SIZE;
773 		}
774 		/* Check next TLV, i.e. current TLV pointer + length + 2 words
775 		 * (for current TLV's type and length)
776 		 */
777 		next_tlv = next_tlv + tlv_len + 2;
778 	}
779 	/* Module does not exist */
780 	return ICE_ERR_DOES_NOT_EXIST;
781 }
782 
783 /**
784  * ice_read_pba_string - Reads part number string from NVM
785  * @hw: pointer to hardware structure
786  * @pba_num: stores the part number string from the NVM
787  * @pba_num_size: part number string buffer length
788  *
789  * Reads the part number string from the NVM.
790  */
791 enum ice_status
792 ice_read_pba_string(struct ice_hw *hw, u8 *pba_num, u32 pba_num_size)
793 {
794 	u16 pba_tlv, pba_tlv_len;
795 	enum ice_status status;
796 	u16 pba_word, pba_size;
797 	u16 i;
798 
799 	status = ice_get_pfa_module_tlv(hw, &pba_tlv, &pba_tlv_len,
800 					ICE_SR_PBA_BLOCK_PTR);
801 	if (status != ICE_SUCCESS) {
802 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block TLV.\n");
803 		return status;
804 	}
805 
806 	/* pba_size is the next word */
807 	status = ice_read_sr_word(hw, (pba_tlv + 2), &pba_size);
808 	if (status != ICE_SUCCESS) {
809 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Section size.\n");
810 		return status;
811 	}
812 
813 	if (pba_tlv_len < pba_size) {
814 		ice_debug(hw, ICE_DBG_INIT, "Invalid PBA Block TLV size.\n");
815 		return ICE_ERR_INVAL_SIZE;
816 	}
817 
818 	/* Subtract one to get PBA word count (PBA Size word is included in
819 	 * total size)
820 	 */
821 	pba_size--;
822 	if (pba_num_size < (((u32)pba_size * 2) + 1)) {
823 		ice_debug(hw, ICE_DBG_INIT, "Buffer too small for PBA data.\n");
824 		return ICE_ERR_PARAM;
825 	}
826 
827 	for (i = 0; i < pba_size; i++) {
828 		status = ice_read_sr_word(hw, (pba_tlv + 2 + 1) + i, &pba_word);
829 		if (status != ICE_SUCCESS) {
830 			ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block word %d.\n", i);
831 			return status;
832 		}
833 
834 		pba_num[(i * 2)] = (pba_word >> 8) & 0xFF;
835 		pba_num[(i * 2) + 1] = pba_word & 0xFF;
836 	}
837 	pba_num[(pba_size * 2)] = '\0';
838 
839 	return status;
840 }
841 
842 /**
843  * ice_get_nvm_srev - Read the security revision from the NVM CSS header
844  * @hw: pointer to the HW struct
845  * @bank: whether to read from the active or inactive flash bank
846  * @srev: storage for security revision
847  *
848  * Read the security revision out of the CSS header of the active NVM module
849  * bank.
850  */
851 static enum ice_status ice_get_nvm_srev(struct ice_hw *hw, enum ice_bank_select bank, u32 *srev)
852 {
853 	enum ice_status status;
854 	u16 srev_l, srev_h;
855 
856 	status = ice_read_nvm_module(hw, bank, ICE_NVM_CSS_SREV_L, &srev_l);
857 	if (status)
858 		return status;
859 
860 	status = ice_read_nvm_module(hw, bank, ICE_NVM_CSS_SREV_H, &srev_h);
861 	if (status)
862 		return status;
863 
864 	*srev = srev_h << 16 | srev_l;
865 
866 	return ICE_SUCCESS;
867 }
868 
869 /**
870  * ice_get_nvm_ver_info - Read NVM version information
871  * @hw: pointer to the HW struct
872  * @bank: whether to read from the active or inactive flash bank
873  * @nvm: pointer to NVM info structure
874  *
875  * Read the NVM EETRACK ID and map version of the main NVM image bank, filling
876  * in the nvm info structure.
877  */
878 static enum ice_status
879 ice_get_nvm_ver_info(struct ice_hw *hw, enum ice_bank_select bank, struct ice_nvm_info *nvm)
880 {
881 	u16 eetrack_lo, eetrack_hi, ver;
882 	enum ice_status status;
883 
884 	status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_DEV_STARTER_VER, &ver);
885 	if (status) {
886 		ice_debug(hw, ICE_DBG_NVM, "Failed to read DEV starter version.\n");
887 		return status;
888 	}
889 
890 	nvm->major = (ver & ICE_NVM_VER_HI_MASK) >> ICE_NVM_VER_HI_SHIFT;
891 	nvm->minor = (ver & ICE_NVM_VER_LO_MASK) >> ICE_NVM_VER_LO_SHIFT;
892 
893 	status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_EETRACK_LO, &eetrack_lo);
894 	if (status) {
895 		ice_debug(hw, ICE_DBG_NVM, "Failed to read EETRACK lo.\n");
896 		return status;
897 	}
898 	status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_EETRACK_HI, &eetrack_hi);
899 	if (status) {
900 		ice_debug(hw, ICE_DBG_NVM, "Failed to read EETRACK hi.\n");
901 		return status;
902 	}
903 
904 	nvm->eetrack = (eetrack_hi << 16) | eetrack_lo;
905 
906 	status = ice_get_nvm_srev(hw, bank, &nvm->srev);
907 	if (status)
908 		ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM security revision.\n");
909 
910 	return ICE_SUCCESS;
911 }
912 
913 /**
914  * ice_get_inactive_nvm_ver - Read Option ROM version from the inactive bank
915  * @hw: pointer to the HW structure
916  * @nvm: storage for Option ROM version information
917  *
918  * Reads the NVM EETRACK ID, Map version, and security revision of the
919  * inactive NVM bank. Used to access version data for a pending update that
920  * has not yet been activated.
921  */
922 enum ice_status ice_get_inactive_nvm_ver(struct ice_hw *hw, struct ice_nvm_info *nvm)
923 {
924 	return ice_get_nvm_ver_info(hw, ICE_INACTIVE_FLASH_BANK, nvm);
925 }
926 
927 /**
928  * ice_get_orom_srev - Read the security revision from the OROM CSS header
929  * @hw: pointer to the HW struct
930  * @bank: whether to read from active or inactive flash module
931  * @srev: storage for security revision
932  *
933  * Read the security revision out of the CSS header of the active OROM module
934  * bank.
935  */
936 static enum ice_status ice_get_orom_srev(struct ice_hw *hw, enum ice_bank_select bank, u32 *srev)
937 {
938 	u32 orom_size_word = hw->flash.banks.orom_size / 2;
939 	enum ice_status status;
940 	u16 srev_l, srev_h;
941 	u32 css_start;
942 	u32 hdr_len;
943 
944 	status = ice_get_nvm_css_hdr_len(hw, bank, &hdr_len);
945 	if (status)
946 		return status;
947 
948 	if (orom_size_word < hdr_len) {
949 		ice_debug(hw, ICE_DBG_NVM, "Unexpected Option ROM Size of %u\n",
950 			  hw->flash.banks.orom_size);
951 		return ICE_ERR_CFG;
952 	}
953 
954 	/* calculate how far into the Option ROM the CSS header starts. Note
955 	 * that ice_read_orom_module takes a word offset
956 	 */
957 	css_start = orom_size_word - hdr_len;
958 	status = ice_read_orom_module(hw, bank, css_start + ICE_NVM_CSS_SREV_L, &srev_l);
959 	if (status)
960 		return status;
961 
962 	status = ice_read_orom_module(hw, bank, css_start + ICE_NVM_CSS_SREV_H, &srev_h);
963 	if (status)
964 		return status;
965 
966 	*srev = srev_h << 16 | srev_l;
967 
968 	return ICE_SUCCESS;
969 }
970 
971 /**
972  * ice_get_orom_civd_data - Get the combo version information from Option ROM
973  * @hw: pointer to the HW struct
974  * @bank: whether to read from the active or inactive flash module
975  * @civd: storage for the Option ROM CIVD data.
976  *
977  * Searches through the Option ROM flash contents to locate the CIVD data for
978  * the image.
979  */
980 static enum ice_status
981 ice_get_orom_civd_data(struct ice_hw *hw, enum ice_bank_select bank,
982 		       struct ice_orom_civd_info *civd)
983 {
984 	u8 *orom_data;
985 	enum ice_status status;
986 	u32 offset;
987 
988 	/* The CIVD section is located in the Option ROM aligned to 512 bytes.
989 	 * The first 4 bytes must contain the ASCII characters "$CIV".
990 	 * A simple modulo 256 sum of all of the bytes of the structure must
991 	 * equal 0.
992 	 *
993 	 * The exact location is unknown and varies between images but is
994 	 * usually somewhere in the middle of the bank. We need to scan the
995 	 * Option ROM bank to locate it.
996 	 *
997 	 * It's significantly faster to read the entire Option ROM up front
998 	 * using the maximum page size, than to read each possible location
999 	 * with a separate firmware command.
1000 	 */
1001 	orom_data = (u8 *)ice_calloc(hw, hw->flash.banks.orom_size, sizeof(u8));
1002 	if (!orom_data)
1003 		return ICE_ERR_NO_MEMORY;
1004 
1005 	status = ice_read_flash_module(hw, bank, ICE_SR_1ST_OROM_BANK_PTR, 0,
1006 				       orom_data, hw->flash.banks.orom_size);
1007 	if (status) {
1008 		ice_debug(hw, ICE_DBG_NVM, "Unable to read Option ROM data\n");
1009 		return status;
1010 	}
1011 
1012 	/* Scan the memory buffer to locate the CIVD data section */
1013 	for (offset = 0; (offset + 512) <= hw->flash.banks.orom_size; offset += 512) {
1014 		struct ice_orom_civd_info *tmp;
1015 		u8 sum = 0, i;
1016 
1017 		tmp = (struct ice_orom_civd_info *)&orom_data[offset];
1018 
1019 		/* Skip forward until we find a matching signature */
1020 		if (memcmp("$CIV", tmp->signature, sizeof(tmp->signature)) != 0)
1021 			continue;
1022 
1023 		ice_debug(hw, ICE_DBG_NVM, "Found CIVD section at offset %u\n",
1024 			  offset);
1025 
1026 		/* Verify that the simple checksum is zero */
1027 		for (i = 0; i < sizeof(*tmp); i++)
1028 			sum += ((u8 *)tmp)[i];
1029 
1030 		if (sum) {
1031 			ice_debug(hw, ICE_DBG_NVM, "Found CIVD data with invalid checksum of %u\n",
1032 				  sum);
1033 			goto err_invalid_checksum;
1034 		}
1035 
1036 		*civd = *tmp;
1037 		ice_free(hw, orom_data);
1038 		return ICE_SUCCESS;
1039 	}
1040 
1041 	ice_debug(hw, ICE_DBG_NVM, "Unable to locate CIVD data within the Option ROM\n");
1042 
1043 err_invalid_checksum:
1044 	ice_free(hw, orom_data);
1045 	return ICE_ERR_NVM;
1046 }
1047 
1048 /**
1049  * ice_get_orom_ver_info - Read Option ROM version information
1050  * @hw: pointer to the HW struct
1051  * @bank: whether to read from the active or inactive flash module
1052  * @orom: pointer to Option ROM info structure
1053  *
1054  * Read Option ROM version and security revision from the Option ROM flash
1055  * section.
1056  */
1057 static enum ice_status
1058 ice_get_orom_ver_info(struct ice_hw *hw, enum ice_bank_select bank, struct ice_orom_info *orom)
1059 {
1060 	struct ice_orom_civd_info civd;
1061 	enum ice_status status;
1062 	u32 combo_ver;
1063 
1064 	status = ice_get_orom_civd_data(hw, bank, &civd);
1065 	if (status) {
1066 		ice_debug(hw, ICE_DBG_NVM, "Failed to locate valid Option ROM CIVD data\n");
1067 		return status;
1068 	}
1069 
1070 	combo_ver = LE32_TO_CPU(civd.combo_ver);
1071 
1072 	orom->major = (u8)((combo_ver & ICE_OROM_VER_MASK) >> ICE_OROM_VER_SHIFT);
1073 	orom->patch = (u8)(combo_ver & ICE_OROM_VER_PATCH_MASK);
1074 	orom->build = (u16)((combo_ver & ICE_OROM_VER_BUILD_MASK) >> ICE_OROM_VER_BUILD_SHIFT);
1075 
1076 	status = ice_get_orom_srev(hw, bank, &orom->srev);
1077 	if (status) {
1078 		ice_debug(hw, ICE_DBG_NVM, "Failed to read Option ROM security revision.\n");
1079 		return status;
1080 	}
1081 
1082 	return ICE_SUCCESS;
1083 }
1084 
1085 /**
1086  * ice_get_inactive_orom_ver - Read Option ROM version from the inactive bank
1087  * @hw: pointer to the HW structure
1088  * @orom: storage for Option ROM version information
1089  *
1090  * Reads the Option ROM version and security revision data for the inactive
1091  * section of flash. Used to access version data for a pending update that has
1092  * not yet been activated.
1093  */
1094 enum ice_status ice_get_inactive_orom_ver(struct ice_hw *hw, struct ice_orom_info *orom)
1095 {
1096 	return ice_get_orom_ver_info(hw, ICE_INACTIVE_FLASH_BANK, orom);
1097 }
1098 
1099 /**
1100  * ice_get_netlist_info
1101  * @hw: pointer to the HW struct
1102  * @bank: whether to read from the active or inactive flash bank
1103  * @netlist: pointer to netlist version info structure
1104  *
1105  * Get the netlist version information from the requested bank. Reads the Link
1106  * Topology section to find the Netlist ID block and extract the relevant
1107  * information into the netlist version structure.
1108  */
1109 static enum ice_status
1110 ice_get_netlist_info(struct ice_hw *hw, enum ice_bank_select bank,
1111 		     struct ice_netlist_info *netlist)
1112 {
1113 	u16 module_id, length, node_count, i;
1114 	enum ice_status status;
1115 	u16 *id_blk;
1116 
1117 	status = ice_read_netlist_module(hw, bank, ICE_NETLIST_TYPE_OFFSET, &module_id);
1118 	if (status)
1119 		return status;
1120 
1121 	if (module_id != ICE_NETLIST_LINK_TOPO_MOD_ID) {
1122 		ice_debug(hw, ICE_DBG_NVM, "Expected netlist module_id ID of 0x%04x, but got 0x%04x\n",
1123 			  ICE_NETLIST_LINK_TOPO_MOD_ID, module_id);
1124 		return ICE_ERR_NVM;
1125 	}
1126 
1127 	status = ice_read_netlist_module(hw, bank, ICE_LINK_TOPO_MODULE_LEN, &length);
1128 	if (status)
1129 		return status;
1130 
1131 	/* sanity check that we have at least enough words to store the netlist ID block */
1132 	if (length < ICE_NETLIST_ID_BLK_SIZE) {
1133 		ice_debug(hw, ICE_DBG_NVM, "Netlist Link Topology module too small. Expected at least %u words, but got %u words.\n",
1134 			  ICE_NETLIST_ID_BLK_SIZE, length);
1135 		return ICE_ERR_NVM;
1136 	}
1137 
1138 	status = ice_read_netlist_module(hw, bank, ICE_LINK_TOPO_NODE_COUNT, &node_count);
1139 	if (status)
1140 		return status;
1141 	node_count &= ICE_LINK_TOPO_NODE_COUNT_M;
1142 
1143 	id_blk = (u16 *)ice_calloc(hw, ICE_NETLIST_ID_BLK_SIZE, sizeof(*id_blk));
1144 	if (!id_blk)
1145 		return ICE_ERR_NO_MEMORY;
1146 
1147 	/* Read out the entire Netlist ID Block at once. */
1148 	status = ice_read_flash_module(hw, bank, ICE_SR_NETLIST_BANK_PTR,
1149 				       ICE_NETLIST_ID_BLK_OFFSET(node_count) * sizeof(u16),
1150 				       (u8 *)id_blk, ICE_NETLIST_ID_BLK_SIZE * sizeof(u16));
1151 	if (status)
1152 		goto exit_error;
1153 
1154 	for (i = 0; i < ICE_NETLIST_ID_BLK_SIZE; i++)
1155 		id_blk[i] = LE16_TO_CPU(((_FORCE_ __le16 *)id_blk)[i]);
1156 
1157 	netlist->major = id_blk[ICE_NETLIST_ID_BLK_MAJOR_VER_HIGH] << 16 |
1158 			 id_blk[ICE_NETLIST_ID_BLK_MAJOR_VER_LOW];
1159 	netlist->minor = id_blk[ICE_NETLIST_ID_BLK_MINOR_VER_HIGH] << 16 |
1160 			 id_blk[ICE_NETLIST_ID_BLK_MINOR_VER_LOW];
1161 	netlist->type = id_blk[ICE_NETLIST_ID_BLK_TYPE_HIGH] << 16 |
1162 			id_blk[ICE_NETLIST_ID_BLK_TYPE_LOW];
1163 	netlist->rev = id_blk[ICE_NETLIST_ID_BLK_REV_HIGH] << 16 |
1164 		       id_blk[ICE_NETLIST_ID_BLK_REV_LOW];
1165 	netlist->cust_ver = id_blk[ICE_NETLIST_ID_BLK_CUST_VER];
1166 	/* Read the left most 4 bytes of SHA */
1167 	netlist->hash = id_blk[ICE_NETLIST_ID_BLK_SHA_HASH_WORD(15)] << 16 |
1168 			id_blk[ICE_NETLIST_ID_BLK_SHA_HASH_WORD(14)];
1169 
1170 exit_error:
1171 	ice_free(hw, id_blk);
1172 
1173 	return status;
1174 }
1175 
1176 /**
1177  * ice_get_netlist_ver_info
1178  * @hw: pointer to the HW struct
1179  * @netlist: pointer to netlist version info structure
1180  *
1181  * Get the netlist version information
1182  */
1183 enum ice_status ice_get_netlist_ver_info(struct ice_hw *hw, struct ice_netlist_info *netlist)
1184 {
1185 	return ice_get_netlist_info(hw, ICE_ACTIVE_FLASH_BANK, netlist);
1186 }
1187 
1188 /**
1189  * ice_get_inactive_netlist_ver
1190  * @hw: pointer to the HW struct
1191  * @netlist: pointer to netlist version info structure
1192  *
1193  * Read the netlist version data from the inactive netlist bank. Used to
1194  * extract version data of a pending flash update in order to display the
1195  * version data.
1196  */
1197 enum ice_status ice_get_inactive_netlist_ver(struct ice_hw *hw, struct ice_netlist_info *netlist)
1198 {
1199 	return ice_get_netlist_info(hw, ICE_INACTIVE_FLASH_BANK, netlist);
1200 }
1201 
1202 /**
1203  * ice_discover_flash_size - Discover the available flash size.
1204  * @hw: pointer to the HW struct
1205  *
1206  * The device flash could be up to 16MB in size. However, it is possible that
1207  * the actual size is smaller. Use bisection to determine the accessible size
1208  * of flash memory.
1209  */
1210 static enum ice_status ice_discover_flash_size(struct ice_hw *hw)
1211 {
1212 	u32 min_size = 0, max_size = ICE_AQC_NVM_MAX_OFFSET + 1;
1213 	enum ice_status status;
1214 
1215 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1216 
1217 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1218 	if (status)
1219 		return status;
1220 
1221 	while ((max_size - min_size) > 1) {
1222 		u32 offset = (max_size + min_size) / 2;
1223 		u32 len = 1;
1224 		u8 data;
1225 
1226 		status = ice_read_flat_nvm(hw, offset, &len, &data, false);
1227 		if (status == ICE_ERR_AQ_ERROR &&
1228 		    hw->adminq.sq_last_status == ICE_AQ_RC_EINVAL) {
1229 			ice_debug(hw, ICE_DBG_NVM, "%s: New upper bound of %u bytes\n",
1230 				  __func__, offset);
1231 			status = ICE_SUCCESS;
1232 			max_size = offset;
1233 		} else if (!status) {
1234 			ice_debug(hw, ICE_DBG_NVM, "%s: New lower bound of %u bytes\n",
1235 				  __func__, offset);
1236 			min_size = offset;
1237 		} else {
1238 			/* an unexpected error occurred */
1239 			goto err_read_flat_nvm;
1240 		}
1241 	}
1242 
1243 	ice_debug(hw, ICE_DBG_NVM, "Predicted flash size is %u bytes\n", max_size);
1244 
1245 	hw->flash.flash_size = max_size;
1246 
1247 err_read_flat_nvm:
1248 	ice_release_nvm(hw);
1249 
1250 	return status;
1251 }
1252 
1253 /**
1254  * ice_read_sr_pointer - Read the value of a Shadow RAM pointer word
1255  * @hw: pointer to the HW structure
1256  * @offset: the word offset of the Shadow RAM word to read
1257  * @pointer: pointer value read from Shadow RAM
1258  *
1259  * Read the given Shadow RAM word, and convert it to a pointer value specified
1260  * in bytes. This function assumes the specified offset is a valid pointer
1261  * word.
1262  *
1263  * Each pointer word specifies whether it is stored in word size or 4KB
1264  * sector size by using the highest bit. The reported pointer value will be in
1265  * bytes, intended for flat NVM reads.
1266  */
1267 static enum ice_status
1268 ice_read_sr_pointer(struct ice_hw *hw, u16 offset, u32 *pointer)
1269 {
1270 	enum ice_status status;
1271 	u16 value;
1272 
1273 	status = ice_read_sr_word(hw, offset, &value);
1274 	if (status)
1275 		return status;
1276 
1277 	/* Determine if the pointer is in 4KB or word units */
1278 	if (value & ICE_SR_NVM_PTR_4KB_UNITS)
1279 		*pointer = (value & ~ICE_SR_NVM_PTR_4KB_UNITS) * 4 * 1024;
1280 	else
1281 		*pointer = value * 2;
1282 
1283 	return ICE_SUCCESS;
1284 }
1285 
1286 /**
1287  * ice_read_sr_area_size - Read an area size from a Shadow RAM word
1288  * @hw: pointer to the HW structure
1289  * @offset: the word offset of the Shadow RAM to read
1290  * @size: size value read from the Shadow RAM
1291  *
1292  * Read the given Shadow RAM word, and convert it to an area size value
1293  * specified in bytes. This function assumes the specified offset is a valid
1294  * area size word.
1295  *
1296  * Each area size word is specified in 4KB sector units. This function reports
1297  * the size in bytes, intended for flat NVM reads.
1298  */
1299 static enum ice_status
1300 ice_read_sr_area_size(struct ice_hw *hw, u16 offset, u32 *size)
1301 {
1302 	enum ice_status status;
1303 	u16 value;
1304 
1305 	status = ice_read_sr_word(hw, offset, &value);
1306 	if (status)
1307 		return status;
1308 
1309 	/* Area sizes are always specified in 4KB units */
1310 	*size = value * 4 * 1024;
1311 
1312 	return ICE_SUCCESS;
1313 }
1314 
1315 /**
1316  * ice_determine_active_flash_banks - Discover active bank for each module
1317  * @hw: pointer to the HW struct
1318  *
1319  * Read the Shadow RAM control word and determine which banks are active for
1320  * the NVM, OROM, and Netlist modules. Also read and calculate the associated
1321  * pointer and size. These values are then cached into the ice_flash_info
1322  * structure for later use in order to calculate the correct offset to read
1323  * from the active module.
1324  */
1325 static enum ice_status
1326 ice_determine_active_flash_banks(struct ice_hw *hw)
1327 {
1328 	struct ice_bank_info *banks = &hw->flash.banks;
1329 	enum ice_status status;
1330 	u16 ctrl_word;
1331 
1332 	status = ice_read_sr_word(hw, ICE_SR_NVM_CTRL_WORD, &ctrl_word);
1333 	if (status) {
1334 		ice_debug(hw, ICE_DBG_NVM, "Failed to read the Shadow RAM control word\n");
1335 		return status;
1336 	}
1337 
1338 	/* Check that the control word indicates validity */
1339 	if ((ctrl_word & ICE_SR_CTRL_WORD_1_M) >> ICE_SR_CTRL_WORD_1_S != ICE_SR_CTRL_WORD_VALID) {
1340 		ice_debug(hw, ICE_DBG_NVM, "Shadow RAM control word is invalid\n");
1341 		return ICE_ERR_CFG;
1342 	}
1343 
1344 	if (!(ctrl_word & ICE_SR_CTRL_WORD_NVM_BANK))
1345 		banks->nvm_bank = ICE_1ST_FLASH_BANK;
1346 	else
1347 		banks->nvm_bank = ICE_2ND_FLASH_BANK;
1348 
1349 	if (!(ctrl_word & ICE_SR_CTRL_WORD_OROM_BANK))
1350 		banks->orom_bank = ICE_1ST_FLASH_BANK;
1351 	else
1352 		banks->orom_bank = ICE_2ND_FLASH_BANK;
1353 
1354 	if (!(ctrl_word & ICE_SR_CTRL_WORD_NETLIST_BANK))
1355 		banks->netlist_bank = ICE_1ST_FLASH_BANK;
1356 	else
1357 		banks->netlist_bank = ICE_2ND_FLASH_BANK;
1358 
1359 	status = ice_read_sr_pointer(hw, ICE_SR_1ST_NVM_BANK_PTR, &banks->nvm_ptr);
1360 	if (status) {
1361 		ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank pointer\n");
1362 		return status;
1363 	}
1364 
1365 	status = ice_read_sr_area_size(hw, ICE_SR_NVM_BANK_SIZE, &banks->nvm_size);
1366 	if (status) {
1367 		ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank area size\n");
1368 		return status;
1369 	}
1370 
1371 	status = ice_read_sr_pointer(hw, ICE_SR_1ST_OROM_BANK_PTR, &banks->orom_ptr);
1372 	if (status) {
1373 		ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank pointer\n");
1374 		return status;
1375 	}
1376 
1377 	status = ice_read_sr_area_size(hw, ICE_SR_OROM_BANK_SIZE, &banks->orom_size);
1378 	if (status) {
1379 		ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank area size\n");
1380 		return status;
1381 	}
1382 
1383 	status = ice_read_sr_pointer(hw, ICE_SR_NETLIST_BANK_PTR, &banks->netlist_ptr);
1384 	if (status) {
1385 		ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank pointer\n");
1386 		return status;
1387 	}
1388 
1389 	status = ice_read_sr_area_size(hw, ICE_SR_NETLIST_BANK_SIZE, &banks->netlist_size);
1390 	if (status) {
1391 		ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank area size\n");
1392 		return status;
1393 	}
1394 
1395 	return ICE_SUCCESS;
1396 }
1397 
1398 /**
1399  * ice_init_nvm - initializes NVM setting
1400  * @hw: pointer to the HW struct
1401  *
1402  * This function reads and populates NVM settings such as Shadow RAM size,
1403  * max_timeout, and blank_nvm_mode
1404  */
1405 enum ice_status ice_init_nvm(struct ice_hw *hw)
1406 {
1407 	struct ice_flash_info *flash = &hw->flash;
1408 	enum ice_status status;
1409 	u32 fla, gens_stat;
1410 	u8 sr_size;
1411 
1412 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1413 
1414 	/* The SR size is stored regardless of the NVM programming mode
1415 	 * as the blank mode may be used in the factory line.
1416 	 */
1417 	gens_stat = rd32(hw, GLNVM_GENS);
1418 	sr_size = (gens_stat & GLNVM_GENS_SR_SIZE_M) >> GLNVM_GENS_SR_SIZE_S;
1419 
1420 	/* Switching to words (sr_size contains power of 2) */
1421 	flash->sr_words = BIT(sr_size) * ICE_SR_WORDS_IN_1KB;
1422 
1423 	/* Check if we are in the normal or blank NVM programming mode */
1424 	fla = rd32(hw, GLNVM_FLA);
1425 	if (fla & GLNVM_FLA_LOCKED_M) { /* Normal programming mode */
1426 		flash->blank_nvm_mode = false;
1427 	} else {
1428 		/* Blank programming mode */
1429 		flash->blank_nvm_mode = true;
1430 		ice_debug(hw, ICE_DBG_NVM, "NVM init error: unsupported blank mode.\n");
1431 		return ICE_ERR_NVM_BLANK_MODE;
1432 	}
1433 
1434 	status = ice_discover_flash_size(hw);
1435 	if (status) {
1436 		ice_debug(hw, ICE_DBG_NVM, "NVM init error: failed to discover flash size.\n");
1437 		return status;
1438 	}
1439 
1440 	status = ice_determine_active_flash_banks(hw);
1441 	if (status) {
1442 		ice_debug(hw, ICE_DBG_NVM, "Failed to determine active flash banks.\n");
1443 		return status;
1444 	}
1445 
1446 	status = ice_get_nvm_ver_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->nvm);
1447 	if (status) {
1448 		ice_debug(hw, ICE_DBG_INIT, "Failed to read NVM info.\n");
1449 		return status;
1450 	}
1451 
1452 	status = ice_get_orom_ver_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->orom);
1453 	if (status)
1454 		ice_debug(hw, ICE_DBG_INIT, "Failed to read Option ROM info.\n");
1455 
1456 	/* read the netlist version information */
1457 	status = ice_get_netlist_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->netlist);
1458 	if (status)
1459 		ice_debug(hw, ICE_DBG_INIT, "Failed to read netlist info.\n");
1460 	return ICE_SUCCESS;
1461 }
1462 
1463 /**
1464  * ice_read_sr_buf - Reads Shadow RAM buf and acquire lock if necessary
1465  * @hw: pointer to the HW structure
1466  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
1467  * @words: (in) number of words to read; (out) number of words actually read
1468  * @data: words read from the Shadow RAM
1469  *
1470  * Reads 16 bit words (data buf) from the SR using the ice_read_nvm_buf_aq
1471  * method. The buf read is preceded by the NVM ownership take
1472  * and followed by the release.
1473  */
1474 enum ice_status
1475 ice_read_sr_buf(struct ice_hw *hw, u16 offset, u16 *words, u16 *data)
1476 {
1477 	enum ice_status status;
1478 
1479 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1480 	if (!status) {
1481 		status = ice_read_sr_buf_aq(hw, offset, words, data);
1482 		ice_release_nvm(hw);
1483 	}
1484 
1485 	return status;
1486 }
1487 
1488 /**
1489  * __ice_write_sr_word - Writes Shadow RAM word
1490  * @hw: pointer to the HW structure
1491  * @offset: offset of the Shadow RAM word to write
1492  * @data: word to write to the Shadow RAM
1493  *
1494  * Writes a 16 bit word to the SR using the ice_write_sr_aq method.
1495  * NVM ownership have to be acquired and released (on ARQ completion event
1496  * reception) by caller. To commit SR to NVM update checksum function
1497  * should be called.
1498  */
1499 enum ice_status
1500 __ice_write_sr_word(struct ice_hw *hw, u32 offset, const u16 *data)
1501 {
1502 	__le16 data_local = CPU_TO_LE16(*data);
1503 
1504 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1505 
1506 	/* Value 0x00 below means that we treat SR as a flat mem */
1507 	return ice_write_sr_aq(hw, offset, 1, &data_local, false);
1508 }
1509 
1510 /**
1511  * __ice_write_sr_buf - Writes Shadow RAM buf
1512  * @hw: pointer to the HW structure
1513  * @offset: offset of the Shadow RAM buffer to write
1514  * @words: number of words to write
1515  * @data: words to write to the Shadow RAM
1516  *
1517  * Writes a 16 bit words buffer to the Shadow RAM using the admin command.
1518  * NVM ownership must be acquired before calling this function and released
1519  * on ARQ completion event reception by caller. To commit SR to NVM update
1520  * checksum function should be called.
1521  */
1522 enum ice_status
1523 __ice_write_sr_buf(struct ice_hw *hw, u32 offset, u16 words, const u16 *data)
1524 {
1525 	enum ice_status status;
1526 	__le16 *data_local;
1527 	void *vmem;
1528 	u32 i;
1529 
1530 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1531 
1532 	vmem = ice_calloc(hw, words, sizeof(u16));
1533 	if (!vmem)
1534 		return ICE_ERR_NO_MEMORY;
1535 	data_local = (_FORCE_ __le16 *)vmem;
1536 
1537 	for (i = 0; i < words; i++)
1538 		data_local[i] = CPU_TO_LE16(data[i]);
1539 
1540 	/* Here we will only write one buffer as the size of the modules
1541 	 * mirrored in the Shadow RAM is always less than 4K.
1542 	 */
1543 	status = ice_write_sr_aq(hw, offset, words, data_local, false);
1544 
1545 	ice_free(hw, vmem);
1546 
1547 	return status;
1548 }
1549 
1550 /**
1551  * ice_calc_sr_checksum - Calculates and returns Shadow RAM SW checksum
1552  * @hw: pointer to hardware structure
1553  * @checksum: pointer to the checksum
1554  *
1555  * This function calculates SW Checksum that covers the whole 64kB shadow RAM
1556  * except the VPD and PCIe ALT Auto-load modules. The structure and size of VPD
1557  * is customer specific and unknown. Therefore, this function skips all maximum
1558  * possible size of VPD (1kB).
1559  */
1560 static enum ice_status ice_calc_sr_checksum(struct ice_hw *hw, u16 *checksum)
1561 {
1562 	enum ice_status status = ICE_SUCCESS;
1563 	u16 pcie_alt_module = 0;
1564 	u16 checksum_local = 0;
1565 	u16 vpd_module;
1566 	void *vmem;
1567 	u16 *data;
1568 	u16 i;
1569 
1570 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1571 
1572 	vmem = ice_calloc(hw, ICE_SR_SECTOR_SIZE_IN_WORDS, sizeof(u16));
1573 	if (!vmem)
1574 		return ICE_ERR_NO_MEMORY;
1575 	data = (u16 *)vmem;
1576 
1577 	/* read pointer to VPD area */
1578 	status = ice_read_sr_word_aq(hw, ICE_SR_VPD_PTR, &vpd_module);
1579 	if (status)
1580 		goto ice_calc_sr_checksum_exit;
1581 
1582 	/* read pointer to PCIe Alt Auto-load module */
1583 	status = ice_read_sr_word_aq(hw, ICE_SR_PCIE_ALT_AUTO_LOAD_PTR,
1584 				     &pcie_alt_module);
1585 	if (status)
1586 		goto ice_calc_sr_checksum_exit;
1587 
1588 	/* Calculate SW checksum that covers the whole 64kB shadow RAM
1589 	 * except the VPD and PCIe ALT Auto-load modules
1590 	 */
1591 	for (i = 0; i < hw->flash.sr_words; i++) {
1592 		/* Read SR page */
1593 		if ((i % ICE_SR_SECTOR_SIZE_IN_WORDS) == 0) {
1594 			u16 words = ICE_SR_SECTOR_SIZE_IN_WORDS;
1595 
1596 			status = ice_read_sr_buf_aq(hw, i, &words, data);
1597 			if (status != ICE_SUCCESS)
1598 				goto ice_calc_sr_checksum_exit;
1599 		}
1600 
1601 		/* Skip Checksum word */
1602 		if (i == ICE_SR_SW_CHECKSUM_WORD)
1603 			continue;
1604 		/* Skip VPD module (convert byte size to word count) */
1605 		if (i >= (u32)vpd_module &&
1606 		    i < ((u32)vpd_module + ICE_SR_VPD_SIZE_WORDS))
1607 			continue;
1608 		/* Skip PCIe ALT module (convert byte size to word count) */
1609 		if (i >= (u32)pcie_alt_module &&
1610 		    i < ((u32)pcie_alt_module + ICE_SR_PCIE_ALT_SIZE_WORDS))
1611 			continue;
1612 
1613 		checksum_local += data[i % ICE_SR_SECTOR_SIZE_IN_WORDS];
1614 	}
1615 
1616 	*checksum = (u16)ICE_SR_SW_CHECKSUM_BASE - checksum_local;
1617 
1618 ice_calc_sr_checksum_exit:
1619 	ice_free(hw, vmem);
1620 	return status;
1621 }
1622 
1623 /**
1624  * ice_update_sr_checksum - Updates the Shadow RAM SW checksum
1625  * @hw: pointer to hardware structure
1626  *
1627  * NVM ownership must be acquired before calling this function and released
1628  * on ARQ completion event reception by caller.
1629  * This function will commit SR to NVM.
1630  */
1631 enum ice_status ice_update_sr_checksum(struct ice_hw *hw)
1632 {
1633 	enum ice_status status;
1634 	__le16 le_sum;
1635 	u16 checksum;
1636 
1637 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1638 
1639 	status = ice_calc_sr_checksum(hw, &checksum);
1640 	if (!status) {
1641 		le_sum = CPU_TO_LE16(checksum);
1642 		status = ice_write_sr_aq(hw, ICE_SR_SW_CHECKSUM_WORD, 1,
1643 					 &le_sum, true);
1644 	}
1645 	return status;
1646 }
1647 
1648 /**
1649  * ice_validate_sr_checksum - Validate Shadow RAM SW checksum
1650  * @hw: pointer to hardware structure
1651  * @checksum: calculated checksum
1652  *
1653  * Performs checksum calculation and validates the Shadow RAM SW checksum.
1654  * If the caller does not need checksum, the value can be NULL.
1655  */
1656 enum ice_status ice_validate_sr_checksum(struct ice_hw *hw, u16 *checksum)
1657 {
1658 	enum ice_status status;
1659 	u16 checksum_local;
1660 	u16 checksum_sr;
1661 
1662 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1663 
1664 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1665 	if (!status) {
1666 		status = ice_calc_sr_checksum(hw, &checksum_local);
1667 		ice_release_nvm(hw);
1668 		if (status)
1669 			return status;
1670 	} else {
1671 		return status;
1672 	}
1673 
1674 	ice_read_sr_word(hw, ICE_SR_SW_CHECKSUM_WORD, &checksum_sr);
1675 
1676 	/* Verify read checksum from EEPROM is the same as
1677 	 * calculated checksum
1678 	 */
1679 	if (checksum_local != checksum_sr)
1680 		status = ICE_ERR_NVM_CHECKSUM;
1681 
1682 	/* If the user cares, return the calculated checksum */
1683 	if (checksum)
1684 		*checksum = checksum_local;
1685 
1686 	return status;
1687 }
1688 
1689 /**
1690  * ice_nvm_validate_checksum
1691  * @hw: pointer to the HW struct
1692  *
1693  * Verify NVM PFA checksum validity (0x0706)
1694  */
1695 enum ice_status ice_nvm_validate_checksum(struct ice_hw *hw)
1696 {
1697 	struct ice_aqc_nvm_checksum *cmd;
1698 	struct ice_aq_desc desc;
1699 	enum ice_status status;
1700 
1701 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1702 	if (status)
1703 		return status;
1704 
1705 	cmd = &desc.params.nvm_checksum;
1706 
1707 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_checksum);
1708 	cmd->flags = ICE_AQC_NVM_CHECKSUM_VERIFY;
1709 
1710 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1711 
1712 	ice_release_nvm(hw);
1713 
1714 	if (!status)
1715 		if (LE16_TO_CPU(cmd->checksum) != ICE_AQC_NVM_CHECKSUM_CORRECT)
1716 			status = ICE_ERR_NVM_CHECKSUM;
1717 
1718 	return status;
1719 }
1720 
1721 /**
1722  * ice_nvm_recalculate_checksum
1723  * @hw: pointer to the HW struct
1724  *
1725  * Recalculate NVM PFA checksum (0x0706)
1726  */
1727 enum ice_status ice_nvm_recalculate_checksum(struct ice_hw *hw)
1728 {
1729 	struct ice_aqc_nvm_checksum *cmd;
1730 	struct ice_aq_desc desc;
1731 	enum ice_status status;
1732 
1733 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1734 	if (status)
1735 		return status;
1736 
1737 	cmd = &desc.params.nvm_checksum;
1738 
1739 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_checksum);
1740 	cmd->flags = ICE_AQC_NVM_CHECKSUM_RECALC;
1741 
1742 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1743 
1744 	ice_release_nvm(hw);
1745 
1746 	return status;
1747 }
1748 
1749 /**
1750  * ice_nvm_write_activate
1751  * @hw: pointer to the HW struct
1752  * @cmd_flags: flags for write activate command
1753  * @response_flags: response indicators from firmware
1754  *
1755  * Update the control word with the required banks' validity bits
1756  * and dumps the Shadow RAM to flash (0x0707)
1757  *
1758  * cmd_flags controls which banks to activate, the preservation level to use
1759  * when activating the NVM bank, and whether an EMP reset is required for
1760  * activation.
1761  *
1762  * Note that the 16bit cmd_flags value is split between two separate 1 byte
1763  * flag values in the descriptor.
1764  *
1765  * On successful return of the firmware command, the response_flags variable
1766  * is updated with the flags reported by firmware indicating certain status,
1767  * such as whether EMP reset is enabled.
1768  */
1769 enum ice_status
1770 ice_nvm_write_activate(struct ice_hw *hw, u16 cmd_flags, u8 *response_flags)
1771 {
1772 	struct ice_aqc_nvm *cmd;
1773 	struct ice_aq_desc desc;
1774 	enum ice_status status;
1775 
1776 	cmd = &desc.params.nvm;
1777 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_write_activate);
1778 
1779 	cmd->cmd_flags = ICE_LO_BYTE(cmd_flags);
1780 	cmd->offset_high = ICE_HI_BYTE(cmd_flags);
1781 
1782 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1783 	if (!status && response_flags)
1784 		*response_flags = cmd->cmd_flags;
1785 
1786 	return status;
1787 }
1788 
1789 /**
1790  * ice_get_nvm_minsrevs - Get the Minimum Security Revision values from flash
1791  * @hw: pointer to the HW struct
1792  * @minsrevs: structure to store NVM and OROM minsrev values
1793  *
1794  * Read the Minimum Security Revision TLV and extract the revision values from
1795  * the flash image into a readable structure for processing.
1796  */
1797 enum ice_status
1798 ice_get_nvm_minsrevs(struct ice_hw *hw, struct ice_minsrev_info *minsrevs)
1799 {
1800 	struct ice_aqc_nvm_minsrev data;
1801 	enum ice_status status;
1802 	u16 valid;
1803 
1804 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1805 
1806 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1807 	if (status)
1808 		return status;
1809 
1810 	status = ice_aq_read_nvm(hw, ICE_AQC_NVM_MINSREV_MOD_ID, 0, sizeof(data),
1811 				 &data, true, false, NULL);
1812 
1813 	ice_release_nvm(hw);
1814 
1815 	if (status)
1816 		return status;
1817 
1818 	valid = LE16_TO_CPU(data.validity);
1819 
1820 	/* Extract NVM minimum security revision */
1821 	if (valid & ICE_AQC_NVM_MINSREV_NVM_VALID) {
1822 		u16 minsrev_l, minsrev_h;
1823 
1824 		minsrev_l = LE16_TO_CPU(data.nvm_minsrev_l);
1825 		minsrev_h = LE16_TO_CPU(data.nvm_minsrev_h);
1826 
1827 		minsrevs->nvm = minsrev_h << 16 | minsrev_l;
1828 		minsrevs->nvm_valid = true;
1829 	}
1830 
1831 	/* Extract the OROM minimum security revision */
1832 	if (valid & ICE_AQC_NVM_MINSREV_OROM_VALID) {
1833 		u16 minsrev_l, minsrev_h;
1834 
1835 		minsrev_l = LE16_TO_CPU(data.orom_minsrev_l);
1836 		minsrev_h = LE16_TO_CPU(data.orom_minsrev_h);
1837 
1838 		minsrevs->orom = minsrev_h << 16 | minsrev_l;
1839 		minsrevs->orom_valid = true;
1840 	}
1841 
1842 	return ICE_SUCCESS;
1843 }
1844 
1845 /**
1846  * ice_update_nvm_minsrevs - Update minimum security revision TLV data in flash
1847  * @hw: pointer to the HW struct
1848  * @minsrevs: minimum security revision information
1849  *
1850  * Update the NVM or Option ROM minimum security revision fields in the PFA
1851  * area of the flash. Reads the minsrevs->nvm_valid and minsrevs->orom_valid
1852  * fields to determine what update is being requested. If the valid bit is not
1853  * set for that module, then the associated minsrev will be left as is.
1854  */
1855 enum ice_status
1856 ice_update_nvm_minsrevs(struct ice_hw *hw, struct ice_minsrev_info *minsrevs)
1857 {
1858 	struct ice_aqc_nvm_minsrev data;
1859 	enum ice_status status;
1860 
1861 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1862 
1863 	if (!minsrevs->nvm_valid && !minsrevs->orom_valid) {
1864 		ice_debug(hw, ICE_DBG_NVM, "At least one of NVM and OROM MinSrev must be valid");
1865 		return ICE_ERR_PARAM;
1866 	}
1867 
1868 	status = ice_acquire_nvm(hw, ICE_RES_WRITE);
1869 	if (status)
1870 		return status;
1871 
1872 	/* Get current data */
1873 	status = ice_aq_read_nvm(hw, ICE_AQC_NVM_MINSREV_MOD_ID, 0, sizeof(data),
1874 				 &data, true, false, NULL);
1875 	if (status)
1876 		goto exit_release_res;
1877 
1878 	if (minsrevs->nvm_valid) {
1879 		data.nvm_minsrev_l = CPU_TO_LE16(minsrevs->nvm & 0xFFFF);
1880 		data.nvm_minsrev_h = CPU_TO_LE16(minsrevs->nvm >> 16);
1881 		data.validity |= CPU_TO_LE16(ICE_AQC_NVM_MINSREV_NVM_VALID);
1882 	}
1883 
1884 	if (minsrevs->orom_valid) {
1885 		data.orom_minsrev_l = CPU_TO_LE16(minsrevs->orom & 0xFFFF);
1886 		data.orom_minsrev_h = CPU_TO_LE16(minsrevs->orom >> 16);
1887 		data.validity |= CPU_TO_LE16(ICE_AQC_NVM_MINSREV_OROM_VALID);
1888 	}
1889 
1890 	/* Update flash data */
1891 	status = ice_aq_update_nvm(hw, ICE_AQC_NVM_MINSREV_MOD_ID, 0, sizeof(data), &data,
1892 				   false, ICE_AQC_NVM_SPECIAL_UPDATE, NULL);
1893 	if (status)
1894 		goto exit_release_res;
1895 
1896 	/* Dump the Shadow RAM to the flash */
1897 	status = ice_nvm_write_activate(hw, 0, NULL);
1898 
1899 exit_release_res:
1900 	ice_release_nvm(hw);
1901 
1902 	return status;
1903 }
1904 
1905 /**
1906  * ice_nvm_access_get_features - Return the NVM access features structure
1907  * @cmd: NVM access command to process
1908  * @data: storage for the driver NVM features
1909  *
1910  * Fill in the data section of the NVM access request with a copy of the NVM
1911  * features structure.
1912  */
1913 enum ice_status
1914 ice_nvm_access_get_features(struct ice_nvm_access_cmd *cmd,
1915 			    union ice_nvm_access_data *data)
1916 {
1917 	/* The provided data_size must be at least as large as our NVM
1918 	 * features structure. A larger size should not be treated as an
1919 	 * error, to allow future extensions to the features structure to
1920 	 * work on older drivers.
1921 	 */
1922 	if (cmd->data_size < sizeof(struct ice_nvm_features))
1923 		return ICE_ERR_NO_MEMORY;
1924 
1925 	/* Initialize the data buffer to zeros */
1926 	ice_memset(data, 0, cmd->data_size, ICE_NONDMA_MEM);
1927 
1928 	/* Fill in the features data */
1929 	data->drv_features.major = ICE_NVM_ACCESS_MAJOR_VER;
1930 	data->drv_features.minor = ICE_NVM_ACCESS_MINOR_VER;
1931 	data->drv_features.size = sizeof(struct ice_nvm_features);
1932 	data->drv_features.features[0] = ICE_NVM_FEATURES_0_REG_ACCESS;
1933 
1934 	return ICE_SUCCESS;
1935 }
1936 
1937 /**
1938  * ice_nvm_access_get_module - Helper function to read module value
1939  * @cmd: NVM access command structure
1940  *
1941  * Reads the module value out of the NVM access config field.
1942  */
1943 u32 ice_nvm_access_get_module(struct ice_nvm_access_cmd *cmd)
1944 {
1945 	return ((cmd->config & ICE_NVM_CFG_MODULE_M) >> ICE_NVM_CFG_MODULE_S);
1946 }
1947 
1948 /**
1949  * ice_nvm_access_get_flags - Helper function to read flags value
1950  * @cmd: NVM access command structure
1951  *
1952  * Reads the flags value out of the NVM access config field.
1953  */
1954 u32 ice_nvm_access_get_flags(struct ice_nvm_access_cmd *cmd)
1955 {
1956 	return ((cmd->config & ICE_NVM_CFG_FLAGS_M) >> ICE_NVM_CFG_FLAGS_S);
1957 }
1958 
1959 /**
1960  * ice_nvm_access_get_adapter - Helper function to read adapter info
1961  * @cmd: NVM access command structure
1962  *
1963  * Read the adapter info value out of the NVM access config field.
1964  */
1965 u32 ice_nvm_access_get_adapter(struct ice_nvm_access_cmd *cmd)
1966 {
1967 	return ((cmd->config & ICE_NVM_CFG_ADAPTER_INFO_M) >>
1968 		ICE_NVM_CFG_ADAPTER_INFO_S);
1969 }
1970 
1971 /**
1972  * ice_validate_nvm_rw_reg - Check than an NVM access request is valid
1973  * @cmd: NVM access command structure
1974  *
1975  * Validates that an NVM access structure is request to read or write a valid
1976  * register offset. First validates that the module and flags are correct, and
1977  * then ensures that the register offset is one of the accepted registers.
1978  */
1979 static enum ice_status
1980 ice_validate_nvm_rw_reg(struct ice_nvm_access_cmd *cmd)
1981 {
1982 	u32 module, flags, offset;
1983 	u16 i;
1984 
1985 	module = ice_nvm_access_get_module(cmd);
1986 	flags = ice_nvm_access_get_flags(cmd);
1987 	offset = cmd->offset;
1988 
1989 	/* Make sure the module and flags indicate a read/write request */
1990 	if (module != ICE_NVM_REG_RW_MODULE ||
1991 	    flags != ICE_NVM_REG_RW_FLAGS ||
1992 	    cmd->data_size != FIELD_SIZEOF(union ice_nvm_access_data, regval))
1993 		return ICE_ERR_PARAM;
1994 
1995 	switch (offset) {
1996 	case GL_HICR:
1997 	case GL_HICR_EN: /* Note, this register is read only */
1998 	case GL_FWSTS:
1999 	case GL_MNG_FWSM:
2000 	case GLGEN_CSR_DEBUG_C:
2001 	case GLGEN_RSTAT:
2002 	case GLPCI_LBARCTRL:
2003 	case GL_MNG_DEF_DEVID:
2004 	case GLNVM_GENS:
2005 	case GLNVM_FLA:
2006 	case PF_FUNC_RID:
2007 		return ICE_SUCCESS;
2008 	default:
2009 		break;
2010 	}
2011 
2012 	for (i = 0; i <= GL_HIDA_MAX_INDEX; i++)
2013 		if (offset == (u32)GL_HIDA(i))
2014 			return ICE_SUCCESS;
2015 
2016 	for (i = 0; i <= GL_HIBA_MAX_INDEX; i++)
2017 		if (offset == (u32)GL_HIBA(i))
2018 			return ICE_SUCCESS;
2019 
2020 	/* All other register offsets are not valid */
2021 	return ICE_ERR_OUT_OF_RANGE;
2022 }
2023 
2024 /**
2025  * ice_nvm_access_read - Handle an NVM read request
2026  * @hw: pointer to the HW struct
2027  * @cmd: NVM access command to process
2028  * @data: storage for the register value read
2029  *
2030  * Process an NVM access request to read a register.
2031  */
2032 enum ice_status
2033 ice_nvm_access_read(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
2034 		    union ice_nvm_access_data *data)
2035 {
2036 	enum ice_status status;
2037 
2038 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
2039 
2040 	/* Always initialize the output data, even on failure */
2041 	ice_memset(data, 0, cmd->data_size, ICE_NONDMA_MEM);
2042 
2043 	/* Make sure this is a valid read/write access request */
2044 	status = ice_validate_nvm_rw_reg(cmd);
2045 	if (status)
2046 		return status;
2047 
2048 	ice_debug(hw, ICE_DBG_NVM, "NVM access: reading register %08x\n",
2049 		  cmd->offset);
2050 
2051 	/* Read the register and store the contents in the data field */
2052 	data->regval = rd32(hw, cmd->offset);
2053 
2054 	return ICE_SUCCESS;
2055 }
2056 
2057 /**
2058  * ice_nvm_access_write - Handle an NVM write request
2059  * @hw: pointer to the HW struct
2060  * @cmd: NVM access command to process
2061  * @data: NVM access data to write
2062  *
2063  * Process an NVM access request to write a register.
2064  */
2065 enum ice_status
2066 ice_nvm_access_write(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
2067 		     union ice_nvm_access_data *data)
2068 {
2069 	enum ice_status status;
2070 
2071 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
2072 
2073 	/* Make sure this is a valid read/write access request */
2074 	status = ice_validate_nvm_rw_reg(cmd);
2075 	if (status)
2076 		return status;
2077 
2078 	/* Reject requests to write to read-only registers */
2079 	switch (cmd->offset) {
2080 	case GL_HICR_EN:
2081 	case GLGEN_RSTAT:
2082 		return ICE_ERR_OUT_OF_RANGE;
2083 	default:
2084 		break;
2085 	}
2086 
2087 	ice_debug(hw, ICE_DBG_NVM, "NVM access: writing register %08x with value %08x\n",
2088 		  cmd->offset, data->regval);
2089 
2090 	/* Write the data field to the specified register */
2091 	wr32(hw, cmd->offset, data->regval);
2092 
2093 	return ICE_SUCCESS;
2094 }
2095 
2096 /**
2097  * ice_handle_nvm_access - Handle an NVM access request
2098  * @hw: pointer to the HW struct
2099  * @cmd: NVM access command info
2100  * @data: pointer to read or return data
2101  *
2102  * Process an NVM access request. Read the command structure information and
2103  * determine if it is valid. If not, report an error indicating the command
2104  * was invalid.
2105  *
2106  * For valid commands, perform the necessary function, copying the data into
2107  * the provided data buffer.
2108  */
2109 enum ice_status
2110 ice_handle_nvm_access(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
2111 		      union ice_nvm_access_data *data)
2112 {
2113 	u32 module, flags, adapter_info;
2114 
2115 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
2116 
2117 	/* Extended flags are currently reserved and must be zero */
2118 	if ((cmd->config & ICE_NVM_CFG_EXT_FLAGS_M) != 0)
2119 		return ICE_ERR_PARAM;
2120 
2121 	/* Adapter info must match the HW device ID */
2122 	adapter_info = ice_nvm_access_get_adapter(cmd);
2123 	if (adapter_info != hw->device_id)
2124 		return ICE_ERR_PARAM;
2125 
2126 	switch (cmd->command) {
2127 	case ICE_NVM_CMD_READ:
2128 		module = ice_nvm_access_get_module(cmd);
2129 		flags = ice_nvm_access_get_flags(cmd);
2130 
2131 		/* Getting the driver's NVM features structure shares the same
2132 		 * command type as reading a register. Read the config field
2133 		 * to determine if this is a request to get features.
2134 		 */
2135 		if (module == ICE_NVM_GET_FEATURES_MODULE &&
2136 		    flags == ICE_NVM_GET_FEATURES_FLAGS &&
2137 		    cmd->offset == 0)
2138 			return ice_nvm_access_get_features(cmd, data);
2139 		else
2140 			return ice_nvm_access_read(hw, cmd, data);
2141 	case ICE_NVM_CMD_WRITE:
2142 		return ice_nvm_access_write(hw, cmd, data);
2143 	default:
2144 		return ICE_ERR_PARAM;
2145 	}
2146 }
2147 
2148