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