blob: 6a27e4c85a248187dbeff640a1b90bf47cffaa4a [file] [log] [blame]
Michał Kępień2f9f26b2016-01-22 15:27:13 +01001/*
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
Michał Kępień92ebd0d2016-01-22 15:27:21 +010034static struct calling_interface_buffer *buffer;
Michał Kępień2f9f26b2016-01-22 15:27:13 +010035static DEFINE_MUTEX(buffer_mutex);
36
37static int da_command_address;
38static int da_command_code;
39static int da_num_tokens;
40struct calling_interface_token *da_tokens;
41EXPORT_SYMBOL_GPL(da_tokens);
42
Michał Kępieńbc2104c2016-01-22 15:27:20 +010043struct calling_interface_buffer *dell_smbios_get_buffer(void)
Michał Kępień2f9f26b2016-01-22 15:27:13 +010044{
45 mutex_lock(&buffer_mutex);
Michał Kępieńb6aa7e12016-01-22 15:27:15 +010046 dell_smbios_clear_buffer();
Michał Kępieńbc2104c2016-01-22 15:27:20 +010047 return buffer;
Michał Kępień2f9f26b2016-01-22 15:27:13 +010048}
Michał Kępieńee83c472016-01-22 15:27:14 +010049EXPORT_SYMBOL_GPL(dell_smbios_get_buffer);
Michał Kępień2f9f26b2016-01-22 15:27:13 +010050
Michał Kępieńb6aa7e12016-01-22 15:27:15 +010051void dell_smbios_clear_buffer(void)
Michał Kępień2f9f26b2016-01-22 15:27:13 +010052{
53 memset(buffer, 0, sizeof(struct calling_interface_buffer));
54}
Michał Kępieńb6aa7e12016-01-22 15:27:15 +010055EXPORT_SYMBOL_GPL(dell_smbios_clear_buffer);
Michał Kępień2f9f26b2016-01-22 15:27:13 +010056
Michał Kępieńcb1617632016-01-22 15:27:16 +010057void dell_smbios_release_buffer(void)
Michał Kępień2f9f26b2016-01-22 15:27:13 +010058{
59 mutex_unlock(&buffer_mutex);
60}
Michał Kępieńcb1617632016-01-22 15:27:16 +010061EXPORT_SYMBOL_GPL(dell_smbios_release_buffer);
Michał Kępień2f9f26b2016-01-22 15:27:13 +010062
Michał Kępieńc42831c2016-01-22 15:27:19 +010063void dell_smbios_send_request(int class, int select)
Michał Kępień2f9f26b2016-01-22 15:27:13 +010064{
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);
Michał Kępień2f9f26b2016-01-22 15:27:13 +010077}
Michał Kępień2f262132016-01-22 15:27:17 +010078EXPORT_SYMBOL_GPL(dell_smbios_send_request);
Michał Kępień2f9f26b2016-01-22 15:27:13 +010079
Michał Kępień96f7ef92016-01-22 15:27:22 +010080struct calling_interface_token *dell_smbios_find_token(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 &da_tokens[i];
87 }
88
89 return NULL;
90}
91EXPORT_SYMBOL_GPL(dell_smbios_find_token);
92
Michał Kępień2f9f26b2016-01-22 15:27:13 +010093int find_token_id(int tokenid)
94{
95 int i;
96
97 for (i = 0; i < da_num_tokens; i++) {
98 if (da_tokens[i].tokenID == tokenid)
99 return i;
100 }
101
102 return -1;
103}
104EXPORT_SYMBOL_GPL(find_token_id);
105
106int find_token_location(int tokenid)
107{
108 int id;
109
110 id = find_token_id(tokenid);
111 if (id == -1)
112 return -1;
113
114 return da_tokens[id].location;
115}
116EXPORT_SYMBOL_GPL(find_token_location);
117
118static void __init parse_da_table(const struct dmi_header *dm)
119{
120 /* Final token is a terminator, so we don't want to copy it */
121 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
122 struct calling_interface_token *new_da_tokens;
123 struct calling_interface_structure *table =
124 container_of(dm, struct calling_interface_structure, header);
125
126 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
127 6 bytes of entry */
128
129 if (dm->length < 17)
130 return;
131
132 da_command_address = table->cmdIOAddress;
133 da_command_code = table->cmdIOCode;
134
135 new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
136 sizeof(struct calling_interface_token),
137 GFP_KERNEL);
138
139 if (!new_da_tokens)
140 return;
141 da_tokens = new_da_tokens;
142
143 memcpy(da_tokens+da_num_tokens, table->tokens,
144 sizeof(struct calling_interface_token) * tokens);
145
146 da_num_tokens += tokens;
147}
148
149static void __init find_tokens(const struct dmi_header *dm, void *dummy)
150{
151 switch (dm->type) {
152 case 0xd4: /* Indexed IO */
153 case 0xd5: /* Protected Area Type 1 */
154 case 0xd6: /* Protected Area Type 2 */
155 break;
156 case 0xda: /* Calling interface */
157 parse_da_table(dm);
158 break;
159 }
160}
161
162static int __init dell_smbios_init(void)
163{
164 int ret;
165
166 dmi_walk(find_tokens, NULL);
167
168 if (!da_tokens) {
169 pr_info("Unable to find dmi tokens\n");
170 return -ENODEV;
171 }
172
173 /*
174 * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
175 * is passed to SMI handler.
176 */
177 buffer = (void *)__get_free_page(GFP_KERNEL | GFP_DMA32);
178 if (!buffer) {
179 ret = -ENOMEM;
180 goto fail_buffer;
181 }
182
183 return 0;
184
185fail_buffer:
186 kfree(da_tokens);
187 return ret;
188}
189
190static void __exit dell_smbios_exit(void)
191{
192 kfree(da_tokens);
193 free_page((unsigned long)buffer);
194}
195
196subsys_initcall(dell_smbios_init);
197module_exit(dell_smbios_exit);
198
199MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
200MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
201MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
202MODULE_DESCRIPTION("Common functions for kernel modules using Dell SMBIOS");
203MODULE_LICENSE("GPL");