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