Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * dcdbas.c: Dell Systems Management Base Driver |
| 3 | * |
| 4 | * The Dell Systems Management Base Driver provides a sysfs interface for |
| 5 | * systems management software to perform System Management Interrupts (SMIs) |
| 6 | * and Host Control Actions (power cycle or power off after OS shutdown) on |
| 7 | * Dell systems. |
| 8 | * |
| 9 | * See Documentation/dcdbas.txt for more information. |
| 10 | * |
Doug Warzecha | b95936c | 2006-10-19 23:29:09 -0700 | [diff] [blame] | 11 | * Copyright (C) 1995-2006 Dell Inc. |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License v2.0 as published by |
| 15 | * the Free Software Foundation. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | */ |
| 22 | |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 23 | #include <linux/platform_device.h> |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 24 | #include <linux/dma-mapping.h> |
| 25 | #include <linux/errno.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/gfp.h> |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 27 | #include <linux/init.h> |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/mc146818rtc.h> |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/reboot.h> |
| 32 | #include <linux/sched.h> |
| 33 | #include <linux/smp.h> |
| 34 | #include <linux/spinlock.h> |
| 35 | #include <linux/string.h> |
| 36 | #include <linux/types.h> |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 37 | #include <linux/mutex.h> |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 38 | #include <asm/io.h> |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 39 | |
| 40 | #include "dcdbas.h" |
| 41 | |
| 42 | #define DRIVER_NAME "dcdbas" |
Doug Warzecha | b95936c | 2006-10-19 23:29:09 -0700 | [diff] [blame] | 43 | #define DRIVER_VERSION "5.6.0-3.2" |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 44 | #define DRIVER_DESCRIPTION "Dell Systems Management Base Driver" |
| 45 | |
| 46 | static struct platform_device *dcdbas_pdev; |
| 47 | |
| 48 | static u8 *smi_data_buf; |
| 49 | static dma_addr_t smi_data_buf_handle; |
| 50 | static unsigned long smi_data_buf_size; |
| 51 | static u32 smi_data_buf_phys_addr; |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 52 | static DEFINE_MUTEX(smi_data_lock); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 53 | |
| 54 | static unsigned int host_control_action; |
| 55 | static unsigned int host_control_smi_type; |
| 56 | static unsigned int host_control_on_shutdown; |
| 57 | |
| 58 | /** |
| 59 | * smi_data_buf_free: free SMI data buffer |
| 60 | */ |
| 61 | static void smi_data_buf_free(void) |
| 62 | { |
| 63 | if (!smi_data_buf) |
| 64 | return; |
| 65 | |
| 66 | dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n", |
Harvey Harrison | eecd585 | 2008-04-29 00:59:19 -0700 | [diff] [blame] | 67 | __func__, smi_data_buf_phys_addr, smi_data_buf_size); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 68 | |
| 69 | dma_free_coherent(&dcdbas_pdev->dev, smi_data_buf_size, smi_data_buf, |
| 70 | smi_data_buf_handle); |
| 71 | smi_data_buf = NULL; |
| 72 | smi_data_buf_handle = 0; |
| 73 | smi_data_buf_phys_addr = 0; |
| 74 | smi_data_buf_size = 0; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * smi_data_buf_realloc: grow SMI data buffer if needed |
| 79 | */ |
| 80 | static int smi_data_buf_realloc(unsigned long size) |
| 81 | { |
| 82 | void *buf; |
| 83 | dma_addr_t handle; |
| 84 | |
| 85 | if (smi_data_buf_size >= size) |
| 86 | return 0; |
| 87 | |
| 88 | if (size > MAX_SMI_DATA_BUF_SIZE) |
| 89 | return -EINVAL; |
| 90 | |
| 91 | /* new buffer is needed */ |
| 92 | buf = dma_alloc_coherent(&dcdbas_pdev->dev, size, &handle, GFP_KERNEL); |
| 93 | if (!buf) { |
| 94 | dev_dbg(&dcdbas_pdev->dev, |
| 95 | "%s: failed to allocate memory size %lu\n", |
Harvey Harrison | eecd585 | 2008-04-29 00:59:19 -0700 | [diff] [blame] | 96 | __func__, size); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 97 | return -ENOMEM; |
| 98 | } |
| 99 | /* memory zeroed by dma_alloc_coherent */ |
| 100 | |
| 101 | if (smi_data_buf) |
| 102 | memcpy(buf, smi_data_buf, smi_data_buf_size); |
| 103 | |
| 104 | /* free any existing buffer */ |
| 105 | smi_data_buf_free(); |
| 106 | |
| 107 | /* set up new buffer for use */ |
| 108 | smi_data_buf = buf; |
| 109 | smi_data_buf_handle = handle; |
| 110 | smi_data_buf_phys_addr = (u32) virt_to_phys(buf); |
| 111 | smi_data_buf_size = size; |
| 112 | |
| 113 | dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n", |
Harvey Harrison | eecd585 | 2008-04-29 00:59:19 -0700 | [diff] [blame] | 114 | __func__, smi_data_buf_phys_addr, smi_data_buf_size); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static ssize_t smi_data_buf_phys_addr_show(struct device *dev, |
| 120 | struct device_attribute *attr, |
| 121 | char *buf) |
| 122 | { |
| 123 | return sprintf(buf, "%x\n", smi_data_buf_phys_addr); |
| 124 | } |
| 125 | |
| 126 | static ssize_t smi_data_buf_size_show(struct device *dev, |
| 127 | struct device_attribute *attr, |
| 128 | char *buf) |
| 129 | { |
| 130 | return sprintf(buf, "%lu\n", smi_data_buf_size); |
| 131 | } |
| 132 | |
| 133 | static ssize_t smi_data_buf_size_store(struct device *dev, |
| 134 | struct device_attribute *attr, |
| 135 | const char *buf, size_t count) |
| 136 | { |
| 137 | unsigned long buf_size; |
| 138 | ssize_t ret; |
| 139 | |
| 140 | buf_size = simple_strtoul(buf, NULL, 10); |
| 141 | |
| 142 | /* make sure SMI data buffer is at least buf_size */ |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 143 | mutex_lock(&smi_data_lock); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 144 | ret = smi_data_buf_realloc(buf_size); |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 145 | mutex_unlock(&smi_data_lock); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 146 | if (ret) |
| 147 | return ret; |
| 148 | |
| 149 | return count; |
| 150 | } |
| 151 | |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 152 | static ssize_t smi_data_read(struct file *filp, struct kobject *kobj, |
Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 153 | struct bin_attribute *bin_attr, |
| 154 | char *buf, loff_t pos, size_t count) |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 155 | { |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 156 | ssize_t ret; |
| 157 | |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 158 | mutex_lock(&smi_data_lock); |
Akinobu Mita | abe19b7 | 2008-07-25 01:48:24 -0700 | [diff] [blame] | 159 | ret = memory_read_from_buffer(buf, count, &pos, smi_data_buf, |
| 160 | smi_data_buf_size); |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 161 | mutex_unlock(&smi_data_lock); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 162 | return ret; |
| 163 | } |
| 164 | |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 165 | static ssize_t smi_data_write(struct file *filp, struct kobject *kobj, |
Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 166 | struct bin_attribute *bin_attr, |
| 167 | char *buf, loff_t pos, size_t count) |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 168 | { |
| 169 | ssize_t ret; |
| 170 | |
Doug Warzecha | b95936c | 2006-10-19 23:29:09 -0700 | [diff] [blame] | 171 | if ((pos + count) > MAX_SMI_DATA_BUF_SIZE) |
| 172 | return -EINVAL; |
| 173 | |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 174 | mutex_lock(&smi_data_lock); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 175 | |
| 176 | ret = smi_data_buf_realloc(pos + count); |
| 177 | if (ret) |
| 178 | goto out; |
| 179 | |
| 180 | memcpy(smi_data_buf + pos, buf, count); |
| 181 | ret = count; |
| 182 | out: |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 183 | mutex_unlock(&smi_data_lock); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 184 | return ret; |
| 185 | } |
| 186 | |
| 187 | static ssize_t host_control_action_show(struct device *dev, |
| 188 | struct device_attribute *attr, |
| 189 | char *buf) |
| 190 | { |
| 191 | return sprintf(buf, "%u\n", host_control_action); |
| 192 | } |
| 193 | |
| 194 | static ssize_t host_control_action_store(struct device *dev, |
| 195 | struct device_attribute *attr, |
| 196 | const char *buf, size_t count) |
| 197 | { |
| 198 | ssize_t ret; |
| 199 | |
| 200 | /* make sure buffer is available for host control command */ |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 201 | mutex_lock(&smi_data_lock); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 202 | ret = smi_data_buf_realloc(sizeof(struct apm_cmd)); |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 203 | mutex_unlock(&smi_data_lock); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 204 | if (ret) |
| 205 | return ret; |
| 206 | |
| 207 | host_control_action = simple_strtoul(buf, NULL, 10); |
| 208 | return count; |
| 209 | } |
| 210 | |
| 211 | static ssize_t host_control_smi_type_show(struct device *dev, |
| 212 | struct device_attribute *attr, |
| 213 | char *buf) |
| 214 | { |
| 215 | return sprintf(buf, "%u\n", host_control_smi_type); |
| 216 | } |
| 217 | |
| 218 | static ssize_t host_control_smi_type_store(struct device *dev, |
| 219 | struct device_attribute *attr, |
| 220 | const char *buf, size_t count) |
| 221 | { |
| 222 | host_control_smi_type = simple_strtoul(buf, NULL, 10); |
| 223 | return count; |
| 224 | } |
| 225 | |
| 226 | static ssize_t host_control_on_shutdown_show(struct device *dev, |
| 227 | struct device_attribute *attr, |
| 228 | char *buf) |
| 229 | { |
| 230 | return sprintf(buf, "%u\n", host_control_on_shutdown); |
| 231 | } |
| 232 | |
| 233 | static ssize_t host_control_on_shutdown_store(struct device *dev, |
| 234 | struct device_attribute *attr, |
| 235 | const char *buf, size_t count) |
| 236 | { |
| 237 | host_control_on_shutdown = simple_strtoul(buf, NULL, 10); |
| 238 | return count; |
| 239 | } |
| 240 | |
| 241 | /** |
Matthew Garrett | 3cab7fd | 2009-01-07 18:08:54 -0800 | [diff] [blame] | 242 | * dcdbas_smi_request: generate SMI request |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 243 | * |
| 244 | * Called with smi_data_lock. |
| 245 | */ |
Matthew Garrett | 3cab7fd | 2009-01-07 18:08:54 -0800 | [diff] [blame] | 246 | int dcdbas_smi_request(struct smi_cmd *smi_cmd) |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 247 | { |
Mike Travis | c90e785 | 2009-01-10 21:58:10 -0800 | [diff] [blame] | 248 | cpumask_var_t old_mask; |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 249 | int ret = 0; |
| 250 | |
| 251 | if (smi_cmd->magic != SMI_CMD_MAGIC) { |
| 252 | dev_info(&dcdbas_pdev->dev, "%s: invalid magic value\n", |
Harvey Harrison | eecd585 | 2008-04-29 00:59:19 -0700 | [diff] [blame] | 253 | __func__); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 254 | return -EBADR; |
| 255 | } |
| 256 | |
| 257 | /* SMI requires CPU 0 */ |
Mike Travis | c90e785 | 2009-01-10 21:58:10 -0800 | [diff] [blame] | 258 | if (!alloc_cpumask_var(&old_mask, GFP_KERNEL)) |
| 259 | return -ENOMEM; |
| 260 | |
| 261 | cpumask_copy(old_mask, ¤t->cpus_allowed); |
| 262 | set_cpus_allowed_ptr(current, cpumask_of(0)); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 263 | if (smp_processor_id() != 0) { |
| 264 | dev_dbg(&dcdbas_pdev->dev, "%s: failed to get CPU 0\n", |
Harvey Harrison | eecd585 | 2008-04-29 00:59:19 -0700 | [diff] [blame] | 265 | __func__); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 266 | ret = -EBUSY; |
| 267 | goto out; |
| 268 | } |
| 269 | |
| 270 | /* generate SMI */ |
Stuart Hayes | dd65c73 | 2011-03-02 13:42:05 +0100 | [diff] [blame] | 271 | /* inb to force posted write through and make SMI happen now */ |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 272 | asm volatile ( |
Stuart Hayes | dd65c73 | 2011-03-02 13:42:05 +0100 | [diff] [blame] | 273 | "outb %b0,%w1\n" |
| 274 | "inb %w1" |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 275 | : /* no output args */ |
| 276 | : "a" (smi_cmd->command_code), |
| 277 | "d" (smi_cmd->command_address), |
| 278 | "b" (smi_cmd->ebx), |
| 279 | "c" (smi_cmd->ecx) |
| 280 | : "memory" |
| 281 | ); |
| 282 | |
| 283 | out: |
Mike Travis | c90e785 | 2009-01-10 21:58:10 -0800 | [diff] [blame] | 284 | set_cpus_allowed_ptr(current, old_mask); |
| 285 | free_cpumask_var(old_mask); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * smi_request_store: |
| 291 | * |
| 292 | * The valid values are: |
| 293 | * 0: zero SMI data buffer |
| 294 | * 1: generate calling interface SMI |
| 295 | * 2: generate raw SMI |
| 296 | * |
| 297 | * User application writes smi_cmd to smi_data before telling driver |
| 298 | * to generate SMI. |
| 299 | */ |
| 300 | static ssize_t smi_request_store(struct device *dev, |
| 301 | struct device_attribute *attr, |
| 302 | const char *buf, size_t count) |
| 303 | { |
| 304 | struct smi_cmd *smi_cmd; |
| 305 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 306 | ssize_t ret; |
| 307 | |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 308 | mutex_lock(&smi_data_lock); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 309 | |
| 310 | if (smi_data_buf_size < sizeof(struct smi_cmd)) { |
| 311 | ret = -ENODEV; |
| 312 | goto out; |
| 313 | } |
| 314 | smi_cmd = (struct smi_cmd *)smi_data_buf; |
| 315 | |
| 316 | switch (val) { |
| 317 | case 2: |
| 318 | /* Raw SMI */ |
Matthew Garrett | 3cab7fd | 2009-01-07 18:08:54 -0800 | [diff] [blame] | 319 | ret = dcdbas_smi_request(smi_cmd); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 320 | if (!ret) |
| 321 | ret = count; |
| 322 | break; |
| 323 | case 1: |
| 324 | /* Calling Interface SMI */ |
| 325 | smi_cmd->ebx = (u32) virt_to_phys(smi_cmd->command_buffer); |
Matthew Garrett | 3cab7fd | 2009-01-07 18:08:54 -0800 | [diff] [blame] | 326 | ret = dcdbas_smi_request(smi_cmd); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 327 | if (!ret) |
| 328 | ret = count; |
| 329 | break; |
| 330 | case 0: |
| 331 | memset(smi_data_buf, 0, smi_data_buf_size); |
| 332 | ret = count; |
| 333 | break; |
| 334 | default: |
| 335 | ret = -EINVAL; |
| 336 | break; |
| 337 | } |
| 338 | |
| 339 | out: |
Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 340 | mutex_unlock(&smi_data_lock); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 341 | return ret; |
| 342 | } |
Matthew Garrett | 3cab7fd | 2009-01-07 18:08:54 -0800 | [diff] [blame] | 343 | EXPORT_SYMBOL(dcdbas_smi_request); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 344 | |
| 345 | /** |
| 346 | * host_control_smi: generate host control SMI |
| 347 | * |
| 348 | * Caller must set up the host control command in smi_data_buf. |
| 349 | */ |
| 350 | static int host_control_smi(void) |
| 351 | { |
| 352 | struct apm_cmd *apm_cmd; |
| 353 | u8 *data; |
| 354 | unsigned long flags; |
| 355 | u32 num_ticks; |
| 356 | s8 cmd_status; |
| 357 | u8 index; |
| 358 | |
| 359 | apm_cmd = (struct apm_cmd *)smi_data_buf; |
| 360 | apm_cmd->status = ESM_STATUS_CMD_UNSUCCESSFUL; |
| 361 | |
| 362 | switch (host_control_smi_type) { |
| 363 | case HC_SMITYPE_TYPE1: |
| 364 | spin_lock_irqsave(&rtc_lock, flags); |
| 365 | /* write SMI data buffer physical address */ |
| 366 | data = (u8 *)&smi_data_buf_phys_addr; |
| 367 | for (index = PE1300_CMOS_CMD_STRUCT_PTR; |
| 368 | index < (PE1300_CMOS_CMD_STRUCT_PTR + 4); |
| 369 | index++, data++) { |
| 370 | outb(index, |
| 371 | (CMOS_BASE_PORT + CMOS_PAGE2_INDEX_PORT_PIIX4)); |
| 372 | outb(*data, |
| 373 | (CMOS_BASE_PORT + CMOS_PAGE2_DATA_PORT_PIIX4)); |
| 374 | } |
| 375 | |
| 376 | /* first set status to -1 as called by spec */ |
| 377 | cmd_status = ESM_STATUS_CMD_UNSUCCESSFUL; |
| 378 | outb((u8) cmd_status, PCAT_APM_STATUS_PORT); |
| 379 | |
| 380 | /* generate SMM call */ |
| 381 | outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT); |
| 382 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 383 | |
| 384 | /* wait a few to see if it executed */ |
| 385 | num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING; |
| 386 | while ((cmd_status = inb(PCAT_APM_STATUS_PORT)) |
| 387 | == ESM_STATUS_CMD_UNSUCCESSFUL) { |
| 388 | num_ticks--; |
| 389 | if (num_ticks == EXPIRED_TIMER) |
| 390 | return -ETIME; |
| 391 | } |
| 392 | break; |
| 393 | |
| 394 | case HC_SMITYPE_TYPE2: |
| 395 | case HC_SMITYPE_TYPE3: |
| 396 | spin_lock_irqsave(&rtc_lock, flags); |
| 397 | /* write SMI data buffer physical address */ |
| 398 | data = (u8 *)&smi_data_buf_phys_addr; |
| 399 | for (index = PE1400_CMOS_CMD_STRUCT_PTR; |
| 400 | index < (PE1400_CMOS_CMD_STRUCT_PTR + 4); |
| 401 | index++, data++) { |
| 402 | outb(index, (CMOS_BASE_PORT + CMOS_PAGE1_INDEX_PORT)); |
| 403 | outb(*data, (CMOS_BASE_PORT + CMOS_PAGE1_DATA_PORT)); |
| 404 | } |
| 405 | |
| 406 | /* generate SMM call */ |
| 407 | if (host_control_smi_type == HC_SMITYPE_TYPE3) |
| 408 | outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT); |
| 409 | else |
| 410 | outb(ESM_APM_CMD, PE1400_APM_CONTROL_PORT); |
| 411 | |
| 412 | /* restore RTC index pointer since it was written to above */ |
| 413 | CMOS_READ(RTC_REG_C); |
| 414 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 415 | |
| 416 | /* read control port back to serialize write */ |
| 417 | cmd_status = inb(PE1400_APM_CONTROL_PORT); |
| 418 | |
| 419 | /* wait a few to see if it executed */ |
| 420 | num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING; |
| 421 | while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) { |
| 422 | num_ticks--; |
| 423 | if (num_ticks == EXPIRED_TIMER) |
| 424 | return -ETIME; |
| 425 | } |
| 426 | break; |
| 427 | |
| 428 | default: |
| 429 | dev_dbg(&dcdbas_pdev->dev, "%s: invalid SMI type %u\n", |
Harvey Harrison | eecd585 | 2008-04-29 00:59:19 -0700 | [diff] [blame] | 430 | __func__, host_control_smi_type); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 431 | return -ENOSYS; |
| 432 | } |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * dcdbas_host_control: initiate host control |
| 439 | * |
| 440 | * This function is called by the driver after the system has |
| 441 | * finished shutting down if the user application specified a |
| 442 | * host control action to perform on shutdown. It is safe to |
| 443 | * use smi_data_buf at this point because the system has finished |
| 444 | * shutting down and no userspace apps are running. |
| 445 | */ |
| 446 | static void dcdbas_host_control(void) |
| 447 | { |
| 448 | struct apm_cmd *apm_cmd; |
| 449 | u8 action; |
| 450 | |
| 451 | if (host_control_action == HC_ACTION_NONE) |
| 452 | return; |
| 453 | |
| 454 | action = host_control_action; |
| 455 | host_control_action = HC_ACTION_NONE; |
| 456 | |
| 457 | if (!smi_data_buf) { |
Harvey Harrison | eecd585 | 2008-04-29 00:59:19 -0700 | [diff] [blame] | 458 | dev_dbg(&dcdbas_pdev->dev, "%s: no SMI buffer\n", __func__); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 459 | return; |
| 460 | } |
| 461 | |
| 462 | if (smi_data_buf_size < sizeof(struct apm_cmd)) { |
| 463 | dev_dbg(&dcdbas_pdev->dev, "%s: SMI buffer too small\n", |
Harvey Harrison | eecd585 | 2008-04-29 00:59:19 -0700 | [diff] [blame] | 464 | __func__); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 465 | return; |
| 466 | } |
| 467 | |
| 468 | apm_cmd = (struct apm_cmd *)smi_data_buf; |
| 469 | |
| 470 | /* power off takes precedence */ |
| 471 | if (action & HC_ACTION_HOST_CONTROL_POWEROFF) { |
| 472 | apm_cmd->command = ESM_APM_POWER_CYCLE; |
| 473 | apm_cmd->reserved = 0; |
| 474 | *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 0; |
| 475 | host_control_smi(); |
| 476 | } else if (action & HC_ACTION_HOST_CONTROL_POWERCYCLE) { |
| 477 | apm_cmd->command = ESM_APM_POWER_CYCLE; |
| 478 | apm_cmd->reserved = 0; |
| 479 | *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 20; |
| 480 | host_control_smi(); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * dcdbas_reboot_notify: handle reboot notification for host control |
| 486 | */ |
| 487 | static int dcdbas_reboot_notify(struct notifier_block *nb, unsigned long code, |
| 488 | void *unused) |
| 489 | { |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 490 | switch (code) { |
| 491 | case SYS_DOWN: |
| 492 | case SYS_HALT: |
| 493 | case SYS_POWER_OFF: |
| 494 | if (host_control_on_shutdown) { |
| 495 | /* firmware is going to perform host control action */ |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 496 | printk(KERN_WARNING "Please wait for shutdown " |
| 497 | "action to complete...\n"); |
| 498 | dcdbas_host_control(); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 499 | } |
| 500 | break; |
| 501 | } |
| 502 | |
| 503 | return NOTIFY_DONE; |
| 504 | } |
| 505 | |
| 506 | static struct notifier_block dcdbas_reboot_nb = { |
| 507 | .notifier_call = dcdbas_reboot_notify, |
| 508 | .next = NULL, |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 509 | .priority = INT_MIN |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 510 | }; |
| 511 | |
| 512 | static DCDBAS_BIN_ATTR_RW(smi_data); |
| 513 | |
| 514 | static struct bin_attribute *dcdbas_bin_attrs[] = { |
| 515 | &bin_attr_smi_data, |
| 516 | NULL |
| 517 | }; |
| 518 | |
| 519 | static DCDBAS_DEV_ATTR_RW(smi_data_buf_size); |
| 520 | static DCDBAS_DEV_ATTR_RO(smi_data_buf_phys_addr); |
| 521 | static DCDBAS_DEV_ATTR_WO(smi_request); |
| 522 | static DCDBAS_DEV_ATTR_RW(host_control_action); |
| 523 | static DCDBAS_DEV_ATTR_RW(host_control_smi_type); |
| 524 | static DCDBAS_DEV_ATTR_RW(host_control_on_shutdown); |
| 525 | |
Dmitry Torokhov | a7f3ea7 | 2006-03-22 00:07:56 -0800 | [diff] [blame] | 526 | static struct attribute *dcdbas_dev_attrs[] = { |
| 527 | &dev_attr_smi_data_buf_size.attr, |
| 528 | &dev_attr_smi_data_buf_phys_addr.attr, |
| 529 | &dev_attr_smi_request.attr, |
| 530 | &dev_attr_host_control_action.attr, |
| 531 | &dev_attr_host_control_smi_type.attr, |
| 532 | &dev_attr_host_control_on_shutdown.attr, |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 533 | NULL |
| 534 | }; |
| 535 | |
Dmitry Torokhov | a7f3ea7 | 2006-03-22 00:07:56 -0800 | [diff] [blame] | 536 | static struct attribute_group dcdbas_attr_group = { |
| 537 | .attrs = dcdbas_dev_attrs, |
Greg KH | 4c33dea | 2013-08-20 17:17:57 -0700 | [diff] [blame] | 538 | .bin_attrs = dcdbas_bin_attrs, |
Dmitry Torokhov | a7f3ea7 | 2006-03-22 00:07:56 -0800 | [diff] [blame] | 539 | }; |
| 540 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 541 | static int dcdbas_probe(struct platform_device *dev) |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 542 | { |
Greg KH | 4c33dea | 2013-08-20 17:17:57 -0700 | [diff] [blame] | 543 | int error; |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 544 | |
| 545 | host_control_action = HC_ACTION_NONE; |
| 546 | host_control_smi_type = HC_SMITYPE_NONE; |
| 547 | |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 548 | /* |
| 549 | * BIOS SMI calls require buffer addresses be in 32-bit address space. |
| 550 | * This is done by setting the DMA mask below. |
| 551 | */ |
Yang Hongyang | 284901a | 2009-04-06 19:01:15 -0700 | [diff] [blame] | 552 | dcdbas_pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 553 | dcdbas_pdev->dev.dma_mask = &dcdbas_pdev->dev.coherent_dma_mask; |
| 554 | |
Dmitry Torokhov | a7f3ea7 | 2006-03-22 00:07:56 -0800 | [diff] [blame] | 555 | error = sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group); |
| 556 | if (error) |
| 557 | return error; |
| 558 | |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 559 | register_reboot_notifier(&dcdbas_reboot_nb); |
| 560 | |
Dmitry Torokhov | a7f3ea7 | 2006-03-22 00:07:56 -0800 | [diff] [blame] | 561 | dev_info(&dev->dev, "%s (version %s)\n", |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 562 | DRIVER_DESCRIPTION, DRIVER_VERSION); |
| 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 567 | static int dcdbas_remove(struct platform_device *dev) |
Dmitry Torokhov | a7f3ea7 | 2006-03-22 00:07:56 -0800 | [diff] [blame] | 568 | { |
Dmitry Torokhov | a7f3ea7 | 2006-03-22 00:07:56 -0800 | [diff] [blame] | 569 | unregister_reboot_notifier(&dcdbas_reboot_nb); |
Dmitry Torokhov | a7f3ea7 | 2006-03-22 00:07:56 -0800 | [diff] [blame] | 570 | sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group); |
| 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | static struct platform_driver dcdbas_driver = { |
| 576 | .driver = { |
| 577 | .name = DRIVER_NAME, |
| 578 | .owner = THIS_MODULE, |
| 579 | }, |
| 580 | .probe = dcdbas_probe, |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 581 | .remove = dcdbas_remove, |
Dmitry Torokhov | a7f3ea7 | 2006-03-22 00:07:56 -0800 | [diff] [blame] | 582 | }; |
| 583 | |
| 584 | /** |
| 585 | * dcdbas_init: initialize driver |
| 586 | */ |
| 587 | static int __init dcdbas_init(void) |
| 588 | { |
| 589 | int error; |
| 590 | |
| 591 | error = platform_driver_register(&dcdbas_driver); |
| 592 | if (error) |
| 593 | return error; |
| 594 | |
| 595 | dcdbas_pdev = platform_device_alloc(DRIVER_NAME, -1); |
| 596 | if (!dcdbas_pdev) { |
| 597 | error = -ENOMEM; |
| 598 | goto err_unregister_driver; |
| 599 | } |
| 600 | |
| 601 | error = platform_device_add(dcdbas_pdev); |
| 602 | if (error) |
| 603 | goto err_free_device; |
| 604 | |
| 605 | return 0; |
| 606 | |
| 607 | err_free_device: |
| 608 | platform_device_put(dcdbas_pdev); |
| 609 | err_unregister_driver: |
| 610 | platform_driver_unregister(&dcdbas_driver); |
| 611 | return error; |
| 612 | } |
| 613 | |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 614 | /** |
| 615 | * dcdbas_exit: perform driver cleanup |
| 616 | */ |
| 617 | static void __exit dcdbas_exit(void) |
| 618 | { |
Doug Warzecha | 435a80f | 2006-03-09 17:33:35 -0800 | [diff] [blame] | 619 | /* |
| 620 | * make sure functions that use dcdbas_pdev are called |
| 621 | * before platform_device_unregister |
| 622 | */ |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 623 | unregister_reboot_notifier(&dcdbas_reboot_nb); |
Dmitry Torokhov | a7f3ea7 | 2006-03-22 00:07:56 -0800 | [diff] [blame] | 624 | |
| 625 | /* |
| 626 | * We have to free the buffer here instead of dcdbas_remove |
| 627 | * because only in module exit function we can be sure that |
| 628 | * all sysfs attributes belonging to this module have been |
| 629 | * released. |
| 630 | */ |
| 631 | smi_data_buf_free(); |
Axel Lin | e3ed249 | 2010-07-05 09:35:47 +0800 | [diff] [blame] | 632 | platform_device_unregister(dcdbas_pdev); |
| 633 | platform_driver_unregister(&dcdbas_driver); |
Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | module_init(dcdbas_init); |
| 637 | module_exit(dcdbas_exit); |
| 638 | |
| 639 | MODULE_DESCRIPTION(DRIVER_DESCRIPTION " (version " DRIVER_VERSION ")"); |
| 640 | MODULE_VERSION(DRIVER_VERSION); |
| 641 | MODULE_AUTHOR("Dell Inc."); |
| 642 | MODULE_LICENSE("GPL"); |
Matt Domsch | 8f47f0b | 2008-02-06 01:36:24 -0800 | [diff] [blame] | 643 | /* Any System or BIOS claiming to be by Dell */ |
| 644 | MODULE_ALIAS("dmi:*:[bs]vnD[Ee][Ll][Ll]*:*"); |