18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright(c) 2015 - 2017 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#include <linux/mutex.h>
508c2ecf20Sopenharmony_ci#include <linux/module.h>
518c2ecf20Sopenharmony_ci#include <linux/delay.h>
528c2ecf20Sopenharmony_ci#include <linux/crc32.h>
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#include "hfi.h"
558c2ecf20Sopenharmony_ci#include "trace.h"
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/*
588c2ecf20Sopenharmony_ci * Make it easy to toggle firmware file name and if it gets loaded by
598c2ecf20Sopenharmony_ci * editing the following. This may be something we do while in development
608c2ecf20Sopenharmony_ci * but not necessarily something a user would ever need to use.
618c2ecf20Sopenharmony_ci */
628c2ecf20Sopenharmony_ci#define DEFAULT_FW_8051_NAME_FPGA "hfi_dc8051.bin"
638c2ecf20Sopenharmony_ci#define DEFAULT_FW_8051_NAME_ASIC "hfi1_dc8051.fw"
648c2ecf20Sopenharmony_ci#define DEFAULT_FW_FABRIC_NAME "hfi1_fabric.fw"
658c2ecf20Sopenharmony_ci#define DEFAULT_FW_SBUS_NAME "hfi1_sbus.fw"
668c2ecf20Sopenharmony_ci#define DEFAULT_FW_PCIE_NAME "hfi1_pcie.fw"
678c2ecf20Sopenharmony_ci#define ALT_FW_8051_NAME_ASIC "hfi1_dc8051_d.fw"
688c2ecf20Sopenharmony_ci#define ALT_FW_FABRIC_NAME "hfi1_fabric_d.fw"
698c2ecf20Sopenharmony_ci#define ALT_FW_SBUS_NAME "hfi1_sbus_d.fw"
708c2ecf20Sopenharmony_ci#define ALT_FW_PCIE_NAME "hfi1_pcie_d.fw"
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ciMODULE_FIRMWARE(DEFAULT_FW_8051_NAME_ASIC);
738c2ecf20Sopenharmony_ciMODULE_FIRMWARE(DEFAULT_FW_FABRIC_NAME);
748c2ecf20Sopenharmony_ciMODULE_FIRMWARE(DEFAULT_FW_SBUS_NAME);
758c2ecf20Sopenharmony_ciMODULE_FIRMWARE(DEFAULT_FW_PCIE_NAME);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic uint fw_8051_load = 1;
788c2ecf20Sopenharmony_cistatic uint fw_fabric_serdes_load = 1;
798c2ecf20Sopenharmony_cistatic uint fw_pcie_serdes_load = 1;
808c2ecf20Sopenharmony_cistatic uint fw_sbus_load = 1;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci/* Firmware file names get set in hfi1_firmware_init() based on the above */
838c2ecf20Sopenharmony_cistatic char *fw_8051_name;
848c2ecf20Sopenharmony_cistatic char *fw_fabric_serdes_name;
858c2ecf20Sopenharmony_cistatic char *fw_sbus_name;
868c2ecf20Sopenharmony_cistatic char *fw_pcie_serdes_name;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci#define SBUS_MAX_POLL_COUNT 100
898c2ecf20Sopenharmony_ci#define SBUS_COUNTER(reg, name) \
908c2ecf20Sopenharmony_ci	(((reg) >> ASIC_STS_SBUS_COUNTERS_##name##_CNT_SHIFT) & \
918c2ecf20Sopenharmony_ci	 ASIC_STS_SBUS_COUNTERS_##name##_CNT_MASK)
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci/*
948c2ecf20Sopenharmony_ci * Firmware security header.
958c2ecf20Sopenharmony_ci */
968c2ecf20Sopenharmony_cistruct css_header {
978c2ecf20Sopenharmony_ci	u32 module_type;
988c2ecf20Sopenharmony_ci	u32 header_len;
998c2ecf20Sopenharmony_ci	u32 header_version;
1008c2ecf20Sopenharmony_ci	u32 module_id;
1018c2ecf20Sopenharmony_ci	u32 module_vendor;
1028c2ecf20Sopenharmony_ci	u32 date;		/* BCD yyyymmdd */
1038c2ecf20Sopenharmony_ci	u32 size;		/* in DWORDs */
1048c2ecf20Sopenharmony_ci	u32 key_size;		/* in DWORDs */
1058c2ecf20Sopenharmony_ci	u32 modulus_size;	/* in DWORDs */
1068c2ecf20Sopenharmony_ci	u32 exponent_size;	/* in DWORDs */
1078c2ecf20Sopenharmony_ci	u32 reserved[22];
1088c2ecf20Sopenharmony_ci};
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci/* expected field values */
1118c2ecf20Sopenharmony_ci#define CSS_MODULE_TYPE	   0x00000006
1128c2ecf20Sopenharmony_ci#define CSS_HEADER_LEN	   0x000000a1
1138c2ecf20Sopenharmony_ci#define CSS_HEADER_VERSION 0x00010000
1148c2ecf20Sopenharmony_ci#define CSS_MODULE_VENDOR  0x00008086
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci#define KEY_SIZE      256
1178c2ecf20Sopenharmony_ci#define MU_SIZE		8
1188c2ecf20Sopenharmony_ci#define EXPONENT_SIZE	4
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci/* size of platform configuration partition */
1218c2ecf20Sopenharmony_ci#define MAX_PLATFORM_CONFIG_FILE_SIZE 4096
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci/* size of file of plaform configuration encoded in format version 4 */
1248c2ecf20Sopenharmony_ci#define PLATFORM_CONFIG_FORMAT_4_FILE_SIZE 528
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci/* the file itself */
1278c2ecf20Sopenharmony_cistruct firmware_file {
1288c2ecf20Sopenharmony_ci	struct css_header css_header;
1298c2ecf20Sopenharmony_ci	u8 modulus[KEY_SIZE];
1308c2ecf20Sopenharmony_ci	u8 exponent[EXPONENT_SIZE];
1318c2ecf20Sopenharmony_ci	u8 signature[KEY_SIZE];
1328c2ecf20Sopenharmony_ci	u8 firmware[];
1338c2ecf20Sopenharmony_ci};
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistruct augmented_firmware_file {
1368c2ecf20Sopenharmony_ci	struct css_header css_header;
1378c2ecf20Sopenharmony_ci	u8 modulus[KEY_SIZE];
1388c2ecf20Sopenharmony_ci	u8 exponent[EXPONENT_SIZE];
1398c2ecf20Sopenharmony_ci	u8 signature[KEY_SIZE];
1408c2ecf20Sopenharmony_ci	u8 r2[KEY_SIZE];
1418c2ecf20Sopenharmony_ci	u8 mu[MU_SIZE];
1428c2ecf20Sopenharmony_ci	u8 firmware[];
1438c2ecf20Sopenharmony_ci};
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci/* augmented file size difference */
1468c2ecf20Sopenharmony_ci#define AUGMENT_SIZE (sizeof(struct augmented_firmware_file) - \
1478c2ecf20Sopenharmony_ci						sizeof(struct firmware_file))
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistruct firmware_details {
1508c2ecf20Sopenharmony_ci	/* Linux core piece */
1518c2ecf20Sopenharmony_ci	const struct firmware *fw;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	struct css_header *css_header;
1548c2ecf20Sopenharmony_ci	u8 *firmware_ptr;		/* pointer to binary data */
1558c2ecf20Sopenharmony_ci	u32 firmware_len;		/* length in bytes */
1568c2ecf20Sopenharmony_ci	u8 *modulus;			/* pointer to the modulus */
1578c2ecf20Sopenharmony_ci	u8 *exponent;			/* pointer to the exponent */
1588c2ecf20Sopenharmony_ci	u8 *signature;			/* pointer to the signature */
1598c2ecf20Sopenharmony_ci	u8 *r2;				/* pointer to r2 */
1608c2ecf20Sopenharmony_ci	u8 *mu;				/* pointer to mu */
1618c2ecf20Sopenharmony_ci	struct augmented_firmware_file dummy_header;
1628c2ecf20Sopenharmony_ci};
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci/*
1658c2ecf20Sopenharmony_ci * The mutex protects fw_state, fw_err, and all of the firmware_details
1668c2ecf20Sopenharmony_ci * variables.
1678c2ecf20Sopenharmony_ci */
1688c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(fw_mutex);
1698c2ecf20Sopenharmony_cienum fw_state {
1708c2ecf20Sopenharmony_ci	FW_EMPTY,
1718c2ecf20Sopenharmony_ci	FW_TRY,
1728c2ecf20Sopenharmony_ci	FW_FINAL,
1738c2ecf20Sopenharmony_ci	FW_ERR
1748c2ecf20Sopenharmony_ci};
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic enum fw_state fw_state = FW_EMPTY;
1778c2ecf20Sopenharmony_cistatic int fw_err;
1788c2ecf20Sopenharmony_cistatic struct firmware_details fw_8051;
1798c2ecf20Sopenharmony_cistatic struct firmware_details fw_fabric;
1808c2ecf20Sopenharmony_cistatic struct firmware_details fw_pcie;
1818c2ecf20Sopenharmony_cistatic struct firmware_details fw_sbus;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci/* flags for turn_off_spicos() */
1848c2ecf20Sopenharmony_ci#define SPICO_SBUS   0x1
1858c2ecf20Sopenharmony_ci#define SPICO_FABRIC 0x2
1868c2ecf20Sopenharmony_ci#define ENABLE_SPICO_SMASK 0x1
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci/* security block commands */
1898c2ecf20Sopenharmony_ci#define RSA_CMD_INIT  0x1
1908c2ecf20Sopenharmony_ci#define RSA_CMD_START 0x2
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci/* security block status */
1938c2ecf20Sopenharmony_ci#define RSA_STATUS_IDLE   0x0
1948c2ecf20Sopenharmony_ci#define RSA_STATUS_ACTIVE 0x1
1958c2ecf20Sopenharmony_ci#define RSA_STATUS_DONE   0x2
1968c2ecf20Sopenharmony_ci#define RSA_STATUS_FAILED 0x3
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci/* RSA engine timeout, in ms */
1998c2ecf20Sopenharmony_ci#define RSA_ENGINE_TIMEOUT 100 /* ms */
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci/* hardware mutex timeout, in ms */
2028c2ecf20Sopenharmony_ci#define HM_TIMEOUT 10 /* ms */
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci/* 8051 memory access timeout, in us */
2058c2ecf20Sopenharmony_ci#define DC8051_ACCESS_TIMEOUT 100 /* us */
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci/* the number of fabric SerDes on the SBus */
2088c2ecf20Sopenharmony_ci#define NUM_FABRIC_SERDES 4
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci/* ASIC_STS_SBUS_RESULT.RESULT_CODE value */
2118c2ecf20Sopenharmony_ci#define SBUS_READ_COMPLETE 0x4
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci/* SBus fabric SerDes addresses, one set per HFI */
2148c2ecf20Sopenharmony_cistatic const u8 fabric_serdes_addrs[2][NUM_FABRIC_SERDES] = {
2158c2ecf20Sopenharmony_ci	{ 0x01, 0x02, 0x03, 0x04 },
2168c2ecf20Sopenharmony_ci	{ 0x28, 0x29, 0x2a, 0x2b }
2178c2ecf20Sopenharmony_ci};
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci/* SBus PCIe SerDes addresses, one set per HFI */
2208c2ecf20Sopenharmony_cistatic const u8 pcie_serdes_addrs[2][NUM_PCIE_SERDES] = {
2218c2ecf20Sopenharmony_ci	{ 0x08, 0x0a, 0x0c, 0x0e, 0x10, 0x12, 0x14, 0x16,
2228c2ecf20Sopenharmony_ci	  0x18, 0x1a, 0x1c, 0x1e, 0x20, 0x22, 0x24, 0x26 },
2238c2ecf20Sopenharmony_ci	{ 0x2f, 0x31, 0x33, 0x35, 0x37, 0x39, 0x3b, 0x3d,
2248c2ecf20Sopenharmony_ci	  0x3f, 0x41, 0x43, 0x45, 0x47, 0x49, 0x4b, 0x4d }
2258c2ecf20Sopenharmony_ci};
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci/* SBus PCIe PCS addresses, one set per HFI */
2288c2ecf20Sopenharmony_ciconst u8 pcie_pcs_addrs[2][NUM_PCIE_SERDES] = {
2298c2ecf20Sopenharmony_ci	{ 0x09, 0x0b, 0x0d, 0x0f, 0x11, 0x13, 0x15, 0x17,
2308c2ecf20Sopenharmony_ci	  0x19, 0x1b, 0x1d, 0x1f, 0x21, 0x23, 0x25, 0x27 },
2318c2ecf20Sopenharmony_ci	{ 0x30, 0x32, 0x34, 0x36, 0x38, 0x3a, 0x3c, 0x3e,
2328c2ecf20Sopenharmony_ci	  0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c, 0x4e }
2338c2ecf20Sopenharmony_ci};
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci/* SBus fabric SerDes broadcast addresses, one per HFI */
2368c2ecf20Sopenharmony_cistatic const u8 fabric_serdes_broadcast[2] = { 0xe4, 0xe5 };
2378c2ecf20Sopenharmony_cistatic const u8 all_fabric_serdes_broadcast = 0xe1;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci/* SBus PCIe SerDes broadcast addresses, one per HFI */
2408c2ecf20Sopenharmony_ciconst u8 pcie_serdes_broadcast[2] = { 0xe2, 0xe3 };
2418c2ecf20Sopenharmony_cistatic const u8 all_pcie_serdes_broadcast = 0xe0;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_cistatic const u32 platform_config_table_limits[PLATFORM_CONFIG_TABLE_MAX] = {
2448c2ecf20Sopenharmony_ci	0,
2458c2ecf20Sopenharmony_ci	SYSTEM_TABLE_MAX,
2468c2ecf20Sopenharmony_ci	PORT_TABLE_MAX,
2478c2ecf20Sopenharmony_ci	RX_PRESET_TABLE_MAX,
2488c2ecf20Sopenharmony_ci	TX_PRESET_TABLE_MAX,
2498c2ecf20Sopenharmony_ci	QSFP_ATTEN_TABLE_MAX,
2508c2ecf20Sopenharmony_ci	VARIABLE_SETTINGS_TABLE_MAX
2518c2ecf20Sopenharmony_ci};
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci/* forwards */
2548c2ecf20Sopenharmony_cistatic void dispose_one_firmware(struct firmware_details *fdet);
2558c2ecf20Sopenharmony_cistatic int load_fabric_serdes_firmware(struct hfi1_devdata *dd,
2568c2ecf20Sopenharmony_ci				       struct firmware_details *fdet);
2578c2ecf20Sopenharmony_cistatic void dump_fw_version(struct hfi1_devdata *dd);
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci/*
2608c2ecf20Sopenharmony_ci * Read a single 64-bit value from 8051 data memory.
2618c2ecf20Sopenharmony_ci *
2628c2ecf20Sopenharmony_ci * Expects:
2638c2ecf20Sopenharmony_ci * o caller to have already set up data read, no auto increment
2648c2ecf20Sopenharmony_ci * o caller to turn off read enable when finished
2658c2ecf20Sopenharmony_ci *
2668c2ecf20Sopenharmony_ci * The address argument is a byte offset.  Bits 0:2 in the address are
2678c2ecf20Sopenharmony_ci * ignored - i.e. the hardware will always do aligned 8-byte reads as if
2688c2ecf20Sopenharmony_ci * the lower bits are zero.
2698c2ecf20Sopenharmony_ci *
2708c2ecf20Sopenharmony_ci * Return 0 on success, -ENXIO on a read error (timeout).
2718c2ecf20Sopenharmony_ci */
2728c2ecf20Sopenharmony_cistatic int __read_8051_data(struct hfi1_devdata *dd, u32 addr, u64 *result)
2738c2ecf20Sopenharmony_ci{
2748c2ecf20Sopenharmony_ci	u64 reg;
2758c2ecf20Sopenharmony_ci	int count;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	/* step 1: set the address, clear enable */
2788c2ecf20Sopenharmony_ci	reg = (addr & DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_MASK)
2798c2ecf20Sopenharmony_ci			<< DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_SHIFT;
2808c2ecf20Sopenharmony_ci	write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL, reg);
2818c2ecf20Sopenharmony_ci	/* step 2: enable */
2828c2ecf20Sopenharmony_ci	write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL,
2838c2ecf20Sopenharmony_ci		  reg | DC_DC8051_CFG_RAM_ACCESS_CTRL_READ_ENA_SMASK);
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	/* wait until ACCESS_COMPLETED is set */
2868c2ecf20Sopenharmony_ci	count = 0;
2878c2ecf20Sopenharmony_ci	while ((read_csr(dd, DC_DC8051_CFG_RAM_ACCESS_STATUS)
2888c2ecf20Sopenharmony_ci		    & DC_DC8051_CFG_RAM_ACCESS_STATUS_ACCESS_COMPLETED_SMASK)
2898c2ecf20Sopenharmony_ci		    == 0) {
2908c2ecf20Sopenharmony_ci		count++;
2918c2ecf20Sopenharmony_ci		if (count > DC8051_ACCESS_TIMEOUT) {
2928c2ecf20Sopenharmony_ci			dd_dev_err(dd, "timeout reading 8051 data\n");
2938c2ecf20Sopenharmony_ci			return -ENXIO;
2948c2ecf20Sopenharmony_ci		}
2958c2ecf20Sopenharmony_ci		ndelay(10);
2968c2ecf20Sopenharmony_ci	}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	/* gather the data */
2998c2ecf20Sopenharmony_ci	*result = read_csr(dd, DC_DC8051_CFG_RAM_ACCESS_RD_DATA);
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	return 0;
3028c2ecf20Sopenharmony_ci}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci/*
3058c2ecf20Sopenharmony_ci * Read 8051 data starting at addr, for len bytes.  Will read in 8-byte chunks.
3068c2ecf20Sopenharmony_ci * Return 0 on success, -errno on error.
3078c2ecf20Sopenharmony_ci */
3088c2ecf20Sopenharmony_ciint read_8051_data(struct hfi1_devdata *dd, u32 addr, u32 len, u64 *result)
3098c2ecf20Sopenharmony_ci{
3108c2ecf20Sopenharmony_ci	unsigned long flags;
3118c2ecf20Sopenharmony_ci	u32 done;
3128c2ecf20Sopenharmony_ci	int ret = 0;
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dd->dc8051_memlock, flags);
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	/* data read set-up, no auto-increment */
3178c2ecf20Sopenharmony_ci	write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_SETUP, 0);
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	for (done = 0; done < len; addr += 8, done += 8, result++) {
3208c2ecf20Sopenharmony_ci		ret = __read_8051_data(dd, addr, result);
3218c2ecf20Sopenharmony_ci		if (ret)
3228c2ecf20Sopenharmony_ci			break;
3238c2ecf20Sopenharmony_ci	}
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	/* turn off read enable */
3268c2ecf20Sopenharmony_ci	write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL, 0);
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dd->dc8051_memlock, flags);
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	return ret;
3318c2ecf20Sopenharmony_ci}
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci/*
3348c2ecf20Sopenharmony_ci * Write data or code to the 8051 code or data RAM.
3358c2ecf20Sopenharmony_ci */
3368c2ecf20Sopenharmony_cistatic int write_8051(struct hfi1_devdata *dd, int code, u32 start,
3378c2ecf20Sopenharmony_ci		      const u8 *data, u32 len)
3388c2ecf20Sopenharmony_ci{
3398c2ecf20Sopenharmony_ci	u64 reg;
3408c2ecf20Sopenharmony_ci	u32 offset;
3418c2ecf20Sopenharmony_ci	int aligned, count;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	/* check alignment */
3448c2ecf20Sopenharmony_ci	aligned = ((unsigned long)data & 0x7) == 0;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	/* write set-up */
3478c2ecf20Sopenharmony_ci	reg = (code ? DC_DC8051_CFG_RAM_ACCESS_SETUP_RAM_SEL_SMASK : 0ull)
3488c2ecf20Sopenharmony_ci		| DC_DC8051_CFG_RAM_ACCESS_SETUP_AUTO_INCR_ADDR_SMASK;
3498c2ecf20Sopenharmony_ci	write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_SETUP, reg);
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	reg = ((start & DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_MASK)
3528c2ecf20Sopenharmony_ci			<< DC_DC8051_CFG_RAM_ACCESS_CTRL_ADDRESS_SHIFT)
3538c2ecf20Sopenharmony_ci		| DC_DC8051_CFG_RAM_ACCESS_CTRL_WRITE_ENA_SMASK;
3548c2ecf20Sopenharmony_ci	write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL, reg);
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	/* write */
3578c2ecf20Sopenharmony_ci	for (offset = 0; offset < len; offset += 8) {
3588c2ecf20Sopenharmony_ci		int bytes = len - offset;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci		if (bytes < 8) {
3618c2ecf20Sopenharmony_ci			reg = 0;
3628c2ecf20Sopenharmony_ci			memcpy(&reg, &data[offset], bytes);
3638c2ecf20Sopenharmony_ci		} else if (aligned) {
3648c2ecf20Sopenharmony_ci			reg = *(u64 *)&data[offset];
3658c2ecf20Sopenharmony_ci		} else {
3668c2ecf20Sopenharmony_ci			memcpy(&reg, &data[offset], 8);
3678c2ecf20Sopenharmony_ci		}
3688c2ecf20Sopenharmony_ci		write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_WR_DATA, reg);
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci		/* wait until ACCESS_COMPLETED is set */
3718c2ecf20Sopenharmony_ci		count = 0;
3728c2ecf20Sopenharmony_ci		while ((read_csr(dd, DC_DC8051_CFG_RAM_ACCESS_STATUS)
3738c2ecf20Sopenharmony_ci		    & DC_DC8051_CFG_RAM_ACCESS_STATUS_ACCESS_COMPLETED_SMASK)
3748c2ecf20Sopenharmony_ci		    == 0) {
3758c2ecf20Sopenharmony_ci			count++;
3768c2ecf20Sopenharmony_ci			if (count > DC8051_ACCESS_TIMEOUT) {
3778c2ecf20Sopenharmony_ci				dd_dev_err(dd, "timeout writing 8051 data\n");
3788c2ecf20Sopenharmony_ci				return -ENXIO;
3798c2ecf20Sopenharmony_ci			}
3808c2ecf20Sopenharmony_ci			udelay(1);
3818c2ecf20Sopenharmony_ci		}
3828c2ecf20Sopenharmony_ci	}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	/* turn off write access, auto increment (also sets to data access) */
3858c2ecf20Sopenharmony_ci	write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_CTRL, 0);
3868c2ecf20Sopenharmony_ci	write_csr(dd, DC_DC8051_CFG_RAM_ACCESS_SETUP, 0);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	return 0;
3898c2ecf20Sopenharmony_ci}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci/* return 0 if values match, non-zero and complain otherwise */
3928c2ecf20Sopenharmony_cistatic int invalid_header(struct hfi1_devdata *dd, const char *what,
3938c2ecf20Sopenharmony_ci			  u32 actual, u32 expected)
3948c2ecf20Sopenharmony_ci{
3958c2ecf20Sopenharmony_ci	if (actual == expected)
3968c2ecf20Sopenharmony_ci		return 0;
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	dd_dev_err(dd,
3998c2ecf20Sopenharmony_ci		   "invalid firmware header field %s: expected 0x%x, actual 0x%x\n",
4008c2ecf20Sopenharmony_ci		   what, expected, actual);
4018c2ecf20Sopenharmony_ci	return 1;
4028c2ecf20Sopenharmony_ci}
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci/*
4058c2ecf20Sopenharmony_ci * Verify that the static fields in the CSS header match.
4068c2ecf20Sopenharmony_ci */
4078c2ecf20Sopenharmony_cistatic int verify_css_header(struct hfi1_devdata *dd, struct css_header *css)
4088c2ecf20Sopenharmony_ci{
4098c2ecf20Sopenharmony_ci	/* verify CSS header fields (most sizes are in DW, so add /4) */
4108c2ecf20Sopenharmony_ci	if (invalid_header(dd, "module_type", css->module_type,
4118c2ecf20Sopenharmony_ci			   CSS_MODULE_TYPE) ||
4128c2ecf20Sopenharmony_ci	    invalid_header(dd, "header_len", css->header_len,
4138c2ecf20Sopenharmony_ci			   (sizeof(struct firmware_file) / 4)) ||
4148c2ecf20Sopenharmony_ci	    invalid_header(dd, "header_version", css->header_version,
4158c2ecf20Sopenharmony_ci			   CSS_HEADER_VERSION) ||
4168c2ecf20Sopenharmony_ci	    invalid_header(dd, "module_vendor", css->module_vendor,
4178c2ecf20Sopenharmony_ci			   CSS_MODULE_VENDOR) ||
4188c2ecf20Sopenharmony_ci	    invalid_header(dd, "key_size", css->key_size, KEY_SIZE / 4) ||
4198c2ecf20Sopenharmony_ci	    invalid_header(dd, "modulus_size", css->modulus_size,
4208c2ecf20Sopenharmony_ci			   KEY_SIZE / 4) ||
4218c2ecf20Sopenharmony_ci	    invalid_header(dd, "exponent_size", css->exponent_size,
4228c2ecf20Sopenharmony_ci			   EXPONENT_SIZE / 4)) {
4238c2ecf20Sopenharmony_ci		return -EINVAL;
4248c2ecf20Sopenharmony_ci	}
4258c2ecf20Sopenharmony_ci	return 0;
4268c2ecf20Sopenharmony_ci}
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci/*
4298c2ecf20Sopenharmony_ci * Make sure there are at least some bytes after the prefix.
4308c2ecf20Sopenharmony_ci */
4318c2ecf20Sopenharmony_cistatic int payload_check(struct hfi1_devdata *dd, const char *name,
4328c2ecf20Sopenharmony_ci			 long file_size, long prefix_size)
4338c2ecf20Sopenharmony_ci{
4348c2ecf20Sopenharmony_ci	/* make sure we have some payload */
4358c2ecf20Sopenharmony_ci	if (prefix_size >= file_size) {
4368c2ecf20Sopenharmony_ci		dd_dev_err(dd,
4378c2ecf20Sopenharmony_ci			   "firmware \"%s\", size %ld, must be larger than %ld bytes\n",
4388c2ecf20Sopenharmony_ci			   name, file_size, prefix_size);
4398c2ecf20Sopenharmony_ci		return -EINVAL;
4408c2ecf20Sopenharmony_ci	}
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	return 0;
4438c2ecf20Sopenharmony_ci}
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci/*
4468c2ecf20Sopenharmony_ci * Request the firmware from the system.  Extract the pieces and fill in
4478c2ecf20Sopenharmony_ci * fdet.  If successful, the caller will need to call dispose_one_firmware().
4488c2ecf20Sopenharmony_ci * Returns 0 on success, -ERRNO on error.
4498c2ecf20Sopenharmony_ci */
4508c2ecf20Sopenharmony_cistatic int obtain_one_firmware(struct hfi1_devdata *dd, const char *name,
4518c2ecf20Sopenharmony_ci			       struct firmware_details *fdet)
4528c2ecf20Sopenharmony_ci{
4538c2ecf20Sopenharmony_ci	struct css_header *css;
4548c2ecf20Sopenharmony_ci	int ret;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	memset(fdet, 0, sizeof(*fdet));
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	ret = request_firmware(&fdet->fw, name, &dd->pcidev->dev);
4598c2ecf20Sopenharmony_ci	if (ret) {
4608c2ecf20Sopenharmony_ci		dd_dev_warn(dd, "cannot find firmware \"%s\", err %d\n",
4618c2ecf20Sopenharmony_ci			    name, ret);
4628c2ecf20Sopenharmony_ci		return ret;
4638c2ecf20Sopenharmony_ci	}
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	/* verify the firmware */
4668c2ecf20Sopenharmony_ci	if (fdet->fw->size < sizeof(struct css_header)) {
4678c2ecf20Sopenharmony_ci		dd_dev_err(dd, "firmware \"%s\" is too small\n", name);
4688c2ecf20Sopenharmony_ci		ret = -EINVAL;
4698c2ecf20Sopenharmony_ci		goto done;
4708c2ecf20Sopenharmony_ci	}
4718c2ecf20Sopenharmony_ci	css = (struct css_header *)fdet->fw->data;
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	hfi1_cdbg(FIRMWARE, "Firmware %s details:", name);
4748c2ecf20Sopenharmony_ci	hfi1_cdbg(FIRMWARE, "file size: 0x%lx bytes", fdet->fw->size);
4758c2ecf20Sopenharmony_ci	hfi1_cdbg(FIRMWARE, "CSS structure:");
4768c2ecf20Sopenharmony_ci	hfi1_cdbg(FIRMWARE, "  module_type    0x%x", css->module_type);
4778c2ecf20Sopenharmony_ci	hfi1_cdbg(FIRMWARE, "  header_len     0x%03x (0x%03x bytes)",
4788c2ecf20Sopenharmony_ci		  css->header_len, 4 * css->header_len);
4798c2ecf20Sopenharmony_ci	hfi1_cdbg(FIRMWARE, "  header_version 0x%x", css->header_version);
4808c2ecf20Sopenharmony_ci	hfi1_cdbg(FIRMWARE, "  module_id      0x%x", css->module_id);
4818c2ecf20Sopenharmony_ci	hfi1_cdbg(FIRMWARE, "  module_vendor  0x%x", css->module_vendor);
4828c2ecf20Sopenharmony_ci	hfi1_cdbg(FIRMWARE, "  date           0x%x", css->date);
4838c2ecf20Sopenharmony_ci	hfi1_cdbg(FIRMWARE, "  size           0x%03x (0x%03x bytes)",
4848c2ecf20Sopenharmony_ci		  css->size, 4 * css->size);
4858c2ecf20Sopenharmony_ci	hfi1_cdbg(FIRMWARE, "  key_size       0x%03x (0x%03x bytes)",
4868c2ecf20Sopenharmony_ci		  css->key_size, 4 * css->key_size);
4878c2ecf20Sopenharmony_ci	hfi1_cdbg(FIRMWARE, "  modulus_size   0x%03x (0x%03x bytes)",
4888c2ecf20Sopenharmony_ci		  css->modulus_size, 4 * css->modulus_size);
4898c2ecf20Sopenharmony_ci	hfi1_cdbg(FIRMWARE, "  exponent_size  0x%03x (0x%03x bytes)",
4908c2ecf20Sopenharmony_ci		  css->exponent_size, 4 * css->exponent_size);
4918c2ecf20Sopenharmony_ci	hfi1_cdbg(FIRMWARE, "firmware size: 0x%lx bytes",
4928c2ecf20Sopenharmony_ci		  fdet->fw->size - sizeof(struct firmware_file));
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	/*
4958c2ecf20Sopenharmony_ci	 * If the file does not have a valid CSS header, fail.
4968c2ecf20Sopenharmony_ci	 * Otherwise, check the CSS size field for an expected size.
4978c2ecf20Sopenharmony_ci	 * The augmented file has r2 and mu inserted after the header
4988c2ecf20Sopenharmony_ci	 * was generated, so there will be a known difference between
4998c2ecf20Sopenharmony_ci	 * the CSS header size and the actual file size.  Use this
5008c2ecf20Sopenharmony_ci	 * difference to identify an augmented file.
5018c2ecf20Sopenharmony_ci	 *
5028c2ecf20Sopenharmony_ci	 * Note: css->size is in DWORDs, multiply by 4 to get bytes.
5038c2ecf20Sopenharmony_ci	 */
5048c2ecf20Sopenharmony_ci	ret = verify_css_header(dd, css);
5058c2ecf20Sopenharmony_ci	if (ret) {
5068c2ecf20Sopenharmony_ci		dd_dev_info(dd, "Invalid CSS header for \"%s\"\n", name);
5078c2ecf20Sopenharmony_ci	} else if ((css->size * 4) == fdet->fw->size) {
5088c2ecf20Sopenharmony_ci		/* non-augmented firmware file */
5098c2ecf20Sopenharmony_ci		struct firmware_file *ff = (struct firmware_file *)
5108c2ecf20Sopenharmony_ci							fdet->fw->data;
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci		/* make sure there are bytes in the payload */
5138c2ecf20Sopenharmony_ci		ret = payload_check(dd, name, fdet->fw->size,
5148c2ecf20Sopenharmony_ci				    sizeof(struct firmware_file));
5158c2ecf20Sopenharmony_ci		if (ret == 0) {
5168c2ecf20Sopenharmony_ci			fdet->css_header = css;
5178c2ecf20Sopenharmony_ci			fdet->modulus = ff->modulus;
5188c2ecf20Sopenharmony_ci			fdet->exponent = ff->exponent;
5198c2ecf20Sopenharmony_ci			fdet->signature = ff->signature;
5208c2ecf20Sopenharmony_ci			fdet->r2 = fdet->dummy_header.r2; /* use dummy space */
5218c2ecf20Sopenharmony_ci			fdet->mu = fdet->dummy_header.mu; /* use dummy space */
5228c2ecf20Sopenharmony_ci			fdet->firmware_ptr = ff->firmware;
5238c2ecf20Sopenharmony_ci			fdet->firmware_len = fdet->fw->size -
5248c2ecf20Sopenharmony_ci						sizeof(struct firmware_file);
5258c2ecf20Sopenharmony_ci			/*
5268c2ecf20Sopenharmony_ci			 * Header does not include r2 and mu - generate here.
5278c2ecf20Sopenharmony_ci			 * For now, fail.
5288c2ecf20Sopenharmony_ci			 */
5298c2ecf20Sopenharmony_ci			dd_dev_err(dd, "driver is unable to validate firmware without r2 and mu (not in firmware file)\n");
5308c2ecf20Sopenharmony_ci			ret = -EINVAL;
5318c2ecf20Sopenharmony_ci		}
5328c2ecf20Sopenharmony_ci	} else if ((css->size * 4) + AUGMENT_SIZE == fdet->fw->size) {
5338c2ecf20Sopenharmony_ci		/* augmented firmware file */
5348c2ecf20Sopenharmony_ci		struct augmented_firmware_file *aff =
5358c2ecf20Sopenharmony_ci			(struct augmented_firmware_file *)fdet->fw->data;
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci		/* make sure there are bytes in the payload */
5388c2ecf20Sopenharmony_ci		ret = payload_check(dd, name, fdet->fw->size,
5398c2ecf20Sopenharmony_ci				    sizeof(struct augmented_firmware_file));
5408c2ecf20Sopenharmony_ci		if (ret == 0) {
5418c2ecf20Sopenharmony_ci			fdet->css_header = css;
5428c2ecf20Sopenharmony_ci			fdet->modulus = aff->modulus;
5438c2ecf20Sopenharmony_ci			fdet->exponent = aff->exponent;
5448c2ecf20Sopenharmony_ci			fdet->signature = aff->signature;
5458c2ecf20Sopenharmony_ci			fdet->r2 = aff->r2;
5468c2ecf20Sopenharmony_ci			fdet->mu = aff->mu;
5478c2ecf20Sopenharmony_ci			fdet->firmware_ptr = aff->firmware;
5488c2ecf20Sopenharmony_ci			fdet->firmware_len = fdet->fw->size -
5498c2ecf20Sopenharmony_ci					sizeof(struct augmented_firmware_file);
5508c2ecf20Sopenharmony_ci		}
5518c2ecf20Sopenharmony_ci	} else {
5528c2ecf20Sopenharmony_ci		/* css->size check failed */
5538c2ecf20Sopenharmony_ci		dd_dev_err(dd,
5548c2ecf20Sopenharmony_ci			   "invalid firmware header field size: expected 0x%lx or 0x%lx, actual 0x%x\n",
5558c2ecf20Sopenharmony_ci			   fdet->fw->size / 4,
5568c2ecf20Sopenharmony_ci			   (fdet->fw->size - AUGMENT_SIZE) / 4,
5578c2ecf20Sopenharmony_ci			   css->size);
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci		ret = -EINVAL;
5608c2ecf20Sopenharmony_ci	}
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_cidone:
5638c2ecf20Sopenharmony_ci	/* if returning an error, clean up after ourselves */
5648c2ecf20Sopenharmony_ci	if (ret)
5658c2ecf20Sopenharmony_ci		dispose_one_firmware(fdet);
5668c2ecf20Sopenharmony_ci	return ret;
5678c2ecf20Sopenharmony_ci}
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_cistatic void dispose_one_firmware(struct firmware_details *fdet)
5708c2ecf20Sopenharmony_ci{
5718c2ecf20Sopenharmony_ci	release_firmware(fdet->fw);
5728c2ecf20Sopenharmony_ci	/* erase all previous information */
5738c2ecf20Sopenharmony_ci	memset(fdet, 0, sizeof(*fdet));
5748c2ecf20Sopenharmony_ci}
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci/*
5778c2ecf20Sopenharmony_ci * Obtain the 4 firmwares from the OS.  All must be obtained at once or not
5788c2ecf20Sopenharmony_ci * at all.  If called with the firmware state in FW_TRY, use alternate names.
5798c2ecf20Sopenharmony_ci * On exit, this routine will have set the firmware state to one of FW_TRY,
5808c2ecf20Sopenharmony_ci * FW_FINAL, or FW_ERR.
5818c2ecf20Sopenharmony_ci *
5828c2ecf20Sopenharmony_ci * Must be holding fw_mutex.
5838c2ecf20Sopenharmony_ci */
5848c2ecf20Sopenharmony_cistatic void __obtain_firmware(struct hfi1_devdata *dd)
5858c2ecf20Sopenharmony_ci{
5868c2ecf20Sopenharmony_ci	int err = 0;
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	if (fw_state == FW_FINAL)	/* nothing more to obtain */
5898c2ecf20Sopenharmony_ci		return;
5908c2ecf20Sopenharmony_ci	if (fw_state == FW_ERR)		/* already in error */
5918c2ecf20Sopenharmony_ci		return;
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	/* fw_state is FW_EMPTY or FW_TRY */
5948c2ecf20Sopenharmony_ciretry:
5958c2ecf20Sopenharmony_ci	if (fw_state == FW_TRY) {
5968c2ecf20Sopenharmony_ci		/*
5978c2ecf20Sopenharmony_ci		 * We tried the original and it failed.  Move to the
5988c2ecf20Sopenharmony_ci		 * alternate.
5998c2ecf20Sopenharmony_ci		 */
6008c2ecf20Sopenharmony_ci		dd_dev_warn(dd, "using alternate firmware names\n");
6018c2ecf20Sopenharmony_ci		/*
6028c2ecf20Sopenharmony_ci		 * Let others run.  Some systems, when missing firmware, does
6038c2ecf20Sopenharmony_ci		 * something that holds for 30 seconds.  If we do that twice
6048c2ecf20Sopenharmony_ci		 * in a row it triggers task blocked warning.
6058c2ecf20Sopenharmony_ci		 */
6068c2ecf20Sopenharmony_ci		cond_resched();
6078c2ecf20Sopenharmony_ci		if (fw_8051_load)
6088c2ecf20Sopenharmony_ci			dispose_one_firmware(&fw_8051);
6098c2ecf20Sopenharmony_ci		if (fw_fabric_serdes_load)
6108c2ecf20Sopenharmony_ci			dispose_one_firmware(&fw_fabric);
6118c2ecf20Sopenharmony_ci		if (fw_sbus_load)
6128c2ecf20Sopenharmony_ci			dispose_one_firmware(&fw_sbus);
6138c2ecf20Sopenharmony_ci		if (fw_pcie_serdes_load)
6148c2ecf20Sopenharmony_ci			dispose_one_firmware(&fw_pcie);
6158c2ecf20Sopenharmony_ci		fw_8051_name = ALT_FW_8051_NAME_ASIC;
6168c2ecf20Sopenharmony_ci		fw_fabric_serdes_name = ALT_FW_FABRIC_NAME;
6178c2ecf20Sopenharmony_ci		fw_sbus_name = ALT_FW_SBUS_NAME;
6188c2ecf20Sopenharmony_ci		fw_pcie_serdes_name = ALT_FW_PCIE_NAME;
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci		/*
6218c2ecf20Sopenharmony_ci		 * Add a delay before obtaining and loading debug firmware.
6228c2ecf20Sopenharmony_ci		 * Authorization will fail if the delay between firmware
6238c2ecf20Sopenharmony_ci		 * authorization events is shorter than 50us. Add 100us to
6248c2ecf20Sopenharmony_ci		 * make a delay time safe.
6258c2ecf20Sopenharmony_ci		 */
6268c2ecf20Sopenharmony_ci		usleep_range(100, 120);
6278c2ecf20Sopenharmony_ci	}
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	if (fw_sbus_load) {
6308c2ecf20Sopenharmony_ci		err = obtain_one_firmware(dd, fw_sbus_name, &fw_sbus);
6318c2ecf20Sopenharmony_ci		if (err)
6328c2ecf20Sopenharmony_ci			goto done;
6338c2ecf20Sopenharmony_ci	}
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	if (fw_pcie_serdes_load) {
6368c2ecf20Sopenharmony_ci		err = obtain_one_firmware(dd, fw_pcie_serdes_name, &fw_pcie);
6378c2ecf20Sopenharmony_ci		if (err)
6388c2ecf20Sopenharmony_ci			goto done;
6398c2ecf20Sopenharmony_ci	}
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	if (fw_fabric_serdes_load) {
6428c2ecf20Sopenharmony_ci		err = obtain_one_firmware(dd, fw_fabric_serdes_name,
6438c2ecf20Sopenharmony_ci					  &fw_fabric);
6448c2ecf20Sopenharmony_ci		if (err)
6458c2ecf20Sopenharmony_ci			goto done;
6468c2ecf20Sopenharmony_ci	}
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	if (fw_8051_load) {
6498c2ecf20Sopenharmony_ci		err = obtain_one_firmware(dd, fw_8051_name, &fw_8051);
6508c2ecf20Sopenharmony_ci		if (err)
6518c2ecf20Sopenharmony_ci			goto done;
6528c2ecf20Sopenharmony_ci	}
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_cidone:
6558c2ecf20Sopenharmony_ci	if (err) {
6568c2ecf20Sopenharmony_ci		/* oops, had problems obtaining a firmware */
6578c2ecf20Sopenharmony_ci		if (fw_state == FW_EMPTY && dd->icode == ICODE_RTL_SILICON) {
6588c2ecf20Sopenharmony_ci			/* retry with alternate (RTL only) */
6598c2ecf20Sopenharmony_ci			fw_state = FW_TRY;
6608c2ecf20Sopenharmony_ci			goto retry;
6618c2ecf20Sopenharmony_ci		}
6628c2ecf20Sopenharmony_ci		dd_dev_err(dd, "unable to obtain working firmware\n");
6638c2ecf20Sopenharmony_ci		fw_state = FW_ERR;
6648c2ecf20Sopenharmony_ci		fw_err = -ENOENT;
6658c2ecf20Sopenharmony_ci	} else {
6668c2ecf20Sopenharmony_ci		/* success */
6678c2ecf20Sopenharmony_ci		if (fw_state == FW_EMPTY &&
6688c2ecf20Sopenharmony_ci		    dd->icode != ICODE_FUNCTIONAL_SIMULATOR)
6698c2ecf20Sopenharmony_ci			fw_state = FW_TRY;	/* may retry later */
6708c2ecf20Sopenharmony_ci		else
6718c2ecf20Sopenharmony_ci			fw_state = FW_FINAL;	/* cannot try again */
6728c2ecf20Sopenharmony_ci	}
6738c2ecf20Sopenharmony_ci}
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci/*
6768c2ecf20Sopenharmony_ci * Called by all HFIs when loading their firmware - i.e. device probe time.
6778c2ecf20Sopenharmony_ci * The first one will do the actual firmware load.  Use a mutex to resolve
6788c2ecf20Sopenharmony_ci * any possible race condition.
6798c2ecf20Sopenharmony_ci *
6808c2ecf20Sopenharmony_ci * The call to this routine cannot be moved to driver load because the kernel
6818c2ecf20Sopenharmony_ci * call request_firmware() requires a device which is only available after
6828c2ecf20Sopenharmony_ci * the first device probe.
6838c2ecf20Sopenharmony_ci */
6848c2ecf20Sopenharmony_cistatic int obtain_firmware(struct hfi1_devdata *dd)
6858c2ecf20Sopenharmony_ci{
6868c2ecf20Sopenharmony_ci	unsigned long timeout;
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	mutex_lock(&fw_mutex);
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	/* 40s delay due to long delay on missing firmware on some systems */
6918c2ecf20Sopenharmony_ci	timeout = jiffies + msecs_to_jiffies(40000);
6928c2ecf20Sopenharmony_ci	while (fw_state == FW_TRY) {
6938c2ecf20Sopenharmony_ci		/*
6948c2ecf20Sopenharmony_ci		 * Another device is trying the firmware.  Wait until it
6958c2ecf20Sopenharmony_ci		 * decides what works (or not).
6968c2ecf20Sopenharmony_ci		 */
6978c2ecf20Sopenharmony_ci		if (time_after(jiffies, timeout)) {
6988c2ecf20Sopenharmony_ci			/* waited too long */
6998c2ecf20Sopenharmony_ci			dd_dev_err(dd, "Timeout waiting for firmware try");
7008c2ecf20Sopenharmony_ci			fw_state = FW_ERR;
7018c2ecf20Sopenharmony_ci			fw_err = -ETIMEDOUT;
7028c2ecf20Sopenharmony_ci			break;
7038c2ecf20Sopenharmony_ci		}
7048c2ecf20Sopenharmony_ci		mutex_unlock(&fw_mutex);
7058c2ecf20Sopenharmony_ci		msleep(20);	/* arbitrary delay */
7068c2ecf20Sopenharmony_ci		mutex_lock(&fw_mutex);
7078c2ecf20Sopenharmony_ci	}
7088c2ecf20Sopenharmony_ci	/* not in FW_TRY state */
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	/* set fw_state to FW_TRY, FW_FINAL, or FW_ERR, and fw_err */
7118c2ecf20Sopenharmony_ci	if (fw_state == FW_EMPTY)
7128c2ecf20Sopenharmony_ci		__obtain_firmware(dd);
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	mutex_unlock(&fw_mutex);
7158c2ecf20Sopenharmony_ci	return fw_err;
7168c2ecf20Sopenharmony_ci}
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci/*
7198c2ecf20Sopenharmony_ci * Called when the driver unloads.  The timing is asymmetric with its
7208c2ecf20Sopenharmony_ci * counterpart, obtain_firmware().  If called at device remove time,
7218c2ecf20Sopenharmony_ci * then it is conceivable that another device could probe while the
7228c2ecf20Sopenharmony_ci * firmware is being disposed.  The mutexes can be moved to do that
7238c2ecf20Sopenharmony_ci * safely, but then the firmware would be requested from the OS multiple
7248c2ecf20Sopenharmony_ci * times.
7258c2ecf20Sopenharmony_ci *
7268c2ecf20Sopenharmony_ci * No mutex is needed as the driver is unloading and there cannot be any
7278c2ecf20Sopenharmony_ci * other callers.
7288c2ecf20Sopenharmony_ci */
7298c2ecf20Sopenharmony_civoid dispose_firmware(void)
7308c2ecf20Sopenharmony_ci{
7318c2ecf20Sopenharmony_ci	dispose_one_firmware(&fw_8051);
7328c2ecf20Sopenharmony_ci	dispose_one_firmware(&fw_fabric);
7338c2ecf20Sopenharmony_ci	dispose_one_firmware(&fw_pcie);
7348c2ecf20Sopenharmony_ci	dispose_one_firmware(&fw_sbus);
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci	/* retain the error state, otherwise revert to empty */
7378c2ecf20Sopenharmony_ci	if (fw_state != FW_ERR)
7388c2ecf20Sopenharmony_ci		fw_state = FW_EMPTY;
7398c2ecf20Sopenharmony_ci}
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci/*
7428c2ecf20Sopenharmony_ci * Called with the result of a firmware download.
7438c2ecf20Sopenharmony_ci *
7448c2ecf20Sopenharmony_ci * Return 1 to retry loading the firmware, 0 to stop.
7458c2ecf20Sopenharmony_ci */
7468c2ecf20Sopenharmony_cistatic int retry_firmware(struct hfi1_devdata *dd, int load_result)
7478c2ecf20Sopenharmony_ci{
7488c2ecf20Sopenharmony_ci	int retry;
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	mutex_lock(&fw_mutex);
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	if (load_result == 0) {
7538c2ecf20Sopenharmony_ci		/*
7548c2ecf20Sopenharmony_ci		 * The load succeeded, so expect all others to do the same.
7558c2ecf20Sopenharmony_ci		 * Do not retry again.
7568c2ecf20Sopenharmony_ci		 */
7578c2ecf20Sopenharmony_ci		if (fw_state == FW_TRY)
7588c2ecf20Sopenharmony_ci			fw_state = FW_FINAL;
7598c2ecf20Sopenharmony_ci		retry = 0;	/* do NOT retry */
7608c2ecf20Sopenharmony_ci	} else if (fw_state == FW_TRY) {
7618c2ecf20Sopenharmony_ci		/* load failed, obtain alternate firmware */
7628c2ecf20Sopenharmony_ci		__obtain_firmware(dd);
7638c2ecf20Sopenharmony_ci		retry = (fw_state == FW_FINAL);
7648c2ecf20Sopenharmony_ci	} else {
7658c2ecf20Sopenharmony_ci		/* else in FW_FINAL or FW_ERR, no retry in either case */
7668c2ecf20Sopenharmony_ci		retry = 0;
7678c2ecf20Sopenharmony_ci	}
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	mutex_unlock(&fw_mutex);
7708c2ecf20Sopenharmony_ci	return retry;
7718c2ecf20Sopenharmony_ci}
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci/*
7748c2ecf20Sopenharmony_ci * Write a block of data to a given array CSR.  All calls will be in
7758c2ecf20Sopenharmony_ci * multiples of 8 bytes.
7768c2ecf20Sopenharmony_ci */
7778c2ecf20Sopenharmony_cistatic void write_rsa_data(struct hfi1_devdata *dd, int what,
7788c2ecf20Sopenharmony_ci			   const u8 *data, int nbytes)
7798c2ecf20Sopenharmony_ci{
7808c2ecf20Sopenharmony_ci	int qw_size = nbytes / 8;
7818c2ecf20Sopenharmony_ci	int i;
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	if (((unsigned long)data & 0x7) == 0) {
7848c2ecf20Sopenharmony_ci		/* aligned */
7858c2ecf20Sopenharmony_ci		u64 *ptr = (u64 *)data;
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci		for (i = 0; i < qw_size; i++, ptr++)
7888c2ecf20Sopenharmony_ci			write_csr(dd, what + (8 * i), *ptr);
7898c2ecf20Sopenharmony_ci	} else {
7908c2ecf20Sopenharmony_ci		/* not aligned */
7918c2ecf20Sopenharmony_ci		for (i = 0; i < qw_size; i++, data += 8) {
7928c2ecf20Sopenharmony_ci			u64 value;
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci			memcpy(&value, data, 8);
7958c2ecf20Sopenharmony_ci			write_csr(dd, what + (8 * i), value);
7968c2ecf20Sopenharmony_ci		}
7978c2ecf20Sopenharmony_ci	}
7988c2ecf20Sopenharmony_ci}
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci/*
8018c2ecf20Sopenharmony_ci * Write a block of data to a given CSR as a stream of writes.  All calls will
8028c2ecf20Sopenharmony_ci * be in multiples of 8 bytes.
8038c2ecf20Sopenharmony_ci */
8048c2ecf20Sopenharmony_cistatic void write_streamed_rsa_data(struct hfi1_devdata *dd, int what,
8058c2ecf20Sopenharmony_ci				    const u8 *data, int nbytes)
8068c2ecf20Sopenharmony_ci{
8078c2ecf20Sopenharmony_ci	u64 *ptr = (u64 *)data;
8088c2ecf20Sopenharmony_ci	int qw_size = nbytes / 8;
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci	for (; qw_size > 0; qw_size--, ptr++)
8118c2ecf20Sopenharmony_ci		write_csr(dd, what, *ptr);
8128c2ecf20Sopenharmony_ci}
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci/*
8158c2ecf20Sopenharmony_ci * Download the signature and start the RSA mechanism.  Wait for
8168c2ecf20Sopenharmony_ci * RSA_ENGINE_TIMEOUT before giving up.
8178c2ecf20Sopenharmony_ci */
8188c2ecf20Sopenharmony_cistatic int run_rsa(struct hfi1_devdata *dd, const char *who,
8198c2ecf20Sopenharmony_ci		   const u8 *signature)
8208c2ecf20Sopenharmony_ci{
8218c2ecf20Sopenharmony_ci	unsigned long timeout;
8228c2ecf20Sopenharmony_ci	u64 reg;
8238c2ecf20Sopenharmony_ci	u32 status;
8248c2ecf20Sopenharmony_ci	int ret = 0;
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci	/* write the signature */
8278c2ecf20Sopenharmony_ci	write_rsa_data(dd, MISC_CFG_RSA_SIGNATURE, signature, KEY_SIZE);
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci	/* initialize RSA */
8308c2ecf20Sopenharmony_ci	write_csr(dd, MISC_CFG_RSA_CMD, RSA_CMD_INIT);
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci	/*
8338c2ecf20Sopenharmony_ci	 * Make sure the engine is idle and insert a delay between the two
8348c2ecf20Sopenharmony_ci	 * writes to MISC_CFG_RSA_CMD.
8358c2ecf20Sopenharmony_ci	 */
8368c2ecf20Sopenharmony_ci	status = (read_csr(dd, MISC_CFG_FW_CTRL)
8378c2ecf20Sopenharmony_ci			   & MISC_CFG_FW_CTRL_RSA_STATUS_SMASK)
8388c2ecf20Sopenharmony_ci			     >> MISC_CFG_FW_CTRL_RSA_STATUS_SHIFT;
8398c2ecf20Sopenharmony_ci	if (status != RSA_STATUS_IDLE) {
8408c2ecf20Sopenharmony_ci		dd_dev_err(dd, "%s security engine not idle - giving up\n",
8418c2ecf20Sopenharmony_ci			   who);
8428c2ecf20Sopenharmony_ci		return -EBUSY;
8438c2ecf20Sopenharmony_ci	}
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	/* start RSA */
8468c2ecf20Sopenharmony_ci	write_csr(dd, MISC_CFG_RSA_CMD, RSA_CMD_START);
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci	/*
8498c2ecf20Sopenharmony_ci	 * Look for the result.
8508c2ecf20Sopenharmony_ci	 *
8518c2ecf20Sopenharmony_ci	 * The RSA engine is hooked up to two MISC errors.  The driver
8528c2ecf20Sopenharmony_ci	 * masks these errors as they do not respond to the standard
8538c2ecf20Sopenharmony_ci	 * error "clear down" mechanism.  Look for these errors here and
8548c2ecf20Sopenharmony_ci	 * clear them when possible.  This routine will exit with the
8558c2ecf20Sopenharmony_ci	 * errors of the current run still set.
8568c2ecf20Sopenharmony_ci	 *
8578c2ecf20Sopenharmony_ci	 * MISC_FW_AUTH_FAILED_ERR
8588c2ecf20Sopenharmony_ci	 *	Firmware authorization failed.  This can be cleared by
8598c2ecf20Sopenharmony_ci	 *	re-initializing the RSA engine, then clearing the status bit.
8608c2ecf20Sopenharmony_ci	 *	Do not re-init the RSA angine immediately after a successful
8618c2ecf20Sopenharmony_ci	 *	run - this will reset the current authorization.
8628c2ecf20Sopenharmony_ci	 *
8638c2ecf20Sopenharmony_ci	 * MISC_KEY_MISMATCH_ERR
8648c2ecf20Sopenharmony_ci	 *	Key does not match.  The only way to clear this is to load
8658c2ecf20Sopenharmony_ci	 *	a matching key then clear the status bit.  If this error
8668c2ecf20Sopenharmony_ci	 *	is raised, it will persist outside of this routine until a
8678c2ecf20Sopenharmony_ci	 *	matching key is loaded.
8688c2ecf20Sopenharmony_ci	 */
8698c2ecf20Sopenharmony_ci	timeout = msecs_to_jiffies(RSA_ENGINE_TIMEOUT) + jiffies;
8708c2ecf20Sopenharmony_ci	while (1) {
8718c2ecf20Sopenharmony_ci		status = (read_csr(dd, MISC_CFG_FW_CTRL)
8728c2ecf20Sopenharmony_ci			   & MISC_CFG_FW_CTRL_RSA_STATUS_SMASK)
8738c2ecf20Sopenharmony_ci			     >> MISC_CFG_FW_CTRL_RSA_STATUS_SHIFT;
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_ci		if (status == RSA_STATUS_IDLE) {
8768c2ecf20Sopenharmony_ci			/* should not happen */
8778c2ecf20Sopenharmony_ci			dd_dev_err(dd, "%s firmware security bad idle state\n",
8788c2ecf20Sopenharmony_ci				   who);
8798c2ecf20Sopenharmony_ci			ret = -EINVAL;
8808c2ecf20Sopenharmony_ci			break;
8818c2ecf20Sopenharmony_ci		} else if (status == RSA_STATUS_DONE) {
8828c2ecf20Sopenharmony_ci			/* finished successfully */
8838c2ecf20Sopenharmony_ci			break;
8848c2ecf20Sopenharmony_ci		} else if (status == RSA_STATUS_FAILED) {
8858c2ecf20Sopenharmony_ci			/* finished unsuccessfully */
8868c2ecf20Sopenharmony_ci			ret = -EINVAL;
8878c2ecf20Sopenharmony_ci			break;
8888c2ecf20Sopenharmony_ci		}
8898c2ecf20Sopenharmony_ci		/* else still active */
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci		if (time_after(jiffies, timeout)) {
8928c2ecf20Sopenharmony_ci			/*
8938c2ecf20Sopenharmony_ci			 * Timed out while active.  We can't reset the engine
8948c2ecf20Sopenharmony_ci			 * if it is stuck active, but run through the
8958c2ecf20Sopenharmony_ci			 * error code to see what error bits are set.
8968c2ecf20Sopenharmony_ci			 */
8978c2ecf20Sopenharmony_ci			dd_dev_err(dd, "%s firmware security time out\n", who);
8988c2ecf20Sopenharmony_ci			ret = -ETIMEDOUT;
8998c2ecf20Sopenharmony_ci			break;
9008c2ecf20Sopenharmony_ci		}
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_ci		msleep(20);
9038c2ecf20Sopenharmony_ci	}
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	/*
9068c2ecf20Sopenharmony_ci	 * Arrive here on success or failure.  Clear all RSA engine
9078c2ecf20Sopenharmony_ci	 * errors.  All current errors will stick - the RSA logic is keeping
9088c2ecf20Sopenharmony_ci	 * error high.  All previous errors will clear - the RSA logic
9098c2ecf20Sopenharmony_ci	 * is not keeping the error high.
9108c2ecf20Sopenharmony_ci	 */
9118c2ecf20Sopenharmony_ci	write_csr(dd, MISC_ERR_CLEAR,
9128c2ecf20Sopenharmony_ci		  MISC_ERR_STATUS_MISC_FW_AUTH_FAILED_ERR_SMASK |
9138c2ecf20Sopenharmony_ci		  MISC_ERR_STATUS_MISC_KEY_MISMATCH_ERR_SMASK);
9148c2ecf20Sopenharmony_ci	/*
9158c2ecf20Sopenharmony_ci	 * All that is left are the current errors.  Print warnings on
9168c2ecf20Sopenharmony_ci	 * authorization failure details, if any.  Firmware authorization
9178c2ecf20Sopenharmony_ci	 * can be retried, so these are only warnings.
9188c2ecf20Sopenharmony_ci	 */
9198c2ecf20Sopenharmony_ci	reg = read_csr(dd, MISC_ERR_STATUS);
9208c2ecf20Sopenharmony_ci	if (ret) {
9218c2ecf20Sopenharmony_ci		if (reg & MISC_ERR_STATUS_MISC_FW_AUTH_FAILED_ERR_SMASK)
9228c2ecf20Sopenharmony_ci			dd_dev_warn(dd, "%s firmware authorization failed\n",
9238c2ecf20Sopenharmony_ci				    who);
9248c2ecf20Sopenharmony_ci		if (reg & MISC_ERR_STATUS_MISC_KEY_MISMATCH_ERR_SMASK)
9258c2ecf20Sopenharmony_ci			dd_dev_warn(dd, "%s firmware key mismatch\n", who);
9268c2ecf20Sopenharmony_ci	}
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci	return ret;
9298c2ecf20Sopenharmony_ci}
9308c2ecf20Sopenharmony_ci
9318c2ecf20Sopenharmony_cistatic void load_security_variables(struct hfi1_devdata *dd,
9328c2ecf20Sopenharmony_ci				    struct firmware_details *fdet)
9338c2ecf20Sopenharmony_ci{
9348c2ecf20Sopenharmony_ci	/* Security variables a.  Write the modulus */
9358c2ecf20Sopenharmony_ci	write_rsa_data(dd, MISC_CFG_RSA_MODULUS, fdet->modulus, KEY_SIZE);
9368c2ecf20Sopenharmony_ci	/* Security variables b.  Write the r2 */
9378c2ecf20Sopenharmony_ci	write_rsa_data(dd, MISC_CFG_RSA_R2, fdet->r2, KEY_SIZE);
9388c2ecf20Sopenharmony_ci	/* Security variables c.  Write the mu */
9398c2ecf20Sopenharmony_ci	write_rsa_data(dd, MISC_CFG_RSA_MU, fdet->mu, MU_SIZE);
9408c2ecf20Sopenharmony_ci	/* Security variables d.  Write the header */
9418c2ecf20Sopenharmony_ci	write_streamed_rsa_data(dd, MISC_CFG_SHA_PRELOAD,
9428c2ecf20Sopenharmony_ci				(u8 *)fdet->css_header,
9438c2ecf20Sopenharmony_ci				sizeof(struct css_header));
9448c2ecf20Sopenharmony_ci}
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci/* return the 8051 firmware state */
9478c2ecf20Sopenharmony_cistatic inline u32 get_firmware_state(struct hfi1_devdata *dd)
9488c2ecf20Sopenharmony_ci{
9498c2ecf20Sopenharmony_ci	u64 reg = read_csr(dd, DC_DC8051_STS_CUR_STATE);
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci	return (reg >> DC_DC8051_STS_CUR_STATE_FIRMWARE_SHIFT)
9528c2ecf20Sopenharmony_ci				& DC_DC8051_STS_CUR_STATE_FIRMWARE_MASK;
9538c2ecf20Sopenharmony_ci}
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ci/*
9568c2ecf20Sopenharmony_ci * Wait until the firmware is up and ready to take host requests.
9578c2ecf20Sopenharmony_ci * Return 0 on success, -ETIMEDOUT on timeout.
9588c2ecf20Sopenharmony_ci */
9598c2ecf20Sopenharmony_ciint wait_fm_ready(struct hfi1_devdata *dd, u32 mstimeout)
9608c2ecf20Sopenharmony_ci{
9618c2ecf20Sopenharmony_ci	unsigned long timeout;
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_ci	/* in the simulator, the fake 8051 is always ready */
9648c2ecf20Sopenharmony_ci	if (dd->icode == ICODE_FUNCTIONAL_SIMULATOR)
9658c2ecf20Sopenharmony_ci		return 0;
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_ci	timeout = msecs_to_jiffies(mstimeout) + jiffies;
9688c2ecf20Sopenharmony_ci	while (1) {
9698c2ecf20Sopenharmony_ci		if (get_firmware_state(dd) == 0xa0)	/* ready */
9708c2ecf20Sopenharmony_ci			return 0;
9718c2ecf20Sopenharmony_ci		if (time_after(jiffies, timeout))	/* timed out */
9728c2ecf20Sopenharmony_ci			return -ETIMEDOUT;
9738c2ecf20Sopenharmony_ci		usleep_range(1950, 2050); /* sleep 2ms-ish */
9748c2ecf20Sopenharmony_ci	}
9758c2ecf20Sopenharmony_ci}
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_ci/*
9788c2ecf20Sopenharmony_ci * Load the 8051 firmware.
9798c2ecf20Sopenharmony_ci */
9808c2ecf20Sopenharmony_cistatic int load_8051_firmware(struct hfi1_devdata *dd,
9818c2ecf20Sopenharmony_ci			      struct firmware_details *fdet)
9828c2ecf20Sopenharmony_ci{
9838c2ecf20Sopenharmony_ci	u64 reg;
9848c2ecf20Sopenharmony_ci	int ret;
9858c2ecf20Sopenharmony_ci	u8 ver_major;
9868c2ecf20Sopenharmony_ci	u8 ver_minor;
9878c2ecf20Sopenharmony_ci	u8 ver_patch;
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_ci	/*
9908c2ecf20Sopenharmony_ci	 * DC Reset sequence
9918c2ecf20Sopenharmony_ci	 * Load DC 8051 firmware
9928c2ecf20Sopenharmony_ci	 */
9938c2ecf20Sopenharmony_ci	/*
9948c2ecf20Sopenharmony_ci	 * DC reset step 1: Reset DC8051
9958c2ecf20Sopenharmony_ci	 */
9968c2ecf20Sopenharmony_ci	reg = DC_DC8051_CFG_RST_M8051W_SMASK
9978c2ecf20Sopenharmony_ci		| DC_DC8051_CFG_RST_CRAM_SMASK
9988c2ecf20Sopenharmony_ci		| DC_DC8051_CFG_RST_DRAM_SMASK
9998c2ecf20Sopenharmony_ci		| DC_DC8051_CFG_RST_IRAM_SMASK
10008c2ecf20Sopenharmony_ci		| DC_DC8051_CFG_RST_SFR_SMASK;
10018c2ecf20Sopenharmony_ci	write_csr(dd, DC_DC8051_CFG_RST, reg);
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci	/*
10048c2ecf20Sopenharmony_ci	 * DC reset step 2 (optional): Load 8051 data memory with link
10058c2ecf20Sopenharmony_ci	 * configuration
10068c2ecf20Sopenharmony_ci	 */
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci	/*
10098c2ecf20Sopenharmony_ci	 * DC reset step 3: Load DC8051 firmware
10108c2ecf20Sopenharmony_ci	 */
10118c2ecf20Sopenharmony_ci	/* release all but the core reset */
10128c2ecf20Sopenharmony_ci	reg = DC_DC8051_CFG_RST_M8051W_SMASK;
10138c2ecf20Sopenharmony_ci	write_csr(dd, DC_DC8051_CFG_RST, reg);
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_ci	/* Firmware load step 1 */
10168c2ecf20Sopenharmony_ci	load_security_variables(dd, fdet);
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ci	/*
10198c2ecf20Sopenharmony_ci	 * Firmware load step 2.  Clear MISC_CFG_FW_CTRL.FW_8051_LOADED
10208c2ecf20Sopenharmony_ci	 */
10218c2ecf20Sopenharmony_ci	write_csr(dd, MISC_CFG_FW_CTRL, 0);
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci	/* Firmware load steps 3-5 */
10248c2ecf20Sopenharmony_ci	ret = write_8051(dd, 1/*code*/, 0, fdet->firmware_ptr,
10258c2ecf20Sopenharmony_ci			 fdet->firmware_len);
10268c2ecf20Sopenharmony_ci	if (ret)
10278c2ecf20Sopenharmony_ci		return ret;
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci	/*
10308c2ecf20Sopenharmony_ci	 * DC reset step 4. Host starts the DC8051 firmware
10318c2ecf20Sopenharmony_ci	 */
10328c2ecf20Sopenharmony_ci	/*
10338c2ecf20Sopenharmony_ci	 * Firmware load step 6.  Set MISC_CFG_FW_CTRL.FW_8051_LOADED
10348c2ecf20Sopenharmony_ci	 */
10358c2ecf20Sopenharmony_ci	write_csr(dd, MISC_CFG_FW_CTRL, MISC_CFG_FW_CTRL_FW_8051_LOADED_SMASK);
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci	/* Firmware load steps 7-10 */
10388c2ecf20Sopenharmony_ci	ret = run_rsa(dd, "8051", fdet->signature);
10398c2ecf20Sopenharmony_ci	if (ret)
10408c2ecf20Sopenharmony_ci		return ret;
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_ci	/* clear all reset bits, releasing the 8051 */
10438c2ecf20Sopenharmony_ci	write_csr(dd, DC_DC8051_CFG_RST, 0ull);
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_ci	/*
10468c2ecf20Sopenharmony_ci	 * DC reset step 5. Wait for firmware to be ready to accept host
10478c2ecf20Sopenharmony_ci	 * requests.
10488c2ecf20Sopenharmony_ci	 */
10498c2ecf20Sopenharmony_ci	ret = wait_fm_ready(dd, TIMEOUT_8051_START);
10508c2ecf20Sopenharmony_ci	if (ret) { /* timed out */
10518c2ecf20Sopenharmony_ci		dd_dev_err(dd, "8051 start timeout, current state 0x%x\n",
10528c2ecf20Sopenharmony_ci			   get_firmware_state(dd));
10538c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
10548c2ecf20Sopenharmony_ci	}
10558c2ecf20Sopenharmony_ci
10568c2ecf20Sopenharmony_ci	read_misc_status(dd, &ver_major, &ver_minor, &ver_patch);
10578c2ecf20Sopenharmony_ci	dd_dev_info(dd, "8051 firmware version %d.%d.%d\n",
10588c2ecf20Sopenharmony_ci		    (int)ver_major, (int)ver_minor, (int)ver_patch);
10598c2ecf20Sopenharmony_ci	dd->dc8051_ver = dc8051_ver(ver_major, ver_minor, ver_patch);
10608c2ecf20Sopenharmony_ci	ret = write_host_interface_version(dd, HOST_INTERFACE_VERSION);
10618c2ecf20Sopenharmony_ci	if (ret != HCMD_SUCCESS) {
10628c2ecf20Sopenharmony_ci		dd_dev_err(dd,
10638c2ecf20Sopenharmony_ci			   "Failed to set host interface version, return 0x%x\n",
10648c2ecf20Sopenharmony_ci			   ret);
10658c2ecf20Sopenharmony_ci		return -EIO;
10668c2ecf20Sopenharmony_ci	}
10678c2ecf20Sopenharmony_ci
10688c2ecf20Sopenharmony_ci	return 0;
10698c2ecf20Sopenharmony_ci}
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_ci/*
10728c2ecf20Sopenharmony_ci * Write the SBus request register
10738c2ecf20Sopenharmony_ci *
10748c2ecf20Sopenharmony_ci * No need for masking - the arguments are sized exactly.
10758c2ecf20Sopenharmony_ci */
10768c2ecf20Sopenharmony_civoid sbus_request(struct hfi1_devdata *dd,
10778c2ecf20Sopenharmony_ci		  u8 receiver_addr, u8 data_addr, u8 command, u32 data_in)
10788c2ecf20Sopenharmony_ci{
10798c2ecf20Sopenharmony_ci	write_csr(dd, ASIC_CFG_SBUS_REQUEST,
10808c2ecf20Sopenharmony_ci		  ((u64)data_in << ASIC_CFG_SBUS_REQUEST_DATA_IN_SHIFT) |
10818c2ecf20Sopenharmony_ci		  ((u64)command << ASIC_CFG_SBUS_REQUEST_COMMAND_SHIFT) |
10828c2ecf20Sopenharmony_ci		  ((u64)data_addr << ASIC_CFG_SBUS_REQUEST_DATA_ADDR_SHIFT) |
10838c2ecf20Sopenharmony_ci		  ((u64)receiver_addr <<
10848c2ecf20Sopenharmony_ci		   ASIC_CFG_SBUS_REQUEST_RECEIVER_ADDR_SHIFT));
10858c2ecf20Sopenharmony_ci}
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_ci/*
10888c2ecf20Sopenharmony_ci * Read a value from the SBus.
10898c2ecf20Sopenharmony_ci *
10908c2ecf20Sopenharmony_ci * Requires the caller to be in fast mode
10918c2ecf20Sopenharmony_ci */
10928c2ecf20Sopenharmony_cistatic u32 sbus_read(struct hfi1_devdata *dd, u8 receiver_addr, u8 data_addr,
10938c2ecf20Sopenharmony_ci		     u32 data_in)
10948c2ecf20Sopenharmony_ci{
10958c2ecf20Sopenharmony_ci	u64 reg;
10968c2ecf20Sopenharmony_ci	int retries;
10978c2ecf20Sopenharmony_ci	int success = 0;
10988c2ecf20Sopenharmony_ci	u32 result = 0;
10998c2ecf20Sopenharmony_ci	u32 result_code = 0;
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_ci	sbus_request(dd, receiver_addr, data_addr, READ_SBUS_RECEIVER, data_in);
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci	for (retries = 0; retries < 100; retries++) {
11048c2ecf20Sopenharmony_ci		usleep_range(1000, 1200); /* arbitrary */
11058c2ecf20Sopenharmony_ci		reg = read_csr(dd, ASIC_STS_SBUS_RESULT);
11068c2ecf20Sopenharmony_ci		result_code = (reg >> ASIC_STS_SBUS_RESULT_RESULT_CODE_SHIFT)
11078c2ecf20Sopenharmony_ci				& ASIC_STS_SBUS_RESULT_RESULT_CODE_MASK;
11088c2ecf20Sopenharmony_ci		if (result_code != SBUS_READ_COMPLETE)
11098c2ecf20Sopenharmony_ci			continue;
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci		success = 1;
11128c2ecf20Sopenharmony_ci		result = (reg >> ASIC_STS_SBUS_RESULT_DATA_OUT_SHIFT)
11138c2ecf20Sopenharmony_ci			   & ASIC_STS_SBUS_RESULT_DATA_OUT_MASK;
11148c2ecf20Sopenharmony_ci		break;
11158c2ecf20Sopenharmony_ci	}
11168c2ecf20Sopenharmony_ci
11178c2ecf20Sopenharmony_ci	if (!success) {
11188c2ecf20Sopenharmony_ci		dd_dev_err(dd, "%s: read failed, result code 0x%x\n", __func__,
11198c2ecf20Sopenharmony_ci			   result_code);
11208c2ecf20Sopenharmony_ci	}
11218c2ecf20Sopenharmony_ci
11228c2ecf20Sopenharmony_ci	return result;
11238c2ecf20Sopenharmony_ci}
11248c2ecf20Sopenharmony_ci
11258c2ecf20Sopenharmony_ci/*
11268c2ecf20Sopenharmony_ci * Turn off the SBus and fabric serdes spicos.
11278c2ecf20Sopenharmony_ci *
11288c2ecf20Sopenharmony_ci * + Must be called with Sbus fast mode turned on.
11298c2ecf20Sopenharmony_ci * + Must be called after fabric serdes broadcast is set up.
11308c2ecf20Sopenharmony_ci * + Must be called before the 8051 is loaded - assumes 8051 is not loaded
11318c2ecf20Sopenharmony_ci *   when using MISC_CFG_FW_CTRL.
11328c2ecf20Sopenharmony_ci */
11338c2ecf20Sopenharmony_cistatic void turn_off_spicos(struct hfi1_devdata *dd, int flags)
11348c2ecf20Sopenharmony_ci{
11358c2ecf20Sopenharmony_ci	/* only needed on A0 */
11368c2ecf20Sopenharmony_ci	if (!is_ax(dd))
11378c2ecf20Sopenharmony_ci		return;
11388c2ecf20Sopenharmony_ci
11398c2ecf20Sopenharmony_ci	dd_dev_info(dd, "Turning off spicos:%s%s\n",
11408c2ecf20Sopenharmony_ci		    flags & SPICO_SBUS ? " SBus" : "",
11418c2ecf20Sopenharmony_ci		    flags & SPICO_FABRIC ? " fabric" : "");
11428c2ecf20Sopenharmony_ci
11438c2ecf20Sopenharmony_ci	write_csr(dd, MISC_CFG_FW_CTRL, ENABLE_SPICO_SMASK);
11448c2ecf20Sopenharmony_ci	/* disable SBus spico */
11458c2ecf20Sopenharmony_ci	if (flags & SPICO_SBUS)
11468c2ecf20Sopenharmony_ci		sbus_request(dd, SBUS_MASTER_BROADCAST, 0x01,
11478c2ecf20Sopenharmony_ci			     WRITE_SBUS_RECEIVER, 0x00000040);
11488c2ecf20Sopenharmony_ci
11498c2ecf20Sopenharmony_ci	/* disable the fabric serdes spicos */
11508c2ecf20Sopenharmony_ci	if (flags & SPICO_FABRIC)
11518c2ecf20Sopenharmony_ci		sbus_request(dd, fabric_serdes_broadcast[dd->hfi1_id],
11528c2ecf20Sopenharmony_ci			     0x07, WRITE_SBUS_RECEIVER, 0x00000000);
11538c2ecf20Sopenharmony_ci	write_csr(dd, MISC_CFG_FW_CTRL, 0);
11548c2ecf20Sopenharmony_ci}
11558c2ecf20Sopenharmony_ci
11568c2ecf20Sopenharmony_ci/*
11578c2ecf20Sopenharmony_ci * Reset all of the fabric serdes for this HFI in preparation to take the
11588c2ecf20Sopenharmony_ci * link to Polling.
11598c2ecf20Sopenharmony_ci *
11608c2ecf20Sopenharmony_ci * To do a reset, we need to write to to the serdes registers.  Unfortunately,
11618c2ecf20Sopenharmony_ci * the fabric serdes download to the other HFI on the ASIC will have turned
11628c2ecf20Sopenharmony_ci * off the firmware validation on this HFI.  This means we can't write to the
11638c2ecf20Sopenharmony_ci * registers to reset the serdes.  Work around this by performing a complete
11648c2ecf20Sopenharmony_ci * re-download and validation of the fabric serdes firmware.  This, as a
11658c2ecf20Sopenharmony_ci * by-product, will reset the serdes.  NOTE: the re-download requires that
11668c2ecf20Sopenharmony_ci * the 8051 be in the Offline state.  I.e. not actively trying to use the
11678c2ecf20Sopenharmony_ci * serdes.  This routine is called at the point where the link is Offline and
11688c2ecf20Sopenharmony_ci * is getting ready to go to Polling.
11698c2ecf20Sopenharmony_ci */
11708c2ecf20Sopenharmony_civoid fabric_serdes_reset(struct hfi1_devdata *dd)
11718c2ecf20Sopenharmony_ci{
11728c2ecf20Sopenharmony_ci	int ret;
11738c2ecf20Sopenharmony_ci
11748c2ecf20Sopenharmony_ci	if (!fw_fabric_serdes_load)
11758c2ecf20Sopenharmony_ci		return;
11768c2ecf20Sopenharmony_ci
11778c2ecf20Sopenharmony_ci	ret = acquire_chip_resource(dd, CR_SBUS, SBUS_TIMEOUT);
11788c2ecf20Sopenharmony_ci	if (ret) {
11798c2ecf20Sopenharmony_ci		dd_dev_err(dd,
11808c2ecf20Sopenharmony_ci			   "Cannot acquire SBus resource to reset fabric SerDes - perhaps you should reboot\n");
11818c2ecf20Sopenharmony_ci		return;
11828c2ecf20Sopenharmony_ci	}
11838c2ecf20Sopenharmony_ci	set_sbus_fast_mode(dd);
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_ci	if (is_ax(dd)) {
11868c2ecf20Sopenharmony_ci		/* A0 serdes do not work with a re-download */
11878c2ecf20Sopenharmony_ci		u8 ra = fabric_serdes_broadcast[dd->hfi1_id];
11888c2ecf20Sopenharmony_ci
11898c2ecf20Sopenharmony_ci		/* place SerDes in reset and disable SPICO */
11908c2ecf20Sopenharmony_ci		sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000011);
11918c2ecf20Sopenharmony_ci		/* wait 100 refclk cycles @ 156.25MHz => 640ns */
11928c2ecf20Sopenharmony_ci		udelay(1);
11938c2ecf20Sopenharmony_ci		/* remove SerDes reset */
11948c2ecf20Sopenharmony_ci		sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000010);
11958c2ecf20Sopenharmony_ci		/* turn SPICO enable on */
11968c2ecf20Sopenharmony_ci		sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000002);
11978c2ecf20Sopenharmony_ci	} else {
11988c2ecf20Sopenharmony_ci		turn_off_spicos(dd, SPICO_FABRIC);
11998c2ecf20Sopenharmony_ci		/*
12008c2ecf20Sopenharmony_ci		 * No need for firmware retry - what to download has already
12018c2ecf20Sopenharmony_ci		 * been decided.
12028c2ecf20Sopenharmony_ci		 * No need to pay attention to the load return - the only
12038c2ecf20Sopenharmony_ci		 * failure is a validation failure, which has already been
12048c2ecf20Sopenharmony_ci		 * checked by the initial download.
12058c2ecf20Sopenharmony_ci		 */
12068c2ecf20Sopenharmony_ci		(void)load_fabric_serdes_firmware(dd, &fw_fabric);
12078c2ecf20Sopenharmony_ci	}
12088c2ecf20Sopenharmony_ci
12098c2ecf20Sopenharmony_ci	clear_sbus_fast_mode(dd);
12108c2ecf20Sopenharmony_ci	release_chip_resource(dd, CR_SBUS);
12118c2ecf20Sopenharmony_ci}
12128c2ecf20Sopenharmony_ci
12138c2ecf20Sopenharmony_ci/* Access to the SBus in this routine should probably be serialized */
12148c2ecf20Sopenharmony_ciint sbus_request_slow(struct hfi1_devdata *dd,
12158c2ecf20Sopenharmony_ci		      u8 receiver_addr, u8 data_addr, u8 command, u32 data_in)
12168c2ecf20Sopenharmony_ci{
12178c2ecf20Sopenharmony_ci	u64 reg, count = 0;
12188c2ecf20Sopenharmony_ci
12198c2ecf20Sopenharmony_ci	/* make sure fast mode is clear */
12208c2ecf20Sopenharmony_ci	clear_sbus_fast_mode(dd);
12218c2ecf20Sopenharmony_ci
12228c2ecf20Sopenharmony_ci	sbus_request(dd, receiver_addr, data_addr, command, data_in);
12238c2ecf20Sopenharmony_ci	write_csr(dd, ASIC_CFG_SBUS_EXECUTE,
12248c2ecf20Sopenharmony_ci		  ASIC_CFG_SBUS_EXECUTE_EXECUTE_SMASK);
12258c2ecf20Sopenharmony_ci	/* Wait for both DONE and RCV_DATA_VALID to go high */
12268c2ecf20Sopenharmony_ci	reg = read_csr(dd, ASIC_STS_SBUS_RESULT);
12278c2ecf20Sopenharmony_ci	while (!((reg & ASIC_STS_SBUS_RESULT_DONE_SMASK) &&
12288c2ecf20Sopenharmony_ci		 (reg & ASIC_STS_SBUS_RESULT_RCV_DATA_VALID_SMASK))) {
12298c2ecf20Sopenharmony_ci		if (count++ >= SBUS_MAX_POLL_COUNT) {
12308c2ecf20Sopenharmony_ci			u64 counts = read_csr(dd, ASIC_STS_SBUS_COUNTERS);
12318c2ecf20Sopenharmony_ci			/*
12328c2ecf20Sopenharmony_ci			 * If the loop has timed out, we are OK if DONE bit
12338c2ecf20Sopenharmony_ci			 * is set and RCV_DATA_VALID and EXECUTE counters
12348c2ecf20Sopenharmony_ci			 * are the same. If not, we cannot proceed.
12358c2ecf20Sopenharmony_ci			 */
12368c2ecf20Sopenharmony_ci			if ((reg & ASIC_STS_SBUS_RESULT_DONE_SMASK) &&
12378c2ecf20Sopenharmony_ci			    (SBUS_COUNTER(counts, RCV_DATA_VALID) ==
12388c2ecf20Sopenharmony_ci			     SBUS_COUNTER(counts, EXECUTE)))
12398c2ecf20Sopenharmony_ci				break;
12408c2ecf20Sopenharmony_ci			return -ETIMEDOUT;
12418c2ecf20Sopenharmony_ci		}
12428c2ecf20Sopenharmony_ci		udelay(1);
12438c2ecf20Sopenharmony_ci		reg = read_csr(dd, ASIC_STS_SBUS_RESULT);
12448c2ecf20Sopenharmony_ci	}
12458c2ecf20Sopenharmony_ci	count = 0;
12468c2ecf20Sopenharmony_ci	write_csr(dd, ASIC_CFG_SBUS_EXECUTE, 0);
12478c2ecf20Sopenharmony_ci	/* Wait for DONE to clear after EXECUTE is cleared */
12488c2ecf20Sopenharmony_ci	reg = read_csr(dd, ASIC_STS_SBUS_RESULT);
12498c2ecf20Sopenharmony_ci	while (reg & ASIC_STS_SBUS_RESULT_DONE_SMASK) {
12508c2ecf20Sopenharmony_ci		if (count++ >= SBUS_MAX_POLL_COUNT)
12518c2ecf20Sopenharmony_ci			return -ETIME;
12528c2ecf20Sopenharmony_ci		udelay(1);
12538c2ecf20Sopenharmony_ci		reg = read_csr(dd, ASIC_STS_SBUS_RESULT);
12548c2ecf20Sopenharmony_ci	}
12558c2ecf20Sopenharmony_ci	return 0;
12568c2ecf20Sopenharmony_ci}
12578c2ecf20Sopenharmony_ci
12588c2ecf20Sopenharmony_cistatic int load_fabric_serdes_firmware(struct hfi1_devdata *dd,
12598c2ecf20Sopenharmony_ci				       struct firmware_details *fdet)
12608c2ecf20Sopenharmony_ci{
12618c2ecf20Sopenharmony_ci	int i, err;
12628c2ecf20Sopenharmony_ci	const u8 ra = fabric_serdes_broadcast[dd->hfi1_id]; /* receiver addr */
12638c2ecf20Sopenharmony_ci
12648c2ecf20Sopenharmony_ci	dd_dev_info(dd, "Downloading fabric firmware\n");
12658c2ecf20Sopenharmony_ci
12668c2ecf20Sopenharmony_ci	/* step 1: load security variables */
12678c2ecf20Sopenharmony_ci	load_security_variables(dd, fdet);
12688c2ecf20Sopenharmony_ci	/* step 2: place SerDes in reset and disable SPICO */
12698c2ecf20Sopenharmony_ci	sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000011);
12708c2ecf20Sopenharmony_ci	/* wait 100 refclk cycles @ 156.25MHz => 640ns */
12718c2ecf20Sopenharmony_ci	udelay(1);
12728c2ecf20Sopenharmony_ci	/* step 3:  remove SerDes reset */
12738c2ecf20Sopenharmony_ci	sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000010);
12748c2ecf20Sopenharmony_ci	/* step 4: assert IMEM override */
12758c2ecf20Sopenharmony_ci	sbus_request(dd, ra, 0x00, WRITE_SBUS_RECEIVER, 0x40000000);
12768c2ecf20Sopenharmony_ci	/* step 5: download SerDes machine code */
12778c2ecf20Sopenharmony_ci	for (i = 0; i < fdet->firmware_len; i += 4) {
12788c2ecf20Sopenharmony_ci		sbus_request(dd, ra, 0x0a, WRITE_SBUS_RECEIVER,
12798c2ecf20Sopenharmony_ci			     *(u32 *)&fdet->firmware_ptr[i]);
12808c2ecf20Sopenharmony_ci	}
12818c2ecf20Sopenharmony_ci	/* step 6: IMEM override off */
12828c2ecf20Sopenharmony_ci	sbus_request(dd, ra, 0x00, WRITE_SBUS_RECEIVER, 0x00000000);
12838c2ecf20Sopenharmony_ci	/* step 7: turn ECC on */
12848c2ecf20Sopenharmony_ci	sbus_request(dd, ra, 0x0b, WRITE_SBUS_RECEIVER, 0x000c0000);
12858c2ecf20Sopenharmony_ci
12868c2ecf20Sopenharmony_ci	/* steps 8-11: run the RSA engine */
12878c2ecf20Sopenharmony_ci	err = run_rsa(dd, "fabric serdes", fdet->signature);
12888c2ecf20Sopenharmony_ci	if (err)
12898c2ecf20Sopenharmony_ci		return err;
12908c2ecf20Sopenharmony_ci
12918c2ecf20Sopenharmony_ci	/* step 12: turn SPICO enable on */
12928c2ecf20Sopenharmony_ci	sbus_request(dd, ra, 0x07, WRITE_SBUS_RECEIVER, 0x00000002);
12938c2ecf20Sopenharmony_ci	/* step 13: enable core hardware interrupts */
12948c2ecf20Sopenharmony_ci	sbus_request(dd, ra, 0x08, WRITE_SBUS_RECEIVER, 0x00000000);
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_ci	return 0;
12978c2ecf20Sopenharmony_ci}
12988c2ecf20Sopenharmony_ci
12998c2ecf20Sopenharmony_cistatic int load_sbus_firmware(struct hfi1_devdata *dd,
13008c2ecf20Sopenharmony_ci			      struct firmware_details *fdet)
13018c2ecf20Sopenharmony_ci{
13028c2ecf20Sopenharmony_ci	int i, err;
13038c2ecf20Sopenharmony_ci	const u8 ra = SBUS_MASTER_BROADCAST; /* receiver address */
13048c2ecf20Sopenharmony_ci
13058c2ecf20Sopenharmony_ci	dd_dev_info(dd, "Downloading SBus firmware\n");
13068c2ecf20Sopenharmony_ci
13078c2ecf20Sopenharmony_ci	/* step 1: load security variables */
13088c2ecf20Sopenharmony_ci	load_security_variables(dd, fdet);
13098c2ecf20Sopenharmony_ci	/* step 2: place SPICO into reset and enable off */
13108c2ecf20Sopenharmony_ci	sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x000000c0);
13118c2ecf20Sopenharmony_ci	/* step 3: remove reset, enable off, IMEM_CNTRL_EN on */
13128c2ecf20Sopenharmony_ci	sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x00000240);
13138c2ecf20Sopenharmony_ci	/* step 4: set starting IMEM address for burst download */
13148c2ecf20Sopenharmony_ci	sbus_request(dd, ra, 0x03, WRITE_SBUS_RECEIVER, 0x80000000);
13158c2ecf20Sopenharmony_ci	/* step 5: download the SBus Master machine code */
13168c2ecf20Sopenharmony_ci	for (i = 0; i < fdet->firmware_len; i += 4) {
13178c2ecf20Sopenharmony_ci		sbus_request(dd, ra, 0x14, WRITE_SBUS_RECEIVER,
13188c2ecf20Sopenharmony_ci			     *(u32 *)&fdet->firmware_ptr[i]);
13198c2ecf20Sopenharmony_ci	}
13208c2ecf20Sopenharmony_ci	/* step 6: set IMEM_CNTL_EN off */
13218c2ecf20Sopenharmony_ci	sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x00000040);
13228c2ecf20Sopenharmony_ci	/* step 7: turn ECC on */
13238c2ecf20Sopenharmony_ci	sbus_request(dd, ra, 0x16, WRITE_SBUS_RECEIVER, 0x000c0000);
13248c2ecf20Sopenharmony_ci
13258c2ecf20Sopenharmony_ci	/* steps 8-11: run the RSA engine */
13268c2ecf20Sopenharmony_ci	err = run_rsa(dd, "SBus", fdet->signature);
13278c2ecf20Sopenharmony_ci	if (err)
13288c2ecf20Sopenharmony_ci		return err;
13298c2ecf20Sopenharmony_ci
13308c2ecf20Sopenharmony_ci	/* step 12: set SPICO_ENABLE on */
13318c2ecf20Sopenharmony_ci	sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x00000140);
13328c2ecf20Sopenharmony_ci
13338c2ecf20Sopenharmony_ci	return 0;
13348c2ecf20Sopenharmony_ci}
13358c2ecf20Sopenharmony_ci
13368c2ecf20Sopenharmony_cistatic int load_pcie_serdes_firmware(struct hfi1_devdata *dd,
13378c2ecf20Sopenharmony_ci				     struct firmware_details *fdet)
13388c2ecf20Sopenharmony_ci{
13398c2ecf20Sopenharmony_ci	int i;
13408c2ecf20Sopenharmony_ci	const u8 ra = SBUS_MASTER_BROADCAST; /* receiver address */
13418c2ecf20Sopenharmony_ci
13428c2ecf20Sopenharmony_ci	dd_dev_info(dd, "Downloading PCIe firmware\n");
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_ci	/* step 1: load security variables */
13458c2ecf20Sopenharmony_ci	load_security_variables(dd, fdet);
13468c2ecf20Sopenharmony_ci	/* step 2: assert single step (halts the SBus Master spico) */
13478c2ecf20Sopenharmony_ci	sbus_request(dd, ra, 0x05, WRITE_SBUS_RECEIVER, 0x00000001);
13488c2ecf20Sopenharmony_ci	/* step 3: enable XDMEM access */
13498c2ecf20Sopenharmony_ci	sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x00000d40);
13508c2ecf20Sopenharmony_ci	/* step 4: load firmware into SBus Master XDMEM */
13518c2ecf20Sopenharmony_ci	/*
13528c2ecf20Sopenharmony_ci	 * NOTE: the dmem address, write_en, and wdata are all pre-packed,
13538c2ecf20Sopenharmony_ci	 * we only need to pick up the bytes and write them
13548c2ecf20Sopenharmony_ci	 */
13558c2ecf20Sopenharmony_ci	for (i = 0; i < fdet->firmware_len; i += 4) {
13568c2ecf20Sopenharmony_ci		sbus_request(dd, ra, 0x04, WRITE_SBUS_RECEIVER,
13578c2ecf20Sopenharmony_ci			     *(u32 *)&fdet->firmware_ptr[i]);
13588c2ecf20Sopenharmony_ci	}
13598c2ecf20Sopenharmony_ci	/* step 5: disable XDMEM access */
13608c2ecf20Sopenharmony_ci	sbus_request(dd, ra, 0x01, WRITE_SBUS_RECEIVER, 0x00000140);
13618c2ecf20Sopenharmony_ci	/* step 6: allow SBus Spico to run */
13628c2ecf20Sopenharmony_ci	sbus_request(dd, ra, 0x05, WRITE_SBUS_RECEIVER, 0x00000000);
13638c2ecf20Sopenharmony_ci
13648c2ecf20Sopenharmony_ci	/*
13658c2ecf20Sopenharmony_ci	 * steps 7-11: run RSA, if it succeeds, firmware is available to
13668c2ecf20Sopenharmony_ci	 * be swapped
13678c2ecf20Sopenharmony_ci	 */
13688c2ecf20Sopenharmony_ci	return run_rsa(dd, "PCIe serdes", fdet->signature);
13698c2ecf20Sopenharmony_ci}
13708c2ecf20Sopenharmony_ci
13718c2ecf20Sopenharmony_ci/*
13728c2ecf20Sopenharmony_ci * Set the given broadcast values on the given list of devices.
13738c2ecf20Sopenharmony_ci */
13748c2ecf20Sopenharmony_cistatic void set_serdes_broadcast(struct hfi1_devdata *dd, u8 bg1, u8 bg2,
13758c2ecf20Sopenharmony_ci				 const u8 *addrs, int count)
13768c2ecf20Sopenharmony_ci{
13778c2ecf20Sopenharmony_ci	while (--count >= 0) {
13788c2ecf20Sopenharmony_ci		/*
13798c2ecf20Sopenharmony_ci		 * Set BROADCAST_GROUP_1 and BROADCAST_GROUP_2, leave
13808c2ecf20Sopenharmony_ci		 * defaults for everything else.  Do not read-modify-write,
13818c2ecf20Sopenharmony_ci		 * per instruction from the manufacturer.
13828c2ecf20Sopenharmony_ci		 *
13838c2ecf20Sopenharmony_ci		 * Register 0xfd:
13848c2ecf20Sopenharmony_ci		 *	bits    what
13858c2ecf20Sopenharmony_ci		 *	-----	---------------------------------
13868c2ecf20Sopenharmony_ci		 *	  0	IGNORE_BROADCAST  (default 0)
13878c2ecf20Sopenharmony_ci		 *	11:4	BROADCAST_GROUP_1 (default 0xff)
13888c2ecf20Sopenharmony_ci		 *	23:16	BROADCAST_GROUP_2 (default 0xff)
13898c2ecf20Sopenharmony_ci		 */
13908c2ecf20Sopenharmony_ci		sbus_request(dd, addrs[count], 0xfd, WRITE_SBUS_RECEIVER,
13918c2ecf20Sopenharmony_ci			     (u32)bg1 << 4 | (u32)bg2 << 16);
13928c2ecf20Sopenharmony_ci	}
13938c2ecf20Sopenharmony_ci}
13948c2ecf20Sopenharmony_ci
13958c2ecf20Sopenharmony_ciint acquire_hw_mutex(struct hfi1_devdata *dd)
13968c2ecf20Sopenharmony_ci{
13978c2ecf20Sopenharmony_ci	unsigned long timeout;
13988c2ecf20Sopenharmony_ci	int try = 0;
13998c2ecf20Sopenharmony_ci	u8 mask = 1 << dd->hfi1_id;
14008c2ecf20Sopenharmony_ci	u8 user = (u8)read_csr(dd, ASIC_CFG_MUTEX);
14018c2ecf20Sopenharmony_ci
14028c2ecf20Sopenharmony_ci	if (user == mask) {
14038c2ecf20Sopenharmony_ci		dd_dev_info(dd,
14048c2ecf20Sopenharmony_ci			    "Hardware mutex already acquired, mutex mask %u\n",
14058c2ecf20Sopenharmony_ci			    (u32)mask);
14068c2ecf20Sopenharmony_ci		return 0;
14078c2ecf20Sopenharmony_ci	}
14088c2ecf20Sopenharmony_ci
14098c2ecf20Sopenharmony_ciretry:
14108c2ecf20Sopenharmony_ci	timeout = msecs_to_jiffies(HM_TIMEOUT) + jiffies;
14118c2ecf20Sopenharmony_ci	while (1) {
14128c2ecf20Sopenharmony_ci		write_csr(dd, ASIC_CFG_MUTEX, mask);
14138c2ecf20Sopenharmony_ci		user = (u8)read_csr(dd, ASIC_CFG_MUTEX);
14148c2ecf20Sopenharmony_ci		if (user == mask)
14158c2ecf20Sopenharmony_ci			return 0; /* success */
14168c2ecf20Sopenharmony_ci		if (time_after(jiffies, timeout))
14178c2ecf20Sopenharmony_ci			break; /* timed out */
14188c2ecf20Sopenharmony_ci		msleep(20);
14198c2ecf20Sopenharmony_ci	}
14208c2ecf20Sopenharmony_ci
14218c2ecf20Sopenharmony_ci	/* timed out */
14228c2ecf20Sopenharmony_ci	dd_dev_err(dd,
14238c2ecf20Sopenharmony_ci		   "Unable to acquire hardware mutex, mutex mask %u, my mask %u (%s)\n",
14248c2ecf20Sopenharmony_ci		   (u32)user, (u32)mask, (try == 0) ? "retrying" : "giving up");
14258c2ecf20Sopenharmony_ci
14268c2ecf20Sopenharmony_ci	if (try == 0) {
14278c2ecf20Sopenharmony_ci		/* break mutex and retry */
14288c2ecf20Sopenharmony_ci		write_csr(dd, ASIC_CFG_MUTEX, 0);
14298c2ecf20Sopenharmony_ci		try++;
14308c2ecf20Sopenharmony_ci		goto retry;
14318c2ecf20Sopenharmony_ci	}
14328c2ecf20Sopenharmony_ci
14338c2ecf20Sopenharmony_ci	return -EBUSY;
14348c2ecf20Sopenharmony_ci}
14358c2ecf20Sopenharmony_ci
14368c2ecf20Sopenharmony_civoid release_hw_mutex(struct hfi1_devdata *dd)
14378c2ecf20Sopenharmony_ci{
14388c2ecf20Sopenharmony_ci	u8 mask = 1 << dd->hfi1_id;
14398c2ecf20Sopenharmony_ci	u8 user = (u8)read_csr(dd, ASIC_CFG_MUTEX);
14408c2ecf20Sopenharmony_ci
14418c2ecf20Sopenharmony_ci	if (user != mask)
14428c2ecf20Sopenharmony_ci		dd_dev_warn(dd,
14438c2ecf20Sopenharmony_ci			    "Unable to release hardware mutex, mutex mask %u, my mask %u\n",
14448c2ecf20Sopenharmony_ci			    (u32)user, (u32)mask);
14458c2ecf20Sopenharmony_ci	else
14468c2ecf20Sopenharmony_ci		write_csr(dd, ASIC_CFG_MUTEX, 0);
14478c2ecf20Sopenharmony_ci}
14488c2ecf20Sopenharmony_ci
14498c2ecf20Sopenharmony_ci/* return the given resource bit(s) as a mask for the given HFI */
14508c2ecf20Sopenharmony_cistatic inline u64 resource_mask(u32 hfi1_id, u32 resource)
14518c2ecf20Sopenharmony_ci{
14528c2ecf20Sopenharmony_ci	return ((u64)resource) << (hfi1_id ? CR_DYN_SHIFT : 0);
14538c2ecf20Sopenharmony_ci}
14548c2ecf20Sopenharmony_ci
14558c2ecf20Sopenharmony_cistatic void fail_mutex_acquire_message(struct hfi1_devdata *dd,
14568c2ecf20Sopenharmony_ci				       const char *func)
14578c2ecf20Sopenharmony_ci{
14588c2ecf20Sopenharmony_ci	dd_dev_err(dd,
14598c2ecf20Sopenharmony_ci		   "%s: hardware mutex stuck - suggest rebooting the machine\n",
14608c2ecf20Sopenharmony_ci		   func);
14618c2ecf20Sopenharmony_ci}
14628c2ecf20Sopenharmony_ci
14638c2ecf20Sopenharmony_ci/*
14648c2ecf20Sopenharmony_ci * Acquire access to a chip resource.
14658c2ecf20Sopenharmony_ci *
14668c2ecf20Sopenharmony_ci * Return 0 on success, -EBUSY if resource busy, -EIO if mutex acquire failed.
14678c2ecf20Sopenharmony_ci */
14688c2ecf20Sopenharmony_cistatic int __acquire_chip_resource(struct hfi1_devdata *dd, u32 resource)
14698c2ecf20Sopenharmony_ci{
14708c2ecf20Sopenharmony_ci	u64 scratch0, all_bits, my_bit;
14718c2ecf20Sopenharmony_ci	int ret;
14728c2ecf20Sopenharmony_ci
14738c2ecf20Sopenharmony_ci	if (resource & CR_DYN_MASK) {
14748c2ecf20Sopenharmony_ci		/* a dynamic resource is in use if either HFI has set the bit */
14758c2ecf20Sopenharmony_ci		if (dd->pcidev->device == PCI_DEVICE_ID_INTEL0 &&
14768c2ecf20Sopenharmony_ci		    (resource & (CR_I2C1 | CR_I2C2))) {
14778c2ecf20Sopenharmony_ci			/* discrete devices must serialize across both chains */
14788c2ecf20Sopenharmony_ci			all_bits = resource_mask(0, CR_I2C1 | CR_I2C2) |
14798c2ecf20Sopenharmony_ci					resource_mask(1, CR_I2C1 | CR_I2C2);
14808c2ecf20Sopenharmony_ci		} else {
14818c2ecf20Sopenharmony_ci			all_bits = resource_mask(0, resource) |
14828c2ecf20Sopenharmony_ci						resource_mask(1, resource);
14838c2ecf20Sopenharmony_ci		}
14848c2ecf20Sopenharmony_ci		my_bit = resource_mask(dd->hfi1_id, resource);
14858c2ecf20Sopenharmony_ci	} else {
14868c2ecf20Sopenharmony_ci		/* non-dynamic resources are not split between HFIs */
14878c2ecf20Sopenharmony_ci		all_bits = resource;
14888c2ecf20Sopenharmony_ci		my_bit = resource;
14898c2ecf20Sopenharmony_ci	}
14908c2ecf20Sopenharmony_ci
14918c2ecf20Sopenharmony_ci	/* lock against other callers within the driver wanting a resource */
14928c2ecf20Sopenharmony_ci	mutex_lock(&dd->asic_data->asic_resource_mutex);
14938c2ecf20Sopenharmony_ci
14948c2ecf20Sopenharmony_ci	ret = acquire_hw_mutex(dd);
14958c2ecf20Sopenharmony_ci	if (ret) {
14968c2ecf20Sopenharmony_ci		fail_mutex_acquire_message(dd, __func__);
14978c2ecf20Sopenharmony_ci		ret = -EIO;
14988c2ecf20Sopenharmony_ci		goto done;
14998c2ecf20Sopenharmony_ci	}
15008c2ecf20Sopenharmony_ci
15018c2ecf20Sopenharmony_ci	scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
15028c2ecf20Sopenharmony_ci	if (scratch0 & all_bits) {
15038c2ecf20Sopenharmony_ci		ret = -EBUSY;
15048c2ecf20Sopenharmony_ci	} else {
15058c2ecf20Sopenharmony_ci		write_csr(dd, ASIC_CFG_SCRATCH, scratch0 | my_bit);
15068c2ecf20Sopenharmony_ci		/* force write to be visible to other HFI on another OS */
15078c2ecf20Sopenharmony_ci		(void)read_csr(dd, ASIC_CFG_SCRATCH);
15088c2ecf20Sopenharmony_ci	}
15098c2ecf20Sopenharmony_ci
15108c2ecf20Sopenharmony_ci	release_hw_mutex(dd);
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_cidone:
15138c2ecf20Sopenharmony_ci	mutex_unlock(&dd->asic_data->asic_resource_mutex);
15148c2ecf20Sopenharmony_ci	return ret;
15158c2ecf20Sopenharmony_ci}
15168c2ecf20Sopenharmony_ci
15178c2ecf20Sopenharmony_ci/*
15188c2ecf20Sopenharmony_ci * Acquire access to a chip resource, wait up to mswait milliseconds for
15198c2ecf20Sopenharmony_ci * the resource to become available.
15208c2ecf20Sopenharmony_ci *
15218c2ecf20Sopenharmony_ci * Return 0 on success, -EBUSY if busy (even after wait), -EIO if mutex
15228c2ecf20Sopenharmony_ci * acquire failed.
15238c2ecf20Sopenharmony_ci */
15248c2ecf20Sopenharmony_ciint acquire_chip_resource(struct hfi1_devdata *dd, u32 resource, u32 mswait)
15258c2ecf20Sopenharmony_ci{
15268c2ecf20Sopenharmony_ci	unsigned long timeout;
15278c2ecf20Sopenharmony_ci	int ret;
15288c2ecf20Sopenharmony_ci
15298c2ecf20Sopenharmony_ci	timeout = jiffies + msecs_to_jiffies(mswait);
15308c2ecf20Sopenharmony_ci	while (1) {
15318c2ecf20Sopenharmony_ci		ret = __acquire_chip_resource(dd, resource);
15328c2ecf20Sopenharmony_ci		if (ret != -EBUSY)
15338c2ecf20Sopenharmony_ci			return ret;
15348c2ecf20Sopenharmony_ci		/* resource is busy, check our timeout */
15358c2ecf20Sopenharmony_ci		if (time_after_eq(jiffies, timeout))
15368c2ecf20Sopenharmony_ci			return -EBUSY;
15378c2ecf20Sopenharmony_ci		usleep_range(80, 120);	/* arbitrary delay */
15388c2ecf20Sopenharmony_ci	}
15398c2ecf20Sopenharmony_ci}
15408c2ecf20Sopenharmony_ci
15418c2ecf20Sopenharmony_ci/*
15428c2ecf20Sopenharmony_ci * Release access to a chip resource
15438c2ecf20Sopenharmony_ci */
15448c2ecf20Sopenharmony_civoid release_chip_resource(struct hfi1_devdata *dd, u32 resource)
15458c2ecf20Sopenharmony_ci{
15468c2ecf20Sopenharmony_ci	u64 scratch0, bit;
15478c2ecf20Sopenharmony_ci
15488c2ecf20Sopenharmony_ci	/* only dynamic resources should ever be cleared */
15498c2ecf20Sopenharmony_ci	if (!(resource & CR_DYN_MASK)) {
15508c2ecf20Sopenharmony_ci		dd_dev_err(dd, "%s: invalid resource 0x%x\n", __func__,
15518c2ecf20Sopenharmony_ci			   resource);
15528c2ecf20Sopenharmony_ci		return;
15538c2ecf20Sopenharmony_ci	}
15548c2ecf20Sopenharmony_ci	bit = resource_mask(dd->hfi1_id, resource);
15558c2ecf20Sopenharmony_ci
15568c2ecf20Sopenharmony_ci	/* lock against other callers within the driver wanting a resource */
15578c2ecf20Sopenharmony_ci	mutex_lock(&dd->asic_data->asic_resource_mutex);
15588c2ecf20Sopenharmony_ci
15598c2ecf20Sopenharmony_ci	if (acquire_hw_mutex(dd)) {
15608c2ecf20Sopenharmony_ci		fail_mutex_acquire_message(dd, __func__);
15618c2ecf20Sopenharmony_ci		goto done;
15628c2ecf20Sopenharmony_ci	}
15638c2ecf20Sopenharmony_ci
15648c2ecf20Sopenharmony_ci	scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
15658c2ecf20Sopenharmony_ci	if ((scratch0 & bit) != 0) {
15668c2ecf20Sopenharmony_ci		scratch0 &= ~bit;
15678c2ecf20Sopenharmony_ci		write_csr(dd, ASIC_CFG_SCRATCH, scratch0);
15688c2ecf20Sopenharmony_ci		/* force write to be visible to other HFI on another OS */
15698c2ecf20Sopenharmony_ci		(void)read_csr(dd, ASIC_CFG_SCRATCH);
15708c2ecf20Sopenharmony_ci	} else {
15718c2ecf20Sopenharmony_ci		dd_dev_warn(dd, "%s: id %d, resource 0x%x: bit not set\n",
15728c2ecf20Sopenharmony_ci			    __func__, dd->hfi1_id, resource);
15738c2ecf20Sopenharmony_ci	}
15748c2ecf20Sopenharmony_ci
15758c2ecf20Sopenharmony_ci	release_hw_mutex(dd);
15768c2ecf20Sopenharmony_ci
15778c2ecf20Sopenharmony_cidone:
15788c2ecf20Sopenharmony_ci	mutex_unlock(&dd->asic_data->asic_resource_mutex);
15798c2ecf20Sopenharmony_ci}
15808c2ecf20Sopenharmony_ci
15818c2ecf20Sopenharmony_ci/*
15828c2ecf20Sopenharmony_ci * Return true if resource is set, false otherwise.  Print a warning
15838c2ecf20Sopenharmony_ci * if not set and a function is supplied.
15848c2ecf20Sopenharmony_ci */
15858c2ecf20Sopenharmony_cibool check_chip_resource(struct hfi1_devdata *dd, u32 resource,
15868c2ecf20Sopenharmony_ci			 const char *func)
15878c2ecf20Sopenharmony_ci{
15888c2ecf20Sopenharmony_ci	u64 scratch0, bit;
15898c2ecf20Sopenharmony_ci
15908c2ecf20Sopenharmony_ci	if (resource & CR_DYN_MASK)
15918c2ecf20Sopenharmony_ci		bit = resource_mask(dd->hfi1_id, resource);
15928c2ecf20Sopenharmony_ci	else
15938c2ecf20Sopenharmony_ci		bit = resource;
15948c2ecf20Sopenharmony_ci
15958c2ecf20Sopenharmony_ci	scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
15968c2ecf20Sopenharmony_ci	if ((scratch0 & bit) == 0) {
15978c2ecf20Sopenharmony_ci		if (func)
15988c2ecf20Sopenharmony_ci			dd_dev_warn(dd,
15998c2ecf20Sopenharmony_ci				    "%s: id %d, resource 0x%x, not acquired!\n",
16008c2ecf20Sopenharmony_ci				    func, dd->hfi1_id, resource);
16018c2ecf20Sopenharmony_ci		return false;
16028c2ecf20Sopenharmony_ci	}
16038c2ecf20Sopenharmony_ci	return true;
16048c2ecf20Sopenharmony_ci}
16058c2ecf20Sopenharmony_ci
16068c2ecf20Sopenharmony_cistatic void clear_chip_resources(struct hfi1_devdata *dd, const char *func)
16078c2ecf20Sopenharmony_ci{
16088c2ecf20Sopenharmony_ci	u64 scratch0;
16098c2ecf20Sopenharmony_ci
16108c2ecf20Sopenharmony_ci	/* lock against other callers within the driver wanting a resource */
16118c2ecf20Sopenharmony_ci	mutex_lock(&dd->asic_data->asic_resource_mutex);
16128c2ecf20Sopenharmony_ci
16138c2ecf20Sopenharmony_ci	if (acquire_hw_mutex(dd)) {
16148c2ecf20Sopenharmony_ci		fail_mutex_acquire_message(dd, func);
16158c2ecf20Sopenharmony_ci		goto done;
16168c2ecf20Sopenharmony_ci	}
16178c2ecf20Sopenharmony_ci
16188c2ecf20Sopenharmony_ci	/* clear all dynamic access bits for this HFI */
16198c2ecf20Sopenharmony_ci	scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
16208c2ecf20Sopenharmony_ci	scratch0 &= ~resource_mask(dd->hfi1_id, CR_DYN_MASK);
16218c2ecf20Sopenharmony_ci	write_csr(dd, ASIC_CFG_SCRATCH, scratch0);
16228c2ecf20Sopenharmony_ci	/* force write to be visible to other HFI on another OS */
16238c2ecf20Sopenharmony_ci	(void)read_csr(dd, ASIC_CFG_SCRATCH);
16248c2ecf20Sopenharmony_ci
16258c2ecf20Sopenharmony_ci	release_hw_mutex(dd);
16268c2ecf20Sopenharmony_ci
16278c2ecf20Sopenharmony_cidone:
16288c2ecf20Sopenharmony_ci	mutex_unlock(&dd->asic_data->asic_resource_mutex);
16298c2ecf20Sopenharmony_ci}
16308c2ecf20Sopenharmony_ci
16318c2ecf20Sopenharmony_civoid init_chip_resources(struct hfi1_devdata *dd)
16328c2ecf20Sopenharmony_ci{
16338c2ecf20Sopenharmony_ci	/* clear any holds left by us */
16348c2ecf20Sopenharmony_ci	clear_chip_resources(dd, __func__);
16358c2ecf20Sopenharmony_ci}
16368c2ecf20Sopenharmony_ci
16378c2ecf20Sopenharmony_civoid finish_chip_resources(struct hfi1_devdata *dd)
16388c2ecf20Sopenharmony_ci{
16398c2ecf20Sopenharmony_ci	/* clear any holds left by us */
16408c2ecf20Sopenharmony_ci	clear_chip_resources(dd, __func__);
16418c2ecf20Sopenharmony_ci}
16428c2ecf20Sopenharmony_ci
16438c2ecf20Sopenharmony_civoid set_sbus_fast_mode(struct hfi1_devdata *dd)
16448c2ecf20Sopenharmony_ci{
16458c2ecf20Sopenharmony_ci	write_csr(dd, ASIC_CFG_SBUS_EXECUTE,
16468c2ecf20Sopenharmony_ci		  ASIC_CFG_SBUS_EXECUTE_FAST_MODE_SMASK);
16478c2ecf20Sopenharmony_ci}
16488c2ecf20Sopenharmony_ci
16498c2ecf20Sopenharmony_civoid clear_sbus_fast_mode(struct hfi1_devdata *dd)
16508c2ecf20Sopenharmony_ci{
16518c2ecf20Sopenharmony_ci	u64 reg, count = 0;
16528c2ecf20Sopenharmony_ci
16538c2ecf20Sopenharmony_ci	reg = read_csr(dd, ASIC_STS_SBUS_COUNTERS);
16548c2ecf20Sopenharmony_ci	while (SBUS_COUNTER(reg, EXECUTE) !=
16558c2ecf20Sopenharmony_ci	       SBUS_COUNTER(reg, RCV_DATA_VALID)) {
16568c2ecf20Sopenharmony_ci		if (count++ >= SBUS_MAX_POLL_COUNT)
16578c2ecf20Sopenharmony_ci			break;
16588c2ecf20Sopenharmony_ci		udelay(1);
16598c2ecf20Sopenharmony_ci		reg = read_csr(dd, ASIC_STS_SBUS_COUNTERS);
16608c2ecf20Sopenharmony_ci	}
16618c2ecf20Sopenharmony_ci	write_csr(dd, ASIC_CFG_SBUS_EXECUTE, 0);
16628c2ecf20Sopenharmony_ci}
16638c2ecf20Sopenharmony_ci
16648c2ecf20Sopenharmony_ciint load_firmware(struct hfi1_devdata *dd)
16658c2ecf20Sopenharmony_ci{
16668c2ecf20Sopenharmony_ci	int ret;
16678c2ecf20Sopenharmony_ci
16688c2ecf20Sopenharmony_ci	if (fw_fabric_serdes_load) {
16698c2ecf20Sopenharmony_ci		ret = acquire_chip_resource(dd, CR_SBUS, SBUS_TIMEOUT);
16708c2ecf20Sopenharmony_ci		if (ret)
16718c2ecf20Sopenharmony_ci			return ret;
16728c2ecf20Sopenharmony_ci
16738c2ecf20Sopenharmony_ci		set_sbus_fast_mode(dd);
16748c2ecf20Sopenharmony_ci
16758c2ecf20Sopenharmony_ci		set_serdes_broadcast(dd, all_fabric_serdes_broadcast,
16768c2ecf20Sopenharmony_ci				     fabric_serdes_broadcast[dd->hfi1_id],
16778c2ecf20Sopenharmony_ci				     fabric_serdes_addrs[dd->hfi1_id],
16788c2ecf20Sopenharmony_ci				     NUM_FABRIC_SERDES);
16798c2ecf20Sopenharmony_ci		turn_off_spicos(dd, SPICO_FABRIC);
16808c2ecf20Sopenharmony_ci		do {
16818c2ecf20Sopenharmony_ci			ret = load_fabric_serdes_firmware(dd, &fw_fabric);
16828c2ecf20Sopenharmony_ci		} while (retry_firmware(dd, ret));
16838c2ecf20Sopenharmony_ci
16848c2ecf20Sopenharmony_ci		clear_sbus_fast_mode(dd);
16858c2ecf20Sopenharmony_ci		release_chip_resource(dd, CR_SBUS);
16868c2ecf20Sopenharmony_ci		if (ret)
16878c2ecf20Sopenharmony_ci			return ret;
16888c2ecf20Sopenharmony_ci	}
16898c2ecf20Sopenharmony_ci
16908c2ecf20Sopenharmony_ci	if (fw_8051_load) {
16918c2ecf20Sopenharmony_ci		do {
16928c2ecf20Sopenharmony_ci			ret = load_8051_firmware(dd, &fw_8051);
16938c2ecf20Sopenharmony_ci		} while (retry_firmware(dd, ret));
16948c2ecf20Sopenharmony_ci		if (ret)
16958c2ecf20Sopenharmony_ci			return ret;
16968c2ecf20Sopenharmony_ci	}
16978c2ecf20Sopenharmony_ci
16988c2ecf20Sopenharmony_ci	dump_fw_version(dd);
16998c2ecf20Sopenharmony_ci	return 0;
17008c2ecf20Sopenharmony_ci}
17018c2ecf20Sopenharmony_ci
17028c2ecf20Sopenharmony_ciint hfi1_firmware_init(struct hfi1_devdata *dd)
17038c2ecf20Sopenharmony_ci{
17048c2ecf20Sopenharmony_ci	/* only RTL can use these */
17058c2ecf20Sopenharmony_ci	if (dd->icode != ICODE_RTL_SILICON) {
17068c2ecf20Sopenharmony_ci		fw_fabric_serdes_load = 0;
17078c2ecf20Sopenharmony_ci		fw_pcie_serdes_load = 0;
17088c2ecf20Sopenharmony_ci		fw_sbus_load = 0;
17098c2ecf20Sopenharmony_ci	}
17108c2ecf20Sopenharmony_ci
17118c2ecf20Sopenharmony_ci	/* no 8051 or QSFP on simulator */
17128c2ecf20Sopenharmony_ci	if (dd->icode == ICODE_FUNCTIONAL_SIMULATOR)
17138c2ecf20Sopenharmony_ci		fw_8051_load = 0;
17148c2ecf20Sopenharmony_ci
17158c2ecf20Sopenharmony_ci	if (!fw_8051_name) {
17168c2ecf20Sopenharmony_ci		if (dd->icode == ICODE_RTL_SILICON)
17178c2ecf20Sopenharmony_ci			fw_8051_name = DEFAULT_FW_8051_NAME_ASIC;
17188c2ecf20Sopenharmony_ci		else
17198c2ecf20Sopenharmony_ci			fw_8051_name = DEFAULT_FW_8051_NAME_FPGA;
17208c2ecf20Sopenharmony_ci	}
17218c2ecf20Sopenharmony_ci	if (!fw_fabric_serdes_name)
17228c2ecf20Sopenharmony_ci		fw_fabric_serdes_name = DEFAULT_FW_FABRIC_NAME;
17238c2ecf20Sopenharmony_ci	if (!fw_sbus_name)
17248c2ecf20Sopenharmony_ci		fw_sbus_name = DEFAULT_FW_SBUS_NAME;
17258c2ecf20Sopenharmony_ci	if (!fw_pcie_serdes_name)
17268c2ecf20Sopenharmony_ci		fw_pcie_serdes_name = DEFAULT_FW_PCIE_NAME;
17278c2ecf20Sopenharmony_ci
17288c2ecf20Sopenharmony_ci	return obtain_firmware(dd);
17298c2ecf20Sopenharmony_ci}
17308c2ecf20Sopenharmony_ci
17318c2ecf20Sopenharmony_ci/*
17328c2ecf20Sopenharmony_ci * This function is a helper function for parse_platform_config(...) and
17338c2ecf20Sopenharmony_ci * does not check for validity of the platform configuration cache
17348c2ecf20Sopenharmony_ci * (because we know it is invalid as we are building up the cache).
17358c2ecf20Sopenharmony_ci * As such, this should not be called from anywhere other than
17368c2ecf20Sopenharmony_ci * parse_platform_config
17378c2ecf20Sopenharmony_ci */
17388c2ecf20Sopenharmony_cistatic int check_meta_version(struct hfi1_devdata *dd, u32 *system_table)
17398c2ecf20Sopenharmony_ci{
17408c2ecf20Sopenharmony_ci	u32 meta_ver, meta_ver_meta, ver_start, ver_len, mask;
17418c2ecf20Sopenharmony_ci	struct platform_config_cache *pcfgcache = &dd->pcfg_cache;
17428c2ecf20Sopenharmony_ci
17438c2ecf20Sopenharmony_ci	if (!system_table)
17448c2ecf20Sopenharmony_ci		return -EINVAL;
17458c2ecf20Sopenharmony_ci
17468c2ecf20Sopenharmony_ci	meta_ver_meta =
17478c2ecf20Sopenharmony_ci	*(pcfgcache->config_tables[PLATFORM_CONFIG_SYSTEM_TABLE].table_metadata
17488c2ecf20Sopenharmony_ci	+ SYSTEM_TABLE_META_VERSION);
17498c2ecf20Sopenharmony_ci
17508c2ecf20Sopenharmony_ci	mask = ((1 << METADATA_TABLE_FIELD_START_LEN_BITS) - 1);
17518c2ecf20Sopenharmony_ci	ver_start = meta_ver_meta & mask;
17528c2ecf20Sopenharmony_ci
17538c2ecf20Sopenharmony_ci	meta_ver_meta >>= METADATA_TABLE_FIELD_LEN_SHIFT;
17548c2ecf20Sopenharmony_ci
17558c2ecf20Sopenharmony_ci	mask = ((1 << METADATA_TABLE_FIELD_LEN_LEN_BITS) - 1);
17568c2ecf20Sopenharmony_ci	ver_len = meta_ver_meta & mask;
17578c2ecf20Sopenharmony_ci
17588c2ecf20Sopenharmony_ci	ver_start /= 8;
17598c2ecf20Sopenharmony_ci	meta_ver = *((u8 *)system_table + ver_start) & ((1 << ver_len) - 1);
17608c2ecf20Sopenharmony_ci
17618c2ecf20Sopenharmony_ci	if (meta_ver < 4) {
17628c2ecf20Sopenharmony_ci		dd_dev_info(
17638c2ecf20Sopenharmony_ci			dd, "%s:Please update platform config\n", __func__);
17648c2ecf20Sopenharmony_ci		return -EINVAL;
17658c2ecf20Sopenharmony_ci	}
17668c2ecf20Sopenharmony_ci	return 0;
17678c2ecf20Sopenharmony_ci}
17688c2ecf20Sopenharmony_ci
17698c2ecf20Sopenharmony_ciint parse_platform_config(struct hfi1_devdata *dd)
17708c2ecf20Sopenharmony_ci{
17718c2ecf20Sopenharmony_ci	struct platform_config_cache *pcfgcache = &dd->pcfg_cache;
17728c2ecf20Sopenharmony_ci	struct hfi1_pportdata *ppd = dd->pport;
17738c2ecf20Sopenharmony_ci	u32 *ptr = NULL;
17748c2ecf20Sopenharmony_ci	u32 header1 = 0, header2 = 0, magic_num = 0, crc = 0, file_length = 0;
17758c2ecf20Sopenharmony_ci	u32 record_idx = 0, table_type = 0, table_length_dwords = 0;
17768c2ecf20Sopenharmony_ci	int ret = -EINVAL; /* assume failure */
17778c2ecf20Sopenharmony_ci
17788c2ecf20Sopenharmony_ci	/*
17798c2ecf20Sopenharmony_ci	 * For integrated devices that did not fall back to the default file,
17808c2ecf20Sopenharmony_ci	 * the SI tuning information for active channels is acquired from the
17818c2ecf20Sopenharmony_ci	 * scratch register bitmap, thus there is no platform config to parse.
17828c2ecf20Sopenharmony_ci	 * Skip parsing in these situations.
17838c2ecf20Sopenharmony_ci	 */
17848c2ecf20Sopenharmony_ci	if (ppd->config_from_scratch)
17858c2ecf20Sopenharmony_ci		return 0;
17868c2ecf20Sopenharmony_ci
17878c2ecf20Sopenharmony_ci	if (!dd->platform_config.data) {
17888c2ecf20Sopenharmony_ci		dd_dev_err(dd, "%s: Missing config file\n", __func__);
17898c2ecf20Sopenharmony_ci		ret = -EINVAL;
17908c2ecf20Sopenharmony_ci		goto bail;
17918c2ecf20Sopenharmony_ci	}
17928c2ecf20Sopenharmony_ci	ptr = (u32 *)dd->platform_config.data;
17938c2ecf20Sopenharmony_ci
17948c2ecf20Sopenharmony_ci	magic_num = *ptr;
17958c2ecf20Sopenharmony_ci	ptr++;
17968c2ecf20Sopenharmony_ci	if (magic_num != PLATFORM_CONFIG_MAGIC_NUM) {
17978c2ecf20Sopenharmony_ci		dd_dev_err(dd, "%s: Bad config file\n", __func__);
17988c2ecf20Sopenharmony_ci		ret = -EINVAL;
17998c2ecf20Sopenharmony_ci		goto bail;
18008c2ecf20Sopenharmony_ci	}
18018c2ecf20Sopenharmony_ci
18028c2ecf20Sopenharmony_ci	/* Field is file size in DWORDs */
18038c2ecf20Sopenharmony_ci	file_length = (*ptr) * 4;
18048c2ecf20Sopenharmony_ci
18058c2ecf20Sopenharmony_ci	/*
18068c2ecf20Sopenharmony_ci	 * Length can't be larger than partition size. Assume platform
18078c2ecf20Sopenharmony_ci	 * config format version 4 is being used. Interpret the file size
18088c2ecf20Sopenharmony_ci	 * field as header instead by not moving the pointer.
18098c2ecf20Sopenharmony_ci	 */
18108c2ecf20Sopenharmony_ci	if (file_length > MAX_PLATFORM_CONFIG_FILE_SIZE) {
18118c2ecf20Sopenharmony_ci		dd_dev_info(dd,
18128c2ecf20Sopenharmony_ci			    "%s:File length out of bounds, using alternative format\n",
18138c2ecf20Sopenharmony_ci			    __func__);
18148c2ecf20Sopenharmony_ci		file_length = PLATFORM_CONFIG_FORMAT_4_FILE_SIZE;
18158c2ecf20Sopenharmony_ci	} else {
18168c2ecf20Sopenharmony_ci		ptr++;
18178c2ecf20Sopenharmony_ci	}
18188c2ecf20Sopenharmony_ci
18198c2ecf20Sopenharmony_ci	if (file_length > dd->platform_config.size) {
18208c2ecf20Sopenharmony_ci		dd_dev_info(dd, "%s:File claims to be larger than read size\n",
18218c2ecf20Sopenharmony_ci			    __func__);
18228c2ecf20Sopenharmony_ci		ret = -EINVAL;
18238c2ecf20Sopenharmony_ci		goto bail;
18248c2ecf20Sopenharmony_ci	} else if (file_length < dd->platform_config.size) {
18258c2ecf20Sopenharmony_ci		dd_dev_info(dd,
18268c2ecf20Sopenharmony_ci			    "%s:File claims to be smaller than read size, continuing\n",
18278c2ecf20Sopenharmony_ci			    __func__);
18288c2ecf20Sopenharmony_ci	}
18298c2ecf20Sopenharmony_ci	/* exactly equal, perfection */
18308c2ecf20Sopenharmony_ci
18318c2ecf20Sopenharmony_ci	/*
18328c2ecf20Sopenharmony_ci	 * In both cases where we proceed, using the self-reported file length
18338c2ecf20Sopenharmony_ci	 * is the safer option. In case of old format a predefined value is
18348c2ecf20Sopenharmony_ci	 * being used.
18358c2ecf20Sopenharmony_ci	 */
18368c2ecf20Sopenharmony_ci	while (ptr < (u32 *)(dd->platform_config.data + file_length)) {
18378c2ecf20Sopenharmony_ci		header1 = *ptr;
18388c2ecf20Sopenharmony_ci		header2 = *(ptr + 1);
18398c2ecf20Sopenharmony_ci		if (header1 != ~header2) {
18408c2ecf20Sopenharmony_ci			dd_dev_err(dd, "%s: Failed validation at offset %ld\n",
18418c2ecf20Sopenharmony_ci				   __func__, (ptr - (u32 *)
18428c2ecf20Sopenharmony_ci					      dd->platform_config.data));
18438c2ecf20Sopenharmony_ci			ret = -EINVAL;
18448c2ecf20Sopenharmony_ci			goto bail;
18458c2ecf20Sopenharmony_ci		}
18468c2ecf20Sopenharmony_ci
18478c2ecf20Sopenharmony_ci		record_idx = *ptr &
18488c2ecf20Sopenharmony_ci			((1 << PLATFORM_CONFIG_HEADER_RECORD_IDX_LEN_BITS) - 1);
18498c2ecf20Sopenharmony_ci
18508c2ecf20Sopenharmony_ci		table_length_dwords = (*ptr >>
18518c2ecf20Sopenharmony_ci				PLATFORM_CONFIG_HEADER_TABLE_LENGTH_SHIFT) &
18528c2ecf20Sopenharmony_ci		      ((1 << PLATFORM_CONFIG_HEADER_TABLE_LENGTH_LEN_BITS) - 1);
18538c2ecf20Sopenharmony_ci
18548c2ecf20Sopenharmony_ci		table_type = (*ptr >> PLATFORM_CONFIG_HEADER_TABLE_TYPE_SHIFT) &
18558c2ecf20Sopenharmony_ci			((1 << PLATFORM_CONFIG_HEADER_TABLE_TYPE_LEN_BITS) - 1);
18568c2ecf20Sopenharmony_ci
18578c2ecf20Sopenharmony_ci		/* Done with this set of headers */
18588c2ecf20Sopenharmony_ci		ptr += 2;
18598c2ecf20Sopenharmony_ci
18608c2ecf20Sopenharmony_ci		if (record_idx) {
18618c2ecf20Sopenharmony_ci			/* data table */
18628c2ecf20Sopenharmony_ci			switch (table_type) {
18638c2ecf20Sopenharmony_ci			case PLATFORM_CONFIG_SYSTEM_TABLE:
18648c2ecf20Sopenharmony_ci				pcfgcache->config_tables[table_type].num_table =
18658c2ecf20Sopenharmony_ci									1;
18668c2ecf20Sopenharmony_ci				ret = check_meta_version(dd, ptr);
18678c2ecf20Sopenharmony_ci				if (ret)
18688c2ecf20Sopenharmony_ci					goto bail;
18698c2ecf20Sopenharmony_ci				break;
18708c2ecf20Sopenharmony_ci			case PLATFORM_CONFIG_PORT_TABLE:
18718c2ecf20Sopenharmony_ci				pcfgcache->config_tables[table_type].num_table =
18728c2ecf20Sopenharmony_ci									2;
18738c2ecf20Sopenharmony_ci				break;
18748c2ecf20Sopenharmony_ci			case PLATFORM_CONFIG_RX_PRESET_TABLE:
18758c2ecf20Sopenharmony_ci			case PLATFORM_CONFIG_TX_PRESET_TABLE:
18768c2ecf20Sopenharmony_ci			case PLATFORM_CONFIG_QSFP_ATTEN_TABLE:
18778c2ecf20Sopenharmony_ci			case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE:
18788c2ecf20Sopenharmony_ci				pcfgcache->config_tables[table_type].num_table =
18798c2ecf20Sopenharmony_ci							table_length_dwords;
18808c2ecf20Sopenharmony_ci				break;
18818c2ecf20Sopenharmony_ci			default:
18828c2ecf20Sopenharmony_ci				dd_dev_err(dd,
18838c2ecf20Sopenharmony_ci					   "%s: Unknown data table %d, offset %ld\n",
18848c2ecf20Sopenharmony_ci					   __func__, table_type,
18858c2ecf20Sopenharmony_ci					   (ptr - (u32 *)
18868c2ecf20Sopenharmony_ci					    dd->platform_config.data));
18878c2ecf20Sopenharmony_ci				ret = -EINVAL;
18888c2ecf20Sopenharmony_ci				goto bail; /* We don't trust this file now */
18898c2ecf20Sopenharmony_ci			}
18908c2ecf20Sopenharmony_ci			pcfgcache->config_tables[table_type].table = ptr;
18918c2ecf20Sopenharmony_ci		} else {
18928c2ecf20Sopenharmony_ci			/* metadata table */
18938c2ecf20Sopenharmony_ci			switch (table_type) {
18948c2ecf20Sopenharmony_ci			case PLATFORM_CONFIG_SYSTEM_TABLE:
18958c2ecf20Sopenharmony_ci			case PLATFORM_CONFIG_PORT_TABLE:
18968c2ecf20Sopenharmony_ci			case PLATFORM_CONFIG_RX_PRESET_TABLE:
18978c2ecf20Sopenharmony_ci			case PLATFORM_CONFIG_TX_PRESET_TABLE:
18988c2ecf20Sopenharmony_ci			case PLATFORM_CONFIG_QSFP_ATTEN_TABLE:
18998c2ecf20Sopenharmony_ci			case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE:
19008c2ecf20Sopenharmony_ci				break;
19018c2ecf20Sopenharmony_ci			default:
19028c2ecf20Sopenharmony_ci				dd_dev_err(dd,
19038c2ecf20Sopenharmony_ci					   "%s: Unknown meta table %d, offset %ld\n",
19048c2ecf20Sopenharmony_ci					   __func__, table_type,
19058c2ecf20Sopenharmony_ci					   (ptr -
19068c2ecf20Sopenharmony_ci					    (u32 *)dd->platform_config.data));
19078c2ecf20Sopenharmony_ci				ret = -EINVAL;
19088c2ecf20Sopenharmony_ci				goto bail; /* We don't trust this file now */
19098c2ecf20Sopenharmony_ci			}
19108c2ecf20Sopenharmony_ci			pcfgcache->config_tables[table_type].table_metadata =
19118c2ecf20Sopenharmony_ci									ptr;
19128c2ecf20Sopenharmony_ci		}
19138c2ecf20Sopenharmony_ci
19148c2ecf20Sopenharmony_ci		/* Calculate and check table crc */
19158c2ecf20Sopenharmony_ci		crc = crc32_le(~(u32)0, (unsigned char const *)ptr,
19168c2ecf20Sopenharmony_ci			       (table_length_dwords * 4));
19178c2ecf20Sopenharmony_ci		crc ^= ~(u32)0;
19188c2ecf20Sopenharmony_ci
19198c2ecf20Sopenharmony_ci		/* Jump the table */
19208c2ecf20Sopenharmony_ci		ptr += table_length_dwords;
19218c2ecf20Sopenharmony_ci		if (crc != *ptr) {
19228c2ecf20Sopenharmony_ci			dd_dev_err(dd, "%s: Failed CRC check at offset %ld\n",
19238c2ecf20Sopenharmony_ci				   __func__, (ptr -
19248c2ecf20Sopenharmony_ci				   (u32 *)dd->platform_config.data));
19258c2ecf20Sopenharmony_ci			ret = -EINVAL;
19268c2ecf20Sopenharmony_ci			goto bail;
19278c2ecf20Sopenharmony_ci		}
19288c2ecf20Sopenharmony_ci		/* Jump the CRC DWORD */
19298c2ecf20Sopenharmony_ci		ptr++;
19308c2ecf20Sopenharmony_ci	}
19318c2ecf20Sopenharmony_ci
19328c2ecf20Sopenharmony_ci	pcfgcache->cache_valid = 1;
19338c2ecf20Sopenharmony_ci	return 0;
19348c2ecf20Sopenharmony_cibail:
19358c2ecf20Sopenharmony_ci	memset(pcfgcache, 0, sizeof(struct platform_config_cache));
19368c2ecf20Sopenharmony_ci	return ret;
19378c2ecf20Sopenharmony_ci}
19388c2ecf20Sopenharmony_ci
19398c2ecf20Sopenharmony_cistatic void get_integrated_platform_config_field(
19408c2ecf20Sopenharmony_ci		struct hfi1_devdata *dd,
19418c2ecf20Sopenharmony_ci		enum platform_config_table_type_encoding table_type,
19428c2ecf20Sopenharmony_ci		int field_index, u32 *data)
19438c2ecf20Sopenharmony_ci{
19448c2ecf20Sopenharmony_ci	struct hfi1_pportdata *ppd = dd->pport;
19458c2ecf20Sopenharmony_ci	u8 *cache = ppd->qsfp_info.cache;
19468c2ecf20Sopenharmony_ci	u32 tx_preset = 0;
19478c2ecf20Sopenharmony_ci
19488c2ecf20Sopenharmony_ci	switch (table_type) {
19498c2ecf20Sopenharmony_ci	case PLATFORM_CONFIG_SYSTEM_TABLE:
19508c2ecf20Sopenharmony_ci		if (field_index == SYSTEM_TABLE_QSFP_POWER_CLASS_MAX)
19518c2ecf20Sopenharmony_ci			*data = ppd->max_power_class;
19528c2ecf20Sopenharmony_ci		else if (field_index == SYSTEM_TABLE_QSFP_ATTENUATION_DEFAULT_25G)
19538c2ecf20Sopenharmony_ci			*data = ppd->default_atten;
19548c2ecf20Sopenharmony_ci		break;
19558c2ecf20Sopenharmony_ci	case PLATFORM_CONFIG_PORT_TABLE:
19568c2ecf20Sopenharmony_ci		if (field_index == PORT_TABLE_PORT_TYPE)
19578c2ecf20Sopenharmony_ci			*data = ppd->port_type;
19588c2ecf20Sopenharmony_ci		else if (field_index == PORT_TABLE_LOCAL_ATTEN_25G)
19598c2ecf20Sopenharmony_ci			*data = ppd->local_atten;
19608c2ecf20Sopenharmony_ci		else if (field_index == PORT_TABLE_REMOTE_ATTEN_25G)
19618c2ecf20Sopenharmony_ci			*data = ppd->remote_atten;
19628c2ecf20Sopenharmony_ci		break;
19638c2ecf20Sopenharmony_ci	case PLATFORM_CONFIG_RX_PRESET_TABLE:
19648c2ecf20Sopenharmony_ci		if (field_index == RX_PRESET_TABLE_QSFP_RX_CDR_APPLY)
19658c2ecf20Sopenharmony_ci			*data = (ppd->rx_preset & QSFP_RX_CDR_APPLY_SMASK) >>
19668c2ecf20Sopenharmony_ci				QSFP_RX_CDR_APPLY_SHIFT;
19678c2ecf20Sopenharmony_ci		else if (field_index == RX_PRESET_TABLE_QSFP_RX_EMP_APPLY)
19688c2ecf20Sopenharmony_ci			*data = (ppd->rx_preset & QSFP_RX_EMP_APPLY_SMASK) >>
19698c2ecf20Sopenharmony_ci				QSFP_RX_EMP_APPLY_SHIFT;
19708c2ecf20Sopenharmony_ci		else if (field_index == RX_PRESET_TABLE_QSFP_RX_AMP_APPLY)
19718c2ecf20Sopenharmony_ci			*data = (ppd->rx_preset & QSFP_RX_AMP_APPLY_SMASK) >>
19728c2ecf20Sopenharmony_ci				QSFP_RX_AMP_APPLY_SHIFT;
19738c2ecf20Sopenharmony_ci		else if (field_index == RX_PRESET_TABLE_QSFP_RX_CDR)
19748c2ecf20Sopenharmony_ci			*data = (ppd->rx_preset & QSFP_RX_CDR_SMASK) >>
19758c2ecf20Sopenharmony_ci				QSFP_RX_CDR_SHIFT;
19768c2ecf20Sopenharmony_ci		else if (field_index == RX_PRESET_TABLE_QSFP_RX_EMP)
19778c2ecf20Sopenharmony_ci			*data = (ppd->rx_preset & QSFP_RX_EMP_SMASK) >>
19788c2ecf20Sopenharmony_ci				QSFP_RX_EMP_SHIFT;
19798c2ecf20Sopenharmony_ci		else if (field_index == RX_PRESET_TABLE_QSFP_RX_AMP)
19808c2ecf20Sopenharmony_ci			*data = (ppd->rx_preset & QSFP_RX_AMP_SMASK) >>
19818c2ecf20Sopenharmony_ci				QSFP_RX_AMP_SHIFT;
19828c2ecf20Sopenharmony_ci		break;
19838c2ecf20Sopenharmony_ci	case PLATFORM_CONFIG_TX_PRESET_TABLE:
19848c2ecf20Sopenharmony_ci		if (cache[QSFP_EQ_INFO_OFFS] & 0x4)
19858c2ecf20Sopenharmony_ci			tx_preset = ppd->tx_preset_eq;
19868c2ecf20Sopenharmony_ci		else
19878c2ecf20Sopenharmony_ci			tx_preset = ppd->tx_preset_noeq;
19888c2ecf20Sopenharmony_ci		if (field_index == TX_PRESET_TABLE_PRECUR)
19898c2ecf20Sopenharmony_ci			*data = (tx_preset & TX_PRECUR_SMASK) >>
19908c2ecf20Sopenharmony_ci				TX_PRECUR_SHIFT;
19918c2ecf20Sopenharmony_ci		else if (field_index == TX_PRESET_TABLE_ATTN)
19928c2ecf20Sopenharmony_ci			*data = (tx_preset & TX_ATTN_SMASK) >>
19938c2ecf20Sopenharmony_ci				TX_ATTN_SHIFT;
19948c2ecf20Sopenharmony_ci		else if (field_index == TX_PRESET_TABLE_POSTCUR)
19958c2ecf20Sopenharmony_ci			*data = (tx_preset & TX_POSTCUR_SMASK) >>
19968c2ecf20Sopenharmony_ci				TX_POSTCUR_SHIFT;
19978c2ecf20Sopenharmony_ci		else if (field_index == TX_PRESET_TABLE_QSFP_TX_CDR_APPLY)
19988c2ecf20Sopenharmony_ci			*data = (tx_preset & QSFP_TX_CDR_APPLY_SMASK) >>
19998c2ecf20Sopenharmony_ci				QSFP_TX_CDR_APPLY_SHIFT;
20008c2ecf20Sopenharmony_ci		else if (field_index == TX_PRESET_TABLE_QSFP_TX_EQ_APPLY)
20018c2ecf20Sopenharmony_ci			*data = (tx_preset & QSFP_TX_EQ_APPLY_SMASK) >>
20028c2ecf20Sopenharmony_ci				QSFP_TX_EQ_APPLY_SHIFT;
20038c2ecf20Sopenharmony_ci		else if (field_index == TX_PRESET_TABLE_QSFP_TX_CDR)
20048c2ecf20Sopenharmony_ci			*data = (tx_preset & QSFP_TX_CDR_SMASK) >>
20058c2ecf20Sopenharmony_ci				QSFP_TX_CDR_SHIFT;
20068c2ecf20Sopenharmony_ci		else if (field_index == TX_PRESET_TABLE_QSFP_TX_EQ)
20078c2ecf20Sopenharmony_ci			*data = (tx_preset & QSFP_TX_EQ_SMASK) >>
20088c2ecf20Sopenharmony_ci				QSFP_TX_EQ_SHIFT;
20098c2ecf20Sopenharmony_ci		break;
20108c2ecf20Sopenharmony_ci	case PLATFORM_CONFIG_QSFP_ATTEN_TABLE:
20118c2ecf20Sopenharmony_ci	case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE:
20128c2ecf20Sopenharmony_ci	default:
20138c2ecf20Sopenharmony_ci		break;
20148c2ecf20Sopenharmony_ci	}
20158c2ecf20Sopenharmony_ci}
20168c2ecf20Sopenharmony_ci
20178c2ecf20Sopenharmony_cistatic int get_platform_fw_field_metadata(struct hfi1_devdata *dd, int table,
20188c2ecf20Sopenharmony_ci					  int field, u32 *field_len_bits,
20198c2ecf20Sopenharmony_ci					  u32 *field_start_bits)
20208c2ecf20Sopenharmony_ci{
20218c2ecf20Sopenharmony_ci	struct platform_config_cache *pcfgcache = &dd->pcfg_cache;
20228c2ecf20Sopenharmony_ci	u32 *src_ptr = NULL;
20238c2ecf20Sopenharmony_ci
20248c2ecf20Sopenharmony_ci	if (!pcfgcache->cache_valid)
20258c2ecf20Sopenharmony_ci		return -EINVAL;
20268c2ecf20Sopenharmony_ci
20278c2ecf20Sopenharmony_ci	switch (table) {
20288c2ecf20Sopenharmony_ci	case PLATFORM_CONFIG_SYSTEM_TABLE:
20298c2ecf20Sopenharmony_ci	case PLATFORM_CONFIG_PORT_TABLE:
20308c2ecf20Sopenharmony_ci	case PLATFORM_CONFIG_RX_PRESET_TABLE:
20318c2ecf20Sopenharmony_ci	case PLATFORM_CONFIG_TX_PRESET_TABLE:
20328c2ecf20Sopenharmony_ci	case PLATFORM_CONFIG_QSFP_ATTEN_TABLE:
20338c2ecf20Sopenharmony_ci	case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE:
20348c2ecf20Sopenharmony_ci		if (field && field < platform_config_table_limits[table])
20358c2ecf20Sopenharmony_ci			src_ptr =
20368c2ecf20Sopenharmony_ci			pcfgcache->config_tables[table].table_metadata + field;
20378c2ecf20Sopenharmony_ci		break;
20388c2ecf20Sopenharmony_ci	default:
20398c2ecf20Sopenharmony_ci		dd_dev_info(dd, "%s: Unknown table\n", __func__);
20408c2ecf20Sopenharmony_ci		break;
20418c2ecf20Sopenharmony_ci	}
20428c2ecf20Sopenharmony_ci
20438c2ecf20Sopenharmony_ci	if (!src_ptr)
20448c2ecf20Sopenharmony_ci		return -EINVAL;
20458c2ecf20Sopenharmony_ci
20468c2ecf20Sopenharmony_ci	if (field_start_bits)
20478c2ecf20Sopenharmony_ci		*field_start_bits = *src_ptr &
20488c2ecf20Sopenharmony_ci		      ((1 << METADATA_TABLE_FIELD_START_LEN_BITS) - 1);
20498c2ecf20Sopenharmony_ci
20508c2ecf20Sopenharmony_ci	if (field_len_bits)
20518c2ecf20Sopenharmony_ci		*field_len_bits = (*src_ptr >> METADATA_TABLE_FIELD_LEN_SHIFT)
20528c2ecf20Sopenharmony_ci		       & ((1 << METADATA_TABLE_FIELD_LEN_LEN_BITS) - 1);
20538c2ecf20Sopenharmony_ci
20548c2ecf20Sopenharmony_ci	return 0;
20558c2ecf20Sopenharmony_ci}
20568c2ecf20Sopenharmony_ci
20578c2ecf20Sopenharmony_ci/* This is the central interface to getting data out of the platform config
20588c2ecf20Sopenharmony_ci * file. It depends on parse_platform_config() having populated the
20598c2ecf20Sopenharmony_ci * platform_config_cache in hfi1_devdata, and checks the cache_valid member to
20608c2ecf20Sopenharmony_ci * validate the sanity of the cache.
20618c2ecf20Sopenharmony_ci *
20628c2ecf20Sopenharmony_ci * The non-obvious parameters:
20638c2ecf20Sopenharmony_ci * @table_index: Acts as a look up key into which instance of the tables the
20648c2ecf20Sopenharmony_ci * relevant field is fetched from.
20658c2ecf20Sopenharmony_ci *
20668c2ecf20Sopenharmony_ci * This applies to the data tables that have multiple instances. The port table
20678c2ecf20Sopenharmony_ci * is an exception to this rule as each HFI only has one port and thus the
20688c2ecf20Sopenharmony_ci * relevant table can be distinguished by hfi_id.
20698c2ecf20Sopenharmony_ci *
20708c2ecf20Sopenharmony_ci * @data: pointer to memory that will be populated with the field requested.
20718c2ecf20Sopenharmony_ci * @len: length of memory pointed by @data in bytes.
20728c2ecf20Sopenharmony_ci */
20738c2ecf20Sopenharmony_ciint get_platform_config_field(struct hfi1_devdata *dd,
20748c2ecf20Sopenharmony_ci			      enum platform_config_table_type_encoding
20758c2ecf20Sopenharmony_ci			      table_type, int table_index, int field_index,
20768c2ecf20Sopenharmony_ci			      u32 *data, u32 len)
20778c2ecf20Sopenharmony_ci{
20788c2ecf20Sopenharmony_ci	int ret = 0, wlen = 0, seek = 0;
20798c2ecf20Sopenharmony_ci	u32 field_len_bits = 0, field_start_bits = 0, *src_ptr = NULL;
20808c2ecf20Sopenharmony_ci	struct platform_config_cache *pcfgcache = &dd->pcfg_cache;
20818c2ecf20Sopenharmony_ci	struct hfi1_pportdata *ppd = dd->pport;
20828c2ecf20Sopenharmony_ci
20838c2ecf20Sopenharmony_ci	if (data)
20848c2ecf20Sopenharmony_ci		memset(data, 0, len);
20858c2ecf20Sopenharmony_ci	else
20868c2ecf20Sopenharmony_ci		return -EINVAL;
20878c2ecf20Sopenharmony_ci
20888c2ecf20Sopenharmony_ci	if (ppd->config_from_scratch) {
20898c2ecf20Sopenharmony_ci		/*
20908c2ecf20Sopenharmony_ci		 * Use saved configuration from ppd for integrated platforms
20918c2ecf20Sopenharmony_ci		 */
20928c2ecf20Sopenharmony_ci		get_integrated_platform_config_field(dd, table_type,
20938c2ecf20Sopenharmony_ci						     field_index, data);
20948c2ecf20Sopenharmony_ci		return 0;
20958c2ecf20Sopenharmony_ci	}
20968c2ecf20Sopenharmony_ci
20978c2ecf20Sopenharmony_ci	ret = get_platform_fw_field_metadata(dd, table_type, field_index,
20988c2ecf20Sopenharmony_ci					     &field_len_bits,
20998c2ecf20Sopenharmony_ci					     &field_start_bits);
21008c2ecf20Sopenharmony_ci	if (ret)
21018c2ecf20Sopenharmony_ci		return -EINVAL;
21028c2ecf20Sopenharmony_ci
21038c2ecf20Sopenharmony_ci	/* Convert length to bits */
21048c2ecf20Sopenharmony_ci	len *= 8;
21058c2ecf20Sopenharmony_ci
21068c2ecf20Sopenharmony_ci	/* Our metadata function checked cache_valid and field_index for us */
21078c2ecf20Sopenharmony_ci	switch (table_type) {
21088c2ecf20Sopenharmony_ci	case PLATFORM_CONFIG_SYSTEM_TABLE:
21098c2ecf20Sopenharmony_ci		src_ptr = pcfgcache->config_tables[table_type].table;
21108c2ecf20Sopenharmony_ci
21118c2ecf20Sopenharmony_ci		if (field_index != SYSTEM_TABLE_QSFP_POWER_CLASS_MAX) {
21128c2ecf20Sopenharmony_ci			if (len < field_len_bits)
21138c2ecf20Sopenharmony_ci				return -EINVAL;
21148c2ecf20Sopenharmony_ci
21158c2ecf20Sopenharmony_ci			seek = field_start_bits / 8;
21168c2ecf20Sopenharmony_ci			wlen = field_len_bits / 8;
21178c2ecf20Sopenharmony_ci
21188c2ecf20Sopenharmony_ci			src_ptr = (u32 *)((u8 *)src_ptr + seek);
21198c2ecf20Sopenharmony_ci
21208c2ecf20Sopenharmony_ci			/*
21218c2ecf20Sopenharmony_ci			 * We expect the field to be byte aligned and whole byte
21228c2ecf20Sopenharmony_ci			 * lengths if we are here
21238c2ecf20Sopenharmony_ci			 */
21248c2ecf20Sopenharmony_ci			memcpy(data, src_ptr, wlen);
21258c2ecf20Sopenharmony_ci			return 0;
21268c2ecf20Sopenharmony_ci		}
21278c2ecf20Sopenharmony_ci		break;
21288c2ecf20Sopenharmony_ci	case PLATFORM_CONFIG_PORT_TABLE:
21298c2ecf20Sopenharmony_ci		/* Port table is 4 DWORDS */
21308c2ecf20Sopenharmony_ci		src_ptr = dd->hfi1_id ?
21318c2ecf20Sopenharmony_ci			pcfgcache->config_tables[table_type].table + 4 :
21328c2ecf20Sopenharmony_ci			pcfgcache->config_tables[table_type].table;
21338c2ecf20Sopenharmony_ci		break;
21348c2ecf20Sopenharmony_ci	case PLATFORM_CONFIG_RX_PRESET_TABLE:
21358c2ecf20Sopenharmony_ci	case PLATFORM_CONFIG_TX_PRESET_TABLE:
21368c2ecf20Sopenharmony_ci	case PLATFORM_CONFIG_QSFP_ATTEN_TABLE:
21378c2ecf20Sopenharmony_ci	case PLATFORM_CONFIG_VARIABLE_SETTINGS_TABLE:
21388c2ecf20Sopenharmony_ci		src_ptr = pcfgcache->config_tables[table_type].table;
21398c2ecf20Sopenharmony_ci
21408c2ecf20Sopenharmony_ci		if (table_index <
21418c2ecf20Sopenharmony_ci			pcfgcache->config_tables[table_type].num_table)
21428c2ecf20Sopenharmony_ci			src_ptr += table_index;
21438c2ecf20Sopenharmony_ci		else
21448c2ecf20Sopenharmony_ci			src_ptr = NULL;
21458c2ecf20Sopenharmony_ci		break;
21468c2ecf20Sopenharmony_ci	default:
21478c2ecf20Sopenharmony_ci		dd_dev_info(dd, "%s: Unknown table\n", __func__);
21488c2ecf20Sopenharmony_ci		break;
21498c2ecf20Sopenharmony_ci	}
21508c2ecf20Sopenharmony_ci
21518c2ecf20Sopenharmony_ci	if (!src_ptr || len < field_len_bits)
21528c2ecf20Sopenharmony_ci		return -EINVAL;
21538c2ecf20Sopenharmony_ci
21548c2ecf20Sopenharmony_ci	src_ptr += (field_start_bits / 32);
21558c2ecf20Sopenharmony_ci	*data = (*src_ptr >> (field_start_bits % 32)) &
21568c2ecf20Sopenharmony_ci			((1 << field_len_bits) - 1);
21578c2ecf20Sopenharmony_ci
21588c2ecf20Sopenharmony_ci	return 0;
21598c2ecf20Sopenharmony_ci}
21608c2ecf20Sopenharmony_ci
21618c2ecf20Sopenharmony_ci/*
21628c2ecf20Sopenharmony_ci * Download the firmware needed for the Gen3 PCIe SerDes.  An update
21638c2ecf20Sopenharmony_ci * to the SBus firmware is needed before updating the PCIe firmware.
21648c2ecf20Sopenharmony_ci *
21658c2ecf20Sopenharmony_ci * Note: caller must be holding the SBus resource.
21668c2ecf20Sopenharmony_ci */
21678c2ecf20Sopenharmony_ciint load_pcie_firmware(struct hfi1_devdata *dd)
21688c2ecf20Sopenharmony_ci{
21698c2ecf20Sopenharmony_ci	int ret = 0;
21708c2ecf20Sopenharmony_ci
21718c2ecf20Sopenharmony_ci	/* both firmware loads below use the SBus */
21728c2ecf20Sopenharmony_ci	set_sbus_fast_mode(dd);
21738c2ecf20Sopenharmony_ci
21748c2ecf20Sopenharmony_ci	if (fw_sbus_load) {
21758c2ecf20Sopenharmony_ci		turn_off_spicos(dd, SPICO_SBUS);
21768c2ecf20Sopenharmony_ci		do {
21778c2ecf20Sopenharmony_ci			ret = load_sbus_firmware(dd, &fw_sbus);
21788c2ecf20Sopenharmony_ci		} while (retry_firmware(dd, ret));
21798c2ecf20Sopenharmony_ci		if (ret)
21808c2ecf20Sopenharmony_ci			goto done;
21818c2ecf20Sopenharmony_ci	}
21828c2ecf20Sopenharmony_ci
21838c2ecf20Sopenharmony_ci	if (fw_pcie_serdes_load) {
21848c2ecf20Sopenharmony_ci		dd_dev_info(dd, "Setting PCIe SerDes broadcast\n");
21858c2ecf20Sopenharmony_ci		set_serdes_broadcast(dd, all_pcie_serdes_broadcast,
21868c2ecf20Sopenharmony_ci				     pcie_serdes_broadcast[dd->hfi1_id],
21878c2ecf20Sopenharmony_ci				     pcie_serdes_addrs[dd->hfi1_id],
21888c2ecf20Sopenharmony_ci				     NUM_PCIE_SERDES);
21898c2ecf20Sopenharmony_ci		do {
21908c2ecf20Sopenharmony_ci			ret = load_pcie_serdes_firmware(dd, &fw_pcie);
21918c2ecf20Sopenharmony_ci		} while (retry_firmware(dd, ret));
21928c2ecf20Sopenharmony_ci		if (ret)
21938c2ecf20Sopenharmony_ci			goto done;
21948c2ecf20Sopenharmony_ci	}
21958c2ecf20Sopenharmony_ci
21968c2ecf20Sopenharmony_cidone:
21978c2ecf20Sopenharmony_ci	clear_sbus_fast_mode(dd);
21988c2ecf20Sopenharmony_ci
21998c2ecf20Sopenharmony_ci	return ret;
22008c2ecf20Sopenharmony_ci}
22018c2ecf20Sopenharmony_ci
22028c2ecf20Sopenharmony_ci/*
22038c2ecf20Sopenharmony_ci * Read the GUID from the hardware, store it in dd.
22048c2ecf20Sopenharmony_ci */
22058c2ecf20Sopenharmony_civoid read_guid(struct hfi1_devdata *dd)
22068c2ecf20Sopenharmony_ci{
22078c2ecf20Sopenharmony_ci	/* Take the DC out of reset to get a valid GUID value */
22088c2ecf20Sopenharmony_ci	write_csr(dd, CCE_DC_CTRL, 0);
22098c2ecf20Sopenharmony_ci	(void)read_csr(dd, CCE_DC_CTRL);
22108c2ecf20Sopenharmony_ci
22118c2ecf20Sopenharmony_ci	dd->base_guid = read_csr(dd, DC_DC8051_CFG_LOCAL_GUID);
22128c2ecf20Sopenharmony_ci	dd_dev_info(dd, "GUID %llx",
22138c2ecf20Sopenharmony_ci		    (unsigned long long)dd->base_guid);
22148c2ecf20Sopenharmony_ci}
22158c2ecf20Sopenharmony_ci
22168c2ecf20Sopenharmony_ci/* read and display firmware version info */
22178c2ecf20Sopenharmony_cistatic void dump_fw_version(struct hfi1_devdata *dd)
22188c2ecf20Sopenharmony_ci{
22198c2ecf20Sopenharmony_ci	u32 pcie_vers[NUM_PCIE_SERDES];
22208c2ecf20Sopenharmony_ci	u32 fabric_vers[NUM_FABRIC_SERDES];
22218c2ecf20Sopenharmony_ci	u32 sbus_vers;
22228c2ecf20Sopenharmony_ci	int i;
22238c2ecf20Sopenharmony_ci	int all_same;
22248c2ecf20Sopenharmony_ci	int ret;
22258c2ecf20Sopenharmony_ci	u8 rcv_addr;
22268c2ecf20Sopenharmony_ci
22278c2ecf20Sopenharmony_ci	ret = acquire_chip_resource(dd, CR_SBUS, SBUS_TIMEOUT);
22288c2ecf20Sopenharmony_ci	if (ret) {
22298c2ecf20Sopenharmony_ci		dd_dev_err(dd, "Unable to acquire SBus to read firmware versions\n");
22308c2ecf20Sopenharmony_ci		return;
22318c2ecf20Sopenharmony_ci	}
22328c2ecf20Sopenharmony_ci
22338c2ecf20Sopenharmony_ci	/* set fast mode */
22348c2ecf20Sopenharmony_ci	set_sbus_fast_mode(dd);
22358c2ecf20Sopenharmony_ci
22368c2ecf20Sopenharmony_ci	/* read version for SBus Master */
22378c2ecf20Sopenharmony_ci	sbus_request(dd, SBUS_MASTER_BROADCAST, 0x02, WRITE_SBUS_RECEIVER, 0);
22388c2ecf20Sopenharmony_ci	sbus_request(dd, SBUS_MASTER_BROADCAST, 0x07, WRITE_SBUS_RECEIVER, 0x1);
22398c2ecf20Sopenharmony_ci	/* wait for interrupt to be processed */
22408c2ecf20Sopenharmony_ci	usleep_range(10000, 11000);
22418c2ecf20Sopenharmony_ci	sbus_vers = sbus_read(dd, SBUS_MASTER_BROADCAST, 0x08, 0x1);
22428c2ecf20Sopenharmony_ci	dd_dev_info(dd, "SBus Master firmware version 0x%08x\n", sbus_vers);
22438c2ecf20Sopenharmony_ci
22448c2ecf20Sopenharmony_ci	/* read version for PCIe SerDes */
22458c2ecf20Sopenharmony_ci	all_same = 1;
22468c2ecf20Sopenharmony_ci	pcie_vers[0] = 0;
22478c2ecf20Sopenharmony_ci	for (i = 0; i < NUM_PCIE_SERDES; i++) {
22488c2ecf20Sopenharmony_ci		rcv_addr = pcie_serdes_addrs[dd->hfi1_id][i];
22498c2ecf20Sopenharmony_ci		sbus_request(dd, rcv_addr, 0x03, WRITE_SBUS_RECEIVER, 0);
22508c2ecf20Sopenharmony_ci		/* wait for interrupt to be processed */
22518c2ecf20Sopenharmony_ci		usleep_range(10000, 11000);
22528c2ecf20Sopenharmony_ci		pcie_vers[i] = sbus_read(dd, rcv_addr, 0x04, 0x0);
22538c2ecf20Sopenharmony_ci		if (i > 0 && pcie_vers[0] != pcie_vers[i])
22548c2ecf20Sopenharmony_ci			all_same = 0;
22558c2ecf20Sopenharmony_ci	}
22568c2ecf20Sopenharmony_ci
22578c2ecf20Sopenharmony_ci	if (all_same) {
22588c2ecf20Sopenharmony_ci		dd_dev_info(dd, "PCIe SerDes firmware version 0x%x\n",
22598c2ecf20Sopenharmony_ci			    pcie_vers[0]);
22608c2ecf20Sopenharmony_ci	} else {
22618c2ecf20Sopenharmony_ci		dd_dev_warn(dd, "PCIe SerDes do not have the same firmware version\n");
22628c2ecf20Sopenharmony_ci		for (i = 0; i < NUM_PCIE_SERDES; i++) {
22638c2ecf20Sopenharmony_ci			dd_dev_info(dd,
22648c2ecf20Sopenharmony_ci				    "PCIe SerDes lane %d firmware version 0x%x\n",
22658c2ecf20Sopenharmony_ci				    i, pcie_vers[i]);
22668c2ecf20Sopenharmony_ci		}
22678c2ecf20Sopenharmony_ci	}
22688c2ecf20Sopenharmony_ci
22698c2ecf20Sopenharmony_ci	/* read version for fabric SerDes */
22708c2ecf20Sopenharmony_ci	all_same = 1;
22718c2ecf20Sopenharmony_ci	fabric_vers[0] = 0;
22728c2ecf20Sopenharmony_ci	for (i = 0; i < NUM_FABRIC_SERDES; i++) {
22738c2ecf20Sopenharmony_ci		rcv_addr = fabric_serdes_addrs[dd->hfi1_id][i];
22748c2ecf20Sopenharmony_ci		sbus_request(dd, rcv_addr, 0x03, WRITE_SBUS_RECEIVER, 0);
22758c2ecf20Sopenharmony_ci		/* wait for interrupt to be processed */
22768c2ecf20Sopenharmony_ci		usleep_range(10000, 11000);
22778c2ecf20Sopenharmony_ci		fabric_vers[i] = sbus_read(dd, rcv_addr, 0x04, 0x0);
22788c2ecf20Sopenharmony_ci		if (i > 0 && fabric_vers[0] != fabric_vers[i])
22798c2ecf20Sopenharmony_ci			all_same = 0;
22808c2ecf20Sopenharmony_ci	}
22818c2ecf20Sopenharmony_ci
22828c2ecf20Sopenharmony_ci	if (all_same) {
22838c2ecf20Sopenharmony_ci		dd_dev_info(dd, "Fabric SerDes firmware version 0x%x\n",
22848c2ecf20Sopenharmony_ci			    fabric_vers[0]);
22858c2ecf20Sopenharmony_ci	} else {
22868c2ecf20Sopenharmony_ci		dd_dev_warn(dd, "Fabric SerDes do not have the same firmware version\n");
22878c2ecf20Sopenharmony_ci		for (i = 0; i < NUM_FABRIC_SERDES; i++) {
22888c2ecf20Sopenharmony_ci			dd_dev_info(dd,
22898c2ecf20Sopenharmony_ci				    "Fabric SerDes lane %d firmware version 0x%x\n",
22908c2ecf20Sopenharmony_ci				    i, fabric_vers[i]);
22918c2ecf20Sopenharmony_ci		}
22928c2ecf20Sopenharmony_ci	}
22938c2ecf20Sopenharmony_ci
22948c2ecf20Sopenharmony_ci	clear_sbus_fast_mode(dd);
22958c2ecf20Sopenharmony_ci	release_chip_resource(dd, CR_SBUS);
22968c2ecf20Sopenharmony_ci}
2297