dell-smbios: don't return an SMBIOS buffer from dell_smbios_send_request()
[cascardo/linux.git] / drivers / platform / x86 / dell-smbios.c
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
26 struct 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
34 struct calling_interface_buffer *buffer;
35 EXPORT_SYMBOL_GPL(buffer);
36 static DEFINE_MUTEX(buffer_mutex);
37
38 static int da_command_address;
39 static int da_command_code;
40 static int da_num_tokens;
41 struct calling_interface_token *da_tokens;
42 EXPORT_SYMBOL_GPL(da_tokens);
43
44 void dell_smbios_get_buffer(void)
45 {
46         mutex_lock(&buffer_mutex);
47         dell_smbios_clear_buffer();
48 }
49 EXPORT_SYMBOL_GPL(dell_smbios_get_buffer);
50
51 void dell_smbios_clear_buffer(void)
52 {
53         memset(buffer, 0, sizeof(struct calling_interface_buffer));
54 }
55 EXPORT_SYMBOL_GPL(dell_smbios_clear_buffer);
56
57 void dell_smbios_release_buffer(void)
58 {
59         mutex_unlock(&buffer_mutex);
60 }
61 EXPORT_SYMBOL_GPL(dell_smbios_release_buffer);
62
63 void dell_smbios_send_request(int class, int select)
64 {
65         struct smi_cmd command;
66
67         command.magic = SMI_CMD_MAGIC;
68         command.command_address = da_command_address;
69         command.command_code = da_command_code;
70         command.ebx = virt_to_phys(buffer);
71         command.ecx = 0x42534931;
72
73         buffer->class = class;
74         buffer->select = select;
75
76         dcdbas_smi_request(&command);
77 }
78 EXPORT_SYMBOL_GPL(dell_smbios_send_request);
79
80 int find_token_id(int tokenid)
81 {
82         int i;
83
84         for (i = 0; i < da_num_tokens; i++) {
85                 if (da_tokens[i].tokenID == tokenid)
86                         return i;
87         }
88
89         return -1;
90 }
91 EXPORT_SYMBOL_GPL(find_token_id);
92
93 int find_token_location(int tokenid)
94 {
95         int id;
96
97         id = find_token_id(tokenid);
98         if (id == -1)
99                 return -1;
100
101         return da_tokens[id].location;
102 }
103 EXPORT_SYMBOL_GPL(find_token_location);
104
105 static void __init parse_da_table(const struct dmi_header *dm)
106 {
107         /* Final token is a terminator, so we don't want to copy it */
108         int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
109         struct calling_interface_token *new_da_tokens;
110         struct calling_interface_structure *table =
111                 container_of(dm, struct calling_interface_structure, header);
112
113         /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
114            6 bytes of entry */
115
116         if (dm->length < 17)
117                 return;
118
119         da_command_address = table->cmdIOAddress;
120         da_command_code = table->cmdIOCode;
121
122         new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
123                                  sizeof(struct calling_interface_token),
124                                  GFP_KERNEL);
125
126         if (!new_da_tokens)
127                 return;
128         da_tokens = new_da_tokens;
129
130         memcpy(da_tokens+da_num_tokens, table->tokens,
131                sizeof(struct calling_interface_token) * tokens);
132
133         da_num_tokens += tokens;
134 }
135
136 static void __init find_tokens(const struct dmi_header *dm, void *dummy)
137 {
138         switch (dm->type) {
139         case 0xd4: /* Indexed IO */
140         case 0xd5: /* Protected Area Type 1 */
141         case 0xd6: /* Protected Area Type 2 */
142                 break;
143         case 0xda: /* Calling interface */
144                 parse_da_table(dm);
145                 break;
146         }
147 }
148
149 static int __init dell_smbios_init(void)
150 {
151         int ret;
152
153         dmi_walk(find_tokens, NULL);
154
155         if (!da_tokens)  {
156                 pr_info("Unable to find dmi tokens\n");
157                 return -ENODEV;
158         }
159
160         /*
161          * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
162          * is passed to SMI handler.
163          */
164         buffer = (void *)__get_free_page(GFP_KERNEL | GFP_DMA32);
165         if (!buffer) {
166                 ret = -ENOMEM;
167                 goto fail_buffer;
168         }
169
170         return 0;
171
172 fail_buffer:
173         kfree(da_tokens);
174         return ret;
175 }
176
177 static void __exit dell_smbios_exit(void)
178 {
179         kfree(da_tokens);
180         free_page((unsigned long)buffer);
181 }
182
183 subsys_initcall(dell_smbios_init);
184 module_exit(dell_smbios_exit);
185
186 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
187 MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
188 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
189 MODULE_DESCRIPTION("Common functions for kernel modules using Dell SMBIOS");
190 MODULE_LICENSE("GPL");