xref: /freebsd/sys/dev/ice/ice_nvm.c (revision d9a42747950146bf03cda7f6e25d219253f8a57a)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*  Copyright (c) 2022, 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 	struct ice_orom_civd_info tmp;
986 	u32 offset;
987 
988 	/* The CIVD section is located in the Option ROM aligned to 512 bytes.
989 	 * The first 4 bytes must contain the ASCII characters "$CIV".
990 	 * A simple modulo 256 sum of all of the bytes of the structure must
991 	 * equal 0.
992 	 */
993 	for (offset = 0; (offset + 512) <= hw->flash.banks.orom_size; offset += 512) {
994 		enum ice_status status;
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: flags for write activate command
1730  * @response_flags: response indicators from firmware
1731  *
1732  * Update the control word with the required banks' validity bits
1733  * and dumps the Shadow RAM to flash (0x0707)
1734  *
1735  * cmd_flags controls which banks to activate, the preservation level to use
1736  * when activating the NVM bank, and whether an EMP reset is required for
1737  * activation.
1738  *
1739  * Note that the 16bit cmd_flags value is split between two separate 1 byte
1740  * flag values in the descriptor.
1741  *
1742  * On successful return of the firmware command, the response_flags variable
1743  * is updated with the flags reported by firmware indicating certain status,
1744  * such as whether EMP reset is enabled.
1745  */
1746 enum ice_status
1747 ice_nvm_write_activate(struct ice_hw *hw, u16 cmd_flags, u8 *response_flags)
1748 {
1749 	struct ice_aqc_nvm *cmd;
1750 	struct ice_aq_desc desc;
1751 	enum ice_status status;
1752 
1753 	cmd = &desc.params.nvm;
1754 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_write_activate);
1755 
1756 	cmd->cmd_flags = ICE_LO_BYTE(cmd_flags);
1757 	cmd->offset_high = ICE_HI_BYTE(cmd_flags);
1758 
1759 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1760 	if (!status && response_flags)
1761 		*response_flags = cmd->cmd_flags;
1762 
1763 	return status;
1764 }
1765 
1766 /**
1767  * ice_get_nvm_minsrevs - Get the Minimum Security Revision values from flash
1768  * @hw: pointer to the HW struct
1769  * @minsrevs: structure to store NVM and OROM minsrev values
1770  *
1771  * Read the Minimum Security Revision TLV and extract the revision values from
1772  * the flash image into a readable structure for processing.
1773  */
1774 enum ice_status
1775 ice_get_nvm_minsrevs(struct ice_hw *hw, struct ice_minsrev_info *minsrevs)
1776 {
1777 	struct ice_aqc_nvm_minsrev data;
1778 	enum ice_status status;
1779 	u16 valid;
1780 
1781 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1782 
1783 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1784 	if (status)
1785 		return status;
1786 
1787 	status = ice_aq_read_nvm(hw, ICE_AQC_NVM_MINSREV_MOD_ID, 0, sizeof(data),
1788 				 &data, true, false, NULL);
1789 
1790 	ice_release_nvm(hw);
1791 
1792 	if (status)
1793 		return status;
1794 
1795 	valid = LE16_TO_CPU(data.validity);
1796 
1797 	/* Extract NVM minimum security revision */
1798 	if (valid & ICE_AQC_NVM_MINSREV_NVM_VALID) {
1799 		u16 minsrev_l, minsrev_h;
1800 
1801 		minsrev_l = LE16_TO_CPU(data.nvm_minsrev_l);
1802 		minsrev_h = LE16_TO_CPU(data.nvm_minsrev_h);
1803 
1804 		minsrevs->nvm = minsrev_h << 16 | minsrev_l;
1805 		minsrevs->nvm_valid = true;
1806 	}
1807 
1808 	/* Extract the OROM minimum security revision */
1809 	if (valid & ICE_AQC_NVM_MINSREV_OROM_VALID) {
1810 		u16 minsrev_l, minsrev_h;
1811 
1812 		minsrev_l = LE16_TO_CPU(data.orom_minsrev_l);
1813 		minsrev_h = LE16_TO_CPU(data.orom_minsrev_h);
1814 
1815 		minsrevs->orom = minsrev_h << 16 | minsrev_l;
1816 		minsrevs->orom_valid = true;
1817 	}
1818 
1819 	return ICE_SUCCESS;
1820 }
1821 
1822 /**
1823  * ice_update_nvm_minsrevs - Update minimum security revision TLV data in flash
1824  * @hw: pointer to the HW struct
1825  * @minsrevs: minimum security revision information
1826  *
1827  * Update the NVM or Option ROM minimum security revision fields in the PFA
1828  * area of the flash. Reads the minsrevs->nvm_valid and minsrevs->orom_valid
1829  * fields to determine what update is being requested. If the valid bit is not
1830  * set for that module, then the associated minsrev will be left as is.
1831  */
1832 enum ice_status
1833 ice_update_nvm_minsrevs(struct ice_hw *hw, struct ice_minsrev_info *minsrevs)
1834 {
1835 	struct ice_aqc_nvm_minsrev data;
1836 	enum ice_status status;
1837 
1838 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1839 
1840 	if (!minsrevs->nvm_valid && !minsrevs->orom_valid) {
1841 		ice_debug(hw, ICE_DBG_NVM, "At least one of NVM and OROM MinSrev must be valid");
1842 		return ICE_ERR_PARAM;
1843 	}
1844 
1845 	status = ice_acquire_nvm(hw, ICE_RES_WRITE);
1846 	if (status)
1847 		return status;
1848 
1849 	/* Get current data */
1850 	status = ice_aq_read_nvm(hw, ICE_AQC_NVM_MINSREV_MOD_ID, 0, sizeof(data),
1851 				 &data, true, false, NULL);
1852 	if (status)
1853 		goto exit_release_res;
1854 
1855 	if (minsrevs->nvm_valid) {
1856 		data.nvm_minsrev_l = CPU_TO_LE16(minsrevs->nvm & 0xFFFF);
1857 		data.nvm_minsrev_h = CPU_TO_LE16(minsrevs->nvm >> 16);
1858 		data.validity |= CPU_TO_LE16(ICE_AQC_NVM_MINSREV_NVM_VALID);
1859 	}
1860 
1861 	if (minsrevs->orom_valid) {
1862 		data.orom_minsrev_l = CPU_TO_LE16(minsrevs->orom & 0xFFFF);
1863 		data.orom_minsrev_h = CPU_TO_LE16(minsrevs->orom >> 16);
1864 		data.validity |= CPU_TO_LE16(ICE_AQC_NVM_MINSREV_OROM_VALID);
1865 	}
1866 
1867 	/* Update flash data */
1868 	status = ice_aq_update_nvm(hw, ICE_AQC_NVM_MINSREV_MOD_ID, 0, sizeof(data), &data,
1869 				   false, ICE_AQC_NVM_SPECIAL_UPDATE, NULL);
1870 	if (status)
1871 		goto exit_release_res;
1872 
1873 	/* Dump the Shadow RAM to the flash */
1874 	status = ice_nvm_write_activate(hw, 0, NULL);
1875 
1876 exit_release_res:
1877 	ice_release_nvm(hw);
1878 
1879 	return status;
1880 }
1881 
1882 /**
1883  * ice_nvm_access_get_features - Return the NVM access features structure
1884  * @cmd: NVM access command to process
1885  * @data: storage for the driver NVM features
1886  *
1887  * Fill in the data section of the NVM access request with a copy of the NVM
1888  * features structure.
1889  */
1890 enum ice_status
1891 ice_nvm_access_get_features(struct ice_nvm_access_cmd *cmd,
1892 			    union ice_nvm_access_data *data)
1893 {
1894 	/* The provided data_size must be at least as large as our NVM
1895 	 * features structure. A larger size should not be treated as an
1896 	 * error, to allow future extensions to the features structure to
1897 	 * work on older drivers.
1898 	 */
1899 	if (cmd->data_size < sizeof(struct ice_nvm_features))
1900 		return ICE_ERR_NO_MEMORY;
1901 
1902 	/* Initialize the data buffer to zeros */
1903 	ice_memset(data, 0, cmd->data_size, ICE_NONDMA_MEM);
1904 
1905 	/* Fill in the features data */
1906 	data->drv_features.major = ICE_NVM_ACCESS_MAJOR_VER;
1907 	data->drv_features.minor = ICE_NVM_ACCESS_MINOR_VER;
1908 	data->drv_features.size = sizeof(struct ice_nvm_features);
1909 	data->drv_features.features[0] = ICE_NVM_FEATURES_0_REG_ACCESS;
1910 
1911 	return ICE_SUCCESS;
1912 }
1913 
1914 /**
1915  * ice_nvm_access_get_module - Helper function to read module value
1916  * @cmd: NVM access command structure
1917  *
1918  * Reads the module value out of the NVM access config field.
1919  */
1920 u32 ice_nvm_access_get_module(struct ice_nvm_access_cmd *cmd)
1921 {
1922 	return ((cmd->config & ICE_NVM_CFG_MODULE_M) >> ICE_NVM_CFG_MODULE_S);
1923 }
1924 
1925 /**
1926  * ice_nvm_access_get_flags - Helper function to read flags value
1927  * @cmd: NVM access command structure
1928  *
1929  * Reads the flags value out of the NVM access config field.
1930  */
1931 u32 ice_nvm_access_get_flags(struct ice_nvm_access_cmd *cmd)
1932 {
1933 	return ((cmd->config & ICE_NVM_CFG_FLAGS_M) >> ICE_NVM_CFG_FLAGS_S);
1934 }
1935 
1936 /**
1937  * ice_nvm_access_get_adapter - Helper function to read adapter info
1938  * @cmd: NVM access command structure
1939  *
1940  * Read the adapter info value out of the NVM access config field.
1941  */
1942 u32 ice_nvm_access_get_adapter(struct ice_nvm_access_cmd *cmd)
1943 {
1944 	return ((cmd->config & ICE_NVM_CFG_ADAPTER_INFO_M) >>
1945 		ICE_NVM_CFG_ADAPTER_INFO_S);
1946 }
1947 
1948 /**
1949  * ice_validate_nvm_rw_reg - Check than an NVM access request is valid
1950  * @cmd: NVM access command structure
1951  *
1952  * Validates that an NVM access structure is request to read or write a valid
1953  * register offset. First validates that the module and flags are correct, and
1954  * then ensures that the register offset is one of the accepted registers.
1955  */
1956 static enum ice_status
1957 ice_validate_nvm_rw_reg(struct ice_nvm_access_cmd *cmd)
1958 {
1959 	u32 module, flags, offset;
1960 	u16 i;
1961 
1962 	module = ice_nvm_access_get_module(cmd);
1963 	flags = ice_nvm_access_get_flags(cmd);
1964 	offset = cmd->offset;
1965 
1966 	/* Make sure the module and flags indicate a read/write request */
1967 	if (module != ICE_NVM_REG_RW_MODULE ||
1968 	    flags != ICE_NVM_REG_RW_FLAGS ||
1969 	    cmd->data_size != FIELD_SIZEOF(union ice_nvm_access_data, regval))
1970 		return ICE_ERR_PARAM;
1971 
1972 	switch (offset) {
1973 	case GL_HICR:
1974 	case GL_HICR_EN: /* Note, this register is read only */
1975 	case GL_FWSTS:
1976 	case GL_MNG_FWSM:
1977 	case GLGEN_CSR_DEBUG_C:
1978 	case GLGEN_RSTAT:
1979 	case GLPCI_LBARCTRL:
1980 	case GL_MNG_DEF_DEVID:
1981 	case GLNVM_GENS:
1982 	case GLNVM_FLA:
1983 	case PF_FUNC_RID:
1984 		return ICE_SUCCESS;
1985 	default:
1986 		break;
1987 	}
1988 
1989 	for (i = 0; i <= GL_HIDA_MAX_INDEX; i++)
1990 		if (offset == (u32)GL_HIDA(i))
1991 			return ICE_SUCCESS;
1992 
1993 	for (i = 0; i <= GL_HIBA_MAX_INDEX; i++)
1994 		if (offset == (u32)GL_HIBA(i))
1995 			return ICE_SUCCESS;
1996 
1997 	/* All other register offsets are not valid */
1998 	return ICE_ERR_OUT_OF_RANGE;
1999 }
2000 
2001 /**
2002  * ice_nvm_access_read - Handle an NVM read request
2003  * @hw: pointer to the HW struct
2004  * @cmd: NVM access command to process
2005  * @data: storage for the register value read
2006  *
2007  * Process an NVM access request to read a register.
2008  */
2009 enum ice_status
2010 ice_nvm_access_read(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
2011 		    union ice_nvm_access_data *data)
2012 {
2013 	enum ice_status status;
2014 
2015 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
2016 
2017 	/* Always initialize the output data, even on failure */
2018 	ice_memset(data, 0, cmd->data_size, ICE_NONDMA_MEM);
2019 
2020 	/* Make sure this is a valid read/write access request */
2021 	status = ice_validate_nvm_rw_reg(cmd);
2022 	if (status)
2023 		return status;
2024 
2025 	ice_debug(hw, ICE_DBG_NVM, "NVM access: reading register %08x\n",
2026 		  cmd->offset);
2027 
2028 	/* Read the register and store the contents in the data field */
2029 	data->regval = rd32(hw, cmd->offset);
2030 
2031 	return ICE_SUCCESS;
2032 }
2033 
2034 /**
2035  * ice_nvm_access_write - Handle an NVM write request
2036  * @hw: pointer to the HW struct
2037  * @cmd: NVM access command to process
2038  * @data: NVM access data to write
2039  *
2040  * Process an NVM access request to write a register.
2041  */
2042 enum ice_status
2043 ice_nvm_access_write(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
2044 		     union ice_nvm_access_data *data)
2045 {
2046 	enum ice_status status;
2047 
2048 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
2049 
2050 	/* Make sure this is a valid read/write access request */
2051 	status = ice_validate_nvm_rw_reg(cmd);
2052 	if (status)
2053 		return status;
2054 
2055 	/* Reject requests to write to read-only registers */
2056 	switch (cmd->offset) {
2057 	case GL_HICR_EN:
2058 	case GLGEN_RSTAT:
2059 		return ICE_ERR_OUT_OF_RANGE;
2060 	default:
2061 		break;
2062 	}
2063 
2064 	ice_debug(hw, ICE_DBG_NVM, "NVM access: writing register %08x with value %08x\n",
2065 		  cmd->offset, data->regval);
2066 
2067 	/* Write the data field to the specified register */
2068 	wr32(hw, cmd->offset, data->regval);
2069 
2070 	return ICE_SUCCESS;
2071 }
2072 
2073 /**
2074  * ice_handle_nvm_access - Handle an NVM access request
2075  * @hw: pointer to the HW struct
2076  * @cmd: NVM access command info
2077  * @data: pointer to read or return data
2078  *
2079  * Process an NVM access request. Read the command structure information and
2080  * determine if it is valid. If not, report an error indicating the command
2081  * was invalid.
2082  *
2083  * For valid commands, perform the necessary function, copying the data into
2084  * the provided data buffer.
2085  */
2086 enum ice_status
2087 ice_handle_nvm_access(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
2088 		      union ice_nvm_access_data *data)
2089 {
2090 	u32 module, flags, adapter_info;
2091 
2092 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
2093 
2094 	/* Extended flags are currently reserved and must be zero */
2095 	if ((cmd->config & ICE_NVM_CFG_EXT_FLAGS_M) != 0)
2096 		return ICE_ERR_PARAM;
2097 
2098 	/* Adapter info must match the HW device ID */
2099 	adapter_info = ice_nvm_access_get_adapter(cmd);
2100 	if (adapter_info != hw->device_id)
2101 		return ICE_ERR_PARAM;
2102 
2103 	switch (cmd->command) {
2104 	case ICE_NVM_CMD_READ:
2105 		module = ice_nvm_access_get_module(cmd);
2106 		flags = ice_nvm_access_get_flags(cmd);
2107 
2108 		/* Getting the driver's NVM features structure shares the same
2109 		 * command type as reading a register. Read the config field
2110 		 * to determine if this is a request to get features.
2111 		 */
2112 		if (module == ICE_NVM_GET_FEATURES_MODULE &&
2113 		    flags == ICE_NVM_GET_FEATURES_FLAGS &&
2114 		    cmd->offset == 0)
2115 			return ice_nvm_access_get_features(cmd, data);
2116 		else
2117 			return ice_nvm_access_read(hw, cmd, data);
2118 	case ICE_NVM_CMD_WRITE:
2119 		return ice_nvm_access_write(hw, cmd, data);
2120 	default:
2121 		return ICE_ERR_PARAM;
2122 	}
2123 }
2124 
2125