Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 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 | |
Michał Kępień | 92ebd0d | 2016-01-22 15:27:21 +0100 | [diff] [blame] | 34 | static struct calling_interface_buffer *buffer; |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 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 | |
Michał Kępień | bc2104c | 2016-01-22 15:27:20 +0100 | [diff] [blame] | 43 | struct calling_interface_buffer *dell_smbios_get_buffer(void) |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 44 | { |
| 45 | mutex_lock(&buffer_mutex); |
Michał Kępień | b6aa7e1 | 2016-01-22 15:27:15 +0100 | [diff] [blame] | 46 | dell_smbios_clear_buffer(); |
Michał Kępień | bc2104c | 2016-01-22 15:27:20 +0100 | [diff] [blame] | 47 | return buffer; |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 48 | } |
Michał Kępień | ee83c47 | 2016-01-22 15:27:14 +0100 | [diff] [blame] | 49 | EXPORT_SYMBOL_GPL(dell_smbios_get_buffer); |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 50 | |
Michał Kępień | b6aa7e1 | 2016-01-22 15:27:15 +0100 | [diff] [blame] | 51 | void dell_smbios_clear_buffer(void) |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 52 | { |
| 53 | memset(buffer, 0, sizeof(struct calling_interface_buffer)); |
| 54 | } |
Michał Kępień | b6aa7e1 | 2016-01-22 15:27:15 +0100 | [diff] [blame] | 55 | EXPORT_SYMBOL_GPL(dell_smbios_clear_buffer); |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 56 | |
Michał Kępień | cb161763 | 2016-01-22 15:27:16 +0100 | [diff] [blame] | 57 | void dell_smbios_release_buffer(void) |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 58 | { |
| 59 | mutex_unlock(&buffer_mutex); |
| 60 | } |
Michał Kępień | cb161763 | 2016-01-22 15:27:16 +0100 | [diff] [blame] | 61 | EXPORT_SYMBOL_GPL(dell_smbios_release_buffer); |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 62 | |
Michał Kępień | c42831c | 2016-01-22 15:27:19 +0100 | [diff] [blame] | 63 | void dell_smbios_send_request(int class, int select) |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 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); |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 77 | } |
Michał Kępień | 2f26213 | 2016-01-22 15:27:17 +0100 | [diff] [blame] | 78 | EXPORT_SYMBOL_GPL(dell_smbios_send_request); |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 79 | |
Michał Kępień | 96f7ef9 | 2016-01-22 15:27:22 +0100 | [diff] [blame^] | 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 | |
Michał Kępień | 2f9f26b | 2016-01-22 15:27:13 +0100 | [diff] [blame] | 93 | int 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 | } |
| 104 | EXPORT_SYMBOL_GPL(find_token_id); |
| 105 | |
| 106 | int 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 | } |
| 116 | EXPORT_SYMBOL_GPL(find_token_location); |
| 117 | |
| 118 | static 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 | |
| 149 | static 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 | |
| 162 | static 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 | |
| 185 | fail_buffer: |
| 186 | kfree(da_tokens); |
| 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | static void __exit dell_smbios_exit(void) |
| 191 | { |
| 192 | kfree(da_tokens); |
| 193 | free_page((unsigned long)buffer); |
| 194 | } |
| 195 | |
| 196 | subsys_initcall(dell_smbios_init); |
| 197 | module_exit(dell_smbios_exit); |
| 198 | |
| 199 | MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>"); |
| 200 | MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>"); |
| 201 | MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>"); |
| 202 | MODULE_DESCRIPTION("Common functions for kernel modules using Dell SMBIOS"); |
| 203 | MODULE_LICENSE("GPL"); |