Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
92ebd0d1 | 34 | static struct calling_interface_buffer *buffer; |
2f9f26bd MK |
35 | static DEFINE_MUTEX(buffer_mutex); |
36 | ||
37 | static int da_command_address; | |
38 | static int da_command_code; | |
39 | static int da_num_tokens; | |
40 | struct calling_interface_token *da_tokens; | |
41 | EXPORT_SYMBOL_GPL(da_tokens); | |
42 | ||
bc2104c2 | 43 | struct calling_interface_buffer *dell_smbios_get_buffer(void) |
2f9f26bd MK |
44 | { |
45 | mutex_lock(&buffer_mutex); | |
b6aa7e18 | 46 | dell_smbios_clear_buffer(); |
bc2104c2 | 47 | return buffer; |
2f9f26bd | 48 | } |
ee83c475 | 49 | EXPORT_SYMBOL_GPL(dell_smbios_get_buffer); |
2f9f26bd | 50 | |
b6aa7e18 | 51 | void dell_smbios_clear_buffer(void) |
2f9f26bd MK |
52 | { |
53 | memset(buffer, 0, sizeof(struct calling_interface_buffer)); | |
54 | } | |
b6aa7e18 | 55 | EXPORT_SYMBOL_GPL(dell_smbios_clear_buffer); |
2f9f26bd | 56 | |
cb161763 | 57 | void dell_smbios_release_buffer(void) |
2f9f26bd MK |
58 | { |
59 | mutex_unlock(&buffer_mutex); | |
60 | } | |
cb161763 | 61 | EXPORT_SYMBOL_GPL(dell_smbios_release_buffer); |
2f9f26bd | 62 | |
c42831c8 | 63 | void dell_smbios_send_request(int class, int select) |
2f9f26bd MK |
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); | |
2f9f26bd | 77 | } |
2f262136 | 78 | EXPORT_SYMBOL_GPL(dell_smbios_send_request); |
2f9f26bd | 79 | |
96f7ef90 MK |
80 | struct 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 | } | |
91 | EXPORT_SYMBOL_GPL(dell_smbios_find_token); | |
92 | ||
2f9f26bd MK |
93 | static void __init parse_da_table(const struct dmi_header *dm) |
94 | { | |
95 | /* Final token is a terminator, so we don't want to copy it */ | |
96 | int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1; | |
97 | struct calling_interface_token *new_da_tokens; | |
98 | struct calling_interface_structure *table = | |
99 | container_of(dm, struct calling_interface_structure, header); | |
100 | ||
101 | /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least | |
102 | 6 bytes of entry */ | |
103 | ||
104 | if (dm->length < 17) | |
105 | return; | |
106 | ||
107 | da_command_address = table->cmdIOAddress; | |
108 | da_command_code = table->cmdIOCode; | |
109 | ||
110 | new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) * | |
111 | sizeof(struct calling_interface_token), | |
112 | GFP_KERNEL); | |
113 | ||
114 | if (!new_da_tokens) | |
115 | return; | |
116 | da_tokens = new_da_tokens; | |
117 | ||
118 | memcpy(da_tokens+da_num_tokens, table->tokens, | |
119 | sizeof(struct calling_interface_token) * tokens); | |
120 | ||
121 | da_num_tokens += tokens; | |
122 | } | |
123 | ||
124 | static void __init find_tokens(const struct dmi_header *dm, void *dummy) | |
125 | { | |
126 | switch (dm->type) { | |
127 | case 0xd4: /* Indexed IO */ | |
128 | case 0xd5: /* Protected Area Type 1 */ | |
129 | case 0xd6: /* Protected Area Type 2 */ | |
130 | break; | |
131 | case 0xda: /* Calling interface */ | |
132 | parse_da_table(dm); | |
133 | break; | |
134 | } | |
135 | } | |
136 | ||
137 | static int __init dell_smbios_init(void) | |
138 | { | |
139 | int ret; | |
140 | ||
141 | dmi_walk(find_tokens, NULL); | |
142 | ||
143 | if (!da_tokens) { | |
144 | pr_info("Unable to find dmi tokens\n"); | |
145 | return -ENODEV; | |
146 | } | |
147 | ||
148 | /* | |
149 | * Allocate buffer below 4GB for SMI data--only 32-bit physical addr | |
150 | * is passed to SMI handler. | |
151 | */ | |
152 | buffer = (void *)__get_free_page(GFP_KERNEL | GFP_DMA32); | |
153 | if (!buffer) { | |
154 | ret = -ENOMEM; | |
155 | goto fail_buffer; | |
156 | } | |
157 | ||
158 | return 0; | |
159 | ||
160 | fail_buffer: | |
161 | kfree(da_tokens); | |
162 | return ret; | |
163 | } | |
164 | ||
165 | static void __exit dell_smbios_exit(void) | |
166 | { | |
167 | kfree(da_tokens); | |
168 | free_page((unsigned long)buffer); | |
169 | } | |
170 | ||
171 | subsys_initcall(dell_smbios_init); | |
172 | module_exit(dell_smbios_exit); | |
173 | ||
174 | MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>"); | |
175 | MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>"); | |
176 | MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>"); | |
177 | MODULE_DESCRIPTION("Common functions for kernel modules using Dell SMBIOS"); | |
178 | MODULE_LICENSE("GPL"); |