linux/drivers/ata/libata-acpi.c
<<
>>
Prefs
   1/*
   2 * libata-acpi.c
   3 * Provides ACPI support for PATA/SATA.
   4 *
   5 * Copyright (C) 2006 Intel Corp.
   6 * Copyright (C) 2006 Randy Dunlap
   7 */
   8
   9#include <linux/module.h>
  10#include <linux/ata.h>
  11#include <linux/delay.h>
  12#include <linux/device.h>
  13#include <linux/errno.h>
  14#include <linux/kernel.h>
  15#include <linux/acpi.h>
  16#include <linux/libata.h>
  17#include <linux/pci.h>
  18#include <linux/slab.h>
  19#include <linux/pm_runtime.h>
  20#include <linux/pm_qos.h>
  21#include <scsi/scsi_device.h>
  22#include "libata.h"
  23
  24#include <acpi/acpi_bus.h>
  25
  26unsigned int ata_acpi_gtf_filter = ATA_ACPI_FILTER_DEFAULT;
  27module_param_named(acpi_gtf_filter, ata_acpi_gtf_filter, int, 0644);
  28MODULE_PARM_DESC(acpi_gtf_filter, "filter mask for ACPI _GTF commands, set to filter out (0x1=set xfermode, 0x2=lock/freeze lock, 0x4=DIPM, 0x8=FPDMA non-zero offset, 0x10=FPDMA DMA Setup FIS auto-activate)");
  29
  30#define NO_PORT_MULT            0xffff
  31#define SATA_ADR(root, pmp)     (((root) << 16) | (pmp))
  32
  33#define REGS_PER_GTF            7
  34struct ata_acpi_gtf {
  35        u8      tf[REGS_PER_GTF];       /* regs. 0x1f1 - 0x1f7 */
  36} __packed;
  37
  38/*
  39 *      Helper - belongs in the PCI layer somewhere eventually
  40 */
  41static int is_pci_dev(struct device *dev)
  42{
  43        return (dev->bus == &pci_bus_type);
  44}
  45
  46static void ata_acpi_clear_gtf(struct ata_device *dev)
  47{
  48        kfree(dev->gtf_cache);
  49        dev->gtf_cache = NULL;
  50}
  51
  52/**
  53 * ata_ap_acpi_handle - provide the acpi_handle for an ata_port
  54 * @ap: the acpi_handle returned will correspond to this port
  55 *
  56 * Returns the acpi_handle for the ACPI namespace object corresponding to
  57 * the ata_port passed into the function, or NULL if no such object exists
  58 */
  59acpi_handle ata_ap_acpi_handle(struct ata_port *ap)
  60{
  61        if (ap->flags & ATA_FLAG_ACPI_SATA)
  62                return NULL;
  63
  64        return ap->scsi_host ?
  65                DEVICE_ACPI_HANDLE(&ap->scsi_host->shost_gendev) : NULL;
  66}
  67EXPORT_SYMBOL(ata_ap_acpi_handle);
  68
  69/**
  70 * ata_dev_acpi_handle - provide the acpi_handle for an ata_device
  71 * @dev: the acpi_device returned will correspond to this port
  72 *
  73 * Returns the acpi_handle for the ACPI namespace object corresponding to
  74 * the ata_device passed into the function, or NULL if no such object exists
  75 */
  76acpi_handle ata_dev_acpi_handle(struct ata_device *dev)
  77{
  78        acpi_integer adr;
  79        struct ata_port *ap = dev->link->ap;
  80
  81        if (dev->flags & ATA_DFLAG_ACPI_DISABLED)
  82                return NULL;
  83
  84        if (ap->flags & ATA_FLAG_ACPI_SATA) {
  85                if (!sata_pmp_attached(ap))
  86                        adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
  87                else
  88                        adr = SATA_ADR(ap->port_no, dev->link->pmp);
  89                return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev), adr);
  90        } else
  91                return acpi_get_child(ata_ap_acpi_handle(ap), dev->devno);
  92}
  93EXPORT_SYMBOL(ata_dev_acpi_handle);
  94
  95/* @ap and @dev are the same as ata_acpi_handle_hotplug() */
  96static void ata_acpi_detach_device(struct ata_port *ap, struct ata_device *dev)
  97{
  98        if (dev)
  99                dev->flags |= ATA_DFLAG_DETACH;
 100        else {
 101                struct ata_link *tlink;
 102                struct ata_device *tdev;
 103
 104                ata_for_each_link(tlink, ap, EDGE)
 105                        ata_for_each_dev(tdev, tlink, ALL)
 106                                tdev->flags |= ATA_DFLAG_DETACH;
 107        }
 108
 109        ata_port_schedule_eh(ap);
 110}
 111
 112/**
 113 * ata_acpi_handle_hotplug - ACPI event handler backend
 114 * @ap: ATA port ACPI event occurred
 115 * @dev: ATA device ACPI event occurred (can be NULL)
 116 * @event: ACPI event which occurred
 117 *
 118 * All ACPI bay / device realted events end up in this function.  If
 119 * the event is port-wide @dev is NULL.  If the event is specific to a
 120 * device, @dev points to it.
 121 *
 122 * Hotplug (as opposed to unplug) notification is always handled as
 123 * port-wide while unplug only kills the target device on device-wide
 124 * event.
 125 *
 126 * LOCKING:
 127 * ACPI notify handler context.  May sleep.
 128 */
 129static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev,
 130                                    u32 event)
 131{
 132        struct ata_eh_info *ehi = &ap->link.eh_info;
 133        int wait = 0;
 134        unsigned long flags;
 135
 136        spin_lock_irqsave(ap->lock, flags);
 137        /*
 138         * When dock driver calls into the routine, it will always use
 139         * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
 140         * ACPI_NOTIFY_EJECT_REQUEST for remove
 141         */
 142        switch (event) {
 143        case ACPI_NOTIFY_BUS_CHECK:
 144        case ACPI_NOTIFY_DEVICE_CHECK:
 145                ata_ehi_push_desc(ehi, "ACPI event");
 146
 147                ata_ehi_hotplugged(ehi);
 148                ata_port_freeze(ap);
 149                break;
 150        case ACPI_NOTIFY_EJECT_REQUEST:
 151                ata_ehi_push_desc(ehi, "ACPI event");
 152
 153                ata_acpi_detach_device(ap, dev);
 154                wait = 1;
 155                break;
 156        }
 157
 158        spin_unlock_irqrestore(ap->lock, flags);
 159
 160        if (wait)
 161                ata_port_wait_eh(ap);
 162}
 163
 164static void ata_acpi_dev_notify_dock(acpi_handle handle, u32 event, void *data)
 165{
 166        struct ata_device *dev = data;
 167
 168        ata_acpi_handle_hotplug(dev->link->ap, dev, event);
 169}
 170
 171static void ata_acpi_ap_notify_dock(acpi_handle handle, u32 event, void *data)
 172{
 173        struct ata_port *ap = data;
 174
 175        ata_acpi_handle_hotplug(ap, NULL, event);
 176}
 177
 178static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev,
 179        u32 event)
 180{
 181        struct kobject *kobj = NULL;
 182        char event_string[20];
 183        char *envp[] = { event_string, NULL };
 184
 185        if (dev) {
 186                if (dev->sdev)
 187                        kobj = &dev->sdev->sdev_gendev.kobj;
 188        } else
 189                kobj = &ap->dev->kobj;
 190
 191        if (kobj) {
 192                snprintf(event_string, 20, "BAY_EVENT=%d", event);
 193                kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
 194        }
 195}
 196
 197static void ata_acpi_ap_uevent(acpi_handle handle, u32 event, void *data)
 198{
 199        ata_acpi_uevent(data, NULL, event);
 200}
 201
 202static void ata_acpi_dev_uevent(acpi_handle handle, u32 event, void *data)
 203{
 204        struct ata_device *dev = data;
 205        ata_acpi_uevent(dev->link->ap, dev, event);
 206}
 207
 208static const struct acpi_dock_ops ata_acpi_dev_dock_ops = {
 209        .handler = ata_acpi_dev_notify_dock,
 210        .uevent = ata_acpi_dev_uevent,
 211};
 212
 213static const struct acpi_dock_ops ata_acpi_ap_dock_ops = {
 214        .handler = ata_acpi_ap_notify_dock,
 215        .uevent = ata_acpi_ap_uevent,
 216};
 217
 218/**
 219 * ata_acpi_dissociate - dissociate ATA host from ACPI objects
 220 * @host: target ATA host
 221 *
 222 * This function is called during driver detach after the whole host
 223 * is shut down.
 224 *
 225 * LOCKING:
 226 * EH context.
 227 */
 228void ata_acpi_dissociate(struct ata_host *host)
 229{
 230        int i;
 231
 232        /* Restore initial _GTM values so that driver which attaches
 233         * afterward can use them too.
 234         */
 235        for (i = 0; i < host->n_ports; i++) {
 236                struct ata_port *ap = host->ports[i];
 237                const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
 238
 239                if (ata_ap_acpi_handle(ap) && gtm)
 240                        ata_acpi_stm(ap, gtm);
 241        }
 242}
 243
 244static int __ata_acpi_gtm(struct ata_port *ap, acpi_handle handle,
 245                          struct ata_acpi_gtm *gtm)
 246{
 247        struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
 248        union acpi_object *out_obj;
 249        acpi_status status;
 250        int rc = 0;
 251
 252        status = acpi_evaluate_object(handle, "_GTM", NULL, &output);
 253
 254        rc = -ENOENT;
 255        if (status == AE_NOT_FOUND)
 256                goto out_free;
 257
 258        rc = -EINVAL;
 259        if (ACPI_FAILURE(status)) {
 260                ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n",
 261                             status);
 262                goto out_free;
 263        }
 264
 265        out_obj = output.pointer;
 266        if (out_obj->type != ACPI_TYPE_BUFFER) {
 267                ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n",
 268                              out_obj->type);
 269
 270                goto out_free;
 271        }
 272
 273        if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
 274                ata_port_err(ap, "_GTM returned invalid length %d\n",
 275                             out_obj->buffer.length);
 276                goto out_free;
 277        }
 278
 279        memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
 280        rc = 0;
 281 out_free:
 282        kfree(output.pointer);
 283        return rc;
 284}
 285
 286/**
 287 * ata_acpi_gtm - execute _GTM
 288 * @ap: target ATA port
 289 * @gtm: out parameter for _GTM result
 290 *
 291 * Evaluate _GTM and store the result in @gtm.
 292 *
 293 * LOCKING:
 294 * EH context.
 295 *
 296 * RETURNS:
 297 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
 298 */
 299int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
 300{
 301        if (ata_ap_acpi_handle(ap))
 302                return __ata_acpi_gtm(ap, ata_ap_acpi_handle(ap), gtm);
 303        else
 304                return -EINVAL;
 305}
 306
 307EXPORT_SYMBOL_GPL(ata_acpi_gtm);
 308
 309/**
 310 * ata_acpi_stm - execute _STM
 311 * @ap: target ATA port
 312 * @stm: timing parameter to _STM
 313 *
 314 * Evaluate _STM with timing parameter @stm.
 315 *
 316 * LOCKING:
 317 * EH context.
 318 *
 319 * RETURNS:
 320 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
 321 */
 322int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
 323{
 324        acpi_status status;
 325        struct ata_acpi_gtm             stm_buf = *stm;
 326        struct acpi_object_list         input;
 327        union acpi_object               in_params[3];
 328
 329        in_params[0].type = ACPI_TYPE_BUFFER;
 330        in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
 331        in_params[0].buffer.pointer = (u8 *)&stm_buf;
 332        /* Buffers for id may need byteswapping ? */
 333        in_params[1].type = ACPI_TYPE_BUFFER;
 334        in_params[1].buffer.length = 512;
 335        in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
 336        in_params[2].type = ACPI_TYPE_BUFFER;
 337        in_params[2].buffer.length = 512;
 338        in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
 339
 340        input.count = 3;
 341        input.pointer = in_params;
 342
 343        status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_STM", &input,
 344                                      NULL);
 345
 346        if (status == AE_NOT_FOUND)
 347                return -ENOENT;
 348        if (ACPI_FAILURE(status)) {
 349                ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n",
 350                             status);
 351                return -EINVAL;
 352        }
 353        return 0;
 354}
 355
 356EXPORT_SYMBOL_GPL(ata_acpi_stm);
 357
 358/**
 359 * ata_dev_get_GTF - get the drive bootup default taskfile settings
 360 * @dev: target ATA device
 361 * @gtf: output parameter for buffer containing _GTF taskfile arrays
 362 *
 363 * This applies to both PATA and SATA drives.
 364 *
 365 * The _GTF method has no input parameters.
 366 * It returns a variable number of register set values (registers
 367 * hex 1F1..1F7, taskfiles).
 368 * The <variable number> is not known in advance, so have ACPI-CA
 369 * allocate the buffer as needed and return it, then free it later.
 370 *
 371 * LOCKING:
 372 * EH context.
 373 *
 374 * RETURNS:
 375 * Number of taskfiles on success, 0 if _GTF doesn't exist.  -EINVAL
 376 * if _GTF is invalid.
 377 */
 378static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
 379{
 380        struct ata_port *ap = dev->link->ap;
 381        acpi_status status;
 382        struct acpi_buffer output;
 383        union acpi_object *out_obj;
 384        int rc = 0;
 385
 386        /* if _GTF is cached, use the cached value */
 387        if (dev->gtf_cache) {
 388                out_obj = dev->gtf_cache;
 389                goto done;
 390        }
 391
 392        /* set up output buffer */
 393        output.length = ACPI_ALLOCATE_BUFFER;
 394        output.pointer = NULL;  /* ACPI-CA sets this; save/free it later */
 395
 396        if (ata_msg_probe(ap))
 397                ata_dev_dbg(dev, "%s: ENTER: port#: %d\n",
 398                            __func__, ap->port_no);
 399
 400        /* _GTF has no input parameters */
 401        status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL,
 402                                      &output);
 403        out_obj = dev->gtf_cache = output.pointer;
 404
 405        if (ACPI_FAILURE(status)) {
 406                if (status != AE_NOT_FOUND) {
 407                        ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n",
 408                                     status);
 409                        rc = -EINVAL;
 410                }
 411                goto out_free;
 412        }
 413
 414        if (!output.length || !output.pointer) {
 415                if (ata_msg_probe(ap))
 416                        ata_dev_dbg(dev, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
 417                                    __func__,
 418                                    (unsigned long long)output.length,
 419                                    output.pointer);
 420                rc = -EINVAL;
 421                goto out_free;
 422        }
 423
 424        if (out_obj->type != ACPI_TYPE_BUFFER) {
 425                ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n",
 426                             out_obj->type);
 427                rc = -EINVAL;
 428                goto out_free;
 429        }
 430
 431        if (out_obj->buffer.length % REGS_PER_GTF) {
 432                ata_dev_warn(dev, "unexpected _GTF length (%d)\n",
 433                             out_obj->buffer.length);
 434                rc = -EINVAL;
 435                goto out_free;
 436        }
 437
 438 done:
 439        rc = out_obj->buffer.length / REGS_PER_GTF;
 440        if (gtf) {
 441                *gtf = (void *)out_obj->buffer.pointer;
 442                if (ata_msg_probe(ap))
 443                        ata_dev_dbg(dev, "%s: returning gtf=%p, gtf_count=%d\n",
 444                                    __func__, *gtf, rc);
 445        }
 446        return rc;
 447
 448 out_free:
 449        ata_acpi_clear_gtf(dev);
 450        return rc;
 451}
 452
 453/**
 454 * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
 455 * @dev: target device
 456 * @gtm: GTM parameter to use
 457 *
 458 * Determine xfermask for @dev from @gtm.
 459 *
 460 * LOCKING:
 461 * None.
 462 *
 463 * RETURNS:
 464 * Determined xfermask.
 465 */
 466unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
 467                                    const struct ata_acpi_gtm *gtm)
 468{
 469        unsigned long xfer_mask = 0;
 470        unsigned int type;
 471        int unit;
 472        u8 mode;
 473
 474        /* we always use the 0 slot for crap hardware */
 475        unit = dev->devno;
 476        if (!(gtm->flags & 0x10))
 477                unit = 0;
 478
 479        /* PIO */
 480        mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
 481        xfer_mask |= ata_xfer_mode2mask(mode);
 482
 483        /* See if we have MWDMA or UDMA data. We don't bother with
 484         * MWDMA if UDMA is available as this means the BIOS set UDMA
 485         * and our error changedown if it works is UDMA to PIO anyway.
 486         */
 487        if (!(gtm->flags & (1 << (2 * unit))))
 488                type = ATA_SHIFT_MWDMA;
 489        else
 490                type = ATA_SHIFT_UDMA;
 491
 492        mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
 493        xfer_mask |= ata_xfer_mode2mask(mode);
 494
 495        return xfer_mask;
 496}
 497EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
 498
 499/**
 500 * ata_acpi_cbl_80wire          -       Check for 80 wire cable
 501 * @ap: Port to check
 502 * @gtm: GTM data to use
 503 *
 504 * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
 505 */
 506int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
 507{
 508        struct ata_device *dev;
 509
 510        ata_for_each_dev(dev, &ap->link, ENABLED) {
 511                unsigned long xfer_mask, udma_mask;
 512
 513                xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
 514                ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
 515
 516                if (udma_mask & ~ATA_UDMA_MASK_40C)
 517                        return 1;
 518        }
 519
 520        return 0;
 521}
 522EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
 523
 524static void ata_acpi_gtf_to_tf(struct ata_device *dev,
 525                               const struct ata_acpi_gtf *gtf,
 526                               struct ata_taskfile *tf)
 527{
 528        ata_tf_init(dev, tf);
 529
 530        tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
 531        tf->protocol = ATA_PROT_NODATA;
 532        tf->feature = gtf->tf[0];       /* 0x1f1 */
 533        tf->nsect   = gtf->tf[1];       /* 0x1f2 */
 534        tf->lbal    = gtf->tf[2];       /* 0x1f3 */
 535        tf->lbam    = gtf->tf[3];       /* 0x1f4 */
 536        tf->lbah    = gtf->tf[4];       /* 0x1f5 */
 537        tf->device  = gtf->tf[5];       /* 0x1f6 */
 538        tf->command = gtf->tf[6];       /* 0x1f7 */
 539}
 540
 541static int ata_acpi_filter_tf(struct ata_device *dev,
 542                              const struct ata_taskfile *tf,
 543                              const struct ata_taskfile *ptf)
 544{
 545        if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
 546                /* libata doesn't use ACPI to configure transfer mode.
 547                 * It will only confuse device configuration.  Skip.
 548                 */
 549                if (tf->command == ATA_CMD_SET_FEATURES &&
 550                    tf->feature == SETFEATURES_XFER)
 551                        return 1;
 552        }
 553
 554        if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
 555                /* BIOS writers, sorry but we don't wanna lock
 556                 * features unless the user explicitly said so.
 557                 */
 558
 559                /* DEVICE CONFIGURATION FREEZE LOCK */
 560                if (tf->command == ATA_CMD_CONF_OVERLAY &&
 561                    tf->feature == ATA_DCO_FREEZE_LOCK)
 562                        return 1;
 563
 564                /* SECURITY FREEZE LOCK */
 565                if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
 566                        return 1;
 567
 568                /* SET MAX LOCK and SET MAX FREEZE LOCK */
 569                if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
 570                    tf->command == ATA_CMD_SET_MAX &&
 571                    (tf->feature == ATA_SET_MAX_LOCK ||
 572                     tf->feature == ATA_SET_MAX_FREEZE_LOCK))
 573                        return 1;
 574        }
 575
 576        if (tf->command == ATA_CMD_SET_FEATURES &&
 577            tf->feature == SETFEATURES_SATA_ENABLE) {
 578                /* inhibit enabling DIPM */
 579                if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
 580                    tf->nsect == SATA_DIPM)
 581                        return 1;
 582
 583                /* inhibit FPDMA non-zero offset */
 584                if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
 585                    (tf->nsect == SATA_FPDMA_OFFSET ||
 586                     tf->nsect == SATA_FPDMA_IN_ORDER))
 587                        return 1;
 588
 589                /* inhibit FPDMA auto activation */
 590                if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
 591                    tf->nsect == SATA_FPDMA_AA)
 592                        return 1;
 593        }
 594
 595        return 0;
 596}
 597
 598/**
 599 * ata_acpi_run_tf - send taskfile registers to host controller
 600 * @dev: target ATA device
 601 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
 602 *
 603 * Outputs ATA taskfile to standard ATA host controller.
 604 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
 605 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
 606 * hob_lbal, hob_lbam, and hob_lbah.
 607 *
 608 * This function waits for idle (!BUSY and !DRQ) after writing
 609 * registers.  If the control register has a new value, this
 610 * function also waits for idle after writing control and before
 611 * writing the remaining registers.
 612 *
 613 * LOCKING:
 614 * EH context.
 615 *
 616 * RETURNS:
 617 * 1 if command is executed successfully.  0 if ignored, rejected or
 618 * filtered out, -errno on other errors.
 619 */
 620static int ata_acpi_run_tf(struct ata_device *dev,
 621                           const struct ata_acpi_gtf *gtf,
 622                           const struct ata_acpi_gtf *prev_gtf)
 623{
 624        struct ata_taskfile *pptf = NULL;
 625        struct ata_taskfile tf, ptf, rtf;
 626        unsigned int err_mask;
 627        const char *level;
 628        const char *descr;
 629        char msg[60];
 630        int rc;
 631
 632        if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
 633            && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
 634            && (gtf->tf[6] == 0))
 635                return 0;
 636
 637        ata_acpi_gtf_to_tf(dev, gtf, &tf);
 638        if (prev_gtf) {
 639                ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
 640                pptf = &ptf;
 641        }
 642
 643        if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
 644                rtf = tf;
 645                err_mask = ata_exec_internal(dev, &rtf, NULL,
 646                                             DMA_NONE, NULL, 0, 0);
 647
 648                switch (err_mask) {
 649                case 0:
 650                        level = KERN_DEBUG;
 651                        snprintf(msg, sizeof(msg), "succeeded");
 652                        rc = 1;
 653                        break;
 654
 655                case AC_ERR_DEV:
 656                        level = KERN_INFO;
 657                        snprintf(msg, sizeof(msg),
 658                                 "rejected by device (Stat=0x%02x Err=0x%02x)",
 659                                 rtf.command, rtf.feature);
 660                        rc = 0;
 661                        break;
 662
 663                default:
 664                        level = KERN_ERR;
 665                        snprintf(msg, sizeof(msg),
 666                                 "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
 667                                 err_mask, rtf.command, rtf.feature);
 668                        rc = -EIO;
 669                        break;
 670                }
 671        } else {
 672                level = KERN_INFO;
 673                snprintf(msg, sizeof(msg), "filtered out");
 674                rc = 0;
 675        }
 676        descr = ata_get_cmd_descript(tf.command);
 677
 678        ata_dev_printk(dev, level,
 679                       "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
 680                       tf.command, tf.feature, tf.nsect, tf.lbal,
 681                       tf.lbam, tf.lbah, tf.device,
 682                       (descr ? descr : "unknown"), msg);
 683
 684        return rc;
 685}
 686
 687/**
 688 * ata_acpi_exec_tfs - get then write drive taskfile settings
 689 * @dev: target ATA device
 690 * @nr_executed: out parameter for the number of executed commands
 691 *
 692 * Evaluate _GTF and execute returned taskfiles.
 693 *
 694 * LOCKING:
 695 * EH context.
 696 *
 697 * RETURNS:
 698 * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
 699 * -errno on other errors.
 700 */
 701static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
 702{
 703        struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
 704        int gtf_count, i, rc;
 705
 706        /* get taskfiles */
 707        rc = ata_dev_get_GTF(dev, &gtf);
 708        if (rc < 0)
 709                return rc;
 710        gtf_count = rc;
 711
 712        /* execute them */
 713        for (i = 0; i < gtf_count; i++, gtf++) {
 714                rc = ata_acpi_run_tf(dev, gtf, pgtf);
 715                if (rc < 0)
 716                        break;
 717                if (rc) {
 718                        (*nr_executed)++;
 719                        pgtf = gtf;
 720                }
 721        }
 722
 723        ata_acpi_clear_gtf(dev);
 724
 725        if (rc < 0)
 726                return rc;
 727        return 0;
 728}
 729
 730/**
 731 * ata_acpi_push_id - send Identify data to drive
 732 * @dev: target ATA device
 733 *
 734 * _SDD ACPI object: for SATA mode only
 735 * Must be after Identify (Packet) Device -- uses its data
 736 * ATM this function never returns a failure.  It is an optional
 737 * method and if it fails for whatever reason, we should still
 738 * just keep going.
 739 *
 740 * LOCKING:
 741 * EH context.
 742 *
 743 * RETURNS:
 744 * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
 745 */
 746static int ata_acpi_push_id(struct ata_device *dev)
 747{
 748        struct ata_port *ap = dev->link->ap;
 749        acpi_status status;
 750        struct acpi_object_list input;
 751        union acpi_object in_params[1];
 752
 753        if (ata_msg_probe(ap))
 754                ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n",
 755                            __func__, dev->devno, ap->port_no);
 756
 757        /* Give the drive Identify data to the drive via the _SDD method */
 758        /* _SDD: set up input parameters */
 759        input.count = 1;
 760        input.pointer = in_params;
 761        in_params[0].type = ACPI_TYPE_BUFFER;
 762        in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
 763        in_params[0].buffer.pointer = (u8 *)dev->id;
 764        /* Output buffer: _SDD has no output */
 765
 766        /* It's OK for _SDD to be missing too. */
 767        swap_buf_le16(dev->id, ATA_ID_WORDS);
 768        status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input,
 769                                      NULL);
 770        swap_buf_le16(dev->id, ATA_ID_WORDS);
 771
 772        if (status == AE_NOT_FOUND)
 773                return -ENOENT;
 774
 775        if (ACPI_FAILURE(status)) {
 776                ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status);
 777                return -EIO;
 778        }
 779
 780        return 0;
 781}
 782
 783/**
 784 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
 785 * @ap: target ATA port
 786 *
 787 * This function is called when @ap is about to be suspended.  All
 788 * devices are already put to sleep but the port_suspend() callback
 789 * hasn't been executed yet.  Error return from this function aborts
 790 * suspend.
 791 *
 792 * LOCKING:
 793 * EH context.
 794 *
 795 * RETURNS:
 796 * 0 on success, -errno on failure.
 797 */
 798int ata_acpi_on_suspend(struct ata_port *ap)
 799{
 800        /* nada */
 801        return 0;
 802}
 803
 804/**
 805 * ata_acpi_on_resume - ATA ACPI hook called on resume
 806 * @ap: target ATA port
 807 *
 808 * This function is called when @ap is resumed - right after port
 809 * itself is resumed but before any EH action is taken.
 810 *
 811 * LOCKING:
 812 * EH context.
 813 */
 814void ata_acpi_on_resume(struct ata_port *ap)
 815{
 816        const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
 817        struct ata_device *dev;
 818
 819        if (ata_ap_acpi_handle(ap) && gtm) {
 820                /* _GTM valid */
 821
 822                /* restore timing parameters */
 823                ata_acpi_stm(ap, gtm);
 824
 825                /* _GTF should immediately follow _STM so that it can
 826                 * use values set by _STM.  Cache _GTF result and
 827                 * schedule _GTF.
 828                 */
 829                ata_for_each_dev(dev, &ap->link, ALL) {
 830                        ata_acpi_clear_gtf(dev);
 831                        if (ata_dev_enabled(dev) &&
 832                            ata_dev_get_GTF(dev, NULL) >= 0)
 833                                dev->flags |= ATA_DFLAG_ACPI_PENDING;
 834                }
 835        } else {
 836                /* SATA _GTF needs to be evaulated after _SDD and
 837                 * there's no reason to evaluate IDE _GTF early
 838                 * without _STM.  Clear cache and schedule _GTF.
 839                 */
 840                ata_for_each_dev(dev, &ap->link, ALL) {
 841                        ata_acpi_clear_gtf(dev);
 842                        if (ata_dev_enabled(dev))
 843                                dev->flags |= ATA_DFLAG_ACPI_PENDING;
 844                }
 845        }
 846}
 847
 848static int ata_acpi_choose_suspend_state(struct ata_device *dev, bool runtime)
 849{
 850        int d_max_in = ACPI_STATE_D3_COLD;
 851        if (!runtime)
 852                goto out;
 853
 854        /*
 855         * For ATAPI, runtime D3 cold is only allowed
 856         * for ZPODD in zero power ready state
 857         */
 858        if (dev->class == ATA_DEV_ATAPI &&
 859            !(zpodd_dev_enabled(dev) && zpodd_zpready(dev)))
 860                d_max_in = ACPI_STATE_D3_HOT;
 861
 862out:
 863        return acpi_pm_device_sleep_state(&dev->sdev->sdev_gendev,
 864                                          NULL, d_max_in);
 865}
 866
 867static void sata_acpi_set_state(struct ata_port *ap, pm_message_t state)
 868{
 869        bool runtime = PMSG_IS_AUTO(state);
 870        struct ata_device *dev;
 871        acpi_handle handle;
 872        int acpi_state;
 873
 874        ata_for_each_dev(dev, &ap->link, ENABLED) {
 875                handle = ata_dev_acpi_handle(dev);
 876                if (!handle)
 877                        continue;
 878
 879                if (!(state.event & PM_EVENT_RESUME)) {
 880                        acpi_state = ata_acpi_choose_suspend_state(dev, runtime);
 881                        if (acpi_state == ACPI_STATE_D0)
 882                                continue;
 883                        if (runtime && zpodd_dev_enabled(dev) &&
 884                            acpi_state == ACPI_STATE_D3_COLD)
 885                                zpodd_enable_run_wake(dev);
 886                        acpi_bus_set_power(handle, acpi_state);
 887                } else {
 888                        if (runtime && zpodd_dev_enabled(dev))
 889                                zpodd_disable_run_wake(dev);
 890                        acpi_bus_set_power(handle, ACPI_STATE_D0);
 891                }
 892        }
 893}
 894
 895/* ACPI spec requires _PS0 when IDE power on and _PS3 when power off */
 896static void pata_acpi_set_state(struct ata_port *ap, pm_message_t state)
 897{
 898        struct ata_device *dev;
 899        acpi_handle port_handle;
 900
 901        port_handle = ata_ap_acpi_handle(ap);
 902        if (!port_handle)
 903                return;
 904
 905        /* channel first and then drives for power on and vica versa
 906           for power off */
 907        if (state.event & PM_EVENT_RESUME)
 908                acpi_bus_set_power(port_handle, ACPI_STATE_D0);
 909
 910        ata_for_each_dev(dev, &ap->link, ENABLED) {
 911                acpi_handle dev_handle = ata_dev_acpi_handle(dev);
 912                if (!dev_handle)
 913                        continue;
 914
 915                acpi_bus_set_power(dev_handle, state.event & PM_EVENT_RESUME ?
 916                                                ACPI_STATE_D0 : ACPI_STATE_D3);
 917        }
 918
 919        if (!(state.event & PM_EVENT_RESUME))
 920                acpi_bus_set_power(port_handle, ACPI_STATE_D3);
 921}
 922
 923/**
 924 * ata_acpi_set_state - set the port power state
 925 * @ap: target ATA port
 926 * @state: state, on/off
 927 *
 928 * This function sets a proper ACPI D state for the device on
 929 * system and runtime PM operations.
 930 */
 931void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
 932{
 933        if (ap->flags & ATA_FLAG_ACPI_SATA)
 934                sata_acpi_set_state(ap, state);
 935        else
 936                pata_acpi_set_state(ap, state);
 937}
 938
 939/**
 940 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
 941 * @dev: target ATA device
 942 *
 943 * This function is called when @dev is about to be configured.
 944 * IDENTIFY data might have been modified after this hook is run.
 945 *
 946 * LOCKING:
 947 * EH context.
 948 *
 949 * RETURNS:
 950 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
 951 * -errno on failure.
 952 */
 953int ata_acpi_on_devcfg(struct ata_device *dev)
 954{
 955        struct ata_port *ap = dev->link->ap;
 956        struct ata_eh_context *ehc = &ap->link.eh_context;
 957        int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
 958        int nr_executed = 0;
 959        int rc;
 960
 961        if (!ata_dev_acpi_handle(dev))
 962                return 0;
 963
 964        /* do we need to do _GTF? */
 965        if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
 966            !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
 967                return 0;
 968
 969        /* do _SDD if SATA */
 970        if (acpi_sata) {
 971                rc = ata_acpi_push_id(dev);
 972                if (rc && rc != -ENOENT)
 973                        goto acpi_err;
 974        }
 975
 976        /* do _GTF */
 977        rc = ata_acpi_exec_tfs(dev, &nr_executed);
 978        if (rc)
 979                goto acpi_err;
 980
 981        dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
 982
 983        /* refresh IDENTIFY page if any _GTF command has been executed */
 984        if (nr_executed) {
 985                rc = ata_dev_reread_id(dev, 0);
 986                if (rc < 0) {
 987                        ata_dev_err(dev,
 988                                    "failed to IDENTIFY after ACPI commands\n");
 989                        return rc;
 990                }
 991        }
 992
 993        return 0;
 994
 995 acpi_err:
 996        /* ignore evaluation failure if we can continue safely */
 997        if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
 998                return 0;
 999
1000        /* fail and let EH retry once more for unknown IO errors */
1001        if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
1002                dev->flags |= ATA_DFLAG_ACPI_FAILED;
1003                return rc;
1004        }
1005
1006        dev->flags |= ATA_DFLAG_ACPI_DISABLED;
1007        ata_dev_warn(dev, "ACPI: failed the second time, disabled\n");
1008
1009        /* We can safely continue if no _GTF command has been executed
1010         * and port is not frozen.
1011         */
1012        if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
1013                return 0;
1014
1015        return rc;
1016}
1017
1018/**
1019 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
1020 * @dev: target ATA device
1021 *
1022 * This function is called when @dev is about to be disabled.
1023 *
1024 * LOCKING:
1025 * EH context.
1026 */
1027void ata_acpi_on_disable(struct ata_device *dev)
1028{
1029        ata_acpi_clear_gtf(dev);
1030}
1031
1032static void ata_acpi_register_power_resource(struct ata_device *dev)
1033{
1034        struct scsi_device *sdev = dev->sdev;
1035        acpi_handle handle;
1036
1037        handle = ata_dev_acpi_handle(dev);
1038        if (handle)
1039                acpi_dev_pm_add_dependent(handle, &sdev->sdev_gendev);
1040}
1041
1042static void ata_acpi_unregister_power_resource(struct ata_device *dev)
1043{
1044        struct scsi_device *sdev = dev->sdev;
1045        acpi_handle handle;
1046
1047        handle = ata_dev_acpi_handle(dev);
1048        if (handle)
1049                acpi_dev_pm_remove_dependent(handle, &sdev->sdev_gendev);
1050}
1051
1052void ata_acpi_bind(struct ata_device *dev)
1053{
1054        ata_acpi_register_power_resource(dev);
1055        if (zpodd_dev_enabled(dev))
1056                dev_pm_qos_expose_flags(&dev->sdev->sdev_gendev, 0);
1057}
1058
1059void ata_acpi_unbind(struct ata_device *dev)
1060{
1061        ata_acpi_unregister_power_resource(dev);
1062}
1063
1064static int compat_pci_ata(struct ata_port *ap)
1065{
1066        struct device *dev = ap->tdev.parent;
1067        struct pci_dev *pdev;
1068
1069        if (!is_pci_dev(dev))
1070                return 0;
1071
1072        pdev = to_pci_dev(dev);
1073
1074        if ((pdev->class >> 8) != PCI_CLASS_STORAGE_SATA &&
1075            (pdev->class >> 8) != PCI_CLASS_STORAGE_IDE)
1076                return 0;
1077
1078        return 1;
1079}
1080
1081static int ata_acpi_bind_host(struct ata_port *ap, acpi_handle *handle)
1082{
1083        if (ap->flags & ATA_FLAG_ACPI_SATA)
1084                return -ENODEV;
1085
1086        *handle = acpi_get_child(DEVICE_ACPI_HANDLE(ap->tdev.parent),
1087                        ap->port_no);
1088
1089        if (!*handle)
1090                return -ENODEV;
1091
1092        if (__ata_acpi_gtm(ap, *handle, &ap->__acpi_init_gtm) == 0)
1093                ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
1094
1095        return 0;
1096}
1097
1098static int ata_acpi_bind_device(struct ata_port *ap, struct scsi_device *sdev,
1099                                acpi_handle *handle)
1100{
1101        struct ata_device *ata_dev;
1102
1103        if (ap->flags & ATA_FLAG_ACPI_SATA) {
1104                if (!sata_pmp_attached(ap))
1105                        ata_dev = &ap->link.device[sdev->id];
1106                else
1107                        ata_dev = &ap->pmp_link[sdev->channel].device[sdev->id];
1108        }
1109        else {
1110                ata_dev = &ap->link.device[sdev->id];
1111        }
1112
1113        *handle = ata_dev_acpi_handle(ata_dev);
1114
1115        if (!*handle)
1116                return -ENODEV;
1117
1118        return 0;
1119}
1120
1121static int is_ata_port(const struct device *dev)
1122{
1123        return dev->type == &ata_port_type;
1124}
1125
1126static struct ata_port *dev_to_ata_port(struct device *dev)
1127{
1128        while (!is_ata_port(dev)) {
1129                if (!dev->parent)
1130                        return NULL;
1131                dev = dev->parent;
1132        }
1133        return to_ata_port(dev);
1134}
1135
1136static int ata_acpi_find_device(struct device *dev, acpi_handle *handle)
1137{
1138        struct ata_port *ap = dev_to_ata_port(dev);
1139
1140        if (!ap)
1141                return -ENODEV;
1142
1143        if (!compat_pci_ata(ap))
1144                return -ENODEV;
1145
1146        if (scsi_is_host_device(dev))
1147                return ata_acpi_bind_host(ap, handle);
1148        else if (scsi_is_sdev_device(dev)) {
1149                struct scsi_device *sdev = to_scsi_device(dev);
1150
1151                return ata_acpi_bind_device(ap, sdev, handle);
1152        } else
1153                return -ENODEV;
1154}
1155
1156static struct acpi_bus_type ata_acpi_bus = {
1157        .name = "ATA",
1158        .find_device = ata_acpi_find_device,
1159};
1160
1161int ata_acpi_register(void)
1162{
1163        return scsi_register_acpi_bus_type(&ata_acpi_bus);
1164}
1165
1166void ata_acpi_unregister(void)
1167{
1168        scsi_unregister_acpi_bus_type(&ata_acpi_bus);
1169}
1170
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.