linux/drivers/ata/libahci.c
<<
>>
Prefs
   1/*
   2 *  libahci.c - Common AHCI SATA low-level routines
   3 *
   4 *  Maintained by:  Tejun Heo <tj@kernel.org>
   5 *                  Please ALWAYS copy linux-ide@vger.kernel.org
   6 *                  on emails.
   7 *
   8 *  Copyright 2004-2005 Red Hat, Inc.
   9 *
  10 *
  11 *  This program is free software; you can redistribute it and/or modify
  12 *  it under the terms of the GNU General Public License as published by
  13 *  the Free Software Foundation; either version 2, or (at your option)
  14 *  any later version.
  15 *
  16 *  This program is distributed in the hope that it will be useful,
  17 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  18 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19 *  GNU General Public License for more details.
  20 *
  21 *  You should have received a copy of the GNU General Public License
  22 *  along with this program; see the file COPYING.  If not, write to
  23 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24 *
  25 *
  26 * libata documentation is available via 'make {ps|pdf}docs',
  27 * as Documentation/driver-api/libata.rst
  28 *
  29 * AHCI hardware documentation:
  30 * http://www.intel.com/technology/serialata/pdf/rev1_0.pdf
  31 * http://www.intel.com/technology/serialata/pdf/rev1_1.pdf
  32 *
  33 */
  34
  35#include <linux/kernel.h>
  36#include <linux/gfp.h>
  37#include <linux/module.h>
  38#include <linux/nospec.h>
  39#include <linux/blkdev.h>
  40#include <linux/delay.h>
  41#include <linux/interrupt.h>
  42#include <linux/dma-mapping.h>
  43#include <linux/device.h>
  44#include <scsi/scsi_host.h>
  45#include <scsi/scsi_cmnd.h>
  46#include <linux/libata.h>
  47#include <linux/pci.h>
  48#include "ahci.h"
  49#include "libata.h"
  50
  51static int ahci_skip_host_reset;
  52int ahci_ignore_sss;
  53EXPORT_SYMBOL_GPL(ahci_ignore_sss);
  54
  55module_param_named(skip_host_reset, ahci_skip_host_reset, int, 0444);
  56MODULE_PARM_DESC(skip_host_reset, "skip global host reset (0=don't skip, 1=skip)");
  57
  58module_param_named(ignore_sss, ahci_ignore_sss, int, 0444);
  59MODULE_PARM_DESC(ignore_sss, "Ignore staggered spinup flag (0=don't ignore, 1=ignore)");
  60
  61static int ahci_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
  62                        unsigned hints);
  63static ssize_t ahci_led_show(struct ata_port *ap, char *buf);
  64static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
  65                              size_t size);
  66static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
  67                                        ssize_t size);
  68
  69
  70
  71static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val);
  72static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val);
  73static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc);
  74static int ahci_port_start(struct ata_port *ap);
  75static void ahci_port_stop(struct ata_port *ap);
  76static void ahci_qc_prep(struct ata_queued_cmd *qc);
  77static int ahci_pmp_qc_defer(struct ata_queued_cmd *qc);
  78static void ahci_freeze(struct ata_port *ap);
  79static void ahci_thaw(struct ata_port *ap);
  80static void ahci_set_aggressive_devslp(struct ata_port *ap, bool sleep);
  81static void ahci_enable_fbs(struct ata_port *ap);
  82static void ahci_disable_fbs(struct ata_port *ap);
  83static void ahci_pmp_attach(struct ata_port *ap);
  84static void ahci_pmp_detach(struct ata_port *ap);
  85static int ahci_softreset(struct ata_link *link, unsigned int *class,
  86                          unsigned long deadline);
  87static int ahci_pmp_retry_softreset(struct ata_link *link, unsigned int *class,
  88                          unsigned long deadline);
  89static int ahci_hardreset(struct ata_link *link, unsigned int *class,
  90                          unsigned long deadline);
  91static void ahci_postreset(struct ata_link *link, unsigned int *class);
  92static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
  93static void ahci_dev_config(struct ata_device *dev);
  94#ifdef CONFIG_PM
  95static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg);
  96#endif
  97static ssize_t ahci_activity_show(struct ata_device *dev, char *buf);
  98static ssize_t ahci_activity_store(struct ata_device *dev,
  99                                   enum sw_activity val);
 100static void ahci_init_sw_activity(struct ata_link *link);
 101
 102static ssize_t ahci_show_host_caps(struct device *dev,
 103                                   struct device_attribute *attr, char *buf);
 104static ssize_t ahci_show_host_cap2(struct device *dev,
 105                                   struct device_attribute *attr, char *buf);
 106static ssize_t ahci_show_host_version(struct device *dev,
 107                                      struct device_attribute *attr, char *buf);
 108static ssize_t ahci_show_port_cmd(struct device *dev,
 109                                  struct device_attribute *attr, char *buf);
 110static ssize_t ahci_read_em_buffer(struct device *dev,
 111                                   struct device_attribute *attr, char *buf);
 112static ssize_t ahci_store_em_buffer(struct device *dev,
 113                                    struct device_attribute *attr,
 114                                    const char *buf, size_t size);
 115static ssize_t ahci_show_em_supported(struct device *dev,
 116                                      struct device_attribute *attr, char *buf);
 117static irqreturn_t ahci_single_level_irq_intr(int irq, void *dev_instance);
 118
 119static DEVICE_ATTR(ahci_host_caps, S_IRUGO, ahci_show_host_caps, NULL);
 120static DEVICE_ATTR(ahci_host_cap2, S_IRUGO, ahci_show_host_cap2, NULL);
 121static DEVICE_ATTR(ahci_host_version, S_IRUGO, ahci_show_host_version, NULL);
 122static DEVICE_ATTR(ahci_port_cmd, S_IRUGO, ahci_show_port_cmd, NULL);
 123static DEVICE_ATTR(em_buffer, S_IWUSR | S_IRUGO,
 124                   ahci_read_em_buffer, ahci_store_em_buffer);
 125static DEVICE_ATTR(em_message_supported, S_IRUGO, ahci_show_em_supported, NULL);
 126
 127struct device_attribute *ahci_shost_attrs[] = {
 128        &dev_attr_link_power_management_policy,
 129        &dev_attr_em_message_type,
 130        &dev_attr_em_message,
 131        &dev_attr_ahci_host_caps,
 132        &dev_attr_ahci_host_cap2,
 133        &dev_attr_ahci_host_version,
 134        &dev_attr_ahci_port_cmd,
 135        &dev_attr_em_buffer,
 136        &dev_attr_em_message_supported,
 137        NULL
 138};
 139EXPORT_SYMBOL_GPL(ahci_shost_attrs);
 140
 141struct device_attribute *ahci_sdev_attrs[] = {
 142        &dev_attr_sw_activity,
 143        &dev_attr_unload_heads,
 144        &dev_attr_ncq_prio_enable,
 145        NULL
 146};
 147EXPORT_SYMBOL_GPL(ahci_sdev_attrs);
 148
 149struct ata_port_operations ahci_ops = {
 150        .inherits               = &sata_pmp_port_ops,
 151
 152        .qc_defer               = ahci_pmp_qc_defer,
 153        .qc_prep                = ahci_qc_prep,
 154        .qc_issue               = ahci_qc_issue,
 155        .qc_fill_rtf            = ahci_qc_fill_rtf,
 156
 157        .freeze                 = ahci_freeze,
 158        .thaw                   = ahci_thaw,
 159        .softreset              = ahci_softreset,
 160        .hardreset              = ahci_hardreset,
 161        .postreset              = ahci_postreset,
 162        .pmp_softreset          = ahci_softreset,
 163        .error_handler          = ahci_error_handler,
 164        .post_internal_cmd      = ahci_post_internal_cmd,
 165        .dev_config             = ahci_dev_config,
 166
 167        .scr_read               = ahci_scr_read,
 168        .scr_write              = ahci_scr_write,
 169        .pmp_attach             = ahci_pmp_attach,
 170        .pmp_detach             = ahci_pmp_detach,
 171
 172        .set_lpm                = ahci_set_lpm,
 173        .em_show                = ahci_led_show,
 174        .em_store               = ahci_led_store,
 175        .sw_activity_show       = ahci_activity_show,
 176        .sw_activity_store      = ahci_activity_store,
 177        .transmit_led_message   = ahci_transmit_led_message,
 178#ifdef CONFIG_PM
 179        .port_suspend           = ahci_port_suspend,
 180        .port_resume            = ahci_port_resume,
 181#endif
 182        .port_start             = ahci_port_start,
 183        .port_stop              = ahci_port_stop,
 184};
 185EXPORT_SYMBOL_GPL(ahci_ops);
 186
 187struct ata_port_operations ahci_pmp_retry_srst_ops = {
 188        .inherits               = &ahci_ops,
 189        .softreset              = ahci_pmp_retry_softreset,
 190};
 191EXPORT_SYMBOL_GPL(ahci_pmp_retry_srst_ops);
 192
 193static bool ahci_em_messages __read_mostly = true;
 194EXPORT_SYMBOL_GPL(ahci_em_messages);
 195module_param(ahci_em_messages, bool, 0444);
 196/* add other LED protocol types when they become supported */
 197MODULE_PARM_DESC(ahci_em_messages,
 198        "AHCI Enclosure Management Message control (0 = off, 1 = on)");
 199
 200/* device sleep idle timeout in ms */
 201static int devslp_idle_timeout __read_mostly = 1000;
 202module_param(devslp_idle_timeout, int, 0644);
 203MODULE_PARM_DESC(devslp_idle_timeout, "device sleep idle timeout");
 204
 205static void ahci_enable_ahci(void __iomem *mmio)
 206{
 207        int i;
 208        u32 tmp;
 209
 210        /* turn on AHCI_EN */
 211        tmp = readl(mmio + HOST_CTL);
 212        if (tmp & HOST_AHCI_EN)
 213                return;
 214
 215        /* Some controllers need AHCI_EN to be written multiple times.
 216         * Try a few times before giving up.
 217         */
 218        for (i = 0; i < 5; i++) {
 219                tmp |= HOST_AHCI_EN;
 220                writel(tmp, mmio + HOST_CTL);
 221                tmp = readl(mmio + HOST_CTL);   /* flush && sanity check */
 222                if (tmp & HOST_AHCI_EN)
 223                        return;
 224                msleep(10);
 225        }
 226
 227        WARN_ON(1);
 228}
 229
 230/**
 231 *      ahci_rpm_get_port - Make sure the port is powered on
 232 *      @ap: Port to power on
 233 *
 234 *      Whenever there is need to access the AHCI host registers outside of
 235 *      normal execution paths, call this function to make sure the host is
 236 *      actually powered on.
 237 */
 238static int ahci_rpm_get_port(struct ata_port *ap)
 239{
 240        return pm_runtime_get_sync(ap->dev);
 241}
 242
 243/**
 244 *      ahci_rpm_put_port - Undoes ahci_rpm_get_port()
 245 *      @ap: Port to power down
 246 *
 247 *      Undoes ahci_rpm_get_port() and possibly powers down the AHCI host
 248 *      if it has no more active users.
 249 */
 250static void ahci_rpm_put_port(struct ata_port *ap)
 251{
 252        pm_runtime_put(ap->dev);
 253}
 254
 255static ssize_t ahci_show_host_caps(struct device *dev,
 256                                   struct device_attribute *attr, char *buf)
 257{
 258        struct Scsi_Host *shost = class_to_shost(dev);
 259        struct ata_port *ap = ata_shost_to_port(shost);
 260        struct ahci_host_priv *hpriv = ap->host->private_data;
 261
 262        return sprintf(buf, "%x\n", hpriv->cap);
 263}
 264
 265static ssize_t ahci_show_host_cap2(struct device *dev,
 266                                   struct device_attribute *attr, char *buf)
 267{
 268        struct Scsi_Host *shost = class_to_shost(dev);
 269        struct ata_port *ap = ata_shost_to_port(shost);
 270        struct ahci_host_priv *hpriv = ap->host->private_data;
 271
 272        return sprintf(buf, "%x\n", hpriv->cap2);
 273}
 274
 275static ssize_t ahci_show_host_version(struct device *dev,
 276                                   struct device_attribute *attr, char *buf)
 277{
 278        struct Scsi_Host *shost = class_to_shost(dev);
 279        struct ata_port *ap = ata_shost_to_port(shost);
 280        struct ahci_host_priv *hpriv = ap->host->private_data;
 281
 282        return sprintf(buf, "%x\n", hpriv->version);
 283}
 284
 285static ssize_t ahci_show_port_cmd(struct device *dev,
 286                                  struct device_attribute *attr, char *buf)
 287{
 288        struct Scsi_Host *shost = class_to_shost(dev);
 289        struct ata_port *ap = ata_shost_to_port(shost);
 290        void __iomem *port_mmio = ahci_port_base(ap);
 291        ssize_t ret;
 292
 293        ahci_rpm_get_port(ap);
 294        ret = sprintf(buf, "%x\n", readl(port_mmio + PORT_CMD));
 295        ahci_rpm_put_port(ap);
 296
 297        return ret;
 298}
 299
 300static ssize_t ahci_read_em_buffer(struct device *dev,
 301                                   struct device_attribute *attr, char *buf)
 302{
 303        struct Scsi_Host *shost = class_to_shost(dev);
 304        struct ata_port *ap = ata_shost_to_port(shost);
 305        struct ahci_host_priv *hpriv = ap->host->private_data;
 306        void __iomem *mmio = hpriv->mmio;
 307        void __iomem *em_mmio = mmio + hpriv->em_loc;
 308        u32 em_ctl, msg;
 309        unsigned long flags;
 310        size_t count;
 311        int i;
 312
 313        ahci_rpm_get_port(ap);
 314        spin_lock_irqsave(ap->lock, flags);
 315
 316        em_ctl = readl(mmio + HOST_EM_CTL);
 317        if (!(ap->flags & ATA_FLAG_EM) || em_ctl & EM_CTL_XMT ||
 318            !(hpriv->em_msg_type & EM_MSG_TYPE_SGPIO)) {
 319                spin_unlock_irqrestore(ap->lock, flags);
 320                ahci_rpm_put_port(ap);
 321                return -EINVAL;
 322        }
 323
 324        if (!(em_ctl & EM_CTL_MR)) {
 325                spin_unlock_irqrestore(ap->lock, flags);
 326                ahci_rpm_put_port(ap);
 327                return -EAGAIN;
 328        }
 329
 330        if (!(em_ctl & EM_CTL_SMB))
 331                em_mmio += hpriv->em_buf_sz;
 332
 333        count = hpriv->em_buf_sz;
 334
 335        /* the count should not be larger than PAGE_SIZE */
 336        if (count > PAGE_SIZE) {
 337                if (printk_ratelimit())
 338                        ata_port_warn(ap,
 339                                      "EM read buffer size too large: "
 340                                      "buffer size %u, page size %lu\n",
 341                                      hpriv->em_buf_sz, PAGE_SIZE);
 342                count = PAGE_SIZE;
 343        }
 344
 345        for (i = 0; i < count; i += 4) {
 346                msg = readl(em_mmio + i);
 347                buf[i] = msg & 0xff;
 348                buf[i + 1] = (msg >> 8) & 0xff;
 349                buf[i + 2] = (msg >> 16) & 0xff;
 350                buf[i + 3] = (msg >> 24) & 0xff;
 351        }
 352
 353        spin_unlock_irqrestore(ap->lock, flags);
 354        ahci_rpm_put_port(ap);
 355
 356        return i;
 357}
 358
 359static ssize_t ahci_store_em_buffer(struct device *dev,
 360                                    struct device_attribute *attr,
 361                                    const char *buf, size_t size)
 362{
 363        struct Scsi_Host *shost = class_to_shost(dev);
 364        struct ata_port *ap = ata_shost_to_port(shost);
 365        struct ahci_host_priv *hpriv = ap->host->private_data;
 366        void __iomem *mmio = hpriv->mmio;
 367        void __iomem *em_mmio = mmio + hpriv->em_loc;
 368        const unsigned char *msg_buf = buf;
 369        u32 em_ctl, msg;
 370        unsigned long flags;
 371        int i;
 372
 373        /* check size validity */
 374        if (!(ap->flags & ATA_FLAG_EM) ||
 375            !(hpriv->em_msg_type & EM_MSG_TYPE_SGPIO) ||
 376            size % 4 || size > hpriv->em_buf_sz)
 377                return -EINVAL;
 378
 379        ahci_rpm_get_port(ap);
 380        spin_lock_irqsave(ap->lock, flags);
 381
 382        em_ctl = readl(mmio + HOST_EM_CTL);
 383        if (em_ctl & EM_CTL_TM) {
 384                spin_unlock_irqrestore(ap->lock, flags);
 385                ahci_rpm_put_port(ap);
 386                return -EBUSY;
 387        }
 388
 389        for (i = 0; i < size; i += 4) {
 390                msg = msg_buf[i] | msg_buf[i + 1] << 8 |
 391                      msg_buf[i + 2] << 16 | msg_buf[i + 3] << 24;
 392                writel(msg, em_mmio + i);
 393        }
 394
 395        writel(em_ctl | EM_CTL_TM, mmio + HOST_EM_CTL);
 396
 397        spin_unlock_irqrestore(ap->lock, flags);
 398        ahci_rpm_put_port(ap);
 399
 400        return size;
 401}
 402
 403static ssize_t ahci_show_em_supported(struct device *dev,
 404                                      struct device_attribute *attr, char *buf)
 405{
 406        struct Scsi_Host *shost = class_to_shost(dev);
 407        struct ata_port *ap = ata_shost_to_port(shost);
 408        struct ahci_host_priv *hpriv = ap->host->private_data;
 409        void __iomem *mmio = hpriv->mmio;
 410        u32 em_ctl;
 411
 412        ahci_rpm_get_port(ap);
 413        em_ctl = readl(mmio + HOST_EM_CTL);
 414        ahci_rpm_put_port(ap);
 415
 416        return sprintf(buf, "%s%s%s%s\n",
 417                       em_ctl & EM_CTL_LED ? "led " : "",
 418                       em_ctl & EM_CTL_SAFTE ? "saf-te " : "",
 419                       em_ctl & EM_CTL_SES ? "ses-2 " : "",
 420                       em_ctl & EM_CTL_SGPIO ? "sgpio " : "");
 421}
 422
 423/**
 424 *      ahci_save_initial_config - Save and fixup initial config values
 425 *      @dev: target AHCI device
 426 *      @hpriv: host private area to store config values
 427 *
 428 *      Some registers containing configuration info might be setup by
 429 *      BIOS and might be cleared on reset.  This function saves the
 430 *      initial values of those registers into @hpriv such that they
 431 *      can be restored after controller reset.
 432 *
 433 *      If inconsistent, config values are fixed up by this function.
 434 *
 435 *      If it is not set already this function sets hpriv->start_engine to
 436 *      ahci_start_engine.
 437 *
 438 *      LOCKING:
 439 *      None.
 440 */
 441void ahci_save_initial_config(struct device *dev, struct ahci_host_priv *hpriv)
 442{
 443        void __iomem *mmio = hpriv->mmio;
 444        u32 cap, cap2, vers, port_map;
 445        int i;
 446
 447        /* make sure AHCI mode is enabled before accessing CAP */
 448        ahci_enable_ahci(mmio);
 449
 450        /* Values prefixed with saved_ are written back to host after
 451         * reset.  Values without are used for driver operation.
 452         */
 453        hpriv->saved_cap = cap = readl(mmio + HOST_CAP);
 454        hpriv->saved_port_map = port_map = readl(mmio + HOST_PORTS_IMPL);
 455
 456        /* CAP2 register is only defined for AHCI 1.2 and later */
 457        vers = readl(mmio + HOST_VERSION);
 458        if ((vers >> 16) > 1 ||
 459           ((vers >> 16) == 1 && (vers & 0xFFFF) >= 0x200))
 460                hpriv->saved_cap2 = cap2 = readl(mmio + HOST_CAP2);
 461        else
 462                hpriv->saved_cap2 = cap2 = 0;
 463
 464        /* some chips have errata preventing 64bit use */
 465        if ((cap & HOST_CAP_64) && (hpriv->flags & AHCI_HFLAG_32BIT_ONLY)) {
 466                dev_info(dev, "controller can't do 64bit DMA, forcing 32bit\n");
 467                cap &= ~HOST_CAP_64;
 468        }
 469
 470        if ((cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_NO_NCQ)) {
 471                dev_info(dev, "controller can't do NCQ, turning off CAP_NCQ\n");
 472                cap &= ~HOST_CAP_NCQ;
 473        }
 474
 475        if (!(cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_YES_NCQ)) {
 476                dev_info(dev, "controller can do NCQ, turning on CAP_NCQ\n");
 477                cap |= HOST_CAP_NCQ;
 478        }
 479
 480        if ((cap & HOST_CAP_PMP) && (hpriv->flags & AHCI_HFLAG_NO_PMP)) {
 481                dev_info(dev, "controller can't do PMP, turning off CAP_PMP\n");
 482                cap &= ~HOST_CAP_PMP;
 483        }
 484
 485        if ((cap & HOST_CAP_SNTF) && (hpriv->flags & AHCI_HFLAG_NO_SNTF)) {
 486                dev_info(dev,
 487                         "controller can't do SNTF, turning off CAP_SNTF\n");
 488                cap &= ~HOST_CAP_SNTF;
 489        }
 490
 491        if ((cap2 & HOST_CAP2_SDS) && (hpriv->flags & AHCI_HFLAG_NO_DEVSLP)) {
 492                dev_info(dev,
 493                         "controller can't do DEVSLP, turning off\n");
 494                cap2 &= ~HOST_CAP2_SDS;
 495                cap2 &= ~HOST_CAP2_SADM;
 496        }
 497
 498        if (!(cap & HOST_CAP_FBS) && (hpriv->flags & AHCI_HFLAG_YES_FBS)) {
 499                dev_info(dev, "controller can do FBS, turning on CAP_FBS\n");
 500                cap |= HOST_CAP_FBS;
 501        }
 502
 503        if ((cap & HOST_CAP_FBS) && (hpriv->flags & AHCI_HFLAG_NO_FBS)) {
 504                dev_info(dev, "controller can't do FBS, turning off CAP_FBS\n");
 505                cap &= ~HOST_CAP_FBS;
 506        }
 507
 508        if (!(cap & HOST_CAP_ALPM) && (hpriv->flags & AHCI_HFLAG_YES_ALPM)) {
 509                dev_info(dev, "controller can do ALPM, turning on CAP_ALPM\n");
 510                cap |= HOST_CAP_ALPM;
 511        }
 512
 513        if (hpriv->force_port_map && port_map != hpriv->force_port_map) {
 514                dev_info(dev, "forcing port_map 0x%x -> 0x%x\n",
 515                         port_map, hpriv->force_port_map);
 516                port_map = hpriv->force_port_map;
 517                hpriv->saved_port_map = port_map;
 518        }
 519
 520        if (hpriv->mask_port_map) {
 521                dev_warn(dev, "masking port_map 0x%x -> 0x%x\n",
 522                        port_map,
 523                        port_map & hpriv->mask_port_map);
 524                port_map &= hpriv->mask_port_map;
 525        }
 526
 527        /* cross check port_map and cap.n_ports */
 528        if (port_map) {
 529                int map_ports = 0;
 530
 531                for (i = 0; i < AHCI_MAX_PORTS; i++)
 532                        if (port_map & (1 << i))
 533                                map_ports++;
 534
 535                /* If PI has more ports than n_ports, whine, clear
 536                 * port_map and let it be generated from n_ports.
 537                 */
 538                if (map_ports > ahci_nr_ports(cap)) {
 539                        dev_warn(dev,
 540                                 "implemented port map (0x%x) contains more ports than nr_ports (%u), using nr_ports\n",
 541                                 port_map, ahci_nr_ports(cap));
 542                        port_map = 0;
 543                }
 544        }
 545
 546        /* fabricate port_map from cap.nr_ports for < AHCI 1.3 */
 547        if (!port_map && vers < 0x10300) {
 548                port_map = (1 << ahci_nr_ports(cap)) - 1;
 549                dev_warn(dev, "forcing PORTS_IMPL to 0x%x\n", port_map);
 550
 551                /* write the fixed up value to the PI register */
 552                hpriv->saved_port_map = port_map;
 553        }
 554
 555        /* record values to use during operation */
 556        hpriv->cap = cap;
 557        hpriv->cap2 = cap2;
 558        hpriv->version = readl(mmio + HOST_VERSION);
 559        hpriv->port_map = port_map;
 560
 561        if (!hpriv->start_engine)
 562                hpriv->start_engine = ahci_start_engine;
 563
 564        if (!hpriv->stop_engine)
 565                hpriv->stop_engine = ahci_stop_engine;
 566
 567        if (!hpriv->irq_handler)
 568                hpriv->irq_handler = ahci_single_level_irq_intr;
 569}
 570EXPORT_SYMBOL_GPL(ahci_save_initial_config);
 571
 572/**
 573 *      ahci_restore_initial_config - Restore initial config
 574 *      @host: target ATA host
 575 *
 576 *      Restore initial config stored by ahci_save_initial_config().
 577 *
 578 *      LOCKING:
 579 *      None.
 580 */
 581static void ahci_restore_initial_config(struct ata_host *host)
 582{
 583        struct ahci_host_priv *hpriv = host->private_data;
 584        void __iomem *mmio = hpriv->mmio;
 585
 586        writel(hpriv->saved_cap, mmio + HOST_CAP);
 587        if (hpriv->saved_cap2)
 588                writel(hpriv->saved_cap2, mmio + HOST_CAP2);
 589        writel(hpriv->saved_port_map, mmio + HOST_PORTS_IMPL);
 590        (void) readl(mmio + HOST_PORTS_IMPL);   /* flush */
 591}
 592
 593static unsigned ahci_scr_offset(struct ata_port *ap, unsigned int sc_reg)
 594{
 595        static const int offset[] = {
 596                [SCR_STATUS]            = PORT_SCR_STAT,
 597                [SCR_CONTROL]           = PORT_SCR_CTL,
 598                [SCR_ERROR]             = PORT_SCR_ERR,
 599                [SCR_ACTIVE]            = PORT_SCR_ACT,
 600                [SCR_NOTIFICATION]      = PORT_SCR_NTF,
 601        };
 602        struct ahci_host_priv *hpriv = ap->host->private_data;
 603
 604        if (sc_reg < ARRAY_SIZE(offset) &&
 605            (sc_reg != SCR_NOTIFICATION || (hpriv->cap & HOST_CAP_SNTF)))
 606                return offset[sc_reg];
 607        return 0;
 608}
 609
 610static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val)
 611{
 612        void __iomem *port_mmio = ahci_port_base(link->ap);
 613        int offset = ahci_scr_offset(link->ap, sc_reg);
 614
 615        if (offset) {
 616                *val = readl(port_mmio + offset);
 617                return 0;
 618        }
 619        return -EINVAL;
 620}
 621
 622static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val)
 623{
 624        void __iomem *port_mmio = ahci_port_base(link->ap);
 625        int offset = ahci_scr_offset(link->ap, sc_reg);
 626
 627        if (offset) {
 628                writel(val, port_mmio + offset);
 629                return 0;
 630        }
 631        return -EINVAL;
 632}
 633
 634void ahci_start_engine(struct ata_port *ap)
 635{
 636        void __iomem *port_mmio = ahci_port_base(ap);
 637        u32 tmp;
 638
 639        /* start DMA */
 640        tmp = readl(port_mmio + PORT_CMD);
 641        tmp |= PORT_CMD_START;
 642        writel(tmp, port_mmio + PORT_CMD);
 643        readl(port_mmio + PORT_CMD); /* flush */
 644}
 645EXPORT_SYMBOL_GPL(ahci_start_engine);
 646
 647int ahci_stop_engine(struct ata_port *ap)
 648{
 649        void __iomem *port_mmio = ahci_port_base(ap);
 650        struct ahci_host_priv *hpriv = ap->host->private_data;
 651        u32 tmp;
 652
 653        /*
 654         * On some controllers, stopping a port's DMA engine while the port
 655         * is in ALPM state (partial or slumber) results in failures on
 656         * subsequent DMA engine starts.  For those controllers, put the
 657         * port back in active state before stopping its DMA engine.
 658         */
 659        if ((hpriv->flags & AHCI_HFLAG_WAKE_BEFORE_STOP) &&
 660            (ap->link.lpm_policy > ATA_LPM_MAX_POWER) &&
 661            ahci_set_lpm(&ap->link, ATA_LPM_MAX_POWER, ATA_LPM_WAKE_ONLY)) {
 662                dev_err(ap->host->dev, "Failed to wake up port before engine stop\n");
 663                return -EIO;
 664        }
 665
 666        tmp = readl(port_mmio + PORT_CMD);
 667
 668        /* check if the HBA is idle */
 669        if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
 670                return 0;
 671
 672        /*
 673         * Don't try to issue commands but return with ENODEV if the
 674         * AHCI controller not available anymore (e.g. due to PCIe hot
 675         * unplugging). Otherwise a 500ms delay for each port is added.
 676         */
 677        if (tmp == 0xffffffff) {
 678                dev_err(ap->host->dev, "AHCI controller unavailable!\n");
 679                return -ENODEV;
 680        }
 681
 682        /* setting HBA to idle */
 683        tmp &= ~PORT_CMD_START;
 684        writel(tmp, port_mmio + PORT_CMD);
 685
 686#ifdef CONFIG_ARM64
 687        /* Rev Ax of Cavium CN99XX needs a hack for port stop */
 688        if (dev_is_pci(ap->host->dev) &&
 689            to_pci_dev(ap->host->dev)->vendor == 0x14e4 &&
 690            to_pci_dev(ap->host->dev)->device == 0x9027 &&
 691            MIDR_IS_CPU_MODEL_RANGE(read_cpuid_id(),
 692                        MIDR_CPU_MODEL(ARM_CPU_IMP_BRCM, BRCM_CPU_PART_VULCAN),
 693                        MIDR_CPU_VAR_REV(0, 0),
 694                        MIDR_CPU_VAR_REV(0, MIDR_REVISION_MASK))) {
 695                tmp = readl(hpriv->mmio + 0x8000);
 696                udelay(100);
 697                writel(tmp | (1 << 26), hpriv->mmio + 0x8000);
 698                udelay(100);
 699                writel(tmp & ~(1 << 26), hpriv->mmio + 0x8000);
 700                dev_warn(ap->host->dev, "CN99XX SATA reset workaround applied\n");
 701        }
 702#endif
 703
 704        /* wait for engine to stop. This could be as long as 500 msec */
 705        tmp = ata_wait_register(ap, port_mmio + PORT_CMD,
 706                                PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
 707        if (tmp & PORT_CMD_LIST_ON)
 708                return -EIO;
 709
 710        return 0;
 711}
 712EXPORT_SYMBOL_GPL(ahci_stop_engine);
 713
 714void ahci_start_fis_rx(struct ata_port *ap)
 715{
 716        void __iomem *port_mmio = ahci_port_base(ap);
 717        struct ahci_host_priv *hpriv = ap->host->private_data;
 718        struct ahci_port_priv *pp = ap->private_data;
 719        u32 tmp;
 720
 721        /* set FIS registers */
 722        if (hpriv->cap & HOST_CAP_64)
 723                writel((pp->cmd_slot_dma >> 16) >> 16,
 724                       port_mmio + PORT_LST_ADDR_HI);
 725        writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
 726
 727        if (hpriv->cap & HOST_CAP_64)
 728                writel((pp->rx_fis_dma >> 16) >> 16,
 729                       port_mmio + PORT_FIS_ADDR_HI);
 730        writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
 731
 732        /* enable FIS reception */
 733        tmp = readl(port_mmio + PORT_CMD);
 734        tmp |= PORT_CMD_FIS_RX;
 735        writel(tmp, port_mmio + PORT_CMD);
 736
 737        /* flush */
 738        readl(port_mmio + PORT_CMD);
 739}
 740EXPORT_SYMBOL_GPL(ahci_start_fis_rx);
 741
 742static int ahci_stop_fis_rx(struct ata_port *ap)
 743{
 744        void __iomem *port_mmio = ahci_port_base(ap);
 745        u32 tmp;
 746
 747        /* disable FIS reception */
 748        tmp = readl(port_mmio + PORT_CMD);
 749        tmp &= ~PORT_CMD_FIS_RX;
 750        writel(tmp, port_mmio + PORT_CMD);
 751
 752        /* wait for completion, spec says 500ms, give it 1000 */
 753        tmp = ata_wait_register(ap, port_mmio + PORT_CMD, PORT_CMD_FIS_ON,
 754                                PORT_CMD_FIS_ON, 10, 1000);
 755        if (tmp & PORT_CMD_FIS_ON)
 756                return -EBUSY;
 757
 758        return 0;
 759}
 760
 761static void ahci_power_up(struct ata_port *ap)
 762{
 763        struct ahci_host_priv *hpriv = ap->host->private_data;
 764        void __iomem *port_mmio = ahci_port_base(ap);
 765        u32 cmd;
 766
 767        cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
 768
 769        /* spin up device */
 770        if (hpriv->cap & HOST_CAP_SSS) {
 771                cmd |= PORT_CMD_SPIN_UP;
 772                writel(cmd, port_mmio + PORT_CMD);
 773        }
 774
 775        /* wake up link */
 776        writel(cmd | PORT_CMD_ICC_ACTIVE, port_mmio + PORT_CMD);
 777}
 778
 779static int ahci_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
 780                        unsigned int hints)
 781{
 782        struct ata_port *ap = link->ap;
 783        struct ahci_host_priv *hpriv = ap->host->private_data;
 784        struct ahci_port_priv *pp = ap->private_data;
 785        void __iomem *port_mmio = ahci_port_base(ap);
 786
 787        if (policy != ATA_LPM_MAX_POWER) {
 788                /* wakeup flag only applies to the max power policy */
 789                hints &= ~ATA_LPM_WAKE_ONLY;
 790
 791                /*
 792                 * Disable interrupts on Phy Ready. This keeps us from
 793                 * getting woken up due to spurious phy ready
 794                 * interrupts.
 795                 */
 796                pp->intr_mask &= ~PORT_IRQ_PHYRDY;
 797                writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
 798
 799                sata_link_scr_lpm(link, policy, false);
 800        }
 801
 802        if (hpriv->cap & HOST_CAP_ALPM) {
 803                u32 cmd = readl(port_mmio + PORT_CMD);
 804
 805                if (policy == ATA_LPM_MAX_POWER || !(hints & ATA_LPM_HIPM)) {
 806                        if (!(hints & ATA_LPM_WAKE_ONLY))
 807                                cmd &= ~(PORT_CMD_ASP | PORT_CMD_ALPE);
 808                        cmd |= PORT_CMD_ICC_ACTIVE;
 809
 810                        writel(cmd, port_mmio + PORT_CMD);
 811                        readl(port_mmio + PORT_CMD);
 812
 813                        /* wait 10ms to be sure we've come out of LPM state */
 814                        ata_msleep(ap, 10);
 815
 816                        if (hints & ATA_LPM_WAKE_ONLY)
 817                                return 0;
 818                } else {
 819                        cmd |= PORT_CMD_ALPE;
 820                        if (policy == ATA_LPM_MIN_POWER)
 821                                cmd |= PORT_CMD_ASP;
 822
 823                        /* write out new cmd value */
 824                        writel(cmd, port_mmio + PORT_CMD);
 825                }
 826        }
 827
 828        /* set aggressive device sleep */
 829        if ((hpriv->cap2 & HOST_CAP2_SDS) &&
 830            (hpriv->cap2 & HOST_CAP2_SADM) &&
 831            (link->device->flags & ATA_DFLAG_DEVSLP)) {
 832                if (policy == ATA_LPM_MIN_POWER)
 833                        ahci_set_aggressive_devslp(ap, true);
 834                else
 835                        ahci_set_aggressive_devslp(ap, false);
 836        }
 837
 838        if (policy == ATA_LPM_MAX_POWER) {
 839                sata_link_scr_lpm(link, policy, false);
 840
 841                /* turn PHYRDY IRQ back on */
 842                pp->intr_mask |= PORT_IRQ_PHYRDY;
 843                writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
 844        }
 845
 846        return 0;
 847}
 848
 849#ifdef CONFIG_PM
 850static void ahci_power_down(struct ata_port *ap)
 851{
 852        struct ahci_host_priv *hpriv = ap->host->private_data;
 853        void __iomem *port_mmio = ahci_port_base(ap);
 854        u32 cmd, scontrol;
 855
 856        if (!(hpriv->cap & HOST_CAP_SSS))
 857                return;
 858
 859        /* put device into listen mode, first set PxSCTL.DET to 0 */
 860        scontrol = readl(port_mmio + PORT_SCR_CTL);
 861        scontrol &= ~0xf;
 862        writel(scontrol, port_mmio + PORT_SCR_CTL);
 863
 864        /* then set PxCMD.SUD to 0 */
 865        cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
 866        cmd &= ~PORT_CMD_SPIN_UP;
 867        writel(cmd, port_mmio + PORT_CMD);
 868}
 869#endif
 870
 871static void ahci_start_port(struct ata_port *ap)
 872{
 873        struct ahci_host_priv *hpriv = ap->host->private_data;
 874        struct ahci_port_priv *pp = ap->private_data;
 875        struct ata_link *link;
 876        struct ahci_em_priv *emp;
 877        ssize_t rc;
 878        int i;
 879
 880        /* enable FIS reception */
 881        ahci_start_fis_rx(ap);
 882
 883        /* enable DMA */
 884        if (!(hpriv->flags & AHCI_HFLAG_DELAY_ENGINE))
 885                hpriv->start_engine(ap);
 886
 887        /* turn on LEDs */
 888        if (ap->flags & ATA_FLAG_EM) {
 889                ata_for_each_link(link, ap, EDGE) {
 890                        emp = &pp->em_priv[link->pmp];
 891
 892                        /* EM Transmit bit maybe busy during init */
 893                        for (i = 0; i < EM_MAX_RETRY; i++) {
 894                                rc = ap->ops->transmit_led_message(ap,
 895                                                               emp->led_state,
 896                                                               4);
 897                                /*
 898                                 * If busy, give a breather but do not
 899                                 * release EH ownership by using msleep()
 900                                 * instead of ata_msleep().  EM Transmit
 901                                 * bit is busy for the whole host and
 902                                 * releasing ownership will cause other
 903                                 * ports to fail the same way.
 904                                 */
 905                                if (rc == -EBUSY)
 906                                        msleep(1);
 907                                else
 908                                        break;
 909                        }
 910                }
 911        }
 912
 913        if (ap->flags & ATA_FLAG_SW_ACTIVITY)
 914                ata_for_each_link(link, ap, EDGE)
 915                        ahci_init_sw_activity(link);
 916
 917}
 918
 919static int ahci_deinit_port(struct ata_port *ap, const char **emsg)
 920{
 921        int rc;
 922        struct ahci_host_priv *hpriv = ap->host->private_data;
 923
 924        /* disable DMA */
 925        rc = hpriv->stop_engine(ap);
 926        if (rc) {
 927                *emsg = "failed to stop engine";
 928                return rc;
 929        }
 930
 931        /* disable FIS reception */
 932        rc = ahci_stop_fis_rx(ap);
 933        if (rc) {
 934                *emsg = "failed stop FIS RX";
 935                return rc;
 936        }
 937
 938        return 0;
 939}
 940
 941int ahci_reset_controller(struct ata_host *host)
 942{
 943        struct ahci_host_priv *hpriv = host->private_data;
 944        void __iomem *mmio = hpriv->mmio;
 945        u32 tmp;
 946
 947        /* we must be in AHCI mode, before using anything
 948         * AHCI-specific, such as HOST_RESET.
 949         */
 950        ahci_enable_ahci(mmio);
 951
 952        /* global controller reset */
 953        if (!ahci_skip_host_reset) {
 954                tmp = readl(mmio + HOST_CTL);
 955                if ((tmp & HOST_RESET) == 0) {
 956                        writel(tmp | HOST_RESET, mmio + HOST_CTL);
 957                        readl(mmio + HOST_CTL); /* flush */
 958                }
 959
 960                /*
 961                 * to perform host reset, OS should set HOST_RESET
 962                 * and poll until this bit is read to be "0".
 963                 * reset must complete within 1 second, or
 964                 * the hardware should be considered fried.
 965                 */
 966                tmp = ata_wait_register(NULL, mmio + HOST_CTL, HOST_RESET,
 967                                        HOST_RESET, 10, 1000);
 968
 969                if (tmp & HOST_RESET) {
 970                        dev_err(host->dev, "controller reset failed (0x%x)\n",
 971                                tmp);
 972                        return -EIO;
 973                }
 974
 975                /* turn on AHCI mode */
 976                ahci_enable_ahci(mmio);
 977
 978                /* Some registers might be cleared on reset.  Restore
 979                 * initial values.
 980                 */
 981                if (!(hpriv->flags & AHCI_HFLAG_NO_WRITE_TO_RO))
 982                        ahci_restore_initial_config(host);
 983        } else
 984                dev_info(host->dev, "skipping global host reset\n");
 985
 986        return 0;
 987}
 988EXPORT_SYMBOL_GPL(ahci_reset_controller);
 989
 990static void ahci_sw_activity(struct ata_link *link)
 991{
 992        struct ata_port *ap = link->ap;
 993        struct ahci_port_priv *pp = ap->private_data;
 994        struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
 995
 996        if (!(link->flags & ATA_LFLAG_SW_ACTIVITY))
 997                return;
 998
 999        emp->activity++;
1000        if (!timer_pending(&emp->timer))
1001                mod_timer(&emp->timer, jiffies + msecs_to_jiffies(10));
1002}
1003
1004static void ahci_sw_activity_blink(struct timer_list *t)
1005{
1006        struct ahci_em_priv *emp = from_timer(emp, t, timer);
1007        struct ata_link *link = emp->link;
1008        struct ata_port *ap = link->ap;
1009
1010        unsigned long led_message = emp->led_state;
1011        u32 activity_led_state;
1012        unsigned long flags;
1013
1014        led_message &= EM_MSG_LED_VALUE;
1015        led_message |= ap->port_no | (link->pmp << 8);
1016
1017        /* check to see if we've had activity.  If so,
1018         * toggle state of LED and reset timer.  If not,
1019         * turn LED to desired idle state.
1020         */
1021        spin_lock_irqsave(ap->lock, flags);
1022        if (emp->saved_activity != emp->activity) {
1023                emp->saved_activity = emp->activity;
1024                /* get the current LED state */
1025                activity_led_state = led_message & EM_MSG_LED_VALUE_ON;
1026
1027                if (activity_led_state)
1028                        activity_led_state = 0;
1029                else
1030                        activity_led_state = 1;
1031
1032                /* clear old state */
1033                led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
1034
1035                /* toggle state */
1036                led_message |= (activity_led_state << 16);
1037                mod_timer(&emp->timer, jiffies + msecs_to_jiffies(100));
1038        } else {
1039                /* switch to idle */
1040                led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
1041                if (emp->blink_policy == BLINK_OFF)
1042                        led_message |= (1 << 16);
1043        }
1044        spin_unlock_irqrestore(ap->lock, flags);
1045        ap->ops->transmit_led_message(ap, led_message, 4);
1046}
1047
1048static void ahci_init_sw_activity(struct ata_link *link)
1049{
1050        struct ata_port *ap = link->ap;
1051        struct ahci_port_priv *pp = ap->private_data;
1052        struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1053
1054        /* init activity stats, setup timer */
1055        emp->saved_activity = emp->activity = 0;
1056        emp->link = link;
1057        timer_setup(&emp->timer, ahci_sw_activity_blink, 0);
1058
1059        /* check our blink policy and set flag for link if it's enabled */
1060        if (emp->blink_policy)
1061                link->flags |= ATA_LFLAG_SW_ACTIVITY;
1062}
1063
1064int ahci_reset_em(struct ata_host *host)
1065{
1066        struct ahci_host_priv *hpriv = host->private_data;
1067        void __iomem *mmio = hpriv->mmio;
1068        u32 em_ctl;
1069
1070        em_ctl = readl(mmio + HOST_EM_CTL);
1071        if ((em_ctl & EM_CTL_TM) || (em_ctl & EM_CTL_RST))
1072                return -EINVAL;
1073
1074        writel(em_ctl | EM_CTL_RST, mmio + HOST_EM_CTL);
1075        return 0;
1076}
1077EXPORT_SYMBOL_GPL(ahci_reset_em);
1078
1079static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
1080                                        ssize_t size)
1081{
1082        struct ahci_host_priv *hpriv = ap->host->private_data;
1083        struct ahci_port_priv *pp = ap->private_data;
1084        void __iomem *mmio = hpriv->mmio;
1085        u32 em_ctl;
1086        u32 message[] = {0, 0};
1087        unsigned long flags;
1088        int pmp;
1089        struct ahci_em_priv *emp;
1090
1091        /* get the slot number from the message */
1092        pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
1093        if (pmp < EM_MAX_SLOTS)
1094                emp = &pp->em_priv[pmp];
1095        else
1096                return -EINVAL;
1097
1098        ahci_rpm_get_port(ap);
1099        spin_lock_irqsave(ap->lock, flags);
1100
1101        /*
1102         * if we are still busy transmitting a previous message,
1103         * do not allow
1104         */
1105        em_ctl = readl(mmio + HOST_EM_CTL);
1106        if (em_ctl & EM_CTL_TM) {
1107                spin_unlock_irqrestore(ap->lock, flags);
1108                ahci_rpm_put_port(ap);
1109                return -EBUSY;
1110        }
1111
1112        if (hpriv->em_msg_type & EM_MSG_TYPE_LED) {
1113                /*
1114                 * create message header - this is all zero except for
1115                 * the message size, which is 4 bytes.
1116                 */
1117                message[0] |= (4 << 8);
1118
1119                /* ignore 0:4 of byte zero, fill in port info yourself */
1120                message[1] = ((state & ~EM_MSG_LED_HBA_PORT) | ap->port_no);
1121
1122                /* write message to EM_LOC */
1123                writel(message[0], mmio + hpriv->em_loc);
1124                writel(message[1], mmio + hpriv->em_loc+4);
1125
1126                /*
1127                 * tell hardware to transmit the message
1128                 */
1129                writel(em_ctl | EM_CTL_TM, mmio + HOST_EM_CTL);
1130        }
1131
1132        /* save off new led state for port/slot */
1133        emp->led_state = state;
1134
1135        spin_unlock_irqrestore(ap->lock, flags);
1136        ahci_rpm_put_port(ap);
1137
1138        return size;
1139}
1140
1141static ssize_t ahci_led_show(struct ata_port *ap, char *buf)
1142{
1143        struct ahci_port_priv *pp = ap->private_data;
1144        struct ata_link *link;
1145        struct ahci_em_priv *emp;
1146        int rc = 0;
1147
1148        ata_for_each_link(link, ap, EDGE) {
1149                emp = &pp->em_priv[link->pmp];
1150                rc += sprintf(buf, "%lx\n", emp->led_state);
1151        }
1152        return rc;
1153}
1154
1155static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
1156                                size_t size)
1157{
1158        unsigned int state;
1159        int pmp;
1160        struct ahci_port_priv *pp = ap->private_data;
1161        struct ahci_em_priv *emp;
1162
1163        if (kstrtouint(buf, 0, &state) < 0)
1164                return -EINVAL;
1165
1166        /* get the slot number from the message */
1167        pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
1168        if (pmp < EM_MAX_SLOTS) {
1169                pmp = array_index_nospec(pmp, EM_MAX_SLOTS);
1170                emp = &pp->em_priv[pmp];
1171        } else {
1172                return -EINVAL;
1173        }
1174
1175        /* mask off the activity bits if we are in sw_activity
1176         * mode, user should turn off sw_activity before setting
1177         * activity led through em_message
1178         */
1179        if (emp->blink_policy)
1180                state &= ~EM_MSG_LED_VALUE_ACTIVITY;
1181
1182        return ap->ops->transmit_led_message(ap, state, size);
1183}
1184
1185static ssize_t ahci_activity_store(struct ata_device *dev, enum sw_activity val)
1186{
1187        struct ata_link *link = dev->link;
1188        struct ata_port *ap = link->ap;
1189        struct ahci_port_priv *pp = ap->private_data;
1190        struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1191        u32 port_led_state = emp->led_state;
1192
1193        /* save the desired Activity LED behavior */
1194        if (val == OFF) {
1195                /* clear LFLAG */
1196                link->flags &= ~(ATA_LFLAG_SW_ACTIVITY);
1197
1198                /* set the LED to OFF */
1199                port_led_state &= EM_MSG_LED_VALUE_OFF;
1200                port_led_state |= (ap->port_no | (link->pmp << 8));
1201                ap->ops->transmit_led_message(ap, port_led_state, 4);
1202        } else {
1203                link->flags |= ATA_LFLAG_SW_ACTIVITY;
1204                if (val == BLINK_OFF) {
1205                        /* set LED to ON for idle */
1206                        port_led_state &= EM_MSG_LED_VALUE_OFF;
1207                        port_led_state |= (ap->port_no | (link->pmp << 8));
1208                        port_led_state |= EM_MSG_LED_VALUE_ON; /* check this */
1209                        ap->ops->transmit_led_message(ap, port_led_state, 4);
1210                }
1211        }
1212        emp->blink_policy = val;
1213        return 0;
1214}
1215
1216static ssize_t ahci_activity_show(struct ata_device *dev, char *buf)
1217{
1218        struct ata_link *link = dev->link;
1219        struct ata_port *ap = link->ap;
1220        struct ahci_port_priv *pp = ap->private_data;
1221        struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1222
1223        /* display the saved value of activity behavior for this
1224         * disk.
1225         */
1226        return sprintf(buf, "%d\n", emp->blink_policy);
1227}
1228
1229static void ahci_port_init(struct device *dev, struct ata_port *ap,
1230                           int port_no, void __iomem *mmio,
1231                           void __iomem *port_mmio)
1232{
1233        struct ahci_host_priv *hpriv = ap->host->private_data;
1234        const char *emsg = NULL;
1235        int rc;
1236        u32 tmp;
1237
1238        /* make sure port is not active */
1239        rc = ahci_deinit_port(ap, &emsg);
1240        if (rc)
1241                dev_warn(dev, "%s (%d)\n", emsg, rc);
1242
1243        /* clear SError */
1244        tmp = readl(port_mmio + PORT_SCR_ERR);
1245        VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
1246        writel(tmp, port_mmio + PORT_SCR_ERR);
1247
1248        /* clear port IRQ */
1249        tmp = readl(port_mmio + PORT_IRQ_STAT);
1250        VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
1251        if (tmp)
1252                writel(tmp, port_mmio + PORT_IRQ_STAT);
1253
1254        writel(1 << port_no, mmio + HOST_IRQ_STAT);
1255
1256        /* mark esata ports */
1257        tmp = readl(port_mmio + PORT_CMD);
1258        if ((tmp & PORT_CMD_ESP) && (hpriv->cap & HOST_CAP_SXS))
1259                ap->pflags |= ATA_PFLAG_EXTERNAL;
1260}
1261
1262void ahci_init_controller(struct ata_host *host)
1263{
1264        struct ahci_host_priv *hpriv = host->private_data;
1265        void __iomem *mmio = hpriv->mmio;
1266        int i;
1267        void __iomem *port_mmio;
1268        u32 tmp;
1269
1270        for (i = 0; i < host->n_ports; i++) {
1271                struct ata_port *ap = host->ports[i];
1272
1273                port_mmio = ahci_port_base(ap);
1274                if (ata_port_is_dummy(ap))
1275                        continue;
1276
1277                ahci_port_init(host->dev, ap, i, mmio, port_mmio);
1278        }
1279
1280        tmp = readl(mmio + HOST_CTL);
1281        VPRINTK("HOST_CTL 0x%x\n", tmp);
1282        writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
1283        tmp = readl(mmio + HOST_CTL);
1284        VPRINTK("HOST_CTL 0x%x\n", tmp);
1285}
1286EXPORT_SYMBOL_GPL(ahci_init_controller);
1287
1288static void ahci_dev_config(struct ata_device *dev)
1289{
1290        struct ahci_host_priv *hpriv = dev->link->ap->host->private_data;
1291
1292        if (hpriv->flags & AHCI_HFLAG_SECT255) {
1293                dev->max_sectors = 255;
1294                ata_dev_info(dev,
1295                             "SB600 AHCI: limiting to 255 sectors per cmd\n");
1296        }
1297}
1298
1299unsigned int ahci_dev_classify(struct ata_port *ap)
1300{
1301        void __iomem *port_mmio = ahci_port_base(ap);
1302        struct ata_taskfile tf;
1303        u32 tmp;
1304
1305        tmp = readl(port_mmio + PORT_SIG);
1306        tf.lbah         = (tmp >> 24)   & 0xff;
1307        tf.lbam         = (tmp >> 16)   & 0xff;
1308        tf.lbal         = (tmp >> 8)    & 0xff;
1309        tf.nsect        = (tmp)         & 0xff;
1310
1311        return ata_dev_classify(&tf);
1312}
1313EXPORT_SYMBOL_GPL(ahci_dev_classify);
1314
1315void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
1316                        u32 opts)
1317{
1318        dma_addr_t cmd_tbl_dma;
1319
1320        cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
1321
1322        pp->cmd_slot[tag].opts = cpu_to_le32(opts);
1323        pp->cmd_slot[tag].status = 0;
1324        pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
1325        pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
1326}
1327EXPORT_SYMBOL_GPL(ahci_fill_cmd_slot);
1328
1329int ahci_kick_engine(struct ata_port *ap)
1330{
1331        void __iomem *port_mmio = ahci_port_base(ap);
1332        struct ahci_host_priv *hpriv = ap->host->private_data;
1333        u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1334        u32 tmp;
1335        int busy, rc;
1336
1337        /* stop engine */
1338        rc = hpriv->stop_engine(ap);
1339        if (rc)
1340                goto out_restart;
1341
1342        /* need to do CLO?
1343         * always do CLO if PMP is attached (AHCI-1.3 9.2)
1344         */
1345        busy = status & (ATA_BUSY | ATA_DRQ);
1346        if (!busy && !sata_pmp_attached(ap)) {
1347                rc = 0;
1348                goto out_restart;
1349        }
1350
1351        if (!(hpriv->cap & HOST_CAP_CLO)) {
1352                rc = -EOPNOTSUPP;
1353                goto out_restart;
1354        }
1355
1356        /* perform CLO */
1357        tmp = readl(port_mmio + PORT_CMD);
1358        tmp |= PORT_CMD_CLO;
1359        writel(tmp, port_mmio + PORT_CMD);
1360
1361        rc = 0;
1362        tmp = ata_wait_register(ap, port_mmio + PORT_CMD,
1363                                PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
1364        if (tmp & PORT_CMD_CLO)
1365                rc = -EIO;
1366
1367        /* restart engine */
1368 out_restart:
1369        hpriv->start_engine(ap);
1370        return rc;
1371}
1372EXPORT_SYMBOL_GPL(ahci_kick_engine);
1373
1374static int ahci_exec_polled_cmd(struct ata_port *ap, int pmp,
1375                                struct ata_taskfile *tf, int is_cmd, u16 flags,
1376                                unsigned long timeout_msec)
1377{
1378        const u32 cmd_fis_len = 5; /* five dwords */
1379        struct ahci_port_priv *pp = ap->private_data;
1380        void __iomem *port_mmio = ahci_port_base(ap);
1381        u8 *fis = pp->cmd_tbl;
1382        u32 tmp;
1383
1384        /* prep the command */
1385        ata_tf_to_fis(tf, pmp, is_cmd, fis);
1386        ahci_fill_cmd_slot(pp, 0, cmd_fis_len | flags | (pmp << 12));
1387
1388        /* set port value for softreset of Port Multiplier */
1389        if (pp->fbs_enabled && pp->fbs_last_dev != pmp) {
1390                tmp = readl(port_mmio + PORT_FBS);
1391                tmp &= ~(PORT_FBS_DEV_MASK | PORT_FBS_DEC);
1392                tmp |= pmp << PORT_FBS_DEV_OFFSET;
1393                writel(tmp, port_mmio + PORT_FBS);
1394                pp->fbs_last_dev = pmp;
1395        }
1396
1397        /* issue & wait */
1398        writel(1, port_mmio + PORT_CMD_ISSUE);
1399
1400        if (timeout_msec) {
1401                tmp = ata_wait_register(ap, port_mmio + PORT_CMD_ISSUE,
1402                                        0x1, 0x1, 1, timeout_msec);
1403                if (tmp & 0x1) {
1404                        ahci_kick_engine(ap);
1405                        return -EBUSY;
1406                }
1407        } else
1408                readl(port_mmio + PORT_CMD_ISSUE);      /* flush */
1409
1410        return 0;
1411}
1412
1413int ahci_do_softreset(struct ata_link *link, unsigned int *class,
1414                      int pmp, unsigned long deadline,
1415                      int (*check_ready)(struct ata_link *link))
1416{
1417        struct ata_port *ap = link->ap;
1418        struct ahci_host_priv *hpriv = ap->host->private_data;
1419        struct ahci_port_priv *pp = ap->private_data;
1420        const char *reason = NULL;
1421        unsigned long now, msecs;
1422        struct ata_taskfile tf;
1423        bool fbs_disabled = false;
1424        int rc;
1425
1426        DPRINTK("ENTER\n");
1427
1428        /* prepare for SRST (AHCI-1.1 10.4.1) */
1429        rc = ahci_kick_engine(ap);
1430        if (rc && rc != -EOPNOTSUPP)
1431                ata_link_warn(link, "failed to reset engine (errno=%d)\n", rc);
1432
1433        /*
1434         * According to AHCI-1.2 9.3.9: if FBS is enable, software shall
1435         * clear PxFBS.EN to '0' prior to issuing software reset to devices
1436         * that is attached to port multiplier.
1437         */
1438        if (!ata_is_host_link(link) && pp->fbs_enabled) {
1439                ahci_disable_fbs(ap);
1440                fbs_disabled = true;
1441        }
1442
1443        ata_tf_init(link->device, &tf);
1444
1445        /* issue the first H2D Register FIS */
1446        msecs = 0;
1447        now = jiffies;
1448        if (time_after(deadline, now))
1449                msecs = jiffies_to_msecs(deadline - now);
1450
1451        tf.ctl |= ATA_SRST;
1452        if (ahci_exec_polled_cmd(ap, pmp, &tf, 0,
1453                                 AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY, msecs)) {
1454                rc = -EIO;
1455                reason = "1st FIS failed";
1456                goto fail;
1457        }
1458
1459        /* spec says at least 5us, but be generous and sleep for 1ms */
1460        ata_msleep(ap, 1);
1461
1462        /* issue the second H2D Register FIS */
1463        tf.ctl &= ~ATA_SRST;
1464        ahci_exec_polled_cmd(ap, pmp, &tf, 0, 0, 0);
1465
1466        /* wait for link to become ready */
1467        rc = ata_wait_after_reset(link, deadline, check_ready);
1468        if (rc == -EBUSY && hpriv->flags & AHCI_HFLAG_SRST_TOUT_IS_OFFLINE) {
1469                /*
1470                 * Workaround for cases where link online status can't
1471                 * be trusted.  Treat device readiness timeout as link
1472                 * offline.
1473                 */
1474                ata_link_info(link, "device not ready, treating as offline\n");
1475                *class = ATA_DEV_NONE;
1476        } else if (rc) {
1477                /* link occupied, -ENODEV too is an error */
1478                reason = "device not ready";
1479                goto fail;
1480        } else
1481                *class = ahci_dev_classify(ap);
1482
1483        /* re-enable FBS if disabled before */
1484        if (fbs_disabled)
1485                ahci_enable_fbs(ap);
1486
1487        DPRINTK("EXIT, class=%u\n", *class);
1488        return 0;
1489
1490 fail:
1491        ata_link_err(link, "softreset failed (%s)\n", reason);
1492        return rc;
1493}
1494
1495int ahci_check_ready(struct ata_link *link)
1496{
1497        void __iomem *port_mmio = ahci_port_base(link->ap);
1498        u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1499
1500        return ata_check_ready(status);
1501}
1502EXPORT_SYMBOL_GPL(ahci_check_ready);
1503
1504static int ahci_softreset(struct ata_link *link, unsigned int *class,
1505                          unsigned long deadline)
1506{
1507        int pmp = sata_srst_pmp(link);
1508
1509        DPRINTK("ENTER\n");
1510
1511        return ahci_do_softreset(link, class, pmp, deadline, ahci_check_ready);
1512}
1513EXPORT_SYMBOL_GPL(ahci_do_softreset);
1514
1515static int ahci_bad_pmp_check_ready(struct ata_link *link)
1516{
1517        void __iomem *port_mmio = ahci_port_base(link->ap);
1518        u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1519        u32 irq_status = readl(port_mmio + PORT_IRQ_STAT);
1520
1521        /*
1522         * There is no need to check TFDATA if BAD PMP is found due to HW bug,
1523         * which can save timeout delay.
1524         */
1525        if (irq_status & PORT_IRQ_BAD_PMP)
1526                return -EIO;
1527
1528        return ata_check_ready(status);
1529}
1530
1531static int ahci_pmp_retry_softreset(struct ata_link *link, unsigned int *class,
1532                                    unsigned long deadline)
1533{
1534        struct ata_port *ap = link->ap;
1535        void __iomem *port_mmio = ahci_port_base(ap);
1536        int pmp = sata_srst_pmp(link);
1537        int rc;
1538        u32 irq_sts;
1539
1540        DPRINTK("ENTER\n");
1541
1542        rc = ahci_do_softreset(link, class, pmp, deadline,
1543                               ahci_bad_pmp_check_ready);
1544
1545        /*
1546         * Soft reset fails with IPMS set when PMP is enabled but
1547         * SATA HDD/ODD is connected to SATA port, do soft reset
1548         * again to port 0.
1549         */
1550        if (rc == -EIO) {
1551                irq_sts = readl(port_mmio + PORT_IRQ_STAT);
1552                if (irq_sts & PORT_IRQ_BAD_PMP) {
1553                        ata_link_warn(link,
1554                                        "applying PMP SRST workaround "
1555                                        "and retrying\n");
1556                        rc = ahci_do_softreset(link, class, 0, deadline,
1557                                               ahci_check_ready);
1558                }
1559        }
1560
1561        return rc;
1562}
1563
1564int ahci_do_hardreset(struct ata_link *link, unsigned int *class,
1565                      unsigned long deadline, bool *online)
1566{
1567        const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
1568        struct ata_port *ap = link->ap;
1569        struct ahci_port_priv *pp = ap->private_data;
1570        struct ahci_host_priv *hpriv = ap->host->private_data;
1571        u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
1572        struct ata_taskfile tf;
1573        int rc;
1574
1575        DPRINTK("ENTER\n");
1576
1577        hpriv->stop_engine(ap);
1578
1579        /* clear D2H reception area to properly wait for D2H FIS */
1580        ata_tf_init(link->device, &tf);
1581        tf.command = ATA_BUSY;
1582        ata_tf_to_fis(&tf, 0, 0, d2h_fis);
1583
1584        rc = sata_link_hardreset(link, timing, deadline, online,
1585                                 ahci_check_ready);
1586
1587        hpriv->start_engine(ap);
1588
1589        if (*online)
1590                *class = ahci_dev_classify(ap);
1591
1592        DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
1593        return rc;
1594}
1595EXPORT_SYMBOL_GPL(ahci_do_hardreset);
1596
1597static int ahci_hardreset(struct ata_link *link, unsigned int *class,
1598                          unsigned long deadline)
1599{
1600        bool online;
1601
1602        return ahci_do_hardreset(link, class, deadline, &online);
1603}
1604
1605static void ahci_postreset(struct ata_link *link, unsigned int *class)
1606{
1607        struct ata_port *ap = link->ap;
1608        void __iomem *port_mmio = ahci_port_base(ap);
1609        u32 new_tmp, tmp;
1610
1611        ata_std_postreset(link, class);
1612
1613        /* Make sure port's ATAPI bit is set appropriately */
1614        new_tmp = tmp = readl(port_mmio + PORT_CMD);
1615        if (*class == ATA_DEV_ATAPI)
1616                new_tmp |= PORT_CMD_ATAPI;
1617        else
1618                new_tmp &= ~PORT_CMD_ATAPI;
1619        if (new_tmp != tmp) {
1620                writel(new_tmp, port_mmio + PORT_CMD);
1621                readl(port_mmio + PORT_CMD); /* flush */
1622        }
1623}
1624
1625static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
1626{
1627        struct scatterlist *sg;
1628        struct ahci_sg *ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
1629        unsigned int si;
1630
1631        VPRINTK("ENTER\n");
1632
1633        /*
1634         * Next, the S/G list.
1635         */
1636        for_each_sg(qc->sg, sg, qc->n_elem, si) {
1637                dma_addr_t addr = sg_dma_address(sg);
1638                u32 sg_len = sg_dma_len(sg);
1639
1640                ahci_sg[si].addr = cpu_to_le32(addr & 0xffffffff);
1641                ahci_sg[si].addr_hi = cpu_to_le32((addr >> 16) >> 16);
1642                ahci_sg[si].flags_size = cpu_to_le32(sg_len - 1);
1643        }
1644
1645        return si;
1646}
1647
1648static int ahci_pmp_qc_defer(struct ata_queued_cmd *qc)
1649{
1650        struct ata_port *ap = qc->ap;
1651        struct ahci_port_priv *pp = ap->private_data;
1652
1653        if (!sata_pmp_attached(ap) || pp->fbs_enabled)
1654                return ata_std_qc_defer(qc);
1655        else
1656                return sata_pmp_qc_defer_cmd_switch(qc);
1657}
1658
1659static void ahci_qc_prep(struct ata_queued_cmd *qc)
1660{
1661        struct ata_port *ap = qc->ap;
1662        struct ahci_port_priv *pp = ap->private_data;
1663        int is_atapi = ata_is_atapi(qc->tf.protocol);
1664        void *cmd_tbl;
1665        u32 opts;
1666        const u32 cmd_fis_len = 5; /* five dwords */
1667        unsigned int n_elem;
1668
1669        /*
1670         * Fill in command table information.  First, the header,
1671         * a SATA Register - Host to Device command FIS.
1672         */
1673        cmd_tbl = pp->cmd_tbl + qc->hw_tag * AHCI_CMD_TBL_SZ;
1674
1675        ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, cmd_tbl);
1676        if (is_atapi) {
1677                memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
1678                memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
1679        }
1680
1681        n_elem = 0;
1682        if (qc->flags & ATA_QCFLAG_DMAMAP)
1683                n_elem = ahci_fill_sg(qc, cmd_tbl);
1684
1685        /*
1686         * Fill in command slot information.
1687         */
1688        opts = cmd_fis_len | n_elem << 16 | (qc->dev->link->pmp << 12);
1689        if (qc->tf.flags & ATA_TFLAG_WRITE)
1690                opts |= AHCI_CMD_WRITE;
1691        if (is_atapi)
1692                opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
1693
1694        ahci_fill_cmd_slot(pp, qc->hw_tag, opts);
1695}
1696
1697static void ahci_fbs_dec_intr(struct ata_port *ap)
1698{
1699        struct ahci_port_priv *pp = ap->private_data;
1700        void __iomem *port_mmio = ahci_port_base(ap);
1701        u32 fbs = readl(port_mmio + PORT_FBS);
1702        int retries = 3;
1703
1704        DPRINTK("ENTER\n");
1705        BUG_ON(!pp->fbs_enabled);
1706
1707        /* time to wait for DEC is not specified by AHCI spec,
1708         * add a retry loop for safety.
1709         */
1710        writel(fbs | PORT_FBS_DEC, port_mmio + PORT_FBS);
1711        fbs = readl(port_mmio + PORT_FBS);
1712        while ((fbs & PORT_FBS_DEC) && retries--) {
1713                udelay(1);
1714                fbs = readl(port_mmio + PORT_FBS);
1715        }
1716
1717        if (fbs & PORT_FBS_DEC)
1718                dev_err(ap->host->dev, "failed to clear device error\n");
1719}
1720
1721static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
1722{
1723        struct ahci_host_priv *hpriv = ap->host->private_data;
1724        struct ahci_port_priv *pp = ap->private_data;
1725        struct ata_eh_info *host_ehi = &ap->link.eh_info;
1726        struct ata_link *link = NULL;
1727        struct ata_queued_cmd *active_qc;
1728        struct ata_eh_info *active_ehi;
1729        bool fbs_need_dec = false;
1730        u32 serror;
1731
1732        /* determine active link with error */
1733        if (pp->fbs_enabled) {
1734                void __iomem *port_mmio = ahci_port_base(ap);
1735                u32 fbs = readl(port_mmio + PORT_FBS);
1736                int pmp = fbs >> PORT_FBS_DWE_OFFSET;
1737
1738                if ((fbs & PORT_FBS_SDE) && (pmp < ap->nr_pmp_links)) {
1739                        link = &ap->pmp_link[pmp];
1740                        fbs_need_dec = true;
1741                }
1742
1743        } else
1744                ata_for_each_link(link, ap, EDGE)
1745                        if (ata_link_active(link))
1746                                break;
1747
1748        if (!link)
1749                link = &ap->link;
1750
1751        active_qc = ata_qc_from_tag(ap, link->active_tag);
1752        active_ehi = &link->eh_info;
1753
1754        /* record irq stat */
1755        ata_ehi_clear_desc(host_ehi);
1756        ata_ehi_push_desc(host_ehi, "irq_stat 0x%08x", irq_stat);
1757
1758        /* AHCI needs SError cleared; otherwise, it might lock up */
1759        ahci_scr_read(&ap->link, SCR_ERROR, &serror);
1760        ahci_scr_write(&ap->link, SCR_ERROR, serror);
1761        host_ehi->serror |= serror;
1762
1763        /* some controllers set IRQ_IF_ERR on device errors, ignore it */
1764        if (hpriv->flags & AHCI_HFLAG_IGN_IRQ_IF_ERR)
1765                irq_stat &= ~PORT_IRQ_IF_ERR;
1766
1767        if (irq_stat & PORT_IRQ_TF_ERR) {
1768                /* If qc is active, charge it; otherwise, the active
1769                 * link.  There's no active qc on NCQ errors.  It will
1770                 * be determined by EH by reading log page 10h.
1771                 */
1772                if (active_qc)
1773                        active_qc->err_mask |= AC_ERR_DEV;
1774                else
1775                        active_ehi->err_mask |= AC_ERR_DEV;
1776
1777                if (hpriv->flags & AHCI_HFLAG_IGN_SERR_INTERNAL)
1778                        host_ehi->serror &= ~SERR_INTERNAL;
1779        }
1780
1781        if (irq_stat & PORT_IRQ_UNK_FIS) {
1782                u32 *unk = pp->rx_fis + RX_FIS_UNK;
1783
1784                active_ehi->err_mask |= AC_ERR_HSM;
1785                active_ehi->action |= ATA_EH_RESET;
1786                ata_ehi_push_desc(active_ehi,
1787                                  "unknown FIS %08x %08x %08x %08x" ,
1788                                  unk[0], unk[1], unk[2], unk[3]);
1789        }
1790
1791        if (sata_pmp_attached(ap) && (irq_stat & PORT_IRQ_BAD_PMP)) {
1792                active_ehi->err_mask |= AC_ERR_HSM;
1793                active_ehi->action |= ATA_EH_RESET;
1794                ata_ehi_push_desc(active_ehi, "incorrect PMP");
1795        }
1796
1797        if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
1798                host_ehi->err_mask |= AC_ERR_HOST_BUS;
1799                host_ehi->action |= ATA_EH_RESET;
1800                ata_ehi_push_desc(host_ehi, "host bus error");
1801        }
1802
1803        if (irq_stat & PORT_IRQ_IF_ERR) {
1804                if (fbs_need_dec)
1805                        active_ehi->err_mask |= AC_ERR_DEV;
1806                else {
1807                        host_ehi->err_mask |= AC_ERR_ATA_BUS;
1808                        host_ehi->action |= ATA_EH_RESET;
1809                }
1810
1811                ata_ehi_push_desc(host_ehi, "interface fatal error");
1812        }
1813
1814        if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
1815                ata_ehi_hotplugged(host_ehi);
1816                ata_ehi_push_desc(host_ehi, "%s",
1817                        irq_stat & PORT_IRQ_CONNECT ?
1818                        "connection status changed" : "PHY RDY changed");
1819        }
1820
1821        /* okay, let's hand over to EH */
1822
1823        if (irq_stat & PORT_IRQ_FREEZE)
1824                ata_port_freeze(ap);
1825        else if (fbs_need_dec) {
1826                ata_link_abort(link);
1827                ahci_fbs_dec_intr(ap);
1828        } else
1829                ata_port_abort(ap);
1830}
1831
1832static void ahci_handle_port_interrupt(struct ata_port *ap,
1833                                       void __iomem *port_mmio, u32 status)
1834{
1835        struct ata_eh_info *ehi = &ap->link.eh_info;
1836        struct ahci_port_priv *pp = ap->private_data;
1837        struct ahci_host_priv *hpriv = ap->host->private_data;
1838        int resetting = !!(ap->pflags & ATA_PFLAG_RESETTING);
1839        u32 qc_active = 0;
1840        int rc;
1841
1842        /* ignore BAD_PMP while resetting */
1843        if (unlikely(resetting))
1844                status &= ~PORT_IRQ_BAD_PMP;
1845
1846        if (sata_lpm_ignore_phy_events(&ap->link)) {
1847                status &= ~PORT_IRQ_PHYRDY;
1848                ahci_scr_write(&ap->link, SCR_ERROR, SERR_PHYRDY_CHG);
1849        }
1850
1851        if (unlikely(status & PORT_IRQ_ERROR)) {
1852                ahci_error_intr(ap, status);
1853                return;
1854        }
1855
1856        if (status & PORT_IRQ_SDB_FIS) {
1857                /* If SNotification is available, leave notification
1858                 * handling to sata_async_notification().  If not,
1859                 * emulate it by snooping SDB FIS RX area.
1860                 *
1861                 * Snooping FIS RX area is probably cheaper than
1862                 * poking SNotification but some constrollers which
1863                 * implement SNotification, ICH9 for example, don't
1864                 * store AN SDB FIS into receive area.
1865                 */
1866                if (hpriv->cap & HOST_CAP_SNTF)
1867                        sata_async_notification(ap);
1868                else {
1869                        /* If the 'N' bit in word 0 of the FIS is set,
1870                         * we just received asynchronous notification.
1871                         * Tell libata about it.
1872                         *
1873                         * Lack of SNotification should not appear in
1874                         * ahci 1.2, so the workaround is unnecessary
1875                         * when FBS is enabled.
1876                         */
1877                        if (pp->fbs_enabled)
1878                                WARN_ON_ONCE(1);
1879                        else {
1880                                const __le32 *f = pp->rx_fis + RX_FIS_SDB;
1881                                u32 f0 = le32_to_cpu(f[0]);
1882                                if (f0 & (1 << 15))
1883                                        sata_async_notification(ap);
1884                        }
1885                }
1886        }
1887
1888        /* pp->active_link is not reliable once FBS is enabled, both
1889         * PORT_SCR_ACT and PORT_CMD_ISSUE should be checked because
1890         * NCQ and non-NCQ commands may be in flight at the same time.
1891         */
1892        if (pp->fbs_enabled) {
1893                if (ap->qc_active) {
1894                        qc_active = readl(port_mmio + PORT_SCR_ACT);
1895                        qc_active |= readl(port_mmio + PORT_CMD_ISSUE);
1896                }
1897        } else {
1898                /* pp->active_link is valid iff any command is in flight */
1899                if (ap->qc_active && pp->active_link->sactive)
1900                        qc_active = readl(port_mmio + PORT_SCR_ACT);
1901                else
1902                        qc_active = readl(port_mmio + PORT_CMD_ISSUE);
1903        }
1904
1905
1906        rc = ata_qc_complete_multiple(ap, qc_active);
1907
1908        /* while resetting, invalid completions are expected */
1909        if (unlikely(rc < 0 && !resetting)) {
1910                ehi->err_mask |= AC_ERR_HSM;
1911                ehi->action |= ATA_EH_RESET;
1912                ata_port_freeze(ap);
1913        }
1914}
1915
1916static void ahci_port_intr(struct ata_port *ap)
1917{
1918        void __iomem *port_mmio = ahci_port_base(ap);
1919        u32 status;
1920
1921        status = readl(port_mmio + PORT_IRQ_STAT);
1922        writel(status, port_mmio + PORT_IRQ_STAT);
1923
1924        ahci_handle_port_interrupt(ap, port_mmio, status);
1925}
1926
1927static irqreturn_t ahci_multi_irqs_intr_hard(int irq, void *dev_instance)
1928{
1929        struct ata_port *ap = dev_instance;
1930        void __iomem *port_mmio = ahci_port_base(ap);
1931        u32 status;
1932
1933        VPRINTK("ENTER\n");
1934
1935        status = readl(port_mmio + PORT_IRQ_STAT);
1936        writel(status, port_mmio + PORT_IRQ_STAT);
1937
1938        spin_lock(ap->lock);
1939        ahci_handle_port_interrupt(ap, port_mmio, status);
1940        spin_unlock(ap->lock);
1941
1942        VPRINTK("EXIT\n");
1943
1944        return IRQ_HANDLED;
1945}
1946
1947u32 ahci_handle_port_intr(struct ata_host *host, u32 irq_masked)
1948{
1949        unsigned int i, handled = 0;
1950
1951        for (i = 0; i < host->n_ports; i++) {
1952                struct ata_port *ap;
1953
1954                if (!(irq_masked & (1 << i)))
1955                        continue;
1956
1957                ap = host->ports[i];
1958                if (ap) {
1959                        ahci_port_intr(ap);
1960                        VPRINTK("port %u\n", i);
1961                } else {
1962                        VPRINTK("port %u (no irq)\n", i);
1963                        if (ata_ratelimit())
1964                                dev_warn(host->dev,
1965                                         "interrupt on disabled port %u\n", i);
1966                }
1967
1968                handled = 1;
1969        }
1970
1971        return handled;
1972}
1973EXPORT_SYMBOL_GPL(ahci_handle_port_intr);
1974
1975static irqreturn_t ahci_single_level_irq_intr(int irq, void *dev_instance)
1976{
1977        struct ata_host *host = dev_instance;
1978        struct ahci_host_priv *hpriv;
1979        unsigned int rc = 0;
1980        void __iomem *mmio;
1981        u32 irq_stat, irq_masked;
1982
1983        VPRINTK("ENTER\n");
1984
1985        hpriv = host->private_data;
1986        mmio = hpriv->mmio;
1987
1988        /* sigh.  0xffffffff is a valid return from h/w */
1989        irq_stat = readl(mmio + HOST_IRQ_STAT);
1990        if (!irq_stat)
1991                return IRQ_NONE;
1992
1993        irq_masked = irq_stat & hpriv->port_map;
1994
1995        spin_lock(&host->lock);
1996
1997        rc = ahci_handle_port_intr(host, irq_masked);
1998
1999        /* HOST_IRQ_STAT behaves as level triggered latch meaning that
2000         * it should be cleared after all the port events are cleared;
2001         * otherwise, it will raise a spurious interrupt after each
2002         * valid one.  Please read section 10.6.2 of ahci 1.1 for more
2003         * information.
2004         *
2005         * Also, use the unmasked value to clear interrupt as spurious
2006         * pending event on a dummy port might cause screaming IRQ.
2007         */
2008        writel(irq_stat, mmio + HOST_IRQ_STAT);
2009
2010        spin_unlock(&host->lock);
2011
2012        VPRINTK("EXIT\n");
2013
2014        return IRQ_RETVAL(rc);
2015}
2016
2017unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
2018{
2019        struct ata_port *ap = qc->ap;
2020        void __iomem *port_mmio = ahci_port_base(ap);
2021        struct ahci_port_priv *pp = ap->private_data;
2022
2023        /* Keep track of the currently active link.  It will be used
2024         * in completion path to determine whether NCQ phase is in
2025         * progress.
2026         */
2027        pp->active_link = qc->dev->link;
2028
2029        if (ata_is_ncq(qc->tf.protocol))
2030                writel(1 << qc->hw_tag, port_mmio + PORT_SCR_ACT);
2031
2032        if (pp->fbs_enabled && pp->fbs_last_dev != qc->dev->link->pmp) {
2033                u32 fbs = readl(port_mmio + PORT_FBS);
2034                fbs &= ~(PORT_FBS_DEV_MASK | PORT_FBS_DEC);
2035                fbs |= qc->dev->link->pmp << PORT_FBS_DEV_OFFSET;
2036                writel(fbs, port_mmio + PORT_FBS);
2037                pp->fbs_last_dev = qc->dev->link->pmp;
2038        }
2039
2040        writel(1 << qc->hw_tag, port_mmio + PORT_CMD_ISSUE);
2041
2042        ahci_sw_activity(qc->dev->link);
2043
2044        return 0;
2045}
2046EXPORT_SYMBOL_GPL(ahci_qc_issue);
2047
2048static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc)
2049{
2050        struct ahci_port_priv *pp = qc->ap->private_data;
2051        u8 *rx_fis = pp->rx_fis;
2052
2053        if (pp->fbs_enabled)
2054                rx_fis += qc->dev->link->pmp * AHCI_RX_FIS_SZ;
2055
2056        /*
2057         * After a successful execution of an ATA PIO data-in command,
2058         * the device doesn't send D2H Reg FIS to update the TF and
2059         * the host should take TF and E_Status from the preceding PIO
2060         * Setup FIS.
2061         */
2062        if (qc->tf.protocol == ATA_PROT_PIO && qc->dma_dir == DMA_FROM_DEVICE &&
2063            !(qc->flags & ATA_QCFLAG_FAILED)) {
2064                ata_tf_from_fis(rx_fis + RX_FIS_PIO_SETUP, &qc->result_tf);
2065                qc->result_tf.command = (rx_fis + RX_FIS_PIO_SETUP)[15];
2066        } else
2067                ata_tf_from_fis(rx_fis + RX_FIS_D2H_REG, &qc->result_tf);
2068
2069        return true;
2070}
2071
2072static void ahci_freeze(struct ata_port *ap)
2073{
2074        void __iomem *port_mmio = ahci_port_base(ap);
2075
2076        /* turn IRQ off */
2077        writel(0, port_mmio + PORT_IRQ_MASK);
2078}
2079
2080static void ahci_thaw(struct ata_port *ap)
2081{
2082        struct ahci_host_priv *hpriv = ap->host->private_data;
2083        void __iomem *mmio = hpriv->mmio;
2084        void __iomem *port_mmio = ahci_port_base(ap);
2085        u32 tmp;
2086        struct ahci_port_priv *pp = ap->private_data;
2087
2088        /* clear IRQ */
2089        tmp = readl(port_mmio + PORT_IRQ_STAT);
2090        writel(tmp, port_mmio + PORT_IRQ_STAT);
2091        writel(1 << ap->port_no, mmio + HOST_IRQ_STAT);
2092
2093        /* turn IRQ back on */
2094        writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
2095}
2096
2097void ahci_error_handler(struct ata_port *ap)
2098{
2099        struct ahci_host_priv *hpriv = ap->host->private_data;
2100
2101        if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
2102                /* restart engine */
2103                hpriv->stop_engine(ap);
2104                hpriv->start_engine(ap);
2105        }
2106
2107        sata_pmp_error_handler(ap);
2108
2109        if (!ata_dev_enabled(ap->link.device))
2110                hpriv->stop_engine(ap);
2111}
2112EXPORT_SYMBOL_GPL(ahci_error_handler);
2113
2114static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
2115{
2116        struct ata_port *ap = qc->ap;
2117
2118        /* make DMA engine forget about the failed command */
2119        if (qc->flags & ATA_QCFLAG_FAILED)
2120                ahci_kick_engine(ap);
2121}
2122
2123static void ahci_set_aggressive_devslp(struct ata_port *ap, bool sleep)
2124{
2125        struct ahci_host_priv *hpriv = ap->host->private_data;
2126        void __iomem *port_mmio = ahci_port_base(ap);
2127        struct ata_device *dev = ap->link.device;
2128        u32 devslp, dm, dito, mdat, deto;
2129        int rc;
2130        unsigned int err_mask;
2131
2132        devslp = readl(port_mmio + PORT_DEVSLP);
2133        if (!(devslp & PORT_DEVSLP_DSP)) {
2134                dev_info(ap->host->dev, "port does not support device sleep\n");
2135                return;
2136        }
2137
2138        /* disable device sleep */
2139        if (!sleep) {
2140                if (devslp & PORT_DEVSLP_ADSE) {
2141                        writel(devslp & ~PORT_DEVSLP_ADSE,
2142                               port_mmio + PORT_DEVSLP);
2143                        err_mask = ata_dev_set_feature(dev,
2144                                                       SETFEATURES_SATA_DISABLE,
2145                                                       SATA_DEVSLP);
2146                        if (err_mask && err_mask != AC_ERR_DEV)
2147                                ata_dev_warn(dev, "failed to disable DEVSLP\n");
2148                }
2149                return;
2150        }
2151
2152        /* device sleep was already enabled */
2153        if (devslp & PORT_DEVSLP_ADSE)
2154                return;
2155
2156        /* set DITO, MDAT, DETO and enable DevSlp, need to stop engine first */
2157        rc = hpriv->stop_engine(ap);
2158        if (rc)
2159                return;
2160
2161        dm = (devslp & PORT_DEVSLP_DM_MASK) >> PORT_DEVSLP_DM_OFFSET;
2162        dito = devslp_idle_timeout / (dm + 1);
2163        if (dito > 0x3ff)
2164                dito = 0x3ff;
2165
2166        /* Use the nominal value 10 ms if the read MDAT is zero,
2167         * the nominal value of DETO is 20 ms.
2168         */
2169        if (dev->devslp_timing[ATA_LOG_DEVSLP_VALID] &
2170            ATA_LOG_DEVSLP_VALID_MASK) {
2171                mdat = dev->devslp_timing[ATA_LOG_DEVSLP_MDAT] &
2172                       ATA_LOG_DEVSLP_MDAT_MASK;
2173                if (!mdat)
2174                        mdat = 10;
2175                deto = dev->devslp_timing[ATA_LOG_DEVSLP_DETO];
2176                if (!deto)
2177                        deto = 20;
2178        } else {
2179                mdat = 10;
2180                deto = 20;
2181        }
2182
2183        devslp |= ((dito << PORT_DEVSLP_DITO_OFFSET) |
2184                   (mdat << PORT_DEVSLP_MDAT_OFFSET) |
2185                   (deto << PORT_DEVSLP_DETO_OFFSET) |
2186                   PORT_DEVSLP_ADSE);
2187        writel(devslp, port_mmio + PORT_DEVSLP);
2188
2189        hpriv->start_engine(ap);
2190
2191        /* enable device sleep feature for the drive */
2192        err_mask = ata_dev_set_feature(dev,
2193                                       SETFEATURES_SATA_ENABLE,
2194                                       SATA_DEVSLP);
2195        if (err_mask && err_mask != AC_ERR_DEV)
2196                ata_dev_warn(dev, "failed to enable DEVSLP\n");
2197}
2198
2199static void ahci_enable_fbs(struct ata_port *ap)
2200{
2201        struct ahci_host_priv *hpriv = ap->host->private_data;
2202        struct ahci_port_priv *pp = ap->private_data;
2203        void __iomem *port_mmio = ahci_port_base(ap);
2204        u32 fbs;
2205        int rc;
2206
2207        if (!pp->fbs_supported)
2208                return;
2209
2210        fbs = readl(port_mmio + PORT_FBS);
2211        if (fbs & PORT_FBS_EN) {
2212                pp->fbs_enabled = true;
2213                pp->fbs_last_dev = -1; /* initialization */
2214                return;
2215        }
2216
2217        rc = hpriv->stop_engine(ap);
2218        if (rc)
2219                return;
2220
2221        writel(fbs | PORT_FBS_EN, port_mmio + PORT_FBS);
2222        fbs = readl(port_mmio + PORT_FBS);
2223        if (fbs & PORT_FBS_EN) {
2224                dev_info(ap->host->dev, "FBS is enabled\n");
2225                pp->fbs_enabled = true;
2226                pp->fbs_last_dev = -1; /* initialization */
2227        } else
2228                dev_err(ap->host->dev, "Failed to enable FBS\n");
2229
2230        hpriv->start_engine(ap);
2231}
2232
2233static void ahci_disable_fbs(struct ata_port *ap)
2234{
2235        struct ahci_host_priv *hpriv = ap->host->private_data;
2236        struct ahci_port_priv *pp = ap->private_data;
2237        void __iomem *port_mmio = ahci_port_base(ap);
2238        u32 fbs;
2239        int rc;
2240
2241        if (!pp->fbs_supported)
2242                return;
2243
2244        fbs = readl(port_mmio + PORT_FBS);
2245        if ((fbs & PORT_FBS_EN) == 0) {
2246                pp->fbs_enabled = false;
2247                return;
2248        }
2249
2250        rc = hpriv->stop_engine(ap);
2251        if (rc)
2252                return;
2253
2254        writel(fbs & ~PORT_FBS_EN, port_mmio + PORT_FBS);
2255        fbs = readl(port_mmio + PORT_FBS);
2256        if (fbs & PORT_FBS_EN)
2257                dev_err(ap->host->dev, "Failed to disable FBS\n");
2258        else {
2259                dev_info(ap->host->dev, "FBS is disabled\n");
2260                pp->fbs_enabled = false;
2261        }
2262
2263        hpriv->start_engine(ap);
2264}
2265
2266static void ahci_pmp_attach(struct ata_port *ap)
2267{
2268        void __iomem *port_mmio = ahci_port_base(ap);
2269        struct ahci_port_priv *pp = ap->private_data;
2270        u32 cmd;
2271
2272        cmd = readl(port_mmio + PORT_CMD);
2273        cmd |= PORT_CMD_PMP;
2274        writel(cmd, port_mmio + PORT_CMD);
2275
2276        ahci_enable_fbs(ap);
2277
2278        pp->intr_mask |= PORT_IRQ_BAD_PMP;
2279
2280        /*
2281         * We must not change the port interrupt mask register if the
2282         * port is marked frozen, the value in pp->intr_mask will be
2283         * restored later when the port is thawed.
2284         *
2285         * Note that during initialization, the port is marked as
2286         * frozen since the irq handler is not yet registered.
2287         */
2288        if (!(ap->pflags & ATA_PFLAG_FROZEN))
2289                writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
2290}
2291
2292static void ahci_pmp_detach(struct ata_port *ap)
2293{
2294        void __iomem *port_mmio = ahci_port_base(ap);
2295        struct ahci_port_priv *pp = ap->private_data;
2296        u32 cmd;
2297
2298        ahci_disable_fbs(ap);
2299
2300        cmd = readl(port_mmio + PORT_CMD);
2301        cmd &= ~PORT_CMD_PMP;
2302        writel(cmd, port_mmio + PORT_CMD);
2303
2304        pp->intr_mask &= ~PORT_IRQ_BAD_PMP;
2305
2306        /* see comment above in ahci_pmp_attach() */
2307        if (!(ap->pflags & ATA_PFLAG_FROZEN))
2308                writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
2309}
2310
2311int ahci_port_resume(struct ata_port *ap)
2312{
2313        ahci_rpm_get_port(ap);
2314
2315        ahci_power_up(ap);
2316        ahci_start_port(ap);
2317
2318        if (sata_pmp_attached(ap))
2319                ahci_pmp_attach(ap);
2320        else
2321                ahci_pmp_detach(ap);
2322
2323        return 0;
2324}
2325EXPORT_SYMBOL_GPL(ahci_port_resume);
2326
2327#ifdef CONFIG_PM
2328static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
2329{
2330        const char *emsg = NULL;
2331        int rc;
2332
2333        rc = ahci_deinit_port(ap, &emsg);
2334        if (rc == 0)
2335                ahci_power_down(ap);
2336        else {
2337                ata_port_err(ap, "%s (%d)\n", emsg, rc);
2338                ata_port_freeze(ap);
2339        }
2340
2341        ahci_rpm_put_port(ap);
2342        return rc;
2343}
2344#endif
2345
2346static int ahci_port_start(struct ata_port *ap)
2347{
2348        struct ahci_host_priv *hpriv = ap->host->private_data;
2349        struct device *dev = ap->host->dev;
2350        struct ahci_port_priv *pp;
2351        void *mem;
2352        dma_addr_t mem_dma;
2353        size_t dma_sz, rx_fis_sz;
2354
2355        pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
2356        if (!pp)
2357                return -ENOMEM;
2358
2359        if (ap->host->n_ports > 1) {
2360                pp->irq_desc = devm_kzalloc(dev, 8, GFP_KERNEL);
2361                if (!pp->irq_desc) {
2362                        devm_kfree(dev, pp);
2363                        return -ENOMEM;
2364                }
2365                snprintf(pp->irq_desc, 8,
2366                         "%s%d", dev_driver_string(dev), ap->port_no);
2367        }
2368
2369        /* check FBS capability */
2370        if ((hpriv->cap & HOST_CAP_FBS) && sata_pmp_supported(ap)) {
2371                void __iomem *port_mmio = ahci_port_base(ap);
2372                u32 cmd = readl(port_mmio + PORT_CMD);
2373                if (cmd & PORT_CMD_FBSCP)
2374                        pp->fbs_supported = true;
2375                else if (hpriv->flags & AHCI_HFLAG_YES_FBS) {
2376                        dev_info(dev, "port %d can do FBS, forcing FBSCP\n",
2377                                 ap->port_no);
2378                        pp->fbs_supported = true;
2379                } else
2380                        dev_warn(dev, "port %d is not capable of FBS\n",
2381                                 ap->port_no);
2382        }
2383
2384        if (pp->fbs_supported) {
2385                dma_sz = AHCI_PORT_PRIV_FBS_DMA_SZ;
2386                rx_fis_sz = AHCI_RX_FIS_SZ * 16;
2387        } else {
2388                dma_sz = AHCI_PORT_PRIV_DMA_SZ;
2389                rx_fis_sz = AHCI_RX_FIS_SZ;
2390        }
2391
2392        mem = dmam_alloc_coherent(dev, dma_sz, &mem_dma, GFP_KERNEL);
2393        if (!mem)
2394                return -ENOMEM;
2395        memset(mem, 0, dma_sz);
2396
2397        /*
2398         * First item in chunk of DMA memory: 32-slot command table,
2399         * 32 bytes each in size
2400         */
2401        pp->cmd_slot = mem;
2402        pp->cmd_slot_dma = mem_dma;
2403
2404        mem += AHCI_CMD_SLOT_SZ;
2405        mem_dma += AHCI_CMD_SLOT_SZ;
2406
2407        /*
2408         * Second item: Received-FIS area
2409         */
2410        pp->rx_fis = mem;
2411        pp->rx_fis_dma = mem_dma;
2412
2413        mem += rx_fis_sz;
2414        mem_dma += rx_fis_sz;
2415
2416        /*
2417         * Third item: data area for storing a single command
2418         * and its scatter-gather table
2419         */
2420        pp->cmd_tbl = mem;
2421        pp->cmd_tbl_dma = mem_dma;
2422
2423        /*
2424         * Save off initial list of interrupts to be enabled.
2425         * This could be changed later
2426         */
2427        pp->intr_mask = DEF_PORT_IRQ;
2428
2429        /*
2430         * Switch to per-port locking in case each port has its own MSI vector.
2431         */
2432        if (hpriv->flags & AHCI_HFLAG_MULTI_MSI) {
2433                spin_lock_init(&pp->lock);
2434                ap->lock = &pp->lock;
2435        }
2436
2437        ap->private_data = pp;
2438
2439        /* engage engines, captain */
2440        return ahci_port_resume(ap);
2441}
2442
2443static void ahci_port_stop(struct ata_port *ap)
2444{
2445        const char *emsg = NULL;
2446        struct ahci_host_priv *hpriv = ap->host->private_data;
2447        void __iomem *host_mmio = hpriv->mmio;
2448        int rc;
2449
2450        /* de-initialize port */
2451        rc = ahci_deinit_port(ap, &emsg);
2452        if (rc)
2453                ata_port_warn(ap, "%s (%d)\n", emsg, rc);
2454
2455        /*
2456         * Clear GHC.IS to prevent stuck INTx after disabling MSI and
2457         * re-enabling INTx.
2458         */
2459        writel(1 << ap->port_no, host_mmio + HOST_IRQ_STAT);
2460}
2461
2462void ahci_print_info(struct ata_host *host, const char *scc_s)
2463{
2464        struct ahci_host_priv *hpriv = host->private_data;
2465        u32 vers, cap, cap2, impl, speed;
2466        const char *speed_s;
2467
2468        vers = hpriv->version;
2469        cap = hpriv->cap;
2470        cap2 = hpriv->cap2;
2471        impl = hpriv->port_map;
2472
2473        speed = (cap >> 20) & 0xf;
2474        if (speed == 1)
2475                speed_s = "1.5";
2476        else if (speed == 2)
2477                speed_s = "3";
2478        else if (speed == 3)
2479                speed_s = "6";
2480        else
2481                speed_s = "?";
2482
2483        dev_info(host->dev,
2484                "AHCI %02x%02x.%02x%02x "
2485                "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
2486                ,
2487
2488                (vers >> 24) & 0xff,
2489                (vers >> 16) & 0xff,
2490                (vers >> 8) & 0xff,
2491                vers & 0xff,
2492
2493                ((cap >> 8) & 0x1f) + 1,
2494                (cap & 0x1f) + 1,
2495                speed_s,
2496                impl,
2497                scc_s);
2498
2499        dev_info(host->dev,
2500                "flags: "
2501                "%s%s%s%s%s%s%s"
2502                "%s%s%s%s%s%s%s"
2503                "%s%s%s%s%s%s%s"
2504                "%s%s\n"
2505                ,
2506
2507                cap & HOST_CAP_64 ? "64bit " : "",
2508                cap & HOST_CAP_NCQ ? "ncq " : "",
2509                cap & HOST_CAP_SNTF ? "sntf " : "",
2510                cap & HOST_CAP_MPS ? "ilck " : "",
2511                cap & HOST_CAP_SSS ? "stag " : "",
2512                cap & HOST_CAP_ALPM ? "pm " : "",
2513                cap & HOST_CAP_LED ? "led " : "",
2514                cap & HOST_CAP_CLO ? "clo " : "",
2515                cap & HOST_CAP_ONLY ? "only " : "",
2516                cap & HOST_CAP_PMP ? "pmp " : "",
2517                cap & HOST_CAP_FBS ? "fbs " : "",
2518                cap & HOST_CAP_PIO_MULTI ? "pio " : "",
2519                cap & HOST_CAP_SSC ? "slum " : "",
2520                cap & HOST_CAP_PART ? "part " : "",
2521                cap & HOST_CAP_CCC ? "ccc " : "",
2522                cap & HOST_CAP_EMS ? "ems " : "",
2523                cap & HOST_CAP_SXS ? "sxs " : "",
2524                cap2 & HOST_CAP2_DESO ? "deso " : "",
2525                cap2 & HOST_CAP2_SADM ? "sadm " : "",
2526                cap2 & HOST_CAP2_SDS ? "sds " : "",
2527                cap2 & HOST_CAP2_APST ? "apst " : "",
2528                cap2 & HOST_CAP2_NVMHCI ? "nvmp " : "",
2529                cap2 & HOST_CAP2_BOH ? "boh " : ""
2530                );
2531}
2532EXPORT_SYMBOL_GPL(ahci_print_info);
2533
2534void ahci_set_em_messages(struct ahci_host_priv *hpriv,
2535                          struct ata_port_info *pi)
2536{
2537        u8 messages;
2538        void __iomem *mmio = hpriv->mmio;
2539        u32 em_loc = readl(mmio + HOST_EM_LOC);
2540        u32 em_ctl = readl(mmio + HOST_EM_CTL);
2541
2542        if (!ahci_em_messages || !(hpriv->cap & HOST_CAP_EMS))
2543                return;
2544
2545        messages = (em_ctl & EM_CTRL_MSG_TYPE) >> 16;
2546
2547        if (messages) {
2548                /* store em_loc */
2549                hpriv->em_loc = ((em_loc >> 16) * 4);
2550                hpriv->em_buf_sz = ((em_loc & 0xff) * 4);
2551                hpriv->em_msg_type = messages;
2552                pi->flags |= ATA_FLAG_EM;
2553                if (!(em_ctl & EM_CTL_ALHD))
2554                        pi->flags |= ATA_FLAG_SW_ACTIVITY;
2555        }
2556}
2557EXPORT_SYMBOL_GPL(ahci_set_em_messages);
2558
2559static int ahci_host_activate_multi_irqs(struct ata_host *host,
2560                                         struct scsi_host_template *sht)
2561{
2562        struct ahci_host_priv *hpriv = host->private_data;
2563        int i, rc;
2564
2565        rc = ata_host_start(host);
2566        if (rc)
2567                return rc;
2568        /*
2569         * Requests IRQs according to AHCI-1.1 when multiple MSIs were
2570         * allocated. That is one MSI per port, starting from @irq.
2571         */
2572        for (i = 0; i < host->n_ports; i++) {
2573                struct ahci_port_priv *pp = host->ports[i]->private_data;
2574                int irq = hpriv->get_irq_vector(host, i);
2575
2576                /* Do not receive interrupts sent by dummy ports */
2577                if (!pp) {
2578                        disable_irq(irq);
2579                        continue;
2580                }
2581
2582                rc = devm_request_irq(host->dev, irq, ahci_multi_irqs_intr_hard,
2583                                0, pp->irq_desc, host->ports[i]);
2584
2585                if (rc)
2586                        return rc;
2587                ata_port_desc(host->ports[i], "irq %d", irq);
2588        }
2589
2590        return ata_host_register(host, sht);
2591}
2592
2593/**
2594 *      ahci_host_activate - start AHCI host, request IRQs and register it
2595 *      @host: target ATA host
2596 *      @sht: scsi_host_template to use when registering the host
2597 *
2598 *      LOCKING:
2599 *      Inherited from calling layer (may sleep).
2600 *
2601 *      RETURNS:
2602 *      0 on success, -errno otherwise.
2603 */
2604int ahci_host_activate(struct ata_host *host, struct scsi_host_template *sht)
2605{
2606        struct ahci_host_priv *hpriv = host->private_data;
2607        int irq = hpriv->irq;
2608        int rc;
2609
2610        if (hpriv->flags & AHCI_HFLAG_MULTI_MSI) {
2611                if (hpriv->irq_handler)
2612                        dev_warn(host->dev,
2613                                 "both AHCI_HFLAG_MULTI_MSI flag set and custom irq handler implemented\n");
2614                if (!hpriv->get_irq_vector) {
2615                        dev_err(host->dev,
2616                                "AHCI_HFLAG_MULTI_MSI requires ->get_irq_vector!\n");
2617                        return -EIO;
2618                }
2619
2620                rc = ahci_host_activate_multi_irqs(host, sht);
2621        } else {
2622                rc = ata_host_activate(host, irq, hpriv->irq_handler,
2623                                       IRQF_SHARED, sht);
2624        }
2625
2626
2627        return rc;
2628}
2629EXPORT_SYMBOL_GPL(ahci_host_activate);
2630
2631MODULE_AUTHOR("Jeff Garzik");
2632MODULE_DESCRIPTION("Common AHCI SATA low-level routines");
2633MODULE_LICENSE("GPL");
2634