dell-smbios-wmi.c (6846B)
1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * WMI methods for use with dell-smbios 4 * 5 * Copyright (c) 2017 Dell Inc. 6 */ 7#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 8 9#include <linux/dmi.h> 10#include <linux/list.h> 11#include <linux/module.h> 12#include <linux/mutex.h> 13#include <linux/uaccess.h> 14#include <linux/wmi.h> 15#include "dell-smbios.h" 16#include "dell-wmi-descriptor.h" 17 18static DEFINE_MUTEX(call_mutex); 19static DEFINE_MUTEX(list_mutex); 20static int wmi_supported; 21 22struct misc_bios_flags_structure { 23 struct dmi_header header; 24 u16 flags0; 25} __packed; 26#define FLAG_HAS_ACPI_WMI 0x02 27 28#define DELL_WMI_SMBIOS_GUID "A80593CE-A997-11DA-B012-B622A1EF5492" 29 30struct wmi_smbios_priv { 31 struct dell_wmi_smbios_buffer *buf; 32 struct list_head list; 33 struct wmi_device *wdev; 34 struct device *child; 35 u32 req_buf_size; 36}; 37static LIST_HEAD(wmi_list); 38 39static inline struct wmi_smbios_priv *get_first_smbios_priv(void) 40{ 41 return list_first_entry_or_null(&wmi_list, 42 struct wmi_smbios_priv, 43 list); 44} 45 46static int run_smbios_call(struct wmi_device *wdev) 47{ 48 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL}; 49 struct wmi_smbios_priv *priv; 50 struct acpi_buffer input; 51 union acpi_object *obj; 52 acpi_status status; 53 54 priv = dev_get_drvdata(&wdev->dev); 55 input.length = priv->req_buf_size - sizeof(u64); 56 input.pointer = &priv->buf->std; 57 58 dev_dbg(&wdev->dev, "evaluating: %u/%u [%x,%x,%x,%x]\n", 59 priv->buf->std.cmd_class, priv->buf->std.cmd_select, 60 priv->buf->std.input[0], priv->buf->std.input[1], 61 priv->buf->std.input[2], priv->buf->std.input[3]); 62 63 status = wmidev_evaluate_method(wdev, 0, 1, &input, &output); 64 if (ACPI_FAILURE(status)) 65 return -EIO; 66 obj = (union acpi_object *)output.pointer; 67 if (obj->type != ACPI_TYPE_BUFFER) { 68 dev_dbg(&wdev->dev, "received type: %d\n", obj->type); 69 if (obj->type == ACPI_TYPE_INTEGER) 70 dev_dbg(&wdev->dev, "SMBIOS call failed: %llu\n", 71 obj->integer.value); 72 kfree(output.pointer); 73 return -EIO; 74 } 75 memcpy(input.pointer, obj->buffer.pointer, obj->buffer.length); 76 dev_dbg(&wdev->dev, "result: [%08x,%08x,%08x,%08x]\n", 77 priv->buf->std.output[0], priv->buf->std.output[1], 78 priv->buf->std.output[2], priv->buf->std.output[3]); 79 kfree(output.pointer); 80 81 return 0; 82} 83 84static int dell_smbios_wmi_call(struct calling_interface_buffer *buffer) 85{ 86 struct wmi_smbios_priv *priv; 87 size_t difference; 88 size_t size; 89 int ret; 90 91 mutex_lock(&call_mutex); 92 priv = get_first_smbios_priv(); 93 if (!priv) { 94 ret = -ENODEV; 95 goto out_wmi_call; 96 } 97 98 size = sizeof(struct calling_interface_buffer); 99 difference = priv->req_buf_size - sizeof(u64) - size; 100 101 memset(&priv->buf->ext, 0, difference); 102 memcpy(&priv->buf->std, buffer, size); 103 ret = run_smbios_call(priv->wdev); 104 memcpy(buffer, &priv->buf->std, size); 105out_wmi_call: 106 mutex_unlock(&call_mutex); 107 108 return ret; 109} 110 111static long dell_smbios_wmi_filter(struct wmi_device *wdev, unsigned int cmd, 112 struct wmi_ioctl_buffer *arg) 113{ 114 struct wmi_smbios_priv *priv; 115 int ret = 0; 116 117 switch (cmd) { 118 case DELL_WMI_SMBIOS_CMD: 119 mutex_lock(&call_mutex); 120 priv = dev_get_drvdata(&wdev->dev); 121 if (!priv) { 122 ret = -ENODEV; 123 goto fail_smbios_cmd; 124 } 125 memcpy(priv->buf, arg, priv->req_buf_size); 126 if (dell_smbios_call_filter(&wdev->dev, &priv->buf->std)) { 127 dev_err(&wdev->dev, "Invalid call %d/%d:%8x\n", 128 priv->buf->std.cmd_class, 129 priv->buf->std.cmd_select, 130 priv->buf->std.input[0]); 131 ret = -EFAULT; 132 goto fail_smbios_cmd; 133 } 134 ret = run_smbios_call(priv->wdev); 135 if (ret) 136 goto fail_smbios_cmd; 137 memcpy(arg, priv->buf, priv->req_buf_size); 138fail_smbios_cmd: 139 mutex_unlock(&call_mutex); 140 break; 141 default: 142 ret = -ENOIOCTLCMD; 143 } 144 return ret; 145} 146 147static int dell_smbios_wmi_probe(struct wmi_device *wdev, const void *context) 148{ 149 struct wmi_driver *wdriver = 150 container_of(wdev->dev.driver, struct wmi_driver, driver); 151 struct wmi_smbios_priv *priv; 152 u32 hotfix; 153 int count; 154 int ret; 155 156 ret = dell_wmi_get_descriptor_valid(); 157 if (ret) 158 return ret; 159 160 priv = devm_kzalloc(&wdev->dev, sizeof(struct wmi_smbios_priv), 161 GFP_KERNEL); 162 if (!priv) 163 return -ENOMEM; 164 165 /* WMI buffer size will be either 4k or 32k depending on machine */ 166 if (!dell_wmi_get_size(&priv->req_buf_size)) 167 return -EPROBE_DEFER; 168 169 /* some SMBIOS calls fail unless BIOS contains hotfix */ 170 if (!dell_wmi_get_hotfix(&hotfix)) 171 return -EPROBE_DEFER; 172 if (!hotfix) { 173 dev_warn(&wdev->dev, 174 "WMI SMBIOS userspace interface not supported(%u), try upgrading to a newer BIOS\n", 175 hotfix); 176 wdriver->filter_callback = NULL; 177 } 178 179 /* add in the length object we will use internally with ioctl */ 180 priv->req_buf_size += sizeof(u64); 181 ret = set_required_buffer_size(wdev, priv->req_buf_size); 182 if (ret) 183 return ret; 184 185 count = get_order(priv->req_buf_size); 186 priv->buf = (void *)__get_free_pages(GFP_KERNEL, count); 187 if (!priv->buf) 188 return -ENOMEM; 189 190 /* ID is used by dell-smbios to set priority of drivers */ 191 wdev->dev.id = 1; 192 ret = dell_smbios_register_device(&wdev->dev, &dell_smbios_wmi_call); 193 if (ret) 194 goto fail_register; 195 196 priv->wdev = wdev; 197 dev_set_drvdata(&wdev->dev, priv); 198 mutex_lock(&list_mutex); 199 list_add_tail(&priv->list, &wmi_list); 200 mutex_unlock(&list_mutex); 201 202 return 0; 203 204fail_register: 205 free_pages((unsigned long)priv->buf, count); 206 return ret; 207} 208 209static void dell_smbios_wmi_remove(struct wmi_device *wdev) 210{ 211 struct wmi_smbios_priv *priv = dev_get_drvdata(&wdev->dev); 212 int count; 213 214 mutex_lock(&call_mutex); 215 mutex_lock(&list_mutex); 216 list_del(&priv->list); 217 mutex_unlock(&list_mutex); 218 dell_smbios_unregister_device(&wdev->dev); 219 count = get_order(priv->req_buf_size); 220 free_pages((unsigned long)priv->buf, count); 221 mutex_unlock(&call_mutex); 222} 223 224static const struct wmi_device_id dell_smbios_wmi_id_table[] = { 225 { .guid_string = DELL_WMI_SMBIOS_GUID }, 226 { }, 227}; 228 229static void parse_b1_table(const struct dmi_header *dm) 230{ 231 struct misc_bios_flags_structure *flags = 232 container_of(dm, struct misc_bios_flags_structure, header); 233 234 /* 4 bytes header, 8 bytes flags */ 235 if (dm->length < 12) 236 return; 237 if (dm->handle != 0xb100) 238 return; 239 if ((flags->flags0 & FLAG_HAS_ACPI_WMI)) 240 wmi_supported = 1; 241} 242 243static void find_b1(const struct dmi_header *dm, void *dummy) 244{ 245 switch (dm->type) { 246 case 0xb1: /* misc bios flags */ 247 parse_b1_table(dm); 248 break; 249 } 250} 251 252static struct wmi_driver dell_smbios_wmi_driver = { 253 .driver = { 254 .name = "dell-smbios", 255 }, 256 .probe = dell_smbios_wmi_probe, 257 .remove = dell_smbios_wmi_remove, 258 .id_table = dell_smbios_wmi_id_table, 259 .filter_callback = dell_smbios_wmi_filter, 260}; 261 262int init_dell_smbios_wmi(void) 263{ 264 dmi_walk(find_b1, NULL); 265 266 if (!wmi_supported) 267 return -ENODEV; 268 269 return wmi_driver_register(&dell_smbios_wmi_driver); 270} 271 272void exit_dell_smbios_wmi(void) 273{ 274 if (wmi_supported) 275 wmi_driver_unregister(&dell_smbios_wmi_driver); 276} 277 278MODULE_DEVICE_TABLE(wmi, dell_smbios_wmi_id_table);