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
48*4882a593Smuzhiyun #include <linux/ctype.h>
49*4882a593Smuzhiyun #include "efivar.h"
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun /* GUID for HFI1 variables in EFI */
52*4882a593Smuzhiyun #define HFI1_EFIVAR_GUID EFI_GUID(0xc50a953e, 0xa8b2, 0x42a6, \
53*4882a593Smuzhiyun 0xbf, 0x89, 0xd3, 0x33, 0xa6, 0xe9, 0xe6, 0xd4)
54*4882a593Smuzhiyun /* largest EFI data size we expect */
55*4882a593Smuzhiyun #define EFI_DATA_SIZE 4096
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun /*
58*4882a593Smuzhiyun * Read the named EFI variable. Return the size of the actual data in *size
59*4882a593Smuzhiyun * and a kmalloc'ed buffer in *return_data. The caller must free the
60*4882a593Smuzhiyun * data. It is guaranteed that *return_data will be NULL and *size = 0
61*4882a593Smuzhiyun * if this routine fails.
62*4882a593Smuzhiyun *
63*4882a593Smuzhiyun * Return 0 on success, -errno on failure.
64*4882a593Smuzhiyun */
read_efi_var(const char * name,unsigned long * size,void ** return_data)65*4882a593Smuzhiyun static int read_efi_var(const char *name, unsigned long *size,
66*4882a593Smuzhiyun void **return_data)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun efi_status_t status;
69*4882a593Smuzhiyun efi_char16_t *uni_name;
70*4882a593Smuzhiyun efi_guid_t guid;
71*4882a593Smuzhiyun unsigned long temp_size;
72*4882a593Smuzhiyun void *temp_buffer;
73*4882a593Smuzhiyun void *data;
74*4882a593Smuzhiyun int i;
75*4882a593Smuzhiyun int ret;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /* set failure return values */
78*4882a593Smuzhiyun *size = 0;
79*4882a593Smuzhiyun *return_data = NULL;
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE))
82*4882a593Smuzhiyun return -EOPNOTSUPP;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun uni_name = kcalloc(strlen(name) + 1, sizeof(efi_char16_t), GFP_KERNEL);
85*4882a593Smuzhiyun temp_buffer = kzalloc(EFI_DATA_SIZE, GFP_KERNEL);
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun if (!uni_name || !temp_buffer) {
88*4882a593Smuzhiyun ret = -ENOMEM;
89*4882a593Smuzhiyun goto fail;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun /* input: the size of the buffer */
93*4882a593Smuzhiyun temp_size = EFI_DATA_SIZE;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun /* convert ASCII to unicode - it is a 1:1 mapping */
96*4882a593Smuzhiyun for (i = 0; name[i]; i++)
97*4882a593Smuzhiyun uni_name[i] = name[i];
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun /* need a variable for our GUID */
100*4882a593Smuzhiyun guid = HFI1_EFIVAR_GUID;
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /* call into EFI runtime services */
103*4882a593Smuzhiyun status = efi.get_variable(
104*4882a593Smuzhiyun uni_name,
105*4882a593Smuzhiyun &guid,
106*4882a593Smuzhiyun NULL,
107*4882a593Smuzhiyun &temp_size,
108*4882a593Smuzhiyun temp_buffer);
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun /*
111*4882a593Smuzhiyun * It would be nice to call efi_status_to_err() here, but that
112*4882a593Smuzhiyun * is in the EFIVAR_FS code and may not be compiled in.
113*4882a593Smuzhiyun * However, even that is insufficient since it does not cover
114*4882a593Smuzhiyun * EFI_BUFFER_TOO_SMALL which could be an important return.
115*4882a593Smuzhiyun * For now, just split out succces or not found.
116*4882a593Smuzhiyun */
117*4882a593Smuzhiyun ret = status == EFI_SUCCESS ? 0 :
118*4882a593Smuzhiyun status == EFI_NOT_FOUND ? -ENOENT :
119*4882a593Smuzhiyun -EINVAL;
120*4882a593Smuzhiyun if (ret)
121*4882a593Smuzhiyun goto fail;
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun /*
124*4882a593Smuzhiyun * We have successfully read the EFI variable into our
125*4882a593Smuzhiyun * temporary buffer. Now allocate a correctly sized
126*4882a593Smuzhiyun * buffer.
127*4882a593Smuzhiyun */
128*4882a593Smuzhiyun data = kmemdup(temp_buffer, temp_size, GFP_KERNEL);
129*4882a593Smuzhiyun if (!data) {
130*4882a593Smuzhiyun ret = -ENOMEM;
131*4882a593Smuzhiyun goto fail;
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun *size = temp_size;
135*4882a593Smuzhiyun *return_data = data;
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun fail:
138*4882a593Smuzhiyun kfree(uni_name);
139*4882a593Smuzhiyun kfree(temp_buffer);
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun return ret;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun /*
145*4882a593Smuzhiyun * Read an HFI1 EFI variable of the form:
146*4882a593Smuzhiyun * <PCIe address>-<kind>
147*4882a593Smuzhiyun * Return an kalloc'ed array and size of the data.
148*4882a593Smuzhiyun *
149*4882a593Smuzhiyun * Returns 0 on success, -errno on failure.
150*4882a593Smuzhiyun */
read_hfi1_efi_var(struct hfi1_devdata * dd,const char * kind,unsigned long * size,void ** return_data)151*4882a593Smuzhiyun int read_hfi1_efi_var(struct hfi1_devdata *dd, const char *kind,
152*4882a593Smuzhiyun unsigned long *size, void **return_data)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun char prefix_name[64];
155*4882a593Smuzhiyun char name[64];
156*4882a593Smuzhiyun int result;
157*4882a593Smuzhiyun int i;
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun /* create a common prefix */
160*4882a593Smuzhiyun snprintf(prefix_name, sizeof(prefix_name), "%04x:%02x:%02x.%x",
161*4882a593Smuzhiyun pci_domain_nr(dd->pcidev->bus),
162*4882a593Smuzhiyun dd->pcidev->bus->number,
163*4882a593Smuzhiyun PCI_SLOT(dd->pcidev->devfn),
164*4882a593Smuzhiyun PCI_FUNC(dd->pcidev->devfn));
165*4882a593Smuzhiyun snprintf(name, sizeof(name), "%s-%s", prefix_name, kind);
166*4882a593Smuzhiyun result = read_efi_var(name, size, return_data);
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun /*
169*4882a593Smuzhiyun * If reading the lowercase EFI variable fail, read the uppercase
170*4882a593Smuzhiyun * variable.
171*4882a593Smuzhiyun */
172*4882a593Smuzhiyun if (result) {
173*4882a593Smuzhiyun /* Converting to uppercase */
174*4882a593Smuzhiyun for (i = 0; prefix_name[i]; i++)
175*4882a593Smuzhiyun if (isalpha(prefix_name[i]))
176*4882a593Smuzhiyun prefix_name[i] = toupper(prefix_name[i]);
177*4882a593Smuzhiyun snprintf(name, sizeof(name), "%s-%s", prefix_name, kind);
178*4882a593Smuzhiyun result = read_efi_var(name, size, return_data);
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun return result;
182*4882a593Smuzhiyun }
183