linux/drivers/ata/libata-pmp.c
<<
>>
Prefs
   1/*
   2 * libata-pmp.c - libata port multiplier support
   3 *
   4 * Copyright (c) 2007  SUSE Linux Products GmbH
   5 * Copyright (c) 2007  Tejun Heo <teheo@suse.de>
   6 *
   7 * This file is released under the GPLv2.
   8 */
   9
  10#include <linux/kernel.h>
  11#include <linux/libata.h>
  12#include "libata.h"
  13
  14const struct ata_port_operations sata_pmp_port_ops = {
  15        .inherits               = &sata_port_ops,
  16        .pmp_prereset           = ata_std_prereset,
  17        .pmp_hardreset          = sata_std_hardreset,
  18        .pmp_postreset          = ata_std_postreset,
  19        .error_handler          = sata_pmp_error_handler,
  20};
  21
  22/**
  23 *      sata_pmp_read - read PMP register
  24 *      @link: link to read PMP register for
  25 *      @reg: register to read
  26 *      @r_val: resulting value
  27 *
  28 *      Read PMP register.
  29 *
  30 *      LOCKING:
  31 *      Kernel thread context (may sleep).
  32 *
  33 *      RETURNS:
  34 *      0 on success, AC_ERR_* mask on failure.
  35 */
  36static unsigned int sata_pmp_read(struct ata_link *link, int reg, u32 *r_val)
  37{
  38        struct ata_port *ap = link->ap;
  39        struct ata_device *pmp_dev = ap->link.device;
  40        struct ata_taskfile tf;
  41        unsigned int err_mask;
  42
  43        ata_tf_init(pmp_dev, &tf);
  44        tf.command = ATA_CMD_PMP_READ;
  45        tf.protocol = ATA_PROT_NODATA;
  46        tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
  47        tf.feature = reg;
  48        tf.device = link->pmp;
  49
  50        err_mask = ata_exec_internal(pmp_dev, &tf, NULL, DMA_NONE, NULL, 0,
  51                                     SATA_PMP_RW_TIMEOUT);
  52        if (err_mask)
  53                return err_mask;
  54
  55        *r_val = tf.nsect | tf.lbal << 8 | tf.lbam << 16 | tf.lbah << 24;
  56        return 0;
  57}
  58
  59/**
  60 *      sata_pmp_write - write PMP register
  61 *      @link: link to write PMP register for
  62 *      @reg: register to write
  63 *      @r_val: value to write
  64 *
  65 *      Write PMP register.
  66 *
  67 *      LOCKING:
  68 *      Kernel thread context (may sleep).
  69 *
  70 *      RETURNS:
  71 *      0 on success, AC_ERR_* mask on failure.
  72 */
  73static unsigned int sata_pmp_write(struct ata_link *link, int reg, u32 val)
  74{
  75        struct ata_port *ap = link->ap;
  76        struct ata_device *pmp_dev = ap->link.device;
  77        struct ata_taskfile tf;
  78
  79        ata_tf_init(pmp_dev, &tf);
  80        tf.command = ATA_CMD_PMP_WRITE;
  81        tf.protocol = ATA_PROT_NODATA;
  82        tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
  83        tf.feature = reg;
  84        tf.device = link->pmp;
  85        tf.nsect = val & 0xff;
  86        tf.lbal = (val >> 8) & 0xff;
  87        tf.lbam = (val >> 16) & 0xff;
  88        tf.lbah = (val >> 24) & 0xff;
  89
  90        return ata_exec_internal(pmp_dev, &tf, NULL, DMA_NONE, NULL, 0,
  91                                 SATA_PMP_RW_TIMEOUT);
  92}
  93
  94/**
  95 *      sata_pmp_qc_defer_cmd_switch - qc_defer for command switching PMP
  96 *      @qc: ATA command in question
  97 *
  98 *      A host which has command switching PMP support cannot issue
  99 *      commands to multiple links simultaneously.
 100 *
 101 *      LOCKING:
 102 *      spin_lock_irqsave(host lock)
 103 *
 104 *      RETURNS:
 105 *      ATA_DEFER_* if deferring is needed, 0 otherwise.
 106 */
 107int sata_pmp_qc_defer_cmd_switch(struct ata_queued_cmd *qc)
 108{
 109        struct ata_link *link = qc->dev->link;
 110        struct ata_port *ap = link->ap;
 111
 112        if (ap->excl_link == NULL || ap->excl_link == link) {
 113                if (ap->nr_active_links == 0 || ata_link_active(link)) {
 114                        qc->flags |= ATA_QCFLAG_CLEAR_EXCL;
 115                        return ata_std_qc_defer(qc);
 116                }
 117
 118                ap->excl_link = link;
 119        }
 120
 121        return ATA_DEFER_PORT;
 122}
 123
 124/**
 125 *      sata_pmp_scr_read - read PSCR
 126 *      @link: ATA link to read PSCR for
 127 *      @reg: PSCR to read
 128 *      @r_val: resulting value
 129 *
 130 *      Read PSCR @reg into @r_val for @link, to be called from
 131 *      ata_scr_read().
 132 *
 133 *      LOCKING:
 134 *      Kernel thread context (may sleep).
 135 *
 136 *      RETURNS:
 137 *      0 on success, -errno on failure.
 138 */
 139int sata_pmp_scr_read(struct ata_link *link, int reg, u32 *r_val)
 140{
 141        unsigned int err_mask;
 142
 143        if (reg > SATA_PMP_PSCR_CONTROL)
 144                return -EINVAL;
 145
 146        err_mask = sata_pmp_read(link, reg, r_val);
 147        if (err_mask) {
 148                ata_link_printk(link, KERN_WARNING, "failed to read SCR %d "
 149                                "(Emask=0x%x)\n", reg, err_mask);
 150                return -EIO;
 151        }
 152        return 0;
 153}
 154
 155/**
 156 *      sata_pmp_scr_write - write PSCR
 157 *      @link: ATA link to write PSCR for
 158 *      @reg: PSCR to write
 159 *      @val: value to be written
 160 *
 161 *      Write @val to PSCR @reg for @link, to be called from
 162 *      ata_scr_write() and ata_scr_write_flush().
 163 *
 164 *      LOCKING:
 165 *      Kernel thread context (may sleep).
 166 *
 167 *      RETURNS:
 168 *      0 on success, -errno on failure.
 169 */
 170int sata_pmp_scr_write(struct ata_link *link, int reg, u32 val)
 171{
 172        unsigned int err_mask;
 173
 174        if (reg > SATA_PMP_PSCR_CONTROL)
 175                return -EINVAL;
 176
 177        err_mask = sata_pmp_write(link, reg, val);
 178        if (err_mask) {
 179                ata_link_printk(link, KERN_WARNING, "failed to write SCR %d "
 180                                "(Emask=0x%x)\n", reg, err_mask);
 181                return -EIO;
 182        }
 183        return 0;
 184}
 185
 186/**
 187 *      sata_pmp_read_gscr - read GSCR block of SATA PMP
 188 *      @dev: PMP device
 189 *      @gscr: buffer to read GSCR block into
 190 *
 191 *      Read selected PMP GSCRs from the PMP at @dev.  This will serve
 192 *      as configuration and identification info for the PMP.
 193 *
 194 *      LOCKING:
 195 *      Kernel thread context (may sleep).
 196 *
 197 *      RETURNS:
 198 *      0 on success, -errno on failure.
 199 */
 200static int sata_pmp_read_gscr(struct ata_device *dev, u32 *gscr)
 201{
 202        static const int gscr_to_read[] = { 0, 1, 2, 32, 33, 64, 96 };
 203        int i;
 204
 205        for (i = 0; i < ARRAY_SIZE(gscr_to_read); i++) {
 206                int reg = gscr_to_read[i];
 207                unsigned int err_mask;
 208
 209                err_mask = sata_pmp_read(dev->link, reg, &gscr[reg]);
 210                if (err_mask) {
 211                        ata_dev_printk(dev, KERN_ERR, "failed to read PMP "
 212                                "GSCR[%d] (Emask=0x%x)\n", reg, err_mask);
 213                        return -EIO;
 214                }
 215        }
 216
 217        return 0;
 218}
 219
 220static const char *sata_pmp_spec_rev_str(const u32 *gscr)
 221{
 222        u32 rev = gscr[SATA_PMP_GSCR_REV];
 223
 224        if (rev & (1 << 3))
 225                return "1.2";
 226        if (rev & (1 << 2))
 227                return "1.1";
 228        if (rev & (1 << 1))
 229                return "1.0";
 230        return "<unknown>";
 231}
 232
 233static int sata_pmp_configure(struct ata_device *dev, int print_info)
 234{
 235        struct ata_port *ap = dev->link->ap;
 236        u32 *gscr = dev->gscr;
 237        unsigned int err_mask = 0;
 238        const char *reason;
 239        int nr_ports, rc;
 240
 241        nr_ports = sata_pmp_gscr_ports(gscr);
 242
 243        if (nr_ports <= 0 || nr_ports > SATA_PMP_MAX_PORTS) {
 244                rc = -EINVAL;
 245                reason = "invalid nr_ports";
 246                goto fail;
 247        }
 248
 249        if ((ap->flags & ATA_FLAG_AN) &&
 250            (gscr[SATA_PMP_GSCR_FEAT] & SATA_PMP_FEAT_NOTIFY))
 251                dev->flags |= ATA_DFLAG_AN;
 252
 253        /* monitor SERR_PHYRDY_CHG on fan-out ports */
 254        err_mask = sata_pmp_write(dev->link, SATA_PMP_GSCR_ERROR_EN,
 255                                  SERR_PHYRDY_CHG);
 256        if (err_mask) {
 257                rc = -EIO;
 258                reason = "failed to write GSCR_ERROR_EN";
 259                goto fail;
 260        }
 261
 262        if (print_info) {
 263                ata_dev_printk(dev, KERN_INFO, "Port Multiplier %s, "
 264                               "0x%04x:0x%04x r%d, %d ports, feat 0x%x/0x%x\n",
 265                               sata_pmp_spec_rev_str(gscr),
 266                               sata_pmp_gscr_vendor(gscr),
 267                               sata_pmp_gscr_devid(gscr),
 268                               sata_pmp_gscr_rev(gscr),
 269                               nr_ports, gscr[SATA_PMP_GSCR_FEAT_EN],
 270                               gscr[SATA_PMP_GSCR_FEAT]);
 271
 272                if (!(dev->flags & ATA_DFLAG_AN))
 273                        ata_dev_printk(dev, KERN_INFO,
 274                                "Asynchronous notification not supported, "
 275                                "hotplug won't\n         work on fan-out "
 276                                "ports. Use warm-plug instead.\n");
 277        }
 278
 279        return 0;
 280
 281 fail:
 282        ata_dev_printk(dev, KERN_ERR,
 283                       "failed to configure Port Multiplier (%s, Emask=0x%x)\n",
 284                       reason, err_mask);
 285        return rc;
 286}
 287
 288static int sata_pmp_init_links(struct ata_port *ap, int nr_ports)
 289{
 290        struct ata_link *pmp_link = ap->pmp_link;
 291        int i;
 292
 293        if (!pmp_link) {
 294                pmp_link = kzalloc(sizeof(pmp_link[0]) * SATA_PMP_MAX_PORTS,
 295                                   GFP_NOIO);
 296                if (!pmp_link)
 297                        return -ENOMEM;
 298
 299                for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
 300                        ata_link_init(ap, &pmp_link[i], i);
 301
 302                ap->pmp_link = pmp_link;
 303        }
 304
 305        for (i = 0; i < nr_ports; i++) {
 306                struct ata_link *link = &pmp_link[i];
 307                struct ata_eh_context *ehc = &link->eh_context;
 308
 309                link->flags = 0;
 310                ehc->i.probe_mask |= ATA_ALL_DEVICES;
 311                ehc->i.action |= ATA_EH_RESET;
 312        }
 313
 314        return 0;
 315}
 316
 317static void sata_pmp_quirks(struct ata_port *ap)
 318{
 319        u32 *gscr = ap->link.device->gscr;
 320        u16 vendor = sata_pmp_gscr_vendor(gscr);
 321        u16 devid = sata_pmp_gscr_devid(gscr);
 322        struct ata_link *link;
 323
 324        if (vendor == 0x1095 && devid == 0x3726) {
 325                /* sil3726 quirks */
 326                ata_for_each_link(link, ap, EDGE) {
 327                        /* Class code report is unreliable and SRST
 328                         * times out under certain configurations.
 329                         */
 330                        if (link->pmp < 5)
 331                                link->flags |= ATA_LFLAG_NO_SRST |
 332                                               ATA_LFLAG_ASSUME_ATA;
 333
 334                        /* port 5 is for SEMB device and it doesn't like SRST */
 335                        if (link->pmp == 5)
 336                                link->flags |= ATA_LFLAG_NO_SRST |
 337                                               ATA_LFLAG_ASSUME_SEMB;
 338                }
 339        } else if (vendor == 0x1095 && devid == 0x4723) {
 340                /* sil4723 quirks */
 341                ata_for_each_link(link, ap, EDGE) {
 342                        /* class code report is unreliable */
 343                        if (link->pmp < 2)
 344                                link->flags |= ATA_LFLAG_ASSUME_ATA;
 345
 346                        /* the config device at port 2 locks up on SRST */
 347                        if (link->pmp == 2)
 348                                link->flags |= ATA_LFLAG_NO_SRST |
 349                                               ATA_LFLAG_ASSUME_ATA;
 350                }
 351        } else if (vendor == 0x1095 && devid == 0x4726) {
 352                /* sil4726 quirks */
 353                ata_for_each_link(link, ap, EDGE) {
 354                        /* Class code report is unreliable and SRST
 355                         * times out under certain configurations.
 356                         * Config device can be at port 0 or 5 and
 357                         * locks up on SRST.
 358                         */
 359                        if (link->pmp <= 5)
 360                                link->flags |= ATA_LFLAG_NO_SRST |
 361                                               ATA_LFLAG_ASSUME_ATA;
 362
 363                        /* Port 6 is for SEMB device which doesn't
 364                         * like SRST either.
 365                         */
 366                        if (link->pmp == 6)
 367                                link->flags |= ATA_LFLAG_NO_SRST |
 368                                               ATA_LFLAG_ASSUME_SEMB;
 369                }
 370        } else if (vendor == 0x1095 && (devid == 0x5723 || devid == 0x5733 ||
 371                                        devid == 0x5734 || devid == 0x5744)) {
 372                /* sil5723/5744 quirks */
 373
 374                /* sil5723/5744 has either two or three downstream
 375                 * ports depending on operation mode.  The last port
 376                 * is empty if any actual IO device is available or
 377                 * occupied by a pseudo configuration device
 378                 * otherwise.  Don't try hard to recover it.
 379                 */
 380                ap->pmp_link[ap->nr_pmp_links - 1].flags |= ATA_LFLAG_NO_RETRY;
 381        }
 382}
 383
 384/**
 385 *      sata_pmp_attach - attach a SATA PMP device
 386 *      @dev: SATA PMP device to attach
 387 *
 388 *      Configure and attach SATA PMP device @dev.  This function is
 389 *      also responsible for allocating and initializing PMP links.
 390 *
 391 *      LOCKING:
 392 *      Kernel thread context (may sleep).
 393 *
 394 *      RETURNS:
 395 *      0 on success, -errno on failure.
 396 */
 397int sata_pmp_attach(struct ata_device *dev)
 398{
 399        struct ata_link *link = dev->link;
 400        struct ata_port *ap = link->ap;
 401        unsigned long flags;
 402        struct ata_link *tlink;
 403        int rc;
 404
 405        /* is it hanging off the right place? */
 406        if (!sata_pmp_supported(ap)) {
 407                ata_dev_printk(dev, KERN_ERR,
 408                               "host does not support Port Multiplier\n");
 409                return -EINVAL;
 410        }
 411
 412        if (!ata_is_host_link(link)) {
 413                ata_dev_printk(dev, KERN_ERR,
 414                               "Port Multipliers cannot be nested\n");
 415                return -EINVAL;
 416        }
 417
 418        if (dev->devno) {
 419                ata_dev_printk(dev, KERN_ERR,
 420                               "Port Multiplier must be the first device\n");
 421                return -EINVAL;
 422        }
 423
 424        WARN_ON(link->pmp != 0);
 425        link->pmp = SATA_PMP_CTRL_PORT;
 426
 427        /* read GSCR block */
 428        rc = sata_pmp_read_gscr(dev, dev->gscr);
 429        if (rc)
 430                goto fail;
 431
 432        /* config PMP */
 433        rc = sata_pmp_configure(dev, 1);
 434        if (rc)
 435                goto fail;
 436
 437        rc = sata_pmp_init_links(ap, sata_pmp_gscr_ports(dev->gscr));
 438        if (rc) {
 439                ata_dev_printk(dev, KERN_INFO,
 440                               "failed to initialize PMP links\n");
 441                goto fail;
 442        }
 443
 444        /* attach it */
 445        spin_lock_irqsave(ap->lock, flags);
 446        WARN_ON(ap->nr_pmp_links);
 447        ap->nr_pmp_links = sata_pmp_gscr_ports(dev->gscr);
 448        spin_unlock_irqrestore(ap->lock, flags);
 449
 450        sata_pmp_quirks(ap);
 451
 452        if (ap->ops->pmp_attach)
 453                ap->ops->pmp_attach(ap);
 454
 455        ata_for_each_link(tlink, ap, EDGE)
 456                sata_link_init_spd(tlink);
 457
 458        ata_acpi_associate_sata_port(ap);
 459
 460        return 0;
 461
 462 fail:
 463        link->pmp = 0;
 464        return rc;
 465}
 466
 467/**
 468 *      sata_pmp_detach - detach a SATA PMP device
 469 *      @dev: SATA PMP device to detach
 470 *
 471 *      Detach SATA PMP device @dev.  This function is also
 472 *      responsible for deconfiguring PMP links.
 473 *
 474 *      LOCKING:
 475 *      Kernel thread context (may sleep).
 476 */
 477static void sata_pmp_detach(struct ata_device *dev)
 478{
 479        struct ata_link *link = dev->link;
 480        struct ata_port *ap = link->ap;
 481        struct ata_link *tlink;
 482        unsigned long flags;
 483
 484        ata_dev_printk(dev, KERN_INFO, "Port Multiplier detaching\n");
 485
 486        WARN_ON(!ata_is_host_link(link) || dev->devno ||
 487                link->pmp != SATA_PMP_CTRL_PORT);
 488
 489        if (ap->ops->pmp_detach)
 490                ap->ops->pmp_detach(ap);
 491
 492        ata_for_each_link(tlink, ap, EDGE)
 493                ata_eh_detach_dev(tlink->device);
 494
 495        spin_lock_irqsave(ap->lock, flags);
 496        ap->nr_pmp_links = 0;
 497        link->pmp = 0;
 498        spin_unlock_irqrestore(ap->lock, flags);
 499
 500        ata_acpi_associate_sata_port(ap);
 501}
 502
 503/**
 504 *      sata_pmp_same_pmp - does new GSCR matches the configured PMP?
 505 *      @dev: PMP device to compare against
 506 *      @new_gscr: GSCR block of the new device
 507 *
 508 *      Compare @new_gscr against @dev and determine whether @dev is
 509 *      the PMP described by @new_gscr.
 510 *
 511 *      LOCKING:
 512 *      None.
 513 *
 514 *      RETURNS:
 515 *      1 if @dev matches @new_gscr, 0 otherwise.
 516 */
 517static int sata_pmp_same_pmp(struct ata_device *dev, const u32 *new_gscr)
 518{
 519        const u32 *old_gscr = dev->gscr;
 520        u16 old_vendor, new_vendor, old_devid, new_devid;
 521        int old_nr_ports, new_nr_ports;
 522
 523        old_vendor = sata_pmp_gscr_vendor(old_gscr);
 524        new_vendor = sata_pmp_gscr_vendor(new_gscr);
 525        old_devid = sata_pmp_gscr_devid(old_gscr);
 526        new_devid = sata_pmp_gscr_devid(new_gscr);
 527        old_nr_ports = sata_pmp_gscr_ports(old_gscr);
 528        new_nr_ports = sata_pmp_gscr_ports(new_gscr);
 529
 530        if (old_vendor != new_vendor) {
 531                ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
 532                               "vendor mismatch '0x%x' != '0x%x'\n",
 533                               old_vendor, new_vendor);
 534                return 0;
 535        }
 536
 537        if (old_devid != new_devid) {
 538                ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
 539                               "device ID mismatch '0x%x' != '0x%x'\n",
 540                               old_devid, new_devid);
 541                return 0;
 542        }
 543
 544        if (old_nr_ports != new_nr_ports) {
 545                ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
 546                               "nr_ports mismatch '0x%x' != '0x%x'\n",
 547                               old_nr_ports, new_nr_ports);
 548                return 0;
 549        }
 550
 551        return 1;
 552}
 553
 554/**
 555 *      sata_pmp_revalidate - revalidate SATA PMP
 556 *      @dev: PMP device to revalidate
 557 *      @new_class: new class code
 558 *
 559 *      Re-read GSCR block and make sure @dev is still attached to the
 560 *      port and properly configured.
 561 *
 562 *      LOCKING:
 563 *      Kernel thread context (may sleep).
 564 *
 565 *      RETURNS:
 566 *      0 on success, -errno otherwise.
 567 */
 568static int sata_pmp_revalidate(struct ata_device *dev, unsigned int new_class)
 569{
 570        struct ata_link *link = dev->link;
 571        struct ata_port *ap = link->ap;
 572        u32 *gscr = (void *)ap->sector_buf;
 573        int rc;
 574
 575        DPRINTK("ENTER\n");
 576
 577        ata_eh_about_to_do(link, NULL, ATA_EH_REVALIDATE);
 578
 579        if (!ata_dev_enabled(dev)) {
 580                rc = -ENODEV;
 581                goto fail;
 582        }
 583
 584        /* wrong class? */
 585        if (ata_class_enabled(new_class) && new_class != ATA_DEV_PMP) {
 586                rc = -ENODEV;
 587                goto fail;
 588        }
 589
 590        /* read GSCR */
 591        rc = sata_pmp_read_gscr(dev, gscr);
 592        if (rc)
 593                goto fail;
 594
 595        /* is the pmp still there? */
 596        if (!sata_pmp_same_pmp(dev, gscr)) {
 597                rc = -ENODEV;
 598                goto fail;
 599        }
 600
 601        memcpy(dev->gscr, gscr, sizeof(gscr[0]) * SATA_PMP_GSCR_DWORDS);
 602
 603        rc = sata_pmp_configure(dev, 0);
 604        if (rc)
 605                goto fail;
 606
 607        ata_eh_done(link, NULL, ATA_EH_REVALIDATE);
 608
 609        DPRINTK("EXIT, rc=0\n");
 610        return 0;
 611
 612 fail:
 613        ata_dev_printk(dev, KERN_ERR,
 614                       "PMP revalidation failed (errno=%d)\n", rc);
 615        DPRINTK("EXIT, rc=%d\n", rc);
 616        return rc;
 617}
 618
 619/**
 620 *      sata_pmp_revalidate_quick - revalidate SATA PMP quickly
 621 *      @dev: PMP device to revalidate
 622 *
 623 *      Make sure the attached PMP is accessible.
 624 *
 625 *      LOCKING:
 626 *      Kernel thread context (may sleep).
 627 *
 628 *      RETURNS:
 629 *      0 on success, -errno otherwise.
 630 */
 631static int sata_pmp_revalidate_quick(struct ata_device *dev)
 632{
 633        unsigned int err_mask;
 634        u32 prod_id;
 635
 636        err_mask = sata_pmp_read(dev->link, SATA_PMP_GSCR_PROD_ID, &prod_id);
 637        if (err_mask) {
 638                ata_dev_printk(dev, KERN_ERR, "failed to read PMP product ID "
 639                               "(Emask=0x%x)\n", err_mask);
 640                return -EIO;
 641        }
 642
 643        if (prod_id != dev->gscr[SATA_PMP_GSCR_PROD_ID]) {
 644                ata_dev_printk(dev, KERN_ERR, "PMP product ID mismatch\n");
 645                /* something weird is going on, request full PMP recovery */
 646                return -EIO;
 647        }
 648
 649        return 0;
 650}
 651
 652/**
 653 *      sata_pmp_eh_recover_pmp - recover PMP
 654 *      @ap: ATA port PMP is attached to
 655 *      @prereset: prereset method (can be NULL)
 656 *      @softreset: softreset method
 657 *      @hardreset: hardreset method
 658 *      @postreset: postreset method (can be NULL)
 659 *
 660 *      Recover PMP attached to @ap.  Recovery procedure is somewhat
 661 *      similar to that of ata_eh_recover() except that reset should
 662 *      always be performed in hard->soft sequence and recovery
 663 *      failure results in PMP detachment.
 664 *
 665 *      LOCKING:
 666 *      Kernel thread context (may sleep).
 667 *
 668 *      RETURNS:
 669 *      0 on success, -errno on failure.
 670 */
 671static int sata_pmp_eh_recover_pmp(struct ata_port *ap,
 672                ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
 673                ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
 674{
 675        struct ata_link *link = &ap->link;
 676        struct ata_eh_context *ehc = &link->eh_context;
 677        struct ata_device *dev = link->device;
 678        int tries = ATA_EH_PMP_TRIES;
 679        int detach = 0, rc = 0;
 680        int reval_failed = 0;
 681
 682        DPRINTK("ENTER\n");
 683
 684        if (dev->flags & ATA_DFLAG_DETACH) {
 685                detach = 1;
 686                goto fail;
 687        }
 688
 689 retry:
 690        ehc->classes[0] = ATA_DEV_UNKNOWN;
 691
 692        if (ehc->i.action & ATA_EH_RESET) {
 693                struct ata_link *tlink;
 694
 695                /* reset */
 696                rc = ata_eh_reset(link, 0, prereset, softreset, hardreset,
 697                                  postreset);
 698                if (rc) {
 699                        ata_link_printk(link, KERN_ERR,
 700                                        "failed to reset PMP, giving up\n");
 701                        goto fail;
 702                }
 703
 704                /* PMP is reset, SErrors cannot be trusted, scan all */
 705                ata_for_each_link(tlink, ap, EDGE) {
 706                        struct ata_eh_context *ehc = &tlink->eh_context;
 707
 708                        ehc->i.probe_mask |= ATA_ALL_DEVICES;
 709                        ehc->i.action |= ATA_EH_RESET;
 710                }
 711        }
 712
 713        /* If revalidation is requested, revalidate and reconfigure;
 714         * otherwise, do quick revalidation.
 715         */
 716        if (ehc->i.action & ATA_EH_REVALIDATE)
 717                rc = sata_pmp_revalidate(dev, ehc->classes[0]);
 718        else
 719                rc = sata_pmp_revalidate_quick(dev);
 720
 721        if (rc) {
 722                tries--;
 723
 724                if (rc == -ENODEV) {
 725                        ehc->i.probe_mask |= ATA_ALL_DEVICES;
 726                        detach = 1;
 727                        /* give it just two more chances */
 728                        tries = min(tries, 2);
 729                }
 730
 731                if (tries) {
 732                        /* consecutive revalidation failures? speed down */
 733                        if (reval_failed)
 734                                sata_down_spd_limit(link, 0);
 735                        else
 736                                reval_failed = 1;
 737
 738                        ehc->i.action |= ATA_EH_RESET;
 739                        goto retry;
 740                } else {
 741                        ata_dev_printk(dev, KERN_ERR, "failed to recover PMP "
 742                                       "after %d tries, giving up\n",
 743                                       ATA_EH_PMP_TRIES);
 744                        goto fail;
 745                }
 746        }
 747
 748        /* okay, PMP resurrected */
 749        ehc->i.flags = 0;
 750
 751        DPRINTK("EXIT, rc=0\n");
 752        return 0;
 753
 754 fail:
 755        sata_pmp_detach(dev);
 756        if (detach)
 757                ata_eh_detach_dev(dev);
 758        else
 759                ata_dev_disable(dev);
 760
 761        DPRINTK("EXIT, rc=%d\n", rc);
 762        return rc;
 763}
 764
 765static int sata_pmp_eh_handle_disabled_links(struct ata_port *ap)
 766{
 767        struct ata_link *link;
 768        unsigned long flags;
 769        int rc;
 770
 771        spin_lock_irqsave(ap->lock, flags);
 772
 773        ata_for_each_link(link, ap, EDGE) {
 774                if (!(link->flags & ATA_LFLAG_DISABLED))
 775                        continue;
 776
 777                spin_unlock_irqrestore(ap->lock, flags);
 778
 779                /* Some PMPs require hardreset sequence to get
 780                 * SError.N working.
 781                 */
 782                sata_link_hardreset(link, sata_deb_timing_normal,
 783                                ata_deadline(jiffies, ATA_TMOUT_INTERNAL_QUICK),
 784                                NULL, NULL);
 785
 786                /* unconditionally clear SError.N */
 787                rc = sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
 788                if (rc) {
 789                        ata_link_printk(link, KERN_ERR, "failed to clear "
 790                                        "SError.N (errno=%d)\n", rc);
 791                        return rc;
 792                }
 793
 794                spin_lock_irqsave(ap->lock, flags);
 795        }
 796
 797        spin_unlock_irqrestore(ap->lock, flags);
 798
 799        return 0;
 800}
 801
 802static int sata_pmp_handle_link_fail(struct ata_link *link, int *link_tries)
 803{
 804        struct ata_port *ap = link->ap;
 805        unsigned long flags;
 806
 807        if (link_tries[link->pmp] && --link_tries[link->pmp])
 808                return 1;
 809
 810        /* disable this link */
 811        if (!(link->flags & ATA_LFLAG_DISABLED)) {
 812                ata_link_printk(link, KERN_WARNING,
 813                        "failed to recover link after %d tries, disabling\n",
 814                        ATA_EH_PMP_LINK_TRIES);
 815
 816                spin_lock_irqsave(ap->lock, flags);
 817                link->flags |= ATA_LFLAG_DISABLED;
 818                spin_unlock_irqrestore(ap->lock, flags);
 819        }
 820
 821        ata_dev_disable(link->device);
 822        link->eh_context.i.action = 0;
 823
 824        return 0;
 825}
 826
 827/**
 828 *      sata_pmp_eh_recover - recover PMP-enabled port
 829 *      @ap: ATA port to recover
 830 *
 831 *      Drive EH recovery operation for PMP enabled port @ap.  This
 832 *      function recovers host and PMP ports with proper retrials and
 833 *      fallbacks.  Actual recovery operations are performed using
 834 *      ata_eh_recover() and sata_pmp_eh_recover_pmp().
 835 *
 836 *      LOCKING:
 837 *      Kernel thread context (may sleep).
 838 *
 839 *      RETURNS:
 840 *      0 on success, -errno on failure.
 841 */
 842static int sata_pmp_eh_recover(struct ata_port *ap)
 843{
 844        struct ata_port_operations *ops = ap->ops;
 845        int pmp_tries, link_tries[SATA_PMP_MAX_PORTS];
 846        struct ata_link *pmp_link = &ap->link;
 847        struct ata_device *pmp_dev = pmp_link->device;
 848        struct ata_eh_context *pmp_ehc = &pmp_link->eh_context;
 849        u32 *gscr = pmp_dev->gscr;
 850        struct ata_link *link;
 851        struct ata_device *dev;
 852        unsigned int err_mask;
 853        u32 gscr_error, sntf;
 854        int cnt, rc;
 855
 856        pmp_tries = ATA_EH_PMP_TRIES;
 857        ata_for_each_link(link, ap, EDGE)
 858                link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
 859
 860 retry:
 861        /* PMP attached? */
 862        if (!sata_pmp_attached(ap)) {
 863                rc = ata_eh_recover(ap, ops->prereset, ops->softreset,
 864                                    ops->hardreset, ops->postreset, NULL);
 865                if (rc) {
 866                        ata_for_each_dev(dev, &ap->link, ALL)
 867                                ata_dev_disable(dev);
 868                        return rc;
 869                }
 870
 871                if (pmp_dev->class != ATA_DEV_PMP)
 872                        return 0;
 873
 874                /* new PMP online */
 875                ata_for_each_link(link, ap, EDGE)
 876                        link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
 877
 878                /* fall through */
 879        }
 880
 881        /* recover pmp */
 882        rc = sata_pmp_eh_recover_pmp(ap, ops->prereset, ops->softreset,
 883                                     ops->hardreset, ops->postreset);
 884        if (rc)
 885                goto pmp_fail;
 886
 887        /* PHY event notification can disturb reset and other recovery
 888         * operations.  Turn it off.
 889         */
 890        if (gscr[SATA_PMP_GSCR_FEAT_EN] & SATA_PMP_FEAT_NOTIFY) {
 891                gscr[SATA_PMP_GSCR_FEAT_EN] &= ~SATA_PMP_FEAT_NOTIFY;
 892
 893                err_mask = sata_pmp_write(pmp_link, SATA_PMP_GSCR_FEAT_EN,
 894                                          gscr[SATA_PMP_GSCR_FEAT_EN]);
 895                if (err_mask) {
 896                        ata_link_printk(pmp_link, KERN_WARNING,
 897                                "failed to disable NOTIFY (err_mask=0x%x)\n",
 898                                err_mask);
 899                        goto pmp_fail;
 900                }
 901        }
 902
 903        /* handle disabled links */
 904        rc = sata_pmp_eh_handle_disabled_links(ap);
 905        if (rc)
 906                goto pmp_fail;
 907
 908        /* recover links */
 909        rc = ata_eh_recover(ap, ops->pmp_prereset, ops->pmp_softreset,
 910                            ops->pmp_hardreset, ops->pmp_postreset, &link);
 911        if (rc)
 912                goto link_fail;
 913
 914        /* Connection status might have changed while resetting other
 915         * links, check SATA_PMP_GSCR_ERROR before returning.
 916         */
 917
 918        /* clear SNotification */
 919        rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
 920        if (rc == 0)
 921                sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
 922
 923        /* enable notification */
 924        if (pmp_dev->flags & ATA_DFLAG_AN) {
 925                gscr[SATA_PMP_GSCR_FEAT_EN] |= SATA_PMP_FEAT_NOTIFY;
 926
 927                err_mask = sata_pmp_write(pmp_link, SATA_PMP_GSCR_FEAT_EN,
 928                                          gscr[SATA_PMP_GSCR_FEAT_EN]);
 929                if (err_mask) {
 930                        ata_dev_printk(pmp_dev, KERN_ERR, "failed to write "
 931                                       "PMP_FEAT_EN (Emask=0x%x)\n", err_mask);
 932                        rc = -EIO;
 933                        goto pmp_fail;
 934                }
 935        }
 936
 937        /* check GSCR_ERROR */
 938        err_mask = sata_pmp_read(pmp_link, SATA_PMP_GSCR_ERROR, &gscr_error);
 939        if (err_mask) {
 940                ata_dev_printk(pmp_dev, KERN_ERR, "failed to read "
 941                               "PMP_GSCR_ERROR (Emask=0x%x)\n", err_mask);
 942                rc = -EIO;
 943                goto pmp_fail;
 944        }
 945
 946        cnt = 0;
 947        ata_for_each_link(link, ap, EDGE) {
 948                if (!(gscr_error & (1 << link->pmp)))
 949                        continue;
 950
 951                if (sata_pmp_handle_link_fail(link, link_tries)) {
 952                        ata_ehi_hotplugged(&link->eh_context.i);
 953                        cnt++;
 954                } else {
 955                        ata_link_printk(link, KERN_WARNING,
 956                                "PHY status changed but maxed out on retries, "
 957                                "giving up\n");
 958                        ata_link_printk(link, KERN_WARNING,
 959                                "Manully issue scan to resume this link\n");
 960                }
 961        }
 962
 963        if (cnt) {
 964                ata_port_printk(ap, KERN_INFO, "PMP SError.N set for some "
 965                                "ports, repeating recovery\n");
 966                goto retry;
 967        }
 968
 969        return 0;
 970
 971 link_fail:
 972        if (sata_pmp_handle_link_fail(link, link_tries)) {
 973                pmp_ehc->i.action |= ATA_EH_RESET;
 974                goto retry;
 975        }
 976
 977        /* fall through */
 978 pmp_fail:
 979        /* Control always ends up here after detaching PMP.  Shut up
 980         * and return if we're unloading.
 981         */
 982        if (ap->pflags & ATA_PFLAG_UNLOADING)
 983                return rc;
 984
 985        if (!sata_pmp_attached(ap))
 986                goto retry;
 987
 988        if (--pmp_tries) {
 989                pmp_ehc->i.action |= ATA_EH_RESET;
 990                goto retry;
 991        }
 992
 993        ata_port_printk(ap, KERN_ERR,
 994                        "failed to recover PMP after %d tries, giving up\n",
 995                        ATA_EH_PMP_TRIES);
 996        sata_pmp_detach(pmp_dev);
 997        ata_dev_disable(pmp_dev);
 998
 999        return rc;
1000}
1001
1002/**
1003 *      sata_pmp_error_handler - do standard error handling for PMP-enabled host
1004 *      @ap: host port to handle error for
1005 *
1006 *      Perform standard error handling sequence for PMP-enabled host
1007 *      @ap.
1008 *
1009 *      LOCKING:
1010 *      Kernel thread context (may sleep).
1011 */
1012void sata_pmp_error_handler(struct ata_port *ap)
1013{
1014        ata_eh_autopsy(ap);
1015        ata_eh_report(ap);
1016        sata_pmp_eh_recover(ap);
1017        ata_eh_finish(ap);
1018}
1019
1020EXPORT_SYMBOL_GPL(sata_pmp_port_ops);
1021EXPORT_SYMBOL_GPL(sata_pmp_qc_defer_cmd_switch);
1022EXPORT_SYMBOL_GPL(sata_pmp_error_handler);
1023