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