xref: /freebsd/sys/dev/ice/ice_nvm.c (revision 3750ccefb8629a08890bfbae894dd6bc6a7483b4)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*  Copyright (c) 2024, 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 int
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 int
96 ice_read_flat_nvm(struct ice_hw *hw, u32 offset, u32 *length, u8 *data,
97 		  bool read_shadow_ram)
98 {
99 	u32 inlen = *length;
100 	u32 bytes_read = 0;
101 	bool last_cmd;
102 	int status;
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 int
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 int 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 	int 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 int
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 	int 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 int
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 int
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 0;
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 int ice_read_sr_word_aq(struct ice_hw *hw, u16 offset, u16 *data)
337 {
338 	u32 bytes = sizeof(u16);
339 	__le16 data_local;
340 	int status;
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 0;
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 int
368 ice_write_sr_aq(struct ice_hw *hw, u32 offset, u16 words, __le16 *data,
369 		bool last_command)
370 {
371 	int 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 int
394 ice_read_sr_buf_aq(struct ice_hw *hw, u16 offset, u16 *words, u16 *data)
395 {
396 	u32 bytes = *words * 2, i;
397 	int 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 = (u16)(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 int ice_acquire_nvm(struct ice_hw *hw, enum ice_aq_res_access_type access)
424 {
425 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
426 	if (hw->flash.blank_nvm_mode)
427 		return 0;
428 
429 	return ice_acquire_res(hw, ICE_NVM_RES_ID, access, ICE_NVM_TIMEOUT);
430 }
431 
432 /**
433  * ice_release_nvm - Generic request for releasing the NVM ownership
434  * @hw: pointer to the HW structure
435  *
436  * This function will release NVM ownership.
437  */
438 void ice_release_nvm(struct ice_hw *hw)
439 {
440 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
441 
442 	if (hw->flash.blank_nvm_mode)
443 		return;
444 
445 	ice_release_res(hw, ICE_NVM_RES_ID);
446 }
447 
448 /**
449  * ice_get_flash_bank_offset - Get offset into requested flash bank
450  * @hw: pointer to the HW structure
451  * @bank: whether to read from the active or inactive flash bank
452  * @module: the module to read from
453  *
454  * Based on the module, lookup the module offset from the beginning of the
455  * flash.
456  *
457  * Returns the flash offset. Note that a value of zero is invalid and must be
458  * treated as an error.
459  */
460 static u32 ice_get_flash_bank_offset(struct ice_hw *hw, enum ice_bank_select bank, u16 module)
461 {
462 	struct ice_bank_info *banks = &hw->flash.banks;
463 	enum ice_flash_bank active_bank;
464 	bool second_bank_active;
465 	u32 offset, size;
466 
467 	switch (module) {
468 	case ICE_SR_1ST_NVM_BANK_PTR:
469 		offset = banks->nvm_ptr;
470 		size = banks->nvm_size;
471 		active_bank = banks->nvm_bank;
472 		break;
473 	case ICE_SR_1ST_OROM_BANK_PTR:
474 		offset = banks->orom_ptr;
475 		size = banks->orom_size;
476 		active_bank = banks->orom_bank;
477 		break;
478 	case ICE_SR_NETLIST_BANK_PTR:
479 		offset = banks->netlist_ptr;
480 		size = banks->netlist_size;
481 		active_bank = banks->netlist_bank;
482 		break;
483 	default:
484 		ice_debug(hw, ICE_DBG_NVM, "Unexpected value for flash module: 0x%04x\n", module);
485 		return 0;
486 	}
487 
488 	switch (active_bank) {
489 	case ICE_1ST_FLASH_BANK:
490 		second_bank_active = false;
491 		break;
492 	case ICE_2ND_FLASH_BANK:
493 		second_bank_active = true;
494 		break;
495 	default:
496 		ice_debug(hw, ICE_DBG_NVM, "Unexpected value for active flash bank: %u\n",
497 			  active_bank);
498 		return 0;
499 	}
500 
501 	/* The second flash bank is stored immediately following the first
502 	 * bank. Based on whether the 1st or 2nd bank is active, and whether
503 	 * we want the active or inactive bank, calculate the desired offset.
504 	 */
505 	switch (bank) {
506 	case ICE_ACTIVE_FLASH_BANK:
507 		return offset + (second_bank_active ? size : 0);
508 	case ICE_INACTIVE_FLASH_BANK:
509 		return offset + (second_bank_active ? 0 : size);
510 	}
511 
512 	ice_debug(hw, ICE_DBG_NVM, "Unexpected value for flash bank selection: %u\n", bank);
513 	return 0;
514 }
515 
516 /**
517  * ice_read_flash_module - Read a word from one of the main NVM modules
518  * @hw: pointer to the HW structure
519  * @bank: which bank of the module to read
520  * @module: the module to read
521  * @offset: the offset into the module in bytes
522  * @data: storage for the word read from the flash
523  * @length: bytes of data to read
524  *
525  * Read data from the specified flash module. The bank parameter indicates
526  * whether or not to read from the active bank or the inactive bank of that
527  * module.
528  *
529  * The word will be read using flat NVM access, and relies on the
530  * hw->flash.banks data being setup by ice_determine_active_flash_banks()
531  * during initialization.
532  */
533 static int
534 ice_read_flash_module(struct ice_hw *hw, enum ice_bank_select bank, u16 module,
535 		      u32 offset, u8 *data, u32 length)
536 {
537 	int status;
538 	u32 start;
539 
540 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
541 
542 	start = ice_get_flash_bank_offset(hw, bank, module);
543 	if (!start) {
544 		ice_debug(hw, ICE_DBG_NVM, "Unable to calculate flash bank offset for module 0x%04x\n",
545 			  module);
546 		return ICE_ERR_PARAM;
547 	}
548 
549 	status = ice_acquire_nvm(hw, ICE_RES_READ);
550 	if (status)
551 		return status;
552 
553 	status = ice_read_flat_nvm(hw, start + offset, &length, data, false);
554 
555 	ice_release_nvm(hw);
556 
557 	return status;
558 }
559 
560 /**
561  * ice_read_nvm_module - Read from the active main NVM module
562  * @hw: pointer to the HW structure
563  * @bank: whether to read from active or inactive NVM module
564  * @offset: offset into the NVM module to read, in words
565  * @data: storage for returned word value
566  *
567  * Read the specified word from the active NVM module. This includes the CSS
568  * header at the start of the NVM module.
569  */
570 static int
571 ice_read_nvm_module(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
572 {
573 	__le16 data_local;
574 	int status;
575 
576 	status = ice_read_flash_module(hw, bank, ICE_SR_1ST_NVM_BANK_PTR, offset * sizeof(u16),
577 				       (_FORCE_ u8 *)&data_local, sizeof(u16));
578 	if (!status)
579 		*data = LE16_TO_CPU(data_local);
580 
581 	return status;
582 }
583 
584 /**
585  * ice_get_nvm_css_hdr_len - Read the CSS header length from the NVM CSS header
586  * @hw: pointer to the HW struct
587  * @bank: whether to read from the active or inactive flash bank
588  * @hdr_len: storage for header length in words
589  *
590  * Read the CSS header length from the NVM CSS header and add the Authentication
591  * header size, and then convert to words.
592  */
593 static int
594 ice_get_nvm_css_hdr_len(struct ice_hw *hw, enum ice_bank_select bank,
595 			u32 *hdr_len)
596 {
597 	u16 hdr_len_l, hdr_len_h;
598 	u32 hdr_len_dword;
599 	int status;
600 
601 	status = ice_read_nvm_module(hw, bank, ICE_NVM_CSS_HDR_LEN_L,
602 				     &hdr_len_l);
603 	if (status)
604 		return status;
605 
606 	status = ice_read_nvm_module(hw, bank, ICE_NVM_CSS_HDR_LEN_H,
607 				     &hdr_len_h);
608 	if (status)
609 		return status;
610 
611 	/* CSS header length is in DWORD, so convert to words and add
612 	 * authentication header size
613 	 */
614 	hdr_len_dword = hdr_len_h << 16 | hdr_len_l;
615 	*hdr_len = (hdr_len_dword * 2) + ICE_NVM_AUTH_HEADER_LEN;
616 
617 	return 0;
618 }
619 
620 /**
621  * ice_read_nvm_sr_copy - Read a word from the Shadow RAM copy in the NVM bank
622  * @hw: pointer to the HW structure
623  * @bank: whether to read from the active or inactive NVM module
624  * @offset: offset into the Shadow RAM copy to read, in words
625  * @data: storage for returned word value
626  *
627  * Read the specified word from the copy of the Shadow RAM found in the
628  * specified NVM module.
629  */
630 static int
631 ice_read_nvm_sr_copy(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
632 {
633 	u32 hdr_len;
634 	int status;
635 
636 	status = ice_get_nvm_css_hdr_len(hw, bank, &hdr_len);
637 	if (status)
638 		return status;
639 
640 	hdr_len = ROUND_UP(hdr_len, 32);
641 
642 	return ice_read_nvm_module(hw, bank, hdr_len + offset, data);
643 }
644 
645 /**
646  * ice_read_orom_module - Read from the active Option ROM module
647  * @hw: pointer to the HW structure
648  * @bank: whether to read from active or inactive OROM module
649  * @offset: offset into the OROM module to read, in words
650  * @data: storage for returned word value
651  *
652  * Read the specified word from the active Option ROM module of the flash.
653  * Note that unlike the NVM module, the CSS data is stored at the end of the
654  * module instead of at the beginning.
655  */
656 static int
657 ice_read_orom_module(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
658 {
659 	__le16 data_local;
660 	int status;
661 
662 	status = ice_read_flash_module(hw, bank, ICE_SR_1ST_OROM_BANK_PTR, offset * sizeof(u16),
663 				       (_FORCE_ u8 *)&data_local, sizeof(u16));
664 	if (!status)
665 		*data = LE16_TO_CPU(data_local);
666 
667 	return status;
668 }
669 
670 /**
671  * ice_read_netlist_module - Read data from the netlist module area
672  * @hw: pointer to the HW structure
673  * @bank: whether to read from the active or inactive module
674  * @offset: offset into the netlist to read from
675  * @data: storage for returned word value
676  *
677  * Read a word from the specified netlist bank.
678  */
679 static int
680 ice_read_netlist_module(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
681 {
682 	__le16 data_local;
683 	int status;
684 
685 	status = ice_read_flash_module(hw, bank, ICE_SR_NETLIST_BANK_PTR, offset * sizeof(u16),
686 				       (_FORCE_ u8 *)&data_local, sizeof(u16));
687 	if (!status)
688 		*data = LE16_TO_CPU(data_local);
689 
690 	return status;
691 }
692 
693 /**
694  * ice_read_sr_word - Reads Shadow RAM word and acquire NVM if necessary
695  * @hw: pointer to the HW structure
696  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
697  * @data: word read from the Shadow RAM
698  *
699  * Reads one 16 bit word from the Shadow RAM using the ice_read_sr_word_aq.
700  */
701 int ice_read_sr_word(struct ice_hw *hw, u16 offset, u16 *data)
702 {
703 	int status;
704 
705 	status = ice_acquire_nvm(hw, ICE_RES_READ);
706 	if (!status) {
707 		status = ice_read_sr_word_aq(hw, offset, data);
708 		ice_release_nvm(hw);
709 	}
710 
711 	return status;
712 }
713 
714 #define check_add_overflow __builtin_add_overflow
715 
716 /**
717  * ice_get_pfa_module_tlv - Reads sub module TLV from NVM PFA
718  * @hw: pointer to hardware structure
719  * @module_tlv: pointer to module TLV to return
720  * @module_tlv_len: pointer to module TLV length to return
721  * @module_type: module type requested
722  *
723  * Finds the requested sub module TLV type from the Preserved Field
724  * Area (PFA) and returns the TLV pointer and length. The caller can
725  * use these to read the variable length TLV value.
726  */
727 int
728 ice_get_pfa_module_tlv(struct ice_hw *hw, u16 *module_tlv, u16 *module_tlv_len,
729 		       u16 module_type)
730 {
731 	u16 pfa_len, pfa_ptr, next_tlv, max_tlv;
732 	int status;
733 
734 	status = ice_read_sr_word(hw, ICE_SR_PFA_PTR, &pfa_ptr);
735 	if (status) {
736 		ice_debug(hw, ICE_DBG_INIT, "Preserved Field Array pointer.\n");
737 		return status;
738 	}
739 	status = ice_read_sr_word(hw, pfa_ptr, &pfa_len);
740 	if (status) {
741 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PFA length.\n");
742 		return status;
743 	}
744 
745 	if (check_add_overflow(pfa_ptr, (u16)(pfa_len - 1), &max_tlv)) {
746 		ice_debug(hw, ICE_DBG_INIT, "PFA starts at offset %u. PFA length of %u caused 16-bit arithmetic overflow.\n",
747 				  pfa_ptr, pfa_len);
748 		return ICE_ERR_INVAL_SIZE;
749 	}
750 
751 	/* The Preserved Fields Area contains a sequence of TLVs which define
752 	 * its contents. The PFA length includes all of the TLVs, plus its
753 	 * initial length word itself, *and* one final word at the end of all
754 	 * of the TLVs.
755 	 *
756 	 * Starting with first TLV after PFA length, iterate through the list
757 	 * of TLVs to find the requested one.
758 	 */
759 	next_tlv = pfa_ptr + 1;
760 	while (next_tlv < max_tlv) {
761 		u16 tlv_sub_module_type;
762 		u16 tlv_len;
763 
764 		/* Read TLV type */
765 		status = ice_read_sr_word(hw, (u16)next_tlv,
766 					  &tlv_sub_module_type);
767 		if (status) {
768 			ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV type.\n");
769 			break;
770 		}
771 		/* Read TLV length */
772 		status = ice_read_sr_word(hw, (u16)(next_tlv + 1), &tlv_len);
773 		if (status) {
774 			ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV length.\n");
775 			break;
776 		}
777 		if (tlv_sub_module_type == module_type) {
778 			if (tlv_len) {
779 				*module_tlv = (u16)next_tlv;
780 				*module_tlv_len = tlv_len;
781 				return 0;
782 			}
783 			return ICE_ERR_INVAL_SIZE;
784 		}
785 
786 		if (check_add_overflow(next_tlv, (u16)2, &next_tlv) ||
787 		    check_add_overflow(next_tlv, tlv_len, &next_tlv)) {
788 			ice_debug(hw, ICE_DBG_INIT, "TLV of type %u and length 0x%04x caused 16-bit arithmetic overflow. The PFA starts at 0x%04x and has length of 0x%04x\n",
789 					  tlv_sub_module_type, tlv_len, pfa_ptr, pfa_len);
790 			return ICE_ERR_INVAL_SIZE;
791 		}
792 	}
793 	/* Module does not exist */
794 	return ICE_ERR_DOES_NOT_EXIST;
795 }
796 
797 /**
798  * ice_read_pba_string - Reads part number string from NVM
799  * @hw: pointer to hardware structure
800  * @pba_num: stores the part number string from the NVM
801  * @pba_num_size: part number string buffer length
802  *
803  * Reads the part number string from the NVM.
804  */
805 int ice_read_pba_string(struct ice_hw *hw, u8 *pba_num, u32 pba_num_size)
806 {
807 	u16 pba_tlv, pba_tlv_len;
808 	u16 pba_word, pba_size;
809 	int status;
810 	u16 i;
811 
812 	status = ice_get_pfa_module_tlv(hw, &pba_tlv, &pba_tlv_len,
813 					ICE_SR_PBA_BLOCK_PTR);
814 	if (status) {
815 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block TLV.\n");
816 		return status;
817 	}
818 
819 	/* pba_size is the next word */
820 	status = ice_read_sr_word(hw, (pba_tlv + 2), &pba_size);
821 	if (status) {
822 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Section size.\n");
823 		return status;
824 	}
825 
826 	if (pba_tlv_len < pba_size) {
827 		ice_debug(hw, ICE_DBG_INIT, "Invalid PBA Block TLV size.\n");
828 		return ICE_ERR_INVAL_SIZE;
829 	}
830 
831 	/* Subtract one to get PBA word count (PBA Size word is included in
832 	 * total size)
833 	 */
834 	pba_size--;
835 	if (pba_num_size < (((u32)pba_size * 2) + 1)) {
836 		ice_debug(hw, ICE_DBG_INIT, "Buffer too small for PBA data.\n");
837 		return ICE_ERR_PARAM;
838 	}
839 
840 	for (i = 0; i < pba_size; i++) {
841 		status = ice_read_sr_word(hw, (pba_tlv + 2 + 1) + i, &pba_word);
842 		if (status) {
843 			ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block word %d.\n", i);
844 			return status;
845 		}
846 
847 		pba_num[(i * 2)] = (pba_word >> 8) & 0xFF;
848 		pba_num[(i * 2) + 1] = pba_word & 0xFF;
849 	}
850 	pba_num[(pba_size * 2)] = '\0';
851 
852 	return status;
853 }
854 
855 /**
856  * ice_get_nvm_srev - Read the security revision from the NVM CSS header
857  * @hw: pointer to the HW struct
858  * @bank: whether to read from the active or inactive flash bank
859  * @srev: storage for security revision
860  *
861  * Read the security revision out of the CSS header of the active NVM module
862  * bank.
863  */
864 static int ice_get_nvm_srev(struct ice_hw *hw, enum ice_bank_select bank, u32 *srev)
865 {
866 	u16 srev_l, srev_h;
867 	int status;
868 
869 	status = ice_read_nvm_module(hw, bank, ICE_NVM_CSS_SREV_L, &srev_l);
870 	if (status)
871 		return status;
872 
873 	status = ice_read_nvm_module(hw, bank, ICE_NVM_CSS_SREV_H, &srev_h);
874 	if (status)
875 		return status;
876 
877 	*srev = srev_h << 16 | srev_l;
878 
879 	return 0;
880 }
881 
882 /**
883  * ice_get_nvm_ver_info - Read NVM version information
884  * @hw: pointer to the HW struct
885  * @bank: whether to read from the active or inactive flash bank
886  * @nvm: pointer to NVM info structure
887  *
888  * Read the NVM EETRACK ID and map version of the main NVM image bank, filling
889  * in the NVM info structure.
890  */
891 static int
892 ice_get_nvm_ver_info(struct ice_hw *hw, enum ice_bank_select bank, struct ice_nvm_info *nvm)
893 {
894 	u16 eetrack_lo, eetrack_hi, ver;
895 	int status;
896 
897 	status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_DEV_STARTER_VER, &ver);
898 	if (status) {
899 		ice_debug(hw, ICE_DBG_NVM, "Failed to read DEV starter version.\n");
900 		return status;
901 	}
902 
903 	nvm->major = (ver & ICE_NVM_VER_HI_MASK) >> ICE_NVM_VER_HI_SHIFT;
904 	nvm->minor = (ver & ICE_NVM_VER_LO_MASK) >> ICE_NVM_VER_LO_SHIFT;
905 
906 	status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_EETRACK_LO, &eetrack_lo);
907 	if (status) {
908 		ice_debug(hw, ICE_DBG_NVM, "Failed to read EETRACK lo.\n");
909 		return status;
910 	}
911 	status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_EETRACK_HI, &eetrack_hi);
912 	if (status) {
913 		ice_debug(hw, ICE_DBG_NVM, "Failed to read EETRACK hi.\n");
914 		return status;
915 	}
916 
917 	nvm->eetrack = (eetrack_hi << 16) | eetrack_lo;
918 
919 	status = ice_get_nvm_srev(hw, bank, &nvm->srev);
920 	if (status)
921 		ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM security revision.\n");
922 
923 	return 0;
924 }
925 
926 /**
927  * ice_get_inactive_nvm_ver - Read Option ROM version from the inactive bank
928  * @hw: pointer to the HW structure
929  * @nvm: storage for Option ROM version information
930  *
931  * Reads the NVM EETRACK ID, Map version, and security revision of the
932  * inactive NVM bank. Used to access version data for a pending update that
933  * has not yet been activated.
934  */
935 int ice_get_inactive_nvm_ver(struct ice_hw *hw, struct ice_nvm_info *nvm)
936 {
937 	return ice_get_nvm_ver_info(hw, ICE_INACTIVE_FLASH_BANK, nvm);
938 }
939 
940 /**
941  * ice_get_orom_srev - Read the security revision from the OROM CSS header
942  * @hw: pointer to the HW struct
943  * @bank: whether to read from active or inactive flash module
944  * @srev: storage for security revision
945  *
946  * Read the security revision out of the CSS header of the active OROM module
947  * bank.
948  */
949 static int ice_get_orom_srev(struct ice_hw *hw, enum ice_bank_select bank, u32 *srev)
950 {
951 	u32 orom_size_word = hw->flash.banks.orom_size / 2;
952 	u16 srev_l, srev_h;
953 	u32 css_start;
954 	u32 hdr_len;
955 	int status;
956 
957 	status = ice_get_nvm_css_hdr_len(hw, bank, &hdr_len);
958 	if (status)
959 		return status;
960 
961 	if (orom_size_word < hdr_len) {
962 		ice_debug(hw, ICE_DBG_NVM, "Unexpected Option ROM Size of %u\n",
963 			  hw->flash.banks.orom_size);
964 		return ICE_ERR_CFG;
965 	}
966 
967 	/* calculate how far into the Option ROM the CSS header starts. Note
968 	 * that ice_read_orom_module takes a word offset
969 	 */
970 	css_start = orom_size_word - hdr_len;
971 	status = ice_read_orom_module(hw, bank, css_start + ICE_NVM_CSS_SREV_L, &srev_l);
972 	if (status)
973 		return status;
974 
975 	status = ice_read_orom_module(hw, bank, css_start + ICE_NVM_CSS_SREV_H, &srev_h);
976 	if (status)
977 		return status;
978 
979 	*srev = srev_h << 16 | srev_l;
980 
981 	return 0;
982 }
983 
984 /**
985  * ice_get_orom_civd_data - Get the combo version information from Option ROM
986  * @hw: pointer to the HW struct
987  * @bank: whether to read from the active or inactive flash module
988  * @civd: storage for the Option ROM CIVD data.
989  *
990  * Searches through the Option ROM flash contents to locate the CIVD data for
991  * the image.
992  */
993 static int
994 ice_get_orom_civd_data(struct ice_hw *hw, enum ice_bank_select bank,
995 		       struct ice_orom_civd_info *civd)
996 {
997 	struct ice_orom_civd_info civd_data_section;
998 	int status;
999 	u32 offset;
1000 	u32 tmp;
1001 
1002 	/* The CIVD section is located in the Option ROM aligned to 512 bytes.
1003 	 * The first 4 bytes must contain the ASCII characters "$CIV".
1004 	 * A simple modulo 256 sum of all of the bytes of the structure must
1005 	 * equal 0.
1006 	 *
1007 	 * The exact location is unknown and varies between images but is
1008 	 * usually somewhere in the middle of the bank. We need to scan the
1009 	 * Option ROM bank to locate it.
1010 	 *
1011 	 */
1012 
1013 	/* Scan the memory buffer to locate the CIVD data section */
1014 	for (offset = 0; (offset + 512) <= hw->flash.banks.orom_size; offset += 512) {
1015 		u8 sum = 0, i;
1016 
1017 		status = ice_read_flash_module(hw, bank, ICE_SR_1ST_OROM_BANK_PTR,
1018 					       offset, (u8 *)&tmp, sizeof(tmp));
1019 		if (status) {
1020 			ice_debug(hw, ICE_DBG_NVM, "Unable to read Option ROM data\n");
1021 			return status;
1022 		}
1023 
1024 		/* Skip forward until we find a matching signature */
1025 		if (memcmp("$CIV", &tmp, sizeof(tmp)) != 0)
1026 			continue;
1027 
1028 		ice_debug(hw, ICE_DBG_NVM, "Found CIVD section at offset %u\n",
1029 			  offset);
1030 
1031 		status = ice_read_flash_module(hw, bank, ICE_SR_1ST_OROM_BANK_PTR,
1032 					       offset, (u8 *)&civd_data_section,
1033 					       sizeof(civd_data_section));
1034 		if (status) {
1035 			ice_debug(hw, ICE_DBG_NVM, "Unable to read CIVD data\n");
1036 			goto exit_error;
1037 		}
1038 
1039 		/* Verify that the simple checksum is zero */
1040 		for (i = 0; i < sizeof(civd_data_section); i++)
1041 			sum += ((u8 *)&civd_data_section)[i];
1042 
1043 		if (sum) {
1044 			ice_debug(hw, ICE_DBG_NVM, "Found CIVD data with invalid checksum of %u\n",
1045 				  sum);
1046 			status = ICE_ERR_NVM;
1047 			goto exit_error;
1048 		}
1049 
1050 		*civd = civd_data_section;
1051 
1052 		return 0;
1053 	}
1054 
1055 	status = ICE_ERR_NVM;
1056 	ice_debug(hw, ICE_DBG_NVM, "Unable to locate CIVD data within the Option ROM\n");
1057 
1058 exit_error:
1059 	return status;
1060 }
1061 
1062 /**
1063  * ice_get_orom_ver_info - Read Option ROM version information
1064  * @hw: pointer to the HW struct
1065  * @bank: whether to read from the active or inactive flash module
1066  * @orom: pointer to Option ROM info structure
1067  *
1068  * Read Option ROM version and security revision from the Option ROM flash
1069  * section.
1070  */
1071 static int
1072 ice_get_orom_ver_info(struct ice_hw *hw, enum ice_bank_select bank, struct ice_orom_info *orom)
1073 {
1074 	struct ice_orom_civd_info civd;
1075 	u32 combo_ver;
1076 	int status;
1077 
1078 	status = ice_get_orom_civd_data(hw, bank, &civd);
1079 	if (status) {
1080 		ice_debug(hw, ICE_DBG_NVM, "Failed to locate valid Option ROM CIVD data\n");
1081 		return status;
1082 	}
1083 
1084 	combo_ver = LE32_TO_CPU(civd.combo_ver);
1085 
1086 	orom->major = (u8)((combo_ver & ICE_OROM_VER_MASK) >> ICE_OROM_VER_SHIFT);
1087 	orom->patch = (u8)(combo_ver & ICE_OROM_VER_PATCH_MASK);
1088 	orom->build = (u16)((combo_ver & ICE_OROM_VER_BUILD_MASK) >> ICE_OROM_VER_BUILD_SHIFT);
1089 
1090 	status = ice_get_orom_srev(hw, bank, &orom->srev);
1091 	if (status) {
1092 		ice_debug(hw, ICE_DBG_NVM, "Failed to read Option ROM security revision.\n");
1093 		return status;
1094 	}
1095 
1096 	return 0;
1097 }
1098 
1099 /**
1100  * ice_get_inactive_orom_ver - Read Option ROM version from the inactive bank
1101  * @hw: pointer to the HW structure
1102  * @orom: storage for Option ROM version information
1103  *
1104  * Reads the Option ROM version and security revision data for the inactive
1105  * section of flash. Used to access version data for a pending update that has
1106  * not yet been activated.
1107  */
1108 int ice_get_inactive_orom_ver(struct ice_hw *hw, struct ice_orom_info *orom)
1109 {
1110 	return ice_get_orom_ver_info(hw, ICE_INACTIVE_FLASH_BANK, orom);
1111 }
1112 
1113 /**
1114  * ice_get_netlist_info
1115  * @hw: pointer to the HW struct
1116  * @bank: whether to read from the active or inactive flash bank
1117  * @netlist: pointer to netlist version info structure
1118  *
1119  * Get the netlist version information from the requested bank. Reads the Link
1120  * Topology section to find the Netlist ID block and extract the relevant
1121  * information into the netlist version structure.
1122  */
1123 static int
1124 ice_get_netlist_info(struct ice_hw *hw, enum ice_bank_select bank,
1125 		     struct ice_netlist_info *netlist)
1126 {
1127 	u16 module_id, length, node_count, i;
1128 	u16 *id_blk;
1129 	int status;
1130 
1131 	status = ice_read_netlist_module(hw, bank, ICE_NETLIST_TYPE_OFFSET, &module_id);
1132 	if (status)
1133 		return status;
1134 
1135 	if (module_id != ICE_NETLIST_LINK_TOPO_MOD_ID) {
1136 		ice_debug(hw, ICE_DBG_NVM, "Expected netlist module_id ID of 0x%04x, but got 0x%04x\n",
1137 			  ICE_NETLIST_LINK_TOPO_MOD_ID, module_id);
1138 		return ICE_ERR_NVM;
1139 	}
1140 
1141 	status = ice_read_netlist_module(hw, bank, ICE_LINK_TOPO_MODULE_LEN, &length);
1142 	if (status)
1143 		return status;
1144 
1145 	/* sanity check that we have at least enough words to store the netlist ID block */
1146 	if (length < ICE_NETLIST_ID_BLK_SIZE) {
1147 		ice_debug(hw, ICE_DBG_NVM, "Netlist Link Topology module too small. Expected at least %u words, but got %u words.\n",
1148 			  ICE_NETLIST_ID_BLK_SIZE, length);
1149 		return ICE_ERR_NVM;
1150 	}
1151 
1152 	status = ice_read_netlist_module(hw, bank, ICE_LINK_TOPO_NODE_COUNT, &node_count);
1153 	if (status)
1154 		return status;
1155 	node_count &= ICE_LINK_TOPO_NODE_COUNT_M;
1156 
1157 	id_blk = (u16 *)ice_calloc(hw, ICE_NETLIST_ID_BLK_SIZE, sizeof(*id_blk));
1158 	if (!id_blk)
1159 		return ICE_ERR_NO_MEMORY;
1160 
1161 	/* Read out the entire Netlist ID Block at once. */
1162 	status = ice_read_flash_module(hw, bank, ICE_SR_NETLIST_BANK_PTR,
1163 				       ICE_NETLIST_ID_BLK_OFFSET(node_count) * sizeof(u16),
1164 				       (u8 *)id_blk, ICE_NETLIST_ID_BLK_SIZE * sizeof(u16));
1165 	if (status)
1166 		goto exit_error;
1167 
1168 	for (i = 0; i < ICE_NETLIST_ID_BLK_SIZE; i++)
1169 		id_blk[i] = LE16_TO_CPU(((_FORCE_ __le16 *)id_blk)[i]);
1170 
1171 	netlist->major = id_blk[ICE_NETLIST_ID_BLK_MAJOR_VER_HIGH] << 16 |
1172 			 id_blk[ICE_NETLIST_ID_BLK_MAJOR_VER_LOW];
1173 	netlist->minor = id_blk[ICE_NETLIST_ID_BLK_MINOR_VER_HIGH] << 16 |
1174 			 id_blk[ICE_NETLIST_ID_BLK_MINOR_VER_LOW];
1175 	netlist->type = id_blk[ICE_NETLIST_ID_BLK_TYPE_HIGH] << 16 |
1176 			id_blk[ICE_NETLIST_ID_BLK_TYPE_LOW];
1177 	netlist->rev = id_blk[ICE_NETLIST_ID_BLK_REV_HIGH] << 16 |
1178 		       id_blk[ICE_NETLIST_ID_BLK_REV_LOW];
1179 	netlist->cust_ver = id_blk[ICE_NETLIST_ID_BLK_CUST_VER];
1180 	/* Read the left most 4 bytes of SHA */
1181 	netlist->hash = id_blk[ICE_NETLIST_ID_BLK_SHA_HASH_WORD(15)] << 16 |
1182 			id_blk[ICE_NETLIST_ID_BLK_SHA_HASH_WORD(14)];
1183 
1184 exit_error:
1185 	ice_free(hw, id_blk);
1186 
1187 	return status;
1188 }
1189 
1190 /**
1191  * ice_get_netlist_ver_info
1192  * @hw: pointer to the HW struct
1193  * @netlist: pointer to netlist version info structure
1194  *
1195  * Get the netlist version information
1196  */
1197 int ice_get_netlist_ver_info(struct ice_hw *hw, struct ice_netlist_info *netlist)
1198 {
1199 	return ice_get_netlist_info(hw, ICE_ACTIVE_FLASH_BANK, netlist);
1200 }
1201 
1202 /**
1203  * ice_get_inactive_netlist_ver
1204  * @hw: pointer to the HW struct
1205  * @netlist: pointer to netlist version info structure
1206  *
1207  * Read the netlist version data from the inactive netlist bank. Used to
1208  * extract version data of a pending flash update in order to display the
1209  * version data.
1210  */
1211 int ice_get_inactive_netlist_ver(struct ice_hw *hw, struct ice_netlist_info *netlist)
1212 {
1213 	return ice_get_netlist_info(hw, ICE_INACTIVE_FLASH_BANK, netlist);
1214 }
1215 
1216 /**
1217  * ice_discover_flash_size - Discover the available flash size
1218  * @hw: pointer to the HW struct
1219  *
1220  * The device flash could be up to 16MB in size. However, it is possible that
1221  * the actual size is smaller. Use bisection to determine the accessible size
1222  * of flash memory.
1223  */
1224 static int ice_discover_flash_size(struct ice_hw *hw)
1225 {
1226 	u32 min_size = 0, max_size = ICE_AQC_NVM_MAX_OFFSET + 1;
1227 	int status;
1228 
1229 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1230 
1231 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1232 	if (status)
1233 		return status;
1234 
1235 	while ((max_size - min_size) > 1) {
1236 		u32 offset = (max_size + min_size) / 2;
1237 		u32 len = 1;
1238 		u8 data;
1239 
1240 		status = ice_read_flat_nvm(hw, offset, &len, &data, false);
1241 		if (status == ICE_ERR_AQ_ERROR &&
1242 		    hw->adminq.sq_last_status == ICE_AQ_RC_EINVAL) {
1243 			ice_debug(hw, ICE_DBG_NVM, "%s: New upper bound of %u bytes\n",
1244 				  __func__, offset);
1245 			status = 0;
1246 			max_size = offset;
1247 		} else if (!status) {
1248 			ice_debug(hw, ICE_DBG_NVM, "%s: New lower bound of %u bytes\n",
1249 				  __func__, offset);
1250 			min_size = offset;
1251 		} else {
1252 			/* an unexpected error occurred */
1253 			goto err_read_flat_nvm;
1254 		}
1255 	}
1256 
1257 	ice_debug(hw, ICE_DBG_NVM, "Predicted flash size is %u bytes\n", max_size);
1258 
1259 	hw->flash.flash_size = max_size;
1260 
1261 err_read_flat_nvm:
1262 	ice_release_nvm(hw);
1263 
1264 	return status;
1265 }
1266 
1267 /**
1268  * ice_read_sr_pointer - Read the value of a Shadow RAM pointer word
1269  * @hw: pointer to the HW structure
1270  * @offset: the word offset of the Shadow RAM word to read
1271  * @pointer: pointer value read from Shadow RAM
1272  *
1273  * Read the given Shadow RAM word, and convert it to a pointer value specified
1274  * in bytes. This function assumes the specified offset is a valid pointer
1275  * word.
1276  *
1277  * Each pointer word specifies whether it is stored in word size or 4KB
1278  * sector size by using the highest bit. The reported pointer value will be in
1279  * bytes, intended for flat NVM reads.
1280  */
1281 static int ice_read_sr_pointer(struct ice_hw *hw, u16 offset, u32 *pointer)
1282 {
1283 	int status;
1284 	u16 value;
1285 
1286 	status = ice_read_sr_word(hw, offset, &value);
1287 	if (status)
1288 		return status;
1289 
1290 	/* Determine if the pointer is in 4KB or word units */
1291 	if (value & ICE_SR_NVM_PTR_4KB_UNITS)
1292 		*pointer = (value & ~ICE_SR_NVM_PTR_4KB_UNITS) * 4 * 1024;
1293 	else
1294 		*pointer = value * 2;
1295 
1296 	return 0;
1297 }
1298 
1299 /**
1300  * ice_read_sr_area_size - Read an area size from a Shadow RAM word
1301  * @hw: pointer to the HW structure
1302  * @offset: the word offset of the Shadow RAM to read
1303  * @size: size value read from the Shadow RAM
1304  *
1305  * Read the given Shadow RAM word, and convert it to an area size value
1306  * specified in bytes. This function assumes the specified offset is a valid
1307  * area size word.
1308  *
1309  * Each area size word is specified in 4KB sector units. This function reports
1310  * the size in bytes, intended for flat NVM reads.
1311  */
1312 static int ice_read_sr_area_size(struct ice_hw *hw, u16 offset, u32 *size)
1313 {
1314 	int status;
1315 	u16 value;
1316 
1317 	status = ice_read_sr_word(hw, offset, &value);
1318 	if (status)
1319 		return status;
1320 
1321 	/* Area sizes are always specified in 4KB units */
1322 	*size = value * 4 * 1024;
1323 
1324 	return 0;
1325 }
1326 
1327 /**
1328  * ice_determine_active_flash_banks - Discover active bank for each module
1329  * @hw: pointer to the HW struct
1330  *
1331  * Read the Shadow RAM control word and determine which banks are active for
1332  * the NVM, OROM, and Netlist modules. Also read and calculate the associated
1333  * pointer and size. These values are then cached into the ice_flash_info
1334  * structure for later use in order to calculate the correct offset to read
1335  * from the active module.
1336  */
1337 static int ice_determine_active_flash_banks(struct ice_hw *hw)
1338 {
1339 	struct ice_bank_info *banks = &hw->flash.banks;
1340 	u16 ctrl_word;
1341 	int status;
1342 
1343 	status = ice_read_sr_word(hw, ICE_SR_NVM_CTRL_WORD, &ctrl_word);
1344 	if (status) {
1345 		ice_debug(hw, ICE_DBG_NVM, "Failed to read the Shadow RAM control word\n");
1346 		return status;
1347 	}
1348 
1349 	/* Check that the control word indicates validity */
1350 	if ((ctrl_word & ICE_SR_CTRL_WORD_1_M) >> ICE_SR_CTRL_WORD_1_S != ICE_SR_CTRL_WORD_VALID) {
1351 		ice_debug(hw, ICE_DBG_NVM, "Shadow RAM control word is invalid\n");
1352 		return ICE_ERR_CFG;
1353 	}
1354 
1355 	if (!(ctrl_word & ICE_SR_CTRL_WORD_NVM_BANK))
1356 		banks->nvm_bank = ICE_1ST_FLASH_BANK;
1357 	else
1358 		banks->nvm_bank = ICE_2ND_FLASH_BANK;
1359 
1360 	if (!(ctrl_word & ICE_SR_CTRL_WORD_OROM_BANK))
1361 		banks->orom_bank = ICE_1ST_FLASH_BANK;
1362 	else
1363 		banks->orom_bank = ICE_2ND_FLASH_BANK;
1364 
1365 	if (!(ctrl_word & ICE_SR_CTRL_WORD_NETLIST_BANK))
1366 		banks->netlist_bank = ICE_1ST_FLASH_BANK;
1367 	else
1368 		banks->netlist_bank = ICE_2ND_FLASH_BANK;
1369 
1370 	status = ice_read_sr_pointer(hw, ICE_SR_1ST_NVM_BANK_PTR, &banks->nvm_ptr);
1371 	if (status) {
1372 		ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank pointer\n");
1373 		return status;
1374 	}
1375 
1376 	status = ice_read_sr_area_size(hw, ICE_SR_NVM_BANK_SIZE, &banks->nvm_size);
1377 	if (status) {
1378 		ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank area size\n");
1379 		return status;
1380 	}
1381 
1382 	status = ice_read_sr_pointer(hw, ICE_SR_1ST_OROM_BANK_PTR, &banks->orom_ptr);
1383 	if (status) {
1384 		ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank pointer\n");
1385 		return status;
1386 	}
1387 
1388 	status = ice_read_sr_area_size(hw, ICE_SR_OROM_BANK_SIZE, &banks->orom_size);
1389 	if (status) {
1390 		ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank area size\n");
1391 		return status;
1392 	}
1393 
1394 	status = ice_read_sr_pointer(hw, ICE_SR_NETLIST_BANK_PTR, &banks->netlist_ptr);
1395 	if (status) {
1396 		ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank pointer\n");
1397 		return status;
1398 	}
1399 
1400 	status = ice_read_sr_area_size(hw, ICE_SR_NETLIST_BANK_SIZE, &banks->netlist_size);
1401 	if (status) {
1402 		ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank area size\n");
1403 		return status;
1404 	}
1405 
1406 	return 0;
1407 }
1408 
1409 /**
1410  * ice_init_nvm - initializes NVM setting
1411  * @hw: pointer to the HW struct
1412  *
1413  * This function reads and populates NVM settings such as Shadow RAM size,
1414  * max_timeout, and blank_nvm_mode
1415  */
1416 int ice_init_nvm(struct ice_hw *hw)
1417 {
1418 	struct ice_flash_info *flash = &hw->flash;
1419 	u32 fla, gens_stat;
1420 	u8 sr_size;
1421 	int status;
1422 
1423 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1424 
1425 	/* The SR size is stored regardless of the NVM programming mode
1426 	 * as the blank mode may be used in the factory line.
1427 	 */
1428 	gens_stat = rd32(hw, GLNVM_GENS);
1429 	sr_size = (gens_stat & GLNVM_GENS_SR_SIZE_M) >> GLNVM_GENS_SR_SIZE_S;
1430 
1431 	/* Switching to words (sr_size contains power of 2) */
1432 	flash->sr_words = BIT(sr_size) * ICE_SR_WORDS_IN_1KB;
1433 
1434 	/* Check if we are in the normal or blank NVM programming mode */
1435 	fla = rd32(hw, GLNVM_FLA);
1436 	if (fla & GLNVM_FLA_LOCKED_M) { /* Normal programming mode */
1437 		flash->blank_nvm_mode = false;
1438 	} else {
1439 		/* Blank programming mode */
1440 		flash->blank_nvm_mode = true;
1441 		ice_debug(hw, ICE_DBG_NVM, "NVM init error: unsupported blank mode.\n");
1442 		return ICE_ERR_NVM_BLANK_MODE;
1443 	}
1444 
1445 	status = ice_discover_flash_size(hw);
1446 	if (status) {
1447 		ice_debug(hw, ICE_DBG_NVM, "NVM init error: failed to discover flash size.\n");
1448 		return status;
1449 	}
1450 
1451 	status = ice_determine_active_flash_banks(hw);
1452 	if (status) {
1453 		ice_debug(hw, ICE_DBG_NVM, "Failed to determine active flash banks.\n");
1454 		return status;
1455 	}
1456 
1457 	status = ice_get_nvm_ver_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->nvm);
1458 	if (status) {
1459 		ice_debug(hw, ICE_DBG_INIT, "Failed to read NVM info.\n");
1460 		return status;
1461 	}
1462 
1463 	status = ice_get_orom_ver_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->orom);
1464 	if (status)
1465 		ice_debug(hw, ICE_DBG_INIT, "Failed to read Option ROM info.\n");
1466 
1467 	/* read the netlist version information */
1468 	status = ice_get_netlist_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->netlist);
1469 	if (status)
1470 		ice_debug(hw, ICE_DBG_INIT, "Failed to read netlist info.\n");
1471 
1472 	return 0;
1473 }
1474 
1475 /**
1476  * ice_read_sr_buf - Reads Shadow RAM buf and acquire lock if necessary
1477  * @hw: pointer to the HW structure
1478  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
1479  * @words: (in) number of words to read; (out) number of words actually read
1480  * @data: words read from the Shadow RAM
1481  *
1482  * Reads 16 bit words (data buf) from the SR using the ice_read_nvm_buf_aq
1483  * method. The buf read is preceded by the NVM ownership take
1484  * and followed by the release.
1485  */
1486 int
1487 ice_read_sr_buf(struct ice_hw *hw, u16 offset, u16 *words, u16 *data)
1488 {
1489 	int status;
1490 
1491 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1492 	if (!status) {
1493 		status = ice_read_sr_buf_aq(hw, offset, words, data);
1494 		ice_release_nvm(hw);
1495 	}
1496 
1497 	return status;
1498 }
1499 
1500 /**
1501  * __ice_write_sr_word - Writes Shadow RAM word
1502  * @hw: pointer to the HW structure
1503  * @offset: offset of the Shadow RAM word to write
1504  * @data: word to write to the Shadow RAM
1505  *
1506  * Writes a 16 bit word to the SR using the ice_write_sr_aq method.
1507  * NVM ownership have to be acquired and released (on ARQ completion event
1508  * reception) by caller. To commit SR to NVM update checksum function
1509  * should be called.
1510  */
1511 int
1512 __ice_write_sr_word(struct ice_hw *hw, u32 offset, const u16 *data)
1513 {
1514 	__le16 data_local = CPU_TO_LE16(*data);
1515 
1516 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1517 
1518 	/* Value 0x00 below means that we treat SR as a flat mem */
1519 	return ice_write_sr_aq(hw, offset, 1, &data_local, false);
1520 }
1521 
1522 /**
1523  * __ice_write_sr_buf - Writes Shadow RAM buf
1524  * @hw: pointer to the HW structure
1525  * @offset: offset of the Shadow RAM buffer to write
1526  * @words: number of words to write
1527  * @data: words to write to the Shadow RAM
1528  *
1529  * Writes a 16 bit words buffer to the Shadow RAM using the admin command.
1530  * NVM ownership must be acquired before calling this function and released
1531  * on ARQ completion event reception by caller. To commit SR to NVM update
1532  * checksum function should be called.
1533  */
1534 int
1535 __ice_write_sr_buf(struct ice_hw *hw, u32 offset, u16 words, const u16 *data)
1536 {
1537 	__le16 *data_local;
1538 	int status;
1539 	void *vmem;
1540 	u32 i;
1541 
1542 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1543 
1544 	vmem = ice_calloc(hw, words, sizeof(u16));
1545 	if (!vmem)
1546 		return ICE_ERR_NO_MEMORY;
1547 	data_local = (_FORCE_ __le16 *)vmem;
1548 
1549 	for (i = 0; i < words; i++)
1550 		data_local[i] = CPU_TO_LE16(data[i]);
1551 
1552 	/* Here we will only write one buffer as the size of the modules
1553 	 * mirrored in the Shadow RAM is always less than 4K.
1554 	 */
1555 	status = ice_write_sr_aq(hw, offset, words, data_local, false);
1556 
1557 	ice_free(hw, vmem);
1558 
1559 	return status;
1560 }
1561 
1562 /**
1563  * ice_calc_sr_checksum - Calculates and returns Shadow RAM SW checksum
1564  * @hw: pointer to hardware structure
1565  * @checksum: pointer to the checksum
1566  *
1567  * This function calculates SW Checksum that covers the whole 64kB shadow RAM
1568  * except the VPD and PCIe ALT Auto-load modules. The structure and size of VPD
1569  * is customer specific and unknown. Therefore, this function skips all maximum
1570  * possible size of VPD (1kB).
1571  */
1572 static int ice_calc_sr_checksum(struct ice_hw *hw, u16 *checksum)
1573 {
1574 	u16 pcie_alt_module = 0;
1575 	u16 checksum_local = 0;
1576 	u16 vpd_module;
1577 	int status = 0;
1578 	void *vmem;
1579 	u16 *data;
1580 	u16 i;
1581 
1582 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1583 
1584 	vmem = ice_calloc(hw, ICE_SR_SECTOR_SIZE_IN_WORDS, sizeof(u16));
1585 	if (!vmem)
1586 		return ICE_ERR_NO_MEMORY;
1587 	data = (u16 *)vmem;
1588 
1589 	/* read pointer to VPD area */
1590 	status = ice_read_sr_word_aq(hw, ICE_SR_VPD_PTR, &vpd_module);
1591 	if (status)
1592 		goto ice_calc_sr_checksum_exit;
1593 
1594 	/* read pointer to PCIe Alt Auto-load module */
1595 	status = ice_read_sr_word_aq(hw, ICE_SR_PCIE_ALT_AUTO_LOAD_PTR,
1596 				     &pcie_alt_module);
1597 	if (status)
1598 		goto ice_calc_sr_checksum_exit;
1599 
1600 	/* Calculate SW checksum that covers the whole 64kB shadow RAM
1601 	 * except the VPD and PCIe ALT Auto-load modules
1602 	 */
1603 	for (i = 0; i < hw->flash.sr_words; i++) {
1604 		/* Read SR page */
1605 		if ((i % ICE_SR_SECTOR_SIZE_IN_WORDS) == 0) {
1606 			u16 words = ICE_SR_SECTOR_SIZE_IN_WORDS;
1607 
1608 			status = ice_read_sr_buf_aq(hw, i, &words, data);
1609 			if (status)
1610 				goto ice_calc_sr_checksum_exit;
1611 		}
1612 
1613 		/* Skip Checksum word */
1614 		if (i == ICE_SR_SW_CHECKSUM_WORD)
1615 			continue;
1616 		/* Skip VPD module (convert byte size to word count) */
1617 		if (i >= (u32)vpd_module &&
1618 		    i < ((u32)vpd_module + ICE_SR_VPD_SIZE_WORDS))
1619 			continue;
1620 		/* Skip PCIe ALT module (convert byte size to word count) */
1621 		if (i >= (u32)pcie_alt_module &&
1622 		    i < ((u32)pcie_alt_module + ICE_SR_PCIE_ALT_SIZE_WORDS))
1623 			continue;
1624 
1625 		checksum_local += data[i % ICE_SR_SECTOR_SIZE_IN_WORDS];
1626 	}
1627 
1628 	*checksum = (u16)ICE_SR_SW_CHECKSUM_BASE - checksum_local;
1629 
1630 ice_calc_sr_checksum_exit:
1631 	ice_free(hw, vmem);
1632 	return status;
1633 }
1634 
1635 /**
1636  * ice_update_sr_checksum - Updates the Shadow RAM SW checksum
1637  * @hw: pointer to hardware structure
1638  *
1639  * NVM ownership must be acquired before calling this function and released
1640  * on ARQ completion event reception by caller.
1641  * This function will commit SR to NVM.
1642  */
1643 int ice_update_sr_checksum(struct ice_hw *hw)
1644 {
1645 	__le16 le_sum;
1646 	u16 checksum;
1647 	int status;
1648 
1649 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1650 
1651 	status = ice_calc_sr_checksum(hw, &checksum);
1652 	if (!status) {
1653 		le_sum = CPU_TO_LE16(checksum);
1654 		status = ice_write_sr_aq(hw, ICE_SR_SW_CHECKSUM_WORD, 1,
1655 					 &le_sum, true);
1656 	}
1657 	return status;
1658 }
1659 
1660 /**
1661  * ice_validate_sr_checksum - Validate Shadow RAM SW checksum
1662  * @hw: pointer to hardware structure
1663  * @checksum: calculated checksum
1664  *
1665  * Performs checksum calculation and validates the Shadow RAM SW checksum.
1666  * If the caller does not need checksum, the value can be NULL.
1667  */
1668 int ice_validate_sr_checksum(struct ice_hw *hw, u16 *checksum)
1669 {
1670 	u16 checksum_local;
1671 	u16 checksum_sr;
1672 	int status;
1673 
1674 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1675 
1676 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1677 	if (!status) {
1678 		status = ice_calc_sr_checksum(hw, &checksum_local);
1679 		ice_release_nvm(hw);
1680 		if (status)
1681 			return status;
1682 	} else {
1683 		return status;
1684 	}
1685 
1686 	ice_read_sr_word(hw, ICE_SR_SW_CHECKSUM_WORD, &checksum_sr);
1687 
1688 	/* Verify read checksum from EEPROM is the same as
1689 	 * calculated checksum
1690 	 */
1691 	if (checksum_local != checksum_sr)
1692 		status = ICE_ERR_NVM_CHECKSUM;
1693 
1694 	/* If the user cares, return the calculated checksum */
1695 	if (checksum)
1696 		*checksum = checksum_local;
1697 
1698 	return status;
1699 }
1700 
1701 /**
1702  * ice_nvm_validate_checksum
1703  * @hw: pointer to the HW struct
1704  *
1705  * Verify NVM PFA checksum validity (0x0706)
1706  */
1707 int ice_nvm_validate_checksum(struct ice_hw *hw)
1708 {
1709 	struct ice_aqc_nvm_checksum *cmd;
1710 	struct ice_aq_desc desc;
1711 	int status;
1712 
1713 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1714 	if (status)
1715 		return status;
1716 
1717 	cmd = &desc.params.nvm_checksum;
1718 
1719 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_checksum);
1720 	cmd->flags = ICE_AQC_NVM_CHECKSUM_VERIFY;
1721 
1722 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1723 	ice_release_nvm(hw);
1724 
1725 	if (!status)
1726 		if (LE16_TO_CPU(cmd->checksum) != ICE_AQC_NVM_CHECKSUM_CORRECT)
1727 			status = ICE_ERR_NVM_CHECKSUM;
1728 
1729 	return status;
1730 }
1731 
1732 /**
1733  * ice_nvm_recalculate_checksum
1734  * @hw: pointer to the HW struct
1735  *
1736  * Recalculate NVM PFA checksum (0x0706)
1737  */
1738 int ice_nvm_recalculate_checksum(struct ice_hw *hw)
1739 {
1740 	struct ice_aqc_nvm_checksum *cmd;
1741 	struct ice_aq_desc desc;
1742 	int status;
1743 
1744 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1745 	if (status)
1746 		return status;
1747 
1748 	cmd = &desc.params.nvm_checksum;
1749 
1750 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_checksum);
1751 	cmd->flags = ICE_AQC_NVM_CHECKSUM_RECALC;
1752 
1753 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1754 
1755 	ice_release_nvm(hw);
1756 
1757 	return status;
1758 }
1759 
1760 /**
1761  * ice_nvm_write_activate
1762  * @hw: pointer to the HW struct
1763  * @cmd_flags: flags for write activate command
1764  * @response_flags: response indicators from firmware
1765  *
1766  * Update the control word with the required banks' validity bits
1767  * and dumps the Shadow RAM to flash (0x0707)
1768  *
1769  * cmd_flags controls which banks to activate, the preservation level to use
1770  * when activating the NVM bank, and whether an EMP reset is required for
1771  * activation.
1772  *
1773  * Note that the 16bit cmd_flags value is split between two separate 1 byte
1774  * flag values in the descriptor.
1775  *
1776  * On successful return of the firmware command, the response_flags variable
1777  * is updated with the flags reported by firmware indicating certain status,
1778  * such as whether EMP reset is enabled.
1779  */
1780 int ice_nvm_write_activate(struct ice_hw *hw, u16 cmd_flags, u8 *response_flags)
1781 {
1782 	struct ice_aqc_nvm *cmd;
1783 	struct ice_aq_desc desc;
1784 	int err;
1785 
1786 	cmd = &desc.params.nvm;
1787 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_write_activate);
1788 
1789 	cmd->cmd_flags = (u8)(cmd_flags & 0xFF);
1790 	cmd->offset_high = (u8)((cmd_flags >> 8) & 0xFF);
1791 
1792 	err = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1793 	if (!err && response_flags)
1794 		*response_flags = cmd->cmd_flags;
1795 
1796 	return err;
1797 }
1798 
1799 /**
1800  * ice_get_nvm_minsrevs - Get the Minimum Security Revision values from flash
1801  * @hw: pointer to the HW struct
1802  * @minsrevs: structure to store NVM and OROM minsrev values
1803  *
1804  * Read the Minimum Security Revision TLV and extract the revision values from
1805  * the flash image into a readable structure for processing.
1806  */
1807 int
1808 ice_get_nvm_minsrevs(struct ice_hw *hw, struct ice_minsrev_info *minsrevs)
1809 {
1810 	struct ice_aqc_nvm_minsrev data;
1811 	int status;
1812 	u16 valid;
1813 
1814 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1815 
1816 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1817 	if (status)
1818 		return status;
1819 
1820 	status = ice_aq_read_nvm(hw, ICE_AQC_NVM_MINSREV_MOD_ID, 0, sizeof(data),
1821 				 &data, true, false, NULL);
1822 
1823 	ice_release_nvm(hw);
1824 
1825 	if (status)
1826 		return status;
1827 
1828 	valid = LE16_TO_CPU(data.validity);
1829 
1830 	/* Extract NVM minimum security revision */
1831 	if (valid & ICE_AQC_NVM_MINSREV_NVM_VALID) {
1832 		u16 minsrev_l, minsrev_h;
1833 
1834 		minsrev_l = LE16_TO_CPU(data.nvm_minsrev_l);
1835 		minsrev_h = LE16_TO_CPU(data.nvm_minsrev_h);
1836 
1837 		minsrevs->nvm = minsrev_h << 16 | minsrev_l;
1838 		minsrevs->nvm_valid = true;
1839 	}
1840 
1841 	/* Extract the OROM minimum security revision */
1842 	if (valid & ICE_AQC_NVM_MINSREV_OROM_VALID) {
1843 		u16 minsrev_l, minsrev_h;
1844 
1845 		minsrev_l = LE16_TO_CPU(data.orom_minsrev_l);
1846 		minsrev_h = LE16_TO_CPU(data.orom_minsrev_h);
1847 
1848 		minsrevs->orom = minsrev_h << 16 | minsrev_l;
1849 		minsrevs->orom_valid = true;
1850 	}
1851 
1852 	return 0;
1853 }
1854 
1855 /**
1856  * ice_update_nvm_minsrevs - Update minimum security revision TLV data in flash
1857  * @hw: pointer to the HW struct
1858  * @minsrevs: minimum security revision information
1859  *
1860  * Update the NVM or Option ROM minimum security revision fields in the PFA
1861  * area of the flash. Reads the minsrevs->nvm_valid and minsrevs->orom_valid
1862  * fields to determine what update is being requested. If the valid bit is not
1863  * set for that module, then the associated minsrev will be left as is.
1864  */
1865 int
1866 ice_update_nvm_minsrevs(struct ice_hw *hw, struct ice_minsrev_info *minsrevs)
1867 {
1868 	struct ice_aqc_nvm_minsrev data;
1869 	int status;
1870 
1871 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1872 
1873 	if (!minsrevs->nvm_valid && !minsrevs->orom_valid) {
1874 		ice_debug(hw, ICE_DBG_NVM, "At least one of NVM and OROM MinSrev must be valid");
1875 		return ICE_ERR_PARAM;
1876 	}
1877 
1878 	status = ice_acquire_nvm(hw, ICE_RES_WRITE);
1879 	if (status)
1880 		return status;
1881 
1882 	/* Get current data */
1883 	status = ice_aq_read_nvm(hw, ICE_AQC_NVM_MINSREV_MOD_ID, 0, sizeof(data),
1884 				 &data, true, false, NULL);
1885 	if (status)
1886 		goto exit_release_res;
1887 
1888 	if (minsrevs->nvm_valid) {
1889 		data.nvm_minsrev_l = CPU_TO_LE16(minsrevs->nvm & 0xFFFF);
1890 		data.nvm_minsrev_h = CPU_TO_LE16(minsrevs->nvm >> 16);
1891 		data.validity |= CPU_TO_LE16(ICE_AQC_NVM_MINSREV_NVM_VALID);
1892 	}
1893 
1894 	if (minsrevs->orom_valid) {
1895 		data.orom_minsrev_l = CPU_TO_LE16(minsrevs->orom & 0xFFFF);
1896 		data.orom_minsrev_h = CPU_TO_LE16(minsrevs->orom >> 16);
1897 		data.validity |= CPU_TO_LE16(ICE_AQC_NVM_MINSREV_OROM_VALID);
1898 	}
1899 
1900 	/* Update flash data */
1901 	status = ice_aq_update_nvm(hw, ICE_AQC_NVM_MINSREV_MOD_ID, 0, sizeof(data), &data,
1902 				   false, ICE_AQC_NVM_SPECIAL_UPDATE, NULL);
1903 	if (status)
1904 		goto exit_release_res;
1905 
1906 	/* Dump the Shadow RAM to the flash */
1907 	status = ice_nvm_write_activate(hw, 0, NULL);
1908 
1909 exit_release_res:
1910 	ice_release_nvm(hw);
1911 
1912 	return status;
1913 }
1914 
1915 /**
1916  * ice_nvm_access_get_features - Return the NVM access features structure
1917  * @cmd: NVM access command to process
1918  * @data: storage for the driver NVM features
1919  *
1920  * Fill in the data section of the NVM access request with a copy of the NVM
1921  * features structure.
1922  */
1923 int
1924 ice_nvm_access_get_features(struct ice_nvm_access_cmd *cmd,
1925 			    union ice_nvm_access_data *data)
1926 {
1927 	/* The provided data_size must be at least as large as our NVM
1928 	 * features structure. A larger size should not be treated as an
1929 	 * error, to allow future extensions to the features structure to
1930 	 * work on older drivers.
1931 	 */
1932 	if (cmd->data_size < sizeof(struct ice_nvm_features))
1933 		return ICE_ERR_NO_MEMORY;
1934 
1935 	/* Initialize the data buffer to zeros */
1936 	ice_memset(data, 0, cmd->data_size, ICE_NONDMA_MEM);
1937 
1938 	/* Fill in the features data */
1939 	data->drv_features.major = ICE_NVM_ACCESS_MAJOR_VER;
1940 	data->drv_features.minor = ICE_NVM_ACCESS_MINOR_VER;
1941 	data->drv_features.size = sizeof(struct ice_nvm_features);
1942 	data->drv_features.features[0] = ICE_NVM_FEATURES_0_REG_ACCESS;
1943 
1944 	return 0;
1945 }
1946 
1947 /**
1948  * ice_nvm_access_get_module - Helper function to read module value
1949  * @cmd: NVM access command structure
1950  *
1951  * Reads the module value out of the NVM access config field.
1952  */
1953 u32 ice_nvm_access_get_module(struct ice_nvm_access_cmd *cmd)
1954 {
1955 	return ((cmd->config & ICE_NVM_CFG_MODULE_M) >> ICE_NVM_CFG_MODULE_S);
1956 }
1957 
1958 /**
1959  * ice_nvm_access_get_flags - Helper function to read flags value
1960  * @cmd: NVM access command structure
1961  *
1962  * Reads the flags value out of the NVM access config field.
1963  */
1964 u32 ice_nvm_access_get_flags(struct ice_nvm_access_cmd *cmd)
1965 {
1966 	return ((cmd->config & ICE_NVM_CFG_FLAGS_M) >> ICE_NVM_CFG_FLAGS_S);
1967 }
1968 
1969 /**
1970  * ice_nvm_access_get_adapter - Helper function to read adapter info
1971  * @cmd: NVM access command structure
1972  *
1973  * Read the adapter info value out of the NVM access config field.
1974  */
1975 u32 ice_nvm_access_get_adapter(struct ice_nvm_access_cmd *cmd)
1976 {
1977 	return ((cmd->config & ICE_NVM_CFG_ADAPTER_INFO_M) >>
1978 		ICE_NVM_CFG_ADAPTER_INFO_S);
1979 }
1980 
1981 /**
1982  * ice_validate_nvm_rw_reg - Check than an NVM access request is valid
1983  * @cmd: NVM access command structure
1984  *
1985  * Validates that an NVM access structure is request to read or write a valid
1986  * register offset. First validates that the module and flags are correct, and
1987  * then ensures that the register offset is one of the accepted registers.
1988  */
1989 static int
1990 ice_validate_nvm_rw_reg(struct ice_nvm_access_cmd *cmd)
1991 {
1992 	u32 module, flags, offset;
1993 	u16 i;
1994 
1995 	module = ice_nvm_access_get_module(cmd);
1996 	flags = ice_nvm_access_get_flags(cmd);
1997 	offset = cmd->offset;
1998 
1999 	/* Make sure the module and flags indicate a read/write request */
2000 	if (module != ICE_NVM_REG_RW_MODULE ||
2001 	    flags != ICE_NVM_REG_RW_FLAGS ||
2002 	    cmd->data_size != FIELD_SIZEOF(union ice_nvm_access_data, regval))
2003 		return ICE_ERR_PARAM;
2004 
2005 	switch (offset) {
2006 	case GL_HICR:
2007 	case GL_HICR_EN: /* Note, this register is read only */
2008 	case GL_FWSTS:
2009 	case GL_MNG_FWSM:
2010 	case GLGEN_CSR_DEBUG_C:
2011 	case GLGEN_RSTAT:
2012 	case GLPCI_LBARCTRL:
2013 	case GL_MNG_DEF_DEVID:
2014 	case GLNVM_GENS:
2015 	case GLNVM_FLA:
2016 	case PF_FUNC_RID:
2017 		return 0;
2018 	default:
2019 		break;
2020 	}
2021 
2022 	for (i = 0; i <= GL_HIDA_MAX_INDEX; i++)
2023 		if (offset == (u32)GL_HIDA(i))
2024 			return 0;
2025 
2026 	for (i = 0; i <= GL_HIBA_MAX_INDEX; i++)
2027 		if (offset == (u32)GL_HIBA(i))
2028 			return 0;
2029 
2030 	/* All other register offsets are not valid */
2031 	return ICE_ERR_OUT_OF_RANGE;
2032 }
2033 
2034 /**
2035  * ice_nvm_access_read - Handle an NVM read request
2036  * @hw: pointer to the HW struct
2037  * @cmd: NVM access command to process
2038  * @data: storage for the register value read
2039  *
2040  * Process an NVM access request to read a register.
2041  */
2042 int
2043 ice_nvm_access_read(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
2044 		    union ice_nvm_access_data *data)
2045 {
2046 	int status;
2047 
2048 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
2049 
2050 	/* Always initialize the output data, even on failure */
2051 	ice_memset(data, 0, cmd->data_size, ICE_NONDMA_MEM);
2052 
2053 	/* Make sure this is a valid read/write access request */
2054 	status = ice_validate_nvm_rw_reg(cmd);
2055 	if (status)
2056 		return status;
2057 
2058 	ice_debug(hw, ICE_DBG_NVM, "NVM access: reading register %08x\n",
2059 		  cmd->offset);
2060 
2061 	/* Read the register and store the contents in the data field */
2062 	data->regval = rd32(hw, cmd->offset);
2063 
2064 	return 0;
2065 }
2066 
2067 /**
2068  * ice_nvm_access_write - Handle an NVM write request
2069  * @hw: pointer to the HW struct
2070  * @cmd: NVM access command to process
2071  * @data: NVM access data to write
2072  *
2073  * Process an NVM access request to write a register.
2074  */
2075 int
2076 ice_nvm_access_write(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
2077 		     union ice_nvm_access_data *data)
2078 {
2079 	int status;
2080 
2081 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
2082 
2083 	/* Make sure this is a valid read/write access request */
2084 	status = ice_validate_nvm_rw_reg(cmd);
2085 	if (status)
2086 		return status;
2087 
2088 	/* Reject requests to write to read-only registers */
2089 	if (hw->mac_type == ICE_MAC_E830) {
2090 		if (cmd->offset == E830_GL_HICR_EN)
2091 			return ICE_ERR_OUT_OF_RANGE;
2092 	} else {
2093 		if (cmd->offset == GL_HICR_EN)
2094 			return ICE_ERR_OUT_OF_RANGE;
2095 	}
2096 
2097 	if (cmd->offset == GLGEN_RSTAT)
2098 		return ICE_ERR_OUT_OF_RANGE;
2099 
2100 	ice_debug(hw, ICE_DBG_NVM, "NVM access: writing register %08x with value %08x\n",
2101 		  cmd->offset, data->regval);
2102 
2103 	/* Write the data field to the specified register */
2104 	wr32(hw, cmd->offset, data->regval);
2105 
2106 	return 0;
2107 }
2108 
2109 /**
2110  * ice_handle_nvm_access - Handle an NVM access request
2111  * @hw: pointer to the HW struct
2112  * @cmd: NVM access command info
2113  * @data: pointer to read or return data
2114  *
2115  * Process an NVM access request. Read the command structure information and
2116  * determine if it is valid. If not, report an error indicating the command
2117  * was invalid.
2118  *
2119  * For valid commands, perform the necessary function, copying the data into
2120  * the provided data buffer.
2121  */
2122 int
2123 ice_handle_nvm_access(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
2124 		      union ice_nvm_access_data *data)
2125 {
2126 	u32 module, flags, adapter_info;
2127 
2128 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
2129 
2130 	/* Extended flags are currently reserved and must be zero */
2131 	if ((cmd->config & ICE_NVM_CFG_EXT_FLAGS_M) != 0)
2132 		return ICE_ERR_PARAM;
2133 
2134 	/* Adapter info must match the HW device ID */
2135 	adapter_info = ice_nvm_access_get_adapter(cmd);
2136 	if (adapter_info != hw->device_id)
2137 		return ICE_ERR_PARAM;
2138 
2139 	switch (cmd->command) {
2140 	case ICE_NVM_CMD_READ:
2141 		module = ice_nvm_access_get_module(cmd);
2142 		flags = ice_nvm_access_get_flags(cmd);
2143 
2144 		/* Getting the driver's NVM features structure shares the same
2145 		 * command type as reading a register. Read the config field
2146 		 * to determine if this is a request to get features.
2147 		 */
2148 		if (module == ICE_NVM_GET_FEATURES_MODULE &&
2149 		    flags == ICE_NVM_GET_FEATURES_FLAGS &&
2150 		    cmd->offset == 0)
2151 			return ice_nvm_access_get_features(cmd, data);
2152 		else
2153 			return ice_nvm_access_read(hw, cmd, data);
2154 	case ICE_NVM_CMD_WRITE:
2155 		return ice_nvm_access_write(hw, cmd, data);
2156 	default:
2157 		return ICE_ERR_PARAM;
2158 	}
2159 }
2160 
2161 /**
2162  * ice_nvm_sanitize_operate - Clear the user data
2163  * @hw: pointer to the HW struct
2164  *
2165  * Clear user data from NVM using AQ command (0x070C).
2166  *
2167  * Return: the exit code of the operation.
2168  */
2169 s32 ice_nvm_sanitize_operate(struct ice_hw *hw)
2170 {
2171 	s32 status;
2172 	u8 values;
2173 
2174 	u8 cmd_flags = ICE_AQ_NVM_SANITIZE_REQ_OPERATE |
2175 		       ICE_AQ_NVM_SANITIZE_OPERATE_SUBJECT_CLEAR;
2176 
2177 	status = ice_nvm_sanitize(hw, cmd_flags, &values);
2178 	if (status)
2179 		return status;
2180 	if ((!(values & ICE_AQ_NVM_SANITIZE_OPERATE_HOST_CLEAN_DONE) &&
2181 	     !(values & ICE_AQ_NVM_SANITIZE_OPERATE_BMC_CLEAN_DONE)) ||
2182 	    ((values & ICE_AQ_NVM_SANITIZE_OPERATE_HOST_CLEAN_DONE) &&
2183 	     !(values & ICE_AQ_NVM_SANITIZE_OPERATE_HOST_CLEAN_SUCCESS)) ||
2184 	    ((values & ICE_AQ_NVM_SANITIZE_OPERATE_BMC_CLEAN_DONE) &&
2185 	     !(values & ICE_AQ_NVM_SANITIZE_OPERATE_BMC_CLEAN_SUCCESS)))
2186 		return ICE_ERR_AQ_ERROR;
2187 
2188 	return ICE_SUCCESS;
2189 }
2190 
2191 /**
2192  * ice_nvm_sanitize - Sanitize NVM
2193  * @hw: pointer to the HW struct
2194  * @cmd_flags: flag to the ACI command
2195  * @values: values returned from the command
2196  *
2197  * Sanitize NVM using AQ command (0x070C).
2198  *
2199  * Return: the exit code of the operation.
2200  */
2201 s32 ice_nvm_sanitize(struct ice_hw *hw, u8 cmd_flags, u8 *values)
2202 {
2203 	struct ice_aqc_nvm_sanitization *cmd;
2204 	struct ice_aq_desc desc;
2205 	s32 status;
2206 
2207 	cmd = &desc.params.sanitization;
2208 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_sanitization);
2209 	cmd->cmd_flags = cmd_flags;
2210 
2211 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
2212 	if (values)
2213 		*values = cmd->values;
2214 
2215 	return status;
2216 }
2217