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