Merge branch 'stable/fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/konrad...
[linux-2.6.git] / drivers / firmware / dcdbas.c
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-2006 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
23 #include <linux/platform_device.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/errno.h>
26 #include <linux/gfp.h>
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>
37 #include <linux/mutex.h>
38 #include <asm/io.h>
39
40 #include "dcdbas.h"
41
42 #define DRIVER_NAME             "dcdbas"
43 #define DRIVER_VERSION          "5.6.0-3.2"
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;
52 static DEFINE_MUTEX(smi_data_lock);
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                 __func__, 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                         __func__, 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                 __func__, 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 */
143         mutex_lock(&smi_data_lock);
144         ret = smi_data_buf_realloc(buf_size);
145         mutex_unlock(&smi_data_lock);
146         if (ret)
147                 return ret;
148
149         return count;
150 }
151
152 static ssize_t smi_data_read(struct file *filp, struct kobject *kobj,
153                              struct bin_attribute *bin_attr,
154                              char *buf, loff_t pos, size_t count)
155 {
156         ssize_t ret;
157
158         mutex_lock(&smi_data_lock);
159         ret = memory_read_from_buffer(buf, count, &pos, smi_data_buf,
160                                         smi_data_buf_size);
161         mutex_unlock(&smi_data_lock);
162         return ret;
163 }
164
165 static ssize_t smi_data_write(struct file *filp, struct kobject *kobj,
166                               struct bin_attribute *bin_attr,
167                               char *buf, loff_t pos, size_t count)
168 {
169         ssize_t ret;
170
171         if ((pos + count) > MAX_SMI_DATA_BUF_SIZE)
172                 return -EINVAL;
173
174         mutex_lock(&smi_data_lock);
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:
183         mutex_unlock(&smi_data_lock);
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 */
201         mutex_lock(&smi_data_lock);
202         ret = smi_data_buf_realloc(sizeof(struct apm_cmd));
203         mutex_unlock(&smi_data_lock);
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 /**
242  * dcdbas_smi_request: generate SMI request
243  *
244  * Called with smi_data_lock.
245  */
246 int dcdbas_smi_request(struct smi_cmd *smi_cmd)
247 {
248         cpumask_var_t old_mask;
249         int ret = 0;
250
251         if (smi_cmd->magic != SMI_CMD_MAGIC) {
252                 dev_info(&dcdbas_pdev->dev, "%s: invalid magic value\n",
253                          __func__);
254                 return -EBADR;
255         }
256
257         /* SMI requires CPU 0 */
258         if (!alloc_cpumask_var(&old_mask, GFP_KERNEL))
259                 return -ENOMEM;
260
261         cpumask_copy(old_mask, &current->cpus_allowed);
262         set_cpus_allowed_ptr(current, cpumask_of(0));
263         if (smp_processor_id() != 0) {
264                 dev_dbg(&dcdbas_pdev->dev, "%s: failed to get CPU 0\n",
265                         __func__);
266                 ret = -EBUSY;
267                 goto out;
268         }
269
270         /* generate SMI */
271         asm volatile (
272                 "outb %b0,%w1"
273                 : /* no output args */
274                 : "a" (smi_cmd->command_code),
275                   "d" (smi_cmd->command_address),
276                   "b" (smi_cmd->ebx),
277                   "c" (smi_cmd->ecx)
278                 : "memory"
279         );
280
281 out:
282         set_cpus_allowed_ptr(current, old_mask);
283         free_cpumask_var(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
306         mutex_lock(&smi_data_lock);
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 = dcdbas_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 = dcdbas_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:
338         mutex_unlock(&smi_data_lock);
339         return ret;
340 }
341 EXPORT_SYMBOL(dcdbas_smi_request);
342
343 /**
344  * host_control_smi: generate host control SMI
345  *
346  * Caller must set up the host control command in smi_data_buf.
347  */
348 static int host_control_smi(void)
349 {
350         struct apm_cmd *apm_cmd;
351         u8 *data;
352         unsigned long flags;
353         u32 num_ticks;
354         s8 cmd_status;
355         u8 index;
356
357         apm_cmd = (struct apm_cmd *)smi_data_buf;
358         apm_cmd->status = ESM_STATUS_CMD_UNSUCCESSFUL;
359
360         switch (host_control_smi_type) {
361         case HC_SMITYPE_TYPE1:
362                 spin_lock_irqsave(&rtc_lock, flags);
363                 /* write SMI data buffer physical address */
364                 data = (u8 *)&smi_data_buf_phys_addr;
365                 for (index = PE1300_CMOS_CMD_STRUCT_PTR;
366                      index < (PE1300_CMOS_CMD_STRUCT_PTR + 4);
367                      index++, data++) {
368                         outb(index,
369                              (CMOS_BASE_PORT + CMOS_PAGE2_INDEX_PORT_PIIX4));
370                         outb(*data,
371                              (CMOS_BASE_PORT + CMOS_PAGE2_DATA_PORT_PIIX4));
372                 }
373
374                 /* first set status to -1 as called by spec */
375                 cmd_status = ESM_STATUS_CMD_UNSUCCESSFUL;
376                 outb((u8) cmd_status, PCAT_APM_STATUS_PORT);
377
378                 /* generate SMM call */
379                 outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
380                 spin_unlock_irqrestore(&rtc_lock, flags);
381
382                 /* wait a few to see if it executed */
383                 num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
384                 while ((cmd_status = inb(PCAT_APM_STATUS_PORT))
385                        == ESM_STATUS_CMD_UNSUCCESSFUL) {
386                         num_ticks--;
387                         if (num_ticks == EXPIRED_TIMER)
388                                 return -ETIME;
389                 }
390                 break;
391
392         case HC_SMITYPE_TYPE2:
393         case HC_SMITYPE_TYPE3:
394                 spin_lock_irqsave(&rtc_lock, flags);
395                 /* write SMI data buffer physical address */
396                 data = (u8 *)&smi_data_buf_phys_addr;
397                 for (index = PE1400_CMOS_CMD_STRUCT_PTR;
398                      index < (PE1400_CMOS_CMD_STRUCT_PTR + 4);
399                      index++, data++) {
400                         outb(index, (CMOS_BASE_PORT + CMOS_PAGE1_INDEX_PORT));
401                         outb(*data, (CMOS_BASE_PORT + CMOS_PAGE1_DATA_PORT));
402                 }
403
404                 /* generate SMM call */
405                 if (host_control_smi_type == HC_SMITYPE_TYPE3)
406                         outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
407                 else
408                         outb(ESM_APM_CMD, PE1400_APM_CONTROL_PORT);
409
410                 /* restore RTC index pointer since it was written to above */
411                 CMOS_READ(RTC_REG_C);
412                 spin_unlock_irqrestore(&rtc_lock, flags);
413
414                 /* read control port back to serialize write */
415                 cmd_status = inb(PE1400_APM_CONTROL_PORT);
416
417                 /* wait a few to see if it executed */
418                 num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
419                 while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) {
420                         num_ticks--;
421                         if (num_ticks == EXPIRED_TIMER)
422                                 return -ETIME;
423                 }
424                 break;
425
426         default:
427                 dev_dbg(&dcdbas_pdev->dev, "%s: invalid SMI type %u\n",
428                         __func__, host_control_smi_type);
429                 return -ENOSYS;
430         }
431
432         return 0;
433 }
434
435 /**
436  * dcdbas_host_control: initiate host control
437  *
438  * This function is called by the driver after the system has
439  * finished shutting down if the user application specified a
440  * host control action to perform on shutdown.  It is safe to
441  * use smi_data_buf at this point because the system has finished
442  * shutting down and no userspace apps are running.
443  */
444 static void dcdbas_host_control(void)
445 {
446         struct apm_cmd *apm_cmd;
447         u8 action;
448
449         if (host_control_action == HC_ACTION_NONE)
450                 return;
451
452         action = host_control_action;
453         host_control_action = HC_ACTION_NONE;
454
455         if (!smi_data_buf) {
456                 dev_dbg(&dcdbas_pdev->dev, "%s: no SMI buffer\n", __func__);
457                 return;
458         }
459
460         if (smi_data_buf_size < sizeof(struct apm_cmd)) {
461                 dev_dbg(&dcdbas_pdev->dev, "%s: SMI buffer too small\n",
462                         __func__);
463                 return;
464         }
465
466         apm_cmd = (struct apm_cmd *)smi_data_buf;
467
468         /* power off takes precedence */
469         if (action & HC_ACTION_HOST_CONTROL_POWEROFF) {
470                 apm_cmd->command = ESM_APM_POWER_CYCLE;
471                 apm_cmd->reserved = 0;
472                 *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 0;
473                 host_control_smi();
474         } else if (action & HC_ACTION_HOST_CONTROL_POWERCYCLE) {
475                 apm_cmd->command = ESM_APM_POWER_CYCLE;
476                 apm_cmd->reserved = 0;
477                 *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 20;
478                 host_control_smi();
479         }
480 }
481
482 /**
483  * dcdbas_reboot_notify: handle reboot notification for host control
484  */
485 static int dcdbas_reboot_notify(struct notifier_block *nb, unsigned long code,
486                                 void *unused)
487 {
488         switch (code) {
489         case SYS_DOWN:
490         case SYS_HALT:
491         case SYS_POWER_OFF:
492                 if (host_control_on_shutdown) {
493                         /* firmware is going to perform host control action */
494                         printk(KERN_WARNING "Please wait for shutdown "
495                                "action to complete...\n");
496                         dcdbas_host_control();
497                 }
498                 break;
499         }
500
501         return NOTIFY_DONE;
502 }
503
504 static struct notifier_block dcdbas_reboot_nb = {
505         .notifier_call = dcdbas_reboot_notify,
506         .next = NULL,
507         .priority = INT_MIN
508 };
509
510 static DCDBAS_BIN_ATTR_RW(smi_data);
511
512 static struct bin_attribute *dcdbas_bin_attrs[] = {
513         &bin_attr_smi_data,
514         NULL
515 };
516
517 static DCDBAS_DEV_ATTR_RW(smi_data_buf_size);
518 static DCDBAS_DEV_ATTR_RO(smi_data_buf_phys_addr);
519 static DCDBAS_DEV_ATTR_WO(smi_request);
520 static DCDBAS_DEV_ATTR_RW(host_control_action);
521 static DCDBAS_DEV_ATTR_RW(host_control_smi_type);
522 static DCDBAS_DEV_ATTR_RW(host_control_on_shutdown);
523
524 static struct attribute *dcdbas_dev_attrs[] = {
525         &dev_attr_smi_data_buf_size.attr,
526         &dev_attr_smi_data_buf_phys_addr.attr,
527         &dev_attr_smi_request.attr,
528         &dev_attr_host_control_action.attr,
529         &dev_attr_host_control_smi_type.attr,
530         &dev_attr_host_control_on_shutdown.attr,
531         NULL
532 };
533
534 static struct attribute_group dcdbas_attr_group = {
535         .attrs = dcdbas_dev_attrs,
536 };
537
538 static int __devinit dcdbas_probe(struct platform_device *dev)
539 {
540         int i, error;
541
542         host_control_action = HC_ACTION_NONE;
543         host_control_smi_type = HC_SMITYPE_NONE;
544
545         /*
546          * BIOS SMI calls require buffer addresses be in 32-bit address space.
547          * This is done by setting the DMA mask below.
548          */
549         dcdbas_pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
550         dcdbas_pdev->dev.dma_mask = &dcdbas_pdev->dev.coherent_dma_mask;
551
552         error = sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group);
553         if (error)
554                 return error;
555
556         for (i = 0; dcdbas_bin_attrs[i]; i++) {
557                 error = sysfs_create_bin_file(&dev->dev.kobj,
558                                               dcdbas_bin_attrs[i]);
559                 if (error) {
560                         while (--i >= 0)
561                                 sysfs_remove_bin_file(&dev->dev.kobj,
562                                                       dcdbas_bin_attrs[i]);
563                         sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
564                         return error;
565                 }
566         }
567
568         register_reboot_notifier(&dcdbas_reboot_nb);
569
570         dev_info(&dev->dev, "%s (version %s)\n",
571                  DRIVER_DESCRIPTION, DRIVER_VERSION);
572
573         return 0;
574 }
575
576 static int __devexit dcdbas_remove(struct platform_device *dev)
577 {
578         int i;
579
580         unregister_reboot_notifier(&dcdbas_reboot_nb);
581         for (i = 0; dcdbas_bin_attrs[i]; i++)
582                 sysfs_remove_bin_file(&dev->dev.kobj, dcdbas_bin_attrs[i]);
583         sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
584
585         return 0;
586 }
587
588 static struct platform_driver dcdbas_driver = {
589         .driver         = {
590                 .name   = DRIVER_NAME,
591                 .owner  = THIS_MODULE,
592         },
593         .probe          = dcdbas_probe,
594         .remove         = __devexit_p(dcdbas_remove),
595 };
596
597 /**
598  * dcdbas_init: initialize driver
599  */
600 static int __init dcdbas_init(void)
601 {
602         int error;
603
604         error = platform_driver_register(&dcdbas_driver);
605         if (error)
606                 return error;
607
608         dcdbas_pdev = platform_device_alloc(DRIVER_NAME, -1);
609         if (!dcdbas_pdev) {
610                 error = -ENOMEM;
611                 goto err_unregister_driver;
612         }
613
614         error = platform_device_add(dcdbas_pdev);
615         if (error)
616                 goto err_free_device;
617
618         return 0;
619
620  err_free_device:
621         platform_device_put(dcdbas_pdev);
622  err_unregister_driver:
623         platform_driver_unregister(&dcdbas_driver);
624         return error;
625 }
626
627 /**
628  * dcdbas_exit: perform driver cleanup
629  */
630 static void __exit dcdbas_exit(void)
631 {
632         /*
633          * make sure functions that use dcdbas_pdev are called
634          * before platform_device_unregister
635          */
636         unregister_reboot_notifier(&dcdbas_reboot_nb);
637
638         /*
639          * We have to free the buffer here instead of dcdbas_remove
640          * because only in module exit function we can be sure that
641          * all sysfs attributes belonging to this module have been
642          * released.
643          */
644         smi_data_buf_free();
645         platform_device_unregister(dcdbas_pdev);
646         platform_driver_unregister(&dcdbas_driver);
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 /* Any System or BIOS claiming to be by Dell */
657 MODULE_ALIAS("dmi:*:[bs]vnD[Ee][Ll][Ll]*:*");