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