xref: /dpdk/drivers/net/ice/base/ice_nvm.c (revision b644b5cf)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2001-2021 Intel Corporation
3  */
4 
5 #include "ice_common.h"
6 
7 #define GL_MNG_DEF_DEVID 0x000B611C
8 
9 /**
10  * ice_aq_read_nvm
11  * @hw: pointer to the HW struct
12  * @module_typeid: module pointer location in words from the NVM beginning
13  * @offset: byte offset from the module beginning
14  * @length: length of the section to be read (in bytes from the offset)
15  * @data: command buffer (size [bytes] = length)
16  * @last_command: tells if this is the last command in a series
17  * @read_shadow_ram: tell if this is a shadow RAM read
18  * @cd: pointer to command details structure or NULL
19  *
20  * Read the NVM using the admin queue commands (0x0701)
21  */
22 enum ice_status
ice_aq_read_nvm(struct ice_hw * hw,u16 module_typeid,u32 offset,u16 length,void * data,bool last_command,bool read_shadow_ram,struct ice_sq_cd * cd)23 ice_aq_read_nvm(struct ice_hw *hw, u16 module_typeid, u32 offset, u16 length,
24 		void *data, bool last_command, bool read_shadow_ram,
25 		struct ice_sq_cd *cd)
26 {
27 	struct ice_aq_desc desc;
28 	struct ice_aqc_nvm *cmd;
29 
30 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
31 
32 	cmd = &desc.params.nvm;
33 
34 	if (offset > ICE_AQC_NVM_MAX_OFFSET)
35 		return ICE_ERR_PARAM;
36 
37 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_read);
38 
39 	if (!read_shadow_ram && module_typeid == ICE_AQC_NVM_START_POINT)
40 		cmd->cmd_flags |= ICE_AQC_NVM_FLASH_ONLY;
41 
42 	/* If this is the last command in a series, set the proper flag. */
43 	if (last_command)
44 		cmd->cmd_flags |= ICE_AQC_NVM_LAST_CMD;
45 	cmd->module_typeid = CPU_TO_LE16(module_typeid);
46 	cmd->offset_low = CPU_TO_LE16(offset & 0xFFFF);
47 	cmd->offset_high = (offset >> 16) & 0xFF;
48 	cmd->length = CPU_TO_LE16(length);
49 
50 	return ice_aq_send_cmd(hw, &desc, data, length, cd);
51 }
52 
53 /**
54  * ice_read_flat_nvm - Read portion of NVM by flat offset
55  * @hw: pointer to the HW struct
56  * @offset: offset from beginning of NVM
57  * @length: (in) number of bytes to read; (out) number of bytes actually read
58  * @data: buffer to return data in (sized to fit the specified length)
59  * @read_shadow_ram: if true, read from shadow RAM instead of NVM
60  *
61  * Reads a portion of the NVM, as a flat memory space. This function correctly
62  * breaks read requests across Shadow RAM sectors and ensures that no single
63  * read request exceeds the maximum 4KB read for a single AdminQ command.
64  *
65  * Returns a status code on failure. Note that the data pointer may be
66  * partially updated if some reads succeed before a failure.
67  */
68 enum ice_status
ice_read_flat_nvm(struct ice_hw * hw,u32 offset,u32 * length,u8 * data,bool read_shadow_ram)69 ice_read_flat_nvm(struct ice_hw *hw, u32 offset, u32 *length, u8 *data,
70 		  bool read_shadow_ram)
71 {
72 	enum ice_status status;
73 	u32 inlen = *length;
74 	u32 bytes_read = 0;
75 	bool last_cmd;
76 
77 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
78 
79 	*length = 0;
80 
81 	/* Verify the length of the read if this is for the Shadow RAM */
82 	if (read_shadow_ram && ((offset + inlen) > (hw->flash.sr_words * 2u))) {
83 		ice_debug(hw, ICE_DBG_NVM, "NVM error: requested data is beyond Shadow RAM limit\n");
84 		return ICE_ERR_PARAM;
85 	}
86 
87 	do {
88 		u32 read_size, sector_offset;
89 
90 		/* ice_aq_read_nvm cannot read more than 4KB at a time.
91 		 * Additionally, a read from the Shadow RAM may not cross over
92 		 * a sector boundary. Conveniently, the sector size is also
93 		 * 4KB.
94 		 */
95 		sector_offset = offset % ICE_AQ_MAX_BUF_LEN;
96 		read_size = MIN_T(u32, ICE_AQ_MAX_BUF_LEN - sector_offset,
97 				  inlen - bytes_read);
98 
99 		last_cmd = !(bytes_read + read_size < inlen);
100 
101 		/* ice_aq_read_nvm takes the length as a u16. Our read_size is
102 		 * calculated using a u32, but the ICE_AQ_MAX_BUF_LEN maximum
103 		 * size guarantees that it will fit within the 2 bytes.
104 		 */
105 		status = ice_aq_read_nvm(hw, ICE_AQC_NVM_START_POINT,
106 					 offset, (u16)read_size,
107 					 data + bytes_read, last_cmd,
108 					 read_shadow_ram, NULL);
109 		if (status)
110 			break;
111 
112 		bytes_read += read_size;
113 		offset += read_size;
114 	} while (!last_cmd);
115 
116 	*length = bytes_read;
117 	return status;
118 }
119 
120 /**
121  * ice_read_sr_word_aq - Reads Shadow RAM via AQ
122  * @hw: pointer to the HW structure
123  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
124  * @data: word read from the Shadow RAM
125  *
126  * Reads one 16 bit word from the Shadow RAM using ice_read_flat_nvm.
127  */
128 static enum ice_status
ice_read_sr_word_aq(struct ice_hw * hw,u16 offset,u16 * data)129 ice_read_sr_word_aq(struct ice_hw *hw, u16 offset, u16 *data)
130 {
131 	u32 bytes = sizeof(u16);
132 	enum ice_status status;
133 	__le16 data_local;
134 
135 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
136 
137 	/* Note that ice_read_flat_nvm checks if the read is past the Shadow
138 	 * RAM size, and ensures we don't read across a Shadow RAM sector
139 	 * boundary
140 	 */
141 	status = ice_read_flat_nvm(hw, offset * sizeof(u16), &bytes,
142 				   (_FORCE_ u8 *)&data_local, true);
143 	if (status)
144 		return status;
145 
146 	*data = LE16_TO_CPU(data_local);
147 	return ICE_SUCCESS;
148 }
149 
150 /**
151  * ice_read_sr_buf_aq - Reads Shadow RAM buf via AQ
152  * @hw: pointer to the HW structure
153  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
154  * @words: (in) number of words to read; (out) number of words actually read
155  * @data: words read from the Shadow RAM
156  *
157  * Reads 16 bit words (data buf) from the Shadow RAM. Ownership of the NVM is
158  * taken before reading the buffer and later released.
159  */
160 static enum ice_status
ice_read_sr_buf_aq(struct ice_hw * hw,u16 offset,u16 * words,u16 * data)161 ice_read_sr_buf_aq(struct ice_hw *hw, u16 offset, u16 *words, u16 *data)
162 {
163 	u32 bytes = *words * 2, i;
164 	enum ice_status status;
165 
166 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
167 
168 	/* ice_read_flat_nvm takes into account the 4KB AdminQ and Shadow RAM
169 	 * sector restrictions necessary when reading from the NVM.
170 	 */
171 	status = ice_read_flat_nvm(hw, offset * 2, &bytes, (u8 *)data, true);
172 
173 	/* Report the number of words successfully read */
174 	*words = bytes / 2;
175 
176 	/* Byte swap the words up to the amount we actually read */
177 	for (i = 0; i < *words; i++)
178 		data[i] = LE16_TO_CPU(((_FORCE_ __le16 *)data)[i]);
179 
180 	return status;
181 }
182 
183 /**
184  * ice_acquire_nvm - Generic request for acquiring the NVM ownership
185  * @hw: pointer to the HW structure
186  * @access: NVM access type (read or write)
187  *
188  * This function will request NVM ownership.
189  */
190 enum ice_status
ice_acquire_nvm(struct ice_hw * hw,enum ice_aq_res_access_type access)191 ice_acquire_nvm(struct ice_hw *hw, enum ice_aq_res_access_type access)
192 {
193 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
194 
195 	if (hw->flash.blank_nvm_mode)
196 		return ICE_SUCCESS;
197 
198 	return ice_acquire_res(hw, ICE_NVM_RES_ID, access, ICE_NVM_TIMEOUT);
199 }
200 
201 /**
202  * ice_release_nvm - Generic request for releasing the NVM ownership
203  * @hw: pointer to the HW structure
204  *
205  * This function will release NVM ownership.
206  */
ice_release_nvm(struct ice_hw * hw)207 void ice_release_nvm(struct ice_hw *hw)
208 {
209 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
210 
211 	if (hw->flash.blank_nvm_mode)
212 		return;
213 
214 	ice_release_res(hw, ICE_NVM_RES_ID);
215 }
216 
217 /**
218  * ice_get_flash_bank_offset - Get offset into requested flash bank
219  * @hw: pointer to the HW structure
220  * @bank: whether to read from the active or inactive flash bank
221  * @module: the module to read from
222  *
223  * Based on the module, lookup the module offset from the beginning of the
224  * flash.
225  *
226  * Returns the flash offset. Note that a value of zero is invalid and must be
227  * treated as an error.
228  */
ice_get_flash_bank_offset(struct ice_hw * hw,enum ice_bank_select bank,u16 module)229 static u32 ice_get_flash_bank_offset(struct ice_hw *hw, enum ice_bank_select bank, u16 module)
230 {
231 	struct ice_bank_info *banks = &hw->flash.banks;
232 	enum ice_flash_bank active_bank;
233 	bool second_bank_active;
234 	u32 offset, size;
235 
236 	switch (module) {
237 	case ICE_SR_1ST_NVM_BANK_PTR:
238 		offset = banks->nvm_ptr;
239 		size = banks->nvm_size;
240 		active_bank = banks->nvm_bank;
241 		break;
242 	case ICE_SR_1ST_OROM_BANK_PTR:
243 		offset = banks->orom_ptr;
244 		size = banks->orom_size;
245 		active_bank = banks->orom_bank;
246 		break;
247 	case ICE_SR_NETLIST_BANK_PTR:
248 		offset = banks->netlist_ptr;
249 		size = banks->netlist_size;
250 		active_bank = banks->netlist_bank;
251 		break;
252 	default:
253 		ice_debug(hw, ICE_DBG_NVM, "Unexpected value for flash module: 0x%04x\n", module);
254 		return 0;
255 	}
256 
257 	switch (active_bank) {
258 	case ICE_1ST_FLASH_BANK:
259 		second_bank_active = false;
260 		break;
261 	case ICE_2ND_FLASH_BANK:
262 		second_bank_active = true;
263 		break;
264 	default:
265 		ice_debug(hw, ICE_DBG_NVM, "Unexpected value for active flash bank: %u\n",
266 			  active_bank);
267 		return 0;
268 	}
269 
270 	/* The second flash bank is stored immediately following the first
271 	 * bank. Based on whether the 1st or 2nd bank is active, and whether
272 	 * we want the active or inactive bank, calculate the desired offset.
273 	 */
274 	switch (bank) {
275 	case ICE_ACTIVE_FLASH_BANK:
276 		return offset + (second_bank_active ? size : 0);
277 	case ICE_INACTIVE_FLASH_BANK:
278 		return offset + (second_bank_active ? 0 : size);
279 	}
280 
281 	ice_debug(hw, ICE_DBG_NVM, "Unexpected value for flash bank selection: %u\n", bank);
282 	return 0;
283 }
284 
285 /**
286  * ice_read_flash_module - Read a word from one of the main NVM modules
287  * @hw: pointer to the HW structure
288  * @bank: which bank of the module to read
289  * @module: the module to read
290  * @offset: the offset into the module in bytes
291  * @data: storage for the word read from the flash
292  * @length: bytes of data to read
293  *
294  * Read data from the specified flash module. The bank parameter indicates
295  * whether or not to read from the active bank or the inactive bank of that
296  * module.
297  *
298  * The word will be read using flat NVM access, and relies on the
299  * hw->flash.banks data being setup by ice_determine_active_flash_banks()
300  * during initialization.
301  */
302 static enum ice_status
ice_read_flash_module(struct ice_hw * hw,enum ice_bank_select bank,u16 module,u32 offset,u8 * data,u32 length)303 ice_read_flash_module(struct ice_hw *hw, enum ice_bank_select bank, u16 module,
304 		      u32 offset, u8 *data, u32 length)
305 {
306 	enum ice_status status;
307 	u32 start;
308 
309 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
310 
311 	start = ice_get_flash_bank_offset(hw, bank, module);
312 	if (!start) {
313 		ice_debug(hw, ICE_DBG_NVM, "Unable to calculate flash bank offset for module 0x%04x\n",
314 			  module);
315 		return ICE_ERR_PARAM;
316 	}
317 
318 	status = ice_acquire_nvm(hw, ICE_RES_READ);
319 	if (status)
320 		return status;
321 
322 	status = ice_read_flat_nvm(hw, start + offset, &length, data, false);
323 
324 	ice_release_nvm(hw);
325 
326 	return status;
327 }
328 
329 /**
330  * ice_read_nvm_module - Read from the active main NVM module
331  * @hw: pointer to the HW structure
332  * @bank: whether to read from active or inactive NVM module
333  * @offset: offset into the NVM module to read, in words
334  * @data: storage for returned word value
335  *
336  * Read the specified word from the active NVM module. This includes the CSS
337  * header at the start of the NVM module.
338  */
339 static enum ice_status
ice_read_nvm_module(struct ice_hw * hw,enum ice_bank_select bank,u32 offset,u16 * data)340 ice_read_nvm_module(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
341 {
342 	enum ice_status status;
343 	__le16 data_local;
344 
345 	status = ice_read_flash_module(hw, bank, ICE_SR_1ST_NVM_BANK_PTR, offset * sizeof(u16),
346 				       (_FORCE_ u8 *)&data_local, sizeof(u16));
347 	if (!status)
348 		*data = LE16_TO_CPU(data_local);
349 
350 	return status;
351 }
352 
353 /**
354  * ice_read_nvm_sr_copy - Read a word from the Shadow RAM copy in the NVM bank
355  * @hw: pointer to the HW structure
356  * @bank: whether to read from the active or inactive NVM module
357  * @offset: offset into the Shadow RAM copy to read, in words
358  * @data: storage for returned word value
359  *
360  * Read the specified word from the copy of the Shadow RAM found in the
361  * specified NVM module.
362  */
363 static enum ice_status
ice_read_nvm_sr_copy(struct ice_hw * hw,enum ice_bank_select bank,u32 offset,u16 * data)364 ice_read_nvm_sr_copy(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
365 {
366 	return ice_read_nvm_module(hw, bank, ICE_NVM_SR_COPY_WORD_OFFSET + offset, data);
367 }
368 
369 /**
370  * ice_read_orom_module - Read from the active Option ROM module
371  * @hw: pointer to the HW structure
372  * @bank: whether to read from active or inactive OROM module
373  * @offset: offset into the OROM module to read, in words
374  * @data: storage for returned word value
375  *
376  * Read the specified word from the active Option ROM module of the flash.
377  * Note that unlike the NVM module, the CSS data is stored at the end of the
378  * module instead of at the beginning.
379  */
380 static enum ice_status
ice_read_orom_module(struct ice_hw * hw,enum ice_bank_select bank,u32 offset,u16 * data)381 ice_read_orom_module(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
382 {
383 	enum ice_status status;
384 	__le16 data_local;
385 
386 	status = ice_read_flash_module(hw, bank, ICE_SR_1ST_OROM_BANK_PTR, offset * sizeof(u16),
387 				       (_FORCE_ u8 *)&data_local, sizeof(u16));
388 	if (!status)
389 		*data = LE16_TO_CPU(data_local);
390 
391 	return status;
392 }
393 
394 /**
395  * ice_read_sr_word - Reads Shadow RAM word and acquire NVM if necessary
396  * @hw: pointer to the HW structure
397  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
398  * @data: word read from the Shadow RAM
399  *
400  * Reads one 16 bit word from the Shadow RAM using the ice_read_sr_word_aq.
401  */
ice_read_sr_word(struct ice_hw * hw,u16 offset,u16 * data)402 enum ice_status ice_read_sr_word(struct ice_hw *hw, u16 offset, u16 *data)
403 {
404 	enum ice_status status;
405 
406 	status = ice_acquire_nvm(hw, ICE_RES_READ);
407 	if (!status) {
408 		status = ice_read_sr_word_aq(hw, offset, data);
409 		ice_release_nvm(hw);
410 	}
411 
412 	return status;
413 }
414 
415 /**
416  * ice_get_pfa_module_tlv - Reads sub module TLV from NVM PFA
417  * @hw: pointer to hardware structure
418  * @module_tlv: pointer to module TLV to return
419  * @module_tlv_len: pointer to module TLV length to return
420  * @module_type: module type requested
421  *
422  * Finds the requested sub module TLV type from the Preserved Field
423  * Area (PFA) and returns the TLV pointer and length. The caller can
424  * use these to read the variable length TLV value.
425  */
426 enum ice_status
ice_get_pfa_module_tlv(struct ice_hw * hw,u16 * module_tlv,u16 * module_tlv_len,u16 module_type)427 ice_get_pfa_module_tlv(struct ice_hw *hw, u16 *module_tlv, u16 *module_tlv_len,
428 		       u16 module_type)
429 {
430 	enum ice_status status;
431 	u16 pfa_len, pfa_ptr;
432 	u16 next_tlv;
433 
434 	status = ice_read_sr_word(hw, ICE_SR_PFA_PTR, &pfa_ptr);
435 	if (status != ICE_SUCCESS) {
436 		ice_debug(hw, ICE_DBG_INIT, "Preserved Field Array pointer.\n");
437 		return status;
438 	}
439 	status = ice_read_sr_word(hw, pfa_ptr, &pfa_len);
440 	if (status != ICE_SUCCESS) {
441 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PFA length.\n");
442 		return status;
443 	}
444 	/* Starting with first TLV after PFA length, iterate through the list
445 	 * of TLVs to find the requested one.
446 	 */
447 	next_tlv = pfa_ptr + 1;
448 	while (next_tlv < pfa_ptr + pfa_len) {
449 		u16 tlv_sub_module_type;
450 		u16 tlv_len;
451 
452 		/* Read TLV type */
453 		status = ice_read_sr_word(hw, next_tlv, &tlv_sub_module_type);
454 		if (status != ICE_SUCCESS) {
455 			ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV type.\n");
456 			break;
457 		}
458 		/* Read TLV length */
459 		status = ice_read_sr_word(hw, next_tlv + 1, &tlv_len);
460 		if (status != ICE_SUCCESS) {
461 			ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV length.\n");
462 			break;
463 		}
464 		if (tlv_sub_module_type == module_type) {
465 			if (tlv_len) {
466 				*module_tlv = next_tlv;
467 				*module_tlv_len = tlv_len;
468 				return ICE_SUCCESS;
469 			}
470 			return ICE_ERR_INVAL_SIZE;
471 		}
472 		/* Check next TLV, i.e. current TLV pointer + length + 2 words
473 		 * (for current TLV's type and length)
474 		 */
475 		next_tlv = next_tlv + tlv_len + 2;
476 	}
477 	/* Module does not exist */
478 	return ICE_ERR_DOES_NOT_EXIST;
479 }
480 
481 /**
482  * ice_read_pba_string - Reads part number string from NVM
483  * @hw: pointer to hardware structure
484  * @pba_num: stores the part number string from the NVM
485  * @pba_num_size: part number string buffer length
486  *
487  * Reads the part number string from the NVM.
488  */
489 enum ice_status
ice_read_pba_string(struct ice_hw * hw,u8 * pba_num,u32 pba_num_size)490 ice_read_pba_string(struct ice_hw *hw, u8 *pba_num, u32 pba_num_size)
491 {
492 	u16 pba_tlv, pba_tlv_len;
493 	enum ice_status status;
494 	u16 pba_word, pba_size;
495 	u16 i;
496 
497 	status = ice_get_pfa_module_tlv(hw, &pba_tlv, &pba_tlv_len,
498 					ICE_SR_PBA_BLOCK_PTR);
499 	if (status != ICE_SUCCESS) {
500 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block TLV.\n");
501 		return status;
502 	}
503 
504 	/* pba_size is the next word */
505 	status = ice_read_sr_word(hw, (pba_tlv + 2), &pba_size);
506 	if (status != ICE_SUCCESS) {
507 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Section size.\n");
508 		return status;
509 	}
510 
511 	if (pba_tlv_len < pba_size) {
512 		ice_debug(hw, ICE_DBG_INIT, "Invalid PBA Block TLV size.\n");
513 		return ICE_ERR_INVAL_SIZE;
514 	}
515 
516 	/* Subtract one to get PBA word count (PBA Size word is included in
517 	 * total size)
518 	 */
519 	pba_size--;
520 	if (pba_num_size < (((u32)pba_size * 2) + 1)) {
521 		ice_debug(hw, ICE_DBG_INIT, "Buffer too small for PBA data.\n");
522 		return ICE_ERR_PARAM;
523 	}
524 
525 	for (i = 0; i < pba_size; i++) {
526 		status = ice_read_sr_word(hw, (pba_tlv + 2 + 1) + i, &pba_word);
527 		if (status != ICE_SUCCESS) {
528 			ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block word %d.\n", i);
529 			return status;
530 		}
531 
532 		pba_num[(i * 2)] = (pba_word >> 8) & 0xFF;
533 		pba_num[(i * 2) + 1] = pba_word & 0xFF;
534 	}
535 	pba_num[(pba_size * 2)] = '\0';
536 
537 	return status;
538 }
539 
540 /**
541  * ice_get_nvm_srev - Read the security revision from the NVM CSS header
542  * @hw: pointer to the HW struct
543  * @bank: whether to read from the active or inactive flash bank
544  * @srev: storage for security revision
545  *
546  * Read the security revision out of the CSS header of the active NVM module
547  * bank.
548  */
ice_get_nvm_srev(struct ice_hw * hw,enum ice_bank_select bank,u32 * srev)549 static enum ice_status ice_get_nvm_srev(struct ice_hw *hw, enum ice_bank_select bank, u32 *srev)
550 {
551 	enum ice_status status;
552 	u16 srev_l, srev_h;
553 
554 	status = ice_read_nvm_module(hw, bank, ICE_NVM_CSS_SREV_L, &srev_l);
555 	if (status)
556 		return status;
557 
558 	status = ice_read_nvm_module(hw, bank, ICE_NVM_CSS_SREV_H, &srev_h);
559 	if (status)
560 		return status;
561 
562 	*srev = srev_h << 16 | srev_l;
563 
564 	return ICE_SUCCESS;
565 }
566 
567 /**
568  * ice_get_nvm_ver_info - Read NVM version information
569  * @hw: pointer to the HW struct
570  * @bank: whether to read from the active or inactive flash bank
571  * @nvm: pointer to NVM info structure
572  *
573  * Read the NVM EETRACK ID and map version of the main NVM image bank, filling
574  * in the nvm info structure.
575  */
576 static enum ice_status
ice_get_nvm_ver_info(struct ice_hw * hw,enum ice_bank_select bank,struct ice_nvm_info * nvm)577 ice_get_nvm_ver_info(struct ice_hw *hw, enum ice_bank_select bank, struct ice_nvm_info *nvm)
578 {
579 	u16 eetrack_lo, eetrack_hi, ver;
580 	enum ice_status status;
581 
582 	status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_DEV_STARTER_VER, &ver);
583 	if (status) {
584 		ice_debug(hw, ICE_DBG_NVM, "Failed to read DEV starter version.\n");
585 		return status;
586 	}
587 
588 	nvm->major = (ver & ICE_NVM_VER_HI_MASK) >> ICE_NVM_VER_HI_SHIFT;
589 	nvm->minor = (ver & ICE_NVM_VER_LO_MASK) >> ICE_NVM_VER_LO_SHIFT;
590 
591 	status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_EETRACK_LO, &eetrack_lo);
592 	if (status) {
593 		ice_debug(hw, ICE_DBG_NVM, "Failed to read EETRACK lo.\n");
594 		return status;
595 	}
596 	status = ice_read_nvm_sr_copy(hw, bank, ICE_SR_NVM_EETRACK_HI, &eetrack_hi);
597 	if (status) {
598 		ice_debug(hw, ICE_DBG_NVM, "Failed to read EETRACK hi.\n");
599 		return status;
600 	}
601 
602 	nvm->eetrack = (eetrack_hi << 16) | eetrack_lo;
603 
604 	status = ice_get_nvm_srev(hw, bank, &nvm->srev);
605 	if (status)
606 		ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM security revision.\n");
607 
608 	return ICE_SUCCESS;
609 }
610 
611 /**
612  * ice_get_inactive_nvm_ver - Read Option ROM version from the inactive bank
613  * @hw: pointer to the HW structure
614  * @nvm: storage for Option ROM version information
615  *
616  * Reads the NVM EETRACK ID, Map version, and security revision of the
617  * inactive NVM bank. Used to access version data for a pending update that
618  * has not yet been activated.
619  */
ice_get_inactive_nvm_ver(struct ice_hw * hw,struct ice_nvm_info * nvm)620 enum ice_status ice_get_inactive_nvm_ver(struct ice_hw *hw, struct ice_nvm_info *nvm)
621 {
622 	return ice_get_nvm_ver_info(hw, ICE_INACTIVE_FLASH_BANK, nvm);
623 }
624 
625 /**
626  * ice_get_orom_srev - Read the security revision from the OROM CSS header
627  * @hw: pointer to the HW struct
628  * @bank: whether to read from active or inactive flash module
629  * @srev: storage for security revision
630  *
631  * Read the security revision out of the CSS header of the active OROM module
632  * bank.
633  */
ice_get_orom_srev(struct ice_hw * hw,enum ice_bank_select bank,u32 * srev)634 static enum ice_status ice_get_orom_srev(struct ice_hw *hw, enum ice_bank_select bank, u32 *srev)
635 {
636 	enum ice_status status;
637 	u16 srev_l, srev_h;
638 	u32 css_start;
639 
640 	if (hw->flash.banks.orom_size < ICE_NVM_OROM_TRAILER_LENGTH) {
641 		ice_debug(hw, ICE_DBG_NVM, "Unexpected Option ROM Size of %u\n",
642 			  hw->flash.banks.orom_size);
643 		return ICE_ERR_CFG;
644 	}
645 
646 	/* calculate how far into the Option ROM the CSS header starts. Note
647 	 * that ice_read_orom_module takes a word offset so we need to
648 	 * divide by 2 here.
649 	 */
650 	css_start = (hw->flash.banks.orom_size - ICE_NVM_OROM_TRAILER_LENGTH) / 2;
651 
652 	status = ice_read_orom_module(hw, bank, css_start + ICE_NVM_CSS_SREV_L, &srev_l);
653 	if (status)
654 		return status;
655 
656 	status = ice_read_orom_module(hw, bank, css_start + ICE_NVM_CSS_SREV_H, &srev_h);
657 	if (status)
658 		return status;
659 
660 	*srev = srev_h << 16 | srev_l;
661 
662 	return ICE_SUCCESS;
663 }
664 
665 /**
666  * ice_get_orom_civd_data - Get the combo version information from Option ROM
667  * @hw: pointer to the HW struct
668  * @bank: whether to read from the active or inactive flash module
669  * @civd: storage for the Option ROM CIVD data.
670  *
671  * Searches through the Option ROM flash contents to locate the CIVD data for
672  * the image.
673  */
674 static enum ice_status
ice_get_orom_civd_data(struct ice_hw * hw,enum ice_bank_select bank,struct ice_orom_civd_info * civd)675 ice_get_orom_civd_data(struct ice_hw *hw, enum ice_bank_select bank,
676 		       struct ice_orom_civd_info *civd)
677 {
678 	struct ice_orom_civd_info tmp;
679 	enum ice_status status;
680 	u32 offset;
681 
682 	/* The CIVD section is located in the Option ROM aligned to 512 bytes.
683 	 * The first 4 bytes must contain the ASCII characters "$CIV".
684 	 * A simple modulo 256 sum of all of the bytes of the structure must
685 	 * equal 0.
686 	 */
687 	for (offset = 0; (offset + 512) <= hw->flash.banks.orom_size; offset += 512) {
688 		u8 sum = 0, i;
689 
690 		status = ice_read_flash_module(hw, bank, ICE_SR_1ST_OROM_BANK_PTR,
691 					       offset, (u8 *)&tmp, sizeof(tmp));
692 		if (status) {
693 			ice_debug(hw, ICE_DBG_NVM, "Unable to read Option ROM CIVD data\n");
694 			return status;
695 		}
696 
697 		/* Skip forward until we find a matching signature */
698 		if (memcmp("$CIV", tmp.signature, sizeof(tmp.signature)) != 0)
699 			continue;
700 
701 		/* Verify that the simple checksum is zero */
702 		for (i = 0; i < sizeof(tmp); i++)
703 			sum += ((u8 *)&tmp)[i];
704 
705 		if (sum) {
706 			ice_debug(hw, ICE_DBG_NVM, "Found CIVD data with invalid checksum of %u\n",
707 				  sum);
708 			return ICE_ERR_NVM;
709 		}
710 
711 		*civd = tmp;
712 		return ICE_SUCCESS;
713 	}
714 
715 	return ICE_ERR_NVM;
716 }
717 
718 /**
719  * ice_get_orom_ver_info - Read Option ROM version information
720  * @hw: pointer to the HW struct
721  * @bank: whether to read from the active or inactive flash module
722  * @orom: pointer to Option ROM info structure
723  *
724  * Read Option ROM version and security revision from the Option ROM flash
725  * section.
726  */
727 static enum ice_status
ice_get_orom_ver_info(struct ice_hw * hw,enum ice_bank_select bank,struct ice_orom_info * orom)728 ice_get_orom_ver_info(struct ice_hw *hw, enum ice_bank_select bank, struct ice_orom_info *orom)
729 {
730 	struct ice_orom_civd_info civd;
731 	enum ice_status status;
732 	u32 combo_ver;
733 
734 	status = ice_get_orom_civd_data(hw, bank, &civd);
735 	if (status) {
736 		ice_debug(hw, ICE_DBG_NVM, "Failed to locate valid Option ROM CIVD data\n");
737 		return status;
738 	}
739 
740 	combo_ver = LE32_TO_CPU(civd.combo_ver);
741 
742 	orom->major = (u8)((combo_ver & ICE_OROM_VER_MASK) >> ICE_OROM_VER_SHIFT);
743 	orom->patch = (u8)(combo_ver & ICE_OROM_VER_PATCH_MASK);
744 	orom->build = (u16)((combo_ver & ICE_OROM_VER_BUILD_MASK) >> ICE_OROM_VER_BUILD_SHIFT);
745 
746 	status = ice_get_orom_srev(hw, bank, &orom->srev);
747 	if (status) {
748 		ice_debug(hw, ICE_DBG_NVM, "Failed to read Option ROM security revision.\n");
749 		return status;
750 	}
751 
752 	return ICE_SUCCESS;
753 }
754 
755 /**
756  * ice_get_inactive_orom_ver - Read Option ROM version from the inactive bank
757  * @hw: pointer to the HW structure
758  * @orom: storage for Option ROM version information
759  *
760  * Reads the Option ROM version and security revision data for the inactive
761  * section of flash. Used to access version data for a pending update that has
762  * not yet been activated.
763  */
ice_get_inactive_orom_ver(struct ice_hw * hw,struct ice_orom_info * orom)764 enum ice_status ice_get_inactive_orom_ver(struct ice_hw *hw, struct ice_orom_info *orom)
765 {
766 	return ice_get_orom_ver_info(hw, ICE_INACTIVE_FLASH_BANK, orom);
767 }
768 
769 /**
770  * ice_discover_flash_size - Discover the available flash size.
771  * @hw: pointer to the HW struct
772  *
773  * The device flash could be up to 16MB in size. However, it is possible that
774  * the actual size is smaller. Use bisection to determine the accessible size
775  * of flash memory.
776  */
ice_discover_flash_size(struct ice_hw * hw)777 static enum ice_status ice_discover_flash_size(struct ice_hw *hw)
778 {
779 	u32 min_size = 0, max_size = ICE_AQC_NVM_MAX_OFFSET + 1;
780 	enum ice_status status;
781 
782 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
783 
784 	status = ice_acquire_nvm(hw, ICE_RES_READ);
785 	if (status)
786 		return status;
787 
788 	while ((max_size - min_size) > 1) {
789 		u32 offset = (max_size + min_size) / 2;
790 		u32 len = 1;
791 		u8 data;
792 
793 		status = ice_read_flat_nvm(hw, offset, &len, &data, false);
794 		if (status == ICE_ERR_AQ_ERROR &&
795 		    hw->adminq.sq_last_status == ICE_AQ_RC_EINVAL) {
796 			ice_debug(hw, ICE_DBG_NVM, "%s: New upper bound of %u bytes\n",
797 				  __func__, offset);
798 			status = ICE_SUCCESS;
799 			max_size = offset;
800 		} else if (!status) {
801 			ice_debug(hw, ICE_DBG_NVM, "%s: New lower bound of %u bytes\n",
802 				  __func__, offset);
803 			min_size = offset;
804 		} else {
805 			/* an unexpected error occurred */
806 			goto err_read_flat_nvm;
807 		}
808 	}
809 
810 	ice_debug(hw, ICE_DBG_NVM, "Predicted flash size is %u bytes\n", max_size);
811 
812 	hw->flash.flash_size = max_size;
813 
814 err_read_flat_nvm:
815 	ice_release_nvm(hw);
816 
817 	return status;
818 }
819 
820 /**
821  * ice_read_sr_pointer - Read the value of a Shadow RAM pointer word
822  * @hw: pointer to the HW structure
823  * @offset: the word offset of the Shadow RAM word to read
824  * @pointer: pointer value read from Shadow RAM
825  *
826  * Read the given Shadow RAM word, and convert it to a pointer value specified
827  * in bytes. This function assumes the specified offset is a valid pointer
828  * word.
829  *
830  * Each pointer word specifies whether it is stored in word size or 4KB
831  * sector size by using the highest bit. The reported pointer value will be in
832  * bytes, intended for flat NVM reads.
833  */
834 static enum ice_status
ice_read_sr_pointer(struct ice_hw * hw,u16 offset,u32 * pointer)835 ice_read_sr_pointer(struct ice_hw *hw, u16 offset, u32 *pointer)
836 {
837 	enum ice_status status;
838 	u16 value;
839 
840 	status = ice_read_sr_word(hw, offset, &value);
841 	if (status)
842 		return status;
843 
844 	/* Determine if the pointer is in 4KB or word units */
845 	if (value & ICE_SR_NVM_PTR_4KB_UNITS)
846 		*pointer = (value & ~ICE_SR_NVM_PTR_4KB_UNITS) * 4 * 1024;
847 	else
848 		*pointer = value * 2;
849 
850 	return ICE_SUCCESS;
851 }
852 
853 /**
854  * ice_read_sr_area_size - Read an area size from a Shadow RAM word
855  * @hw: pointer to the HW structure
856  * @offset: the word offset of the Shadow RAM to read
857  * @size: size value read from the Shadow RAM
858  *
859  * Read the given Shadow RAM word, and convert it to an area size value
860  * specified in bytes. This function assumes the specified offset is a valid
861  * area size word.
862  *
863  * Each area size word is specified in 4KB sector units. This function reports
864  * the size in bytes, intended for flat NVM reads.
865  */
866 static enum ice_status
ice_read_sr_area_size(struct ice_hw * hw,u16 offset,u32 * size)867 ice_read_sr_area_size(struct ice_hw *hw, u16 offset, u32 *size)
868 {
869 	enum ice_status status;
870 	u16 value;
871 
872 	status = ice_read_sr_word(hw, offset, &value);
873 	if (status)
874 		return status;
875 
876 	/* Area sizes are always specified in 4KB units */
877 	*size = value * 4 * 1024;
878 
879 	return ICE_SUCCESS;
880 }
881 
882 /**
883  * ice_determine_active_flash_banks - Discover active bank for each module
884  * @hw: pointer to the HW struct
885  *
886  * Read the Shadow RAM control word and determine which banks are active for
887  * the NVM, OROM, and Netlist modules. Also read and calculate the associated
888  * pointer and size. These values are then cached into the ice_flash_info
889  * structure for later use in order to calculate the correct offset to read
890  * from the active module.
891  */
892 static enum ice_status
ice_determine_active_flash_banks(struct ice_hw * hw)893 ice_determine_active_flash_banks(struct ice_hw *hw)
894 {
895 	struct ice_bank_info *banks = &hw->flash.banks;
896 	enum ice_status status;
897 	u16 ctrl_word;
898 
899 	status = ice_read_sr_word(hw, ICE_SR_NVM_CTRL_WORD, &ctrl_word);
900 	if (status) {
901 		ice_debug(hw, ICE_DBG_NVM, "Failed to read the Shadow RAM control word\n");
902 		return status;
903 	}
904 
905 	/* Check that the control word indicates validity */
906 	if ((ctrl_word & ICE_SR_CTRL_WORD_1_M) >> ICE_SR_CTRL_WORD_1_S != ICE_SR_CTRL_WORD_VALID) {
907 		ice_debug(hw, ICE_DBG_NVM, "Shadow RAM control word is invalid\n");
908 		return ICE_ERR_CFG;
909 	}
910 
911 	if (!(ctrl_word & ICE_SR_CTRL_WORD_NVM_BANK))
912 		banks->nvm_bank = ICE_1ST_FLASH_BANK;
913 	else
914 		banks->nvm_bank = ICE_2ND_FLASH_BANK;
915 
916 	if (!(ctrl_word & ICE_SR_CTRL_WORD_OROM_BANK))
917 		banks->orom_bank = ICE_1ST_FLASH_BANK;
918 	else
919 		banks->orom_bank = ICE_2ND_FLASH_BANK;
920 
921 	if (!(ctrl_word & ICE_SR_CTRL_WORD_NETLIST_BANK))
922 		banks->netlist_bank = ICE_1ST_FLASH_BANK;
923 	else
924 		banks->netlist_bank = ICE_2ND_FLASH_BANK;
925 
926 	status = ice_read_sr_pointer(hw, ICE_SR_1ST_NVM_BANK_PTR, &banks->nvm_ptr);
927 	if (status) {
928 		ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank pointer\n");
929 		return status;
930 	}
931 
932 	status = ice_read_sr_area_size(hw, ICE_SR_NVM_BANK_SIZE, &banks->nvm_size);
933 	if (status) {
934 		ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank area size\n");
935 		return status;
936 	}
937 
938 	status = ice_read_sr_pointer(hw, ICE_SR_1ST_OROM_BANK_PTR, &banks->orom_ptr);
939 	if (status) {
940 		ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank pointer\n");
941 		return status;
942 	}
943 
944 	status = ice_read_sr_area_size(hw, ICE_SR_OROM_BANK_SIZE, &banks->orom_size);
945 	if (status) {
946 		ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank area size\n");
947 		return status;
948 	}
949 
950 	status = ice_read_sr_pointer(hw, ICE_SR_NETLIST_BANK_PTR, &banks->netlist_ptr);
951 	if (status) {
952 		ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank pointer\n");
953 		return status;
954 	}
955 
956 	status = ice_read_sr_area_size(hw, ICE_SR_NETLIST_BANK_SIZE, &banks->netlist_size);
957 	if (status) {
958 		ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank area size\n");
959 		return status;
960 	}
961 
962 	return ICE_SUCCESS;
963 }
964 
965 /**
966  * ice_init_nvm - initializes NVM setting
967  * @hw: pointer to the HW struct
968  *
969  * This function reads and populates NVM settings such as Shadow RAM size,
970  * max_timeout, and blank_nvm_mode
971  */
ice_init_nvm(struct ice_hw * hw)972 enum ice_status ice_init_nvm(struct ice_hw *hw)
973 {
974 	struct ice_flash_info *flash = &hw->flash;
975 	enum ice_status status;
976 	u32 fla, gens_stat;
977 	u8 sr_size;
978 
979 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
980 
981 	/* The SR size is stored regardless of the NVM programming mode
982 	 * as the blank mode may be used in the factory line.
983 	 */
984 	gens_stat = rd32(hw, GLNVM_GENS);
985 	sr_size = (gens_stat & GLNVM_GENS_SR_SIZE_M) >> GLNVM_GENS_SR_SIZE_S;
986 
987 	/* Switching to words (sr_size contains power of 2) */
988 	flash->sr_words = BIT(sr_size) * ICE_SR_WORDS_IN_1KB;
989 
990 	/* Check if we are in the normal or blank NVM programming mode */
991 	fla = rd32(hw, GLNVM_FLA);
992 	if (fla & GLNVM_FLA_LOCKED_M) { /* Normal programming mode */
993 		flash->blank_nvm_mode = false;
994 	} else {
995 		/* Blank programming mode */
996 		flash->blank_nvm_mode = true;
997 		ice_debug(hw, ICE_DBG_NVM, "NVM init error: unsupported blank mode.\n");
998 		return ICE_ERR_NVM_BLANK_MODE;
999 	}
1000 
1001 	status = ice_discover_flash_size(hw);
1002 	if (status) {
1003 		ice_debug(hw, ICE_DBG_NVM, "NVM init error: failed to discover flash size.\n");
1004 		return status;
1005 	}
1006 
1007 	status = ice_determine_active_flash_banks(hw);
1008 	if (status) {
1009 		ice_debug(hw, ICE_DBG_NVM, "Failed to determine active flash banks.\n");
1010 		return status;
1011 	}
1012 
1013 	status = ice_get_nvm_ver_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->nvm);
1014 	if (status) {
1015 		ice_debug(hw, ICE_DBG_INIT, "Failed to read NVM info.\n");
1016 		return status;
1017 	}
1018 
1019 	status = ice_get_orom_ver_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->orom);
1020 	if (status)
1021 		ice_debug(hw, ICE_DBG_INIT, "Failed to read Option ROM info.\n");
1022 
1023 	return ICE_SUCCESS;
1024 }
1025 
1026 /**
1027  * ice_read_sr_buf - Reads Shadow RAM buf and acquire lock if necessary
1028  * @hw: pointer to the HW structure
1029  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
1030  * @words: (in) number of words to read; (out) number of words actually read
1031  * @data: words read from the Shadow RAM
1032  *
1033  * Reads 16 bit words (data buf) from the SR using the ice_read_nvm_buf_aq
1034  * method. The buf read is preceded by the NVM ownership take
1035  * and followed by the release.
1036  */
1037 enum ice_status
ice_read_sr_buf(struct ice_hw * hw,u16 offset,u16 * words,u16 * data)1038 ice_read_sr_buf(struct ice_hw *hw, u16 offset, u16 *words, u16 *data)
1039 {
1040 	enum ice_status status;
1041 
1042 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1043 	if (!status) {
1044 		status = ice_read_sr_buf_aq(hw, offset, words, data);
1045 		ice_release_nvm(hw);
1046 	}
1047 
1048 	return status;
1049 }
1050 
1051 /**
1052  * ice_nvm_validate_checksum
1053  * @hw: pointer to the HW struct
1054  *
1055  * Verify NVM PFA checksum validity (0x0706)
1056  */
ice_nvm_validate_checksum(struct ice_hw * hw)1057 enum ice_status ice_nvm_validate_checksum(struct ice_hw *hw)
1058 {
1059 	struct ice_aqc_nvm_checksum *cmd;
1060 	struct ice_aq_desc desc;
1061 	enum ice_status status;
1062 
1063 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1064 	if (status)
1065 		return status;
1066 
1067 	cmd = &desc.params.nvm_checksum;
1068 
1069 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_checksum);
1070 	cmd->flags = ICE_AQC_NVM_CHECKSUM_VERIFY;
1071 
1072 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1073 	ice_release_nvm(hw);
1074 
1075 	if (!status)
1076 		if (LE16_TO_CPU(cmd->checksum) != ICE_AQC_NVM_CHECKSUM_CORRECT)
1077 			status = ICE_ERR_NVM_CHECKSUM;
1078 
1079 	return status;
1080 }
1081 
1082 /**
1083  * ice_nvm_recalculate_checksum
1084  * @hw: pointer to the HW struct
1085  *
1086  * Recalculate NVM PFA checksum (0x0706)
1087  */
ice_nvm_recalculate_checksum(struct ice_hw * hw)1088 enum ice_status ice_nvm_recalculate_checksum(struct ice_hw *hw)
1089 {
1090 	struct ice_aqc_nvm_checksum *cmd;
1091 	struct ice_aq_desc desc;
1092 	enum ice_status status;
1093 
1094 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1095 	if (status)
1096 		return status;
1097 
1098 	cmd = &desc.params.nvm_checksum;
1099 
1100 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_checksum);
1101 	cmd->flags = ICE_AQC_NVM_CHECKSUM_RECALC;
1102 
1103 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1104 
1105 	ice_release_nvm(hw);
1106 
1107 	return status;
1108 }
1109 
1110 /**
1111  * ice_nvm_access_get_features - Return the NVM access features structure
1112  * @cmd: NVM access command to process
1113  * @data: storage for the driver NVM features
1114  *
1115  * Fill in the data section of the NVM access request with a copy of the NVM
1116  * features structure.
1117  */
1118 enum ice_status
ice_nvm_access_get_features(struct ice_nvm_access_cmd * cmd,union ice_nvm_access_data * data)1119 ice_nvm_access_get_features(struct ice_nvm_access_cmd *cmd,
1120 			    union ice_nvm_access_data *data)
1121 {
1122 	/* The provided data_size must be at least as large as our NVM
1123 	 * features structure. A larger size should not be treated as an
1124 	 * error, to allow future extensions to the features structure to
1125 	 * work on older drivers.
1126 	 */
1127 	if (cmd->data_size < sizeof(struct ice_nvm_features))
1128 		return ICE_ERR_NO_MEMORY;
1129 
1130 	/* Initialize the data buffer to zeros */
1131 	ice_memset(data, 0, cmd->data_size, ICE_NONDMA_MEM);
1132 
1133 	/* Fill in the features data */
1134 	data->drv_features.major = ICE_NVM_ACCESS_MAJOR_VER;
1135 	data->drv_features.minor = ICE_NVM_ACCESS_MINOR_VER;
1136 	data->drv_features.size = sizeof(struct ice_nvm_features);
1137 	data->drv_features.features[0] = ICE_NVM_FEATURES_0_REG_ACCESS;
1138 
1139 	return ICE_SUCCESS;
1140 }
1141 
1142 /**
1143  * ice_nvm_access_get_module - Helper function to read module value
1144  * @cmd: NVM access command structure
1145  *
1146  * Reads the module value out of the NVM access config field.
1147  */
ice_nvm_access_get_module(struct ice_nvm_access_cmd * cmd)1148 u32 ice_nvm_access_get_module(struct ice_nvm_access_cmd *cmd)
1149 {
1150 	return ((cmd->config & ICE_NVM_CFG_MODULE_M) >> ICE_NVM_CFG_MODULE_S);
1151 }
1152 
1153 /**
1154  * ice_nvm_access_get_flags - Helper function to read flags value
1155  * @cmd: NVM access command structure
1156  *
1157  * Reads the flags value out of the NVM access config field.
1158  */
ice_nvm_access_get_flags(struct ice_nvm_access_cmd * cmd)1159 u32 ice_nvm_access_get_flags(struct ice_nvm_access_cmd *cmd)
1160 {
1161 	return ((cmd->config & ICE_NVM_CFG_FLAGS_M) >> ICE_NVM_CFG_FLAGS_S);
1162 }
1163 
1164 /**
1165  * ice_nvm_access_get_adapter - Helper function to read adapter info
1166  * @cmd: NVM access command structure
1167  *
1168  * Read the adapter info value out of the NVM access config field.
1169  */
ice_nvm_access_get_adapter(struct ice_nvm_access_cmd * cmd)1170 u32 ice_nvm_access_get_adapter(struct ice_nvm_access_cmd *cmd)
1171 {
1172 	return ((cmd->config & ICE_NVM_CFG_ADAPTER_INFO_M) >>
1173 		ICE_NVM_CFG_ADAPTER_INFO_S);
1174 }
1175 
1176 /**
1177  * ice_validate_nvm_rw_reg - Check than an NVM access request is valid
1178  * @cmd: NVM access command structure
1179  *
1180  * Validates that an NVM access structure is request to read or write a valid
1181  * register offset. First validates that the module and flags are correct, and
1182  * then ensures that the register offset is one of the accepted registers.
1183  */
1184 static enum ice_status
ice_validate_nvm_rw_reg(struct ice_nvm_access_cmd * cmd)1185 ice_validate_nvm_rw_reg(struct ice_nvm_access_cmd *cmd)
1186 {
1187 	u32 module, flags, offset;
1188 	u16 i;
1189 
1190 	module = ice_nvm_access_get_module(cmd);
1191 	flags = ice_nvm_access_get_flags(cmd);
1192 	offset = cmd->offset;
1193 
1194 	/* Make sure the module and flags indicate a read/write request */
1195 	if (module != ICE_NVM_REG_RW_MODULE ||
1196 	    flags != ICE_NVM_REG_RW_FLAGS ||
1197 	    cmd->data_size != FIELD_SIZEOF(union ice_nvm_access_data, regval))
1198 		return ICE_ERR_PARAM;
1199 
1200 	switch (offset) {
1201 	case GL_HICR:
1202 	case GL_HICR_EN: /* Note, this register is read only */
1203 	case GL_FWSTS:
1204 	case GL_MNG_FWSM:
1205 	case GLGEN_CSR_DEBUG_C:
1206 	case GLGEN_RSTAT:
1207 	case GLPCI_LBARCTRL:
1208 	case GL_MNG_DEF_DEVID:
1209 	case GLNVM_GENS:
1210 	case GLNVM_FLA:
1211 	case PF_FUNC_RID:
1212 		return ICE_SUCCESS;
1213 	default:
1214 		break;
1215 	}
1216 
1217 	for (i = 0; i <= GL_HIDA_MAX_INDEX; i++)
1218 		if (offset == (u32)GL_HIDA(i))
1219 			return ICE_SUCCESS;
1220 
1221 	for (i = 0; i <= GL_HIBA_MAX_INDEX; i++)
1222 		if (offset == (u32)GL_HIBA(i))
1223 			return ICE_SUCCESS;
1224 
1225 	/* All other register offsets are not valid */
1226 	return ICE_ERR_OUT_OF_RANGE;
1227 }
1228 
1229 /**
1230  * ice_nvm_access_read - Handle an NVM read request
1231  * @hw: pointer to the HW struct
1232  * @cmd: NVM access command to process
1233  * @data: storage for the register value read
1234  *
1235  * Process an NVM access request to read a register.
1236  */
1237 enum ice_status
ice_nvm_access_read(struct ice_hw * hw,struct ice_nvm_access_cmd * cmd,union ice_nvm_access_data * data)1238 ice_nvm_access_read(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
1239 		    union ice_nvm_access_data *data)
1240 {
1241 	enum ice_status status;
1242 
1243 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1244 
1245 	/* Always initialize the output data, even on failure */
1246 	ice_memset(data, 0, cmd->data_size, ICE_NONDMA_MEM);
1247 
1248 	/* Make sure this is a valid read/write access request */
1249 	status = ice_validate_nvm_rw_reg(cmd);
1250 	if (status)
1251 		return status;
1252 
1253 	ice_debug(hw, ICE_DBG_NVM, "NVM access: reading register %08x\n",
1254 		  cmd->offset);
1255 
1256 	/* Read the register and store the contents in the data field */
1257 	data->regval = rd32(hw, cmd->offset);
1258 
1259 	return ICE_SUCCESS;
1260 }
1261 
1262 /**
1263  * ice_nvm_access_write - Handle an NVM write request
1264  * @hw: pointer to the HW struct
1265  * @cmd: NVM access command to process
1266  * @data: NVM access data to write
1267  *
1268  * Process an NVM access request to write a register.
1269  */
1270 enum ice_status
ice_nvm_access_write(struct ice_hw * hw,struct ice_nvm_access_cmd * cmd,union ice_nvm_access_data * data)1271 ice_nvm_access_write(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
1272 		     union ice_nvm_access_data *data)
1273 {
1274 	enum ice_status status;
1275 
1276 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1277 
1278 	/* Make sure this is a valid read/write access request */
1279 	status = ice_validate_nvm_rw_reg(cmd);
1280 	if (status)
1281 		return status;
1282 
1283 	/* Reject requests to write to read-only registers */
1284 	switch (cmd->offset) {
1285 	case GL_HICR_EN:
1286 	case GLGEN_RSTAT:
1287 		return ICE_ERR_OUT_OF_RANGE;
1288 	default:
1289 		break;
1290 	}
1291 
1292 	ice_debug(hw, ICE_DBG_NVM, "NVM access: writing register %08x with value %08x\n",
1293 		  cmd->offset, data->regval);
1294 
1295 	/* Write the data field to the specified register */
1296 	wr32(hw, cmd->offset, data->regval);
1297 
1298 	return ICE_SUCCESS;
1299 }
1300 
1301 /**
1302  * ice_handle_nvm_access - Handle an NVM access request
1303  * @hw: pointer to the HW struct
1304  * @cmd: NVM access command info
1305  * @data: pointer to read or return data
1306  *
1307  * Process an NVM access request. Read the command structure information and
1308  * determine if it is valid. If not, report an error indicating the command
1309  * was invalid.
1310  *
1311  * For valid commands, perform the necessary function, copying the data into
1312  * the provided data buffer.
1313  */
1314 enum ice_status
ice_handle_nvm_access(struct ice_hw * hw,struct ice_nvm_access_cmd * cmd,union ice_nvm_access_data * data)1315 ice_handle_nvm_access(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
1316 		      union ice_nvm_access_data *data)
1317 {
1318 	u32 module, flags, adapter_info;
1319 
1320 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1321 
1322 	/* Extended flags are currently reserved and must be zero */
1323 	if ((cmd->config & ICE_NVM_CFG_EXT_FLAGS_M) != 0)
1324 		return ICE_ERR_PARAM;
1325 
1326 	/* Adapter info must match the HW device ID */
1327 	adapter_info = ice_nvm_access_get_adapter(cmd);
1328 	if (adapter_info != hw->device_id)
1329 		return ICE_ERR_PARAM;
1330 
1331 	switch (cmd->command) {
1332 	case ICE_NVM_CMD_READ:
1333 		module = ice_nvm_access_get_module(cmd);
1334 		flags = ice_nvm_access_get_flags(cmd);
1335 
1336 		/* Getting the driver's NVM features structure shares the same
1337 		 * command type as reading a register. Read the config field
1338 		 * to determine if this is a request to get features.
1339 		 */
1340 		if (module == ICE_NVM_GET_FEATURES_MODULE &&
1341 		    flags == ICE_NVM_GET_FEATURES_FLAGS &&
1342 		    cmd->offset == 0)
1343 			return ice_nvm_access_get_features(cmd, data);
1344 		else
1345 			return ice_nvm_access_read(hw, cmd, data);
1346 	case ICE_NVM_CMD_WRITE:
1347 		return ice_nvm_access_write(hw, cmd, data);
1348 	default:
1349 		return ICE_ERR_PARAM;
1350 	}
1351 }
1352