18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright(c) 2015, 2016 Intel Corporation.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * This file is provided under a dual BSD/GPLv2 license.  When using or
58c2ecf20Sopenharmony_ci * redistributing this file, you may do so under either license.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * GPL LICENSE SUMMARY
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
108c2ecf20Sopenharmony_ci * it under the terms of version 2 of the GNU General Public License as
118c2ecf20Sopenharmony_ci * published by the Free Software Foundation.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful, but
148c2ecf20Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
158c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
168c2ecf20Sopenharmony_ci * General Public License for more details.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * BSD LICENSE
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
218c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions
228c2ecf20Sopenharmony_ci * are met:
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci *  - Redistributions of source code must retain the above copyright
258c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer.
268c2ecf20Sopenharmony_ci *  - Redistributions in binary form must reproduce the above copyright
278c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in
288c2ecf20Sopenharmony_ci *    the documentation and/or other materials provided with the
298c2ecf20Sopenharmony_ci *    distribution.
308c2ecf20Sopenharmony_ci *  - Neither the name of Intel Corporation nor the names of its
318c2ecf20Sopenharmony_ci *    contributors may be used to endorse or promote products derived
328c2ecf20Sopenharmony_ci *    from this software without specific prior written permission.
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
358c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
368c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
378c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
388c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
398c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
408c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
418c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
428c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
438c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
448c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
458c2ecf20Sopenharmony_ci *
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#include <linux/firmware.h>
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#include "hfi.h"
518c2ecf20Sopenharmony_ci#include "efivar.h"
528c2ecf20Sopenharmony_ci#include "eprom.h"
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define DEFAULT_PLATFORM_CONFIG_NAME "hfi1_platform.dat"
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic int validate_scratch_checksum(struct hfi1_devdata *dd)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	u64 checksum = 0, temp_scratch = 0;
598c2ecf20Sopenharmony_ci	int i, j, version;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	temp_scratch = read_csr(dd, ASIC_CFG_SCRATCH);
628c2ecf20Sopenharmony_ci	version = (temp_scratch & BITMAP_VERSION_SMASK) >> BITMAP_VERSION_SHIFT;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	/* Prevent power on default of all zeroes from passing checksum */
658c2ecf20Sopenharmony_ci	if (!version) {
668c2ecf20Sopenharmony_ci		dd_dev_err(dd, "%s: Config bitmap uninitialized\n", __func__);
678c2ecf20Sopenharmony_ci		dd_dev_err(dd,
688c2ecf20Sopenharmony_ci			   "%s: Please update your BIOS to support active channels\n",
698c2ecf20Sopenharmony_ci			   __func__);
708c2ecf20Sopenharmony_ci		return 0;
718c2ecf20Sopenharmony_ci	}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	/*
748c2ecf20Sopenharmony_ci	 * ASIC scratch 0 only contains the checksum and bitmap version as
758c2ecf20Sopenharmony_ci	 * fields of interest, both of which are handled separately from the
768c2ecf20Sopenharmony_ci	 * loop below, so skip it
778c2ecf20Sopenharmony_ci	 */
788c2ecf20Sopenharmony_ci	checksum += version;
798c2ecf20Sopenharmony_ci	for (i = 1; i < ASIC_NUM_SCRATCH; i++) {
808c2ecf20Sopenharmony_ci		temp_scratch = read_csr(dd, ASIC_CFG_SCRATCH + (8 * i));
818c2ecf20Sopenharmony_ci		for (j = sizeof(u64); j != 0; j -= 2) {
828c2ecf20Sopenharmony_ci			checksum += (temp_scratch & 0xFFFF);
838c2ecf20Sopenharmony_ci			temp_scratch >>= 16;
848c2ecf20Sopenharmony_ci		}
858c2ecf20Sopenharmony_ci	}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	while (checksum >> 16)
888c2ecf20Sopenharmony_ci		checksum = (checksum & CHECKSUM_MASK) + (checksum >> 16);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	temp_scratch = read_csr(dd, ASIC_CFG_SCRATCH);
918c2ecf20Sopenharmony_ci	temp_scratch &= CHECKSUM_SMASK;
928c2ecf20Sopenharmony_ci	temp_scratch >>= CHECKSUM_SHIFT;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	if (checksum + temp_scratch == 0xFFFF)
958c2ecf20Sopenharmony_ci		return 1;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	dd_dev_err(dd, "%s: Configuration bitmap corrupted\n", __func__);
988c2ecf20Sopenharmony_ci	return 0;
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic void save_platform_config_fields(struct hfi1_devdata *dd)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	struct hfi1_pportdata *ppd = dd->pport;
1048c2ecf20Sopenharmony_ci	u64 temp_scratch = 0, temp_dest = 0;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	temp_scratch = read_csr(dd, ASIC_CFG_SCRATCH_1);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	temp_dest = temp_scratch &
1098c2ecf20Sopenharmony_ci		    (dd->hfi1_id ? PORT1_PORT_TYPE_SMASK :
1108c2ecf20Sopenharmony_ci		     PORT0_PORT_TYPE_SMASK);
1118c2ecf20Sopenharmony_ci	ppd->port_type = temp_dest >>
1128c2ecf20Sopenharmony_ci			 (dd->hfi1_id ? PORT1_PORT_TYPE_SHIFT :
1138c2ecf20Sopenharmony_ci			  PORT0_PORT_TYPE_SHIFT);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	temp_dest = temp_scratch &
1168c2ecf20Sopenharmony_ci		    (dd->hfi1_id ? PORT1_LOCAL_ATTEN_SMASK :
1178c2ecf20Sopenharmony_ci		     PORT0_LOCAL_ATTEN_SMASK);
1188c2ecf20Sopenharmony_ci	ppd->local_atten = temp_dest >>
1198c2ecf20Sopenharmony_ci			   (dd->hfi1_id ? PORT1_LOCAL_ATTEN_SHIFT :
1208c2ecf20Sopenharmony_ci			    PORT0_LOCAL_ATTEN_SHIFT);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	temp_dest = temp_scratch &
1238c2ecf20Sopenharmony_ci		    (dd->hfi1_id ? PORT1_REMOTE_ATTEN_SMASK :
1248c2ecf20Sopenharmony_ci		     PORT0_REMOTE_ATTEN_SMASK);
1258c2ecf20Sopenharmony_ci	ppd->remote_atten = temp_dest >>
1268c2ecf20Sopenharmony_ci			    (dd->hfi1_id ? PORT1_REMOTE_ATTEN_SHIFT :
1278c2ecf20Sopenharmony_ci			     PORT0_REMOTE_ATTEN_SHIFT);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	temp_dest = temp_scratch &
1308c2ecf20Sopenharmony_ci		    (dd->hfi1_id ? PORT1_DEFAULT_ATTEN_SMASK :
1318c2ecf20Sopenharmony_ci		     PORT0_DEFAULT_ATTEN_SMASK);
1328c2ecf20Sopenharmony_ci	ppd->default_atten = temp_dest >>
1338c2ecf20Sopenharmony_ci			     (dd->hfi1_id ? PORT1_DEFAULT_ATTEN_SHIFT :
1348c2ecf20Sopenharmony_ci			      PORT0_DEFAULT_ATTEN_SHIFT);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	temp_scratch = read_csr(dd, dd->hfi1_id ? ASIC_CFG_SCRATCH_3 :
1378c2ecf20Sopenharmony_ci				ASIC_CFG_SCRATCH_2);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	ppd->tx_preset_eq = (temp_scratch & TX_EQ_SMASK) >> TX_EQ_SHIFT;
1408c2ecf20Sopenharmony_ci	ppd->tx_preset_noeq = (temp_scratch & TX_NO_EQ_SMASK) >> TX_NO_EQ_SHIFT;
1418c2ecf20Sopenharmony_ci	ppd->rx_preset = (temp_scratch & RX_SMASK) >> RX_SHIFT;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	ppd->max_power_class = (temp_scratch & QSFP_MAX_POWER_SMASK) >>
1448c2ecf20Sopenharmony_ci				QSFP_MAX_POWER_SHIFT;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	ppd->config_from_scratch = true;
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_civoid get_platform_config(struct hfi1_devdata *dd)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	int ret = 0;
1528c2ecf20Sopenharmony_ci	u8 *temp_platform_config = NULL;
1538c2ecf20Sopenharmony_ci	u32 esize;
1548c2ecf20Sopenharmony_ci	const struct firmware *platform_config_file = NULL;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	if (is_integrated(dd)) {
1578c2ecf20Sopenharmony_ci		if (validate_scratch_checksum(dd)) {
1588c2ecf20Sopenharmony_ci			save_platform_config_fields(dd);
1598c2ecf20Sopenharmony_ci			return;
1608c2ecf20Sopenharmony_ci		}
1618c2ecf20Sopenharmony_ci	} else {
1628c2ecf20Sopenharmony_ci		ret = eprom_read_platform_config(dd,
1638c2ecf20Sopenharmony_ci						 (void **)&temp_platform_config,
1648c2ecf20Sopenharmony_ci						 &esize);
1658c2ecf20Sopenharmony_ci		if (!ret) {
1668c2ecf20Sopenharmony_ci			/* success */
1678c2ecf20Sopenharmony_ci			dd->platform_config.data = temp_platform_config;
1688c2ecf20Sopenharmony_ci			dd->platform_config.size = esize;
1698c2ecf20Sopenharmony_ci			return;
1708c2ecf20Sopenharmony_ci		}
1718c2ecf20Sopenharmony_ci	}
1728c2ecf20Sopenharmony_ci	dd_dev_err(dd,
1738c2ecf20Sopenharmony_ci		   "%s: Failed to get platform config, falling back to sub-optimal default file\n",
1748c2ecf20Sopenharmony_ci		   __func__);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	ret = request_firmware(&platform_config_file,
1778c2ecf20Sopenharmony_ci			       DEFAULT_PLATFORM_CONFIG_NAME,
1788c2ecf20Sopenharmony_ci			       &dd->pcidev->dev);
1798c2ecf20Sopenharmony_ci	if (ret) {
1808c2ecf20Sopenharmony_ci		dd_dev_err(dd,
1818c2ecf20Sopenharmony_ci			   "%s: No default platform config file found\n",
1828c2ecf20Sopenharmony_ci			   __func__);
1838c2ecf20Sopenharmony_ci		return;
1848c2ecf20Sopenharmony_ci	}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	/*
1878c2ecf20Sopenharmony_ci	 * Allocate separate memory block to store data and free firmware
1888c2ecf20Sopenharmony_ci	 * structure. This allows free_platform_config to treat EPROM and
1898c2ecf20Sopenharmony_ci	 * fallback configs in the same manner.
1908c2ecf20Sopenharmony_ci	 */
1918c2ecf20Sopenharmony_ci	dd->platform_config.data = kmemdup(platform_config_file->data,
1928c2ecf20Sopenharmony_ci					   platform_config_file->size,
1938c2ecf20Sopenharmony_ci					   GFP_KERNEL);
1948c2ecf20Sopenharmony_ci	dd->platform_config.size = platform_config_file->size;
1958c2ecf20Sopenharmony_ci	release_firmware(platform_config_file);
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_civoid free_platform_config(struct hfi1_devdata *dd)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	/* Release memory allocated for eprom or fallback file read. */
2018c2ecf20Sopenharmony_ci	kfree(dd->platform_config.data);
2028c2ecf20Sopenharmony_ci	dd->platform_config.data = NULL;
2038c2ecf20Sopenharmony_ci}
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_civoid get_port_type(struct hfi1_pportdata *ppd)
2068c2ecf20Sopenharmony_ci{
2078c2ecf20Sopenharmony_ci	int ret;
2088c2ecf20Sopenharmony_ci	u32 temp;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	ret = get_platform_config_field(ppd->dd, PLATFORM_CONFIG_PORT_TABLE, 0,
2118c2ecf20Sopenharmony_ci					PORT_TABLE_PORT_TYPE, &temp,
2128c2ecf20Sopenharmony_ci					4);
2138c2ecf20Sopenharmony_ci	if (ret) {
2148c2ecf20Sopenharmony_ci		ppd->port_type = PORT_TYPE_UNKNOWN;
2158c2ecf20Sopenharmony_ci		return;
2168c2ecf20Sopenharmony_ci	}
2178c2ecf20Sopenharmony_ci	ppd->port_type = temp;
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ciint set_qsfp_tx(struct hfi1_pportdata *ppd, int on)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	u8 tx_ctrl_byte = on ? 0x0 : 0xF;
2238c2ecf20Sopenharmony_ci	int ret = 0;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	ret = qsfp_write(ppd, ppd->dd->hfi1_id, QSFP_TX_CTRL_BYTE_OFFS,
2268c2ecf20Sopenharmony_ci			 &tx_ctrl_byte, 1);
2278c2ecf20Sopenharmony_ci	/* we expected 1, so consider 0 an error */
2288c2ecf20Sopenharmony_ci	if (ret == 0)
2298c2ecf20Sopenharmony_ci		ret = -EIO;
2308c2ecf20Sopenharmony_ci	else if (ret == 1)
2318c2ecf20Sopenharmony_ci		ret = 0;
2328c2ecf20Sopenharmony_ci	return ret;
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cistatic int qual_power(struct hfi1_pportdata *ppd)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	u32 cable_power_class = 0, power_class_max = 0;
2388c2ecf20Sopenharmony_ci	u8 *cache = ppd->qsfp_info.cache;
2398c2ecf20Sopenharmony_ci	int ret = 0;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	ret = get_platform_config_field(
2428c2ecf20Sopenharmony_ci		ppd->dd, PLATFORM_CONFIG_SYSTEM_TABLE, 0,
2438c2ecf20Sopenharmony_ci		SYSTEM_TABLE_QSFP_POWER_CLASS_MAX, &power_class_max, 4);
2448c2ecf20Sopenharmony_ci	if (ret)
2458c2ecf20Sopenharmony_ci		return ret;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	cable_power_class = get_qsfp_power_class(cache[QSFP_MOD_PWR_OFFS]);
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	if (cable_power_class > power_class_max)
2508c2ecf20Sopenharmony_ci		ppd->offline_disabled_reason =
2518c2ecf20Sopenharmony_ci			HFI1_ODR_MASK(OPA_LINKDOWN_REASON_POWER_POLICY);
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	if (ppd->offline_disabled_reason ==
2548c2ecf20Sopenharmony_ci			HFI1_ODR_MASK(OPA_LINKDOWN_REASON_POWER_POLICY)) {
2558c2ecf20Sopenharmony_ci		dd_dev_err(
2568c2ecf20Sopenharmony_ci			ppd->dd,
2578c2ecf20Sopenharmony_ci			"%s: Port disabled due to system power restrictions\n",
2588c2ecf20Sopenharmony_ci			__func__);
2598c2ecf20Sopenharmony_ci		ret = -EPERM;
2608c2ecf20Sopenharmony_ci	}
2618c2ecf20Sopenharmony_ci	return ret;
2628c2ecf20Sopenharmony_ci}
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_cistatic int qual_bitrate(struct hfi1_pportdata *ppd)
2658c2ecf20Sopenharmony_ci{
2668c2ecf20Sopenharmony_ci	u16 lss = ppd->link_speed_supported, lse = ppd->link_speed_enabled;
2678c2ecf20Sopenharmony_ci	u8 *cache = ppd->qsfp_info.cache;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	if ((lss & OPA_LINK_SPEED_25G) && (lse & OPA_LINK_SPEED_25G) &&
2708c2ecf20Sopenharmony_ci	    cache[QSFP_NOM_BIT_RATE_250_OFFS] < 0x64)
2718c2ecf20Sopenharmony_ci		ppd->offline_disabled_reason =
2728c2ecf20Sopenharmony_ci			   HFI1_ODR_MASK(OPA_LINKDOWN_REASON_LINKSPEED_POLICY);
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	if ((lss & OPA_LINK_SPEED_12_5G) && (lse & OPA_LINK_SPEED_12_5G) &&
2758c2ecf20Sopenharmony_ci	    cache[QSFP_NOM_BIT_RATE_100_OFFS] < 0x7D)
2768c2ecf20Sopenharmony_ci		ppd->offline_disabled_reason =
2778c2ecf20Sopenharmony_ci			   HFI1_ODR_MASK(OPA_LINKDOWN_REASON_LINKSPEED_POLICY);
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	if (ppd->offline_disabled_reason ==
2808c2ecf20Sopenharmony_ci			HFI1_ODR_MASK(OPA_LINKDOWN_REASON_LINKSPEED_POLICY)) {
2818c2ecf20Sopenharmony_ci		dd_dev_err(
2828c2ecf20Sopenharmony_ci			ppd->dd,
2838c2ecf20Sopenharmony_ci			"%s: Cable failed bitrate check, disabling port\n",
2848c2ecf20Sopenharmony_ci			__func__);
2858c2ecf20Sopenharmony_ci		return -EPERM;
2868c2ecf20Sopenharmony_ci	}
2878c2ecf20Sopenharmony_ci	return 0;
2888c2ecf20Sopenharmony_ci}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_cistatic int set_qsfp_high_power(struct hfi1_pportdata *ppd)
2918c2ecf20Sopenharmony_ci{
2928c2ecf20Sopenharmony_ci	u8 cable_power_class = 0, power_ctrl_byte = 0;
2938c2ecf20Sopenharmony_ci	u8 *cache = ppd->qsfp_info.cache;
2948c2ecf20Sopenharmony_ci	int ret;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	cable_power_class = get_qsfp_power_class(cache[QSFP_MOD_PWR_OFFS]);
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	if (cable_power_class > QSFP_POWER_CLASS_1) {
2998c2ecf20Sopenharmony_ci		power_ctrl_byte = cache[QSFP_PWR_CTRL_BYTE_OFFS];
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci		power_ctrl_byte |= 1;
3028c2ecf20Sopenharmony_ci		power_ctrl_byte &= ~(0x2);
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci		ret = qsfp_write(ppd, ppd->dd->hfi1_id,
3058c2ecf20Sopenharmony_ci				 QSFP_PWR_CTRL_BYTE_OFFS,
3068c2ecf20Sopenharmony_ci				 &power_ctrl_byte, 1);
3078c2ecf20Sopenharmony_ci		if (ret != 1)
3088c2ecf20Sopenharmony_ci			return -EIO;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci		if (cable_power_class > QSFP_POWER_CLASS_4) {
3118c2ecf20Sopenharmony_ci			power_ctrl_byte |= (1 << 2);
3128c2ecf20Sopenharmony_ci			ret = qsfp_write(ppd, ppd->dd->hfi1_id,
3138c2ecf20Sopenharmony_ci					 QSFP_PWR_CTRL_BYTE_OFFS,
3148c2ecf20Sopenharmony_ci					 &power_ctrl_byte, 1);
3158c2ecf20Sopenharmony_ci			if (ret != 1)
3168c2ecf20Sopenharmony_ci				return -EIO;
3178c2ecf20Sopenharmony_ci		}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci		/* SFF 8679 rev 1.7 LPMode Deassert time */
3208c2ecf20Sopenharmony_ci		msleep(300);
3218c2ecf20Sopenharmony_ci	}
3228c2ecf20Sopenharmony_ci	return 0;
3238c2ecf20Sopenharmony_ci}
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_cistatic void apply_rx_cdr(struct hfi1_pportdata *ppd,
3268c2ecf20Sopenharmony_ci			 u32 rx_preset_index,
3278c2ecf20Sopenharmony_ci			 u8 *cdr_ctrl_byte)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	u32 rx_preset;
3308c2ecf20Sopenharmony_ci	u8 *cache = ppd->qsfp_info.cache;
3318c2ecf20Sopenharmony_ci	int cable_power_class;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	if (!((cache[QSFP_MOD_PWR_OFFS] & 0x4) &&
3348c2ecf20Sopenharmony_ci	      (cache[QSFP_CDR_INFO_OFFS] & 0x40)))
3358c2ecf20Sopenharmony_ci		return;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	/* RX CDR present, bypass supported */
3388c2ecf20Sopenharmony_ci	cable_power_class = get_qsfp_power_class(cache[QSFP_MOD_PWR_OFFS]);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	if (cable_power_class <= QSFP_POWER_CLASS_3) {
3418c2ecf20Sopenharmony_ci		/* Power class <= 3, ignore config & turn RX CDR on */
3428c2ecf20Sopenharmony_ci		*cdr_ctrl_byte |= 0xF;
3438c2ecf20Sopenharmony_ci		return;
3448c2ecf20Sopenharmony_ci	}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	get_platform_config_field(
3478c2ecf20Sopenharmony_ci		ppd->dd, PLATFORM_CONFIG_RX_PRESET_TABLE,
3488c2ecf20Sopenharmony_ci		rx_preset_index, RX_PRESET_TABLE_QSFP_RX_CDR_APPLY,
3498c2ecf20Sopenharmony_ci		&rx_preset, 4);
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	if (!rx_preset) {
3528c2ecf20Sopenharmony_ci		dd_dev_info(
3538c2ecf20Sopenharmony_ci			ppd->dd,
3548c2ecf20Sopenharmony_ci			"%s: RX_CDR_APPLY is set to disabled\n",
3558c2ecf20Sopenharmony_ci			__func__);
3568c2ecf20Sopenharmony_ci		return;
3578c2ecf20Sopenharmony_ci	}
3588c2ecf20Sopenharmony_ci	get_platform_config_field(
3598c2ecf20Sopenharmony_ci		ppd->dd, PLATFORM_CONFIG_RX_PRESET_TABLE,
3608c2ecf20Sopenharmony_ci		rx_preset_index, RX_PRESET_TABLE_QSFP_RX_CDR,
3618c2ecf20Sopenharmony_ci		&rx_preset, 4);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	/* Expand cdr setting to all 4 lanes */
3648c2ecf20Sopenharmony_ci	rx_preset = (rx_preset | (rx_preset << 1) |
3658c2ecf20Sopenharmony_ci			(rx_preset << 2) | (rx_preset << 3));
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	if (rx_preset) {
3688c2ecf20Sopenharmony_ci		*cdr_ctrl_byte |= rx_preset;
3698c2ecf20Sopenharmony_ci	} else {
3708c2ecf20Sopenharmony_ci		*cdr_ctrl_byte &= rx_preset;
3718c2ecf20Sopenharmony_ci		/* Preserve current TX CDR status */
3728c2ecf20Sopenharmony_ci		*cdr_ctrl_byte |= (cache[QSFP_CDR_CTRL_BYTE_OFFS] & 0xF0);
3738c2ecf20Sopenharmony_ci	}
3748c2ecf20Sopenharmony_ci}
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_cistatic void apply_tx_cdr(struct hfi1_pportdata *ppd,
3778c2ecf20Sopenharmony_ci			 u32 tx_preset_index,
3788c2ecf20Sopenharmony_ci			 u8 *cdr_ctrl_byte)
3798c2ecf20Sopenharmony_ci{
3808c2ecf20Sopenharmony_ci	u32 tx_preset;
3818c2ecf20Sopenharmony_ci	u8 *cache = ppd->qsfp_info.cache;
3828c2ecf20Sopenharmony_ci	int cable_power_class;
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	if (!((cache[QSFP_MOD_PWR_OFFS] & 0x8) &&
3858c2ecf20Sopenharmony_ci	      (cache[QSFP_CDR_INFO_OFFS] & 0x80)))
3868c2ecf20Sopenharmony_ci		return;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	/* TX CDR present, bypass supported */
3898c2ecf20Sopenharmony_ci	cable_power_class = get_qsfp_power_class(cache[QSFP_MOD_PWR_OFFS]);
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	if (cable_power_class <= QSFP_POWER_CLASS_3) {
3928c2ecf20Sopenharmony_ci		/* Power class <= 3, ignore config & turn TX CDR on */
3938c2ecf20Sopenharmony_ci		*cdr_ctrl_byte |= 0xF0;
3948c2ecf20Sopenharmony_ci		return;
3958c2ecf20Sopenharmony_ci	}
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	get_platform_config_field(
3988c2ecf20Sopenharmony_ci		ppd->dd,
3998c2ecf20Sopenharmony_ci		PLATFORM_CONFIG_TX_PRESET_TABLE, tx_preset_index,
4008c2ecf20Sopenharmony_ci		TX_PRESET_TABLE_QSFP_TX_CDR_APPLY, &tx_preset, 4);
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	if (!tx_preset) {
4038c2ecf20Sopenharmony_ci		dd_dev_info(
4048c2ecf20Sopenharmony_ci			ppd->dd,
4058c2ecf20Sopenharmony_ci			"%s: TX_CDR_APPLY is set to disabled\n",
4068c2ecf20Sopenharmony_ci			__func__);
4078c2ecf20Sopenharmony_ci		return;
4088c2ecf20Sopenharmony_ci	}
4098c2ecf20Sopenharmony_ci	get_platform_config_field(
4108c2ecf20Sopenharmony_ci		ppd->dd,
4118c2ecf20Sopenharmony_ci		PLATFORM_CONFIG_TX_PRESET_TABLE,
4128c2ecf20Sopenharmony_ci		tx_preset_index,
4138c2ecf20Sopenharmony_ci		TX_PRESET_TABLE_QSFP_TX_CDR, &tx_preset, 4);
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	/* Expand cdr setting to all 4 lanes */
4168c2ecf20Sopenharmony_ci	tx_preset = (tx_preset | (tx_preset << 1) |
4178c2ecf20Sopenharmony_ci			(tx_preset << 2) | (tx_preset << 3));
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	if (tx_preset)
4208c2ecf20Sopenharmony_ci		*cdr_ctrl_byte |= (tx_preset << 4);
4218c2ecf20Sopenharmony_ci	else
4228c2ecf20Sopenharmony_ci		/* Preserve current/determined RX CDR status */
4238c2ecf20Sopenharmony_ci		*cdr_ctrl_byte &= ((tx_preset << 4) | 0xF);
4248c2ecf20Sopenharmony_ci}
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_cistatic void apply_cdr_settings(
4278c2ecf20Sopenharmony_ci		struct hfi1_pportdata *ppd, u32 rx_preset_index,
4288c2ecf20Sopenharmony_ci		u32 tx_preset_index)
4298c2ecf20Sopenharmony_ci{
4308c2ecf20Sopenharmony_ci	u8 *cache = ppd->qsfp_info.cache;
4318c2ecf20Sopenharmony_ci	u8 cdr_ctrl_byte = cache[QSFP_CDR_CTRL_BYTE_OFFS];
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	apply_rx_cdr(ppd, rx_preset_index, &cdr_ctrl_byte);
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	apply_tx_cdr(ppd, tx_preset_index, &cdr_ctrl_byte);
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	qsfp_write(ppd, ppd->dd->hfi1_id, QSFP_CDR_CTRL_BYTE_OFFS,
4388c2ecf20Sopenharmony_ci		   &cdr_ctrl_byte, 1);
4398c2ecf20Sopenharmony_ci}
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_cistatic void apply_tx_eq_auto(struct hfi1_pportdata *ppd)
4428c2ecf20Sopenharmony_ci{
4438c2ecf20Sopenharmony_ci	u8 *cache = ppd->qsfp_info.cache;
4448c2ecf20Sopenharmony_ci	u8 tx_eq;
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	if (!(cache[QSFP_EQ_INFO_OFFS] & 0x8))
4478c2ecf20Sopenharmony_ci		return;
4488c2ecf20Sopenharmony_ci	/* Disable adaptive TX EQ if present */
4498c2ecf20Sopenharmony_ci	tx_eq = cache[(128 * 3) + 241];
4508c2ecf20Sopenharmony_ci	tx_eq &= 0xF0;
4518c2ecf20Sopenharmony_ci	qsfp_write(ppd, ppd->dd->hfi1_id, (256 * 3) + 241, &tx_eq, 1);
4528c2ecf20Sopenharmony_ci}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_cistatic void apply_tx_eq_prog(struct hfi1_pportdata *ppd, u32 tx_preset_index)
4558c2ecf20Sopenharmony_ci{
4568c2ecf20Sopenharmony_ci	u8 *cache = ppd->qsfp_info.cache;
4578c2ecf20Sopenharmony_ci	u32 tx_preset;
4588c2ecf20Sopenharmony_ci	u8 tx_eq;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	if (!(cache[QSFP_EQ_INFO_OFFS] & 0x4))
4618c2ecf20Sopenharmony_ci		return;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	get_platform_config_field(
4648c2ecf20Sopenharmony_ci		ppd->dd, PLATFORM_CONFIG_TX_PRESET_TABLE,
4658c2ecf20Sopenharmony_ci		tx_preset_index, TX_PRESET_TABLE_QSFP_TX_EQ_APPLY,
4668c2ecf20Sopenharmony_ci		&tx_preset, 4);
4678c2ecf20Sopenharmony_ci	if (!tx_preset) {
4688c2ecf20Sopenharmony_ci		dd_dev_info(
4698c2ecf20Sopenharmony_ci			ppd->dd,
4708c2ecf20Sopenharmony_ci			"%s: TX_EQ_APPLY is set to disabled\n",
4718c2ecf20Sopenharmony_ci			__func__);
4728c2ecf20Sopenharmony_ci		return;
4738c2ecf20Sopenharmony_ci	}
4748c2ecf20Sopenharmony_ci	get_platform_config_field(
4758c2ecf20Sopenharmony_ci			ppd->dd, PLATFORM_CONFIG_TX_PRESET_TABLE,
4768c2ecf20Sopenharmony_ci			tx_preset_index, TX_PRESET_TABLE_QSFP_TX_EQ,
4778c2ecf20Sopenharmony_ci			&tx_preset, 4);
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	if (((cache[(128 * 3) + 224] & 0xF0) >> 4) < tx_preset) {
4808c2ecf20Sopenharmony_ci		dd_dev_info(
4818c2ecf20Sopenharmony_ci			ppd->dd,
4828c2ecf20Sopenharmony_ci			"%s: TX EQ %x unsupported\n",
4838c2ecf20Sopenharmony_ci			__func__, tx_preset);
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci		dd_dev_info(
4868c2ecf20Sopenharmony_ci			ppd->dd,
4878c2ecf20Sopenharmony_ci			"%s: Applying EQ %x\n",
4888c2ecf20Sopenharmony_ci			__func__, cache[608] & 0xF0);
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci		tx_preset = (cache[608] & 0xF0) >> 4;
4918c2ecf20Sopenharmony_ci	}
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	tx_eq = tx_preset | (tx_preset << 4);
4948c2ecf20Sopenharmony_ci	qsfp_write(ppd, ppd->dd->hfi1_id, (256 * 3) + 234, &tx_eq, 1);
4958c2ecf20Sopenharmony_ci	qsfp_write(ppd, ppd->dd->hfi1_id, (256 * 3) + 235, &tx_eq, 1);
4968c2ecf20Sopenharmony_ci}
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_cistatic void apply_rx_eq_emp(struct hfi1_pportdata *ppd, u32 rx_preset_index)
4998c2ecf20Sopenharmony_ci{
5008c2ecf20Sopenharmony_ci	u32 rx_preset;
5018c2ecf20Sopenharmony_ci	u8 rx_eq, *cache = ppd->qsfp_info.cache;
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	if (!(cache[QSFP_EQ_INFO_OFFS] & 0x2))
5048c2ecf20Sopenharmony_ci		return;
5058c2ecf20Sopenharmony_ci	get_platform_config_field(
5068c2ecf20Sopenharmony_ci			ppd->dd, PLATFORM_CONFIG_RX_PRESET_TABLE,
5078c2ecf20Sopenharmony_ci			rx_preset_index, RX_PRESET_TABLE_QSFP_RX_EMP_APPLY,
5088c2ecf20Sopenharmony_ci			&rx_preset, 4);
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	if (!rx_preset) {
5118c2ecf20Sopenharmony_ci		dd_dev_info(
5128c2ecf20Sopenharmony_ci			ppd->dd,
5138c2ecf20Sopenharmony_ci			"%s: RX_EMP_APPLY is set to disabled\n",
5148c2ecf20Sopenharmony_ci			__func__);
5158c2ecf20Sopenharmony_ci		return;
5168c2ecf20Sopenharmony_ci	}
5178c2ecf20Sopenharmony_ci	get_platform_config_field(
5188c2ecf20Sopenharmony_ci		ppd->dd, PLATFORM_CONFIG_RX_PRESET_TABLE,
5198c2ecf20Sopenharmony_ci		rx_preset_index, RX_PRESET_TABLE_QSFP_RX_EMP,
5208c2ecf20Sopenharmony_ci		&rx_preset, 4);
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	if ((cache[(128 * 3) + 224] & 0xF) < rx_preset) {
5238c2ecf20Sopenharmony_ci		dd_dev_info(
5248c2ecf20Sopenharmony_ci			ppd->dd,
5258c2ecf20Sopenharmony_ci			"%s: Requested RX EMP %x\n",
5268c2ecf20Sopenharmony_ci			__func__, rx_preset);
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci		dd_dev_info(
5298c2ecf20Sopenharmony_ci			ppd->dd,
5308c2ecf20Sopenharmony_ci			"%s: Applying supported EMP %x\n",
5318c2ecf20Sopenharmony_ci			__func__, cache[608] & 0xF);
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci		rx_preset = cache[608] & 0xF;
5348c2ecf20Sopenharmony_ci	}
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	rx_eq = rx_preset | (rx_preset << 4);
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	qsfp_write(ppd, ppd->dd->hfi1_id, (256 * 3) + 236, &rx_eq, 1);
5398c2ecf20Sopenharmony_ci	qsfp_write(ppd, ppd->dd->hfi1_id, (256 * 3) + 237, &rx_eq, 1);
5408c2ecf20Sopenharmony_ci}
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_cistatic void apply_eq_settings(struct hfi1_pportdata *ppd,
5438c2ecf20Sopenharmony_ci			      u32 rx_preset_index, u32 tx_preset_index)
5448c2ecf20Sopenharmony_ci{
5458c2ecf20Sopenharmony_ci	u8 *cache = ppd->qsfp_info.cache;
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	/* no point going on w/o a page 3 */
5488c2ecf20Sopenharmony_ci	if (cache[2] & 4) {
5498c2ecf20Sopenharmony_ci		dd_dev_info(ppd->dd,
5508c2ecf20Sopenharmony_ci			    "%s: Upper page 03 not present\n",
5518c2ecf20Sopenharmony_ci			    __func__);
5528c2ecf20Sopenharmony_ci		return;
5538c2ecf20Sopenharmony_ci	}
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	apply_tx_eq_auto(ppd);
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci	apply_tx_eq_prog(ppd, tx_preset_index);
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	apply_rx_eq_emp(ppd, rx_preset_index);
5608c2ecf20Sopenharmony_ci}
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_cistatic void apply_rx_amplitude_settings(
5638c2ecf20Sopenharmony_ci		struct hfi1_pportdata *ppd, u32 rx_preset_index,
5648c2ecf20Sopenharmony_ci		u32 tx_preset_index)
5658c2ecf20Sopenharmony_ci{
5668c2ecf20Sopenharmony_ci	u32 rx_preset;
5678c2ecf20Sopenharmony_ci	u8 rx_amp = 0, i = 0, preferred = 0, *cache = ppd->qsfp_info.cache;
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	/* no point going on w/o a page 3 */
5708c2ecf20Sopenharmony_ci	if (cache[2] & 4) {
5718c2ecf20Sopenharmony_ci		dd_dev_info(ppd->dd,
5728c2ecf20Sopenharmony_ci			    "%s: Upper page 03 not present\n",
5738c2ecf20Sopenharmony_ci			    __func__);
5748c2ecf20Sopenharmony_ci		return;
5758c2ecf20Sopenharmony_ci	}
5768c2ecf20Sopenharmony_ci	if (!(cache[QSFP_EQ_INFO_OFFS] & 0x1)) {
5778c2ecf20Sopenharmony_ci		dd_dev_info(ppd->dd,
5788c2ecf20Sopenharmony_ci			    "%s: RX_AMP_APPLY is set to disabled\n",
5798c2ecf20Sopenharmony_ci			    __func__);
5808c2ecf20Sopenharmony_ci		return;
5818c2ecf20Sopenharmony_ci	}
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	get_platform_config_field(ppd->dd,
5848c2ecf20Sopenharmony_ci				  PLATFORM_CONFIG_RX_PRESET_TABLE,
5858c2ecf20Sopenharmony_ci				  rx_preset_index,
5868c2ecf20Sopenharmony_ci				  RX_PRESET_TABLE_QSFP_RX_AMP_APPLY,
5878c2ecf20Sopenharmony_ci				  &rx_preset, 4);
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	if (!rx_preset) {
5908c2ecf20Sopenharmony_ci		dd_dev_info(ppd->dd,
5918c2ecf20Sopenharmony_ci			    "%s: RX_AMP_APPLY is set to disabled\n",
5928c2ecf20Sopenharmony_ci			    __func__);
5938c2ecf20Sopenharmony_ci		return;
5948c2ecf20Sopenharmony_ci	}
5958c2ecf20Sopenharmony_ci	get_platform_config_field(ppd->dd,
5968c2ecf20Sopenharmony_ci				  PLATFORM_CONFIG_RX_PRESET_TABLE,
5978c2ecf20Sopenharmony_ci				  rx_preset_index,
5988c2ecf20Sopenharmony_ci				  RX_PRESET_TABLE_QSFP_RX_AMP,
5998c2ecf20Sopenharmony_ci				  &rx_preset, 4);
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	dd_dev_info(ppd->dd,
6028c2ecf20Sopenharmony_ci		    "%s: Requested RX AMP %x\n",
6038c2ecf20Sopenharmony_ci		    __func__,
6048c2ecf20Sopenharmony_ci		    rx_preset);
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++) {
6078c2ecf20Sopenharmony_ci		if (cache[(128 * 3) + 225] & (1 << i)) {
6088c2ecf20Sopenharmony_ci			preferred = i;
6098c2ecf20Sopenharmony_ci			if (preferred == rx_preset)
6108c2ecf20Sopenharmony_ci				break;
6118c2ecf20Sopenharmony_ci		}
6128c2ecf20Sopenharmony_ci	}
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	/*
6158c2ecf20Sopenharmony_ci	 * Verify that preferred RX amplitude is not just a
6168c2ecf20Sopenharmony_ci	 * fall through of the default
6178c2ecf20Sopenharmony_ci	 */
6188c2ecf20Sopenharmony_ci	if (!preferred && !(cache[(128 * 3) + 225] & 0x1)) {
6198c2ecf20Sopenharmony_ci		dd_dev_info(ppd->dd, "No supported RX AMP, not applying\n");
6208c2ecf20Sopenharmony_ci		return;
6218c2ecf20Sopenharmony_ci	}
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	dd_dev_info(ppd->dd,
6248c2ecf20Sopenharmony_ci		    "%s: Applying RX AMP %x\n", __func__, preferred);
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	rx_amp = preferred | (preferred << 4);
6278c2ecf20Sopenharmony_ci	qsfp_write(ppd, ppd->dd->hfi1_id, (256 * 3) + 238, &rx_amp, 1);
6288c2ecf20Sopenharmony_ci	qsfp_write(ppd, ppd->dd->hfi1_id, (256 * 3) + 239, &rx_amp, 1);
6298c2ecf20Sopenharmony_ci}
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci#define OPA_INVALID_INDEX 0xFFF
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_cistatic void apply_tx_lanes(struct hfi1_pportdata *ppd, u8 field_id,
6348c2ecf20Sopenharmony_ci			   u32 config_data, const char *message)
6358c2ecf20Sopenharmony_ci{
6368c2ecf20Sopenharmony_ci	u8 i;
6378c2ecf20Sopenharmony_ci	int ret;
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++) {
6408c2ecf20Sopenharmony_ci		ret = load_8051_config(ppd->dd, field_id, i, config_data);
6418c2ecf20Sopenharmony_ci		if (ret != HCMD_SUCCESS) {
6428c2ecf20Sopenharmony_ci			dd_dev_err(
6438c2ecf20Sopenharmony_ci				ppd->dd,
6448c2ecf20Sopenharmony_ci				"%s: %s for lane %u failed\n",
6458c2ecf20Sopenharmony_ci				message, __func__, i);
6468c2ecf20Sopenharmony_ci		}
6478c2ecf20Sopenharmony_ci	}
6488c2ecf20Sopenharmony_ci}
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci/*
6518c2ecf20Sopenharmony_ci * Return a special SerDes setting for low power AOC cables.  The power class
6528c2ecf20Sopenharmony_ci * threshold and setting being used were all found by empirical testing.
6538c2ecf20Sopenharmony_ci *
6548c2ecf20Sopenharmony_ci * Summary of the logic:
6558c2ecf20Sopenharmony_ci *
6568c2ecf20Sopenharmony_ci * if (QSFP and QSFP_TYPE == AOC and QSFP_POWER_CLASS < 4)
6578c2ecf20Sopenharmony_ci *     return 0xe
6588c2ecf20Sopenharmony_ci * return 0; // leave at default
6598c2ecf20Sopenharmony_ci */
6608c2ecf20Sopenharmony_cistatic u8 aoc_low_power_setting(struct hfi1_pportdata *ppd)
6618c2ecf20Sopenharmony_ci{
6628c2ecf20Sopenharmony_ci	u8 *cache = ppd->qsfp_info.cache;
6638c2ecf20Sopenharmony_ci	int power_class;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	/* QSFP only */
6668c2ecf20Sopenharmony_ci	if (ppd->port_type != PORT_TYPE_QSFP)
6678c2ecf20Sopenharmony_ci		return 0; /* leave at default */
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	/* active optical cables only */
6708c2ecf20Sopenharmony_ci	switch ((cache[QSFP_MOD_TECH_OFFS] & 0xF0) >> 4) {
6718c2ecf20Sopenharmony_ci	case 0x0 ... 0x9: fallthrough;
6728c2ecf20Sopenharmony_ci	case 0xC: fallthrough;
6738c2ecf20Sopenharmony_ci	case 0xE:
6748c2ecf20Sopenharmony_ci		/* active AOC */
6758c2ecf20Sopenharmony_ci		power_class = get_qsfp_power_class(cache[QSFP_MOD_PWR_OFFS]);
6768c2ecf20Sopenharmony_ci		if (power_class < QSFP_POWER_CLASS_4)
6778c2ecf20Sopenharmony_ci			return 0xe;
6788c2ecf20Sopenharmony_ci	}
6798c2ecf20Sopenharmony_ci	return 0; /* leave at default */
6808c2ecf20Sopenharmony_ci}
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_cistatic void apply_tunings(
6838c2ecf20Sopenharmony_ci		struct hfi1_pportdata *ppd, u32 tx_preset_index,
6848c2ecf20Sopenharmony_ci		u8 tuning_method, u32 total_atten, u8 limiting_active)
6858c2ecf20Sopenharmony_ci{
6868c2ecf20Sopenharmony_ci	int ret = 0;
6878c2ecf20Sopenharmony_ci	u32 config_data = 0, tx_preset = 0;
6888c2ecf20Sopenharmony_ci	u8 precur = 0, attn = 0, postcur = 0, external_device_config = 0;
6898c2ecf20Sopenharmony_ci	u8 *cache = ppd->qsfp_info.cache;
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	/* Pass tuning method to 8051 */
6928c2ecf20Sopenharmony_ci	read_8051_config(ppd->dd, LINK_TUNING_PARAMETERS, GENERAL_CONFIG,
6938c2ecf20Sopenharmony_ci			 &config_data);
6948c2ecf20Sopenharmony_ci	config_data &= ~(0xff << TUNING_METHOD_SHIFT);
6958c2ecf20Sopenharmony_ci	config_data |= ((u32)tuning_method << TUNING_METHOD_SHIFT);
6968c2ecf20Sopenharmony_ci	ret = load_8051_config(ppd->dd, LINK_TUNING_PARAMETERS, GENERAL_CONFIG,
6978c2ecf20Sopenharmony_ci			       config_data);
6988c2ecf20Sopenharmony_ci	if (ret != HCMD_SUCCESS)
6998c2ecf20Sopenharmony_ci		dd_dev_err(ppd->dd, "%s: Failed to set tuning method\n",
7008c2ecf20Sopenharmony_ci			   __func__);
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	/* Set same channel loss for both TX and RX */
7038c2ecf20Sopenharmony_ci	config_data = 0 | (total_atten << 16) | (total_atten << 24);
7048c2ecf20Sopenharmony_ci	apply_tx_lanes(ppd, CHANNEL_LOSS_SETTINGS, config_data,
7058c2ecf20Sopenharmony_ci		       "Setting channel loss");
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	/* Inform 8051 of cable capabilities */
7088c2ecf20Sopenharmony_ci	if (ppd->qsfp_info.cache_valid) {
7098c2ecf20Sopenharmony_ci		external_device_config =
7108c2ecf20Sopenharmony_ci			((cache[QSFP_MOD_PWR_OFFS] & 0x4) << 3) |
7118c2ecf20Sopenharmony_ci			((cache[QSFP_MOD_PWR_OFFS] & 0x8) << 2) |
7128c2ecf20Sopenharmony_ci			((cache[QSFP_EQ_INFO_OFFS] & 0x2) << 1) |
7138c2ecf20Sopenharmony_ci			(cache[QSFP_EQ_INFO_OFFS] & 0x4);
7148c2ecf20Sopenharmony_ci		ret = read_8051_config(ppd->dd, DC_HOST_COMM_SETTINGS,
7158c2ecf20Sopenharmony_ci				       GENERAL_CONFIG, &config_data);
7168c2ecf20Sopenharmony_ci		/* Clear, then set the external device config field */
7178c2ecf20Sopenharmony_ci		config_data &= ~(u32)0xFF;
7188c2ecf20Sopenharmony_ci		config_data |= external_device_config;
7198c2ecf20Sopenharmony_ci		ret = load_8051_config(ppd->dd, DC_HOST_COMM_SETTINGS,
7208c2ecf20Sopenharmony_ci				       GENERAL_CONFIG, config_data);
7218c2ecf20Sopenharmony_ci		if (ret != HCMD_SUCCESS)
7228c2ecf20Sopenharmony_ci			dd_dev_err(ppd->dd,
7238c2ecf20Sopenharmony_ci				   "%s: Failed set ext device config params\n",
7248c2ecf20Sopenharmony_ci				   __func__);
7258c2ecf20Sopenharmony_ci	}
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	if (tx_preset_index == OPA_INVALID_INDEX) {
7288c2ecf20Sopenharmony_ci		if (ppd->port_type == PORT_TYPE_QSFP && limiting_active)
7298c2ecf20Sopenharmony_ci			dd_dev_err(ppd->dd, "%s: Invalid Tx preset index\n",
7308c2ecf20Sopenharmony_ci				   __func__);
7318c2ecf20Sopenharmony_ci		return;
7328c2ecf20Sopenharmony_ci	}
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	/* Following for limiting active channels only */
7358c2ecf20Sopenharmony_ci	get_platform_config_field(
7368c2ecf20Sopenharmony_ci		ppd->dd, PLATFORM_CONFIG_TX_PRESET_TABLE, tx_preset_index,
7378c2ecf20Sopenharmony_ci		TX_PRESET_TABLE_PRECUR, &tx_preset, 4);
7388c2ecf20Sopenharmony_ci	precur = tx_preset;
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	get_platform_config_field(
7418c2ecf20Sopenharmony_ci		ppd->dd, PLATFORM_CONFIG_TX_PRESET_TABLE,
7428c2ecf20Sopenharmony_ci		tx_preset_index, TX_PRESET_TABLE_ATTN, &tx_preset, 4);
7438c2ecf20Sopenharmony_ci	attn = tx_preset;
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci	get_platform_config_field(
7468c2ecf20Sopenharmony_ci		ppd->dd, PLATFORM_CONFIG_TX_PRESET_TABLE,
7478c2ecf20Sopenharmony_ci		tx_preset_index, TX_PRESET_TABLE_POSTCUR, &tx_preset, 4);
7488c2ecf20Sopenharmony_ci	postcur = tx_preset;
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	/*
7518c2ecf20Sopenharmony_ci	 * NOTES:
7528c2ecf20Sopenharmony_ci	 * o The aoc_low_power_setting is applied to all lanes even
7538c2ecf20Sopenharmony_ci	 *   though only lane 0's value is examined by the firmware.
7548c2ecf20Sopenharmony_ci	 * o A lingering low power setting after a cable swap does
7558c2ecf20Sopenharmony_ci	 *   not occur.  On cable unplug the 8051 is reset and
7568c2ecf20Sopenharmony_ci	 *   restarted on cable insert.  This resets all settings to
7578c2ecf20Sopenharmony_ci	 *   their default, erasing any previous low power setting.
7588c2ecf20Sopenharmony_ci	 */
7598c2ecf20Sopenharmony_ci	config_data = precur | (attn << 8) | (postcur << 16) |
7608c2ecf20Sopenharmony_ci			(aoc_low_power_setting(ppd) << 24);
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci	apply_tx_lanes(ppd, TX_EQ_SETTINGS, config_data,
7638c2ecf20Sopenharmony_ci		       "Applying TX settings");
7648c2ecf20Sopenharmony_ci}
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci/* Must be holding the QSFP i2c resource */
7678c2ecf20Sopenharmony_cistatic int tune_active_qsfp(struct hfi1_pportdata *ppd, u32 *ptr_tx_preset,
7688c2ecf20Sopenharmony_ci			    u32 *ptr_rx_preset, u32 *ptr_total_atten)
7698c2ecf20Sopenharmony_ci{
7708c2ecf20Sopenharmony_ci	int ret;
7718c2ecf20Sopenharmony_ci	u16 lss = ppd->link_speed_supported, lse = ppd->link_speed_enabled;
7728c2ecf20Sopenharmony_ci	u8 *cache = ppd->qsfp_info.cache;
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	ppd->qsfp_info.limiting_active = 1;
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	ret = set_qsfp_tx(ppd, 0);
7778c2ecf20Sopenharmony_ci	if (ret)
7788c2ecf20Sopenharmony_ci		return ret;
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	ret = qual_power(ppd);
7818c2ecf20Sopenharmony_ci	if (ret)
7828c2ecf20Sopenharmony_ci		return ret;
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	ret = qual_bitrate(ppd);
7858c2ecf20Sopenharmony_ci	if (ret)
7868c2ecf20Sopenharmony_ci		return ret;
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci	/*
7898c2ecf20Sopenharmony_ci	 * We'll change the QSFP memory contents from here on out, thus we set a
7908c2ecf20Sopenharmony_ci	 * flag here to remind ourselves to reset the QSFP module. This prevents
7918c2ecf20Sopenharmony_ci	 * reuse of stale settings established in our previous pass through.
7928c2ecf20Sopenharmony_ci	 */
7938c2ecf20Sopenharmony_ci	if (ppd->qsfp_info.reset_needed) {
7948c2ecf20Sopenharmony_ci		ret = reset_qsfp(ppd);
7958c2ecf20Sopenharmony_ci		if (ret)
7968c2ecf20Sopenharmony_ci			return ret;
7978c2ecf20Sopenharmony_ci		refresh_qsfp_cache(ppd, &ppd->qsfp_info);
7988c2ecf20Sopenharmony_ci	} else {
7998c2ecf20Sopenharmony_ci		ppd->qsfp_info.reset_needed = 1;
8008c2ecf20Sopenharmony_ci	}
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	ret = set_qsfp_high_power(ppd);
8038c2ecf20Sopenharmony_ci	if (ret)
8048c2ecf20Sopenharmony_ci		return ret;
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	if (cache[QSFP_EQ_INFO_OFFS] & 0x4) {
8078c2ecf20Sopenharmony_ci		ret = get_platform_config_field(
8088c2ecf20Sopenharmony_ci			ppd->dd,
8098c2ecf20Sopenharmony_ci			PLATFORM_CONFIG_PORT_TABLE, 0,
8108c2ecf20Sopenharmony_ci			PORT_TABLE_TX_PRESET_IDX_ACTIVE_EQ,
8118c2ecf20Sopenharmony_ci			ptr_tx_preset, 4);
8128c2ecf20Sopenharmony_ci		if (ret) {
8138c2ecf20Sopenharmony_ci			*ptr_tx_preset = OPA_INVALID_INDEX;
8148c2ecf20Sopenharmony_ci			return ret;
8158c2ecf20Sopenharmony_ci		}
8168c2ecf20Sopenharmony_ci	} else {
8178c2ecf20Sopenharmony_ci		ret = get_platform_config_field(
8188c2ecf20Sopenharmony_ci			ppd->dd,
8198c2ecf20Sopenharmony_ci			PLATFORM_CONFIG_PORT_TABLE, 0,
8208c2ecf20Sopenharmony_ci			PORT_TABLE_TX_PRESET_IDX_ACTIVE_NO_EQ,
8218c2ecf20Sopenharmony_ci			ptr_tx_preset, 4);
8228c2ecf20Sopenharmony_ci		if (ret) {
8238c2ecf20Sopenharmony_ci			*ptr_tx_preset = OPA_INVALID_INDEX;
8248c2ecf20Sopenharmony_ci			return ret;
8258c2ecf20Sopenharmony_ci		}
8268c2ecf20Sopenharmony_ci	}
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci	ret = get_platform_config_field(
8298c2ecf20Sopenharmony_ci		ppd->dd, PLATFORM_CONFIG_PORT_TABLE, 0,
8308c2ecf20Sopenharmony_ci		PORT_TABLE_RX_PRESET_IDX, ptr_rx_preset, 4);
8318c2ecf20Sopenharmony_ci	if (ret) {
8328c2ecf20Sopenharmony_ci		*ptr_rx_preset = OPA_INVALID_INDEX;
8338c2ecf20Sopenharmony_ci		return ret;
8348c2ecf20Sopenharmony_ci	}
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	if ((lss & OPA_LINK_SPEED_25G) && (lse & OPA_LINK_SPEED_25G))
8378c2ecf20Sopenharmony_ci		get_platform_config_field(
8388c2ecf20Sopenharmony_ci			ppd->dd, PLATFORM_CONFIG_PORT_TABLE, 0,
8398c2ecf20Sopenharmony_ci			PORT_TABLE_LOCAL_ATTEN_25G, ptr_total_atten, 4);
8408c2ecf20Sopenharmony_ci	else if ((lss & OPA_LINK_SPEED_12_5G) && (lse & OPA_LINK_SPEED_12_5G))
8418c2ecf20Sopenharmony_ci		get_platform_config_field(
8428c2ecf20Sopenharmony_ci			ppd->dd, PLATFORM_CONFIG_PORT_TABLE, 0,
8438c2ecf20Sopenharmony_ci			PORT_TABLE_LOCAL_ATTEN_12G, ptr_total_atten, 4);
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	apply_cdr_settings(ppd, *ptr_rx_preset, *ptr_tx_preset);
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci	apply_eq_settings(ppd, *ptr_rx_preset, *ptr_tx_preset);
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	apply_rx_amplitude_settings(ppd, *ptr_rx_preset, *ptr_tx_preset);
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci	ret = set_qsfp_tx(ppd, 1);
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	return ret;
8548c2ecf20Sopenharmony_ci}
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_cistatic int tune_qsfp(struct hfi1_pportdata *ppd,
8578c2ecf20Sopenharmony_ci		     u32 *ptr_tx_preset, u32 *ptr_rx_preset,
8588c2ecf20Sopenharmony_ci		     u8 *ptr_tuning_method, u32 *ptr_total_atten)
8598c2ecf20Sopenharmony_ci{
8608c2ecf20Sopenharmony_ci	u32 cable_atten = 0, remote_atten = 0, platform_atten = 0;
8618c2ecf20Sopenharmony_ci	u16 lss = ppd->link_speed_supported, lse = ppd->link_speed_enabled;
8628c2ecf20Sopenharmony_ci	int ret = 0;
8638c2ecf20Sopenharmony_ci	u8 *cache = ppd->qsfp_info.cache;
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	switch ((cache[QSFP_MOD_TECH_OFFS] & 0xF0) >> 4) {
8668c2ecf20Sopenharmony_ci	case 0xA ... 0xB:
8678c2ecf20Sopenharmony_ci		ret = get_platform_config_field(
8688c2ecf20Sopenharmony_ci			ppd->dd,
8698c2ecf20Sopenharmony_ci			PLATFORM_CONFIG_PORT_TABLE, 0,
8708c2ecf20Sopenharmony_ci			PORT_TABLE_LOCAL_ATTEN_25G,
8718c2ecf20Sopenharmony_ci			&platform_atten, 4);
8728c2ecf20Sopenharmony_ci		if (ret)
8738c2ecf20Sopenharmony_ci			return ret;
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_ci		if ((lss & OPA_LINK_SPEED_25G) && (lse & OPA_LINK_SPEED_25G))
8768c2ecf20Sopenharmony_ci			cable_atten = cache[QSFP_CU_ATTEN_12G_OFFS];
8778c2ecf20Sopenharmony_ci		else if ((lss & OPA_LINK_SPEED_12_5G) &&
8788c2ecf20Sopenharmony_ci			 (lse & OPA_LINK_SPEED_12_5G))
8798c2ecf20Sopenharmony_ci			cable_atten = cache[QSFP_CU_ATTEN_7G_OFFS];
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci		/* Fallback to configured attenuation if cable memory is bad */
8828c2ecf20Sopenharmony_ci		if (cable_atten == 0 || cable_atten > 36) {
8838c2ecf20Sopenharmony_ci			ret = get_platform_config_field(
8848c2ecf20Sopenharmony_ci				ppd->dd,
8858c2ecf20Sopenharmony_ci				PLATFORM_CONFIG_SYSTEM_TABLE, 0,
8868c2ecf20Sopenharmony_ci				SYSTEM_TABLE_QSFP_ATTENUATION_DEFAULT_25G,
8878c2ecf20Sopenharmony_ci				&cable_atten, 4);
8888c2ecf20Sopenharmony_ci			if (ret)
8898c2ecf20Sopenharmony_ci				return ret;
8908c2ecf20Sopenharmony_ci		}
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci		ret = get_platform_config_field(
8938c2ecf20Sopenharmony_ci			ppd->dd, PLATFORM_CONFIG_PORT_TABLE, 0,
8948c2ecf20Sopenharmony_ci			PORT_TABLE_REMOTE_ATTEN_25G, &remote_atten, 4);
8958c2ecf20Sopenharmony_ci		if (ret)
8968c2ecf20Sopenharmony_ci			return ret;
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci		*ptr_total_atten = platform_atten + cable_atten + remote_atten;
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci		*ptr_tuning_method = OPA_PASSIVE_TUNING;
9018c2ecf20Sopenharmony_ci		break;
9028c2ecf20Sopenharmony_ci	case 0x0 ... 0x9: fallthrough;
9038c2ecf20Sopenharmony_ci	case 0xC: fallthrough;
9048c2ecf20Sopenharmony_ci	case 0xE:
9058c2ecf20Sopenharmony_ci		ret = tune_active_qsfp(ppd, ptr_tx_preset, ptr_rx_preset,
9068c2ecf20Sopenharmony_ci				       ptr_total_atten);
9078c2ecf20Sopenharmony_ci		if (ret)
9088c2ecf20Sopenharmony_ci			return ret;
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci		*ptr_tuning_method = OPA_ACTIVE_TUNING;
9118c2ecf20Sopenharmony_ci		break;
9128c2ecf20Sopenharmony_ci	case 0xD: fallthrough;
9138c2ecf20Sopenharmony_ci	case 0xF:
9148c2ecf20Sopenharmony_ci	default:
9158c2ecf20Sopenharmony_ci		dd_dev_warn(ppd->dd, "%s: Unknown/unsupported cable\n",
9168c2ecf20Sopenharmony_ci			    __func__);
9178c2ecf20Sopenharmony_ci		break;
9188c2ecf20Sopenharmony_ci	}
9198c2ecf20Sopenharmony_ci	return ret;
9208c2ecf20Sopenharmony_ci}
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_ci/*
9238c2ecf20Sopenharmony_ci * This function communicates its success or failure via ppd->driver_link_ready
9248c2ecf20Sopenharmony_ci * Thus, it depends on its association with start_link(...) which checks
9258c2ecf20Sopenharmony_ci * driver_link_ready before proceeding with the link negotiation and
9268c2ecf20Sopenharmony_ci * initialization process.
9278c2ecf20Sopenharmony_ci */
9288c2ecf20Sopenharmony_civoid tune_serdes(struct hfi1_pportdata *ppd)
9298c2ecf20Sopenharmony_ci{
9308c2ecf20Sopenharmony_ci	int ret = 0;
9318c2ecf20Sopenharmony_ci	u32 total_atten = 0;
9328c2ecf20Sopenharmony_ci	u32 remote_atten = 0, platform_atten = 0;
9338c2ecf20Sopenharmony_ci	u32 rx_preset_index, tx_preset_index;
9348c2ecf20Sopenharmony_ci	u8 tuning_method = 0, limiting_active = 0;
9358c2ecf20Sopenharmony_ci	struct hfi1_devdata *dd = ppd->dd;
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci	rx_preset_index = OPA_INVALID_INDEX;
9388c2ecf20Sopenharmony_ci	tx_preset_index = OPA_INVALID_INDEX;
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci	/* the link defaults to enabled */
9418c2ecf20Sopenharmony_ci	ppd->link_enabled = 1;
9428c2ecf20Sopenharmony_ci	/* the driver link ready state defaults to not ready */
9438c2ecf20Sopenharmony_ci	ppd->driver_link_ready = 0;
9448c2ecf20Sopenharmony_ci	ppd->offline_disabled_reason = HFI1_ODR_MASK(OPA_LINKDOWN_REASON_NONE);
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci	/* Skip the tuning for testing (loopback != none) and simulations */
9478c2ecf20Sopenharmony_ci	if (loopback != LOOPBACK_NONE ||
9488c2ecf20Sopenharmony_ci	    ppd->dd->icode == ICODE_FUNCTIONAL_SIMULATOR) {
9498c2ecf20Sopenharmony_ci		ppd->driver_link_ready = 1;
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci		if (qsfp_mod_present(ppd)) {
9528c2ecf20Sopenharmony_ci			ret = acquire_chip_resource(ppd->dd,
9538c2ecf20Sopenharmony_ci						    qsfp_resource(ppd->dd),
9548c2ecf20Sopenharmony_ci						    QSFP_WAIT);
9558c2ecf20Sopenharmony_ci			if (ret) {
9568c2ecf20Sopenharmony_ci				dd_dev_err(ppd->dd, "%s: hfi%d: cannot lock i2c chain\n",
9578c2ecf20Sopenharmony_ci					   __func__, (int)ppd->dd->hfi1_id);
9588c2ecf20Sopenharmony_ci				goto bail;
9598c2ecf20Sopenharmony_ci			}
9608c2ecf20Sopenharmony_ci
9618c2ecf20Sopenharmony_ci			refresh_qsfp_cache(ppd, &ppd->qsfp_info);
9628c2ecf20Sopenharmony_ci			release_chip_resource(ppd->dd, qsfp_resource(ppd->dd));
9638c2ecf20Sopenharmony_ci		}
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci		return;
9668c2ecf20Sopenharmony_ci	}
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_ci	switch (ppd->port_type) {
9698c2ecf20Sopenharmony_ci	case PORT_TYPE_DISCONNECTED:
9708c2ecf20Sopenharmony_ci		ppd->offline_disabled_reason =
9718c2ecf20Sopenharmony_ci			HFI1_ODR_MASK(OPA_LINKDOWN_REASON_DISCONNECTED);
9728c2ecf20Sopenharmony_ci		dd_dev_warn(dd, "%s: Port disconnected, disabling port\n",
9738c2ecf20Sopenharmony_ci			    __func__);
9748c2ecf20Sopenharmony_ci		goto bail;
9758c2ecf20Sopenharmony_ci	case PORT_TYPE_FIXED:
9768c2ecf20Sopenharmony_ci		/* platform_atten, remote_atten pre-zeroed to catch error */
9778c2ecf20Sopenharmony_ci		get_platform_config_field(
9788c2ecf20Sopenharmony_ci			ppd->dd, PLATFORM_CONFIG_PORT_TABLE, 0,
9798c2ecf20Sopenharmony_ci			PORT_TABLE_LOCAL_ATTEN_25G, &platform_atten, 4);
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci		get_platform_config_field(
9828c2ecf20Sopenharmony_ci			ppd->dd, PLATFORM_CONFIG_PORT_TABLE, 0,
9838c2ecf20Sopenharmony_ci			PORT_TABLE_REMOTE_ATTEN_25G, &remote_atten, 4);
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci		total_atten = platform_atten + remote_atten;
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci		tuning_method = OPA_PASSIVE_TUNING;
9888c2ecf20Sopenharmony_ci		break;
9898c2ecf20Sopenharmony_ci	case PORT_TYPE_VARIABLE:
9908c2ecf20Sopenharmony_ci		if (qsfp_mod_present(ppd)) {
9918c2ecf20Sopenharmony_ci			/*
9928c2ecf20Sopenharmony_ci			 * platform_atten, remote_atten pre-zeroed to
9938c2ecf20Sopenharmony_ci			 * catch error
9948c2ecf20Sopenharmony_ci			 */
9958c2ecf20Sopenharmony_ci			get_platform_config_field(
9968c2ecf20Sopenharmony_ci				ppd->dd, PLATFORM_CONFIG_PORT_TABLE, 0,
9978c2ecf20Sopenharmony_ci				PORT_TABLE_LOCAL_ATTEN_25G,
9988c2ecf20Sopenharmony_ci				&platform_atten, 4);
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci			get_platform_config_field(
10018c2ecf20Sopenharmony_ci				ppd->dd, PLATFORM_CONFIG_PORT_TABLE, 0,
10028c2ecf20Sopenharmony_ci				PORT_TABLE_REMOTE_ATTEN_25G,
10038c2ecf20Sopenharmony_ci				&remote_atten, 4);
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_ci			total_atten = platform_atten + remote_atten;
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci			tuning_method = OPA_PASSIVE_TUNING;
10088c2ecf20Sopenharmony_ci		} else {
10098c2ecf20Sopenharmony_ci			ppd->offline_disabled_reason =
10108c2ecf20Sopenharmony_ci			     HFI1_ODR_MASK(OPA_LINKDOWN_REASON_CHASSIS_CONFIG);
10118c2ecf20Sopenharmony_ci			goto bail;
10128c2ecf20Sopenharmony_ci		}
10138c2ecf20Sopenharmony_ci		break;
10148c2ecf20Sopenharmony_ci	case PORT_TYPE_QSFP:
10158c2ecf20Sopenharmony_ci		if (qsfp_mod_present(ppd)) {
10168c2ecf20Sopenharmony_ci			ret = acquire_chip_resource(ppd->dd,
10178c2ecf20Sopenharmony_ci						    qsfp_resource(ppd->dd),
10188c2ecf20Sopenharmony_ci						    QSFP_WAIT);
10198c2ecf20Sopenharmony_ci			if (ret) {
10208c2ecf20Sopenharmony_ci				dd_dev_err(ppd->dd, "%s: hfi%d: cannot lock i2c chain\n",
10218c2ecf20Sopenharmony_ci					   __func__, (int)ppd->dd->hfi1_id);
10228c2ecf20Sopenharmony_ci				goto bail;
10238c2ecf20Sopenharmony_ci			}
10248c2ecf20Sopenharmony_ci			refresh_qsfp_cache(ppd, &ppd->qsfp_info);
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_ci			if (ppd->qsfp_info.cache_valid) {
10278c2ecf20Sopenharmony_ci				ret = tune_qsfp(ppd,
10288c2ecf20Sopenharmony_ci						&tx_preset_index,
10298c2ecf20Sopenharmony_ci						&rx_preset_index,
10308c2ecf20Sopenharmony_ci						&tuning_method,
10318c2ecf20Sopenharmony_ci						&total_atten);
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_ci				/*
10348c2ecf20Sopenharmony_ci				 * We may have modified the QSFP memory, so
10358c2ecf20Sopenharmony_ci				 * update the cache to reflect the changes
10368c2ecf20Sopenharmony_ci				 */
10378c2ecf20Sopenharmony_ci				refresh_qsfp_cache(ppd, &ppd->qsfp_info);
10388c2ecf20Sopenharmony_ci				limiting_active =
10398c2ecf20Sopenharmony_ci						ppd->qsfp_info.limiting_active;
10408c2ecf20Sopenharmony_ci			} else {
10418c2ecf20Sopenharmony_ci				dd_dev_err(dd,
10428c2ecf20Sopenharmony_ci					   "%s: Reading QSFP memory failed\n",
10438c2ecf20Sopenharmony_ci					   __func__);
10448c2ecf20Sopenharmony_ci				ret = -EINVAL; /* a fail indication */
10458c2ecf20Sopenharmony_ci			}
10468c2ecf20Sopenharmony_ci			release_chip_resource(ppd->dd, qsfp_resource(ppd->dd));
10478c2ecf20Sopenharmony_ci			if (ret)
10488c2ecf20Sopenharmony_ci				goto bail;
10498c2ecf20Sopenharmony_ci		} else {
10508c2ecf20Sopenharmony_ci			ppd->offline_disabled_reason =
10518c2ecf20Sopenharmony_ci			   HFI1_ODR_MASK(
10528c2ecf20Sopenharmony_ci				OPA_LINKDOWN_REASON_LOCAL_MEDIA_NOT_INSTALLED);
10538c2ecf20Sopenharmony_ci			goto bail;
10548c2ecf20Sopenharmony_ci		}
10558c2ecf20Sopenharmony_ci		break;
10568c2ecf20Sopenharmony_ci	default:
10578c2ecf20Sopenharmony_ci		dd_dev_warn(ppd->dd, "%s: Unknown port type\n", __func__);
10588c2ecf20Sopenharmony_ci		ppd->port_type = PORT_TYPE_UNKNOWN;
10598c2ecf20Sopenharmony_ci		tuning_method = OPA_UNKNOWN_TUNING;
10608c2ecf20Sopenharmony_ci		total_atten = 0;
10618c2ecf20Sopenharmony_ci		limiting_active = 0;
10628c2ecf20Sopenharmony_ci		tx_preset_index = OPA_INVALID_INDEX;
10638c2ecf20Sopenharmony_ci		break;
10648c2ecf20Sopenharmony_ci	}
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_ci	if (ppd->offline_disabled_reason ==
10678c2ecf20Sopenharmony_ci			HFI1_ODR_MASK(OPA_LINKDOWN_REASON_NONE))
10688c2ecf20Sopenharmony_ci		apply_tunings(ppd, tx_preset_index, tuning_method,
10698c2ecf20Sopenharmony_ci			      total_atten, limiting_active);
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_ci	if (!ret)
10728c2ecf20Sopenharmony_ci		ppd->driver_link_ready = 1;
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_ci	return;
10758c2ecf20Sopenharmony_cibail:
10768c2ecf20Sopenharmony_ci	ppd->driver_link_ready = 0;
10778c2ecf20Sopenharmony_ci}
1078