1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright(c) 2015, 2016 Intel Corporation.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * This file is provided under a dual BSD/GPLv2 license. When using or
5*4882a593Smuzhiyun * redistributing this file, you may do so under either license.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * GPL LICENSE SUMMARY
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify
10*4882a593Smuzhiyun * it under the terms of version 2 of the GNU General Public License as
11*4882a593Smuzhiyun * published by the Free Software Foundation.
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * This program is distributed in the hope that it will be useful, but
14*4882a593Smuzhiyun * WITHOUT ANY WARRANTY; without even the implied warranty of
15*4882a593Smuzhiyun * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16*4882a593Smuzhiyun * General Public License for more details.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * BSD LICENSE
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or without
21*4882a593Smuzhiyun * modification, are permitted provided that the following conditions
22*4882a593Smuzhiyun * are met:
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * - Redistributions of source code must retain the above copyright
25*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer.
26*4882a593Smuzhiyun * - Redistributions in binary form must reproduce the above copyright
27*4882a593Smuzhiyun * notice, this list of conditions and the following disclaimer in
28*4882a593Smuzhiyun * the documentation and/or other materials provided with the
29*4882a593Smuzhiyun * distribution.
30*4882a593Smuzhiyun * - Neither the name of Intel Corporation nor the names of its
31*4882a593Smuzhiyun * contributors may be used to endorse or promote products derived
32*4882a593Smuzhiyun * from this software without specific prior written permission.
33*4882a593Smuzhiyun *
34*4882a593Smuzhiyun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35*4882a593Smuzhiyun * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36*4882a593Smuzhiyun * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37*4882a593Smuzhiyun * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38*4882a593Smuzhiyun * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39*4882a593Smuzhiyun * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40*4882a593Smuzhiyun * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41*4882a593Smuzhiyun * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42*4882a593Smuzhiyun * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43*4882a593Smuzhiyun * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44*4882a593Smuzhiyun * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45*4882a593Smuzhiyun *
46*4882a593Smuzhiyun */
47*4882a593Smuzhiyun #include <linux/delay.h>
48*4882a593Smuzhiyun #include "hfi.h"
49*4882a593Smuzhiyun #include "common.h"
50*4882a593Smuzhiyun #include "eprom.h"
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun /*
53*4882a593Smuzhiyun * The EPROM is logically divided into three partitions:
54*4882a593Smuzhiyun * partition 0: the first 128K, visible from PCI ROM BAR
55*4882a593Smuzhiyun * partition 1: 4K config file (sector size)
56*4882a593Smuzhiyun * partition 2: the rest
57*4882a593Smuzhiyun */
58*4882a593Smuzhiyun #define P0_SIZE (128 * 1024)
59*4882a593Smuzhiyun #define P1_SIZE (4 * 1024)
60*4882a593Smuzhiyun #define P1_START P0_SIZE
61*4882a593Smuzhiyun #define P2_START (P0_SIZE + P1_SIZE)
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun /* controller page size, in bytes */
64*4882a593Smuzhiyun #define EP_PAGE_SIZE 256
65*4882a593Smuzhiyun #define EP_PAGE_MASK (EP_PAGE_SIZE - 1)
66*4882a593Smuzhiyun #define EP_PAGE_DWORDS (EP_PAGE_SIZE / sizeof(u32))
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /* controller commands */
69*4882a593Smuzhiyun #define CMD_SHIFT 24
70*4882a593Smuzhiyun #define CMD_NOP (0)
71*4882a593Smuzhiyun #define CMD_READ_DATA(addr) ((0x03 << CMD_SHIFT) | addr)
72*4882a593Smuzhiyun #define CMD_RELEASE_POWERDOWN_NOID ((0xab << CMD_SHIFT))
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /* controller interface speeds */
75*4882a593Smuzhiyun #define EP_SPEED_FULL 0x2 /* full speed */
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /*
78*4882a593Smuzhiyun * How long to wait for the EPROM to become available, in ms.
79*4882a593Smuzhiyun * The spec 32 Mb EPROM takes around 40s to erase then write.
80*4882a593Smuzhiyun * Double it for safety.
81*4882a593Smuzhiyun */
82*4882a593Smuzhiyun #define EPROM_TIMEOUT 80000 /* ms */
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun /*
85*4882a593Smuzhiyun * Read a 256 byte (64 dword) EPROM page.
86*4882a593Smuzhiyun * All callers have verified the offset is at a page boundary.
87*4882a593Smuzhiyun */
read_page(struct hfi1_devdata * dd,u32 offset,u32 * result)88*4882a593Smuzhiyun static void read_page(struct hfi1_devdata *dd, u32 offset, u32 *result)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun int i;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun write_csr(dd, ASIC_EEP_ADDR_CMD, CMD_READ_DATA(offset));
93*4882a593Smuzhiyun for (i = 0; i < EP_PAGE_DWORDS; i++)
94*4882a593Smuzhiyun result[i] = (u32)read_csr(dd, ASIC_EEP_DATA);
95*4882a593Smuzhiyun write_csr(dd, ASIC_EEP_ADDR_CMD, CMD_NOP); /* close open page */
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /*
99*4882a593Smuzhiyun * Read length bytes starting at offset from the start of the EPROM.
100*4882a593Smuzhiyun */
read_length(struct hfi1_devdata * dd,u32 start,u32 len,void * dest)101*4882a593Smuzhiyun static int read_length(struct hfi1_devdata *dd, u32 start, u32 len, void *dest)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun u32 buffer[EP_PAGE_DWORDS];
104*4882a593Smuzhiyun u32 end;
105*4882a593Smuzhiyun u32 start_offset;
106*4882a593Smuzhiyun u32 read_start;
107*4882a593Smuzhiyun u32 bytes;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun if (len == 0)
110*4882a593Smuzhiyun return 0;
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun end = start + len;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /*
115*4882a593Smuzhiyun * Make sure the read range is not outside of the controller read
116*4882a593Smuzhiyun * command address range. Note that '>' is correct below - the end
117*4882a593Smuzhiyun * of the range is OK if it stops at the limit, but no higher.
118*4882a593Smuzhiyun */
119*4882a593Smuzhiyun if (end > (1 << CMD_SHIFT))
120*4882a593Smuzhiyun return -EINVAL;
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun /* read the first partial page */
123*4882a593Smuzhiyun start_offset = start & EP_PAGE_MASK;
124*4882a593Smuzhiyun if (start_offset) {
125*4882a593Smuzhiyun /* partial starting page */
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /* align and read the page that contains the start */
128*4882a593Smuzhiyun read_start = start & ~EP_PAGE_MASK;
129*4882a593Smuzhiyun read_page(dd, read_start, buffer);
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun /* the rest of the page is available data */
132*4882a593Smuzhiyun bytes = EP_PAGE_SIZE - start_offset;
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun if (len <= bytes) {
135*4882a593Smuzhiyun /* end is within this page */
136*4882a593Smuzhiyun memcpy(dest, (u8 *)buffer + start_offset, len);
137*4882a593Smuzhiyun return 0;
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun memcpy(dest, (u8 *)buffer + start_offset, bytes);
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun start += bytes;
143*4882a593Smuzhiyun len -= bytes;
144*4882a593Smuzhiyun dest += bytes;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun /* start is now page aligned */
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun /* read whole pages */
149*4882a593Smuzhiyun while (len >= EP_PAGE_SIZE) {
150*4882a593Smuzhiyun read_page(dd, start, buffer);
151*4882a593Smuzhiyun memcpy(dest, buffer, EP_PAGE_SIZE);
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun start += EP_PAGE_SIZE;
154*4882a593Smuzhiyun len -= EP_PAGE_SIZE;
155*4882a593Smuzhiyun dest += EP_PAGE_SIZE;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun /* read the last partial page */
159*4882a593Smuzhiyun if (len) {
160*4882a593Smuzhiyun read_page(dd, start, buffer);
161*4882a593Smuzhiyun memcpy(dest, buffer, len);
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun return 0;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun /*
168*4882a593Smuzhiyun * Initialize the EPROM handler.
169*4882a593Smuzhiyun */
eprom_init(struct hfi1_devdata * dd)170*4882a593Smuzhiyun int eprom_init(struct hfi1_devdata *dd)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun int ret = 0;
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun /* only the discrete chip has an EPROM */
175*4882a593Smuzhiyun if (dd->pcidev->device != PCI_DEVICE_ID_INTEL0)
176*4882a593Smuzhiyun return 0;
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun /*
179*4882a593Smuzhiyun * It is OK if both HFIs reset the EPROM as long as they don't
180*4882a593Smuzhiyun * do it at the same time.
181*4882a593Smuzhiyun */
182*4882a593Smuzhiyun ret = acquire_chip_resource(dd, CR_EPROM, EPROM_TIMEOUT);
183*4882a593Smuzhiyun if (ret) {
184*4882a593Smuzhiyun dd_dev_err(dd,
185*4882a593Smuzhiyun "%s: unable to acquire EPROM resource, no EPROM support\n",
186*4882a593Smuzhiyun __func__);
187*4882a593Smuzhiyun goto done_asic;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun /* reset EPROM to be sure it is in a good state */
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun /* set reset */
193*4882a593Smuzhiyun write_csr(dd, ASIC_EEP_CTL_STAT, ASIC_EEP_CTL_STAT_EP_RESET_SMASK);
194*4882a593Smuzhiyun /* clear reset, set speed */
195*4882a593Smuzhiyun write_csr(dd, ASIC_EEP_CTL_STAT,
196*4882a593Smuzhiyun EP_SPEED_FULL << ASIC_EEP_CTL_STAT_RATE_SPI_SHIFT);
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun /* wake the device with command "release powerdown NoID" */
199*4882a593Smuzhiyun write_csr(dd, ASIC_EEP_ADDR_CMD, CMD_RELEASE_POWERDOWN_NOID);
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun dd->eprom_available = true;
202*4882a593Smuzhiyun release_chip_resource(dd, CR_EPROM);
203*4882a593Smuzhiyun done_asic:
204*4882a593Smuzhiyun return ret;
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun /* magic character sequence that begins an image */
208*4882a593Smuzhiyun #define IMAGE_START_MAGIC "APO="
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun /* magic character sequence that might trail an image */
211*4882a593Smuzhiyun #define IMAGE_TRAIL_MAGIC "egamiAPO"
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun /* EPROM file types */
214*4882a593Smuzhiyun #define HFI1_EFT_PLATFORM_CONFIG 2
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun /* segment size - 128 KiB */
217*4882a593Smuzhiyun #define SEG_SIZE (128 * 1024)
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun struct hfi1_eprom_footer {
220*4882a593Smuzhiyun u32 oprom_size; /* size of the oprom, in bytes */
221*4882a593Smuzhiyun u16 num_table_entries;
222*4882a593Smuzhiyun u16 version; /* version of this footer */
223*4882a593Smuzhiyun u32 magic; /* must be last */
224*4882a593Smuzhiyun };
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun struct hfi1_eprom_table_entry {
227*4882a593Smuzhiyun u32 type; /* file type */
228*4882a593Smuzhiyun u32 offset; /* file offset from start of EPROM */
229*4882a593Smuzhiyun u32 size; /* file size, in bytes */
230*4882a593Smuzhiyun };
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun /*
233*4882a593Smuzhiyun * Calculate the max number of table entries that will fit within a directory
234*4882a593Smuzhiyun * buffer of size 'dir_size'.
235*4882a593Smuzhiyun */
236*4882a593Smuzhiyun #define MAX_TABLE_ENTRIES(dir_size) \
237*4882a593Smuzhiyun (((dir_size) - sizeof(struct hfi1_eprom_footer)) / \
238*4882a593Smuzhiyun sizeof(struct hfi1_eprom_table_entry))
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun #define DIRECTORY_SIZE(n) (sizeof(struct hfi1_eprom_footer) + \
241*4882a593Smuzhiyun (sizeof(struct hfi1_eprom_table_entry) * (n)))
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun #define MAGIC4(a, b, c, d) ((d) << 24 | (c) << 16 | (b) << 8 | (a))
244*4882a593Smuzhiyun #define FOOTER_MAGIC MAGIC4('e', 'p', 'r', 'm')
245*4882a593Smuzhiyun #define FOOTER_VERSION 1
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun /*
248*4882a593Smuzhiyun * Read all of partition 1. The actual file is at the front. Adjust
249*4882a593Smuzhiyun * the returned size if a trailing image magic is found.
250*4882a593Smuzhiyun */
read_partition_platform_config(struct hfi1_devdata * dd,void ** data,u32 * size)251*4882a593Smuzhiyun static int read_partition_platform_config(struct hfi1_devdata *dd, void **data,
252*4882a593Smuzhiyun u32 *size)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun void *buffer;
255*4882a593Smuzhiyun void *p;
256*4882a593Smuzhiyun u32 length;
257*4882a593Smuzhiyun int ret;
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun buffer = kmalloc(P1_SIZE, GFP_KERNEL);
260*4882a593Smuzhiyun if (!buffer)
261*4882a593Smuzhiyun return -ENOMEM;
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun ret = read_length(dd, P1_START, P1_SIZE, buffer);
264*4882a593Smuzhiyun if (ret) {
265*4882a593Smuzhiyun kfree(buffer);
266*4882a593Smuzhiyun return ret;
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun /* config partition is valid only if it starts with IMAGE_START_MAGIC */
270*4882a593Smuzhiyun if (memcmp(buffer, IMAGE_START_MAGIC, strlen(IMAGE_START_MAGIC))) {
271*4882a593Smuzhiyun kfree(buffer);
272*4882a593Smuzhiyun return -ENOENT;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun /* scan for image magic that may trail the actual data */
276*4882a593Smuzhiyun p = strnstr(buffer, IMAGE_TRAIL_MAGIC, P1_SIZE);
277*4882a593Smuzhiyun if (p)
278*4882a593Smuzhiyun length = p - buffer;
279*4882a593Smuzhiyun else
280*4882a593Smuzhiyun length = P1_SIZE;
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun *data = buffer;
283*4882a593Smuzhiyun *size = length;
284*4882a593Smuzhiyun return 0;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun /*
288*4882a593Smuzhiyun * The segment magic has been checked. There is a footer and table of
289*4882a593Smuzhiyun * contents present.
290*4882a593Smuzhiyun *
291*4882a593Smuzhiyun * directory is a u32 aligned buffer of size EP_PAGE_SIZE.
292*4882a593Smuzhiyun */
read_segment_platform_config(struct hfi1_devdata * dd,void * directory,void ** data,u32 * size)293*4882a593Smuzhiyun static int read_segment_platform_config(struct hfi1_devdata *dd,
294*4882a593Smuzhiyun void *directory, void **data, u32 *size)
295*4882a593Smuzhiyun {
296*4882a593Smuzhiyun struct hfi1_eprom_footer *footer;
297*4882a593Smuzhiyun struct hfi1_eprom_table_entry *table;
298*4882a593Smuzhiyun struct hfi1_eprom_table_entry *entry;
299*4882a593Smuzhiyun void *buffer = NULL;
300*4882a593Smuzhiyun void *table_buffer = NULL;
301*4882a593Smuzhiyun int ret, i;
302*4882a593Smuzhiyun u32 directory_size;
303*4882a593Smuzhiyun u32 seg_base, seg_offset;
304*4882a593Smuzhiyun u32 bytes_available, ncopied, to_copy;
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun /* the footer is at the end of the directory */
307*4882a593Smuzhiyun footer = (struct hfi1_eprom_footer *)
308*4882a593Smuzhiyun (directory + EP_PAGE_SIZE - sizeof(*footer));
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun /* make sure the structure version is supported */
311*4882a593Smuzhiyun if (footer->version != FOOTER_VERSION)
312*4882a593Smuzhiyun return -EINVAL;
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun /* oprom size cannot be larger than a segment */
315*4882a593Smuzhiyun if (footer->oprom_size >= SEG_SIZE)
316*4882a593Smuzhiyun return -EINVAL;
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun /* the file table must fit in a segment with the oprom */
319*4882a593Smuzhiyun if (footer->num_table_entries >
320*4882a593Smuzhiyun MAX_TABLE_ENTRIES(SEG_SIZE - footer->oprom_size))
321*4882a593Smuzhiyun return -EINVAL;
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun /* find the file table start, which precedes the footer */
324*4882a593Smuzhiyun directory_size = DIRECTORY_SIZE(footer->num_table_entries);
325*4882a593Smuzhiyun if (directory_size <= EP_PAGE_SIZE) {
326*4882a593Smuzhiyun /* the file table fits into the directory buffer handed in */
327*4882a593Smuzhiyun table = (struct hfi1_eprom_table_entry *)
328*4882a593Smuzhiyun (directory + EP_PAGE_SIZE - directory_size);
329*4882a593Smuzhiyun } else {
330*4882a593Smuzhiyun /* need to allocate and read more */
331*4882a593Smuzhiyun table_buffer = kmalloc(directory_size, GFP_KERNEL);
332*4882a593Smuzhiyun if (!table_buffer)
333*4882a593Smuzhiyun return -ENOMEM;
334*4882a593Smuzhiyun ret = read_length(dd, SEG_SIZE - directory_size,
335*4882a593Smuzhiyun directory_size, table_buffer);
336*4882a593Smuzhiyun if (ret)
337*4882a593Smuzhiyun goto done;
338*4882a593Smuzhiyun table = table_buffer;
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun /* look for the platform configuration file in the table */
342*4882a593Smuzhiyun for (entry = NULL, i = 0; i < footer->num_table_entries; i++) {
343*4882a593Smuzhiyun if (table[i].type == HFI1_EFT_PLATFORM_CONFIG) {
344*4882a593Smuzhiyun entry = &table[i];
345*4882a593Smuzhiyun break;
346*4882a593Smuzhiyun }
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun if (!entry) {
349*4882a593Smuzhiyun ret = -ENOENT;
350*4882a593Smuzhiyun goto done;
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun /*
354*4882a593Smuzhiyun * Sanity check on the configuration file size - it should never
355*4882a593Smuzhiyun * be larger than 4 KiB.
356*4882a593Smuzhiyun */
357*4882a593Smuzhiyun if (entry->size > (4 * 1024)) {
358*4882a593Smuzhiyun dd_dev_err(dd, "Bad configuration file size 0x%x\n",
359*4882a593Smuzhiyun entry->size);
360*4882a593Smuzhiyun ret = -EINVAL;
361*4882a593Smuzhiyun goto done;
362*4882a593Smuzhiyun }
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun /* check for bogus offset and size that wrap when added together */
365*4882a593Smuzhiyun if (entry->offset + entry->size < entry->offset) {
366*4882a593Smuzhiyun dd_dev_err(dd,
367*4882a593Smuzhiyun "Bad configuration file start + size 0x%x+0x%x\n",
368*4882a593Smuzhiyun entry->offset, entry->size);
369*4882a593Smuzhiyun ret = -EINVAL;
370*4882a593Smuzhiyun goto done;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun /* allocate the buffer to return */
374*4882a593Smuzhiyun buffer = kmalloc(entry->size, GFP_KERNEL);
375*4882a593Smuzhiyun if (!buffer) {
376*4882a593Smuzhiyun ret = -ENOMEM;
377*4882a593Smuzhiyun goto done;
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun /*
381*4882a593Smuzhiyun * Extract the file by looping over segments until it is fully read.
382*4882a593Smuzhiyun */
383*4882a593Smuzhiyun seg_offset = entry->offset % SEG_SIZE;
384*4882a593Smuzhiyun seg_base = entry->offset - seg_offset;
385*4882a593Smuzhiyun ncopied = 0;
386*4882a593Smuzhiyun while (ncopied < entry->size) {
387*4882a593Smuzhiyun /* calculate data bytes available in this segment */
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun /* start with the bytes from the current offset to the end */
390*4882a593Smuzhiyun bytes_available = SEG_SIZE - seg_offset;
391*4882a593Smuzhiyun /* subtract off footer and table from segment 0 */
392*4882a593Smuzhiyun if (seg_base == 0) {
393*4882a593Smuzhiyun /*
394*4882a593Smuzhiyun * Sanity check: should not have a starting point
395*4882a593Smuzhiyun * at or within the directory.
396*4882a593Smuzhiyun */
397*4882a593Smuzhiyun if (bytes_available <= directory_size) {
398*4882a593Smuzhiyun dd_dev_err(dd,
399*4882a593Smuzhiyun "Bad configuration file - offset 0x%x within footer+table\n",
400*4882a593Smuzhiyun entry->offset);
401*4882a593Smuzhiyun ret = -EINVAL;
402*4882a593Smuzhiyun goto done;
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun bytes_available -= directory_size;
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun /* calculate bytes wanted */
408*4882a593Smuzhiyun to_copy = entry->size - ncopied;
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun /* max out at the available bytes in this segment */
411*4882a593Smuzhiyun if (to_copy > bytes_available)
412*4882a593Smuzhiyun to_copy = bytes_available;
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun /*
415*4882a593Smuzhiyun * Read from the EPROM.
416*4882a593Smuzhiyun *
417*4882a593Smuzhiyun * The sanity check for entry->offset is done in read_length().
418*4882a593Smuzhiyun * The EPROM offset is validated against what the hardware
419*4882a593Smuzhiyun * addressing supports. In addition, if the offset is larger
420*4882a593Smuzhiyun * than the actual EPROM, it silently wraps. It will work
421*4882a593Smuzhiyun * fine, though the reader may not get what they expected
422*4882a593Smuzhiyun * from the EPROM.
423*4882a593Smuzhiyun */
424*4882a593Smuzhiyun ret = read_length(dd, seg_base + seg_offset, to_copy,
425*4882a593Smuzhiyun buffer + ncopied);
426*4882a593Smuzhiyun if (ret)
427*4882a593Smuzhiyun goto done;
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun ncopied += to_copy;
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun /* set up for next segment */
432*4882a593Smuzhiyun seg_offset = footer->oprom_size;
433*4882a593Smuzhiyun seg_base += SEG_SIZE;
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun /* success */
437*4882a593Smuzhiyun ret = 0;
438*4882a593Smuzhiyun *data = buffer;
439*4882a593Smuzhiyun *size = entry->size;
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun done:
442*4882a593Smuzhiyun kfree(table_buffer);
443*4882a593Smuzhiyun if (ret)
444*4882a593Smuzhiyun kfree(buffer);
445*4882a593Smuzhiyun return ret;
446*4882a593Smuzhiyun }
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun /*
449*4882a593Smuzhiyun * Read the platform configuration file from the EPROM.
450*4882a593Smuzhiyun *
451*4882a593Smuzhiyun * On success, an allocated buffer containing the data and its size are
452*4882a593Smuzhiyun * returned. It is up to the caller to free this buffer.
453*4882a593Smuzhiyun *
454*4882a593Smuzhiyun * Return value:
455*4882a593Smuzhiyun * 0 - success
456*4882a593Smuzhiyun * -ENXIO - no EPROM is available
457*4882a593Smuzhiyun * -EBUSY - not able to acquire access to the EPROM
458*4882a593Smuzhiyun * -ENOENT - no recognizable file written
459*4882a593Smuzhiyun * -ENOMEM - buffer could not be allocated
460*4882a593Smuzhiyun * -EINVAL - invalid EPROM contentents found
461*4882a593Smuzhiyun */
eprom_read_platform_config(struct hfi1_devdata * dd,void ** data,u32 * size)462*4882a593Smuzhiyun int eprom_read_platform_config(struct hfi1_devdata *dd, void **data, u32 *size)
463*4882a593Smuzhiyun {
464*4882a593Smuzhiyun u32 directory[EP_PAGE_DWORDS]; /* aligned buffer */
465*4882a593Smuzhiyun int ret;
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun if (!dd->eprom_available)
468*4882a593Smuzhiyun return -ENXIO;
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun ret = acquire_chip_resource(dd, CR_EPROM, EPROM_TIMEOUT);
471*4882a593Smuzhiyun if (ret)
472*4882a593Smuzhiyun return -EBUSY;
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun /* read the last page of the segment for the EPROM format magic */
475*4882a593Smuzhiyun ret = read_length(dd, SEG_SIZE - EP_PAGE_SIZE, EP_PAGE_SIZE, directory);
476*4882a593Smuzhiyun if (ret)
477*4882a593Smuzhiyun goto done;
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun /* last dword of the segment contains a magic value */
480*4882a593Smuzhiyun if (directory[EP_PAGE_DWORDS - 1] == FOOTER_MAGIC) {
481*4882a593Smuzhiyun /* segment format */
482*4882a593Smuzhiyun ret = read_segment_platform_config(dd, directory, data, size);
483*4882a593Smuzhiyun } else {
484*4882a593Smuzhiyun /* partition format */
485*4882a593Smuzhiyun ret = read_partition_platform_config(dd, data, size);
486*4882a593Smuzhiyun }
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun done:
489*4882a593Smuzhiyun release_chip_resource(dd, CR_EPROM);
490*4882a593Smuzhiyun return ret;
491*4882a593Smuzhiyun }
492