xref: /freebsd/sys/dev/ice/ice_nvm.c (revision ccfd87fe2ac0e2e6aeb1911a7d7cce6712a8564f)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*  Copyright (c) 2023, Intel Corporation
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above copyright notice,
9  *      this list of conditions and the following disclaimer.
10  *
11  *   2. Redistributions in binary form must reproduce the above copyright
12  *      notice, this list of conditions and the following disclaimer in the
13  *      documentation and/or other materials provided with the distribution.
14  *
15  *   3. Neither the name of the Intel Corporation nor the names of its
16  *      contributors may be used to endorse or promote products derived from
17  *      this software without specific prior written permission.
18  *
19  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  *  POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*$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 = (u16)(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 	u8 *orom_data;
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 	 * The exact location is unknown and varies between images but is
995 	 * usually somewhere in the middle of the bank. We need to scan the
996 	 * Option ROM bank to locate it.
997 	 *
998 	 * It's significantly faster to read the entire Option ROM up front
999 	 * using the maximum page size, than to read each possible location
1000 	 * with a separate firmware command.
1001 	 */
1002 	orom_data = (u8 *)ice_calloc(hw, hw->flash.banks.orom_size, sizeof(u8));
1003 	if (!orom_data)
1004 		return ICE_ERR_NO_MEMORY;
1005 
1006 	status = ice_read_flash_module(hw, bank, ICE_SR_1ST_OROM_BANK_PTR, 0,
1007 				       orom_data, hw->flash.banks.orom_size);
1008 	if (status) {
1009 		ice_debug(hw, ICE_DBG_NVM, "Unable to read Option ROM data\n");
1010 		return status;
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 		struct ice_orom_civd_info *tmp;
1016 		u8 sum = 0, i;
1017 
1018 		tmp = (struct ice_orom_civd_info *)&orom_data[offset];
1019 
1020 		/* Skip forward until we find a matching signature */
1021 		if (memcmp("$CIV", tmp->signature, sizeof(tmp->signature)) != 0)
1022 			continue;
1023 
1024 		ice_debug(hw, ICE_DBG_NVM, "Found CIVD section at offset %u\n",
1025 			  offset);
1026 
1027 		/* Verify that the simple checksum is zero */
1028 		for (i = 0; i < sizeof(*tmp); i++)
1029 			sum += ((u8 *)tmp)[i];
1030 
1031 		if (sum) {
1032 			ice_debug(hw, ICE_DBG_NVM, "Found CIVD data with invalid checksum of %u\n",
1033 				  sum);
1034 			goto err_invalid_checksum;
1035 		}
1036 
1037 		*civd = *tmp;
1038 		ice_free(hw, orom_data);
1039 		return ICE_SUCCESS;
1040 	}
1041 
1042 	ice_debug(hw, ICE_DBG_NVM, "Unable to locate CIVD data within the Option ROM\n");
1043 
1044 err_invalid_checksum:
1045 	ice_free(hw, orom_data);
1046 	return ICE_ERR_NVM;
1047 }
1048 
1049 /**
1050  * ice_get_orom_ver_info - Read Option ROM version information
1051  * @hw: pointer to the HW struct
1052  * @bank: whether to read from the active or inactive flash module
1053  * @orom: pointer to Option ROM info structure
1054  *
1055  * Read Option ROM version and security revision from the Option ROM flash
1056  * section.
1057  */
1058 static enum ice_status
1059 ice_get_orom_ver_info(struct ice_hw *hw, enum ice_bank_select bank, struct ice_orom_info *orom)
1060 {
1061 	struct ice_orom_civd_info civd;
1062 	enum ice_status status;
1063 	u32 combo_ver;
1064 
1065 	status = ice_get_orom_civd_data(hw, bank, &civd);
1066 	if (status) {
1067 		ice_debug(hw, ICE_DBG_NVM, "Failed to locate valid Option ROM CIVD data\n");
1068 		return status;
1069 	}
1070 
1071 	combo_ver = LE32_TO_CPU(civd.combo_ver);
1072 
1073 	orom->major = (u8)((combo_ver & ICE_OROM_VER_MASK) >> ICE_OROM_VER_SHIFT);
1074 	orom->patch = (u8)(combo_ver & ICE_OROM_VER_PATCH_MASK);
1075 	orom->build = (u16)((combo_ver & ICE_OROM_VER_BUILD_MASK) >> ICE_OROM_VER_BUILD_SHIFT);
1076 
1077 	status = ice_get_orom_srev(hw, bank, &orom->srev);
1078 	if (status) {
1079 		ice_debug(hw, ICE_DBG_NVM, "Failed to read Option ROM security revision.\n");
1080 		return status;
1081 	}
1082 
1083 	return ICE_SUCCESS;
1084 }
1085 
1086 /**
1087  * ice_get_inactive_orom_ver - Read Option ROM version from the inactive bank
1088  * @hw: pointer to the HW structure
1089  * @orom: storage for Option ROM version information
1090  *
1091  * Reads the Option ROM version and security revision data for the inactive
1092  * section of flash. Used to access version data for a pending update that has
1093  * not yet been activated.
1094  */
1095 enum ice_status ice_get_inactive_orom_ver(struct ice_hw *hw, struct ice_orom_info *orom)
1096 {
1097 	return ice_get_orom_ver_info(hw, ICE_INACTIVE_FLASH_BANK, orom);
1098 }
1099 
1100 /**
1101  * ice_get_netlist_info
1102  * @hw: pointer to the HW struct
1103  * @bank: whether to read from the active or inactive flash bank
1104  * @netlist: pointer to netlist version info structure
1105  *
1106  * Get the netlist version information from the requested bank. Reads the Link
1107  * Topology section to find the Netlist ID block and extract the relevant
1108  * information into the netlist version structure.
1109  */
1110 static enum ice_status
1111 ice_get_netlist_info(struct ice_hw *hw, enum ice_bank_select bank,
1112 		     struct ice_netlist_info *netlist)
1113 {
1114 	u16 module_id, length, node_count, i;
1115 	enum ice_status status;
1116 	u16 *id_blk;
1117 
1118 	status = ice_read_netlist_module(hw, bank, ICE_NETLIST_TYPE_OFFSET, &module_id);
1119 	if (status)
1120 		return status;
1121 
1122 	if (module_id != ICE_NETLIST_LINK_TOPO_MOD_ID) {
1123 		ice_debug(hw, ICE_DBG_NVM, "Expected netlist module_id ID of 0x%04x, but got 0x%04x\n",
1124 			  ICE_NETLIST_LINK_TOPO_MOD_ID, module_id);
1125 		return ICE_ERR_NVM;
1126 	}
1127 
1128 	status = ice_read_netlist_module(hw, bank, ICE_LINK_TOPO_MODULE_LEN, &length);
1129 	if (status)
1130 		return status;
1131 
1132 	/* sanity check that we have at least enough words to store the netlist ID block */
1133 	if (length < ICE_NETLIST_ID_BLK_SIZE) {
1134 		ice_debug(hw, ICE_DBG_NVM, "Netlist Link Topology module too small. Expected at least %u words, but got %u words.\n",
1135 			  ICE_NETLIST_ID_BLK_SIZE, length);
1136 		return ICE_ERR_NVM;
1137 	}
1138 
1139 	status = ice_read_netlist_module(hw, bank, ICE_LINK_TOPO_NODE_COUNT, &node_count);
1140 	if (status)
1141 		return status;
1142 	node_count &= ICE_LINK_TOPO_NODE_COUNT_M;
1143 
1144 	id_blk = (u16 *)ice_calloc(hw, ICE_NETLIST_ID_BLK_SIZE, sizeof(*id_blk));
1145 	if (!id_blk)
1146 		return ICE_ERR_NO_MEMORY;
1147 
1148 	/* Read out the entire Netlist ID Block at once. */
1149 	status = ice_read_flash_module(hw, bank, ICE_SR_NETLIST_BANK_PTR,
1150 				       ICE_NETLIST_ID_BLK_OFFSET(node_count) * sizeof(u16),
1151 				       (u8 *)id_blk, ICE_NETLIST_ID_BLK_SIZE * sizeof(u16));
1152 	if (status)
1153 		goto exit_error;
1154 
1155 	for (i = 0; i < ICE_NETLIST_ID_BLK_SIZE; i++)
1156 		id_blk[i] = LE16_TO_CPU(((_FORCE_ __le16 *)id_blk)[i]);
1157 
1158 	netlist->major = id_blk[ICE_NETLIST_ID_BLK_MAJOR_VER_HIGH] << 16 |
1159 			 id_blk[ICE_NETLIST_ID_BLK_MAJOR_VER_LOW];
1160 	netlist->minor = id_blk[ICE_NETLIST_ID_BLK_MINOR_VER_HIGH] << 16 |
1161 			 id_blk[ICE_NETLIST_ID_BLK_MINOR_VER_LOW];
1162 	netlist->type = id_blk[ICE_NETLIST_ID_BLK_TYPE_HIGH] << 16 |
1163 			id_blk[ICE_NETLIST_ID_BLK_TYPE_LOW];
1164 	netlist->rev = id_blk[ICE_NETLIST_ID_BLK_REV_HIGH] << 16 |
1165 		       id_blk[ICE_NETLIST_ID_BLK_REV_LOW];
1166 	netlist->cust_ver = id_blk[ICE_NETLIST_ID_BLK_CUST_VER];
1167 	/* Read the left most 4 bytes of SHA */
1168 	netlist->hash = id_blk[ICE_NETLIST_ID_BLK_SHA_HASH_WORD(15)] << 16 |
1169 			id_blk[ICE_NETLIST_ID_BLK_SHA_HASH_WORD(14)];
1170 
1171 exit_error:
1172 	ice_free(hw, id_blk);
1173 
1174 	return status;
1175 }
1176 
1177 /**
1178  * ice_get_netlist_ver_info
1179  * @hw: pointer to the HW struct
1180  * @netlist: pointer to netlist version info structure
1181  *
1182  * Get the netlist version information
1183  */
1184 enum ice_status ice_get_netlist_ver_info(struct ice_hw *hw, struct ice_netlist_info *netlist)
1185 {
1186 	return ice_get_netlist_info(hw, ICE_ACTIVE_FLASH_BANK, netlist);
1187 }
1188 
1189 /**
1190  * ice_get_inactive_netlist_ver
1191  * @hw: pointer to the HW struct
1192  * @netlist: pointer to netlist version info structure
1193  *
1194  * Read the netlist version data from the inactive netlist bank. Used to
1195  * extract version data of a pending flash update in order to display the
1196  * version data.
1197  */
1198 enum ice_status ice_get_inactive_netlist_ver(struct ice_hw *hw, struct ice_netlist_info *netlist)
1199 {
1200 	return ice_get_netlist_info(hw, ICE_INACTIVE_FLASH_BANK, netlist);
1201 }
1202 
1203 /**
1204  * ice_discover_flash_size - Discover the available flash size.
1205  * @hw: pointer to the HW struct
1206  *
1207  * The device flash could be up to 16MB in size. However, it is possible that
1208  * the actual size is smaller. Use bisection to determine the accessible size
1209  * of flash memory.
1210  */
1211 static enum ice_status ice_discover_flash_size(struct ice_hw *hw)
1212 {
1213 	u32 min_size = 0, max_size = ICE_AQC_NVM_MAX_OFFSET + 1;
1214 	enum ice_status status;
1215 
1216 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1217 
1218 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1219 	if (status)
1220 		return status;
1221 
1222 	while ((max_size - min_size) > 1) {
1223 		u32 offset = (max_size + min_size) / 2;
1224 		u32 len = 1;
1225 		u8 data;
1226 
1227 		status = ice_read_flat_nvm(hw, offset, &len, &data, false);
1228 		if (status == ICE_ERR_AQ_ERROR &&
1229 		    hw->adminq.sq_last_status == ICE_AQ_RC_EINVAL) {
1230 			ice_debug(hw, ICE_DBG_NVM, "%s: New upper bound of %u bytes\n",
1231 				  __func__, offset);
1232 			status = ICE_SUCCESS;
1233 			max_size = offset;
1234 		} else if (!status) {
1235 			ice_debug(hw, ICE_DBG_NVM, "%s: New lower bound of %u bytes\n",
1236 				  __func__, offset);
1237 			min_size = offset;
1238 		} else {
1239 			/* an unexpected error occurred */
1240 			goto err_read_flat_nvm;
1241 		}
1242 	}
1243 
1244 	ice_debug(hw, ICE_DBG_NVM, "Predicted flash size is %u bytes\n", max_size);
1245 
1246 	hw->flash.flash_size = max_size;
1247 
1248 err_read_flat_nvm:
1249 	ice_release_nvm(hw);
1250 
1251 	return status;
1252 }
1253 
1254 /**
1255  * ice_read_sr_pointer - Read the value of a Shadow RAM pointer word
1256  * @hw: pointer to the HW structure
1257  * @offset: the word offset of the Shadow RAM word to read
1258  * @pointer: pointer value read from Shadow RAM
1259  *
1260  * Read the given Shadow RAM word, and convert it to a pointer value specified
1261  * in bytes. This function assumes the specified offset is a valid pointer
1262  * word.
1263  *
1264  * Each pointer word specifies whether it is stored in word size or 4KB
1265  * sector size by using the highest bit. The reported pointer value will be in
1266  * bytes, intended for flat NVM reads.
1267  */
1268 static enum ice_status
1269 ice_read_sr_pointer(struct ice_hw *hw, u16 offset, u32 *pointer)
1270 {
1271 	enum ice_status status;
1272 	u16 value;
1273 
1274 	status = ice_read_sr_word(hw, offset, &value);
1275 	if (status)
1276 		return status;
1277 
1278 	/* Determine if the pointer is in 4KB or word units */
1279 	if (value & ICE_SR_NVM_PTR_4KB_UNITS)
1280 		*pointer = (value & ~ICE_SR_NVM_PTR_4KB_UNITS) * 4 * 1024;
1281 	else
1282 		*pointer = value * 2;
1283 
1284 	return ICE_SUCCESS;
1285 }
1286 
1287 /**
1288  * ice_read_sr_area_size - Read an area size from a Shadow RAM word
1289  * @hw: pointer to the HW structure
1290  * @offset: the word offset of the Shadow RAM to read
1291  * @size: size value read from the Shadow RAM
1292  *
1293  * Read the given Shadow RAM word, and convert it to an area size value
1294  * specified in bytes. This function assumes the specified offset is a valid
1295  * area size word.
1296  *
1297  * Each area size word is specified in 4KB sector units. This function reports
1298  * the size in bytes, intended for flat NVM reads.
1299  */
1300 static enum ice_status
1301 ice_read_sr_area_size(struct ice_hw *hw, u16 offset, u32 *size)
1302 {
1303 	enum ice_status status;
1304 	u16 value;
1305 
1306 	status = ice_read_sr_word(hw, offset, &value);
1307 	if (status)
1308 		return status;
1309 
1310 	/* Area sizes are always specified in 4KB units */
1311 	*size = value * 4 * 1024;
1312 
1313 	return ICE_SUCCESS;
1314 }
1315 
1316 /**
1317  * ice_determine_active_flash_banks - Discover active bank for each module
1318  * @hw: pointer to the HW struct
1319  *
1320  * Read the Shadow RAM control word and determine which banks are active for
1321  * the NVM, OROM, and Netlist modules. Also read and calculate the associated
1322  * pointer and size. These values are then cached into the ice_flash_info
1323  * structure for later use in order to calculate the correct offset to read
1324  * from the active module.
1325  */
1326 static enum ice_status
1327 ice_determine_active_flash_banks(struct ice_hw *hw)
1328 {
1329 	struct ice_bank_info *banks = &hw->flash.banks;
1330 	enum ice_status status;
1331 	u16 ctrl_word;
1332 
1333 	status = ice_read_sr_word(hw, ICE_SR_NVM_CTRL_WORD, &ctrl_word);
1334 	if (status) {
1335 		ice_debug(hw, ICE_DBG_NVM, "Failed to read the Shadow RAM control word\n");
1336 		return status;
1337 	}
1338 
1339 	/* Check that the control word indicates validity */
1340 	if ((ctrl_word & ICE_SR_CTRL_WORD_1_M) >> ICE_SR_CTRL_WORD_1_S != ICE_SR_CTRL_WORD_VALID) {
1341 		ice_debug(hw, ICE_DBG_NVM, "Shadow RAM control word is invalid\n");
1342 		return ICE_ERR_CFG;
1343 	}
1344 
1345 	if (!(ctrl_word & ICE_SR_CTRL_WORD_NVM_BANK))
1346 		banks->nvm_bank = ICE_1ST_FLASH_BANK;
1347 	else
1348 		banks->nvm_bank = ICE_2ND_FLASH_BANK;
1349 
1350 	if (!(ctrl_word & ICE_SR_CTRL_WORD_OROM_BANK))
1351 		banks->orom_bank = ICE_1ST_FLASH_BANK;
1352 	else
1353 		banks->orom_bank = ICE_2ND_FLASH_BANK;
1354 
1355 	if (!(ctrl_word & ICE_SR_CTRL_WORD_NETLIST_BANK))
1356 		banks->netlist_bank = ICE_1ST_FLASH_BANK;
1357 	else
1358 		banks->netlist_bank = ICE_2ND_FLASH_BANK;
1359 
1360 	status = ice_read_sr_pointer(hw, ICE_SR_1ST_NVM_BANK_PTR, &banks->nvm_ptr);
1361 	if (status) {
1362 		ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank pointer\n");
1363 		return status;
1364 	}
1365 
1366 	status = ice_read_sr_area_size(hw, ICE_SR_NVM_BANK_SIZE, &banks->nvm_size);
1367 	if (status) {
1368 		ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank area size\n");
1369 		return status;
1370 	}
1371 
1372 	status = ice_read_sr_pointer(hw, ICE_SR_1ST_OROM_BANK_PTR, &banks->orom_ptr);
1373 	if (status) {
1374 		ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank pointer\n");
1375 		return status;
1376 	}
1377 
1378 	status = ice_read_sr_area_size(hw, ICE_SR_OROM_BANK_SIZE, &banks->orom_size);
1379 	if (status) {
1380 		ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank area size\n");
1381 		return status;
1382 	}
1383 
1384 	status = ice_read_sr_pointer(hw, ICE_SR_NETLIST_BANK_PTR, &banks->netlist_ptr);
1385 	if (status) {
1386 		ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank pointer\n");
1387 		return status;
1388 	}
1389 
1390 	status = ice_read_sr_area_size(hw, ICE_SR_NETLIST_BANK_SIZE, &banks->netlist_size);
1391 	if (status) {
1392 		ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank area size\n");
1393 		return status;
1394 	}
1395 
1396 	return ICE_SUCCESS;
1397 }
1398 
1399 /**
1400  * ice_init_nvm - initializes NVM setting
1401  * @hw: pointer to the HW struct
1402  *
1403  * This function reads and populates NVM settings such as Shadow RAM size,
1404  * max_timeout, and blank_nvm_mode
1405  */
1406 enum ice_status ice_init_nvm(struct ice_hw *hw)
1407 {
1408 	struct ice_flash_info *flash = &hw->flash;
1409 	enum ice_status status;
1410 	u32 fla, gens_stat;
1411 	u8 sr_size;
1412 
1413 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1414 
1415 	/* The SR size is stored regardless of the NVM programming mode
1416 	 * as the blank mode may be used in the factory line.
1417 	 */
1418 	gens_stat = rd32(hw, GLNVM_GENS);
1419 	sr_size = (gens_stat & GLNVM_GENS_SR_SIZE_M) >> GLNVM_GENS_SR_SIZE_S;
1420 
1421 	/* Switching to words (sr_size contains power of 2) */
1422 	flash->sr_words = BIT(sr_size) * ICE_SR_WORDS_IN_1KB;
1423 
1424 	/* Check if we are in the normal or blank NVM programming mode */
1425 	fla = rd32(hw, GLNVM_FLA);
1426 	if (fla & GLNVM_FLA_LOCKED_M) { /* Normal programming mode */
1427 		flash->blank_nvm_mode = false;
1428 	} else {
1429 		/* Blank programming mode */
1430 		flash->blank_nvm_mode = true;
1431 		ice_debug(hw, ICE_DBG_NVM, "NVM init error: unsupported blank mode.\n");
1432 		return ICE_ERR_NVM_BLANK_MODE;
1433 	}
1434 
1435 	status = ice_discover_flash_size(hw);
1436 	if (status) {
1437 		ice_debug(hw, ICE_DBG_NVM, "NVM init error: failed to discover flash size.\n");
1438 		return status;
1439 	}
1440 
1441 	status = ice_determine_active_flash_banks(hw);
1442 	if (status) {
1443 		ice_debug(hw, ICE_DBG_NVM, "Failed to determine active flash banks.\n");
1444 		return status;
1445 	}
1446 
1447 	status = ice_get_nvm_ver_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->nvm);
1448 	if (status) {
1449 		ice_debug(hw, ICE_DBG_INIT, "Failed to read NVM info.\n");
1450 		return status;
1451 	}
1452 
1453 	status = ice_get_orom_ver_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->orom);
1454 	if (status)
1455 		ice_debug(hw, ICE_DBG_INIT, "Failed to read Option ROM info.\n");
1456 
1457 	/* read the netlist version information */
1458 	status = ice_get_netlist_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->netlist);
1459 	if (status)
1460 		ice_debug(hw, ICE_DBG_INIT, "Failed to read netlist info.\n");
1461 	return ICE_SUCCESS;
1462 }
1463 
1464 /**
1465  * ice_read_sr_buf - Reads Shadow RAM buf and acquire lock if necessary
1466  * @hw: pointer to the HW structure
1467  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
1468  * @words: (in) number of words to read; (out) number of words actually read
1469  * @data: words read from the Shadow RAM
1470  *
1471  * Reads 16 bit words (data buf) from the SR using the ice_read_nvm_buf_aq
1472  * method. The buf read is preceded by the NVM ownership take
1473  * and followed by the release.
1474  */
1475 enum ice_status
1476 ice_read_sr_buf(struct ice_hw *hw, u16 offset, u16 *words, u16 *data)
1477 {
1478 	enum ice_status status;
1479 
1480 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1481 	if (!status) {
1482 		status = ice_read_sr_buf_aq(hw, offset, words, data);
1483 		ice_release_nvm(hw);
1484 	}
1485 
1486 	return status;
1487 }
1488 
1489 /**
1490  * __ice_write_sr_word - Writes Shadow RAM word
1491  * @hw: pointer to the HW structure
1492  * @offset: offset of the Shadow RAM word to write
1493  * @data: word to write to the Shadow RAM
1494  *
1495  * Writes a 16 bit word to the SR using the ice_write_sr_aq method.
1496  * NVM ownership have to be acquired and released (on ARQ completion event
1497  * reception) by caller. To commit SR to NVM update checksum function
1498  * should be called.
1499  */
1500 enum ice_status
1501 __ice_write_sr_word(struct ice_hw *hw, u32 offset, const u16 *data)
1502 {
1503 	__le16 data_local = CPU_TO_LE16(*data);
1504 
1505 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1506 
1507 	/* Value 0x00 below means that we treat SR as a flat mem */
1508 	return ice_write_sr_aq(hw, offset, 1, &data_local, false);
1509 }
1510 
1511 /**
1512  * __ice_write_sr_buf - Writes Shadow RAM buf
1513  * @hw: pointer to the HW structure
1514  * @offset: offset of the Shadow RAM buffer to write
1515  * @words: number of words to write
1516  * @data: words to write to the Shadow RAM
1517  *
1518  * Writes a 16 bit words buffer to the Shadow RAM using the admin command.
1519  * NVM ownership must be acquired before calling this function and released
1520  * on ARQ completion event reception by caller. To commit SR to NVM update
1521  * checksum function should be called.
1522  */
1523 enum ice_status
1524 __ice_write_sr_buf(struct ice_hw *hw, u32 offset, u16 words, const u16 *data)
1525 {
1526 	enum ice_status status;
1527 	__le16 *data_local;
1528 	void *vmem;
1529 	u32 i;
1530 
1531 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1532 
1533 	vmem = ice_calloc(hw, words, sizeof(u16));
1534 	if (!vmem)
1535 		return ICE_ERR_NO_MEMORY;
1536 	data_local = (_FORCE_ __le16 *)vmem;
1537 
1538 	for (i = 0; i < words; i++)
1539 		data_local[i] = CPU_TO_LE16(data[i]);
1540 
1541 	/* Here we will only write one buffer as the size of the modules
1542 	 * mirrored in the Shadow RAM is always less than 4K.
1543 	 */
1544 	status = ice_write_sr_aq(hw, offset, words, data_local, false);
1545 
1546 	ice_free(hw, vmem);
1547 
1548 	return status;
1549 }
1550 
1551 /**
1552  * ice_calc_sr_checksum - Calculates and returns Shadow RAM SW checksum
1553  * @hw: pointer to hardware structure
1554  * @checksum: pointer to the checksum
1555  *
1556  * This function calculates SW Checksum that covers the whole 64kB shadow RAM
1557  * except the VPD and PCIe ALT Auto-load modules. The structure and size of VPD
1558  * is customer specific and unknown. Therefore, this function skips all maximum
1559  * possible size of VPD (1kB).
1560  */
1561 static enum ice_status ice_calc_sr_checksum(struct ice_hw *hw, u16 *checksum)
1562 {
1563 	enum ice_status status = ICE_SUCCESS;
1564 	u16 pcie_alt_module = 0;
1565 	u16 checksum_local = 0;
1566 	u16 vpd_module;
1567 	void *vmem;
1568 	u16 *data;
1569 	u16 i;
1570 
1571 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1572 
1573 	vmem = ice_calloc(hw, ICE_SR_SECTOR_SIZE_IN_WORDS, sizeof(u16));
1574 	if (!vmem)
1575 		return ICE_ERR_NO_MEMORY;
1576 	data = (u16 *)vmem;
1577 
1578 	/* read pointer to VPD area */
1579 	status = ice_read_sr_word_aq(hw, ICE_SR_VPD_PTR, &vpd_module);
1580 	if (status)
1581 		goto ice_calc_sr_checksum_exit;
1582 
1583 	/* read pointer to PCIe Alt Auto-load module */
1584 	status = ice_read_sr_word_aq(hw, ICE_SR_PCIE_ALT_AUTO_LOAD_PTR,
1585 				     &pcie_alt_module);
1586 	if (status)
1587 		goto ice_calc_sr_checksum_exit;
1588 
1589 	/* Calculate SW checksum that covers the whole 64kB shadow RAM
1590 	 * except the VPD and PCIe ALT Auto-load modules
1591 	 */
1592 	for (i = 0; i < hw->flash.sr_words; i++) {
1593 		/* Read SR page */
1594 		if ((i % ICE_SR_SECTOR_SIZE_IN_WORDS) == 0) {
1595 			u16 words = ICE_SR_SECTOR_SIZE_IN_WORDS;
1596 
1597 			status = ice_read_sr_buf_aq(hw, i, &words, data);
1598 			if (status != ICE_SUCCESS)
1599 				goto ice_calc_sr_checksum_exit;
1600 		}
1601 
1602 		/* Skip Checksum word */
1603 		if (i == ICE_SR_SW_CHECKSUM_WORD)
1604 			continue;
1605 		/* Skip VPD module (convert byte size to word count) */
1606 		if (i >= (u32)vpd_module &&
1607 		    i < ((u32)vpd_module + ICE_SR_VPD_SIZE_WORDS))
1608 			continue;
1609 		/* Skip PCIe ALT module (convert byte size to word count) */
1610 		if (i >= (u32)pcie_alt_module &&
1611 		    i < ((u32)pcie_alt_module + ICE_SR_PCIE_ALT_SIZE_WORDS))
1612 			continue;
1613 
1614 		checksum_local += data[i % ICE_SR_SECTOR_SIZE_IN_WORDS];
1615 	}
1616 
1617 	*checksum = (u16)ICE_SR_SW_CHECKSUM_BASE - checksum_local;
1618 
1619 ice_calc_sr_checksum_exit:
1620 	ice_free(hw, vmem);
1621 	return status;
1622 }
1623 
1624 /**
1625  * ice_update_sr_checksum - Updates the Shadow RAM SW checksum
1626  * @hw: pointer to hardware structure
1627  *
1628  * NVM ownership must be acquired before calling this function and released
1629  * on ARQ completion event reception by caller.
1630  * This function will commit SR to NVM.
1631  */
1632 enum ice_status ice_update_sr_checksum(struct ice_hw *hw)
1633 {
1634 	enum ice_status status;
1635 	__le16 le_sum;
1636 	u16 checksum;
1637 
1638 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1639 
1640 	status = ice_calc_sr_checksum(hw, &checksum);
1641 	if (!status) {
1642 		le_sum = CPU_TO_LE16(checksum);
1643 		status = ice_write_sr_aq(hw, ICE_SR_SW_CHECKSUM_WORD, 1,
1644 					 &le_sum, true);
1645 	}
1646 	return status;
1647 }
1648 
1649 /**
1650  * ice_validate_sr_checksum - Validate Shadow RAM SW checksum
1651  * @hw: pointer to hardware structure
1652  * @checksum: calculated checksum
1653  *
1654  * Performs checksum calculation and validates the Shadow RAM SW checksum.
1655  * If the caller does not need checksum, the value can be NULL.
1656  */
1657 enum ice_status ice_validate_sr_checksum(struct ice_hw *hw, u16 *checksum)
1658 {
1659 	enum ice_status status;
1660 	u16 checksum_local;
1661 	u16 checksum_sr;
1662 
1663 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1664 
1665 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1666 	if (!status) {
1667 		status = ice_calc_sr_checksum(hw, &checksum_local);
1668 		ice_release_nvm(hw);
1669 		if (status)
1670 			return status;
1671 	} else {
1672 		return status;
1673 	}
1674 
1675 	ice_read_sr_word(hw, ICE_SR_SW_CHECKSUM_WORD, &checksum_sr);
1676 
1677 	/* Verify read checksum from EEPROM is the same as
1678 	 * calculated checksum
1679 	 */
1680 	if (checksum_local != checksum_sr)
1681 		status = ICE_ERR_NVM_CHECKSUM;
1682 
1683 	/* If the user cares, return the calculated checksum */
1684 	if (checksum)
1685 		*checksum = checksum_local;
1686 
1687 	return status;
1688 }
1689 
1690 /**
1691  * ice_nvm_validate_checksum
1692  * @hw: pointer to the HW struct
1693  *
1694  * Verify NVM PFA checksum validity (0x0706)
1695  */
1696 enum ice_status ice_nvm_validate_checksum(struct ice_hw *hw)
1697 {
1698 	struct ice_aqc_nvm_checksum *cmd;
1699 	struct ice_aq_desc desc;
1700 	enum ice_status status;
1701 
1702 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1703 	if (status)
1704 		return status;
1705 
1706 	cmd = &desc.params.nvm_checksum;
1707 
1708 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_checksum);
1709 	cmd->flags = ICE_AQC_NVM_CHECKSUM_VERIFY;
1710 
1711 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1712 
1713 	ice_release_nvm(hw);
1714 
1715 	if (!status)
1716 		if (LE16_TO_CPU(cmd->checksum) != ICE_AQC_NVM_CHECKSUM_CORRECT)
1717 			status = ICE_ERR_NVM_CHECKSUM;
1718 
1719 	return status;
1720 }
1721 
1722 /**
1723  * ice_nvm_recalculate_checksum
1724  * @hw: pointer to the HW struct
1725  *
1726  * Recalculate NVM PFA checksum (0x0706)
1727  */
1728 enum ice_status ice_nvm_recalculate_checksum(struct ice_hw *hw)
1729 {
1730 	struct ice_aqc_nvm_checksum *cmd;
1731 	struct ice_aq_desc desc;
1732 	enum ice_status status;
1733 
1734 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1735 	if (status)
1736 		return status;
1737 
1738 	cmd = &desc.params.nvm_checksum;
1739 
1740 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_checksum);
1741 	cmd->flags = ICE_AQC_NVM_CHECKSUM_RECALC;
1742 
1743 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1744 
1745 	ice_release_nvm(hw);
1746 
1747 	return status;
1748 }
1749 
1750 /**
1751  * ice_nvm_write_activate
1752  * @hw: pointer to the HW struct
1753  * @cmd_flags: flags for write activate command
1754  * @response_flags: response indicators from firmware
1755  *
1756  * Update the control word with the required banks' validity bits
1757  * and dumps the Shadow RAM to flash (0x0707)
1758  *
1759  * cmd_flags controls which banks to activate, the preservation level to use
1760  * when activating the NVM bank, and whether an EMP reset is required for
1761  * activation.
1762  *
1763  * Note that the 16bit cmd_flags value is split between two separate 1 byte
1764  * flag values in the descriptor.
1765  *
1766  * On successful return of the firmware command, the response_flags variable
1767  * is updated with the flags reported by firmware indicating certain status,
1768  * such as whether EMP reset is enabled.
1769  */
1770 enum ice_status
1771 ice_nvm_write_activate(struct ice_hw *hw, u16 cmd_flags, u8 *response_flags)
1772 {
1773 	struct ice_aqc_nvm *cmd;
1774 	struct ice_aq_desc desc;
1775 	enum ice_status status;
1776 
1777 	cmd = &desc.params.nvm;
1778 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_write_activate);
1779 
1780 	cmd->cmd_flags = ICE_LO_BYTE(cmd_flags);
1781 	cmd->offset_high = ICE_HI_BYTE(cmd_flags);
1782 
1783 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1784 	if (!status && response_flags)
1785 		*response_flags = cmd->cmd_flags;
1786 
1787 	return status;
1788 }
1789 
1790 /**
1791  * ice_get_nvm_minsrevs - Get the Minimum Security Revision values from flash
1792  * @hw: pointer to the HW struct
1793  * @minsrevs: structure to store NVM and OROM minsrev values
1794  *
1795  * Read the Minimum Security Revision TLV and extract the revision values from
1796  * the flash image into a readable structure for processing.
1797  */
1798 enum ice_status
1799 ice_get_nvm_minsrevs(struct ice_hw *hw, struct ice_minsrev_info *minsrevs)
1800 {
1801 	struct ice_aqc_nvm_minsrev data;
1802 	enum ice_status status;
1803 	u16 valid;
1804 
1805 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1806 
1807 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1808 	if (status)
1809 		return status;
1810 
1811 	status = ice_aq_read_nvm(hw, ICE_AQC_NVM_MINSREV_MOD_ID, 0, sizeof(data),
1812 				 &data, true, false, NULL);
1813 
1814 	ice_release_nvm(hw);
1815 
1816 	if (status)
1817 		return status;
1818 
1819 	valid = LE16_TO_CPU(data.validity);
1820 
1821 	/* Extract NVM minimum security revision */
1822 	if (valid & ICE_AQC_NVM_MINSREV_NVM_VALID) {
1823 		u16 minsrev_l, minsrev_h;
1824 
1825 		minsrev_l = LE16_TO_CPU(data.nvm_minsrev_l);
1826 		minsrev_h = LE16_TO_CPU(data.nvm_minsrev_h);
1827 
1828 		minsrevs->nvm = minsrev_h << 16 | minsrev_l;
1829 		minsrevs->nvm_valid = true;
1830 	}
1831 
1832 	/* Extract the OROM minimum security revision */
1833 	if (valid & ICE_AQC_NVM_MINSREV_OROM_VALID) {
1834 		u16 minsrev_l, minsrev_h;
1835 
1836 		minsrev_l = LE16_TO_CPU(data.orom_minsrev_l);
1837 		minsrev_h = LE16_TO_CPU(data.orom_minsrev_h);
1838 
1839 		minsrevs->orom = minsrev_h << 16 | minsrev_l;
1840 		minsrevs->orom_valid = true;
1841 	}
1842 
1843 	return ICE_SUCCESS;
1844 }
1845 
1846 /**
1847  * ice_update_nvm_minsrevs - Update minimum security revision TLV data in flash
1848  * @hw: pointer to the HW struct
1849  * @minsrevs: minimum security revision information
1850  *
1851  * Update the NVM or Option ROM minimum security revision fields in the PFA
1852  * area of the flash. Reads the minsrevs->nvm_valid and minsrevs->orom_valid
1853  * fields to determine what update is being requested. If the valid bit is not
1854  * set for that module, then the associated minsrev will be left as is.
1855  */
1856 enum ice_status
1857 ice_update_nvm_minsrevs(struct ice_hw *hw, struct ice_minsrev_info *minsrevs)
1858 {
1859 	struct ice_aqc_nvm_minsrev data;
1860 	enum ice_status status;
1861 
1862 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1863 
1864 	if (!minsrevs->nvm_valid && !minsrevs->orom_valid) {
1865 		ice_debug(hw, ICE_DBG_NVM, "At least one of NVM and OROM MinSrev must be valid");
1866 		return ICE_ERR_PARAM;
1867 	}
1868 
1869 	status = ice_acquire_nvm(hw, ICE_RES_WRITE);
1870 	if (status)
1871 		return status;
1872 
1873 	/* Get current data */
1874 	status = ice_aq_read_nvm(hw, ICE_AQC_NVM_MINSREV_MOD_ID, 0, sizeof(data),
1875 				 &data, true, false, NULL);
1876 	if (status)
1877 		goto exit_release_res;
1878 
1879 	if (minsrevs->nvm_valid) {
1880 		data.nvm_minsrev_l = CPU_TO_LE16(minsrevs->nvm & 0xFFFF);
1881 		data.nvm_minsrev_h = CPU_TO_LE16(minsrevs->nvm >> 16);
1882 		data.validity |= CPU_TO_LE16(ICE_AQC_NVM_MINSREV_NVM_VALID);
1883 	}
1884 
1885 	if (minsrevs->orom_valid) {
1886 		data.orom_minsrev_l = CPU_TO_LE16(minsrevs->orom & 0xFFFF);
1887 		data.orom_minsrev_h = CPU_TO_LE16(minsrevs->orom >> 16);
1888 		data.validity |= CPU_TO_LE16(ICE_AQC_NVM_MINSREV_OROM_VALID);
1889 	}
1890 
1891 	/* Update flash data */
1892 	status = ice_aq_update_nvm(hw, ICE_AQC_NVM_MINSREV_MOD_ID, 0, sizeof(data), &data,
1893 				   false, ICE_AQC_NVM_SPECIAL_UPDATE, NULL);
1894 	if (status)
1895 		goto exit_release_res;
1896 
1897 	/* Dump the Shadow RAM to the flash */
1898 	status = ice_nvm_write_activate(hw, 0, NULL);
1899 
1900 exit_release_res:
1901 	ice_release_nvm(hw);
1902 
1903 	return status;
1904 }
1905 
1906 /**
1907  * ice_nvm_access_get_features - Return the NVM access features structure
1908  * @cmd: NVM access command to process
1909  * @data: storage for the driver NVM features
1910  *
1911  * Fill in the data section of the NVM access request with a copy of the NVM
1912  * features structure.
1913  */
1914 enum ice_status
1915 ice_nvm_access_get_features(struct ice_nvm_access_cmd *cmd,
1916 			    union ice_nvm_access_data *data)
1917 {
1918 	/* The provided data_size must be at least as large as our NVM
1919 	 * features structure. A larger size should not be treated as an
1920 	 * error, to allow future extensions to the features structure to
1921 	 * work on older drivers.
1922 	 */
1923 	if (cmd->data_size < sizeof(struct ice_nvm_features))
1924 		return ICE_ERR_NO_MEMORY;
1925 
1926 	/* Initialize the data buffer to zeros */
1927 	ice_memset(data, 0, cmd->data_size, ICE_NONDMA_MEM);
1928 
1929 	/* Fill in the features data */
1930 	data->drv_features.major = ICE_NVM_ACCESS_MAJOR_VER;
1931 	data->drv_features.minor = ICE_NVM_ACCESS_MINOR_VER;
1932 	data->drv_features.size = sizeof(struct ice_nvm_features);
1933 	data->drv_features.features[0] = ICE_NVM_FEATURES_0_REG_ACCESS;
1934 
1935 	return ICE_SUCCESS;
1936 }
1937 
1938 /**
1939  * ice_nvm_access_get_module - Helper function to read module value
1940  * @cmd: NVM access command structure
1941  *
1942  * Reads the module value out of the NVM access config field.
1943  */
1944 u32 ice_nvm_access_get_module(struct ice_nvm_access_cmd *cmd)
1945 {
1946 	return ((cmd->config & ICE_NVM_CFG_MODULE_M) >> ICE_NVM_CFG_MODULE_S);
1947 }
1948 
1949 /**
1950  * ice_nvm_access_get_flags - Helper function to read flags value
1951  * @cmd: NVM access command structure
1952  *
1953  * Reads the flags value out of the NVM access config field.
1954  */
1955 u32 ice_nvm_access_get_flags(struct ice_nvm_access_cmd *cmd)
1956 {
1957 	return ((cmd->config & ICE_NVM_CFG_FLAGS_M) >> ICE_NVM_CFG_FLAGS_S);
1958 }
1959 
1960 /**
1961  * ice_nvm_access_get_adapter - Helper function to read adapter info
1962  * @cmd: NVM access command structure
1963  *
1964  * Read the adapter info value out of the NVM access config field.
1965  */
1966 u32 ice_nvm_access_get_adapter(struct ice_nvm_access_cmd *cmd)
1967 {
1968 	return ((cmd->config & ICE_NVM_CFG_ADAPTER_INFO_M) >>
1969 		ICE_NVM_CFG_ADAPTER_INFO_S);
1970 }
1971 
1972 /**
1973  * ice_validate_nvm_rw_reg - Check than an NVM access request is valid
1974  * @cmd: NVM access command structure
1975  *
1976  * Validates that an NVM access structure is request to read or write a valid
1977  * register offset. First validates that the module and flags are correct, and
1978  * then ensures that the register offset is one of the accepted registers.
1979  */
1980 static enum ice_status
1981 ice_validate_nvm_rw_reg(struct ice_nvm_access_cmd *cmd)
1982 {
1983 	u32 module, flags, offset;
1984 	u16 i;
1985 
1986 	module = ice_nvm_access_get_module(cmd);
1987 	flags = ice_nvm_access_get_flags(cmd);
1988 	offset = cmd->offset;
1989 
1990 	/* Make sure the module and flags indicate a read/write request */
1991 	if (module != ICE_NVM_REG_RW_MODULE ||
1992 	    flags != ICE_NVM_REG_RW_FLAGS ||
1993 	    cmd->data_size != FIELD_SIZEOF(union ice_nvm_access_data, regval))
1994 		return ICE_ERR_PARAM;
1995 
1996 	switch (offset) {
1997 	case GL_HICR:
1998 	case GL_HICR_EN: /* Note, this register is read only */
1999 	case GL_FWSTS:
2000 	case GL_MNG_FWSM:
2001 	case GLGEN_CSR_DEBUG_C:
2002 	case GLGEN_RSTAT:
2003 	case GLPCI_LBARCTRL:
2004 	case GL_MNG_DEF_DEVID:
2005 	case GLNVM_GENS:
2006 	case GLNVM_FLA:
2007 	case PF_FUNC_RID:
2008 		return ICE_SUCCESS;
2009 	default:
2010 		break;
2011 	}
2012 
2013 	for (i = 0; i <= GL_HIDA_MAX_INDEX; i++)
2014 		if (offset == (u32)GL_HIDA(i))
2015 			return ICE_SUCCESS;
2016 
2017 	for (i = 0; i <= GL_HIBA_MAX_INDEX; i++)
2018 		if (offset == (u32)GL_HIBA(i))
2019 			return ICE_SUCCESS;
2020 
2021 	/* All other register offsets are not valid */
2022 	return ICE_ERR_OUT_OF_RANGE;
2023 }
2024 
2025 /**
2026  * ice_nvm_access_read - Handle an NVM read request
2027  * @hw: pointer to the HW struct
2028  * @cmd: NVM access command to process
2029  * @data: storage for the register value read
2030  *
2031  * Process an NVM access request to read a register.
2032  */
2033 enum ice_status
2034 ice_nvm_access_read(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
2035 		    union ice_nvm_access_data *data)
2036 {
2037 	enum ice_status status;
2038 
2039 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
2040 
2041 	/* Always initialize the output data, even on failure */
2042 	ice_memset(data, 0, cmd->data_size, ICE_NONDMA_MEM);
2043 
2044 	/* Make sure this is a valid read/write access request */
2045 	status = ice_validate_nvm_rw_reg(cmd);
2046 	if (status)
2047 		return status;
2048 
2049 	ice_debug(hw, ICE_DBG_NVM, "NVM access: reading register %08x\n",
2050 		  cmd->offset);
2051 
2052 	/* Read the register and store the contents in the data field */
2053 	data->regval = rd32(hw, cmd->offset);
2054 
2055 	return ICE_SUCCESS;
2056 }
2057 
2058 /**
2059  * ice_nvm_access_write - Handle an NVM write request
2060  * @hw: pointer to the HW struct
2061  * @cmd: NVM access command to process
2062  * @data: NVM access data to write
2063  *
2064  * Process an NVM access request to write a register.
2065  */
2066 enum ice_status
2067 ice_nvm_access_write(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
2068 		     union ice_nvm_access_data *data)
2069 {
2070 	enum ice_status status;
2071 
2072 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
2073 
2074 	/* Make sure this is a valid read/write access request */
2075 	status = ice_validate_nvm_rw_reg(cmd);
2076 	if (status)
2077 		return status;
2078 
2079 	/* Reject requests to write to read-only registers */
2080 	switch (cmd->offset) {
2081 	case GL_HICR_EN:
2082 	case GLGEN_RSTAT:
2083 		return ICE_ERR_OUT_OF_RANGE;
2084 	default:
2085 		break;
2086 	}
2087 
2088 	ice_debug(hw, ICE_DBG_NVM, "NVM access: writing register %08x with value %08x\n",
2089 		  cmd->offset, data->regval);
2090 
2091 	/* Write the data field to the specified register */
2092 	wr32(hw, cmd->offset, data->regval);
2093 
2094 	return ICE_SUCCESS;
2095 }
2096 
2097 /**
2098  * ice_handle_nvm_access - Handle an NVM access request
2099  * @hw: pointer to the HW struct
2100  * @cmd: NVM access command info
2101  * @data: pointer to read or return data
2102  *
2103  * Process an NVM access request. Read the command structure information and
2104  * determine if it is valid. If not, report an error indicating the command
2105  * was invalid.
2106  *
2107  * For valid commands, perform the necessary function, copying the data into
2108  * the provided data buffer.
2109  */
2110 enum ice_status
2111 ice_handle_nvm_access(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
2112 		      union ice_nvm_access_data *data)
2113 {
2114 	u32 module, flags, adapter_info;
2115 
2116 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
2117 
2118 	/* Extended flags are currently reserved and must be zero */
2119 	if ((cmd->config & ICE_NVM_CFG_EXT_FLAGS_M) != 0)
2120 		return ICE_ERR_PARAM;
2121 
2122 	/* Adapter info must match the HW device ID */
2123 	adapter_info = ice_nvm_access_get_adapter(cmd);
2124 	if (adapter_info != hw->device_id)
2125 		return ICE_ERR_PARAM;
2126 
2127 	switch (cmd->command) {
2128 	case ICE_NVM_CMD_READ:
2129 		module = ice_nvm_access_get_module(cmd);
2130 		flags = ice_nvm_access_get_flags(cmd);
2131 
2132 		/* Getting the driver's NVM features structure shares the same
2133 		 * command type as reading a register. Read the config field
2134 		 * to determine if this is a request to get features.
2135 		 */
2136 		if (module == ICE_NVM_GET_FEATURES_MODULE &&
2137 		    flags == ICE_NVM_GET_FEATURES_FLAGS &&
2138 		    cmd->offset == 0)
2139 			return ice_nvm_access_get_features(cmd, data);
2140 		else
2141 			return ice_nvm_access_read(hw, cmd, data);
2142 	case ICE_NVM_CMD_WRITE:
2143 		return ice_nvm_access_write(hw, cmd, data);
2144 	default:
2145 		return ICE_ERR_PARAM;
2146 	}
2147 }
2148 
2149