18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * WMI methods for use with dell-smbios 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2017 Dell Inc. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/dmi.h> 108c2ecf20Sopenharmony_ci#include <linux/list.h> 118c2ecf20Sopenharmony_ci#include <linux/module.h> 128c2ecf20Sopenharmony_ci#include <linux/mutex.h> 138c2ecf20Sopenharmony_ci#include <linux/uaccess.h> 148c2ecf20Sopenharmony_ci#include <linux/wmi.h> 158c2ecf20Sopenharmony_ci#include "dell-smbios.h" 168c2ecf20Sopenharmony_ci#include "dell-wmi-descriptor.h" 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(call_mutex); 198c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(list_mutex); 208c2ecf20Sopenharmony_cistatic int wmi_supported; 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_cistruct misc_bios_flags_structure { 238c2ecf20Sopenharmony_ci struct dmi_header header; 248c2ecf20Sopenharmony_ci u16 flags0; 258c2ecf20Sopenharmony_ci} __packed; 268c2ecf20Sopenharmony_ci#define FLAG_HAS_ACPI_WMI 0x02 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci#define DELL_WMI_SMBIOS_GUID "A80593CE-A997-11DA-B012-B622A1EF5492" 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_cistruct wmi_smbios_priv { 318c2ecf20Sopenharmony_ci struct dell_wmi_smbios_buffer *buf; 328c2ecf20Sopenharmony_ci struct list_head list; 338c2ecf20Sopenharmony_ci struct wmi_device *wdev; 348c2ecf20Sopenharmony_ci struct device *child; 358c2ecf20Sopenharmony_ci u32 req_buf_size; 368c2ecf20Sopenharmony_ci}; 378c2ecf20Sopenharmony_cistatic LIST_HEAD(wmi_list); 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_cistatic inline struct wmi_smbios_priv *get_first_smbios_priv(void) 408c2ecf20Sopenharmony_ci{ 418c2ecf20Sopenharmony_ci return list_first_entry_or_null(&wmi_list, 428c2ecf20Sopenharmony_ci struct wmi_smbios_priv, 438c2ecf20Sopenharmony_ci list); 448c2ecf20Sopenharmony_ci} 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_cistatic int run_smbios_call(struct wmi_device *wdev) 478c2ecf20Sopenharmony_ci{ 488c2ecf20Sopenharmony_ci struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL}; 498c2ecf20Sopenharmony_ci struct wmi_smbios_priv *priv; 508c2ecf20Sopenharmony_ci struct acpi_buffer input; 518c2ecf20Sopenharmony_ci union acpi_object *obj; 528c2ecf20Sopenharmony_ci acpi_status status; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci priv = dev_get_drvdata(&wdev->dev); 558c2ecf20Sopenharmony_ci input.length = priv->req_buf_size - sizeof(u64); 568c2ecf20Sopenharmony_ci input.pointer = &priv->buf->std; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci dev_dbg(&wdev->dev, "evaluating: %u/%u [%x,%x,%x,%x]\n", 598c2ecf20Sopenharmony_ci priv->buf->std.cmd_class, priv->buf->std.cmd_select, 608c2ecf20Sopenharmony_ci priv->buf->std.input[0], priv->buf->std.input[1], 618c2ecf20Sopenharmony_ci priv->buf->std.input[2], priv->buf->std.input[3]); 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci status = wmidev_evaluate_method(wdev, 0, 1, &input, &output); 648c2ecf20Sopenharmony_ci if (ACPI_FAILURE(status)) 658c2ecf20Sopenharmony_ci return -EIO; 668c2ecf20Sopenharmony_ci obj = (union acpi_object *)output.pointer; 678c2ecf20Sopenharmony_ci if (obj->type != ACPI_TYPE_BUFFER) { 688c2ecf20Sopenharmony_ci dev_dbg(&wdev->dev, "received type: %d\n", obj->type); 698c2ecf20Sopenharmony_ci if (obj->type == ACPI_TYPE_INTEGER) 708c2ecf20Sopenharmony_ci dev_dbg(&wdev->dev, "SMBIOS call failed: %llu\n", 718c2ecf20Sopenharmony_ci obj->integer.value); 728c2ecf20Sopenharmony_ci kfree(output.pointer); 738c2ecf20Sopenharmony_ci return -EIO; 748c2ecf20Sopenharmony_ci } 758c2ecf20Sopenharmony_ci memcpy(&priv->buf->std, obj->buffer.pointer, obj->buffer.length); 768c2ecf20Sopenharmony_ci dev_dbg(&wdev->dev, "result: [%08x,%08x,%08x,%08x]\n", 778c2ecf20Sopenharmony_ci priv->buf->std.output[0], priv->buf->std.output[1], 788c2ecf20Sopenharmony_ci priv->buf->std.output[2], priv->buf->std.output[3]); 798c2ecf20Sopenharmony_ci kfree(output.pointer); 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci return 0; 828c2ecf20Sopenharmony_ci} 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_cistatic int dell_smbios_wmi_call(struct calling_interface_buffer *buffer) 858c2ecf20Sopenharmony_ci{ 868c2ecf20Sopenharmony_ci struct wmi_smbios_priv *priv; 878c2ecf20Sopenharmony_ci size_t difference; 888c2ecf20Sopenharmony_ci size_t size; 898c2ecf20Sopenharmony_ci int ret; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci mutex_lock(&call_mutex); 928c2ecf20Sopenharmony_ci priv = get_first_smbios_priv(); 938c2ecf20Sopenharmony_ci if (!priv) { 948c2ecf20Sopenharmony_ci ret = -ENODEV; 958c2ecf20Sopenharmony_ci goto out_wmi_call; 968c2ecf20Sopenharmony_ci } 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci size = sizeof(struct calling_interface_buffer); 998c2ecf20Sopenharmony_ci difference = priv->req_buf_size - sizeof(u64) - size; 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci memset(&priv->buf->ext, 0, difference); 1028c2ecf20Sopenharmony_ci memcpy(&priv->buf->std, buffer, size); 1038c2ecf20Sopenharmony_ci ret = run_smbios_call(priv->wdev); 1048c2ecf20Sopenharmony_ci memcpy(buffer, &priv->buf->std, size); 1058c2ecf20Sopenharmony_ciout_wmi_call: 1068c2ecf20Sopenharmony_ci mutex_unlock(&call_mutex); 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci return ret; 1098c2ecf20Sopenharmony_ci} 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_cistatic long dell_smbios_wmi_filter(struct wmi_device *wdev, unsigned int cmd, 1128c2ecf20Sopenharmony_ci struct wmi_ioctl_buffer *arg) 1138c2ecf20Sopenharmony_ci{ 1148c2ecf20Sopenharmony_ci struct wmi_smbios_priv *priv; 1158c2ecf20Sopenharmony_ci int ret = 0; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci switch (cmd) { 1188c2ecf20Sopenharmony_ci case DELL_WMI_SMBIOS_CMD: 1198c2ecf20Sopenharmony_ci mutex_lock(&call_mutex); 1208c2ecf20Sopenharmony_ci priv = dev_get_drvdata(&wdev->dev); 1218c2ecf20Sopenharmony_ci if (!priv) { 1228c2ecf20Sopenharmony_ci ret = -ENODEV; 1238c2ecf20Sopenharmony_ci goto fail_smbios_cmd; 1248c2ecf20Sopenharmony_ci } 1258c2ecf20Sopenharmony_ci memcpy(priv->buf, arg, priv->req_buf_size); 1268c2ecf20Sopenharmony_ci if (dell_smbios_call_filter(&wdev->dev, &priv->buf->std)) { 1278c2ecf20Sopenharmony_ci dev_err(&wdev->dev, "Invalid call %d/%d:%8x\n", 1288c2ecf20Sopenharmony_ci priv->buf->std.cmd_class, 1298c2ecf20Sopenharmony_ci priv->buf->std.cmd_select, 1308c2ecf20Sopenharmony_ci priv->buf->std.input[0]); 1318c2ecf20Sopenharmony_ci ret = -EFAULT; 1328c2ecf20Sopenharmony_ci goto fail_smbios_cmd; 1338c2ecf20Sopenharmony_ci } 1348c2ecf20Sopenharmony_ci ret = run_smbios_call(priv->wdev); 1358c2ecf20Sopenharmony_ci if (ret) 1368c2ecf20Sopenharmony_ci goto fail_smbios_cmd; 1378c2ecf20Sopenharmony_ci memcpy(arg, priv->buf, priv->req_buf_size); 1388c2ecf20Sopenharmony_cifail_smbios_cmd: 1398c2ecf20Sopenharmony_ci mutex_unlock(&call_mutex); 1408c2ecf20Sopenharmony_ci break; 1418c2ecf20Sopenharmony_ci default: 1428c2ecf20Sopenharmony_ci ret = -ENOIOCTLCMD; 1438c2ecf20Sopenharmony_ci } 1448c2ecf20Sopenharmony_ci return ret; 1458c2ecf20Sopenharmony_ci} 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_cistatic int dell_smbios_wmi_probe(struct wmi_device *wdev, const void *context) 1488c2ecf20Sopenharmony_ci{ 1498c2ecf20Sopenharmony_ci struct wmi_driver *wdriver = 1508c2ecf20Sopenharmony_ci container_of(wdev->dev.driver, struct wmi_driver, driver); 1518c2ecf20Sopenharmony_ci struct wmi_smbios_priv *priv; 1528c2ecf20Sopenharmony_ci u32 hotfix; 1538c2ecf20Sopenharmony_ci int count; 1548c2ecf20Sopenharmony_ci int ret; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci ret = dell_wmi_get_descriptor_valid(); 1578c2ecf20Sopenharmony_ci if (ret) 1588c2ecf20Sopenharmony_ci return ret; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci priv = devm_kzalloc(&wdev->dev, sizeof(struct wmi_smbios_priv), 1618c2ecf20Sopenharmony_ci GFP_KERNEL); 1628c2ecf20Sopenharmony_ci if (!priv) 1638c2ecf20Sopenharmony_ci return -ENOMEM; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci /* WMI buffer size will be either 4k or 32k depending on machine */ 1668c2ecf20Sopenharmony_ci if (!dell_wmi_get_size(&priv->req_buf_size)) 1678c2ecf20Sopenharmony_ci return -EPROBE_DEFER; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci /* some SMBIOS calls fail unless BIOS contains hotfix */ 1708c2ecf20Sopenharmony_ci if (!dell_wmi_get_hotfix(&hotfix)) 1718c2ecf20Sopenharmony_ci return -EPROBE_DEFER; 1728c2ecf20Sopenharmony_ci if (!hotfix) { 1738c2ecf20Sopenharmony_ci dev_warn(&wdev->dev, 1748c2ecf20Sopenharmony_ci "WMI SMBIOS userspace interface not supported(%u), try upgrading to a newer BIOS\n", 1758c2ecf20Sopenharmony_ci hotfix); 1768c2ecf20Sopenharmony_ci wdriver->filter_callback = NULL; 1778c2ecf20Sopenharmony_ci } 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci /* add in the length object we will use internally with ioctl */ 1808c2ecf20Sopenharmony_ci priv->req_buf_size += sizeof(u64); 1818c2ecf20Sopenharmony_ci ret = set_required_buffer_size(wdev, priv->req_buf_size); 1828c2ecf20Sopenharmony_ci if (ret) 1838c2ecf20Sopenharmony_ci return ret; 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci count = get_order(priv->req_buf_size); 1868c2ecf20Sopenharmony_ci priv->buf = (void *)__get_free_pages(GFP_KERNEL, count); 1878c2ecf20Sopenharmony_ci if (!priv->buf) 1888c2ecf20Sopenharmony_ci return -ENOMEM; 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci /* ID is used by dell-smbios to set priority of drivers */ 1918c2ecf20Sopenharmony_ci wdev->dev.id = 1; 1928c2ecf20Sopenharmony_ci ret = dell_smbios_register_device(&wdev->dev, &dell_smbios_wmi_call); 1938c2ecf20Sopenharmony_ci if (ret) 1948c2ecf20Sopenharmony_ci goto fail_register; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci priv->wdev = wdev; 1978c2ecf20Sopenharmony_ci dev_set_drvdata(&wdev->dev, priv); 1988c2ecf20Sopenharmony_ci mutex_lock(&list_mutex); 1998c2ecf20Sopenharmony_ci list_add_tail(&priv->list, &wmi_list); 2008c2ecf20Sopenharmony_ci mutex_unlock(&list_mutex); 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci return 0; 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_cifail_register: 2058c2ecf20Sopenharmony_ci free_pages((unsigned long)priv->buf, count); 2068c2ecf20Sopenharmony_ci return ret; 2078c2ecf20Sopenharmony_ci} 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_cistatic int dell_smbios_wmi_remove(struct wmi_device *wdev) 2108c2ecf20Sopenharmony_ci{ 2118c2ecf20Sopenharmony_ci struct wmi_smbios_priv *priv = dev_get_drvdata(&wdev->dev); 2128c2ecf20Sopenharmony_ci int count; 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci mutex_lock(&call_mutex); 2158c2ecf20Sopenharmony_ci mutex_lock(&list_mutex); 2168c2ecf20Sopenharmony_ci list_del(&priv->list); 2178c2ecf20Sopenharmony_ci mutex_unlock(&list_mutex); 2188c2ecf20Sopenharmony_ci dell_smbios_unregister_device(&wdev->dev); 2198c2ecf20Sopenharmony_ci count = get_order(priv->req_buf_size); 2208c2ecf20Sopenharmony_ci free_pages((unsigned long)priv->buf, count); 2218c2ecf20Sopenharmony_ci mutex_unlock(&call_mutex); 2228c2ecf20Sopenharmony_ci return 0; 2238c2ecf20Sopenharmony_ci} 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_cistatic const struct wmi_device_id dell_smbios_wmi_id_table[] = { 2268c2ecf20Sopenharmony_ci { .guid_string = DELL_WMI_SMBIOS_GUID }, 2278c2ecf20Sopenharmony_ci { }, 2288c2ecf20Sopenharmony_ci}; 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_cistatic void parse_b1_table(const struct dmi_header *dm) 2318c2ecf20Sopenharmony_ci{ 2328c2ecf20Sopenharmony_ci struct misc_bios_flags_structure *flags = 2338c2ecf20Sopenharmony_ci container_of(dm, struct misc_bios_flags_structure, header); 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci /* 4 bytes header, 8 bytes flags */ 2368c2ecf20Sopenharmony_ci if (dm->length < 12) 2378c2ecf20Sopenharmony_ci return; 2388c2ecf20Sopenharmony_ci if (dm->handle != 0xb100) 2398c2ecf20Sopenharmony_ci return; 2408c2ecf20Sopenharmony_ci if ((flags->flags0 & FLAG_HAS_ACPI_WMI)) 2418c2ecf20Sopenharmony_ci wmi_supported = 1; 2428c2ecf20Sopenharmony_ci} 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_cistatic void find_b1(const struct dmi_header *dm, void *dummy) 2458c2ecf20Sopenharmony_ci{ 2468c2ecf20Sopenharmony_ci switch (dm->type) { 2478c2ecf20Sopenharmony_ci case 0xb1: /* misc bios flags */ 2488c2ecf20Sopenharmony_ci parse_b1_table(dm); 2498c2ecf20Sopenharmony_ci break; 2508c2ecf20Sopenharmony_ci } 2518c2ecf20Sopenharmony_ci} 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_cistatic struct wmi_driver dell_smbios_wmi_driver = { 2548c2ecf20Sopenharmony_ci .driver = { 2558c2ecf20Sopenharmony_ci .name = "dell-smbios", 2568c2ecf20Sopenharmony_ci }, 2578c2ecf20Sopenharmony_ci .probe = dell_smbios_wmi_probe, 2588c2ecf20Sopenharmony_ci .remove = dell_smbios_wmi_remove, 2598c2ecf20Sopenharmony_ci .id_table = dell_smbios_wmi_id_table, 2608c2ecf20Sopenharmony_ci .filter_callback = dell_smbios_wmi_filter, 2618c2ecf20Sopenharmony_ci}; 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ciint init_dell_smbios_wmi(void) 2648c2ecf20Sopenharmony_ci{ 2658c2ecf20Sopenharmony_ci dmi_walk(find_b1, NULL); 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci if (!wmi_supported) 2688c2ecf20Sopenharmony_ci return -ENODEV; 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci return wmi_driver_register(&dell_smbios_wmi_driver); 2718c2ecf20Sopenharmony_ci} 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_civoid exit_dell_smbios_wmi(void) 2748c2ecf20Sopenharmony_ci{ 2758c2ecf20Sopenharmony_ci if (wmi_supported) 2768c2ecf20Sopenharmony_ci wmi_driver_unregister(&dell_smbios_wmi_driver); 2778c2ecf20Sopenharmony_ci} 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(wmi, dell_smbios_wmi_id_table); 280