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