1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018, Intel Corporation. */
3 
4 #include <linux/vmalloc.h>
5 
6 #include "ice_common.h"
7 
8 /**
9  * ice_aq_read_nvm
10  * @hw: pointer to the HW struct
11  * @module_typeid: module pointer location in words from the NVM beginning
12  * @offset: byte offset from the module beginning
13  * @length: length of the section to be read (in bytes from the offset)
14  * @data: command buffer (size [bytes] = length)
15  * @last_command: tells if this is the last command in a series
16  * @read_shadow_ram: tell if this is a shadow RAM read
17  * @cd: pointer to command details structure or NULL
18  *
19  * Read the NVM using the admin queue commands (0x0701)
20  */
21 static int
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)22 ice_aq_read_nvm(struct ice_hw *hw, u16 module_typeid, u32 offset, u16 length,
23 		void *data, bool last_command, bool read_shadow_ram,
24 		struct ice_sq_cd *cd)
25 {
26 	struct ice_aq_desc desc;
27 	struct ice_aqc_nvm *cmd;
28 
29 	cmd = &desc.params.nvm;
30 
31 	if (offset > ICE_AQC_NVM_MAX_OFFSET)
32 		return -EINVAL;
33 
34 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_read);
35 
36 	if (!read_shadow_ram && module_typeid == ICE_AQC_NVM_START_POINT)
37 		cmd->cmd_flags |= ICE_AQC_NVM_FLASH_ONLY;
38 
39 	/* If this is the last command in a series, set the proper flag. */
40 	if (last_command)
41 		cmd->cmd_flags |= ICE_AQC_NVM_LAST_CMD;
42 	cmd->module_typeid = cpu_to_le16(module_typeid);
43 	cmd->offset_low = cpu_to_le16(offset & 0xFFFF);
44 	cmd->offset_high = (offset >> 16) & 0xFF;
45 	cmd->length = cpu_to_le16(length);
46 
47 	return ice_aq_send_cmd(hw, &desc, data, length, cd);
48 }
49 
50 /**
51  * ice_read_flat_nvm - Read portion of NVM by flat offset
52  * @hw: pointer to the HW struct
53  * @offset: offset from beginning of NVM
54  * @length: (in) number of bytes to read; (out) number of bytes actually read
55  * @data: buffer to return data in (sized to fit the specified length)
56  * @read_shadow_ram: if true, read from shadow RAM instead of NVM
57  *
58  * Reads a portion of the NVM, as a flat memory space. This function correctly
59  * breaks read requests across Shadow RAM sectors and ensures that no single
60  * read request exceeds the maximum 4KB read for a single AdminQ command.
61  *
62  * Returns a status code on failure. Note that the data pointer may be
63  * partially updated if some reads succeed before a failure.
64  */
65 int
ice_read_flat_nvm(struct ice_hw * hw,u32 offset,u32 * length,u8 * data,bool read_shadow_ram)66 ice_read_flat_nvm(struct ice_hw *hw, u32 offset, u32 *length, u8 *data,
67 		  bool read_shadow_ram)
68 {
69 	u32 inlen = *length;
70 	u32 bytes_read = 0;
71 	bool last_cmd;
72 	int status;
73 
74 	*length = 0;
75 
76 	/* Verify the length of the read if this is for the Shadow RAM */
77 	if (read_shadow_ram && ((offset + inlen) > (hw->flash.sr_words * 2u))) {
78 		ice_debug(hw, ICE_DBG_NVM, "NVM error: requested offset is beyond Shadow RAM limit\n");
79 		return -EINVAL;
80 	}
81 
82 	do {
83 		u32 read_size, sector_offset;
84 
85 		/* ice_aq_read_nvm cannot read more than 4KB at a time.
86 		 * Additionally, a read from the Shadow RAM may not cross over
87 		 * a sector boundary. Conveniently, the sector size is also
88 		 * 4KB.
89 		 */
90 		sector_offset = offset % ICE_AQ_MAX_BUF_LEN;
91 		read_size = min_t(u32, ICE_AQ_MAX_BUF_LEN - sector_offset,
92 				  inlen - bytes_read);
93 
94 		last_cmd = !(bytes_read + read_size < inlen);
95 
96 		status = ice_aq_read_nvm(hw, ICE_AQC_NVM_START_POINT,
97 					 offset, read_size,
98 					 data + bytes_read, last_cmd,
99 					 read_shadow_ram, NULL);
100 		if (status)
101 			break;
102 
103 		bytes_read += read_size;
104 		offset += read_size;
105 	} while (!last_cmd);
106 
107 	*length = bytes_read;
108 	return status;
109 }
110 
111 /**
112  * ice_aq_update_nvm
113  * @hw: pointer to the HW struct
114  * @module_typeid: module pointer location in words from the NVM beginning
115  * @offset: byte offset from the module beginning
116  * @length: length of the section to be written (in bytes from the offset)
117  * @data: command buffer (size [bytes] = length)
118  * @last_command: tells if this is the last command in a series
119  * @command_flags: command parameters
120  * @cd: pointer to command details structure or NULL
121  *
122  * Update the NVM using the admin queue commands (0x0703)
123  */
124 int
ice_aq_update_nvm(struct ice_hw * hw,u16 module_typeid,u32 offset,u16 length,void * data,bool last_command,u8 command_flags,struct ice_sq_cd * cd)125 ice_aq_update_nvm(struct ice_hw *hw, u16 module_typeid, u32 offset,
126 		  u16 length, void *data, bool last_command, u8 command_flags,
127 		  struct ice_sq_cd *cd)
128 {
129 	struct ice_aq_desc desc;
130 	struct ice_aqc_nvm *cmd;
131 
132 	cmd = &desc.params.nvm;
133 
134 	/* In offset the highest byte must be zeroed. */
135 	if (offset & 0xFF000000)
136 		return -EINVAL;
137 
138 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_write);
139 
140 	cmd->cmd_flags |= command_flags;
141 
142 	/* If this is the last command in a series, set the proper flag. */
143 	if (last_command)
144 		cmd->cmd_flags |= ICE_AQC_NVM_LAST_CMD;
145 	cmd->module_typeid = cpu_to_le16(module_typeid);
146 	cmd->offset_low = cpu_to_le16(offset & 0xFFFF);
147 	cmd->offset_high = (offset >> 16) & 0xFF;
148 	cmd->length = cpu_to_le16(length);
149 
150 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
151 
152 	return ice_aq_send_cmd(hw, &desc, data, length, cd);
153 }
154 
155 /**
156  * ice_aq_erase_nvm
157  * @hw: pointer to the HW struct
158  * @module_typeid: module pointer location in words from the NVM beginning
159  * @cd: pointer to command details structure or NULL
160  *
161  * Erase the NVM sector using the admin queue commands (0x0702)
162  */
ice_aq_erase_nvm(struct ice_hw * hw,u16 module_typeid,struct ice_sq_cd * cd)163 int ice_aq_erase_nvm(struct ice_hw *hw, u16 module_typeid, struct ice_sq_cd *cd)
164 {
165 	struct ice_aq_desc desc;
166 	struct ice_aqc_nvm *cmd;
167 
168 	cmd = &desc.params.nvm;
169 
170 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_erase);
171 
172 	cmd->module_typeid = cpu_to_le16(module_typeid);
173 	cmd->length = cpu_to_le16(ICE_AQC_NVM_ERASE_LEN);
174 	cmd->offset_low = 0;
175 	cmd->offset_high = 0;
176 
177 	return ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
178 }
179 
180 /**
181  * ice_read_sr_word_aq - Reads Shadow RAM via AQ
182  * @hw: pointer to the HW structure
183  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
184  * @data: word read from the Shadow RAM
185  *
186  * Reads one 16 bit word from the Shadow RAM using ice_read_flat_nvm.
187  */
ice_read_sr_word_aq(struct ice_hw * hw,u16 offset,u16 * data)188 static int ice_read_sr_word_aq(struct ice_hw *hw, u16 offset, u16 *data)
189 {
190 	u32 bytes = sizeof(u16);
191 	__le16 data_local;
192 	int status;
193 
194 	/* Note that ice_read_flat_nvm takes into account the 4Kb AdminQ and
195 	 * Shadow RAM sector restrictions necessary when reading from the NVM.
196 	 */
197 	status = ice_read_flat_nvm(hw, offset * sizeof(u16), &bytes,
198 				   (__force u8 *)&data_local, true);
199 	if (status)
200 		return status;
201 
202 	*data = le16_to_cpu(data_local);
203 	return 0;
204 }
205 
206 /**
207  * ice_acquire_nvm - Generic request for acquiring the NVM ownership
208  * @hw: pointer to the HW structure
209  * @access: NVM access type (read or write)
210  *
211  * This function will request NVM ownership.
212  */
ice_acquire_nvm(struct ice_hw * hw,enum ice_aq_res_access_type access)213 int ice_acquire_nvm(struct ice_hw *hw, enum ice_aq_res_access_type access)
214 {
215 	if (hw->flash.blank_nvm_mode)
216 		return 0;
217 
218 	return ice_acquire_res(hw, ICE_NVM_RES_ID, access, ICE_NVM_TIMEOUT);
219 }
220 
221 /**
222  * ice_release_nvm - Generic request for releasing the NVM ownership
223  * @hw: pointer to the HW structure
224  *
225  * This function will release NVM ownership.
226  */
ice_release_nvm(struct ice_hw * hw)227 void ice_release_nvm(struct ice_hw *hw)
228 {
229 	if (hw->flash.blank_nvm_mode)
230 		return;
231 
232 	ice_release_res(hw, ICE_NVM_RES_ID);
233 }
234 
235 /**
236  * ice_get_flash_bank_offset - Get offset into requested flash bank
237  * @hw: pointer to the HW structure
238  * @bank: whether to read from the active or inactive flash bank
239  * @module: the module to read from
240  *
241  * Based on the module, lookup the module offset from the beginning of the
242  * flash.
243  *
244  * Returns the flash offset. Note that a value of zero is invalid and must be
245  * treated as an error.
246  */
ice_get_flash_bank_offset(struct ice_hw * hw,enum ice_bank_select bank,u16 module)247 static u32 ice_get_flash_bank_offset(struct ice_hw *hw, enum ice_bank_select bank, u16 module)
248 {
249 	struct ice_bank_info *banks = &hw->flash.banks;
250 	enum ice_flash_bank active_bank;
251 	bool second_bank_active;
252 	u32 offset, size;
253 
254 	switch (module) {
255 	case ICE_SR_1ST_NVM_BANK_PTR:
256 		offset = banks->nvm_ptr;
257 		size = banks->nvm_size;
258 		active_bank = banks->nvm_bank;
259 		break;
260 	case ICE_SR_1ST_OROM_BANK_PTR:
261 		offset = banks->orom_ptr;
262 		size = banks->orom_size;
263 		active_bank = banks->orom_bank;
264 		break;
265 	case ICE_SR_NETLIST_BANK_PTR:
266 		offset = banks->netlist_ptr;
267 		size = banks->netlist_size;
268 		active_bank = banks->netlist_bank;
269 		break;
270 	default:
271 		ice_debug(hw, ICE_DBG_NVM, "Unexpected value for flash module: 0x%04x\n", module);
272 		return 0;
273 	}
274 
275 	switch (active_bank) {
276 	case ICE_1ST_FLASH_BANK:
277 		second_bank_active = false;
278 		break;
279 	case ICE_2ND_FLASH_BANK:
280 		second_bank_active = true;
281 		break;
282 	default:
283 		ice_debug(hw, ICE_DBG_NVM, "Unexpected value for active flash bank: %u\n",
284 			  active_bank);
285 		return 0;
286 	}
287 
288 	/* The second flash bank is stored immediately following the first
289 	 * bank. Based on whether the 1st or 2nd bank is active, and whether
290 	 * we want the active or inactive bank, calculate the desired offset.
291 	 */
292 	switch (bank) {
293 	case ICE_ACTIVE_FLASH_BANK:
294 		return offset + (second_bank_active ? size : 0);
295 	case ICE_INACTIVE_FLASH_BANK:
296 		return offset + (second_bank_active ? 0 : size);
297 	}
298 
299 	ice_debug(hw, ICE_DBG_NVM, "Unexpected value for flash bank selection: %u\n", bank);
300 	return 0;
301 }
302 
303 /**
304  * ice_read_flash_module - Read a word from one of the main NVM modules
305  * @hw: pointer to the HW structure
306  * @bank: which bank of the module to read
307  * @module: the module to read
308  * @offset: the offset into the module in bytes
309  * @data: storage for the word read from the flash
310  * @length: bytes of data to read
311  *
312  * Read data from the specified flash module. The bank parameter indicates
313  * whether or not to read from the active bank or the inactive bank of that
314  * module.
315  *
316  * The word will be read using flat NVM access, and relies on the
317  * hw->flash.banks data being setup by ice_determine_active_flash_banks()
318  * during initialization.
319  */
320 static int
ice_read_flash_module(struct ice_hw * hw,enum ice_bank_select bank,u16 module,u32 offset,u8 * data,u32 length)321 ice_read_flash_module(struct ice_hw *hw, enum ice_bank_select bank, u16 module,
322 		      u32 offset, u8 *data, u32 length)
323 {
324 	int status;
325 	u32 start;
326 
327 	start = ice_get_flash_bank_offset(hw, bank, module);
328 	if (!start) {
329 		ice_debug(hw, ICE_DBG_NVM, "Unable to calculate flash bank offset for module 0x%04x\n",
330 			  module);
331 		return -EINVAL;
332 	}
333 
334 	status = ice_acquire_nvm(hw, ICE_RES_READ);
335 	if (status)
336 		return status;
337 
338 	status = ice_read_flat_nvm(hw, start + offset, &length, data, false);
339 
340 	ice_release_nvm(hw);
341 
342 	return status;
343 }
344 
345 /**
346  * ice_read_nvm_module - Read from the active main NVM module
347  * @hw: pointer to the HW structure
348  * @bank: whether to read from active or inactive NVM module
349  * @offset: offset into the NVM module to read, in words
350  * @data: storage for returned word value
351  *
352  * Read the specified word from the active NVM module. This includes the CSS
353  * header at the start of the NVM module.
354  */
355 static int
ice_read_nvm_module(struct ice_hw * hw,enum ice_bank_select bank,u32 offset,u16 * data)356 ice_read_nvm_module(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
357 {
358 	__le16 data_local;
359 	int status;
360 
361 	status = ice_read_flash_module(hw, bank, ICE_SR_1ST_NVM_BANK_PTR, offset * sizeof(u16),
362 				       (__force u8 *)&data_local, sizeof(u16));
363 	if (!status)
364 		*data = le16_to_cpu(data_local);
365 
366 	return status;
367 }
368 
369 /**
370  * ice_read_nvm_sr_copy - Read a word from the Shadow RAM copy in the NVM bank
371  * @hw: pointer to the HW structure
372  * @bank: whether to read from the active or inactive NVM module
373  * @offset: offset into the Shadow RAM copy to read, in words
374  * @data: storage for returned word value
375  *
376  * Read the specified word from the copy of the Shadow RAM found in the
377  * specified NVM module.
378  */
379 static int
ice_read_nvm_sr_copy(struct ice_hw * hw,enum ice_bank_select bank,u32 offset,u16 * data)380 ice_read_nvm_sr_copy(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
381 {
382 	return ice_read_nvm_module(hw, bank, ICE_NVM_SR_COPY_WORD_OFFSET + offset, data);
383 }
384 
385 /**
386  * ice_read_netlist_module - Read data from the netlist module area
387  * @hw: pointer to the HW structure
388  * @bank: whether to read from the active or inactive module
389  * @offset: offset into the netlist to read from
390  * @data: storage for returned word value
391  *
392  * Read a word from the specified netlist bank.
393  */
394 static int
ice_read_netlist_module(struct ice_hw * hw,enum ice_bank_select bank,u32 offset,u16 * data)395 ice_read_netlist_module(struct ice_hw *hw, enum ice_bank_select bank, u32 offset, u16 *data)
396 {
397 	__le16 data_local;
398 	int status;
399 
400 	status = ice_read_flash_module(hw, bank, ICE_SR_NETLIST_BANK_PTR, offset * sizeof(u16),
401 				       (__force u8 *)&data_local, sizeof(u16));
402 	if (!status)
403 		*data = le16_to_cpu(data_local);
404 
405 	return status;
406 }
407 
408 /**
409  * ice_read_sr_word - Reads Shadow RAM word and acquire NVM if necessary
410  * @hw: pointer to the HW structure
411  * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
412  * @data: word read from the Shadow RAM
413  *
414  * Reads one 16 bit word from the Shadow RAM using the ice_read_sr_word_aq.
415  */
ice_read_sr_word(struct ice_hw * hw,u16 offset,u16 * data)416 int ice_read_sr_word(struct ice_hw *hw, u16 offset, u16 *data)
417 {
418 	int status;
419 
420 	status = ice_acquire_nvm(hw, ICE_RES_READ);
421 	if (!status) {
422 		status = ice_read_sr_word_aq(hw, offset, data);
423 		ice_release_nvm(hw);
424 	}
425 
426 	return status;
427 }
428 
429 /**
430  * ice_get_pfa_module_tlv - Reads sub module TLV from NVM PFA
431  * @hw: pointer to hardware structure
432  * @module_tlv: pointer to module TLV to return
433  * @module_tlv_len: pointer to module TLV length to return
434  * @module_type: module type requested
435  *
436  * Finds the requested sub module TLV type from the Preserved Field
437  * Area (PFA) and returns the TLV pointer and length. The caller can
438  * use these to read the variable length TLV value.
439  */
440 int
ice_get_pfa_module_tlv(struct ice_hw * hw,u16 * module_tlv,u16 * module_tlv_len,u16 module_type)441 ice_get_pfa_module_tlv(struct ice_hw *hw, u16 *module_tlv, u16 *module_tlv_len,
442 		       u16 module_type)
443 {
444 	u16 pfa_len, pfa_ptr, next_tlv, max_tlv;
445 	int status;
446 
447 	status = ice_read_sr_word(hw, ICE_SR_PFA_PTR, &pfa_ptr);
448 	if (status) {
449 		ice_debug(hw, ICE_DBG_INIT, "Preserved Field Array pointer.\n");
450 		return status;
451 	}
452 	status = ice_read_sr_word(hw, pfa_ptr, &pfa_len);
453 	if (status) {
454 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PFA length.\n");
455 		return status;
456 	}
457 
458 	/* The Preserved Fields Area contains a sequence of Type-Length-Value
459 	 * structures which define its contents. The PFA length includes all
460 	 * of the TLVs, plus the initial length word itself, *and* one final
461 	 * word at the end after all of the TLVs.
462 	 */
463 	if (check_add_overflow(pfa_ptr, pfa_len - 1, &max_tlv)) {
464 		dev_warn(ice_hw_to_dev(hw), "PFA starts at offset %u. PFA length of %u caused 16-bit arithmetic overflow.\n",
465 			 pfa_ptr, pfa_len);
466 		return -EINVAL;
467 	}
468 
469 	/* Starting with first TLV after PFA length, iterate through the list
470 	 * of TLVs to find the requested one.
471 	 */
472 	next_tlv = pfa_ptr + 1;
473 	while (next_tlv < max_tlv) {
474 		u16 tlv_sub_module_type;
475 		u16 tlv_len;
476 
477 		/* Read TLV type */
478 		status = ice_read_sr_word(hw, next_tlv, &tlv_sub_module_type);
479 		if (status) {
480 			ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV type.\n");
481 			break;
482 		}
483 		/* Read TLV length */
484 		status = ice_read_sr_word(hw, next_tlv + 1, &tlv_len);
485 		if (status) {
486 			ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV length.\n");
487 			break;
488 		}
489 		if (tlv_sub_module_type == module_type) {
490 			if (tlv_len) {
491 				*module_tlv = next_tlv;
492 				*module_tlv_len = tlv_len;
493 				return 0;
494 			}
495 			return -EINVAL;
496 		}
497 
498 		if (check_add_overflow(next_tlv, 2, &next_tlv) ||
499 		    check_add_overflow(next_tlv, tlv_len, &next_tlv)) {
500 			dev_warn(ice_hw_to_dev(hw), "TLV of type %u and length 0x%04x caused 16-bit arithmetic overflow. The PFA starts at 0x%04x and has length of 0x%04x\n",
501 				 tlv_sub_module_type, tlv_len, pfa_ptr, pfa_len);
502 			return -EINVAL;
503 		}
504 	}
505 	/* Module does not exist */
506 	return -ENOENT;
507 }
508 
509 /**
510  * ice_read_pba_string - Reads part number string from NVM
511  * @hw: pointer to hardware structure
512  * @pba_num: stores the part number string from the NVM
513  * @pba_num_size: part number string buffer length
514  *
515  * Reads the part number string from the NVM.
516  */
ice_read_pba_string(struct ice_hw * hw,u8 * pba_num,u32 pba_num_size)517 int ice_read_pba_string(struct ice_hw *hw, u8 *pba_num, u32 pba_num_size)
518 {
519 	u16 pba_tlv, pba_tlv_len;
520 	u16 pba_word, pba_size;
521 	int status;
522 	u16 i;
523 
524 	status = ice_get_pfa_module_tlv(hw, &pba_tlv, &pba_tlv_len,
525 					ICE_SR_PBA_BLOCK_PTR);
526 	if (status) {
527 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block TLV.\n");
528 		return status;
529 	}
530 
531 	/* pba_size is the next word */
532 	status = ice_read_sr_word(hw, (pba_tlv + 2), &pba_size);
533 	if (status) {
534 		ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Section size.\n");
535 		return status;
536 	}
537 
538 	if (pba_tlv_len < pba_size) {
539 		ice_debug(hw, ICE_DBG_INIT, "Invalid PBA Block TLV size.\n");
540 		return -EINVAL;
541 	}
542 
543 	/* Subtract one to get PBA word count (PBA Size word is included in
544 	 * total size)
545 	 */
546 	pba_size--;
547 	if (pba_num_size < (((u32)pba_size * 2) + 1)) {
548 		ice_debug(hw, ICE_DBG_INIT, "Buffer too small for PBA data.\n");
549 		return -EINVAL;
550 	}
551 
552 	for (i = 0; i < pba_size; i++) {
553 		status = ice_read_sr_word(hw, (pba_tlv + 2 + 1) + i, &pba_word);
554 		if (status) {
555 			ice_debug(hw, ICE_DBG_INIT, "Failed to read PBA Block word %d.\n", i);
556 			return status;
557 		}
558 
559 		pba_num[(i * 2)] = (pba_word >> 8) & 0xFF;
560 		pba_num[(i * 2) + 1] = pba_word & 0xFF;
561 	}
562 	pba_num[(pba_size * 2)] = '\0';
563 
564 	return status;
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 int
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 	int 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 	return 0;
605 }
606 
607 /**
608  * ice_get_inactive_nvm_ver - Read Option ROM version from the inactive bank
609  * @hw: pointer to the HW structure
610  * @nvm: storage for Option ROM version information
611  *
612  * Reads the NVM EETRACK ID, Map version, and security revision of the
613  * inactive NVM bank. Used to access version data for a pending update that
614  * has not yet been activated.
615  */
ice_get_inactive_nvm_ver(struct ice_hw * hw,struct ice_nvm_info * nvm)616 int ice_get_inactive_nvm_ver(struct ice_hw *hw, struct ice_nvm_info *nvm)
617 {
618 	return ice_get_nvm_ver_info(hw, ICE_INACTIVE_FLASH_BANK, nvm);
619 }
620 
621 /**
622  * ice_get_orom_civd_data - Get the combo version information from Option ROM
623  * @hw: pointer to the HW struct
624  * @bank: whether to read from the active or inactive flash module
625  * @civd: storage for the Option ROM CIVD data.
626  *
627  * Searches through the Option ROM flash contents to locate the CIVD data for
628  * the image.
629  */
630 static int
ice_get_orom_civd_data(struct ice_hw * hw,enum ice_bank_select bank,struct ice_orom_civd_info * civd)631 ice_get_orom_civd_data(struct ice_hw *hw, enum ice_bank_select bank,
632 		       struct ice_orom_civd_info *civd)
633 {
634 	u8 *orom_data;
635 	int status;
636 	u32 offset;
637 
638 	/* The CIVD section is located in the Option ROM aligned to 512 bytes.
639 	 * The first 4 bytes must contain the ASCII characters "$CIV".
640 	 * A simple modulo 256 sum of all of the bytes of the structure must
641 	 * equal 0.
642 	 *
643 	 * The exact location is unknown and varies between images but is
644 	 * usually somewhere in the middle of the bank. We need to scan the
645 	 * Option ROM bank to locate it.
646 	 *
647 	 * It's significantly faster to read the entire Option ROM up front
648 	 * using the maximum page size, than to read each possible location
649 	 * with a separate firmware command.
650 	 */
651 	orom_data = vzalloc(hw->flash.banks.orom_size);
652 	if (!orom_data)
653 		return -ENOMEM;
654 
655 	status = ice_read_flash_module(hw, bank, ICE_SR_1ST_OROM_BANK_PTR, 0,
656 				       orom_data, hw->flash.banks.orom_size);
657 	if (status) {
658 		vfree(orom_data);
659 		ice_debug(hw, ICE_DBG_NVM, "Unable to read Option ROM data\n");
660 		return status;
661 	}
662 
663 	/* Scan the memory buffer to locate the CIVD data section */
664 	for (offset = 0; (offset + 512) <= hw->flash.banks.orom_size; offset += 512) {
665 		struct ice_orom_civd_info *tmp;
666 		u8 sum = 0, i;
667 
668 		tmp = (struct ice_orom_civd_info *)&orom_data[offset];
669 
670 		/* Skip forward until we find a matching signature */
671 		if (memcmp("$CIV", tmp->signature, sizeof(tmp->signature)) != 0)
672 			continue;
673 
674 		ice_debug(hw, ICE_DBG_NVM, "Found CIVD section at offset %u\n",
675 			  offset);
676 
677 		/* Verify that the simple checksum is zero */
678 		for (i = 0; i < sizeof(*tmp); i++)
679 			sum += ((u8 *)tmp)[i];
680 
681 		if (sum) {
682 			ice_debug(hw, ICE_DBG_NVM, "Found CIVD data with invalid checksum of %u\n",
683 				  sum);
684 			goto err_invalid_checksum;
685 		}
686 
687 		*civd = *tmp;
688 		vfree(orom_data);
689 		return 0;
690 	}
691 
692 	ice_debug(hw, ICE_DBG_NVM, "Unable to locate CIVD data within the Option ROM\n");
693 
694 err_invalid_checksum:
695 	vfree(orom_data);
696 	return -EIO;
697 }
698 
699 /**
700  * ice_get_orom_ver_info - Read Option ROM version information
701  * @hw: pointer to the HW struct
702  * @bank: whether to read from the active or inactive flash module
703  * @orom: pointer to Option ROM info structure
704  *
705  * Read Option ROM version and security revision from the Option ROM flash
706  * section.
707  */
708 static int
ice_get_orom_ver_info(struct ice_hw * hw,enum ice_bank_select bank,struct ice_orom_info * orom)709 ice_get_orom_ver_info(struct ice_hw *hw, enum ice_bank_select bank, struct ice_orom_info *orom)
710 {
711 	struct ice_orom_civd_info civd;
712 	u32 combo_ver;
713 	int status;
714 
715 	status = ice_get_orom_civd_data(hw, bank, &civd);
716 	if (status) {
717 		ice_debug(hw, ICE_DBG_NVM, "Failed to locate valid Option ROM CIVD data\n");
718 		return status;
719 	}
720 
721 	combo_ver = le32_to_cpu(civd.combo_ver);
722 
723 	orom->major = (u8)((combo_ver & ICE_OROM_VER_MASK) >> ICE_OROM_VER_SHIFT);
724 	orom->patch = (u8)(combo_ver & ICE_OROM_VER_PATCH_MASK);
725 	orom->build = (u16)((combo_ver & ICE_OROM_VER_BUILD_MASK) >> ICE_OROM_VER_BUILD_SHIFT);
726 
727 	return 0;
728 }
729 
730 /**
731  * ice_get_inactive_orom_ver - Read Option ROM version from the inactive bank
732  * @hw: pointer to the HW structure
733  * @orom: storage for Option ROM version information
734  *
735  * Reads the Option ROM version and security revision data for the inactive
736  * section of flash. Used to access version data for a pending update that has
737  * not yet been activated.
738  */
ice_get_inactive_orom_ver(struct ice_hw * hw,struct ice_orom_info * orom)739 int ice_get_inactive_orom_ver(struct ice_hw *hw, struct ice_orom_info *orom)
740 {
741 	return ice_get_orom_ver_info(hw, ICE_INACTIVE_FLASH_BANK, orom);
742 }
743 
744 /**
745  * ice_get_netlist_info
746  * @hw: pointer to the HW struct
747  * @bank: whether to read from the active or inactive flash bank
748  * @netlist: pointer to netlist version info structure
749  *
750  * Get the netlist version information from the requested bank. Reads the Link
751  * Topology section to find the Netlist ID block and extract the relevant
752  * information into the netlist version structure.
753  */
754 static int
ice_get_netlist_info(struct ice_hw * hw,enum ice_bank_select bank,struct ice_netlist_info * netlist)755 ice_get_netlist_info(struct ice_hw *hw, enum ice_bank_select bank,
756 		     struct ice_netlist_info *netlist)
757 {
758 	u16 module_id, length, node_count, i;
759 	u16 *id_blk;
760 	int status;
761 
762 	status = ice_read_netlist_module(hw, bank, ICE_NETLIST_TYPE_OFFSET, &module_id);
763 	if (status)
764 		return status;
765 
766 	if (module_id != ICE_NETLIST_LINK_TOPO_MOD_ID) {
767 		ice_debug(hw, ICE_DBG_NVM, "Expected netlist module_id ID of 0x%04x, but got 0x%04x\n",
768 			  ICE_NETLIST_LINK_TOPO_MOD_ID, module_id);
769 		return -EIO;
770 	}
771 
772 	status = ice_read_netlist_module(hw, bank, ICE_LINK_TOPO_MODULE_LEN, &length);
773 	if (status)
774 		return status;
775 
776 	/* sanity check that we have at least enough words to store the netlist ID block */
777 	if (length < ICE_NETLIST_ID_BLK_SIZE) {
778 		ice_debug(hw, ICE_DBG_NVM, "Netlist Link Topology module too small. Expected at least %u words, but got %u words.\n",
779 			  ICE_NETLIST_ID_BLK_SIZE, length);
780 		return -EIO;
781 	}
782 
783 	status = ice_read_netlist_module(hw, bank, ICE_LINK_TOPO_NODE_COUNT, &node_count);
784 	if (status)
785 		return status;
786 	node_count &= ICE_LINK_TOPO_NODE_COUNT_M;
787 
788 	id_blk = kcalloc(ICE_NETLIST_ID_BLK_SIZE, sizeof(*id_blk), GFP_KERNEL);
789 	if (!id_blk)
790 		return -ENOMEM;
791 
792 	/* Read out the entire Netlist ID Block at once. */
793 	status = ice_read_flash_module(hw, bank, ICE_SR_NETLIST_BANK_PTR,
794 				       ICE_NETLIST_ID_BLK_OFFSET(node_count) * sizeof(u16),
795 				       (u8 *)id_blk, ICE_NETLIST_ID_BLK_SIZE * sizeof(u16));
796 	if (status)
797 		goto exit_error;
798 
799 	for (i = 0; i < ICE_NETLIST_ID_BLK_SIZE; i++)
800 		id_blk[i] = le16_to_cpu(((__force __le16 *)id_blk)[i]);
801 
802 	netlist->major = id_blk[ICE_NETLIST_ID_BLK_MAJOR_VER_HIGH] << 16 |
803 			 id_blk[ICE_NETLIST_ID_BLK_MAJOR_VER_LOW];
804 	netlist->minor = id_blk[ICE_NETLIST_ID_BLK_MINOR_VER_HIGH] << 16 |
805 			 id_blk[ICE_NETLIST_ID_BLK_MINOR_VER_LOW];
806 	netlist->type = id_blk[ICE_NETLIST_ID_BLK_TYPE_HIGH] << 16 |
807 			id_blk[ICE_NETLIST_ID_BLK_TYPE_LOW];
808 	netlist->rev = id_blk[ICE_NETLIST_ID_BLK_REV_HIGH] << 16 |
809 		       id_blk[ICE_NETLIST_ID_BLK_REV_LOW];
810 	netlist->cust_ver = id_blk[ICE_NETLIST_ID_BLK_CUST_VER];
811 	/* Read the left most 4 bytes of SHA */
812 	netlist->hash = id_blk[ICE_NETLIST_ID_BLK_SHA_HASH_WORD(15)] << 16 |
813 			id_blk[ICE_NETLIST_ID_BLK_SHA_HASH_WORD(14)];
814 
815 exit_error:
816 	kfree(id_blk);
817 
818 	return status;
819 }
820 
821 /**
822  * ice_get_inactive_netlist_ver
823  * @hw: pointer to the HW struct
824  * @netlist: pointer to netlist version info structure
825  *
826  * Read the netlist version data from the inactive netlist bank. Used to
827  * extract version data of a pending flash update in order to display the
828  * version data.
829  */
ice_get_inactive_netlist_ver(struct ice_hw * hw,struct ice_netlist_info * netlist)830 int ice_get_inactive_netlist_ver(struct ice_hw *hw, struct ice_netlist_info *netlist)
831 {
832 	return ice_get_netlist_info(hw, ICE_INACTIVE_FLASH_BANK, netlist);
833 }
834 
835 /**
836  * ice_discover_flash_size - Discover the available flash size.
837  * @hw: pointer to the HW struct
838  *
839  * The device flash could be up to 16MB in size. However, it is possible that
840  * the actual size is smaller. Use bisection to determine the accessible size
841  * of flash memory.
842  */
ice_discover_flash_size(struct ice_hw * hw)843 static int ice_discover_flash_size(struct ice_hw *hw)
844 {
845 	u32 min_size = 0, max_size = ICE_AQC_NVM_MAX_OFFSET + 1;
846 	int status;
847 
848 	status = ice_acquire_nvm(hw, ICE_RES_READ);
849 	if (status)
850 		return status;
851 
852 	while ((max_size - min_size) > 1) {
853 		u32 offset = (max_size + min_size) / 2;
854 		u32 len = 1;
855 		u8 data;
856 
857 		status = ice_read_flat_nvm(hw, offset, &len, &data, false);
858 		if (status == -EIO &&
859 		    hw->adminq.sq_last_status == ICE_AQ_RC_EINVAL) {
860 			ice_debug(hw, ICE_DBG_NVM, "%s: New upper bound of %u bytes\n",
861 				  __func__, offset);
862 			status = 0;
863 			max_size = offset;
864 		} else if (!status) {
865 			ice_debug(hw, ICE_DBG_NVM, "%s: New lower bound of %u bytes\n",
866 				  __func__, offset);
867 			min_size = offset;
868 		} else {
869 			/* an unexpected error occurred */
870 			goto err_read_flat_nvm;
871 		}
872 	}
873 
874 	ice_debug(hw, ICE_DBG_NVM, "Predicted flash size is %u bytes\n", max_size);
875 
876 	hw->flash.flash_size = max_size;
877 
878 err_read_flat_nvm:
879 	ice_release_nvm(hw);
880 
881 	return status;
882 }
883 
884 /**
885  * ice_read_sr_pointer - Read the value of a Shadow RAM pointer word
886  * @hw: pointer to the HW structure
887  * @offset: the word offset of the Shadow RAM word to read
888  * @pointer: pointer value read from Shadow RAM
889  *
890  * Read the given Shadow RAM word, and convert it to a pointer value specified
891  * in bytes. This function assumes the specified offset is a valid pointer
892  * word.
893  *
894  * Each pointer word specifies whether it is stored in word size or 4KB
895  * sector size by using the highest bit. The reported pointer value will be in
896  * bytes, intended for flat NVM reads.
897  */
ice_read_sr_pointer(struct ice_hw * hw,u16 offset,u32 * pointer)898 static int ice_read_sr_pointer(struct ice_hw *hw, u16 offset, u32 *pointer)
899 {
900 	int status;
901 	u16 value;
902 
903 	status = ice_read_sr_word(hw, offset, &value);
904 	if (status)
905 		return status;
906 
907 	/* Determine if the pointer is in 4KB or word units */
908 	if (value & ICE_SR_NVM_PTR_4KB_UNITS)
909 		*pointer = (value & ~ICE_SR_NVM_PTR_4KB_UNITS) * 4 * 1024;
910 	else
911 		*pointer = value * 2;
912 
913 	return 0;
914 }
915 
916 /**
917  * ice_read_sr_area_size - Read an area size from a Shadow RAM word
918  * @hw: pointer to the HW structure
919  * @offset: the word offset of the Shadow RAM to read
920  * @size: size value read from the Shadow RAM
921  *
922  * Read the given Shadow RAM word, and convert it to an area size value
923  * specified in bytes. This function assumes the specified offset is a valid
924  * area size word.
925  *
926  * Each area size word is specified in 4KB sector units. This function reports
927  * the size in bytes, intended for flat NVM reads.
928  */
ice_read_sr_area_size(struct ice_hw * hw,u16 offset,u32 * size)929 static int ice_read_sr_area_size(struct ice_hw *hw, u16 offset, u32 *size)
930 {
931 	int status;
932 	u16 value;
933 
934 	status = ice_read_sr_word(hw, offset, &value);
935 	if (status)
936 		return status;
937 
938 	/* Area sizes are always specified in 4KB units */
939 	*size = value * 4 * 1024;
940 
941 	return 0;
942 }
943 
944 /**
945  * ice_determine_active_flash_banks - Discover active bank for each module
946  * @hw: pointer to the HW struct
947  *
948  * Read the Shadow RAM control word and determine which banks are active for
949  * the NVM, OROM, and Netlist modules. Also read and calculate the associated
950  * pointer and size. These values are then cached into the ice_flash_info
951  * structure for later use in order to calculate the correct offset to read
952  * from the active module.
953  */
ice_determine_active_flash_banks(struct ice_hw * hw)954 static int ice_determine_active_flash_banks(struct ice_hw *hw)
955 {
956 	struct ice_bank_info *banks = &hw->flash.banks;
957 	u16 ctrl_word;
958 	int status;
959 
960 	status = ice_read_sr_word(hw, ICE_SR_NVM_CTRL_WORD, &ctrl_word);
961 	if (status) {
962 		ice_debug(hw, ICE_DBG_NVM, "Failed to read the Shadow RAM control word\n");
963 		return status;
964 	}
965 
966 	/* Check that the control word indicates validity */
967 	if ((ctrl_word & ICE_SR_CTRL_WORD_1_M) >> ICE_SR_CTRL_WORD_1_S != ICE_SR_CTRL_WORD_VALID) {
968 		ice_debug(hw, ICE_DBG_NVM, "Shadow RAM control word is invalid\n");
969 		return -EIO;
970 	}
971 
972 	if (!(ctrl_word & ICE_SR_CTRL_WORD_NVM_BANK))
973 		banks->nvm_bank = ICE_1ST_FLASH_BANK;
974 	else
975 		banks->nvm_bank = ICE_2ND_FLASH_BANK;
976 
977 	if (!(ctrl_word & ICE_SR_CTRL_WORD_OROM_BANK))
978 		banks->orom_bank = ICE_1ST_FLASH_BANK;
979 	else
980 		banks->orom_bank = ICE_2ND_FLASH_BANK;
981 
982 	if (!(ctrl_word & ICE_SR_CTRL_WORD_NETLIST_BANK))
983 		banks->netlist_bank = ICE_1ST_FLASH_BANK;
984 	else
985 		banks->netlist_bank = ICE_2ND_FLASH_BANK;
986 
987 	status = ice_read_sr_pointer(hw, ICE_SR_1ST_NVM_BANK_PTR, &banks->nvm_ptr);
988 	if (status) {
989 		ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank pointer\n");
990 		return status;
991 	}
992 
993 	status = ice_read_sr_area_size(hw, ICE_SR_NVM_BANK_SIZE, &banks->nvm_size);
994 	if (status) {
995 		ice_debug(hw, ICE_DBG_NVM, "Failed to read NVM bank area size\n");
996 		return status;
997 	}
998 
999 	status = ice_read_sr_pointer(hw, ICE_SR_1ST_OROM_BANK_PTR, &banks->orom_ptr);
1000 	if (status) {
1001 		ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank pointer\n");
1002 		return status;
1003 	}
1004 
1005 	status = ice_read_sr_area_size(hw, ICE_SR_OROM_BANK_SIZE, &banks->orom_size);
1006 	if (status) {
1007 		ice_debug(hw, ICE_DBG_NVM, "Failed to read OROM bank area size\n");
1008 		return status;
1009 	}
1010 
1011 	status = ice_read_sr_pointer(hw, ICE_SR_NETLIST_BANK_PTR, &banks->netlist_ptr);
1012 	if (status) {
1013 		ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank pointer\n");
1014 		return status;
1015 	}
1016 
1017 	status = ice_read_sr_area_size(hw, ICE_SR_NETLIST_BANK_SIZE, &banks->netlist_size);
1018 	if (status) {
1019 		ice_debug(hw, ICE_DBG_NVM, "Failed to read Netlist bank area size\n");
1020 		return status;
1021 	}
1022 
1023 	return 0;
1024 }
1025 
1026 /**
1027  * ice_init_nvm - initializes NVM setting
1028  * @hw: pointer to the HW struct
1029  *
1030  * This function reads and populates NVM settings such as Shadow RAM size,
1031  * max_timeout, and blank_nvm_mode
1032  */
ice_init_nvm(struct ice_hw * hw)1033 int ice_init_nvm(struct ice_hw *hw)
1034 {
1035 	struct ice_flash_info *flash = &hw->flash;
1036 	u32 fla, gens_stat;
1037 	u8 sr_size;
1038 	int status;
1039 
1040 	/* The SR size is stored regardless of the NVM programming mode
1041 	 * as the blank mode may be used in the factory line.
1042 	 */
1043 	gens_stat = rd32(hw, GLNVM_GENS);
1044 	sr_size = (gens_stat & GLNVM_GENS_SR_SIZE_M) >> GLNVM_GENS_SR_SIZE_S;
1045 
1046 	/* Switching to words (sr_size contains power of 2) */
1047 	flash->sr_words = BIT(sr_size) * ICE_SR_WORDS_IN_1KB;
1048 
1049 	/* Check if we are in the normal or blank NVM programming mode */
1050 	fla = rd32(hw, GLNVM_FLA);
1051 	if (fla & GLNVM_FLA_LOCKED_M) { /* Normal programming mode */
1052 		flash->blank_nvm_mode = false;
1053 	} else {
1054 		/* Blank programming mode */
1055 		flash->blank_nvm_mode = true;
1056 		ice_debug(hw, ICE_DBG_NVM, "NVM init error: unsupported blank mode.\n");
1057 		return -EIO;
1058 	}
1059 
1060 	status = ice_discover_flash_size(hw);
1061 	if (status) {
1062 		ice_debug(hw, ICE_DBG_NVM, "NVM init error: failed to discover flash size.\n");
1063 		return status;
1064 	}
1065 
1066 	status = ice_determine_active_flash_banks(hw);
1067 	if (status) {
1068 		ice_debug(hw, ICE_DBG_NVM, "Failed to determine active flash banks.\n");
1069 		return status;
1070 	}
1071 
1072 	status = ice_get_nvm_ver_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->nvm);
1073 	if (status) {
1074 		ice_debug(hw, ICE_DBG_INIT, "Failed to read NVM info.\n");
1075 		return status;
1076 	}
1077 
1078 	status = ice_get_orom_ver_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->orom);
1079 	if (status)
1080 		ice_debug(hw, ICE_DBG_INIT, "Failed to read Option ROM info.\n");
1081 
1082 	/* read the netlist version information */
1083 	status = ice_get_netlist_info(hw, ICE_ACTIVE_FLASH_BANK, &flash->netlist);
1084 	if (status)
1085 		ice_debug(hw, ICE_DBG_INIT, "Failed to read netlist info.\n");
1086 
1087 	return 0;
1088 }
1089 
1090 /**
1091  * ice_nvm_validate_checksum
1092  * @hw: pointer to the HW struct
1093  *
1094  * Verify NVM PFA checksum validity (0x0706)
1095  */
ice_nvm_validate_checksum(struct ice_hw * hw)1096 int ice_nvm_validate_checksum(struct ice_hw *hw)
1097 {
1098 	struct ice_aqc_nvm_checksum *cmd;
1099 	struct ice_aq_desc desc;
1100 	int status;
1101 
1102 	status = ice_acquire_nvm(hw, ICE_RES_READ);
1103 	if (status)
1104 		return status;
1105 
1106 	cmd = &desc.params.nvm_checksum;
1107 
1108 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_checksum);
1109 	cmd->flags = ICE_AQC_NVM_CHECKSUM_VERIFY;
1110 
1111 	status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1112 	ice_release_nvm(hw);
1113 
1114 	if (!status)
1115 		if (le16_to_cpu(cmd->checksum) != ICE_AQC_NVM_CHECKSUM_CORRECT)
1116 			status = -EIO;
1117 
1118 	return status;
1119 }
1120 
1121 /**
1122  * ice_nvm_write_activate
1123  * @hw: pointer to the HW struct
1124  * @cmd_flags: flags for write activate command
1125  * @response_flags: response indicators from firmware
1126  *
1127  * Update the control word with the required banks' validity bits
1128  * and dumps the Shadow RAM to flash (0x0707)
1129  *
1130  * cmd_flags controls which banks to activate, the preservation level to use
1131  * when activating the NVM bank, and whether an EMP reset is required for
1132  * activation.
1133  *
1134  * Note that the 16bit cmd_flags value is split between two separate 1 byte
1135  * flag values in the descriptor.
1136  *
1137  * On successful return of the firmware command, the response_flags variable
1138  * is updated with the flags reported by firmware indicating certain status,
1139  * such as whether EMP reset is enabled.
1140  */
ice_nvm_write_activate(struct ice_hw * hw,u16 cmd_flags,u8 * response_flags)1141 int ice_nvm_write_activate(struct ice_hw *hw, u16 cmd_flags, u8 *response_flags)
1142 {
1143 	struct ice_aqc_nvm *cmd;
1144 	struct ice_aq_desc desc;
1145 	int err;
1146 
1147 	cmd = &desc.params.nvm;
1148 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_write_activate);
1149 
1150 	cmd->cmd_flags = (u8)(cmd_flags & 0xFF);
1151 	cmd->offset_high = (u8)((cmd_flags >> 8) & 0xFF);
1152 
1153 	err = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1154 	if (!err && response_flags)
1155 		*response_flags = cmd->cmd_flags;
1156 
1157 	return err;
1158 }
1159 
1160 /**
1161  * ice_aq_nvm_update_empr
1162  * @hw: pointer to the HW struct
1163  *
1164  * Update empr (0x0709). This command allows SW to
1165  * request an EMPR to activate new FW.
1166  */
ice_aq_nvm_update_empr(struct ice_hw * hw)1167 int ice_aq_nvm_update_empr(struct ice_hw *hw)
1168 {
1169 	struct ice_aq_desc desc;
1170 
1171 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_update_empr);
1172 
1173 	return ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
1174 }
1175 
1176 /* ice_nvm_set_pkg_data
1177  * @hw: pointer to the HW struct
1178  * @del_pkg_data_flag: If is set then the current pkg_data store by FW
1179  *		       is deleted.
1180  *		       If bit is set to 1, then buffer should be size 0.
1181  * @data: pointer to buffer
1182  * @length: length of the buffer
1183  * @cd: pointer to command details structure or NULL
1184  *
1185  * Set package data (0x070A). This command is equivalent to the reception
1186  * of a PLDM FW Update GetPackageData cmd. This command should be sent
1187  * as part of the NVM update as the first cmd in the flow.
1188  */
1189 
1190 int
ice_nvm_set_pkg_data(struct ice_hw * hw,bool del_pkg_data_flag,u8 * data,u16 length,struct ice_sq_cd * cd)1191 ice_nvm_set_pkg_data(struct ice_hw *hw, bool del_pkg_data_flag, u8 *data,
1192 		     u16 length, struct ice_sq_cd *cd)
1193 {
1194 	struct ice_aqc_nvm_pkg_data *cmd;
1195 	struct ice_aq_desc desc;
1196 
1197 	if (length != 0 && !data)
1198 		return -EINVAL;
1199 
1200 	cmd = &desc.params.pkg_data;
1201 
1202 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_pkg_data);
1203 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1204 
1205 	if (del_pkg_data_flag)
1206 		cmd->cmd_flags |= ICE_AQC_NVM_PKG_DELETE;
1207 
1208 	return ice_aq_send_cmd(hw, &desc, data, length, cd);
1209 }
1210 
1211 /* ice_nvm_pass_component_tbl
1212  * @hw: pointer to the HW struct
1213  * @data: pointer to buffer
1214  * @length: length of the buffer
1215  * @transfer_flag: parameter for determining stage of the update
1216  * @comp_response: a pointer to the response from the 0x070B AQC.
1217  * @comp_response_code: a pointer to the response code from the 0x070B AQC.
1218  * @cd: pointer to command details structure or NULL
1219  *
1220  * Pass component table (0x070B). This command is equivalent to the reception
1221  * of a PLDM FW Update PassComponentTable cmd. This command should be sent once
1222  * per component. It can be only sent after Set Package Data cmd and before
1223  * actual update. FW will assume these commands are going to be sent until
1224  * the TransferFlag is set to End or StartAndEnd.
1225  */
1226 
1227 int
ice_nvm_pass_component_tbl(struct ice_hw * hw,u8 * data,u16 length,u8 transfer_flag,u8 * comp_response,u8 * comp_response_code,struct ice_sq_cd * cd)1228 ice_nvm_pass_component_tbl(struct ice_hw *hw, u8 *data, u16 length,
1229 			   u8 transfer_flag, u8 *comp_response,
1230 			   u8 *comp_response_code, struct ice_sq_cd *cd)
1231 {
1232 	struct ice_aqc_nvm_pass_comp_tbl *cmd;
1233 	struct ice_aq_desc desc;
1234 	int status;
1235 
1236 	if (!data || !comp_response || !comp_response_code)
1237 		return -EINVAL;
1238 
1239 	cmd = &desc.params.pass_comp_tbl;
1240 
1241 	ice_fill_dflt_direct_cmd_desc(&desc,
1242 				      ice_aqc_opc_nvm_pass_component_tbl);
1243 	desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
1244 
1245 	cmd->transfer_flag = transfer_flag;
1246 	status = ice_aq_send_cmd(hw, &desc, data, length, cd);
1247 
1248 	if (!status) {
1249 		*comp_response = cmd->component_response;
1250 		*comp_response_code = cmd->component_response_code;
1251 	}
1252 	return status;
1253 }
1254