162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright(c) 2015, 2016 Intel Corporation.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <linux/delay.h>
762306a36Sopenharmony_ci#include "hfi.h"
862306a36Sopenharmony_ci#include "common.h"
962306a36Sopenharmony_ci#include "eprom.h"
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci/*
1262306a36Sopenharmony_ci * The EPROM is logically divided into three partitions:
1362306a36Sopenharmony_ci *	partition 0: the first 128K, visible from PCI ROM BAR
1462306a36Sopenharmony_ci *	partition 1: 4K config file (sector size)
1562306a36Sopenharmony_ci *	partition 2: the rest
1662306a36Sopenharmony_ci */
1762306a36Sopenharmony_ci#define P0_SIZE (128 * 1024)
1862306a36Sopenharmony_ci#define P1_SIZE   (4 * 1024)
1962306a36Sopenharmony_ci#define P1_START P0_SIZE
2062306a36Sopenharmony_ci#define P2_START (P0_SIZE + P1_SIZE)
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci/* controller page size, in bytes */
2362306a36Sopenharmony_ci#define EP_PAGE_SIZE 256
2462306a36Sopenharmony_ci#define EP_PAGE_MASK (EP_PAGE_SIZE - 1)
2562306a36Sopenharmony_ci#define EP_PAGE_DWORDS (EP_PAGE_SIZE / sizeof(u32))
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci/* controller commands */
2862306a36Sopenharmony_ci#define CMD_SHIFT 24
2962306a36Sopenharmony_ci#define CMD_NOP			    (0)
3062306a36Sopenharmony_ci#define CMD_READ_DATA(addr)	    ((0x03 << CMD_SHIFT) | addr)
3162306a36Sopenharmony_ci#define CMD_RELEASE_POWERDOWN_NOID  ((0xab << CMD_SHIFT))
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci/* controller interface speeds */
3462306a36Sopenharmony_ci#define EP_SPEED_FULL 0x2	/* full speed */
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci/*
3762306a36Sopenharmony_ci * How long to wait for the EPROM to become available, in ms.
3862306a36Sopenharmony_ci * The spec 32 Mb EPROM takes around 40s to erase then write.
3962306a36Sopenharmony_ci * Double it for safety.
4062306a36Sopenharmony_ci */
4162306a36Sopenharmony_ci#define EPROM_TIMEOUT 80000 /* ms */
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci/*
4462306a36Sopenharmony_ci * Read a 256 byte (64 dword) EPROM page.
4562306a36Sopenharmony_ci * All callers have verified the offset is at a page boundary.
4662306a36Sopenharmony_ci */
4762306a36Sopenharmony_cistatic void read_page(struct hfi1_devdata *dd, u32 offset, u32 *result)
4862306a36Sopenharmony_ci{
4962306a36Sopenharmony_ci	int i;
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci	write_csr(dd, ASIC_EEP_ADDR_CMD, CMD_READ_DATA(offset));
5262306a36Sopenharmony_ci	for (i = 0; i < EP_PAGE_DWORDS; i++)
5362306a36Sopenharmony_ci		result[i] = (u32)read_csr(dd, ASIC_EEP_DATA);
5462306a36Sopenharmony_ci	write_csr(dd, ASIC_EEP_ADDR_CMD, CMD_NOP); /* close open page */
5562306a36Sopenharmony_ci}
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci/*
5862306a36Sopenharmony_ci * Read length bytes starting at offset from the start of the EPROM.
5962306a36Sopenharmony_ci */
6062306a36Sopenharmony_cistatic int read_length(struct hfi1_devdata *dd, u32 start, u32 len, void *dest)
6162306a36Sopenharmony_ci{
6262306a36Sopenharmony_ci	u32 buffer[EP_PAGE_DWORDS];
6362306a36Sopenharmony_ci	u32 end;
6462306a36Sopenharmony_ci	u32 start_offset;
6562306a36Sopenharmony_ci	u32 read_start;
6662306a36Sopenharmony_ci	u32 bytes;
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	if (len == 0)
6962306a36Sopenharmony_ci		return 0;
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci	end = start + len;
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci	/*
7462306a36Sopenharmony_ci	 * Make sure the read range is not outside of the controller read
7562306a36Sopenharmony_ci	 * command address range.  Note that '>' is correct below - the end
7662306a36Sopenharmony_ci	 * of the range is OK if it stops at the limit, but no higher.
7762306a36Sopenharmony_ci	 */
7862306a36Sopenharmony_ci	if (end > (1 << CMD_SHIFT))
7962306a36Sopenharmony_ci		return -EINVAL;
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	/* read the first partial page */
8262306a36Sopenharmony_ci	start_offset = start & EP_PAGE_MASK;
8362306a36Sopenharmony_ci	if (start_offset) {
8462306a36Sopenharmony_ci		/* partial starting page */
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci		/* align and read the page that contains the start */
8762306a36Sopenharmony_ci		read_start = start & ~EP_PAGE_MASK;
8862306a36Sopenharmony_ci		read_page(dd, read_start, buffer);
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci		/* the rest of the page is available data */
9162306a36Sopenharmony_ci		bytes = EP_PAGE_SIZE - start_offset;
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci		if (len <= bytes) {
9462306a36Sopenharmony_ci			/* end is within this page */
9562306a36Sopenharmony_ci			memcpy(dest, (u8 *)buffer + start_offset, len);
9662306a36Sopenharmony_ci			return 0;
9762306a36Sopenharmony_ci		}
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci		memcpy(dest, (u8 *)buffer + start_offset, bytes);
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci		start += bytes;
10262306a36Sopenharmony_ci		len -= bytes;
10362306a36Sopenharmony_ci		dest += bytes;
10462306a36Sopenharmony_ci	}
10562306a36Sopenharmony_ci	/* start is now page aligned */
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci	/* read whole pages */
10862306a36Sopenharmony_ci	while (len >= EP_PAGE_SIZE) {
10962306a36Sopenharmony_ci		read_page(dd, start, buffer);
11062306a36Sopenharmony_ci		memcpy(dest, buffer, EP_PAGE_SIZE);
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci		start += EP_PAGE_SIZE;
11362306a36Sopenharmony_ci		len -= EP_PAGE_SIZE;
11462306a36Sopenharmony_ci		dest += EP_PAGE_SIZE;
11562306a36Sopenharmony_ci	}
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	/* read the last partial page */
11862306a36Sopenharmony_ci	if (len) {
11962306a36Sopenharmony_ci		read_page(dd, start, buffer);
12062306a36Sopenharmony_ci		memcpy(dest, buffer, len);
12162306a36Sopenharmony_ci	}
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	return 0;
12462306a36Sopenharmony_ci}
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci/*
12762306a36Sopenharmony_ci * Initialize the EPROM handler.
12862306a36Sopenharmony_ci */
12962306a36Sopenharmony_ciint eprom_init(struct hfi1_devdata *dd)
13062306a36Sopenharmony_ci{
13162306a36Sopenharmony_ci	int ret = 0;
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci	/* only the discrete chip has an EPROM */
13462306a36Sopenharmony_ci	if (dd->pcidev->device != PCI_DEVICE_ID_INTEL0)
13562306a36Sopenharmony_ci		return 0;
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci	/*
13862306a36Sopenharmony_ci	 * It is OK if both HFIs reset the EPROM as long as they don't
13962306a36Sopenharmony_ci	 * do it at the same time.
14062306a36Sopenharmony_ci	 */
14162306a36Sopenharmony_ci	ret = acquire_chip_resource(dd, CR_EPROM, EPROM_TIMEOUT);
14262306a36Sopenharmony_ci	if (ret) {
14362306a36Sopenharmony_ci		dd_dev_err(dd,
14462306a36Sopenharmony_ci			   "%s: unable to acquire EPROM resource, no EPROM support\n",
14562306a36Sopenharmony_ci			   __func__);
14662306a36Sopenharmony_ci		goto done_asic;
14762306a36Sopenharmony_ci	}
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci	/* reset EPROM to be sure it is in a good state */
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci	/* set reset */
15262306a36Sopenharmony_ci	write_csr(dd, ASIC_EEP_CTL_STAT, ASIC_EEP_CTL_STAT_EP_RESET_SMASK);
15362306a36Sopenharmony_ci	/* clear reset, set speed */
15462306a36Sopenharmony_ci	write_csr(dd, ASIC_EEP_CTL_STAT,
15562306a36Sopenharmony_ci		  EP_SPEED_FULL << ASIC_EEP_CTL_STAT_RATE_SPI_SHIFT);
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci	/* wake the device with command "release powerdown NoID" */
15862306a36Sopenharmony_ci	write_csr(dd, ASIC_EEP_ADDR_CMD, CMD_RELEASE_POWERDOWN_NOID);
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci	dd->eprom_available = true;
16162306a36Sopenharmony_ci	release_chip_resource(dd, CR_EPROM);
16262306a36Sopenharmony_cidone_asic:
16362306a36Sopenharmony_ci	return ret;
16462306a36Sopenharmony_ci}
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci/* magic character sequence that begins an image */
16762306a36Sopenharmony_ci#define IMAGE_START_MAGIC "APO="
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci/* magic character sequence that might trail an image */
17062306a36Sopenharmony_ci#define IMAGE_TRAIL_MAGIC "egamiAPO"
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci/* EPROM file types */
17362306a36Sopenharmony_ci#define HFI1_EFT_PLATFORM_CONFIG 2
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci/* segment size - 128 KiB */
17662306a36Sopenharmony_ci#define SEG_SIZE (128 * 1024)
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_cistruct hfi1_eprom_footer {
17962306a36Sopenharmony_ci	u32 oprom_size;		/* size of the oprom, in bytes */
18062306a36Sopenharmony_ci	u16 num_table_entries;
18162306a36Sopenharmony_ci	u16 version;		/* version of this footer */
18262306a36Sopenharmony_ci	u32 magic;		/* must be last */
18362306a36Sopenharmony_ci};
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_cistruct hfi1_eprom_table_entry {
18662306a36Sopenharmony_ci	u32 type;		/* file type */
18762306a36Sopenharmony_ci	u32 offset;		/* file offset from start of EPROM */
18862306a36Sopenharmony_ci	u32 size;		/* file size, in bytes */
18962306a36Sopenharmony_ci};
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci/*
19262306a36Sopenharmony_ci * Calculate the max number of table entries that will fit within a directory
19362306a36Sopenharmony_ci * buffer of size 'dir_size'.
19462306a36Sopenharmony_ci */
19562306a36Sopenharmony_ci#define MAX_TABLE_ENTRIES(dir_size) \
19662306a36Sopenharmony_ci	(((dir_size) - sizeof(struct hfi1_eprom_footer)) / \
19762306a36Sopenharmony_ci		sizeof(struct hfi1_eprom_table_entry))
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci#define DIRECTORY_SIZE(n) (sizeof(struct hfi1_eprom_footer) + \
20062306a36Sopenharmony_ci	(sizeof(struct hfi1_eprom_table_entry) * (n)))
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci#define MAGIC4(a, b, c, d) ((d) << 24 | (c) << 16 | (b) << 8 | (a))
20362306a36Sopenharmony_ci#define FOOTER_MAGIC MAGIC4('e', 'p', 'r', 'm')
20462306a36Sopenharmony_ci#define FOOTER_VERSION 1
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci/*
20762306a36Sopenharmony_ci * Read all of partition 1.  The actual file is at the front.  Adjust
20862306a36Sopenharmony_ci * the returned size if a trailing image magic is found.
20962306a36Sopenharmony_ci */
21062306a36Sopenharmony_cistatic int read_partition_platform_config(struct hfi1_devdata *dd, void **data,
21162306a36Sopenharmony_ci					  u32 *size)
21262306a36Sopenharmony_ci{
21362306a36Sopenharmony_ci	void *buffer;
21462306a36Sopenharmony_ci	void *p;
21562306a36Sopenharmony_ci	u32 length;
21662306a36Sopenharmony_ci	int ret;
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci	buffer = kmalloc(P1_SIZE, GFP_KERNEL);
21962306a36Sopenharmony_ci	if (!buffer)
22062306a36Sopenharmony_ci		return -ENOMEM;
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci	ret = read_length(dd, P1_START, P1_SIZE, buffer);
22362306a36Sopenharmony_ci	if (ret) {
22462306a36Sopenharmony_ci		kfree(buffer);
22562306a36Sopenharmony_ci		return ret;
22662306a36Sopenharmony_ci	}
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci	/* config partition is valid only if it starts with IMAGE_START_MAGIC */
22962306a36Sopenharmony_ci	if (memcmp(buffer, IMAGE_START_MAGIC, strlen(IMAGE_START_MAGIC))) {
23062306a36Sopenharmony_ci		kfree(buffer);
23162306a36Sopenharmony_ci		return -ENOENT;
23262306a36Sopenharmony_ci	}
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci	/* scan for image magic that may trail the actual data */
23562306a36Sopenharmony_ci	p = strnstr(buffer, IMAGE_TRAIL_MAGIC, P1_SIZE);
23662306a36Sopenharmony_ci	if (p)
23762306a36Sopenharmony_ci		length = p - buffer;
23862306a36Sopenharmony_ci	else
23962306a36Sopenharmony_ci		length = P1_SIZE;
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci	*data = buffer;
24262306a36Sopenharmony_ci	*size = length;
24362306a36Sopenharmony_ci	return 0;
24462306a36Sopenharmony_ci}
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci/*
24762306a36Sopenharmony_ci * The segment magic has been checked.  There is a footer and table of
24862306a36Sopenharmony_ci * contents present.
24962306a36Sopenharmony_ci *
25062306a36Sopenharmony_ci * directory is a u32 aligned buffer of size EP_PAGE_SIZE.
25162306a36Sopenharmony_ci */
25262306a36Sopenharmony_cistatic int read_segment_platform_config(struct hfi1_devdata *dd,
25362306a36Sopenharmony_ci					void *directory, void **data, u32 *size)
25462306a36Sopenharmony_ci{
25562306a36Sopenharmony_ci	struct hfi1_eprom_footer *footer;
25662306a36Sopenharmony_ci	struct hfi1_eprom_table_entry *table;
25762306a36Sopenharmony_ci	struct hfi1_eprom_table_entry *entry;
25862306a36Sopenharmony_ci	void *buffer = NULL;
25962306a36Sopenharmony_ci	void *table_buffer = NULL;
26062306a36Sopenharmony_ci	int ret, i;
26162306a36Sopenharmony_ci	u32 directory_size;
26262306a36Sopenharmony_ci	u32 seg_base, seg_offset;
26362306a36Sopenharmony_ci	u32 bytes_available, ncopied, to_copy;
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci	/* the footer is at the end of the directory */
26662306a36Sopenharmony_ci	footer = (struct hfi1_eprom_footer *)
26762306a36Sopenharmony_ci			(directory + EP_PAGE_SIZE - sizeof(*footer));
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_ci	/* make sure the structure version is supported */
27062306a36Sopenharmony_ci	if (footer->version != FOOTER_VERSION)
27162306a36Sopenharmony_ci		return -EINVAL;
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci	/* oprom size cannot be larger than a segment */
27462306a36Sopenharmony_ci	if (footer->oprom_size >= SEG_SIZE)
27562306a36Sopenharmony_ci		return -EINVAL;
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_ci	/* the file table must fit in a segment with the oprom */
27862306a36Sopenharmony_ci	if (footer->num_table_entries >
27962306a36Sopenharmony_ci			MAX_TABLE_ENTRIES(SEG_SIZE - footer->oprom_size))
28062306a36Sopenharmony_ci		return -EINVAL;
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci	/* find the file table start, which precedes the footer */
28362306a36Sopenharmony_ci	directory_size = DIRECTORY_SIZE(footer->num_table_entries);
28462306a36Sopenharmony_ci	if (directory_size <= EP_PAGE_SIZE) {
28562306a36Sopenharmony_ci		/* the file table fits into the directory buffer handed in */
28662306a36Sopenharmony_ci		table = (struct hfi1_eprom_table_entry *)
28762306a36Sopenharmony_ci				(directory + EP_PAGE_SIZE - directory_size);
28862306a36Sopenharmony_ci	} else {
28962306a36Sopenharmony_ci		/* need to allocate and read more */
29062306a36Sopenharmony_ci		table_buffer = kmalloc(directory_size, GFP_KERNEL);
29162306a36Sopenharmony_ci		if (!table_buffer)
29262306a36Sopenharmony_ci			return -ENOMEM;
29362306a36Sopenharmony_ci		ret = read_length(dd, SEG_SIZE - directory_size,
29462306a36Sopenharmony_ci				  directory_size, table_buffer);
29562306a36Sopenharmony_ci		if (ret)
29662306a36Sopenharmony_ci			goto done;
29762306a36Sopenharmony_ci		table = table_buffer;
29862306a36Sopenharmony_ci	}
29962306a36Sopenharmony_ci
30062306a36Sopenharmony_ci	/* look for the platform configuration file in the table */
30162306a36Sopenharmony_ci	for (entry = NULL, i = 0; i < footer->num_table_entries; i++) {
30262306a36Sopenharmony_ci		if (table[i].type == HFI1_EFT_PLATFORM_CONFIG) {
30362306a36Sopenharmony_ci			entry = &table[i];
30462306a36Sopenharmony_ci			break;
30562306a36Sopenharmony_ci		}
30662306a36Sopenharmony_ci	}
30762306a36Sopenharmony_ci	if (!entry) {
30862306a36Sopenharmony_ci		ret = -ENOENT;
30962306a36Sopenharmony_ci		goto done;
31062306a36Sopenharmony_ci	}
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci	/*
31362306a36Sopenharmony_ci	 * Sanity check on the configuration file size - it should never
31462306a36Sopenharmony_ci	 * be larger than 4 KiB.
31562306a36Sopenharmony_ci	 */
31662306a36Sopenharmony_ci	if (entry->size > (4 * 1024)) {
31762306a36Sopenharmony_ci		dd_dev_err(dd, "Bad configuration file size 0x%x\n",
31862306a36Sopenharmony_ci			   entry->size);
31962306a36Sopenharmony_ci		ret = -EINVAL;
32062306a36Sopenharmony_ci		goto done;
32162306a36Sopenharmony_ci	}
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ci	/* check for bogus offset and size that wrap when added together */
32462306a36Sopenharmony_ci	if (entry->offset + entry->size < entry->offset) {
32562306a36Sopenharmony_ci		dd_dev_err(dd,
32662306a36Sopenharmony_ci			   "Bad configuration file start + size 0x%x+0x%x\n",
32762306a36Sopenharmony_ci			   entry->offset, entry->size);
32862306a36Sopenharmony_ci		ret = -EINVAL;
32962306a36Sopenharmony_ci		goto done;
33062306a36Sopenharmony_ci	}
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci	/* allocate the buffer to return */
33362306a36Sopenharmony_ci	buffer = kmalloc(entry->size, GFP_KERNEL);
33462306a36Sopenharmony_ci	if (!buffer) {
33562306a36Sopenharmony_ci		ret = -ENOMEM;
33662306a36Sopenharmony_ci		goto done;
33762306a36Sopenharmony_ci	}
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_ci	/*
34062306a36Sopenharmony_ci	 * Extract the file by looping over segments until it is fully read.
34162306a36Sopenharmony_ci	 */
34262306a36Sopenharmony_ci	seg_offset = entry->offset % SEG_SIZE;
34362306a36Sopenharmony_ci	seg_base = entry->offset - seg_offset;
34462306a36Sopenharmony_ci	ncopied = 0;
34562306a36Sopenharmony_ci	while (ncopied < entry->size) {
34662306a36Sopenharmony_ci		/* calculate data bytes available in this segment */
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ci		/* start with the bytes from the current offset to the end */
34962306a36Sopenharmony_ci		bytes_available = SEG_SIZE - seg_offset;
35062306a36Sopenharmony_ci		/* subtract off footer and table from segment 0 */
35162306a36Sopenharmony_ci		if (seg_base == 0) {
35262306a36Sopenharmony_ci			/*
35362306a36Sopenharmony_ci			 * Sanity check: should not have a starting point
35462306a36Sopenharmony_ci			 * at or within the directory.
35562306a36Sopenharmony_ci			 */
35662306a36Sopenharmony_ci			if (bytes_available <= directory_size) {
35762306a36Sopenharmony_ci				dd_dev_err(dd,
35862306a36Sopenharmony_ci					   "Bad configuration file - offset 0x%x within footer+table\n",
35962306a36Sopenharmony_ci					   entry->offset);
36062306a36Sopenharmony_ci				ret = -EINVAL;
36162306a36Sopenharmony_ci				goto done;
36262306a36Sopenharmony_ci			}
36362306a36Sopenharmony_ci			bytes_available -= directory_size;
36462306a36Sopenharmony_ci		}
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_ci		/* calculate bytes wanted */
36762306a36Sopenharmony_ci		to_copy = entry->size - ncopied;
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_ci		/* max out at the available bytes in this segment */
37062306a36Sopenharmony_ci		if (to_copy > bytes_available)
37162306a36Sopenharmony_ci			to_copy = bytes_available;
37262306a36Sopenharmony_ci
37362306a36Sopenharmony_ci		/*
37462306a36Sopenharmony_ci		 * Read from the EPROM.
37562306a36Sopenharmony_ci		 *
37662306a36Sopenharmony_ci		 * The sanity check for entry->offset is done in read_length().
37762306a36Sopenharmony_ci		 * The EPROM offset is validated against what the hardware
37862306a36Sopenharmony_ci		 * addressing supports.  In addition, if the offset is larger
37962306a36Sopenharmony_ci		 * than the actual EPROM, it silently wraps.  It will work
38062306a36Sopenharmony_ci		 * fine, though the reader may not get what they expected
38162306a36Sopenharmony_ci		 * from the EPROM.
38262306a36Sopenharmony_ci		 */
38362306a36Sopenharmony_ci		ret = read_length(dd, seg_base + seg_offset, to_copy,
38462306a36Sopenharmony_ci				  buffer + ncopied);
38562306a36Sopenharmony_ci		if (ret)
38662306a36Sopenharmony_ci			goto done;
38762306a36Sopenharmony_ci
38862306a36Sopenharmony_ci		ncopied += to_copy;
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci		/* set up for next segment */
39162306a36Sopenharmony_ci		seg_offset = footer->oprom_size;
39262306a36Sopenharmony_ci		seg_base += SEG_SIZE;
39362306a36Sopenharmony_ci	}
39462306a36Sopenharmony_ci
39562306a36Sopenharmony_ci	/* success */
39662306a36Sopenharmony_ci	ret = 0;
39762306a36Sopenharmony_ci	*data = buffer;
39862306a36Sopenharmony_ci	*size = entry->size;
39962306a36Sopenharmony_ci
40062306a36Sopenharmony_cidone:
40162306a36Sopenharmony_ci	kfree(table_buffer);
40262306a36Sopenharmony_ci	if (ret)
40362306a36Sopenharmony_ci		kfree(buffer);
40462306a36Sopenharmony_ci	return ret;
40562306a36Sopenharmony_ci}
40662306a36Sopenharmony_ci
40762306a36Sopenharmony_ci/*
40862306a36Sopenharmony_ci * Read the platform configuration file from the EPROM.
40962306a36Sopenharmony_ci *
41062306a36Sopenharmony_ci * On success, an allocated buffer containing the data and its size are
41162306a36Sopenharmony_ci * returned.  It is up to the caller to free this buffer.
41262306a36Sopenharmony_ci *
41362306a36Sopenharmony_ci * Return value:
41462306a36Sopenharmony_ci *   0	      - success
41562306a36Sopenharmony_ci *   -ENXIO   - no EPROM is available
41662306a36Sopenharmony_ci *   -EBUSY   - not able to acquire access to the EPROM
41762306a36Sopenharmony_ci *   -ENOENT  - no recognizable file written
41862306a36Sopenharmony_ci *   -ENOMEM  - buffer could not be allocated
41962306a36Sopenharmony_ci *   -EINVAL  - invalid EPROM contentents found
42062306a36Sopenharmony_ci */
42162306a36Sopenharmony_ciint eprom_read_platform_config(struct hfi1_devdata *dd, void **data, u32 *size)
42262306a36Sopenharmony_ci{
42362306a36Sopenharmony_ci	u32 directory[EP_PAGE_DWORDS]; /* aligned buffer */
42462306a36Sopenharmony_ci	int ret;
42562306a36Sopenharmony_ci
42662306a36Sopenharmony_ci	if (!dd->eprom_available)
42762306a36Sopenharmony_ci		return -ENXIO;
42862306a36Sopenharmony_ci
42962306a36Sopenharmony_ci	ret = acquire_chip_resource(dd, CR_EPROM, EPROM_TIMEOUT);
43062306a36Sopenharmony_ci	if (ret)
43162306a36Sopenharmony_ci		return -EBUSY;
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_ci	/* read the last page of the segment for the EPROM format magic */
43462306a36Sopenharmony_ci	ret = read_length(dd, SEG_SIZE - EP_PAGE_SIZE, EP_PAGE_SIZE, directory);
43562306a36Sopenharmony_ci	if (ret)
43662306a36Sopenharmony_ci		goto done;
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ci	/* last dword of the segment contains a magic value */
43962306a36Sopenharmony_ci	if (directory[EP_PAGE_DWORDS - 1] == FOOTER_MAGIC) {
44062306a36Sopenharmony_ci		/* segment format */
44162306a36Sopenharmony_ci		ret = read_segment_platform_config(dd, directory, data, size);
44262306a36Sopenharmony_ci	} else {
44362306a36Sopenharmony_ci		/* partition format */
44462306a36Sopenharmony_ci		ret = read_partition_platform_config(dd, data, size);
44562306a36Sopenharmony_ci	}
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_cidone:
44862306a36Sopenharmony_ci	release_chip_resource(dd, CR_EPROM);
44962306a36Sopenharmony_ci	return ret;
45062306a36Sopenharmony_ci}
451