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