xref: /dpdk/drivers/net/ice/base/ice_nvm.c (revision 4e2cf9af)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2001-2020 Intel Corporation
3  */
4 
5 #include "ice_common.h"
6 
7 /**
8  * ice_aq_read_nvm
9  * @hw: pointer to the HW struct
10  * @module_typeid: module pointer location in words from the NVM beginning
11  * @offset: byte offset from the module beginning
12  * @length: length of the section to be read (in bytes from the offset)
13  * @data: command buffer (size [bytes] = length)
14  * @last_command: tells if this is the last command in a series
15  * @read_shadow_ram: tell if this is a shadow RAM read
16  * @cd: pointer to command details structure or NULL
17  *
18  * Read the NVM using the admin queue commands (0x0701)
19  */
20 enum ice_status
21 ice_aq_read_nvm(struct ice_hw *hw, u16 module_typeid, u32 offset, u16 length,
22 		void *data, bool last_command, bool read_shadow_ram,
23 		struct ice_sq_cd *cd)
24 {
25 	struct ice_aq_desc desc;
26 	struct ice_aqc_nvm *cmd;
27 
28 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
29 
30 	cmd = &desc.params.nvm;
31 
32 	if (offset > ICE_AQC_NVM_MAX_OFFSET)
33 		return ICE_ERR_PARAM;
34 
35 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_read);
36 
37 	if (!read_shadow_ram && module_typeid == ICE_AQC_NVM_START_POINT)
38 		cmd->cmd_flags |= ICE_AQC_NVM_FLASH_ONLY;
39 
40 	/* If this is the last command in a series, set the proper flag. */
41 	if (last_command)
42 		cmd->cmd_flags |= ICE_AQC_NVM_LAST_CMD;
43 	cmd->module_typeid = CPU_TO_LE16(module_typeid);
44 	cmd->offset_low = CPU_TO_LE16(offset & 0xFFFF);
45 	cmd->offset_high = (offset >> 16) & 0xFF;
46 	cmd->length = CPU_TO_LE16(length);
47 
48 	return ice_aq_send_cmd(hw, &desc, data, length, cd);
49 }
50 
51 /**
52  * ice_read_flat_nvm - Read portion of NVM by flat offset
53  * @hw: pointer to the HW struct
54  * @offset: offset from beginning of NVM
55  * @length: (in) number of bytes to read; (out) number of bytes actually read
56  * @data: buffer to return data in (sized to fit the specified length)
57  * @read_shadow_ram: if true, read from shadow RAM instead of NVM
58  *
59  * Reads a portion of the NVM, as a flat memory space. This function correctly
60  * breaks read requests across Shadow RAM sectors and ensures that no single
61  * read request exceeds the maximum 4KB read for a single AdminQ command.
62  *
63  * Returns a status code on failure. Note that the data pointer may be
64  * partially updated if some reads succeed before a failure.
65  */
66 enum ice_status
67 ice_read_flat_nvm(struct ice_hw *hw, u32 offset, u32 *length, u8 *data,
68 		  bool read_shadow_ram)
69 {
70 	enum ice_status status;
71 	u32 inlen = *length;
72 	u32 bytes_read = 0;
73 	bool last_cmd;
74 
75 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
76 
77 	*length = 0;
78 
79 	/* Verify the length of the read if this is for the Shadow RAM */
80 	if (read_shadow_ram && ((offset + inlen) > (hw->flash.sr_words * 2u))) {
81 		ice_debug(hw, ICE_DBG_NVM, "NVM error: requested data is beyond Shadow RAM limit\n");
82 		return ICE_ERR_PARAM;
83 	}
84 
85 	do {
86 		u32 read_size, sector_offset;
87 
88 		/* ice_aq_read_nvm cannot read more than 4KB at a time.
89 		 * Additionally, a read from the Shadow RAM may not cross over
90 		 * a sector boundary. Conveniently, the sector size is also
91 		 * 4KB.
92 		 */
93 		sector_offset = offset % ICE_AQ_MAX_BUF_LEN;
94 		read_size = MIN_T(u32, ICE_AQ_MAX_BUF_LEN - sector_offset,
95 				  inlen - bytes_read);
96 
97 		last_cmd = !(bytes_read + read_size < inlen);
98 
99 		/* ice_aq_read_nvm takes the length as a u16. Our read_size is
100 		 * calculated using a u32, but the ICE_AQ_MAX_BUF_LEN maximum
101 		 * size guarantees that it will fit within the 2 bytes.
102 		 */
103 		status = ice_aq_read_nvm(hw, ICE_AQC_NVM_START_POINT,
104 					 offset, (u16)read_size,
105 					 data + bytes_read, last_cmd,
106 					 read_shadow_ram, NULL);
107 		if (status)
108 			break;
109 
110 		bytes_read += read_size;
111 		offset += read_size;
112 	} while (!last_cmd);
113 
114 	*length = bytes_read;
115 	return status;
116 }
117 
118 /**
119  * ice_read_sr_word_aq - Reads Shadow RAM via AQ
120  * @hw: pointer to the HW structure
121  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
122  * @data: word read from the Shadow RAM
123  *
124  * Reads one 16 bit word from the Shadow RAM using ice_read_flat_nvm.
125  */
126 static enum ice_status
127 ice_read_sr_word_aq(struct ice_hw *hw, u16 offset, u16 *data)
128 {
129 	u32 bytes = sizeof(u16);
130 	enum ice_status status;
131 	__le16 data_local;
132 
133 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
134 
135 	/* Note that ice_read_flat_nvm checks if the read is past the Shadow
136 	 * RAM size, and ensures we don't read across a Shadow RAM sector
137 	 * boundary
138 	 */
139 	status = ice_read_flat_nvm(hw, offset * sizeof(u16), &bytes,
140 				   (_FORCE_ u8 *)&data_local, true);
141 	if (status)
142 		return status;
143 
144 	*data = LE16_TO_CPU(data_local);
145 	return ICE_SUCCESS;
146 }
147 
148 /**
149  * ice_read_sr_buf_aq - Reads Shadow RAM buf via AQ
150  * @hw: pointer to the HW structure
151  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
152  * @words: (in) number of words to read; (out) number of words actually read
153  * @data: words read from the Shadow RAM
154  *
155  * Reads 16 bit words (data buf) from the Shadow RAM. Ownership of the NVM is
156  * taken before reading the buffer and later released.
157  */
158 static enum ice_status
159 ice_read_sr_buf_aq(struct ice_hw *hw, u16 offset, u16 *words, u16 *data)
160 {
161 	u32 bytes = *words * 2, i;
162 	enum ice_status status;
163 
164 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
165 
166 	/* ice_read_flat_nvm takes into account the 4KB AdminQ and Shadow RAM
167 	 * sector restrictions necessary when reading from the NVM.
168 	 */
169 	status = ice_read_flat_nvm(hw, offset * 2, &bytes, (u8 *)data, true);
170 
171 	/* Report the number of words successfully read */
172 	*words = bytes / 2;
173 
174 	/* Byte swap the words up to the amount we actually read */
175 	for (i = 0; i < *words; i++)
176 		data[i] = LE16_TO_CPU(((_FORCE_ __le16 *)data)[i]);
177 
178 	return status;
179 }
180 
181 /**
182  * ice_acquire_nvm - Generic request for acquiring the NVM ownership
183  * @hw: pointer to the HW structure
184  * @access: NVM access type (read or write)
185  *
186  * This function will request NVM ownership.
187  */
188 enum ice_status
189 ice_acquire_nvm(struct ice_hw *hw, enum ice_aq_res_access_type access)
190 {
191 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
192 
193 	if (hw->flash.blank_nvm_mode)
194 		return ICE_SUCCESS;
195 
196 	return ice_acquire_res(hw, ICE_NVM_RES_ID, access, ICE_NVM_TIMEOUT);
197 }
198 
199 /**
200  * ice_release_nvm - Generic request for releasing the NVM ownership
201  * @hw: pointer to the HW structure
202  *
203  * This function will release NVM ownership.
204  */
205 void ice_release_nvm(struct ice_hw *hw)
206 {
207 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
208 
209 	if (hw->flash.blank_nvm_mode)
210 		return;
211 
212 	ice_release_res(hw, ICE_NVM_RES_ID);
213 }
214 
215 /**
216  * ice_read_sr_word - Reads Shadow RAM word and acquire NVM if necessary
217  * @hw: pointer to the HW structure
218  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
219  * @data: word read from the Shadow RAM
220  *
221  * Reads one 16 bit word from the Shadow RAM using the ice_read_sr_word_aq.
222  */
223 enum ice_status ice_read_sr_word(struct ice_hw *hw, u16 offset, u16 *data)
224 {
225 	enum ice_status status;
226 
227 	status = ice_acquire_nvm(hw, ICE_RES_READ);
228 	if (!status) {
229 		status = ice_read_sr_word_aq(hw, offset, data);
230 		ice_release_nvm(hw);
231 	}
232 
233 	return status;
234 }
235 
236 /**
237  * ice_get_pfa_module_tlv - Reads sub module TLV from NVM PFA
238  * @hw: pointer to hardware structure
239  * @module_tlv: pointer to module TLV to return
240  * @module_tlv_len: pointer to module TLV length to return
241  * @module_type: module type requested
242  *
243  * Finds the requested sub module TLV type from the Preserved Field
244  * Area (PFA) and returns the TLV pointer and length. The caller can
245  * use these to read the variable length TLV value.
246  */
247 enum ice_status
248 ice_get_pfa_module_tlv(struct ice_hw *hw, u16 *module_tlv, u16 *module_tlv_len,
249 		       u16 module_type)
250 {
251 	enum ice_status status;
252 	u16 pfa_len, pfa_ptr;
253 	u16 next_tlv;
254 
255 	status = ice_read_sr_word(hw, ICE_SR_PFA_PTR, &pfa_ptr);
256 	if (status != ICE_SUCCESS) {
257 		ice_debug(hw, ICE_DBG_INIT, "Preserved Field Array pointer.\n");
258 		return status;
259 	}
260 	status = ice_read_sr_word(hw, pfa_ptr, &pfa_len);
261 	if (status != ICE_SUCCESS) {
262 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PFA length.\n");
263 		return status;
264 	}
265 	/* Starting with first TLV after PFA length, iterate through the list
266 	 * of TLVs to find the requested one.
267 	 */
268 	next_tlv = pfa_ptr + 1;
269 	while (next_tlv < pfa_ptr + pfa_len) {
270 		u16 tlv_sub_module_type;
271 		u16 tlv_len;
272 
273 		/* Read TLV type */
274 		status = ice_read_sr_word(hw, next_tlv, &tlv_sub_module_type);
275 		if (status != ICE_SUCCESS) {
276 			ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV type.\n");
277 			break;
278 		}
279 		/* Read TLV length */
280 		status = ice_read_sr_word(hw, next_tlv + 1, &tlv_len);
281 		if (status != ICE_SUCCESS) {
282 			ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV length.\n");
283 			break;
284 		}
285 		if (tlv_sub_module_type == module_type) {
286 			if (tlv_len) {
287 				*module_tlv = next_tlv;
288 				*module_tlv_len = tlv_len;
289 				return ICE_SUCCESS;
290 			}
291 			return ICE_ERR_INVAL_SIZE;
292 		}
293 		/* Check next TLV, i.e. current TLV pointer + length + 2 words
294 		 * (for current TLV's type and length)
295 		 */
296 		next_tlv = next_tlv + tlv_len + 2;
297 	}
298 	/* Module does not exist */
299 	return ICE_ERR_DOES_NOT_EXIST;
300 }
301 
302 /**
303  * ice_read_pba_string - Reads part number string from NVM
304  * @hw: pointer to hardware structure
305  * @pba_num: stores the part number string from the NVM
306  * @pba_num_size: part number string buffer length
307  *
308  * Reads the part number string from the NVM.
309  */
310 enum ice_status
311 ice_read_pba_string(struct ice_hw *hw, u8 *pba_num, u32 pba_num_size)
312 {
313 	u16 pba_tlv, pba_tlv_len;
314 	enum ice_status status;
315 	u16 pba_word, pba_size;
316 	u16 i;
317 
318 	status = ice_get_pfa_module_tlv(hw, &pba_tlv, &pba_tlv_len,
319 					ICE_SR_PBA_BLOCK_PTR);
320 	if (status != ICE_SUCCESS) {
321 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block TLV.\n");
322 		return status;
323 	}
324 
325 	/* pba_size is the next word */
326 	status = ice_read_sr_word(hw, (pba_tlv + 2), &pba_size);
327 	if (status != ICE_SUCCESS) {
328 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Section size.\n");
329 		return status;
330 	}
331 
332 	if (pba_tlv_len < pba_size) {
333 		ice_debug(hw, ICE_DBG_INIT, "Invalid PBA Block TLV size.\n");
334 		return ICE_ERR_INVAL_SIZE;
335 	}
336 
337 	/* Subtract one to get PBA word count (PBA Size word is included in
338 	 * total size)
339 	 */
340 	pba_size--;
341 	if (pba_num_size < (((u32)pba_size * 2) + 1)) {
342 		ice_debug(hw, ICE_DBG_INIT, "Buffer too small for PBA data.\n");
343 		return ICE_ERR_PARAM;
344 	}
345 
346 	for (i = 0; i < pba_size; i++) {
347 		status = ice_read_sr_word(hw, (pba_tlv + 2 + 1) + i, &pba_word);
348 		if (status != ICE_SUCCESS) {
349 			ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block word %d.\n", i);
350 			return status;
351 		}
352 
353 		pba_num[(i * 2)] = (pba_word >> 8) & 0xFF;
354 		pba_num[(i * 2) + 1] = pba_word & 0xFF;
355 	}
356 	pba_num[(pba_size * 2)] = '\0';
357 
358 	return status;
359 }
360 
361 /**
362  * ice_get_nvm_ver_info - Read NVM version information
363  * @hw: pointer to the HW struct
364  * @nvm: pointer to NVM info structure
365  *
366  * Read the NVM EETRACK ID and map version of the main NVM image bank, filling
367  * in the nvm info structure.
368  */
369 static enum ice_status
370 ice_get_nvm_ver_info(struct ice_hw *hw, struct ice_nvm_info *nvm)
371 {
372 	u16 eetrack_lo, eetrack_hi, ver;
373 	enum ice_status status;
374 
375 	status = ice_read_sr_word(hw, ICE_SR_NVM_DEV_STARTER_VER, &ver);
376 	if (status) {
377 		ice_debug(hw, ICE_DBG_NVM, "Failed to read DEV starter version.\n");
378 		return status;
379 	}
380 	nvm->major = (ver & ICE_NVM_VER_HI_MASK) >> ICE_NVM_VER_HI_SHIFT;
381 	nvm->minor = (ver & ICE_NVM_VER_LO_MASK) >> ICE_NVM_VER_LO_SHIFT;
382 
383 	status = ice_read_sr_word(hw, ICE_SR_NVM_EETRACK_LO, &eetrack_lo);
384 	if (status) {
385 		ice_debug(hw, ICE_DBG_NVM, "Failed to read EETRACK lo.\n");
386 		return status;
387 	}
388 	status = ice_read_sr_word(hw, ICE_SR_NVM_EETRACK_HI, &eetrack_hi);
389 	if (status) {
390 		ice_debug(hw, ICE_DBG_NVM, "Failed to read EETRACK hi.\n");
391 		return status;
392 	}
393 
394 	nvm->eetrack = (eetrack_hi << 16) | eetrack_lo;
395 
396 	return ICE_SUCCESS;
397 }
398 
399 /**
400  * ice_get_orom_ver_info - Read Option ROM version information
401  * @hw: pointer to the HW struct
402  * @orom: pointer to Option ROM info structure
403  *
404  * Read the Combo Image version data from the Boot Configuration TLV and fill
405  * in the option ROM version data.
406  */
407 static enum ice_status
408 ice_get_orom_ver_info(struct ice_hw *hw, struct ice_orom_info *orom)
409 {
410 	u16 combo_hi, combo_lo, boot_cfg_tlv, boot_cfg_tlv_len;
411 	enum ice_status status;
412 	u32 combo_ver;
413 
414 	status = ice_get_pfa_module_tlv(hw, &boot_cfg_tlv, &boot_cfg_tlv_len,
415 					ICE_SR_BOOT_CFG_PTR);
416 	if (status) {
417 		ice_debug(hw, ICE_DBG_INIT, "Failed to read Boot Configuration Block TLV.\n");
418 		return status;
419 	}
420 
421 	/* Boot Configuration Block must have length at least 2 words
422 	 * (Combo Image Version High and Combo Image Version Low)
423 	 */
424 	if (boot_cfg_tlv_len < 2) {
425 		ice_debug(hw, ICE_DBG_INIT, "Invalid Boot Configuration Block TLV size.\n");
426 		return ICE_ERR_INVAL_SIZE;
427 	}
428 
429 	status = ice_read_sr_word(hw, (boot_cfg_tlv + ICE_NVM_OROM_VER_OFF),
430 				  &combo_hi);
431 	if (status) {
432 		ice_debug(hw, ICE_DBG_INIT, "Failed to read OROM_VER hi.\n");
433 		return status;
434 	}
435 
436 	status = ice_read_sr_word(hw, (boot_cfg_tlv + ICE_NVM_OROM_VER_OFF + 1),
437 				  &combo_lo);
438 	if (status) {
439 		ice_debug(hw, ICE_DBG_INIT, "Failed to read OROM_VER lo.\n");
440 		return status;
441 	}
442 
443 	combo_ver = ((u32)combo_hi << 16) | combo_lo;
444 
445 	orom->major = (u8)((combo_ver & ICE_OROM_VER_MASK) >>
446 			   ICE_OROM_VER_SHIFT);
447 	orom->patch = (u8)(combo_ver & ICE_OROM_VER_PATCH_MASK);
448 	orom->build = (u16)((combo_ver & ICE_OROM_VER_BUILD_MASK) >>
449 			    ICE_OROM_VER_BUILD_SHIFT);
450 
451 	return ICE_SUCCESS;
452 }
453 
454 /**
455  * ice_discover_flash_size - Discover the available flash size.
456  * @hw: pointer to the HW struct
457  *
458  * The device flash could be up to 16MB in size. However, it is possible that
459  * the actual size is smaller. Use bisection to determine the accessible size
460  * of flash memory.
461  */
462 static enum ice_status ice_discover_flash_size(struct ice_hw *hw)
463 {
464 	u32 min_size = 0, max_size = ICE_AQC_NVM_MAX_OFFSET + 1;
465 	enum ice_status status;
466 
467 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
468 
469 	status = ice_acquire_nvm(hw, ICE_RES_READ);
470 	if (status)
471 		return status;
472 
473 	while ((max_size - min_size) > 1) {
474 		u32 offset = (max_size + min_size) / 2;
475 		u32 len = 1;
476 		u8 data;
477 
478 		status = ice_read_flat_nvm(hw, offset, &len, &data, false);
479 		if (status == ICE_ERR_AQ_ERROR &&
480 		    hw->adminq.sq_last_status == ICE_AQ_RC_EINVAL) {
481 			ice_debug(hw, ICE_DBG_NVM, "%s: New upper bound of %u bytes\n",
482 				  __func__, offset);
483 			status = ICE_SUCCESS;
484 			max_size = offset;
485 		} else if (!status) {
486 			ice_debug(hw, ICE_DBG_NVM, "%s: New lower bound of %u bytes\n",
487 				  __func__, offset);
488 			min_size = offset;
489 		} else {
490 			/* an unexpected error occurred */
491 			goto err_read_flat_nvm;
492 		}
493 	}
494 
495 	ice_debug(hw, ICE_DBG_NVM, "Predicted flash size is %u bytes\n", max_size);
496 
497 	hw->flash.flash_size = max_size;
498 
499 err_read_flat_nvm:
500 	ice_release_nvm(hw);
501 
502 	return status;
503 }
504 
505 /**
506  * ice_read_sr_pointer - Read the value of a Shadow RAM pointer word
507  * @hw: pointer to the HW structure
508  * @offset: the word offset of the Shadow RAM word to read
509  * @pointer: pointer value read from Shadow RAM
510  *
511  * Read the given Shadow RAM word, and convert it to a pointer value specified
512  * in bytes. This function assumes the specified offset is a valid pointer
513  * word.
514  *
515  * Each pointer word specifies whether it is stored in word size or 4KB
516  * sector size by using the highest bit. The reported pointer value will be in
517  * bytes, intended for flat NVM reads.
518  */
519 static enum ice_status
520 ice_read_sr_pointer(struct ice_hw *hw, u16 offset, u32 *pointer)
521 {
522 	enum ice_status status;
523 	u16 value;
524 
525 	status = ice_read_sr_word(hw, offset, &value);
526 	if (status)
527 		return status;
528 
529 	/* Determine if the pointer is in 4KB or word units */
530 	if (value & ICE_SR_NVM_PTR_4KB_UNITS)
531 		*pointer = (value & ~ICE_SR_NVM_PTR_4KB_UNITS) * 4 * 1024;
532 	else
533 		*pointer = value * 2;
534 
535 	return ICE_SUCCESS;
536 }
537 
538 /**
539  * ice_read_sr_area_size - Read an area size from a Shadow RAM word
540  * @hw: pointer to the HW structure
541  * @offset: the word offset of the Shadow RAM to read
542  * @size: size value read from the Shadow RAM
543  *
544  * Read the given Shadow RAM word, and convert it to an area size value
545  * specified in bytes. This function assumes the specified offset is a valid
546  * area size word.
547  *
548  * Each area size word is specified in 4KB sector units. This function reports
549  * the size in bytes, intended for flat NVM reads.
550  */
551 static enum ice_status
552 ice_read_sr_area_size(struct ice_hw *hw, u16 offset, u32 *size)
553 {
554 	enum ice_status status;
555 	u16 value;
556 
557 	status = ice_read_sr_word(hw, offset, &value);
558 	if (status)
559 		return status;
560 
561 	/* Area sizes are always specified in 4KB units */
562 	*size = value * 4 * 1024;
563 
564 	return ICE_SUCCESS;
565 }
566 
567 /**
568  * ice_determine_active_flash_banks - Discover active bank for each module
569  * @hw: pointer to the HW struct
570  *
571  * Read the Shadow RAM control word and determine which banks are active for
572  * the NVM, OROM, and Netlist modules. Also read and calculate the associated
573  * pointer and size. These values are then cached into the ice_flash_info
574  * structure for later use in order to calculate the correct offset to read
575  * from the active module.
576  */
577 static enum ice_status
578 ice_determine_active_flash_banks(struct ice_hw *hw)
579 {
580 	struct ice_bank_info *banks = &hw->flash.banks;
581 	enum ice_status status;
582 	u16 ctrl_word;
583 
584 	status = ice_read_sr_word(hw, ICE_SR_NVM_CTRL_WORD, &ctrl_word);
585 	if (status) {
586 		ice_debug(hw, ICE_DBG_NVM, "Failed to read the Shadow RAM control word\n");
587 		return status;
588 	}
589 
590 	/* Check that the control word indicates validity */
591 	if ((ctrl_word & ICE_SR_CTRL_WORD_1_M) >> ICE_SR_CTRL_WORD_1_S != ICE_SR_CTRL_WORD_VALID) {
592 		ice_debug(hw, ICE_DBG_NVM, "Shadow RAM control word is invalid\n");
593 		return ICE_ERR_CFG;
594 	}
595 
596 	if (!(ctrl_word & ICE_SR_CTRL_WORD_NVM_BANK))
597 		banks->nvm_bank = ICE_1ST_FLASH_BANK;
598 	else
599 		banks->nvm_bank = ICE_2ND_FLASH_BANK;
600 
601 	if (!(ctrl_word & ICE_SR_CTRL_WORD_OROM_BANK))
602 		banks->orom_bank = ICE_1ST_FLASH_BANK;
603 	else
604 		banks->orom_bank = ICE_2ND_FLASH_BANK;
605 
606 	if (!(ctrl_word & ICE_SR_CTRL_WORD_NETLIST_BANK))
607 		banks->netlist_bank = ICE_1ST_FLASH_BANK;
608 	else
609 		banks->netlist_bank = ICE_2ND_FLASH_BANK;
610 
611 	status = ice_read_sr_pointer(hw, ICE_SR_1ST_NVM_BANK_PTR, &banks->nvm_ptr);
612 	if (status) {
613 		ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank pointer\n");
614 		return status;
615 	}
616 
617 	status = ice_read_sr_area_size(hw, ICE_SR_NVM_BANK_SIZE, &banks->nvm_size);
618 	if (status) {
619 		ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank area size\n");
620 		return status;
621 	}
622 
623 	status = ice_read_sr_pointer(hw, ICE_SR_1ST_OROM_BANK_PTR, &banks->orom_ptr);
624 	if (status) {
625 		ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank pointer\n");
626 		return status;
627 	}
628 
629 	status = ice_read_sr_area_size(hw, ICE_SR_OROM_BANK_SIZE, &banks->orom_size);
630 	if (status) {
631 		ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank area size\n");
632 		return status;
633 	}
634 
635 	status = ice_read_sr_pointer(hw, ICE_SR_NETLIST_BANK_PTR, &banks->netlist_ptr);
636 	if (status) {
637 		ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank pointer\n");
638 		return status;
639 	}
640 
641 	status = ice_read_sr_area_size(hw, ICE_SR_NETLIST_BANK_SIZE, &banks->netlist_size);
642 	if (status) {
643 		ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank area size\n");
644 		return status;
645 	}
646 
647 	return ICE_SUCCESS;
648 }
649 
650 /**
651  * ice_init_nvm - initializes NVM setting
652  * @hw: pointer to the HW struct
653  *
654  * This function reads and populates NVM settings such as Shadow RAM size,
655  * max_timeout, and blank_nvm_mode
656  */
657 enum ice_status ice_init_nvm(struct ice_hw *hw)
658 {
659 	struct ice_flash_info *flash = &hw->flash;
660 	enum ice_status status;
661 	u32 fla, gens_stat;
662 	u8 sr_size;
663 
664 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
665 
666 	/* The SR size is stored regardless of the NVM programming mode
667 	 * as the blank mode may be used in the factory line.
668 	 */
669 	gens_stat = rd32(hw, GLNVM_GENS);
670 	sr_size = (gens_stat & GLNVM_GENS_SR_SIZE_M) >> GLNVM_GENS_SR_SIZE_S;
671 
672 	/* Switching to words (sr_size contains power of 2) */
673 	flash->sr_words = BIT(sr_size) * ICE_SR_WORDS_IN_1KB;
674 
675 	/* Check if we are in the normal or blank NVM programming mode */
676 	fla = rd32(hw, GLNVM_FLA);
677 	if (fla & GLNVM_FLA_LOCKED_M) { /* Normal programming mode */
678 		flash->blank_nvm_mode = false;
679 	} else {
680 		/* Blank programming mode */
681 		flash->blank_nvm_mode = true;
682 		ice_debug(hw, ICE_DBG_NVM, "NVM init error: unsupported blank mode.\n");
683 		return ICE_ERR_NVM_BLANK_MODE;
684 	}
685 
686 	status = ice_discover_flash_size(hw);
687 	if (status) {
688 		ice_debug(hw, ICE_DBG_NVM, "NVM init error: failed to discover flash size.\n");
689 		return status;
690 	}
691 
692 	status = ice_determine_active_flash_banks(hw);
693 	if (status) {
694 		ice_debug(hw, ICE_DBG_NVM, "Failed to determine active flash banks.\n");
695 		return status;
696 	}
697 
698 	status = ice_get_nvm_ver_info(hw, &flash->nvm);
699 	if (status) {
700 		ice_debug(hw, ICE_DBG_INIT, "Failed to read NVM info.\n");
701 		return status;
702 	}
703 
704 	status = ice_get_orom_ver_info(hw, &flash->orom);
705 	if (status) {
706 		ice_debug(hw, ICE_DBG_INIT, "Failed to read Option ROM info.\n");
707 		return status;
708 	}
709 
710 	return ICE_SUCCESS;
711 }
712 
713 /**
714  * ice_read_sr_buf - Reads Shadow RAM buf and acquire lock if necessary
715  * @hw: pointer to the HW structure
716  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
717  * @words: (in) number of words to read; (out) number of words actually read
718  * @data: words read from the Shadow RAM
719  *
720  * Reads 16 bit words (data buf) from the SR using the ice_read_nvm_buf_aq
721  * method. The buf read is preceded by the NVM ownership take
722  * and followed by the release.
723  */
724 enum ice_status
725 ice_read_sr_buf(struct ice_hw *hw, u16 offset, u16 *words, u16 *data)
726 {
727 	enum ice_status status;
728 
729 	status = ice_acquire_nvm(hw, ICE_RES_READ);
730 	if (!status) {
731 		status = ice_read_sr_buf_aq(hw, offset, words, data);
732 		ice_release_nvm(hw);
733 	}
734 
735 	return status;
736 }
737 
738 /**
739  * ice_nvm_validate_checksum
740  * @hw: pointer to the HW struct
741  *
742  * Verify NVM PFA checksum validity (0x0706)
743  */
744 enum ice_status ice_nvm_validate_checksum(struct ice_hw *hw)
745 {
746 	struct ice_aqc_nvm_checksum *cmd;
747 	struct ice_aq_desc desc;
748 	enum ice_status status;
749 
750 	status = ice_acquire_nvm(hw, ICE_RES_READ);
751 	if (status)
752 		return status;
753 
754 	cmd = &desc.params.nvm_checksum;
755 
756 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_checksum);
757 	cmd->flags = ICE_AQC_NVM_CHECKSUM_VERIFY;
758 
759 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
760 	ice_release_nvm(hw);
761 
762 	if (!status)
763 		if (LE16_TO_CPU(cmd->checksum) != ICE_AQC_NVM_CHECKSUM_CORRECT)
764 			status = ICE_ERR_NVM_CHECKSUM;
765 
766 	return status;
767 }
768 
769 /**
770  * ice_nvm_access_get_features - Return the NVM access features structure
771  * @cmd: NVM access command to process
772  * @data: storage for the driver NVM features
773  *
774  * Fill in the data section of the NVM access request with a copy of the NVM
775  * features structure.
776  */
777 enum ice_status
778 ice_nvm_access_get_features(struct ice_nvm_access_cmd *cmd,
779 			    union ice_nvm_access_data *data)
780 {
781 	/* The provided data_size must be at least as large as our NVM
782 	 * features structure. A larger size should not be treated as an
783 	 * error, to allow future extensions to the features structure to
784 	 * work on older drivers.
785 	 */
786 	if (cmd->data_size < sizeof(struct ice_nvm_features))
787 		return ICE_ERR_NO_MEMORY;
788 
789 	/* Initialize the data buffer to zeros */
790 	ice_memset(data, 0, cmd->data_size, ICE_NONDMA_MEM);
791 
792 	/* Fill in the features data */
793 	data->drv_features.major = ICE_NVM_ACCESS_MAJOR_VER;
794 	data->drv_features.minor = ICE_NVM_ACCESS_MINOR_VER;
795 	data->drv_features.size = sizeof(struct ice_nvm_features);
796 	data->drv_features.features[0] = ICE_NVM_FEATURES_0_REG_ACCESS;
797 
798 	return ICE_SUCCESS;
799 }
800 
801 /**
802  * ice_nvm_access_get_module - Helper function to read module value
803  * @cmd: NVM access command structure
804  *
805  * Reads the module value out of the NVM access config field.
806  */
807 u32 ice_nvm_access_get_module(struct ice_nvm_access_cmd *cmd)
808 {
809 	return ((cmd->config & ICE_NVM_CFG_MODULE_M) >> ICE_NVM_CFG_MODULE_S);
810 }
811 
812 /**
813  * ice_nvm_access_get_flags - Helper function to read flags value
814  * @cmd: NVM access command structure
815  *
816  * Reads the flags value out of the NVM access config field.
817  */
818 u32 ice_nvm_access_get_flags(struct ice_nvm_access_cmd *cmd)
819 {
820 	return ((cmd->config & ICE_NVM_CFG_FLAGS_M) >> ICE_NVM_CFG_FLAGS_S);
821 }
822 
823 /**
824  * ice_nvm_access_get_adapter - Helper function to read adapter info
825  * @cmd: NVM access command structure
826  *
827  * Read the adapter info value out of the NVM access config field.
828  */
829 u32 ice_nvm_access_get_adapter(struct ice_nvm_access_cmd *cmd)
830 {
831 	return ((cmd->config & ICE_NVM_CFG_ADAPTER_INFO_M) >>
832 		ICE_NVM_CFG_ADAPTER_INFO_S);
833 }
834 
835 /**
836  * ice_validate_nvm_rw_reg - Check than an NVM access request is valid
837  * @cmd: NVM access command structure
838  *
839  * Validates that an NVM access structure is request to read or write a valid
840  * register offset. First validates that the module and flags are correct, and
841  * then ensures that the register offset is one of the accepted registers.
842  */
843 static enum ice_status
844 ice_validate_nvm_rw_reg(struct ice_nvm_access_cmd *cmd)
845 {
846 	u32 module, flags, offset;
847 	u16 i;
848 
849 	module = ice_nvm_access_get_module(cmd);
850 	flags = ice_nvm_access_get_flags(cmd);
851 	offset = cmd->offset;
852 
853 	/* Make sure the module and flags indicate a read/write request */
854 	if (module != ICE_NVM_REG_RW_MODULE ||
855 	    flags != ICE_NVM_REG_RW_FLAGS ||
856 	    cmd->data_size != FIELD_SIZEOF(union ice_nvm_access_data, regval))
857 		return ICE_ERR_PARAM;
858 
859 	switch (offset) {
860 	case GL_HICR:
861 	case GL_HICR_EN: /* Note, this register is read only */
862 	case GL_FWSTS:
863 	case GL_MNG_FWSM:
864 	case GLGEN_CSR_DEBUG_C:
865 	case GLGEN_RSTAT:
866 	case GLPCI_LBARCTRL:
867 	case GLNVM_GENS:
868 	case GLNVM_FLA:
869 	case PF_FUNC_RID:
870 		return ICE_SUCCESS;
871 	default:
872 		break;
873 	}
874 
875 	for (i = 0; i <= ICE_NVM_ACCESS_GL_HIDA_MAX; i++)
876 		if (offset == (u32)GL_HIDA(i))
877 			return ICE_SUCCESS;
878 
879 	for (i = 0; i <= ICE_NVM_ACCESS_GL_HIBA_MAX; i++)
880 		if (offset == (u32)GL_HIBA(i))
881 			return ICE_SUCCESS;
882 
883 	/* All other register offsets are not valid */
884 	return ICE_ERR_OUT_OF_RANGE;
885 }
886 
887 /**
888  * ice_nvm_access_read - Handle an NVM read request
889  * @hw: pointer to the HW struct
890  * @cmd: NVM access command to process
891  * @data: storage for the register value read
892  *
893  * Process an NVM access request to read a register.
894  */
895 enum ice_status
896 ice_nvm_access_read(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
897 		    union ice_nvm_access_data *data)
898 {
899 	enum ice_status status;
900 
901 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
902 
903 	/* Always initialize the output data, even on failure */
904 	ice_memset(data, 0, cmd->data_size, ICE_NONDMA_MEM);
905 
906 	/* Make sure this is a valid read/write access request */
907 	status = ice_validate_nvm_rw_reg(cmd);
908 	if (status)
909 		return status;
910 
911 	ice_debug(hw, ICE_DBG_NVM, "NVM access: reading register %08x\n",
912 		  cmd->offset);
913 
914 	/* Read the register and store the contents in the data field */
915 	data->regval = rd32(hw, cmd->offset);
916 
917 	return ICE_SUCCESS;
918 }
919 
920 /**
921  * ice_nvm_access_write - Handle an NVM write request
922  * @hw: pointer to the HW struct
923  * @cmd: NVM access command to process
924  * @data: NVM access data to write
925  *
926  * Process an NVM access request to write a register.
927  */
928 enum ice_status
929 ice_nvm_access_write(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
930 		     union ice_nvm_access_data *data)
931 {
932 	enum ice_status status;
933 
934 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
935 
936 	/* Make sure this is a valid read/write access request */
937 	status = ice_validate_nvm_rw_reg(cmd);
938 	if (status)
939 		return status;
940 
941 	/* Reject requests to write to read-only registers */
942 	switch (cmd->offset) {
943 	case GL_HICR_EN:
944 	case GLGEN_RSTAT:
945 		return ICE_ERR_OUT_OF_RANGE;
946 	default:
947 		break;
948 	}
949 
950 	ice_debug(hw, ICE_DBG_NVM, "NVM access: writing register %08x with value %08x\n",
951 		  cmd->offset, data->regval);
952 
953 	/* Write the data field to the specified register */
954 	wr32(hw, cmd->offset, data->regval);
955 
956 	return ICE_SUCCESS;
957 }
958 
959 /**
960  * ice_handle_nvm_access - Handle an NVM access request
961  * @hw: pointer to the HW struct
962  * @cmd: NVM access command info
963  * @data: pointer to read or return data
964  *
965  * Process an NVM access request. Read the command structure information and
966  * determine if it is valid. If not, report an error indicating the command
967  * was invalid.
968  *
969  * For valid commands, perform the necessary function, copying the data into
970  * the provided data buffer.
971  */
972 enum ice_status
973 ice_handle_nvm_access(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
974 		      union ice_nvm_access_data *data)
975 {
976 	u32 module, flags, adapter_info;
977 
978 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
979 
980 	/* Extended flags are currently reserved and must be zero */
981 	if ((cmd->config & ICE_NVM_CFG_EXT_FLAGS_M) != 0)
982 		return ICE_ERR_PARAM;
983 
984 	/* Adapter info must match the HW device ID */
985 	adapter_info = ice_nvm_access_get_adapter(cmd);
986 	if (adapter_info != hw->device_id)
987 		return ICE_ERR_PARAM;
988 
989 	switch (cmd->command) {
990 	case ICE_NVM_CMD_READ:
991 		module = ice_nvm_access_get_module(cmd);
992 		flags = ice_nvm_access_get_flags(cmd);
993 
994 		/* Getting the driver's NVM features structure shares the same
995 		 * command type as reading a register. Read the config field
996 		 * to determine if this is a request to get features.
997 		 */
998 		if (module == ICE_NVM_GET_FEATURES_MODULE &&
999 		    flags == ICE_NVM_GET_FEATURES_FLAGS &&
1000 		    cmd->offset == 0)
1001 			return ice_nvm_access_get_features(cmd, data);
1002 		else
1003 			return ice_nvm_access_read(hw, cmd, data);
1004 	case ICE_NVM_CMD_WRITE:
1005 		return ice_nvm_access_write(hw, cmd, data);
1006 	default:
1007 		return ICE_ERR_PARAM;
1008 	}
1009 }
1010