dell-smbios: don't pass an SMBIOS buffer to dell_smbios_send_request()
[linux-2.6-block.git] / drivers / platform / x86 / dell-smbios.c
CommitLineData
2f9f26bd
MK
1/*
2 * Common functions for kernel modules using Dell SMBIOS
3 *
4 * Copyright (c) Red Hat <mjg@redhat.com>
5 * Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
6 * Copyright (c) 2014 Pali Rohár <pali.rohar@gmail.com>
7 *
8 * Based on documentation in the libsmbios package:
9 * Copyright (C) 2005-2014 Dell Inc.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/dmi.h>
19#include <linux/gfp.h>
20#include <linux/mutex.h>
21#include <linux/slab.h>
22#include <linux/io.h>
23#include "../../firmware/dcdbas.h"
24#include "dell-smbios.h"
25
26struct calling_interface_structure {
27 struct dmi_header header;
28 u16 cmdIOAddress;
29 u8 cmdIOCode;
30 u32 supportedCmds;
31 struct calling_interface_token tokens[];
32} __packed;
33
34struct calling_interface_buffer *buffer;
35EXPORT_SYMBOL_GPL(buffer);
36static DEFINE_MUTEX(buffer_mutex);
37
38static int da_command_address;
39static int da_command_code;
40static int da_num_tokens;
41struct calling_interface_token *da_tokens;
42EXPORT_SYMBOL_GPL(da_tokens);
43
ee83c475 44void dell_smbios_get_buffer(void)
2f9f26bd
MK
45{
46 mutex_lock(&buffer_mutex);
b6aa7e18 47 dell_smbios_clear_buffer();
2f9f26bd 48}
ee83c475 49EXPORT_SYMBOL_GPL(dell_smbios_get_buffer);
2f9f26bd 50
b6aa7e18 51void dell_smbios_clear_buffer(void)
2f9f26bd
MK
52{
53 memset(buffer, 0, sizeof(struct calling_interface_buffer));
54}
b6aa7e18 55EXPORT_SYMBOL_GPL(dell_smbios_clear_buffer);
2f9f26bd 56
cb161763 57void dell_smbios_release_buffer(void)
2f9f26bd
MK
58{
59 mutex_unlock(&buffer_mutex);
60}
cb161763 61EXPORT_SYMBOL_GPL(dell_smbios_release_buffer);
2f9f26bd
MK
62
63struct calling_interface_buffer *
17070f24 64dell_smbios_send_request(int class, int select)
2f9f26bd
MK
65{
66 struct smi_cmd command;
67
68 command.magic = SMI_CMD_MAGIC;
69 command.command_address = da_command_address;
70 command.command_code = da_command_code;
71 command.ebx = virt_to_phys(buffer);
72 command.ecx = 0x42534931;
73
74 buffer->class = class;
75 buffer->select = select;
76
77 dcdbas_smi_request(&command);
78
79 return buffer;
80}
2f262136 81EXPORT_SYMBOL_GPL(dell_smbios_send_request);
2f9f26bd
MK
82
83int find_token_id(int tokenid)
84{
85 int i;
86
87 for (i = 0; i < da_num_tokens; i++) {
88 if (da_tokens[i].tokenID == tokenid)
89 return i;
90 }
91
92 return -1;
93}
94EXPORT_SYMBOL_GPL(find_token_id);
95
96int find_token_location(int tokenid)
97{
98 int id;
99
100 id = find_token_id(tokenid);
101 if (id == -1)
102 return -1;
103
104 return da_tokens[id].location;
105}
106EXPORT_SYMBOL_GPL(find_token_location);
107
108static void __init parse_da_table(const struct dmi_header *dm)
109{
110 /* Final token is a terminator, so we don't want to copy it */
111 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
112 struct calling_interface_token *new_da_tokens;
113 struct calling_interface_structure *table =
114 container_of(dm, struct calling_interface_structure, header);
115
116 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
117 6 bytes of entry */
118
119 if (dm->length < 17)
120 return;
121
122 da_command_address = table->cmdIOAddress;
123 da_command_code = table->cmdIOCode;
124
125 new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
126 sizeof(struct calling_interface_token),
127 GFP_KERNEL);
128
129 if (!new_da_tokens)
130 return;
131 da_tokens = new_da_tokens;
132
133 memcpy(da_tokens+da_num_tokens, table->tokens,
134 sizeof(struct calling_interface_token) * tokens);
135
136 da_num_tokens += tokens;
137}
138
139static void __init find_tokens(const struct dmi_header *dm, void *dummy)
140{
141 switch (dm->type) {
142 case 0xd4: /* Indexed IO */
143 case 0xd5: /* Protected Area Type 1 */
144 case 0xd6: /* Protected Area Type 2 */
145 break;
146 case 0xda: /* Calling interface */
147 parse_da_table(dm);
148 break;
149 }
150}
151
152static int __init dell_smbios_init(void)
153{
154 int ret;
155
156 dmi_walk(find_tokens, NULL);
157
158 if (!da_tokens) {
159 pr_info("Unable to find dmi tokens\n");
160 return -ENODEV;
161 }
162
163 /*
164 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
165 * is passed to SMI handler.
166 */
167 buffer = (void *)__get_free_page(GFP_KERNEL | GFP_DMA32);
168 if (!buffer) {
169 ret = -ENOMEM;
170 goto fail_buffer;
171 }
172
173 return 0;
174
175fail_buffer:
176 kfree(da_tokens);
177 return ret;
178}
179
180static void __exit dell_smbios_exit(void)
181{
182 kfree(da_tokens);
183 free_page((unsigned long)buffer);
184}
185
186subsys_initcall(dell_smbios_init);
187module_exit(dell_smbios_exit);
188
189MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
190MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
191MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
192MODULE_DESCRIPTION("Common functions for kernel modules using Dell SMBIOS");
193MODULE_LICENSE("GPL");