162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/* Copyright(c) 1999 - 2018 Intel Corporation. */
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci#include <linux/pci.h>
562306a36Sopenharmony_ci#include <linux/delay.h>
662306a36Sopenharmony_ci#include <linux/sched.h>
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include "ixgbe.h"
962306a36Sopenharmony_ci#include "ixgbe_phy.h"
1062306a36Sopenharmony_ci#include "ixgbe_x540.h"
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#define IXGBE_X540_MAX_TX_QUEUES	128
1362306a36Sopenharmony_ci#define IXGBE_X540_MAX_RX_QUEUES	128
1462306a36Sopenharmony_ci#define IXGBE_X540_RAR_ENTRIES		128
1562306a36Sopenharmony_ci#define IXGBE_X540_MC_TBL_SIZE		128
1662306a36Sopenharmony_ci#define IXGBE_X540_VFT_TBL_SIZE		128
1762306a36Sopenharmony_ci#define IXGBE_X540_RX_PB_SIZE		384
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_cistatic s32 ixgbe_update_flash_X540(struct ixgbe_hw *hw);
2062306a36Sopenharmony_cistatic s32 ixgbe_poll_flash_update_done_X540(struct ixgbe_hw *hw);
2162306a36Sopenharmony_cistatic s32 ixgbe_get_swfw_sync_semaphore(struct ixgbe_hw *hw);
2262306a36Sopenharmony_cistatic void ixgbe_release_swfw_sync_semaphore(struct ixgbe_hw *hw);
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_cienum ixgbe_media_type ixgbe_get_media_type_X540(struct ixgbe_hw *hw)
2562306a36Sopenharmony_ci{
2662306a36Sopenharmony_ci	return ixgbe_media_type_copper;
2762306a36Sopenharmony_ci}
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_cis32 ixgbe_get_invariants_X540(struct ixgbe_hw *hw)
3062306a36Sopenharmony_ci{
3162306a36Sopenharmony_ci	struct ixgbe_mac_info *mac = &hw->mac;
3262306a36Sopenharmony_ci	struct ixgbe_phy_info *phy = &hw->phy;
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci	/* set_phy_power was set by default to NULL */
3562306a36Sopenharmony_ci	phy->ops.set_phy_power = ixgbe_set_copper_phy_power;
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci	mac->mcft_size = IXGBE_X540_MC_TBL_SIZE;
3862306a36Sopenharmony_ci	mac->vft_size = IXGBE_X540_VFT_TBL_SIZE;
3962306a36Sopenharmony_ci	mac->num_rar_entries = IXGBE_X540_RAR_ENTRIES;
4062306a36Sopenharmony_ci	mac->rx_pb_size = IXGBE_X540_RX_PB_SIZE;
4162306a36Sopenharmony_ci	mac->max_rx_queues = IXGBE_X540_MAX_RX_QUEUES;
4262306a36Sopenharmony_ci	mac->max_tx_queues = IXGBE_X540_MAX_TX_QUEUES;
4362306a36Sopenharmony_ci	mac->max_msix_vectors = ixgbe_get_pcie_msix_count_generic(hw);
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci	return 0;
4662306a36Sopenharmony_ci}
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci/**
4962306a36Sopenharmony_ci *  ixgbe_setup_mac_link_X540 - Set the auto advertised capabilitires
5062306a36Sopenharmony_ci *  @hw: pointer to hardware structure
5162306a36Sopenharmony_ci *  @speed: new link speed
5262306a36Sopenharmony_ci *  @autoneg_wait_to_complete: true when waiting for completion is needed
5362306a36Sopenharmony_ci **/
5462306a36Sopenharmony_cis32 ixgbe_setup_mac_link_X540(struct ixgbe_hw *hw, ixgbe_link_speed speed,
5562306a36Sopenharmony_ci			      bool autoneg_wait_to_complete)
5662306a36Sopenharmony_ci{
5762306a36Sopenharmony_ci	return hw->phy.ops.setup_link_speed(hw, speed,
5862306a36Sopenharmony_ci					    autoneg_wait_to_complete);
5962306a36Sopenharmony_ci}
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci/**
6262306a36Sopenharmony_ci *  ixgbe_reset_hw_X540 - Perform hardware reset
6362306a36Sopenharmony_ci *  @hw: pointer to hardware structure
6462306a36Sopenharmony_ci *
6562306a36Sopenharmony_ci *  Resets the hardware by resetting the transmit and receive units, masks
6662306a36Sopenharmony_ci *  and clears all interrupts, perform a PHY reset, and perform a link (MAC)
6762306a36Sopenharmony_ci *  reset.
6862306a36Sopenharmony_ci **/
6962306a36Sopenharmony_cis32 ixgbe_reset_hw_X540(struct ixgbe_hw *hw)
7062306a36Sopenharmony_ci{
7162306a36Sopenharmony_ci	s32 status;
7262306a36Sopenharmony_ci	u32 ctrl, i;
7362306a36Sopenharmony_ci	u32 swfw_mask = hw->phy.phy_semaphore_mask;
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	/* Call adapter stop to disable tx/rx and clear interrupts */
7662306a36Sopenharmony_ci	status = hw->mac.ops.stop_adapter(hw);
7762306a36Sopenharmony_ci	if (status)
7862306a36Sopenharmony_ci		return status;
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	/* flush pending Tx transactions */
8162306a36Sopenharmony_ci	ixgbe_clear_tx_pending(hw);
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_cimac_reset_top:
8462306a36Sopenharmony_ci	status = hw->mac.ops.acquire_swfw_sync(hw, swfw_mask);
8562306a36Sopenharmony_ci	if (status) {
8662306a36Sopenharmony_ci		hw_dbg(hw, "semaphore failed with %d", status);
8762306a36Sopenharmony_ci		return -EBUSY;
8862306a36Sopenharmony_ci	}
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	ctrl = IXGBE_CTRL_RST;
9162306a36Sopenharmony_ci	ctrl |= IXGBE_READ_REG(hw, IXGBE_CTRL);
9262306a36Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_CTRL, ctrl);
9362306a36Sopenharmony_ci	IXGBE_WRITE_FLUSH(hw);
9462306a36Sopenharmony_ci	hw->mac.ops.release_swfw_sync(hw, swfw_mask);
9562306a36Sopenharmony_ci	usleep_range(1000, 1200);
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	/* Poll for reset bit to self-clear indicating reset is complete */
9862306a36Sopenharmony_ci	for (i = 0; i < 10; i++) {
9962306a36Sopenharmony_ci		ctrl = IXGBE_READ_REG(hw, IXGBE_CTRL);
10062306a36Sopenharmony_ci		if (!(ctrl & IXGBE_CTRL_RST_MASK))
10162306a36Sopenharmony_ci			break;
10262306a36Sopenharmony_ci		udelay(1);
10362306a36Sopenharmony_ci	}
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	if (ctrl & IXGBE_CTRL_RST_MASK) {
10662306a36Sopenharmony_ci		status = -EIO;
10762306a36Sopenharmony_ci		hw_dbg(hw, "Reset polling failed to complete.\n");
10862306a36Sopenharmony_ci	}
10962306a36Sopenharmony_ci	msleep(100);
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci	/*
11262306a36Sopenharmony_ci	 * Double resets are required for recovery from certain error
11362306a36Sopenharmony_ci	 * conditions.  Between resets, it is necessary to stall to allow time
11462306a36Sopenharmony_ci	 * for any pending HW events to complete.
11562306a36Sopenharmony_ci	 */
11662306a36Sopenharmony_ci	if (hw->mac.flags & IXGBE_FLAGS_DOUBLE_RESET_REQUIRED) {
11762306a36Sopenharmony_ci		hw->mac.flags &= ~IXGBE_FLAGS_DOUBLE_RESET_REQUIRED;
11862306a36Sopenharmony_ci		goto mac_reset_top;
11962306a36Sopenharmony_ci	}
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	/* Set the Rx packet buffer size. */
12262306a36Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_RXPBSIZE(0), 384 << IXGBE_RXPBSIZE_SHIFT);
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	/* Store the permanent mac address */
12562306a36Sopenharmony_ci	hw->mac.ops.get_mac_addr(hw, hw->mac.perm_addr);
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci	/*
12862306a36Sopenharmony_ci	 * Store MAC address from RAR0, clear receive address registers, and
12962306a36Sopenharmony_ci	 * clear the multicast table.  Also reset num_rar_entries to 128,
13062306a36Sopenharmony_ci	 * since we modify this value when programming the SAN MAC address.
13162306a36Sopenharmony_ci	 */
13262306a36Sopenharmony_ci	hw->mac.num_rar_entries = IXGBE_X540_MAX_TX_QUEUES;
13362306a36Sopenharmony_ci	hw->mac.ops.init_rx_addrs(hw);
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	/* Store the permanent SAN mac address */
13662306a36Sopenharmony_ci	hw->mac.ops.get_san_mac_addr(hw, hw->mac.san_addr);
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci	/* Add the SAN MAC address to the RAR only if it's a valid address */
13962306a36Sopenharmony_ci	if (is_valid_ether_addr(hw->mac.san_addr)) {
14062306a36Sopenharmony_ci		/* Save the SAN MAC RAR index */
14162306a36Sopenharmony_ci		hw->mac.san_mac_rar_index = hw->mac.num_rar_entries - 1;
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci		hw->mac.ops.set_rar(hw, hw->mac.san_mac_rar_index,
14462306a36Sopenharmony_ci				    hw->mac.san_addr, 0, IXGBE_RAH_AV);
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci		/* clear VMDq pool/queue selection for this RAR */
14762306a36Sopenharmony_ci		hw->mac.ops.clear_vmdq(hw, hw->mac.san_mac_rar_index,
14862306a36Sopenharmony_ci				       IXGBE_CLEAR_VMDQ_ALL);
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci		/* Reserve the last RAR for the SAN MAC address */
15162306a36Sopenharmony_ci		hw->mac.num_rar_entries--;
15262306a36Sopenharmony_ci	}
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci	/* Store the alternative WWNN/WWPN prefix */
15562306a36Sopenharmony_ci	hw->mac.ops.get_wwn_prefix(hw, &hw->mac.wwnn_prefix,
15662306a36Sopenharmony_ci				   &hw->mac.wwpn_prefix);
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	return status;
15962306a36Sopenharmony_ci}
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci/**
16262306a36Sopenharmony_ci *  ixgbe_start_hw_X540 - Prepare hardware for Tx/Rx
16362306a36Sopenharmony_ci *  @hw: pointer to hardware structure
16462306a36Sopenharmony_ci *
16562306a36Sopenharmony_ci *  Starts the hardware using the generic start_hw function
16662306a36Sopenharmony_ci *  and the generation start_hw function.
16762306a36Sopenharmony_ci *  Then performs revision-specific operations, if any.
16862306a36Sopenharmony_ci **/
16962306a36Sopenharmony_cis32 ixgbe_start_hw_X540(struct ixgbe_hw *hw)
17062306a36Sopenharmony_ci{
17162306a36Sopenharmony_ci	s32 ret_val;
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci	ret_val = ixgbe_start_hw_generic(hw);
17462306a36Sopenharmony_ci	if (ret_val)
17562306a36Sopenharmony_ci		return ret_val;
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	return ixgbe_start_hw_gen2(hw);
17862306a36Sopenharmony_ci}
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci/**
18162306a36Sopenharmony_ci *  ixgbe_init_eeprom_params_X540 - Initialize EEPROM params
18262306a36Sopenharmony_ci *  @hw: pointer to hardware structure
18362306a36Sopenharmony_ci *
18462306a36Sopenharmony_ci *  Initializes the EEPROM parameters ixgbe_eeprom_info within the
18562306a36Sopenharmony_ci *  ixgbe_hw struct in order to set up EEPROM access.
18662306a36Sopenharmony_ci **/
18762306a36Sopenharmony_cis32 ixgbe_init_eeprom_params_X540(struct ixgbe_hw *hw)
18862306a36Sopenharmony_ci{
18962306a36Sopenharmony_ci	struct ixgbe_eeprom_info *eeprom = &hw->eeprom;
19062306a36Sopenharmony_ci	u32 eec;
19162306a36Sopenharmony_ci	u16 eeprom_size;
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci	if (eeprom->type == ixgbe_eeprom_uninitialized) {
19462306a36Sopenharmony_ci		eeprom->semaphore_delay = 10;
19562306a36Sopenharmony_ci		eeprom->type = ixgbe_flash;
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci		eec = IXGBE_READ_REG(hw, IXGBE_EEC(hw));
19862306a36Sopenharmony_ci		eeprom_size = (u16)((eec & IXGBE_EEC_SIZE) >>
19962306a36Sopenharmony_ci				    IXGBE_EEC_SIZE_SHIFT);
20062306a36Sopenharmony_ci		eeprom->word_size = BIT(eeprom_size +
20162306a36Sopenharmony_ci					IXGBE_EEPROM_WORD_SIZE_SHIFT);
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci		hw_dbg(hw, "Eeprom params: type = %d, size = %d\n",
20462306a36Sopenharmony_ci		       eeprom->type, eeprom->word_size);
20562306a36Sopenharmony_ci	}
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci	return 0;
20862306a36Sopenharmony_ci}
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci/**
21162306a36Sopenharmony_ci *  ixgbe_read_eerd_X540- Read EEPROM word using EERD
21262306a36Sopenharmony_ci *  @hw: pointer to hardware structure
21362306a36Sopenharmony_ci *  @offset: offset of  word in the EEPROM to read
21462306a36Sopenharmony_ci *  @data: word read from the EEPROM
21562306a36Sopenharmony_ci *
21662306a36Sopenharmony_ci *  Reads a 16 bit word from the EEPROM using the EERD register.
21762306a36Sopenharmony_ci **/
21862306a36Sopenharmony_cistatic s32 ixgbe_read_eerd_X540(struct ixgbe_hw *hw, u16 offset, u16 *data)
21962306a36Sopenharmony_ci{
22062306a36Sopenharmony_ci	s32 status;
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci	if (hw->mac.ops.acquire_swfw_sync(hw, IXGBE_GSSR_EEP_SM))
22362306a36Sopenharmony_ci		return -EBUSY;
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci	status = ixgbe_read_eerd_generic(hw, offset, data);
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci	hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
22862306a36Sopenharmony_ci	return status;
22962306a36Sopenharmony_ci}
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci/**
23262306a36Sopenharmony_ci *  ixgbe_read_eerd_buffer_X540 - Read EEPROM word(s) using EERD
23362306a36Sopenharmony_ci *  @hw: pointer to hardware structure
23462306a36Sopenharmony_ci *  @offset: offset of  word in the EEPROM to read
23562306a36Sopenharmony_ci *  @words: number of words
23662306a36Sopenharmony_ci *  @data: word(s) read from the EEPROM
23762306a36Sopenharmony_ci *
23862306a36Sopenharmony_ci *  Reads a 16 bit word(s) from the EEPROM using the EERD register.
23962306a36Sopenharmony_ci **/
24062306a36Sopenharmony_cistatic s32 ixgbe_read_eerd_buffer_X540(struct ixgbe_hw *hw,
24162306a36Sopenharmony_ci				       u16 offset, u16 words, u16 *data)
24262306a36Sopenharmony_ci{
24362306a36Sopenharmony_ci	s32 status;
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	if (hw->mac.ops.acquire_swfw_sync(hw, IXGBE_GSSR_EEP_SM))
24662306a36Sopenharmony_ci		return -EBUSY;
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci	status = ixgbe_read_eerd_buffer_generic(hw, offset, words, data);
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci	hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
25162306a36Sopenharmony_ci	return status;
25262306a36Sopenharmony_ci}
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci/**
25562306a36Sopenharmony_ci *  ixgbe_write_eewr_X540 - Write EEPROM word using EEWR
25662306a36Sopenharmony_ci *  @hw: pointer to hardware structure
25762306a36Sopenharmony_ci *  @offset: offset of  word in the EEPROM to write
25862306a36Sopenharmony_ci *  @data: word write to the EEPROM
25962306a36Sopenharmony_ci *
26062306a36Sopenharmony_ci *  Write a 16 bit word to the EEPROM using the EEWR register.
26162306a36Sopenharmony_ci **/
26262306a36Sopenharmony_cistatic s32 ixgbe_write_eewr_X540(struct ixgbe_hw *hw, u16 offset, u16 data)
26362306a36Sopenharmony_ci{
26462306a36Sopenharmony_ci	s32 status;
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci	if (hw->mac.ops.acquire_swfw_sync(hw, IXGBE_GSSR_EEP_SM))
26762306a36Sopenharmony_ci		return -EBUSY;
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_ci	status = ixgbe_write_eewr_generic(hw, offset, data);
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ci	hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
27262306a36Sopenharmony_ci	return status;
27362306a36Sopenharmony_ci}
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ci/**
27662306a36Sopenharmony_ci *  ixgbe_write_eewr_buffer_X540 - Write EEPROM word(s) using EEWR
27762306a36Sopenharmony_ci *  @hw: pointer to hardware structure
27862306a36Sopenharmony_ci *  @offset: offset of  word in the EEPROM to write
27962306a36Sopenharmony_ci *  @words: number of words
28062306a36Sopenharmony_ci *  @data: word(s) write to the EEPROM
28162306a36Sopenharmony_ci *
28262306a36Sopenharmony_ci *  Write a 16 bit word(s) to the EEPROM using the EEWR register.
28362306a36Sopenharmony_ci **/
28462306a36Sopenharmony_cistatic s32 ixgbe_write_eewr_buffer_X540(struct ixgbe_hw *hw,
28562306a36Sopenharmony_ci					u16 offset, u16 words, u16 *data)
28662306a36Sopenharmony_ci{
28762306a36Sopenharmony_ci	s32 status;
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_ci	if (hw->mac.ops.acquire_swfw_sync(hw, IXGBE_GSSR_EEP_SM))
29062306a36Sopenharmony_ci		return -EBUSY;
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci	status = ixgbe_write_eewr_buffer_generic(hw, offset, words, data);
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_ci	hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
29562306a36Sopenharmony_ci	return status;
29662306a36Sopenharmony_ci}
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ci/**
29962306a36Sopenharmony_ci *  ixgbe_calc_eeprom_checksum_X540 - Calculates and returns the checksum
30062306a36Sopenharmony_ci *
30162306a36Sopenharmony_ci *  This function does not use synchronization for EERD and EEWR. It can
30262306a36Sopenharmony_ci *  be used internally by function which utilize ixgbe_acquire_swfw_sync_X540.
30362306a36Sopenharmony_ci *
30462306a36Sopenharmony_ci *  @hw: pointer to hardware structure
30562306a36Sopenharmony_ci **/
30662306a36Sopenharmony_cistatic s32 ixgbe_calc_eeprom_checksum_X540(struct ixgbe_hw *hw)
30762306a36Sopenharmony_ci{
30862306a36Sopenharmony_ci	u16 i;
30962306a36Sopenharmony_ci	u16 j;
31062306a36Sopenharmony_ci	u16 checksum = 0;
31162306a36Sopenharmony_ci	u16 length = 0;
31262306a36Sopenharmony_ci	u16 pointer = 0;
31362306a36Sopenharmony_ci	u16 word = 0;
31462306a36Sopenharmony_ci	u16 checksum_last_word = IXGBE_EEPROM_CHECKSUM;
31562306a36Sopenharmony_ci	u16 ptr_start = IXGBE_PCIE_ANALOG_PTR;
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_ci	/*
31862306a36Sopenharmony_ci	 * Do not use hw->eeprom.ops.read because we do not want to take
31962306a36Sopenharmony_ci	 * the synchronization semaphores here. Instead use
32062306a36Sopenharmony_ci	 * ixgbe_read_eerd_generic
32162306a36Sopenharmony_ci	 */
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ci	/* Include 0x0-0x3F in the checksum */
32462306a36Sopenharmony_ci	for (i = 0; i < checksum_last_word; i++) {
32562306a36Sopenharmony_ci		if (ixgbe_read_eerd_generic(hw, i, &word)) {
32662306a36Sopenharmony_ci			hw_dbg(hw, "EEPROM read failed\n");
32762306a36Sopenharmony_ci			return -EIO;
32862306a36Sopenharmony_ci		}
32962306a36Sopenharmony_ci		checksum += word;
33062306a36Sopenharmony_ci	}
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci	/*
33362306a36Sopenharmony_ci	 * Include all data from pointers 0x3, 0x6-0xE.  This excludes the
33462306a36Sopenharmony_ci	 * FW, PHY module, and PCIe Expansion/Option ROM pointers.
33562306a36Sopenharmony_ci	 */
33662306a36Sopenharmony_ci	for (i = ptr_start; i < IXGBE_FW_PTR; i++) {
33762306a36Sopenharmony_ci		if (i == IXGBE_PHY_PTR || i == IXGBE_OPTION_ROM_PTR)
33862306a36Sopenharmony_ci			continue;
33962306a36Sopenharmony_ci
34062306a36Sopenharmony_ci		if (ixgbe_read_eerd_generic(hw, i, &pointer)) {
34162306a36Sopenharmony_ci			hw_dbg(hw, "EEPROM read failed\n");
34262306a36Sopenharmony_ci			break;
34362306a36Sopenharmony_ci		}
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_ci		/* Skip pointer section if the pointer is invalid. */
34662306a36Sopenharmony_ci		if (pointer == 0xFFFF || pointer == 0 ||
34762306a36Sopenharmony_ci		    pointer >= hw->eeprom.word_size)
34862306a36Sopenharmony_ci			continue;
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ci		if (ixgbe_read_eerd_generic(hw, pointer, &length)) {
35162306a36Sopenharmony_ci			hw_dbg(hw, "EEPROM read failed\n");
35262306a36Sopenharmony_ci			return -EIO;
35362306a36Sopenharmony_ci		}
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci		/* Skip pointer section if length is invalid. */
35662306a36Sopenharmony_ci		if (length == 0xFFFF || length == 0 ||
35762306a36Sopenharmony_ci		    (pointer + length) >= hw->eeprom.word_size)
35862306a36Sopenharmony_ci			continue;
35962306a36Sopenharmony_ci
36062306a36Sopenharmony_ci		for (j = pointer + 1; j <= pointer + length; j++) {
36162306a36Sopenharmony_ci			if (ixgbe_read_eerd_generic(hw, j, &word)) {
36262306a36Sopenharmony_ci				hw_dbg(hw, "EEPROM read failed\n");
36362306a36Sopenharmony_ci				return -EIO;
36462306a36Sopenharmony_ci			}
36562306a36Sopenharmony_ci			checksum += word;
36662306a36Sopenharmony_ci		}
36762306a36Sopenharmony_ci	}
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_ci	checksum = (u16)IXGBE_EEPROM_SUM - checksum;
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ci	return (s32)checksum;
37262306a36Sopenharmony_ci}
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ci/**
37562306a36Sopenharmony_ci *  ixgbe_validate_eeprom_checksum_X540 - Validate EEPROM checksum
37662306a36Sopenharmony_ci *  @hw: pointer to hardware structure
37762306a36Sopenharmony_ci *  @checksum_val: calculated checksum
37862306a36Sopenharmony_ci *
37962306a36Sopenharmony_ci *  Performs checksum calculation and validates the EEPROM checksum.  If the
38062306a36Sopenharmony_ci *  caller does not need checksum_val, the value can be NULL.
38162306a36Sopenharmony_ci **/
38262306a36Sopenharmony_cistatic s32 ixgbe_validate_eeprom_checksum_X540(struct ixgbe_hw *hw,
38362306a36Sopenharmony_ci					       u16 *checksum_val)
38462306a36Sopenharmony_ci{
38562306a36Sopenharmony_ci	s32 status;
38662306a36Sopenharmony_ci	u16 checksum;
38762306a36Sopenharmony_ci	u16 read_checksum = 0;
38862306a36Sopenharmony_ci
38962306a36Sopenharmony_ci	/* Read the first word from the EEPROM. If this times out or fails, do
39062306a36Sopenharmony_ci	 * not continue or we could be in for a very long wait while every
39162306a36Sopenharmony_ci	 * EEPROM read fails
39262306a36Sopenharmony_ci	 */
39362306a36Sopenharmony_ci	status = hw->eeprom.ops.read(hw, 0, &checksum);
39462306a36Sopenharmony_ci	if (status) {
39562306a36Sopenharmony_ci		hw_dbg(hw, "EEPROM read failed\n");
39662306a36Sopenharmony_ci		return status;
39762306a36Sopenharmony_ci	}
39862306a36Sopenharmony_ci
39962306a36Sopenharmony_ci	if (hw->mac.ops.acquire_swfw_sync(hw, IXGBE_GSSR_EEP_SM))
40062306a36Sopenharmony_ci		return -EBUSY;
40162306a36Sopenharmony_ci
40262306a36Sopenharmony_ci	status = hw->eeprom.ops.calc_checksum(hw);
40362306a36Sopenharmony_ci	if (status < 0)
40462306a36Sopenharmony_ci		goto out;
40562306a36Sopenharmony_ci
40662306a36Sopenharmony_ci	checksum = (u16)(status & 0xffff);
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_ci	/* Do not use hw->eeprom.ops.read because we do not want to take
40962306a36Sopenharmony_ci	 * the synchronization semaphores twice here.
41062306a36Sopenharmony_ci	 */
41162306a36Sopenharmony_ci	status = ixgbe_read_eerd_generic(hw, IXGBE_EEPROM_CHECKSUM,
41262306a36Sopenharmony_ci					 &read_checksum);
41362306a36Sopenharmony_ci	if (status)
41462306a36Sopenharmony_ci		goto out;
41562306a36Sopenharmony_ci
41662306a36Sopenharmony_ci	/* Verify read checksum from EEPROM is the same as
41762306a36Sopenharmony_ci	 * calculated checksum
41862306a36Sopenharmony_ci	 */
41962306a36Sopenharmony_ci	if (read_checksum != checksum) {
42062306a36Sopenharmony_ci		hw_dbg(hw, "Invalid EEPROM checksum");
42162306a36Sopenharmony_ci		status = -EIO;
42262306a36Sopenharmony_ci	}
42362306a36Sopenharmony_ci
42462306a36Sopenharmony_ci	/* If the user cares, return the calculated checksum */
42562306a36Sopenharmony_ci	if (checksum_val)
42662306a36Sopenharmony_ci		*checksum_val = checksum;
42762306a36Sopenharmony_ci
42862306a36Sopenharmony_ciout:
42962306a36Sopenharmony_ci	hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
43062306a36Sopenharmony_ci
43162306a36Sopenharmony_ci	return status;
43262306a36Sopenharmony_ci}
43362306a36Sopenharmony_ci
43462306a36Sopenharmony_ci/**
43562306a36Sopenharmony_ci * ixgbe_update_eeprom_checksum_X540 - Updates the EEPROM checksum and flash
43662306a36Sopenharmony_ci * @hw: pointer to hardware structure
43762306a36Sopenharmony_ci *
43862306a36Sopenharmony_ci * After writing EEPROM to shadow RAM using EEWR register, software calculates
43962306a36Sopenharmony_ci * checksum and updates the EEPROM and instructs the hardware to update
44062306a36Sopenharmony_ci * the flash.
44162306a36Sopenharmony_ci **/
44262306a36Sopenharmony_cistatic s32 ixgbe_update_eeprom_checksum_X540(struct ixgbe_hw *hw)
44362306a36Sopenharmony_ci{
44462306a36Sopenharmony_ci	s32 status;
44562306a36Sopenharmony_ci	u16 checksum;
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_ci	/* Read the first word from the EEPROM. If this times out or fails, do
44862306a36Sopenharmony_ci	 * not continue or we could be in for a very long wait while every
44962306a36Sopenharmony_ci	 * EEPROM read fails
45062306a36Sopenharmony_ci	 */
45162306a36Sopenharmony_ci	status = hw->eeprom.ops.read(hw, 0, &checksum);
45262306a36Sopenharmony_ci	if (status) {
45362306a36Sopenharmony_ci		hw_dbg(hw, "EEPROM read failed\n");
45462306a36Sopenharmony_ci		return status;
45562306a36Sopenharmony_ci	}
45662306a36Sopenharmony_ci
45762306a36Sopenharmony_ci	if (hw->mac.ops.acquire_swfw_sync(hw, IXGBE_GSSR_EEP_SM))
45862306a36Sopenharmony_ci		return  -EBUSY;
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci	status = hw->eeprom.ops.calc_checksum(hw);
46162306a36Sopenharmony_ci	if (status < 0)
46262306a36Sopenharmony_ci		goto out;
46362306a36Sopenharmony_ci
46462306a36Sopenharmony_ci	checksum = (u16)(status & 0xffff);
46562306a36Sopenharmony_ci
46662306a36Sopenharmony_ci	/* Do not use hw->eeprom.ops.write because we do not want to
46762306a36Sopenharmony_ci	 * take the synchronization semaphores twice here.
46862306a36Sopenharmony_ci	 */
46962306a36Sopenharmony_ci	status = ixgbe_write_eewr_generic(hw, IXGBE_EEPROM_CHECKSUM, checksum);
47062306a36Sopenharmony_ci	if (status)
47162306a36Sopenharmony_ci		goto out;
47262306a36Sopenharmony_ci
47362306a36Sopenharmony_ci	status = ixgbe_update_flash_X540(hw);
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ciout:
47662306a36Sopenharmony_ci	hw->mac.ops.release_swfw_sync(hw, IXGBE_GSSR_EEP_SM);
47762306a36Sopenharmony_ci	return status;
47862306a36Sopenharmony_ci}
47962306a36Sopenharmony_ci
48062306a36Sopenharmony_ci/**
48162306a36Sopenharmony_ci * ixgbe_update_flash_X540 - Instruct HW to copy EEPROM to Flash device
48262306a36Sopenharmony_ci * @hw: pointer to hardware structure
48362306a36Sopenharmony_ci *
48462306a36Sopenharmony_ci * Set FLUP (bit 23) of the EEC register to instruct Hardware to copy
48562306a36Sopenharmony_ci * EEPROM from shadow RAM to the flash device.
48662306a36Sopenharmony_ci **/
48762306a36Sopenharmony_cistatic s32 ixgbe_update_flash_X540(struct ixgbe_hw *hw)
48862306a36Sopenharmony_ci{
48962306a36Sopenharmony_ci	u32 flup;
49062306a36Sopenharmony_ci	s32 status;
49162306a36Sopenharmony_ci
49262306a36Sopenharmony_ci	status = ixgbe_poll_flash_update_done_X540(hw);
49362306a36Sopenharmony_ci	if (status == -EIO) {
49462306a36Sopenharmony_ci		hw_dbg(hw, "Flash update time out\n");
49562306a36Sopenharmony_ci		return status;
49662306a36Sopenharmony_ci	}
49762306a36Sopenharmony_ci
49862306a36Sopenharmony_ci	flup = IXGBE_READ_REG(hw, IXGBE_EEC(hw)) | IXGBE_EEC_FLUP;
49962306a36Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_EEC(hw), flup);
50062306a36Sopenharmony_ci
50162306a36Sopenharmony_ci	status = ixgbe_poll_flash_update_done_X540(hw);
50262306a36Sopenharmony_ci	if (status == 0)
50362306a36Sopenharmony_ci		hw_dbg(hw, "Flash update complete\n");
50462306a36Sopenharmony_ci	else
50562306a36Sopenharmony_ci		hw_dbg(hw, "Flash update time out\n");
50662306a36Sopenharmony_ci
50762306a36Sopenharmony_ci	if (hw->revision_id == 0) {
50862306a36Sopenharmony_ci		flup = IXGBE_READ_REG(hw, IXGBE_EEC(hw));
50962306a36Sopenharmony_ci
51062306a36Sopenharmony_ci		if (flup & IXGBE_EEC_SEC1VAL) {
51162306a36Sopenharmony_ci			flup |= IXGBE_EEC_FLUP;
51262306a36Sopenharmony_ci			IXGBE_WRITE_REG(hw, IXGBE_EEC(hw), flup);
51362306a36Sopenharmony_ci		}
51462306a36Sopenharmony_ci
51562306a36Sopenharmony_ci		status = ixgbe_poll_flash_update_done_X540(hw);
51662306a36Sopenharmony_ci		if (status == 0)
51762306a36Sopenharmony_ci			hw_dbg(hw, "Flash update complete\n");
51862306a36Sopenharmony_ci		else
51962306a36Sopenharmony_ci			hw_dbg(hw, "Flash update time out\n");
52062306a36Sopenharmony_ci	}
52162306a36Sopenharmony_ci
52262306a36Sopenharmony_ci	return status;
52362306a36Sopenharmony_ci}
52462306a36Sopenharmony_ci
52562306a36Sopenharmony_ci/**
52662306a36Sopenharmony_ci * ixgbe_poll_flash_update_done_X540 - Poll flash update status
52762306a36Sopenharmony_ci * @hw: pointer to hardware structure
52862306a36Sopenharmony_ci *
52962306a36Sopenharmony_ci * Polls the FLUDONE (bit 26) of the EEC Register to determine when the
53062306a36Sopenharmony_ci * flash update is done.
53162306a36Sopenharmony_ci **/
53262306a36Sopenharmony_cistatic s32 ixgbe_poll_flash_update_done_X540(struct ixgbe_hw *hw)
53362306a36Sopenharmony_ci{
53462306a36Sopenharmony_ci	u32 i;
53562306a36Sopenharmony_ci	u32 reg;
53662306a36Sopenharmony_ci
53762306a36Sopenharmony_ci	for (i = 0; i < IXGBE_FLUDONE_ATTEMPTS; i++) {
53862306a36Sopenharmony_ci		reg = IXGBE_READ_REG(hw, IXGBE_EEC(hw));
53962306a36Sopenharmony_ci		if (reg & IXGBE_EEC_FLUDONE)
54062306a36Sopenharmony_ci			return 0;
54162306a36Sopenharmony_ci		udelay(5);
54262306a36Sopenharmony_ci	}
54362306a36Sopenharmony_ci	return -EIO;
54462306a36Sopenharmony_ci}
54562306a36Sopenharmony_ci
54662306a36Sopenharmony_ci/**
54762306a36Sopenharmony_ci * ixgbe_acquire_swfw_sync_X540 - Acquire SWFW semaphore
54862306a36Sopenharmony_ci * @hw: pointer to hardware structure
54962306a36Sopenharmony_ci * @mask: Mask to specify which semaphore to acquire
55062306a36Sopenharmony_ci *
55162306a36Sopenharmony_ci * Acquires the SWFW semaphore thought the SW_FW_SYNC register for
55262306a36Sopenharmony_ci * the specified function (CSR, PHY0, PHY1, NVM, Flash)
55362306a36Sopenharmony_ci **/
55462306a36Sopenharmony_cis32 ixgbe_acquire_swfw_sync_X540(struct ixgbe_hw *hw, u32 mask)
55562306a36Sopenharmony_ci{
55662306a36Sopenharmony_ci	u32 swmask = mask & IXGBE_GSSR_NVM_PHY_MASK;
55762306a36Sopenharmony_ci	u32 swi2c_mask = mask & IXGBE_GSSR_I2C_MASK;
55862306a36Sopenharmony_ci	u32 fwmask = swmask << 5;
55962306a36Sopenharmony_ci	u32 timeout = 200;
56062306a36Sopenharmony_ci	u32 hwmask = 0;
56162306a36Sopenharmony_ci	u32 swfw_sync;
56262306a36Sopenharmony_ci	u32 i;
56362306a36Sopenharmony_ci
56462306a36Sopenharmony_ci	if (swmask & IXGBE_GSSR_EEP_SM)
56562306a36Sopenharmony_ci		hwmask = IXGBE_GSSR_FLASH_SM;
56662306a36Sopenharmony_ci
56762306a36Sopenharmony_ci	/* SW only mask does not have FW bit pair */
56862306a36Sopenharmony_ci	if (mask & IXGBE_GSSR_SW_MNG_SM)
56962306a36Sopenharmony_ci		swmask |= IXGBE_GSSR_SW_MNG_SM;
57062306a36Sopenharmony_ci
57162306a36Sopenharmony_ci	swmask |= swi2c_mask;
57262306a36Sopenharmony_ci	fwmask |= swi2c_mask << 2;
57362306a36Sopenharmony_ci	for (i = 0; i < timeout; i++) {
57462306a36Sopenharmony_ci		/* SW NVM semaphore bit is used for access to all
57562306a36Sopenharmony_ci		 * SW_FW_SYNC bits (not just NVM)
57662306a36Sopenharmony_ci		 */
57762306a36Sopenharmony_ci		if (ixgbe_get_swfw_sync_semaphore(hw))
57862306a36Sopenharmony_ci			return -EBUSY;
57962306a36Sopenharmony_ci
58062306a36Sopenharmony_ci		swfw_sync = IXGBE_READ_REG(hw, IXGBE_SWFW_SYNC(hw));
58162306a36Sopenharmony_ci		if (!(swfw_sync & (fwmask | swmask | hwmask))) {
58262306a36Sopenharmony_ci			swfw_sync |= swmask;
58362306a36Sopenharmony_ci			IXGBE_WRITE_REG(hw, IXGBE_SWFW_SYNC(hw), swfw_sync);
58462306a36Sopenharmony_ci			ixgbe_release_swfw_sync_semaphore(hw);
58562306a36Sopenharmony_ci			usleep_range(5000, 6000);
58662306a36Sopenharmony_ci			return 0;
58762306a36Sopenharmony_ci		}
58862306a36Sopenharmony_ci		/* Firmware currently using resource (fwmask), hardware
58962306a36Sopenharmony_ci		 * currently using resource (hwmask), or other software
59062306a36Sopenharmony_ci		 * thread currently using resource (swmask)
59162306a36Sopenharmony_ci		 */
59262306a36Sopenharmony_ci		ixgbe_release_swfw_sync_semaphore(hw);
59362306a36Sopenharmony_ci		usleep_range(5000, 10000);
59462306a36Sopenharmony_ci	}
59562306a36Sopenharmony_ci
59662306a36Sopenharmony_ci	/* If the resource is not released by the FW/HW the SW can assume that
59762306a36Sopenharmony_ci	 * the FW/HW malfunctions. In that case the SW should set the SW bit(s)
59862306a36Sopenharmony_ci	 * of the requested resource(s) while ignoring the corresponding FW/HW
59962306a36Sopenharmony_ci	 * bits in the SW_FW_SYNC register.
60062306a36Sopenharmony_ci	 */
60162306a36Sopenharmony_ci	if (ixgbe_get_swfw_sync_semaphore(hw))
60262306a36Sopenharmony_ci		return -EBUSY;
60362306a36Sopenharmony_ci	swfw_sync = IXGBE_READ_REG(hw, IXGBE_SWFW_SYNC(hw));
60462306a36Sopenharmony_ci	if (swfw_sync & (fwmask | hwmask)) {
60562306a36Sopenharmony_ci		swfw_sync |= swmask;
60662306a36Sopenharmony_ci		IXGBE_WRITE_REG(hw, IXGBE_SWFW_SYNC(hw), swfw_sync);
60762306a36Sopenharmony_ci		ixgbe_release_swfw_sync_semaphore(hw);
60862306a36Sopenharmony_ci		usleep_range(5000, 6000);
60962306a36Sopenharmony_ci		return 0;
61062306a36Sopenharmony_ci	}
61162306a36Sopenharmony_ci	/* If the resource is not released by other SW the SW can assume that
61262306a36Sopenharmony_ci	 * the other SW malfunctions. In that case the SW should clear all SW
61362306a36Sopenharmony_ci	 * flags that it does not own and then repeat the whole process once
61462306a36Sopenharmony_ci	 * again.
61562306a36Sopenharmony_ci	 */
61662306a36Sopenharmony_ci	if (swfw_sync & swmask) {
61762306a36Sopenharmony_ci		u32 rmask = IXGBE_GSSR_EEP_SM | IXGBE_GSSR_PHY0_SM |
61862306a36Sopenharmony_ci			    IXGBE_GSSR_PHY1_SM | IXGBE_GSSR_MAC_CSR_SM |
61962306a36Sopenharmony_ci			    IXGBE_GSSR_SW_MNG_SM;
62062306a36Sopenharmony_ci
62162306a36Sopenharmony_ci		if (swi2c_mask)
62262306a36Sopenharmony_ci			rmask |= IXGBE_GSSR_I2C_MASK;
62362306a36Sopenharmony_ci		ixgbe_release_swfw_sync_X540(hw, rmask);
62462306a36Sopenharmony_ci		ixgbe_release_swfw_sync_semaphore(hw);
62562306a36Sopenharmony_ci		return -EBUSY;
62662306a36Sopenharmony_ci	}
62762306a36Sopenharmony_ci	ixgbe_release_swfw_sync_semaphore(hw);
62862306a36Sopenharmony_ci
62962306a36Sopenharmony_ci	return -EBUSY;
63062306a36Sopenharmony_ci}
63162306a36Sopenharmony_ci
63262306a36Sopenharmony_ci/**
63362306a36Sopenharmony_ci * ixgbe_release_swfw_sync_X540 - Release SWFW semaphore
63462306a36Sopenharmony_ci * @hw: pointer to hardware structure
63562306a36Sopenharmony_ci * @mask: Mask to specify which semaphore to release
63662306a36Sopenharmony_ci *
63762306a36Sopenharmony_ci * Releases the SWFW semaphore through the SW_FW_SYNC register
63862306a36Sopenharmony_ci * for the specified function (CSR, PHY0, PHY1, EVM, Flash)
63962306a36Sopenharmony_ci **/
64062306a36Sopenharmony_civoid ixgbe_release_swfw_sync_X540(struct ixgbe_hw *hw, u32 mask)
64162306a36Sopenharmony_ci{
64262306a36Sopenharmony_ci	u32 swmask = mask & (IXGBE_GSSR_NVM_PHY_MASK | IXGBE_GSSR_SW_MNG_SM);
64362306a36Sopenharmony_ci	u32 swfw_sync;
64462306a36Sopenharmony_ci
64562306a36Sopenharmony_ci	if (mask & IXGBE_GSSR_I2C_MASK)
64662306a36Sopenharmony_ci		swmask |= mask & IXGBE_GSSR_I2C_MASK;
64762306a36Sopenharmony_ci	ixgbe_get_swfw_sync_semaphore(hw);
64862306a36Sopenharmony_ci
64962306a36Sopenharmony_ci	swfw_sync = IXGBE_READ_REG(hw, IXGBE_SWFW_SYNC(hw));
65062306a36Sopenharmony_ci	swfw_sync &= ~swmask;
65162306a36Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_SWFW_SYNC(hw), swfw_sync);
65262306a36Sopenharmony_ci
65362306a36Sopenharmony_ci	ixgbe_release_swfw_sync_semaphore(hw);
65462306a36Sopenharmony_ci	usleep_range(5000, 6000);
65562306a36Sopenharmony_ci}
65662306a36Sopenharmony_ci
65762306a36Sopenharmony_ci/**
65862306a36Sopenharmony_ci * ixgbe_get_swfw_sync_semaphore - Get hardware semaphore
65962306a36Sopenharmony_ci * @hw: pointer to hardware structure
66062306a36Sopenharmony_ci *
66162306a36Sopenharmony_ci * Sets the hardware semaphores so SW/FW can gain control of shared resources
66262306a36Sopenharmony_ci */
66362306a36Sopenharmony_cistatic s32 ixgbe_get_swfw_sync_semaphore(struct ixgbe_hw *hw)
66462306a36Sopenharmony_ci{
66562306a36Sopenharmony_ci	u32 timeout = 2000;
66662306a36Sopenharmony_ci	u32 i;
66762306a36Sopenharmony_ci	u32 swsm;
66862306a36Sopenharmony_ci
66962306a36Sopenharmony_ci	/* Get SMBI software semaphore between device drivers first */
67062306a36Sopenharmony_ci	for (i = 0; i < timeout; i++) {
67162306a36Sopenharmony_ci		/* If the SMBI bit is 0 when we read it, then the bit will be
67262306a36Sopenharmony_ci		 * set and we have the semaphore
67362306a36Sopenharmony_ci		 */
67462306a36Sopenharmony_ci		swsm = IXGBE_READ_REG(hw, IXGBE_SWSM(hw));
67562306a36Sopenharmony_ci		if (!(swsm & IXGBE_SWSM_SMBI))
67662306a36Sopenharmony_ci			break;
67762306a36Sopenharmony_ci		usleep_range(50, 100);
67862306a36Sopenharmony_ci	}
67962306a36Sopenharmony_ci
68062306a36Sopenharmony_ci	if (i == timeout) {
68162306a36Sopenharmony_ci		hw_dbg(hw,
68262306a36Sopenharmony_ci		       "Software semaphore SMBI between device drivers not granted.\n");
68362306a36Sopenharmony_ci		return -EIO;
68462306a36Sopenharmony_ci	}
68562306a36Sopenharmony_ci
68662306a36Sopenharmony_ci	/* Now get the semaphore between SW/FW through the REGSMP bit */
68762306a36Sopenharmony_ci	for (i = 0; i < timeout; i++) {
68862306a36Sopenharmony_ci		swsm = IXGBE_READ_REG(hw, IXGBE_SWFW_SYNC(hw));
68962306a36Sopenharmony_ci		if (!(swsm & IXGBE_SWFW_REGSMP))
69062306a36Sopenharmony_ci			return 0;
69162306a36Sopenharmony_ci
69262306a36Sopenharmony_ci		usleep_range(50, 100);
69362306a36Sopenharmony_ci	}
69462306a36Sopenharmony_ci
69562306a36Sopenharmony_ci	/* Release semaphores and return error if SW NVM semaphore
69662306a36Sopenharmony_ci	 * was not granted because we do not have access to the EEPROM
69762306a36Sopenharmony_ci	 */
69862306a36Sopenharmony_ci	hw_dbg(hw, "REGSMP Software NVM semaphore not granted\n");
69962306a36Sopenharmony_ci	ixgbe_release_swfw_sync_semaphore(hw);
70062306a36Sopenharmony_ci	return -EIO;
70162306a36Sopenharmony_ci}
70262306a36Sopenharmony_ci
70362306a36Sopenharmony_ci/**
70462306a36Sopenharmony_ci * ixgbe_release_swfw_sync_semaphore - Release hardware semaphore
70562306a36Sopenharmony_ci * @hw: pointer to hardware structure
70662306a36Sopenharmony_ci *
70762306a36Sopenharmony_ci * This function clears hardware semaphore bits.
70862306a36Sopenharmony_ci **/
70962306a36Sopenharmony_cistatic void ixgbe_release_swfw_sync_semaphore(struct ixgbe_hw *hw)
71062306a36Sopenharmony_ci{
71162306a36Sopenharmony_ci	 u32 swsm;
71262306a36Sopenharmony_ci
71362306a36Sopenharmony_ci	/* Release both semaphores by writing 0 to the bits REGSMP and SMBI */
71462306a36Sopenharmony_ci
71562306a36Sopenharmony_ci	swsm = IXGBE_READ_REG(hw, IXGBE_SWFW_SYNC(hw));
71662306a36Sopenharmony_ci	swsm &= ~IXGBE_SWFW_REGSMP;
71762306a36Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_SWFW_SYNC(hw), swsm);
71862306a36Sopenharmony_ci
71962306a36Sopenharmony_ci	swsm = IXGBE_READ_REG(hw, IXGBE_SWSM(hw));
72062306a36Sopenharmony_ci	swsm &= ~IXGBE_SWSM_SMBI;
72162306a36Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_SWSM(hw), swsm);
72262306a36Sopenharmony_ci
72362306a36Sopenharmony_ci	IXGBE_WRITE_FLUSH(hw);
72462306a36Sopenharmony_ci}
72562306a36Sopenharmony_ci
72662306a36Sopenharmony_ci/**
72762306a36Sopenharmony_ci *  ixgbe_init_swfw_sync_X540 - Release hardware semaphore
72862306a36Sopenharmony_ci *  @hw: pointer to hardware structure
72962306a36Sopenharmony_ci *
73062306a36Sopenharmony_ci *  This function reset hardware semaphore bits for a semaphore that may
73162306a36Sopenharmony_ci *  have be left locked due to a catastrophic failure.
73262306a36Sopenharmony_ci **/
73362306a36Sopenharmony_civoid ixgbe_init_swfw_sync_X540(struct ixgbe_hw *hw)
73462306a36Sopenharmony_ci{
73562306a36Sopenharmony_ci	u32 rmask;
73662306a36Sopenharmony_ci
73762306a36Sopenharmony_ci	/* First try to grab the semaphore but we don't need to bother
73862306a36Sopenharmony_ci	 * looking to see whether we got the lock or not since we do
73962306a36Sopenharmony_ci	 * the same thing regardless of whether we got the lock or not.
74062306a36Sopenharmony_ci	 * We got the lock - we release it.
74162306a36Sopenharmony_ci	 * We timeout trying to get the lock - we force its release.
74262306a36Sopenharmony_ci	 */
74362306a36Sopenharmony_ci	ixgbe_get_swfw_sync_semaphore(hw);
74462306a36Sopenharmony_ci	ixgbe_release_swfw_sync_semaphore(hw);
74562306a36Sopenharmony_ci
74662306a36Sopenharmony_ci	/* Acquire and release all software resources. */
74762306a36Sopenharmony_ci	rmask = IXGBE_GSSR_EEP_SM | IXGBE_GSSR_PHY0_SM |
74862306a36Sopenharmony_ci		IXGBE_GSSR_PHY1_SM | IXGBE_GSSR_MAC_CSR_SM |
74962306a36Sopenharmony_ci		IXGBE_GSSR_SW_MNG_SM | IXGBE_GSSR_I2C_MASK;
75062306a36Sopenharmony_ci
75162306a36Sopenharmony_ci	ixgbe_acquire_swfw_sync_X540(hw, rmask);
75262306a36Sopenharmony_ci	ixgbe_release_swfw_sync_X540(hw, rmask);
75362306a36Sopenharmony_ci}
75462306a36Sopenharmony_ci
75562306a36Sopenharmony_ci/**
75662306a36Sopenharmony_ci * ixgbe_blink_led_start_X540 - Blink LED based on index.
75762306a36Sopenharmony_ci * @hw: pointer to hardware structure
75862306a36Sopenharmony_ci * @index: led number to blink
75962306a36Sopenharmony_ci *
76062306a36Sopenharmony_ci * Devices that implement the version 2 interface:
76162306a36Sopenharmony_ci *   X540
76262306a36Sopenharmony_ci **/
76362306a36Sopenharmony_cis32 ixgbe_blink_led_start_X540(struct ixgbe_hw *hw, u32 index)
76462306a36Sopenharmony_ci{
76562306a36Sopenharmony_ci	u32 macc_reg;
76662306a36Sopenharmony_ci	u32 ledctl_reg;
76762306a36Sopenharmony_ci	ixgbe_link_speed speed;
76862306a36Sopenharmony_ci	bool link_up;
76962306a36Sopenharmony_ci
77062306a36Sopenharmony_ci	if (index > 3)
77162306a36Sopenharmony_ci		return -EINVAL;
77262306a36Sopenharmony_ci
77362306a36Sopenharmony_ci	/* Link should be up in order for the blink bit in the LED control
77462306a36Sopenharmony_ci	 * register to work. Force link and speed in the MAC if link is down.
77562306a36Sopenharmony_ci	 * This will be reversed when we stop the blinking.
77662306a36Sopenharmony_ci	 */
77762306a36Sopenharmony_ci	hw->mac.ops.check_link(hw, &speed, &link_up, false);
77862306a36Sopenharmony_ci	if (!link_up) {
77962306a36Sopenharmony_ci		macc_reg = IXGBE_READ_REG(hw, IXGBE_MACC);
78062306a36Sopenharmony_ci		macc_reg |= IXGBE_MACC_FLU | IXGBE_MACC_FSV_10G | IXGBE_MACC_FS;
78162306a36Sopenharmony_ci		IXGBE_WRITE_REG(hw, IXGBE_MACC, macc_reg);
78262306a36Sopenharmony_ci	}
78362306a36Sopenharmony_ci	/* Set the LED to LINK_UP + BLINK. */
78462306a36Sopenharmony_ci	ledctl_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
78562306a36Sopenharmony_ci	ledctl_reg &= ~IXGBE_LED_MODE_MASK(index);
78662306a36Sopenharmony_ci	ledctl_reg |= IXGBE_LED_BLINK(index);
78762306a36Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, ledctl_reg);
78862306a36Sopenharmony_ci	IXGBE_WRITE_FLUSH(hw);
78962306a36Sopenharmony_ci
79062306a36Sopenharmony_ci	return 0;
79162306a36Sopenharmony_ci}
79262306a36Sopenharmony_ci
79362306a36Sopenharmony_ci/**
79462306a36Sopenharmony_ci * ixgbe_blink_led_stop_X540 - Stop blinking LED based on index.
79562306a36Sopenharmony_ci * @hw: pointer to hardware structure
79662306a36Sopenharmony_ci * @index: led number to stop blinking
79762306a36Sopenharmony_ci *
79862306a36Sopenharmony_ci * Devices that implement the version 2 interface:
79962306a36Sopenharmony_ci *   X540
80062306a36Sopenharmony_ci **/
80162306a36Sopenharmony_cis32 ixgbe_blink_led_stop_X540(struct ixgbe_hw *hw, u32 index)
80262306a36Sopenharmony_ci{
80362306a36Sopenharmony_ci	u32 macc_reg;
80462306a36Sopenharmony_ci	u32 ledctl_reg;
80562306a36Sopenharmony_ci
80662306a36Sopenharmony_ci	if (index > 3)
80762306a36Sopenharmony_ci		return -EINVAL;
80862306a36Sopenharmony_ci
80962306a36Sopenharmony_ci	/* Restore the LED to its default value. */
81062306a36Sopenharmony_ci	ledctl_reg = IXGBE_READ_REG(hw, IXGBE_LEDCTL);
81162306a36Sopenharmony_ci	ledctl_reg &= ~IXGBE_LED_MODE_MASK(index);
81262306a36Sopenharmony_ci	ledctl_reg |= IXGBE_LED_LINK_ACTIVE << IXGBE_LED_MODE_SHIFT(index);
81362306a36Sopenharmony_ci	ledctl_reg &= ~IXGBE_LED_BLINK(index);
81462306a36Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_LEDCTL, ledctl_reg);
81562306a36Sopenharmony_ci
81662306a36Sopenharmony_ci	/* Unforce link and speed in the MAC. */
81762306a36Sopenharmony_ci	macc_reg = IXGBE_READ_REG(hw, IXGBE_MACC);
81862306a36Sopenharmony_ci	macc_reg &= ~(IXGBE_MACC_FLU | IXGBE_MACC_FSV_10G | IXGBE_MACC_FS);
81962306a36Sopenharmony_ci	IXGBE_WRITE_REG(hw, IXGBE_MACC, macc_reg);
82062306a36Sopenharmony_ci	IXGBE_WRITE_FLUSH(hw);
82162306a36Sopenharmony_ci
82262306a36Sopenharmony_ci	return 0;
82362306a36Sopenharmony_ci}
82462306a36Sopenharmony_cistatic const struct ixgbe_mac_operations mac_ops_X540 = {
82562306a36Sopenharmony_ci	.init_hw                = &ixgbe_init_hw_generic,
82662306a36Sopenharmony_ci	.reset_hw               = &ixgbe_reset_hw_X540,
82762306a36Sopenharmony_ci	.start_hw               = &ixgbe_start_hw_X540,
82862306a36Sopenharmony_ci	.clear_hw_cntrs         = &ixgbe_clear_hw_cntrs_generic,
82962306a36Sopenharmony_ci	.get_media_type         = &ixgbe_get_media_type_X540,
83062306a36Sopenharmony_ci	.enable_rx_dma          = &ixgbe_enable_rx_dma_generic,
83162306a36Sopenharmony_ci	.get_mac_addr           = &ixgbe_get_mac_addr_generic,
83262306a36Sopenharmony_ci	.get_san_mac_addr       = &ixgbe_get_san_mac_addr_generic,
83362306a36Sopenharmony_ci	.get_device_caps        = &ixgbe_get_device_caps_generic,
83462306a36Sopenharmony_ci	.get_wwn_prefix         = &ixgbe_get_wwn_prefix_generic,
83562306a36Sopenharmony_ci	.stop_adapter           = &ixgbe_stop_adapter_generic,
83662306a36Sopenharmony_ci	.get_bus_info           = &ixgbe_get_bus_info_generic,
83762306a36Sopenharmony_ci	.set_lan_id             = &ixgbe_set_lan_id_multi_port_pcie,
83862306a36Sopenharmony_ci	.read_analog_reg8       = NULL,
83962306a36Sopenharmony_ci	.write_analog_reg8      = NULL,
84062306a36Sopenharmony_ci	.setup_link             = &ixgbe_setup_mac_link_X540,
84162306a36Sopenharmony_ci	.set_rxpba		= &ixgbe_set_rxpba_generic,
84262306a36Sopenharmony_ci	.check_link             = &ixgbe_check_mac_link_generic,
84362306a36Sopenharmony_ci	.get_link_capabilities  = &ixgbe_get_copper_link_capabilities_generic,
84462306a36Sopenharmony_ci	.led_on                 = &ixgbe_led_on_generic,
84562306a36Sopenharmony_ci	.led_off                = &ixgbe_led_off_generic,
84662306a36Sopenharmony_ci	.init_led_link_act	= ixgbe_init_led_link_act_generic,
84762306a36Sopenharmony_ci	.blink_led_start        = &ixgbe_blink_led_start_X540,
84862306a36Sopenharmony_ci	.blink_led_stop         = &ixgbe_blink_led_stop_X540,
84962306a36Sopenharmony_ci	.set_rar                = &ixgbe_set_rar_generic,
85062306a36Sopenharmony_ci	.clear_rar              = &ixgbe_clear_rar_generic,
85162306a36Sopenharmony_ci	.set_vmdq               = &ixgbe_set_vmdq_generic,
85262306a36Sopenharmony_ci	.set_vmdq_san_mac	= &ixgbe_set_vmdq_san_mac_generic,
85362306a36Sopenharmony_ci	.clear_vmdq             = &ixgbe_clear_vmdq_generic,
85462306a36Sopenharmony_ci	.init_rx_addrs          = &ixgbe_init_rx_addrs_generic,
85562306a36Sopenharmony_ci	.update_mc_addr_list    = &ixgbe_update_mc_addr_list_generic,
85662306a36Sopenharmony_ci	.enable_mc              = &ixgbe_enable_mc_generic,
85762306a36Sopenharmony_ci	.disable_mc             = &ixgbe_disable_mc_generic,
85862306a36Sopenharmony_ci	.clear_vfta             = &ixgbe_clear_vfta_generic,
85962306a36Sopenharmony_ci	.set_vfta               = &ixgbe_set_vfta_generic,
86062306a36Sopenharmony_ci	.fc_enable              = &ixgbe_fc_enable_generic,
86162306a36Sopenharmony_ci	.setup_fc		= ixgbe_setup_fc_generic,
86262306a36Sopenharmony_ci	.fc_autoneg		= ixgbe_fc_autoneg,
86362306a36Sopenharmony_ci	.set_fw_drv_ver         = &ixgbe_set_fw_drv_ver_generic,
86462306a36Sopenharmony_ci	.init_uta_tables        = &ixgbe_init_uta_tables_generic,
86562306a36Sopenharmony_ci	.setup_sfp              = NULL,
86662306a36Sopenharmony_ci	.set_mac_anti_spoofing  = &ixgbe_set_mac_anti_spoofing,
86762306a36Sopenharmony_ci	.set_vlan_anti_spoofing = &ixgbe_set_vlan_anti_spoofing,
86862306a36Sopenharmony_ci	.acquire_swfw_sync      = &ixgbe_acquire_swfw_sync_X540,
86962306a36Sopenharmony_ci	.release_swfw_sync      = &ixgbe_release_swfw_sync_X540,
87062306a36Sopenharmony_ci	.init_swfw_sync		= &ixgbe_init_swfw_sync_X540,
87162306a36Sopenharmony_ci	.disable_rx_buff	= &ixgbe_disable_rx_buff_generic,
87262306a36Sopenharmony_ci	.enable_rx_buff		= &ixgbe_enable_rx_buff_generic,
87362306a36Sopenharmony_ci	.get_thermal_sensor_data = NULL,
87462306a36Sopenharmony_ci	.init_thermal_sensor_thresh = NULL,
87562306a36Sopenharmony_ci	.prot_autoc_read	= &prot_autoc_read_generic,
87662306a36Sopenharmony_ci	.prot_autoc_write	= &prot_autoc_write_generic,
87762306a36Sopenharmony_ci	.enable_rx		= &ixgbe_enable_rx_generic,
87862306a36Sopenharmony_ci	.disable_rx		= &ixgbe_disable_rx_generic,
87962306a36Sopenharmony_ci};
88062306a36Sopenharmony_ci
88162306a36Sopenharmony_cistatic const struct ixgbe_eeprom_operations eeprom_ops_X540 = {
88262306a36Sopenharmony_ci	.init_params            = &ixgbe_init_eeprom_params_X540,
88362306a36Sopenharmony_ci	.read                   = &ixgbe_read_eerd_X540,
88462306a36Sopenharmony_ci	.read_buffer		= &ixgbe_read_eerd_buffer_X540,
88562306a36Sopenharmony_ci	.write                  = &ixgbe_write_eewr_X540,
88662306a36Sopenharmony_ci	.write_buffer		= &ixgbe_write_eewr_buffer_X540,
88762306a36Sopenharmony_ci	.calc_checksum		= &ixgbe_calc_eeprom_checksum_X540,
88862306a36Sopenharmony_ci	.validate_checksum      = &ixgbe_validate_eeprom_checksum_X540,
88962306a36Sopenharmony_ci	.update_checksum        = &ixgbe_update_eeprom_checksum_X540,
89062306a36Sopenharmony_ci};
89162306a36Sopenharmony_ci
89262306a36Sopenharmony_cistatic const struct ixgbe_phy_operations phy_ops_X540 = {
89362306a36Sopenharmony_ci	.identify               = &ixgbe_identify_phy_generic,
89462306a36Sopenharmony_ci	.identify_sfp           = &ixgbe_identify_sfp_module_generic,
89562306a36Sopenharmony_ci	.init			= NULL,
89662306a36Sopenharmony_ci	.reset                  = NULL,
89762306a36Sopenharmony_ci	.read_reg               = &ixgbe_read_phy_reg_generic,
89862306a36Sopenharmony_ci	.write_reg              = &ixgbe_write_phy_reg_generic,
89962306a36Sopenharmony_ci	.setup_link             = &ixgbe_setup_phy_link_generic,
90062306a36Sopenharmony_ci	.setup_link_speed       = &ixgbe_setup_phy_link_speed_generic,
90162306a36Sopenharmony_ci	.read_i2c_byte          = &ixgbe_read_i2c_byte_generic,
90262306a36Sopenharmony_ci	.write_i2c_byte         = &ixgbe_write_i2c_byte_generic,
90362306a36Sopenharmony_ci	.read_i2c_sff8472	= &ixgbe_read_i2c_sff8472_generic,
90462306a36Sopenharmony_ci	.read_i2c_eeprom        = &ixgbe_read_i2c_eeprom_generic,
90562306a36Sopenharmony_ci	.write_i2c_eeprom       = &ixgbe_write_i2c_eeprom_generic,
90662306a36Sopenharmony_ci	.check_overtemp         = &ixgbe_tn_check_overtemp,
90762306a36Sopenharmony_ci	.set_phy_power          = &ixgbe_set_copper_phy_power,
90862306a36Sopenharmony_ci};
90962306a36Sopenharmony_ci
91062306a36Sopenharmony_cistatic const u32 ixgbe_mvals_X540[IXGBE_MVALS_IDX_LIMIT] = {
91162306a36Sopenharmony_ci	IXGBE_MVALS_INIT(X540)
91262306a36Sopenharmony_ci};
91362306a36Sopenharmony_ci
91462306a36Sopenharmony_ciconst struct ixgbe_info ixgbe_X540_info = {
91562306a36Sopenharmony_ci	.mac                    = ixgbe_mac_X540,
91662306a36Sopenharmony_ci	.get_invariants         = &ixgbe_get_invariants_X540,
91762306a36Sopenharmony_ci	.mac_ops                = &mac_ops_X540,
91862306a36Sopenharmony_ci	.eeprom_ops             = &eeprom_ops_X540,
91962306a36Sopenharmony_ci	.phy_ops                = &phy_ops_X540,
92062306a36Sopenharmony_ci	.mbx_ops                = &mbx_ops_generic,
92162306a36Sopenharmony_ci	.mvals			= ixgbe_mvals_X540,
92262306a36Sopenharmony_ci};
923