| 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 |  * | 
 | 11 |  *  Copyright (C) 1995-2005 Dell Inc. | 
 | 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> | 
 | 26 | #include <linux/init.h> | 
 | 27 | #include <linux/kernel.h> | 
 | 28 | #include <linux/mc146818rtc.h> | 
 | 29 | #include <linux/module.h> | 
 | 30 | #include <linux/reboot.h> | 
 | 31 | #include <linux/sched.h> | 
 | 32 | #include <linux/smp.h> | 
 | 33 | #include <linux/spinlock.h> | 
 | 34 | #include <linux/string.h> | 
 | 35 | #include <linux/types.h> | 
| Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 36 | #include <linux/mutex.h> | 
| Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 37 | #include <asm/io.h> | 
 | 38 | #include <asm/semaphore.h> | 
 | 39 |  | 
 | 40 | #include "dcdbas.h" | 
 | 41 |  | 
 | 42 | #define DRIVER_NAME		"dcdbas" | 
| Doug Warzecha | 435a80f | 2006-03-09 17:33:35 -0800 | [diff] [blame] | 43 | #define DRIVER_VERSION		"5.6.0-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", | 
 | 67 | 		__FUNCTION__, smi_data_buf_phys_addr, smi_data_buf_size); | 
 | 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", | 
 | 96 | 			__FUNCTION__, size); | 
 | 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", | 
 | 114 | 		__FUNCTION__, smi_data_buf_phys_addr, smi_data_buf_size); | 
 | 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 |  | 
 | 152 | static ssize_t smi_data_read(struct kobject *kobj, char *buf, loff_t pos, | 
 | 153 | 			     size_t count) | 
 | 154 | { | 
 | 155 | 	size_t max_read; | 
 | 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); | 
| Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 159 |  | 
 | 160 | 	if (pos >= smi_data_buf_size) { | 
 | 161 | 		ret = 0; | 
 | 162 | 		goto out; | 
 | 163 | 	} | 
 | 164 |  | 
 | 165 | 	max_read = smi_data_buf_size - pos; | 
 | 166 | 	ret = min(max_read, count); | 
 | 167 | 	memcpy(buf, smi_data_buf + pos, ret); | 
 | 168 | out: | 
| Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 169 | 	mutex_unlock(&smi_data_lock); | 
| Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 170 | 	return ret; | 
 | 171 | } | 
 | 172 |  | 
 | 173 | static ssize_t smi_data_write(struct kobject *kobj, char *buf, loff_t pos, | 
 | 174 | 			      size_t count) | 
 | 175 | { | 
 | 176 | 	ssize_t ret; | 
 | 177 |  | 
| Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 178 | 	mutex_lock(&smi_data_lock); | 
| Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 179 |  | 
 | 180 | 	ret = smi_data_buf_realloc(pos + count); | 
 | 181 | 	if (ret) | 
 | 182 | 		goto out; | 
 | 183 |  | 
 | 184 | 	memcpy(smi_data_buf + pos, buf, count); | 
 | 185 | 	ret = count; | 
 | 186 | out: | 
| Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 187 | 	mutex_unlock(&smi_data_lock); | 
| Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 188 | 	return ret; | 
 | 189 | } | 
 | 190 |  | 
 | 191 | static ssize_t host_control_action_show(struct device *dev, | 
 | 192 | 					struct device_attribute *attr, | 
 | 193 | 					char *buf) | 
 | 194 | { | 
 | 195 | 	return sprintf(buf, "%u\n", host_control_action); | 
 | 196 | } | 
 | 197 |  | 
 | 198 | static ssize_t host_control_action_store(struct device *dev, | 
 | 199 | 					 struct device_attribute *attr, | 
 | 200 | 					 const char *buf, size_t count) | 
 | 201 | { | 
 | 202 | 	ssize_t ret; | 
 | 203 |  | 
 | 204 | 	/* make sure buffer is available for host control command */ | 
| Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 205 | 	mutex_lock(&smi_data_lock); | 
| Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 206 | 	ret = smi_data_buf_realloc(sizeof(struct apm_cmd)); | 
| Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 207 | 	mutex_unlock(&smi_data_lock); | 
| Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 208 | 	if (ret) | 
 | 209 | 		return ret; | 
 | 210 |  | 
 | 211 | 	host_control_action = simple_strtoul(buf, NULL, 10); | 
 | 212 | 	return count; | 
 | 213 | } | 
 | 214 |  | 
 | 215 | static ssize_t host_control_smi_type_show(struct device *dev, | 
 | 216 | 					  struct device_attribute *attr, | 
 | 217 | 					  char *buf) | 
 | 218 | { | 
 | 219 | 	return sprintf(buf, "%u\n", host_control_smi_type); | 
 | 220 | } | 
 | 221 |  | 
 | 222 | static ssize_t host_control_smi_type_store(struct device *dev, | 
 | 223 | 					   struct device_attribute *attr, | 
 | 224 | 					   const char *buf, size_t count) | 
 | 225 | { | 
 | 226 | 	host_control_smi_type = simple_strtoul(buf, NULL, 10); | 
 | 227 | 	return count; | 
 | 228 | } | 
 | 229 |  | 
 | 230 | static ssize_t host_control_on_shutdown_show(struct device *dev, | 
 | 231 | 					     struct device_attribute *attr, | 
 | 232 | 					     char *buf) | 
 | 233 | { | 
 | 234 | 	return sprintf(buf, "%u\n", host_control_on_shutdown); | 
 | 235 | } | 
 | 236 |  | 
 | 237 | static ssize_t host_control_on_shutdown_store(struct device *dev, | 
 | 238 | 					      struct device_attribute *attr, | 
 | 239 | 					      const char *buf, size_t count) | 
 | 240 | { | 
 | 241 | 	host_control_on_shutdown = simple_strtoul(buf, NULL, 10); | 
 | 242 | 	return count; | 
 | 243 | } | 
 | 244 |  | 
 | 245 | /** | 
 | 246 |  * smi_request: generate SMI request | 
 | 247 |  * | 
 | 248 |  * Called with smi_data_lock. | 
 | 249 |  */ | 
 | 250 | static int smi_request(struct smi_cmd *smi_cmd) | 
 | 251 | { | 
 | 252 | 	cpumask_t old_mask; | 
 | 253 | 	int ret = 0; | 
 | 254 |  | 
 | 255 | 	if (smi_cmd->magic != SMI_CMD_MAGIC) { | 
 | 256 | 		dev_info(&dcdbas_pdev->dev, "%s: invalid magic value\n", | 
 | 257 | 			 __FUNCTION__); | 
 | 258 | 		return -EBADR; | 
 | 259 | 	} | 
 | 260 |  | 
 | 261 | 	/* SMI requires CPU 0 */ | 
 | 262 | 	old_mask = current->cpus_allowed; | 
 | 263 | 	set_cpus_allowed(current, cpumask_of_cpu(0)); | 
 | 264 | 	if (smp_processor_id() != 0) { | 
 | 265 | 		dev_dbg(&dcdbas_pdev->dev, "%s: failed to get CPU 0\n", | 
 | 266 | 			__FUNCTION__); | 
 | 267 | 		ret = -EBUSY; | 
 | 268 | 		goto out; | 
 | 269 | 	} | 
 | 270 |  | 
 | 271 | 	/* generate SMI */ | 
 | 272 | 	asm volatile ( | 
 | 273 | 		"outb %b0,%w1" | 
 | 274 | 		: /* no output args */ | 
 | 275 | 		: "a" (smi_cmd->command_code), | 
 | 276 | 		  "d" (smi_cmd->command_address), | 
 | 277 | 		  "b" (smi_cmd->ebx), | 
 | 278 | 		  "c" (smi_cmd->ecx) | 
 | 279 | 		: "memory" | 
 | 280 | 	); | 
 | 281 |  | 
 | 282 | out: | 
 | 283 | 	set_cpus_allowed(current, old_mask); | 
 | 284 | 	return ret; | 
 | 285 | } | 
 | 286 |  | 
 | 287 | /** | 
 | 288 |  * smi_request_store: | 
 | 289 |  * | 
 | 290 |  * The valid values are: | 
 | 291 |  * 0: zero SMI data buffer | 
 | 292 |  * 1: generate calling interface SMI | 
 | 293 |  * 2: generate raw SMI | 
 | 294 |  * | 
 | 295 |  * User application writes smi_cmd to smi_data before telling driver | 
 | 296 |  * to generate SMI. | 
 | 297 |  */ | 
 | 298 | static ssize_t smi_request_store(struct device *dev, | 
 | 299 | 				 struct device_attribute *attr, | 
 | 300 | 				 const char *buf, size_t count) | 
 | 301 | { | 
 | 302 | 	struct smi_cmd *smi_cmd; | 
 | 303 | 	unsigned long val = simple_strtoul(buf, NULL, 10); | 
 | 304 | 	ssize_t ret; | 
 | 305 |  | 
| Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 306 | 	mutex_lock(&smi_data_lock); | 
| Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 307 |  | 
 | 308 | 	if (smi_data_buf_size < sizeof(struct smi_cmd)) { | 
 | 309 | 		ret = -ENODEV; | 
 | 310 | 		goto out; | 
 | 311 | 	} | 
 | 312 | 	smi_cmd = (struct smi_cmd *)smi_data_buf; | 
 | 313 |  | 
 | 314 | 	switch (val) { | 
 | 315 | 	case 2: | 
 | 316 | 		/* Raw SMI */ | 
 | 317 | 		ret = smi_request(smi_cmd); | 
 | 318 | 		if (!ret) | 
 | 319 | 			ret = count; | 
 | 320 | 		break; | 
 | 321 | 	case 1: | 
 | 322 | 		/* Calling Interface SMI */ | 
 | 323 | 		smi_cmd->ebx = (u32) virt_to_phys(smi_cmd->command_buffer); | 
 | 324 | 		ret = smi_request(smi_cmd); | 
 | 325 | 		if (!ret) | 
 | 326 | 			ret = count; | 
 | 327 | 		break; | 
 | 328 | 	case 0: | 
 | 329 | 		memset(smi_data_buf, 0, smi_data_buf_size); | 
 | 330 | 		ret = count; | 
 | 331 | 		break; | 
 | 332 | 	default: | 
 | 333 | 		ret = -EINVAL; | 
 | 334 | 		break; | 
 | 335 | 	} | 
 | 336 |  | 
 | 337 | out: | 
| Arjan van de Ven | 8ed965d | 2006-03-23 03:00:21 -0800 | [diff] [blame] | 338 | 	mutex_unlock(&smi_data_lock); | 
| Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 339 | 	return ret; | 
 | 340 | } | 
 | 341 |  | 
 | 342 | /** | 
 | 343 |  * host_control_smi: generate host control SMI | 
 | 344 |  * | 
 | 345 |  * Caller must set up the host control command in smi_data_buf. | 
 | 346 |  */ | 
 | 347 | static int host_control_smi(void) | 
 | 348 | { | 
 | 349 | 	struct apm_cmd *apm_cmd; | 
 | 350 | 	u8 *data; | 
 | 351 | 	unsigned long flags; | 
 | 352 | 	u32 num_ticks; | 
 | 353 | 	s8 cmd_status; | 
 | 354 | 	u8 index; | 
 | 355 |  | 
 | 356 | 	apm_cmd = (struct apm_cmd *)smi_data_buf; | 
 | 357 | 	apm_cmd->status = ESM_STATUS_CMD_UNSUCCESSFUL; | 
 | 358 |  | 
 | 359 | 	switch (host_control_smi_type) { | 
 | 360 | 	case HC_SMITYPE_TYPE1: | 
 | 361 | 		spin_lock_irqsave(&rtc_lock, flags); | 
 | 362 | 		/* write SMI data buffer physical address */ | 
 | 363 | 		data = (u8 *)&smi_data_buf_phys_addr; | 
 | 364 | 		for (index = PE1300_CMOS_CMD_STRUCT_PTR; | 
 | 365 | 		     index < (PE1300_CMOS_CMD_STRUCT_PTR + 4); | 
 | 366 | 		     index++, data++) { | 
 | 367 | 			outb(index, | 
 | 368 | 			     (CMOS_BASE_PORT + CMOS_PAGE2_INDEX_PORT_PIIX4)); | 
 | 369 | 			outb(*data, | 
 | 370 | 			     (CMOS_BASE_PORT + CMOS_PAGE2_DATA_PORT_PIIX4)); | 
 | 371 | 		} | 
 | 372 |  | 
 | 373 | 		/* first set status to -1 as called by spec */ | 
 | 374 | 		cmd_status = ESM_STATUS_CMD_UNSUCCESSFUL; | 
 | 375 | 		outb((u8) cmd_status, PCAT_APM_STATUS_PORT); | 
 | 376 |  | 
 | 377 | 		/* generate SMM call */ | 
 | 378 | 		outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT); | 
 | 379 | 		spin_unlock_irqrestore(&rtc_lock, flags); | 
 | 380 |  | 
 | 381 | 		/* wait a few to see if it executed */ | 
 | 382 | 		num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING; | 
 | 383 | 		while ((cmd_status = inb(PCAT_APM_STATUS_PORT)) | 
 | 384 | 		       == ESM_STATUS_CMD_UNSUCCESSFUL) { | 
 | 385 | 			num_ticks--; | 
 | 386 | 			if (num_ticks == EXPIRED_TIMER) | 
 | 387 | 				return -ETIME; | 
 | 388 | 		} | 
 | 389 | 		break; | 
 | 390 |  | 
 | 391 | 	case HC_SMITYPE_TYPE2: | 
 | 392 | 	case HC_SMITYPE_TYPE3: | 
 | 393 | 		spin_lock_irqsave(&rtc_lock, flags); | 
 | 394 | 		/* write SMI data buffer physical address */ | 
 | 395 | 		data = (u8 *)&smi_data_buf_phys_addr; | 
 | 396 | 		for (index = PE1400_CMOS_CMD_STRUCT_PTR; | 
 | 397 | 		     index < (PE1400_CMOS_CMD_STRUCT_PTR + 4); | 
 | 398 | 		     index++, data++) { | 
 | 399 | 			outb(index, (CMOS_BASE_PORT + CMOS_PAGE1_INDEX_PORT)); | 
 | 400 | 			outb(*data, (CMOS_BASE_PORT + CMOS_PAGE1_DATA_PORT)); | 
 | 401 | 		} | 
 | 402 |  | 
 | 403 | 		/* generate SMM call */ | 
 | 404 | 		if (host_control_smi_type == HC_SMITYPE_TYPE3) | 
 | 405 | 			outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT); | 
 | 406 | 		else | 
 | 407 | 			outb(ESM_APM_CMD, PE1400_APM_CONTROL_PORT); | 
 | 408 |  | 
 | 409 | 		/* restore RTC index pointer since it was written to above */ | 
 | 410 | 		CMOS_READ(RTC_REG_C); | 
 | 411 | 		spin_unlock_irqrestore(&rtc_lock, flags); | 
 | 412 |  | 
 | 413 | 		/* read control port back to serialize write */ | 
 | 414 | 		cmd_status = inb(PE1400_APM_CONTROL_PORT); | 
 | 415 |  | 
 | 416 | 		/* wait a few to see if it executed */ | 
 | 417 | 		num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING; | 
 | 418 | 		while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) { | 
 | 419 | 			num_ticks--; | 
 | 420 | 			if (num_ticks == EXPIRED_TIMER) | 
 | 421 | 				return -ETIME; | 
 | 422 | 		} | 
 | 423 | 		break; | 
 | 424 |  | 
 | 425 | 	default: | 
 | 426 | 		dev_dbg(&dcdbas_pdev->dev, "%s: invalid SMI type %u\n", | 
 | 427 | 			__FUNCTION__, host_control_smi_type); | 
 | 428 | 		return -ENOSYS; | 
 | 429 | 	} | 
 | 430 |  | 
 | 431 | 	return 0; | 
 | 432 | } | 
 | 433 |  | 
 | 434 | /** | 
 | 435 |  * dcdbas_host_control: initiate host control | 
 | 436 |  * | 
 | 437 |  * This function is called by the driver after the system has | 
 | 438 |  * finished shutting down if the user application specified a | 
 | 439 |  * host control action to perform on shutdown.  It is safe to | 
 | 440 |  * use smi_data_buf at this point because the system has finished | 
 | 441 |  * shutting down and no userspace apps are running. | 
 | 442 |  */ | 
 | 443 | static void dcdbas_host_control(void) | 
 | 444 | { | 
 | 445 | 	struct apm_cmd *apm_cmd; | 
 | 446 | 	u8 action; | 
 | 447 |  | 
 | 448 | 	if (host_control_action == HC_ACTION_NONE) | 
 | 449 | 		return; | 
 | 450 |  | 
 | 451 | 	action = host_control_action; | 
 | 452 | 	host_control_action = HC_ACTION_NONE; | 
 | 453 |  | 
 | 454 | 	if (!smi_data_buf) { | 
 | 455 | 		dev_dbg(&dcdbas_pdev->dev, "%s: no SMI buffer\n", __FUNCTION__); | 
 | 456 | 		return; | 
 | 457 | 	} | 
 | 458 |  | 
 | 459 | 	if (smi_data_buf_size < sizeof(struct apm_cmd)) { | 
 | 460 | 		dev_dbg(&dcdbas_pdev->dev, "%s: SMI buffer too small\n", | 
 | 461 | 			__FUNCTION__); | 
 | 462 | 		return; | 
 | 463 | 	} | 
 | 464 |  | 
 | 465 | 	apm_cmd = (struct apm_cmd *)smi_data_buf; | 
 | 466 |  | 
 | 467 | 	/* power off takes precedence */ | 
 | 468 | 	if (action & HC_ACTION_HOST_CONTROL_POWEROFF) { | 
 | 469 | 		apm_cmd->command = ESM_APM_POWER_CYCLE; | 
 | 470 | 		apm_cmd->reserved = 0; | 
 | 471 | 		*((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 0; | 
 | 472 | 		host_control_smi(); | 
 | 473 | 	} else if (action & HC_ACTION_HOST_CONTROL_POWERCYCLE) { | 
 | 474 | 		apm_cmd->command = ESM_APM_POWER_CYCLE; | 
 | 475 | 		apm_cmd->reserved = 0; | 
 | 476 | 		*((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 20; | 
 | 477 | 		host_control_smi(); | 
 | 478 | 	} | 
 | 479 | } | 
 | 480 |  | 
 | 481 | /** | 
 | 482 |  * dcdbas_reboot_notify: handle reboot notification for host control | 
 | 483 |  */ | 
 | 484 | static int dcdbas_reboot_notify(struct notifier_block *nb, unsigned long code, | 
 | 485 | 				void *unused) | 
 | 486 | { | 
| Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 487 | 	switch (code) { | 
 | 488 | 	case SYS_DOWN: | 
 | 489 | 	case SYS_HALT: | 
 | 490 | 	case SYS_POWER_OFF: | 
 | 491 | 		if (host_control_on_shutdown) { | 
 | 492 | 			/* firmware is going to perform host control action */ | 
| Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 493 | 			printk(KERN_WARNING "Please wait for shutdown " | 
 | 494 | 			       "action to complete...\n"); | 
 | 495 | 			dcdbas_host_control(); | 
| Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 496 | 		} | 
 | 497 | 		break; | 
 | 498 | 	} | 
 | 499 |  | 
 | 500 | 	return NOTIFY_DONE; | 
 | 501 | } | 
 | 502 |  | 
 | 503 | static struct notifier_block dcdbas_reboot_nb = { | 
 | 504 | 	.notifier_call = dcdbas_reboot_notify, | 
 | 505 | 	.next = NULL, | 
| Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 506 | 	.priority = INT_MIN | 
| Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 507 | }; | 
 | 508 |  | 
 | 509 | static DCDBAS_BIN_ATTR_RW(smi_data); | 
 | 510 |  | 
 | 511 | static struct bin_attribute *dcdbas_bin_attrs[] = { | 
 | 512 | 	&bin_attr_smi_data, | 
 | 513 | 	NULL | 
 | 514 | }; | 
 | 515 |  | 
 | 516 | static DCDBAS_DEV_ATTR_RW(smi_data_buf_size); | 
 | 517 | static DCDBAS_DEV_ATTR_RO(smi_data_buf_phys_addr); | 
 | 518 | static DCDBAS_DEV_ATTR_WO(smi_request); | 
 | 519 | static DCDBAS_DEV_ATTR_RW(host_control_action); | 
 | 520 | static DCDBAS_DEV_ATTR_RW(host_control_smi_type); | 
 | 521 | static DCDBAS_DEV_ATTR_RW(host_control_on_shutdown); | 
 | 522 |  | 
| Dmitry Torokhov | a7f3ea7 | 2006-03-22 00:07:56 -0800 | [diff] [blame] | 523 | static struct attribute *dcdbas_dev_attrs[] = { | 
 | 524 | 	&dev_attr_smi_data_buf_size.attr, | 
 | 525 | 	&dev_attr_smi_data_buf_phys_addr.attr, | 
 | 526 | 	&dev_attr_smi_request.attr, | 
 | 527 | 	&dev_attr_host_control_action.attr, | 
 | 528 | 	&dev_attr_host_control_smi_type.attr, | 
 | 529 | 	&dev_attr_host_control_on_shutdown.attr, | 
| Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 530 | 	NULL | 
 | 531 | }; | 
 | 532 |  | 
| Dmitry Torokhov | a7f3ea7 | 2006-03-22 00:07:56 -0800 | [diff] [blame] | 533 | static struct attribute_group dcdbas_attr_group = { | 
 | 534 | 	.attrs = dcdbas_dev_attrs, | 
 | 535 | }; | 
 | 536 |  | 
 | 537 | static int __devinit dcdbas_probe(struct platform_device *dev) | 
| Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 538 | { | 
| Dmitry Torokhov | a7f3ea7 | 2006-03-22 00:07:56 -0800 | [diff] [blame] | 539 | 	int i, error; | 
| Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 540 |  | 
 | 541 | 	host_control_action = HC_ACTION_NONE; | 
 | 542 | 	host_control_smi_type = HC_SMITYPE_NONE; | 
 | 543 |  | 
| Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 544 | 	/* | 
 | 545 | 	 * BIOS SMI calls require buffer addresses be in 32-bit address space. | 
 | 546 | 	 * This is done by setting the DMA mask below. | 
 | 547 | 	 */ | 
 | 548 | 	dcdbas_pdev->dev.coherent_dma_mask = DMA_32BIT_MASK; | 
 | 549 | 	dcdbas_pdev->dev.dma_mask = &dcdbas_pdev->dev.coherent_dma_mask; | 
 | 550 |  | 
| Dmitry Torokhov | a7f3ea7 | 2006-03-22 00:07:56 -0800 | [diff] [blame] | 551 | 	error = sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group); | 
 | 552 | 	if (error) | 
 | 553 | 		return error; | 
 | 554 |  | 
 | 555 | 	for (i = 0; dcdbas_bin_attrs[i]; i++) { | 
 | 556 | 		error = sysfs_create_bin_file(&dev->dev.kobj, | 
 | 557 | 					      dcdbas_bin_attrs[i]); | 
 | 558 | 		if (error) { | 
 | 559 | 			while (--i >= 0) | 
 | 560 | 				sysfs_remove_bin_file(&dev->dev.kobj, | 
 | 561 | 						      dcdbas_bin_attrs[i]); | 
 | 562 | 			sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group); | 
 | 563 | 			return error; | 
 | 564 | 		} | 
 | 565 | 	} | 
 | 566 |  | 
| Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 567 | 	register_reboot_notifier(&dcdbas_reboot_nb); | 
 | 568 |  | 
| Dmitry Torokhov | a7f3ea7 | 2006-03-22 00:07:56 -0800 | [diff] [blame] | 569 | 	dev_info(&dev->dev, "%s (version %s)\n", | 
| Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 570 | 		 DRIVER_DESCRIPTION, DRIVER_VERSION); | 
 | 571 |  | 
 | 572 | 	return 0; | 
 | 573 | } | 
 | 574 |  | 
| Dmitry Torokhov | a7f3ea7 | 2006-03-22 00:07:56 -0800 | [diff] [blame] | 575 | static int __devexit dcdbas_remove(struct platform_device *dev) | 
 | 576 | { | 
 | 577 | 	int i; | 
 | 578 |  | 
 | 579 | 	unregister_reboot_notifier(&dcdbas_reboot_nb); | 
 | 580 | 	for (i = 0; dcdbas_bin_attrs[i]; i++) | 
 | 581 | 		sysfs_remove_bin_file(&dev->dev.kobj, dcdbas_bin_attrs[i]); | 
 | 582 | 	sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group); | 
 | 583 |  | 
 | 584 | 	return 0; | 
 | 585 | } | 
 | 586 |  | 
 | 587 | static struct platform_driver dcdbas_driver = { | 
 | 588 | 	.driver		= { | 
 | 589 | 		.name	= DRIVER_NAME, | 
 | 590 | 		.owner	= THIS_MODULE, | 
 | 591 | 	}, | 
 | 592 | 	.probe		= dcdbas_probe, | 
 | 593 | 	.remove		= __devexit_p(dcdbas_remove), | 
 | 594 | }; | 
 | 595 |  | 
 | 596 | /** | 
 | 597 |  * dcdbas_init: initialize driver | 
 | 598 |  */ | 
 | 599 | static int __init dcdbas_init(void) | 
 | 600 | { | 
 | 601 | 	int error; | 
 | 602 |  | 
 | 603 | 	error = platform_driver_register(&dcdbas_driver); | 
 | 604 | 	if (error) | 
 | 605 | 		return error; | 
 | 606 |  | 
 | 607 | 	dcdbas_pdev = platform_device_alloc(DRIVER_NAME, -1); | 
 | 608 | 	if (!dcdbas_pdev) { | 
 | 609 | 		error = -ENOMEM; | 
 | 610 | 		goto err_unregister_driver; | 
 | 611 | 	} | 
 | 612 |  | 
 | 613 | 	error = platform_device_add(dcdbas_pdev); | 
 | 614 | 	if (error) | 
 | 615 | 		goto err_free_device; | 
 | 616 |  | 
 | 617 | 	return 0; | 
 | 618 |  | 
 | 619 |  err_free_device: | 
 | 620 | 	platform_device_put(dcdbas_pdev); | 
 | 621 |  err_unregister_driver: | 
 | 622 | 	platform_driver_unregister(&dcdbas_driver); | 
 | 623 | 	return error; | 
 | 624 | } | 
 | 625 |  | 
| Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 626 | /** | 
 | 627 |  * dcdbas_exit: perform driver cleanup | 
 | 628 |  */ | 
 | 629 | static void __exit dcdbas_exit(void) | 
 | 630 | { | 
| Doug Warzecha | 435a80f | 2006-03-09 17:33:35 -0800 | [diff] [blame] | 631 | 	/* | 
 | 632 | 	 * make sure functions that use dcdbas_pdev are called | 
 | 633 | 	 * before platform_device_unregister | 
 | 634 | 	 */ | 
| Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 635 | 	unregister_reboot_notifier(&dcdbas_reboot_nb); | 
 | 636 | 	smi_data_buf_free(); | 
| Doug Warzecha | 435a80f | 2006-03-09 17:33:35 -0800 | [diff] [blame] | 637 | 	platform_device_unregister(dcdbas_pdev); | 
| Dmitry Torokhov | a7f3ea7 | 2006-03-22 00:07:56 -0800 | [diff] [blame] | 638 | 	platform_driver_unregister(&dcdbas_driver); | 
 | 639 |  | 
 | 640 | 	/* | 
 | 641 | 	 * We have to free the buffer here instead of dcdbas_remove | 
 | 642 | 	 * because only in module exit function we can be sure that | 
 | 643 | 	 * all sysfs attributes belonging to this module have been | 
 | 644 | 	 * released. | 
 | 645 | 	 */ | 
 | 646 | 	smi_data_buf_free(); | 
| Doug Warzecha | 90563ec | 2005-09-06 15:17:15 -0700 | [diff] [blame] | 647 | } | 
 | 648 |  | 
 | 649 | module_init(dcdbas_init); | 
 | 650 | module_exit(dcdbas_exit); | 
 | 651 |  | 
 | 652 | MODULE_DESCRIPTION(DRIVER_DESCRIPTION " (version " DRIVER_VERSION ")"); | 
 | 653 | MODULE_VERSION(DRIVER_VERSION); | 
 | 654 | MODULE_AUTHOR("Dell Inc."); | 
 | 655 | MODULE_LICENSE("GPL"); | 
 | 656 |  |