1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2001-2020
5 #include "ice_common.h"
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
18 * Read the NVM using the admin queue commands (0x0701)
20 static enum ice_status
21 ice_aq_read_nvm(struct ice_hw *hw, u16 module_typeid, u32 offset, u16 length,
22 void *data, bool last_command, bool read_shadow_ram,
25 struct ice_aq_desc desc;
26 struct ice_aqc_nvm *cmd;
28 ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
30 cmd = &desc.params.nvm;
32 if (offset > ICE_AQC_NVM_MAX_OFFSET)
35 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_read);
37 if (!read_shadow_ram && module_typeid == ICE_AQC_NVM_START_POINT)
38 cmd->cmd_flags |= ICE_AQC_NVM_FLASH_ONLY;
40 /* If this is the last command in a series, set the proper flag. */
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);
48 return ice_aq_send_cmd(hw, &desc, data, length, cd);
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
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.
63 * Returns a status code on failure. Note that the data pointer may be
64 * partially updated if some reads succeed before a failure.
67 ice_read_flat_nvm(struct ice_hw *hw, u32 offset, u32 *length, u8 *data,
70 enum ice_status status;
75 ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
79 /* Verify the length of the read if this is for the Shadow RAM */
80 if (read_shadow_ram && ((offset + inlen) > (hw->nvm.sr_words * 2u))) {
81 ice_debug(hw, ICE_DBG_NVM,
82 "NVM error: requested data is beyond Shadow RAM limit\n");
87 u32 read_size, sector_offset;
89 /* ice_aq_read_nvm cannot read more than 4Kb at a time.
90 * Additionally, a read from the Shadow RAM may not cross over
91 * a sector boundary. Conveniently, the sector size is also
94 sector_offset = offset % ICE_AQ_MAX_BUF_LEN;
95 read_size = MIN_T(u32, ICE_AQ_MAX_BUF_LEN - sector_offset,
98 last_cmd = !(bytes_read + read_size < inlen);
100 /* ice_aq_read_nvm takes the length as a u16. Our read_size is
101 * calculated using a u32, but the ICE_AQ_MAX_BUF_LEN maximum
102 * size guarantees that it will fit within the 2 bytes.
104 status = ice_aq_read_nvm(hw, ICE_AQC_NVM_START_POINT,
105 offset, (u16)read_size,
106 data + bytes_read, last_cmd,
107 read_shadow_ram, NULL);
111 bytes_read += read_size;
115 *length = bytes_read;
120 * ice_read_sr_word_aq - Reads Shadow RAM via AQ
121 * @hw: pointer to the HW structure
122 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
123 * @data: word read from the Shadow RAM
125 * Reads one 16 bit word from the Shadow RAM using ice_read_flat_nvm.
127 static enum ice_status
128 ice_read_sr_word_aq(struct ice_hw *hw, u16 offset, u16 *data)
130 u32 bytes = sizeof(u16);
131 enum ice_status status;
134 ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
136 /* Note that ice_read_flat_nvm checks if the read is past the Shadow
137 * RAM size, and ensures we don't read across a Shadow RAM sector
140 status = ice_read_flat_nvm(hw, offset * sizeof(u16), &bytes,
141 (u8 *)&data_local, true);
145 *data = LE16_TO_CPU(data_local);
150 * ice_read_sr_buf_aq - Reads Shadow RAM buf via AQ
151 * @hw: pointer to the HW structure
152 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
153 * @words: (in) number of words to read; (out) number of words actually read
154 * @data: words read from the Shadow RAM
156 * Reads 16 bit words (data buf) from the Shadow RAM. Ownership of the NVM is
157 * taken before reading the buffer and later released.
159 static enum ice_status
160 ice_read_sr_buf_aq(struct ice_hw *hw, u16 offset, u16 *words, u16 *data)
162 u32 bytes = *words * 2, i;
163 enum ice_status status;
165 ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
167 /* ice_read_flat_nvm takes into account the 4Kb AdminQ and Shadow RAM
168 * sector restrictions necessary when reading from the NVM.
170 status = ice_read_flat_nvm(hw, offset * 2, &bytes, (u8 *)data, true);
172 /* Report the number of words successfully read */
175 /* Byte swap the words up to the amount we actually read */
176 for (i = 0; i < *words; i++)
177 data[i] = LE16_TO_CPU(((_FORCE_ __le16 *)data)[i]);
183 * ice_acquire_nvm - Generic request for acquiring the NVM ownership
184 * @hw: pointer to the HW structure
185 * @access: NVM access type (read or write)
187 * This function will request NVM ownership.
189 static enum ice_status
190 ice_acquire_nvm(struct ice_hw *hw, enum ice_aq_res_access_type access)
192 ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
194 if (hw->nvm.blank_nvm_mode)
197 return ice_acquire_res(hw, ICE_NVM_RES_ID, access, ICE_NVM_TIMEOUT);
201 * ice_release_nvm - Generic request for releasing the NVM ownership
202 * @hw: pointer to the HW structure
204 * This function will release NVM ownership.
206 static void ice_release_nvm(struct ice_hw *hw)
208 ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
210 if (hw->nvm.blank_nvm_mode)
213 ice_release_res(hw, ICE_NVM_RES_ID);
217 * ice_read_sr_word - Reads Shadow RAM word and acquire NVM if necessary
218 * @hw: pointer to the HW structure
219 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
220 * @data: word read from the Shadow RAM
222 * Reads one 16 bit word from the Shadow RAM using the ice_read_sr_word_aq.
224 enum ice_status ice_read_sr_word(struct ice_hw *hw, u16 offset, u16 *data)
226 enum ice_status status;
228 status = ice_acquire_nvm(hw, ICE_RES_READ);
230 status = ice_read_sr_word_aq(hw, offset, data);
238 * ice_get_pfa_module_tlv - Reads sub module TLV from NVM PFA
239 * @hw: pointer to hardware structure
240 * @module_tlv: pointer to module TLV to return
241 * @module_tlv_len: pointer to module TLV length to return
242 * @module_type: module type requested
244 * Finds the requested sub module TLV type from the Preserved Field
245 * Area (PFA) and returns the TLV pointer and length. The caller can
246 * use these to read the variable length TLV value.
249 ice_get_pfa_module_tlv(struct ice_hw *hw, u16 *module_tlv, u16 *module_tlv_len,
252 enum ice_status status;
253 u16 pfa_len, pfa_ptr;
256 status = ice_read_sr_word(hw, ICE_SR_PFA_PTR, &pfa_ptr);
257 if (status != ICE_SUCCESS) {
258 ice_debug(hw, ICE_DBG_INIT, "Preserved Field Array pointer.\n");
261 status = ice_read_sr_word(hw, pfa_ptr, &pfa_len);
262 if (status != ICE_SUCCESS) {
263 ice_debug(hw, ICE_DBG_INIT, "Failed to read PFA length.\n");
266 /* Starting with first TLV after PFA length, iterate through the list
267 * of TLVs to find the requested one.
269 next_tlv = pfa_ptr + 1;
270 while (next_tlv < pfa_ptr + pfa_len) {
271 u16 tlv_sub_module_type;
275 status = ice_read_sr_word(hw, next_tlv, &tlv_sub_module_type);
276 if (status != ICE_SUCCESS) {
277 ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV type.\n");
280 /* Read TLV length */
281 status = ice_read_sr_word(hw, next_tlv + 1, &tlv_len);
282 if (status != ICE_SUCCESS) {
283 ice_debug(hw, ICE_DBG_INIT, "Failed to read TLV length.\n");
286 if (tlv_sub_module_type == module_type) {
288 *module_tlv = next_tlv;
289 *module_tlv_len = tlv_len;
292 return ICE_ERR_INVAL_SIZE;
294 /* Check next TLV, i.e. current TLV pointer + length + 2 words
295 * (for current TLV's type and length)
297 next_tlv = next_tlv + tlv_len + 2;
299 /* Module does not exist */
300 return ICE_ERR_DOES_NOT_EXIST;
304 * ice_get_orom_ver_info - Read Option ROM version information
305 * @hw: pointer to the HW struct
307 * Read the Combo Image version data from the Boot Configuration TLV and fill
308 * in the option ROM version data.
310 static enum ice_status ice_get_orom_ver_info(struct ice_hw *hw)
312 u16 combo_hi, combo_lo, boot_cfg_tlv, boot_cfg_tlv_len;
313 struct ice_orom_info *orom = &hw->nvm.orom;
314 enum ice_status status;
317 status = ice_get_pfa_module_tlv(hw, &boot_cfg_tlv, &boot_cfg_tlv_len,
318 ICE_SR_BOOT_CFG_PTR);
320 ice_debug(hw, ICE_DBG_INIT,
321 "Failed to read Boot Configuration Block TLV.\n");
325 /* Boot Configuration Block must have length at least 2 words
326 * (Combo Image Version High and Combo Image Version Low)
328 if (boot_cfg_tlv_len < 2) {
329 ice_debug(hw, ICE_DBG_INIT,
330 "Invalid Boot Configuration Block TLV size.\n");
331 return ICE_ERR_INVAL_SIZE;
334 status = ice_read_sr_word(hw, (boot_cfg_tlv + ICE_NVM_OROM_VER_OFF),
337 ice_debug(hw, ICE_DBG_INIT, "Failed to read OROM_VER hi.\n");
341 status = ice_read_sr_word(hw, (boot_cfg_tlv + ICE_NVM_OROM_VER_OFF + 1),
344 ice_debug(hw, ICE_DBG_INIT, "Failed to read OROM_VER lo.\n");
348 combo_ver = ((u32)combo_hi << 16) | combo_lo;
350 orom->major = (u8)((combo_ver & ICE_OROM_VER_MASK) >>
352 orom->patch = (u8)(combo_ver & ICE_OROM_VER_PATCH_MASK);
353 orom->build = (u16)((combo_ver & ICE_OROM_VER_BUILD_MASK) >>
354 ICE_OROM_VER_BUILD_SHIFT);
360 * ice_init_nvm - initializes NVM setting
361 * @hw: pointer to the HW struct
363 * This function reads and populates NVM settings such as Shadow RAM size,
364 * max_timeout, and blank_nvm_mode
366 enum ice_status ice_init_nvm(struct ice_hw *hw)
368 struct ice_nvm_info *nvm = &hw->nvm;
369 u16 eetrack_lo, eetrack_hi, ver;
370 enum ice_status status;
374 ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
376 /* The SR size is stored regardless of the NVM programming mode
377 * as the blank mode may be used in the factory line.
379 gens_stat = rd32(hw, GLNVM_GENS);
380 sr_size = (gens_stat & GLNVM_GENS_SR_SIZE_M) >> GLNVM_GENS_SR_SIZE_S;
382 /* Switching to words (sr_size contains power of 2) */
383 nvm->sr_words = BIT(sr_size) * ICE_SR_WORDS_IN_1KB;
385 /* Check if we are in the normal or blank NVM programming mode */
386 fla = rd32(hw, GLNVM_FLA);
387 if (fla & GLNVM_FLA_LOCKED_M) { /* Normal programming mode */
388 nvm->blank_nvm_mode = false;
390 /* Blank programming mode */
391 nvm->blank_nvm_mode = true;
392 ice_debug(hw, ICE_DBG_NVM,
393 "NVM init error: unsupported blank mode.\n");
394 return ICE_ERR_NVM_BLANK_MODE;
397 status = ice_read_sr_word(hw, ICE_SR_NVM_DEV_STARTER_VER, &ver);
399 ice_debug(hw, ICE_DBG_INIT,
400 "Failed to read DEV starter version.\n");
403 nvm->major_ver = (ver & ICE_NVM_VER_HI_MASK) >> ICE_NVM_VER_HI_SHIFT;
404 nvm->minor_ver = (ver & ICE_NVM_VER_LO_MASK) >> ICE_NVM_VER_LO_SHIFT;
406 status = ice_read_sr_word(hw, ICE_SR_NVM_EETRACK_LO, &eetrack_lo);
408 ice_debug(hw, ICE_DBG_INIT, "Failed to read EETRACK lo.\n");
411 status = ice_read_sr_word(hw, ICE_SR_NVM_EETRACK_HI, &eetrack_hi);
413 ice_debug(hw, ICE_DBG_INIT, "Failed to read EETRACK hi.\n");
417 nvm->eetrack = (eetrack_hi << 16) | eetrack_lo;
419 switch (hw->device_id) {
420 /* the following devices do not have boot_cfg_tlv yet */
421 case ICE_DEV_ID_E822C_BACKPLANE:
422 case ICE_DEV_ID_E822C_QSFP:
423 case ICE_DEV_ID_E822C_10G_BASE_T:
424 case ICE_DEV_ID_E822C_SGMII:
425 case ICE_DEV_ID_E822C_SFP:
426 case ICE_DEV_ID_E822L_BACKPLANE:
427 case ICE_DEV_ID_E822L_SFP:
428 case ICE_DEV_ID_E822L_10G_BASE_T:
429 case ICE_DEV_ID_E822L_SGMII:
435 status = ice_get_orom_ver_info(hw);
437 ice_debug(hw, ICE_DBG_INIT, "Failed to read Option ROM info.\n");
445 * ice_read_sr_buf - Reads Shadow RAM buf and acquire lock if necessary
446 * @hw: pointer to the HW structure
447 * @offset: offset of the Shadow RAM word to read (0x000000 - 0x001FFF)
448 * @words: (in) number of words to read; (out) number of words actually read
449 * @data: words read from the Shadow RAM
451 * Reads 16 bit words (data buf) from the SR using the ice_read_nvm_buf_aq
452 * method. The buf read is preceded by the NVM ownership take
453 * and followed by the release.
456 ice_read_sr_buf(struct ice_hw *hw, u16 offset, u16 *words, u16 *data)
458 enum ice_status status;
460 status = ice_acquire_nvm(hw, ICE_RES_READ);
462 status = ice_read_sr_buf_aq(hw, offset, words, data);
470 * ice_nvm_validate_checksum
471 * @hw: pointer to the HW struct
473 * Verify NVM PFA checksum validity (0x0706)
475 enum ice_status ice_nvm_validate_checksum(struct ice_hw *hw)
477 struct ice_aqc_nvm_checksum *cmd;
478 struct ice_aq_desc desc;
479 enum ice_status status;
481 status = ice_acquire_nvm(hw, ICE_RES_READ);
485 cmd = &desc.params.nvm_checksum;
487 ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_nvm_checksum);
488 cmd->flags = ICE_AQC_NVM_CHECKSUM_VERIFY;
490 status = ice_aq_send_cmd(hw, &desc, NULL, 0, NULL);
494 if (LE16_TO_CPU(cmd->checksum) != ICE_AQC_NVM_CHECKSUM_CORRECT)
495 status = ICE_ERR_NVM_CHECKSUM;
501 * ice_nvm_access_get_features - Return the NVM access features structure
502 * @cmd: NVM access command to process
503 * @data: storage for the driver NVM features
505 * Fill in the data section of the NVM access request with a copy of the NVM
506 * features structure.
509 ice_nvm_access_get_features(struct ice_nvm_access_cmd *cmd,
510 union ice_nvm_access_data *data)
512 /* The provided data_size must be at least as large as our NVM
513 * features structure. A larger size should not be treated as an
514 * error, to allow future extensions to to the features structure to
515 * work on older drivers.
517 if (cmd->data_size < sizeof(struct ice_nvm_features))
518 return ICE_ERR_NO_MEMORY;
520 /* Initialize the data buffer to zeros */
521 ice_memset(data, 0, cmd->data_size, ICE_NONDMA_MEM);
523 /* Fill in the features data */
524 data->drv_features.major = ICE_NVM_ACCESS_MAJOR_VER;
525 data->drv_features.minor = ICE_NVM_ACCESS_MINOR_VER;
526 data->drv_features.size = sizeof(struct ice_nvm_features);
527 data->drv_features.features[0] = ICE_NVM_FEATURES_0_REG_ACCESS;
533 * ice_nvm_access_get_module - Helper function to read module value
534 * @cmd: NVM access command structure
536 * Reads the module value out of the NVM access config field.
538 u32 ice_nvm_access_get_module(struct ice_nvm_access_cmd *cmd)
540 return ((cmd->config & ICE_NVM_CFG_MODULE_M) >> ICE_NVM_CFG_MODULE_S);
544 * ice_nvm_access_get_flags - Helper function to read flags value
545 * @cmd: NVM access command structure
547 * Reads the flags value out of the NVM access config field.
549 u32 ice_nvm_access_get_flags(struct ice_nvm_access_cmd *cmd)
551 return ((cmd->config & ICE_NVM_CFG_FLAGS_M) >> ICE_NVM_CFG_FLAGS_S);
555 * ice_nvm_access_get_adapter - Helper function to read adapter info
556 * @cmd: NVM access command structure
558 * Read the adapter info value out of the NVM access config field.
560 u32 ice_nvm_access_get_adapter(struct ice_nvm_access_cmd *cmd)
562 return ((cmd->config & ICE_NVM_CFG_ADAPTER_INFO_M) >>
563 ICE_NVM_CFG_ADAPTER_INFO_S);
567 * ice_validate_nvm_rw_reg - Check than an NVM access request is valid
568 * @cmd: NVM access command structure
570 * Validates that an NVM access structure is request to read or write a valid
571 * register offset. First validates that the module and flags are correct, and
572 * then ensures that the register offset is one of the accepted registers.
574 static enum ice_status
575 ice_validate_nvm_rw_reg(struct ice_nvm_access_cmd *cmd)
577 u32 module, flags, offset;
580 module = ice_nvm_access_get_module(cmd);
581 flags = ice_nvm_access_get_flags(cmd);
582 offset = cmd->offset;
584 /* Make sure the module and flags indicate a read/write request */
585 if (module != ICE_NVM_REG_RW_MODULE ||
586 flags != ICE_NVM_REG_RW_FLAGS ||
587 cmd->data_size != FIELD_SIZEOF(union ice_nvm_access_data, regval))
588 return ICE_ERR_PARAM;
592 case GL_HICR_EN: /* Note, this register is read only */
595 case GLGEN_CSR_DEBUG_C:
606 for (i = 0; i <= ICE_NVM_ACCESS_GL_HIDA_MAX; i++)
607 if (offset == (u32)GL_HIDA(i))
610 for (i = 0; i <= ICE_NVM_ACCESS_GL_HIBA_MAX; i++)
611 if (offset == (u32)GL_HIBA(i))
614 /* All other register offsets are not valid */
615 return ICE_ERR_OUT_OF_RANGE;
619 * ice_nvm_access_read - Handle an NVM read request
620 * @hw: pointer to the HW struct
621 * @cmd: NVM access command to process
622 * @data: storage for the register value read
624 * Process an NVM access request to read a register.
627 ice_nvm_access_read(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
628 union ice_nvm_access_data *data)
630 enum ice_status status;
632 ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
634 /* Always initialize the output data, even on failure */
635 ice_memset(data, 0, cmd->data_size, ICE_NONDMA_MEM);
637 /* Make sure this is a valid read/write access request */
638 status = ice_validate_nvm_rw_reg(cmd);
642 ice_debug(hw, ICE_DBG_NVM, "NVM access: reading register %08x\n",
645 /* Read the register and store the contents in the data field */
646 data->regval = rd32(hw, cmd->offset);
652 * ice_nvm_access_write - Handle an NVM write request
653 * @hw: pointer to the HW struct
654 * @cmd: NVM access command to process
655 * @data: NVM access data to write
657 * Process an NVM access request to write a register.
660 ice_nvm_access_write(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
661 union ice_nvm_access_data *data)
663 enum ice_status status;
665 ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
667 /* Make sure this is a valid read/write access request */
668 status = ice_validate_nvm_rw_reg(cmd);
672 /* Reject requests to write to read-only registers */
673 switch (cmd->offset) {
676 return ICE_ERR_OUT_OF_RANGE;
681 ice_debug(hw, ICE_DBG_NVM,
682 "NVM access: writing register %08x with value %08x\n",
683 cmd->offset, data->regval);
685 /* Write the data field to the specified register */
686 wr32(hw, cmd->offset, data->regval);
692 * ice_handle_nvm_access - Handle an NVM access request
693 * @hw: pointer to the HW struct
694 * @cmd: NVM access command info
695 * @data: pointer to read or return data
697 * Process an NVM access request. Read the command structure information and
698 * determine if it is valid. If not, report an error indicating the command
701 * For valid commands, perform the necessary function, copying the data into
702 * the provided data buffer.
705 ice_handle_nvm_access(struct ice_hw *hw, struct ice_nvm_access_cmd *cmd,
706 union ice_nvm_access_data *data)
708 u32 module, flags, adapter_info;
710 ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
712 /* Extended flags are currently reserved and must be zero */
713 if ((cmd->config & ICE_NVM_CFG_EXT_FLAGS_M) != 0)
714 return ICE_ERR_PARAM;
716 /* Adapter info must match the HW device ID */
717 adapter_info = ice_nvm_access_get_adapter(cmd);
718 if (adapter_info != hw->device_id)
719 return ICE_ERR_PARAM;
721 switch (cmd->command) {
722 case ICE_NVM_CMD_READ:
723 module = ice_nvm_access_get_module(cmd);
724 flags = ice_nvm_access_get_flags(cmd);
726 /* Getting the driver's NVM features structure shares the same
727 * command type as reading a register. Read the config field
728 * to determine if this is a request to get features.
730 if (module == ICE_NVM_GET_FEATURES_MODULE &&
731 flags == ICE_NVM_GET_FEATURES_FLAGS &&
733 return ice_nvm_access_get_features(cmd, data);
735 return ice_nvm_access_read(hw, cmd, data);
736 case ICE_NVM_CMD_WRITE:
737 return ice_nvm_access_write(hw, cmd, data);
739 return ICE_ERR_PARAM;