18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright(c) 2015, 2016 Intel Corporation. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * This file is provided under a dual BSD/GPLv2 license. When using or 58c2ecf20Sopenharmony_ci * redistributing this file, you may do so under either license. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * GPL LICENSE SUMMARY 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify 108c2ecf20Sopenharmony_ci * it under the terms of version 2 of the GNU General Public License as 118c2ecf20Sopenharmony_ci * published by the Free Software Foundation. 128c2ecf20Sopenharmony_ci * 138c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful, but 148c2ecf20Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of 158c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 168c2ecf20Sopenharmony_ci * General Public License for more details. 178c2ecf20Sopenharmony_ci * 188c2ecf20Sopenharmony_ci * BSD LICENSE 198c2ecf20Sopenharmony_ci * 208c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without 218c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions 228c2ecf20Sopenharmony_ci * are met: 238c2ecf20Sopenharmony_ci * 248c2ecf20Sopenharmony_ci * - Redistributions of source code must retain the above copyright 258c2ecf20Sopenharmony_ci * notice, this list of conditions and the following disclaimer. 268c2ecf20Sopenharmony_ci * - Redistributions in binary form must reproduce the above copyright 278c2ecf20Sopenharmony_ci * notice, this list of conditions and the following disclaimer in 288c2ecf20Sopenharmony_ci * the documentation and/or other materials provided with the 298c2ecf20Sopenharmony_ci * distribution. 308c2ecf20Sopenharmony_ci * - Neither the name of Intel Corporation nor the names of its 318c2ecf20Sopenharmony_ci * contributors may be used to endorse or promote products derived 328c2ecf20Sopenharmony_ci * from this software without specific prior written permission. 338c2ecf20Sopenharmony_ci * 348c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 358c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 368c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 378c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 388c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 398c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 408c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 418c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 428c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 438c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 448c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 458c2ecf20Sopenharmony_ci * 468c2ecf20Sopenharmony_ci */ 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci#include <linux/ctype.h> 498c2ecf20Sopenharmony_ci#include "efivar.h" 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci/* GUID for HFI1 variables in EFI */ 528c2ecf20Sopenharmony_ci#define HFI1_EFIVAR_GUID EFI_GUID(0xc50a953e, 0xa8b2, 0x42a6, \ 538c2ecf20Sopenharmony_ci 0xbf, 0x89, 0xd3, 0x33, 0xa6, 0xe9, 0xe6, 0xd4) 548c2ecf20Sopenharmony_ci/* largest EFI data size we expect */ 558c2ecf20Sopenharmony_ci#define EFI_DATA_SIZE 4096 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci/* 588c2ecf20Sopenharmony_ci * Read the named EFI variable. Return the size of the actual data in *size 598c2ecf20Sopenharmony_ci * and a kmalloc'ed buffer in *return_data. The caller must free the 608c2ecf20Sopenharmony_ci * data. It is guaranteed that *return_data will be NULL and *size = 0 618c2ecf20Sopenharmony_ci * if this routine fails. 628c2ecf20Sopenharmony_ci * 638c2ecf20Sopenharmony_ci * Return 0 on success, -errno on failure. 648c2ecf20Sopenharmony_ci */ 658c2ecf20Sopenharmony_cistatic int read_efi_var(const char *name, unsigned long *size, 668c2ecf20Sopenharmony_ci void **return_data) 678c2ecf20Sopenharmony_ci{ 688c2ecf20Sopenharmony_ci efi_status_t status; 698c2ecf20Sopenharmony_ci efi_char16_t *uni_name; 708c2ecf20Sopenharmony_ci efi_guid_t guid; 718c2ecf20Sopenharmony_ci unsigned long temp_size; 728c2ecf20Sopenharmony_ci void *temp_buffer; 738c2ecf20Sopenharmony_ci void *data; 748c2ecf20Sopenharmony_ci int i; 758c2ecf20Sopenharmony_ci int ret; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci /* set failure return values */ 788c2ecf20Sopenharmony_ci *size = 0; 798c2ecf20Sopenharmony_ci *return_data = NULL; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE)) 828c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci uni_name = kcalloc(strlen(name) + 1, sizeof(efi_char16_t), GFP_KERNEL); 858c2ecf20Sopenharmony_ci temp_buffer = kzalloc(EFI_DATA_SIZE, GFP_KERNEL); 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci if (!uni_name || !temp_buffer) { 888c2ecf20Sopenharmony_ci ret = -ENOMEM; 898c2ecf20Sopenharmony_ci goto fail; 908c2ecf20Sopenharmony_ci } 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci /* input: the size of the buffer */ 938c2ecf20Sopenharmony_ci temp_size = EFI_DATA_SIZE; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci /* convert ASCII to unicode - it is a 1:1 mapping */ 968c2ecf20Sopenharmony_ci for (i = 0; name[i]; i++) 978c2ecf20Sopenharmony_ci uni_name[i] = name[i]; 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci /* need a variable for our GUID */ 1008c2ecf20Sopenharmony_ci guid = HFI1_EFIVAR_GUID; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci /* call into EFI runtime services */ 1038c2ecf20Sopenharmony_ci status = efi.get_variable( 1048c2ecf20Sopenharmony_ci uni_name, 1058c2ecf20Sopenharmony_ci &guid, 1068c2ecf20Sopenharmony_ci NULL, 1078c2ecf20Sopenharmony_ci &temp_size, 1088c2ecf20Sopenharmony_ci temp_buffer); 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci /* 1118c2ecf20Sopenharmony_ci * It would be nice to call efi_status_to_err() here, but that 1128c2ecf20Sopenharmony_ci * is in the EFIVAR_FS code and may not be compiled in. 1138c2ecf20Sopenharmony_ci * However, even that is insufficient since it does not cover 1148c2ecf20Sopenharmony_ci * EFI_BUFFER_TOO_SMALL which could be an important return. 1158c2ecf20Sopenharmony_ci * For now, just split out succces or not found. 1168c2ecf20Sopenharmony_ci */ 1178c2ecf20Sopenharmony_ci ret = status == EFI_SUCCESS ? 0 : 1188c2ecf20Sopenharmony_ci status == EFI_NOT_FOUND ? -ENOENT : 1198c2ecf20Sopenharmony_ci -EINVAL; 1208c2ecf20Sopenharmony_ci if (ret) 1218c2ecf20Sopenharmony_ci goto fail; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci /* 1248c2ecf20Sopenharmony_ci * We have successfully read the EFI variable into our 1258c2ecf20Sopenharmony_ci * temporary buffer. Now allocate a correctly sized 1268c2ecf20Sopenharmony_ci * buffer. 1278c2ecf20Sopenharmony_ci */ 1288c2ecf20Sopenharmony_ci data = kmemdup(temp_buffer, temp_size, GFP_KERNEL); 1298c2ecf20Sopenharmony_ci if (!data) { 1308c2ecf20Sopenharmony_ci ret = -ENOMEM; 1318c2ecf20Sopenharmony_ci goto fail; 1328c2ecf20Sopenharmony_ci } 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci *size = temp_size; 1358c2ecf20Sopenharmony_ci *return_data = data; 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_cifail: 1388c2ecf20Sopenharmony_ci kfree(uni_name); 1398c2ecf20Sopenharmony_ci kfree(temp_buffer); 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci return ret; 1428c2ecf20Sopenharmony_ci} 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci/* 1458c2ecf20Sopenharmony_ci * Read an HFI1 EFI variable of the form: 1468c2ecf20Sopenharmony_ci * <PCIe address>-<kind> 1478c2ecf20Sopenharmony_ci * Return an kalloc'ed array and size of the data. 1488c2ecf20Sopenharmony_ci * 1498c2ecf20Sopenharmony_ci * Returns 0 on success, -errno on failure. 1508c2ecf20Sopenharmony_ci */ 1518c2ecf20Sopenharmony_ciint read_hfi1_efi_var(struct hfi1_devdata *dd, const char *kind, 1528c2ecf20Sopenharmony_ci unsigned long *size, void **return_data) 1538c2ecf20Sopenharmony_ci{ 1548c2ecf20Sopenharmony_ci char prefix_name[64]; 1558c2ecf20Sopenharmony_ci char name[128]; 1568c2ecf20Sopenharmony_ci int result; 1578c2ecf20Sopenharmony_ci int i; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci /* create a common prefix */ 1608c2ecf20Sopenharmony_ci snprintf(prefix_name, sizeof(prefix_name), "%04x:%02x:%02x.%x", 1618c2ecf20Sopenharmony_ci pci_domain_nr(dd->pcidev->bus), 1628c2ecf20Sopenharmony_ci dd->pcidev->bus->number, 1638c2ecf20Sopenharmony_ci PCI_SLOT(dd->pcidev->devfn), 1648c2ecf20Sopenharmony_ci PCI_FUNC(dd->pcidev->devfn)); 1658c2ecf20Sopenharmony_ci snprintf(name, sizeof(name), "%s-%s", prefix_name, kind); 1668c2ecf20Sopenharmony_ci result = read_efi_var(name, size, return_data); 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci /* 1698c2ecf20Sopenharmony_ci * If reading the lowercase EFI variable fail, read the uppercase 1708c2ecf20Sopenharmony_ci * variable. 1718c2ecf20Sopenharmony_ci */ 1728c2ecf20Sopenharmony_ci if (result) { 1738c2ecf20Sopenharmony_ci /* Converting to uppercase */ 1748c2ecf20Sopenharmony_ci for (i = 0; prefix_name[i]; i++) 1758c2ecf20Sopenharmony_ci if (isalpha(prefix_name[i])) 1768c2ecf20Sopenharmony_ci prefix_name[i] = toupper(prefix_name[i]); 1778c2ecf20Sopenharmony_ci snprintf(name, sizeof(name), "%s-%s", prefix_name, kind); 1788c2ecf20Sopenharmony_ci result = read_efi_var(name, size, return_data); 1798c2ecf20Sopenharmony_ci } 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci return result; 1828c2ecf20Sopenharmony_ci} 183