Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  SATA specific part of ATA helper library
0004  *
0005  *  Copyright 2003-2004 Red Hat, Inc.  All rights reserved.
0006  *  Copyright 2003-2004 Jeff Garzik
0007  *  Copyright 2006 Tejun Heo <htejun@gmail.com>
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <scsi/scsi_cmnd.h>
0013 #include <scsi/scsi_device.h>
0014 #include <linux/libata.h>
0015 
0016 #include "libata.h"
0017 #include "libata-transport.h"
0018 
0019 /* debounce timing parameters in msecs { interval, duration, timeout } */
0020 const unsigned long sata_deb_timing_normal[]        = {   5,  100, 2000 };
0021 EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
0022 const unsigned long sata_deb_timing_hotplug[]       = {  25,  500, 2000 };
0023 EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
0024 const unsigned long sata_deb_timing_long[]      = { 100, 2000, 5000 };
0025 EXPORT_SYMBOL_GPL(sata_deb_timing_long);
0026 
0027 /**
0028  *  sata_scr_valid - test whether SCRs are accessible
0029  *  @link: ATA link to test SCR accessibility for
0030  *
0031  *  Test whether SCRs are accessible for @link.
0032  *
0033  *  LOCKING:
0034  *  None.
0035  *
0036  *  RETURNS:
0037  *  1 if SCRs are accessible, 0 otherwise.
0038  */
0039 int sata_scr_valid(struct ata_link *link)
0040 {
0041     struct ata_port *ap = link->ap;
0042 
0043     return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
0044 }
0045 EXPORT_SYMBOL_GPL(sata_scr_valid);
0046 
0047 /**
0048  *  sata_scr_read - read SCR register of the specified port
0049  *  @link: ATA link to read SCR for
0050  *  @reg: SCR to read
0051  *  @val: Place to store read value
0052  *
0053  *  Read SCR register @reg of @link into *@val.  This function is
0054  *  guaranteed to succeed if @link is ap->link, the cable type of
0055  *  the port is SATA and the port implements ->scr_read.
0056  *
0057  *  LOCKING:
0058  *  None if @link is ap->link.  Kernel thread context otherwise.
0059  *
0060  *  RETURNS:
0061  *  0 on success, negative errno on failure.
0062  */
0063 int sata_scr_read(struct ata_link *link, int reg, u32 *val)
0064 {
0065     if (ata_is_host_link(link)) {
0066         if (sata_scr_valid(link))
0067             return link->ap->ops->scr_read(link, reg, val);
0068         return -EOPNOTSUPP;
0069     }
0070 
0071     return sata_pmp_scr_read(link, reg, val);
0072 }
0073 EXPORT_SYMBOL_GPL(sata_scr_read);
0074 
0075 /**
0076  *  sata_scr_write - write SCR register of the specified port
0077  *  @link: ATA link to write SCR for
0078  *  @reg: SCR to write
0079  *  @val: value to write
0080  *
0081  *  Write @val to SCR register @reg of @link.  This function is
0082  *  guaranteed to succeed if @link is ap->link, the cable type of
0083  *  the port is SATA and the port implements ->scr_read.
0084  *
0085  *  LOCKING:
0086  *  None if @link is ap->link.  Kernel thread context otherwise.
0087  *
0088  *  RETURNS:
0089  *  0 on success, negative errno on failure.
0090  */
0091 int sata_scr_write(struct ata_link *link, int reg, u32 val)
0092 {
0093     if (ata_is_host_link(link)) {
0094         if (sata_scr_valid(link))
0095             return link->ap->ops->scr_write(link, reg, val);
0096         return -EOPNOTSUPP;
0097     }
0098 
0099     return sata_pmp_scr_write(link, reg, val);
0100 }
0101 EXPORT_SYMBOL_GPL(sata_scr_write);
0102 
0103 /**
0104  *  sata_scr_write_flush - write SCR register of the specified port and flush
0105  *  @link: ATA link to write SCR for
0106  *  @reg: SCR to write
0107  *  @val: value to write
0108  *
0109  *  This function is identical to sata_scr_write() except that this
0110  *  function performs flush after writing to the register.
0111  *
0112  *  LOCKING:
0113  *  None if @link is ap->link.  Kernel thread context otherwise.
0114  *
0115  *  RETURNS:
0116  *  0 on success, negative errno on failure.
0117  */
0118 int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
0119 {
0120     if (ata_is_host_link(link)) {
0121         int rc;
0122 
0123         if (sata_scr_valid(link)) {
0124             rc = link->ap->ops->scr_write(link, reg, val);
0125             if (rc == 0)
0126                 rc = link->ap->ops->scr_read(link, reg, &val);
0127             return rc;
0128         }
0129         return -EOPNOTSUPP;
0130     }
0131 
0132     return sata_pmp_scr_write(link, reg, val);
0133 }
0134 EXPORT_SYMBOL_GPL(sata_scr_write_flush);
0135 
0136 /**
0137  *  ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
0138  *  @tf: Taskfile to convert
0139  *  @pmp: Port multiplier port
0140  *  @is_cmd: This FIS is for command
0141  *  @fis: Buffer into which data will output
0142  *
0143  *  Converts a standard ATA taskfile to a Serial ATA
0144  *  FIS structure (Register - Host to Device).
0145  *
0146  *  LOCKING:
0147  *  Inherited from caller.
0148  */
0149 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
0150 {
0151     fis[0] = 0x27;          /* Register - Host to Device FIS */
0152     fis[1] = pmp & 0xf;     /* Port multiplier number*/
0153     if (is_cmd)
0154         fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
0155 
0156     fis[2] = tf->command;
0157     fis[3] = tf->feature;
0158 
0159     fis[4] = tf->lbal;
0160     fis[5] = tf->lbam;
0161     fis[6] = tf->lbah;
0162     fis[7] = tf->device;
0163 
0164     fis[8] = tf->hob_lbal;
0165     fis[9] = tf->hob_lbam;
0166     fis[10] = tf->hob_lbah;
0167     fis[11] = tf->hob_feature;
0168 
0169     fis[12] = tf->nsect;
0170     fis[13] = tf->hob_nsect;
0171     fis[14] = 0;
0172     fis[15] = tf->ctl;
0173 
0174     fis[16] = tf->auxiliary & 0xff;
0175     fis[17] = (tf->auxiliary >> 8) & 0xff;
0176     fis[18] = (tf->auxiliary >> 16) & 0xff;
0177     fis[19] = (tf->auxiliary >> 24) & 0xff;
0178 }
0179 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
0180 
0181 /**
0182  *  ata_tf_from_fis - Convert SATA FIS to ATA taskfile
0183  *  @fis: Buffer from which data will be input
0184  *  @tf: Taskfile to output
0185  *
0186  *  Converts a serial ATA FIS structure to a standard ATA taskfile.
0187  *
0188  *  LOCKING:
0189  *  Inherited from caller.
0190  */
0191 
0192 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
0193 {
0194     tf->status  = fis[2];
0195     tf->error   = fis[3];
0196 
0197     tf->lbal    = fis[4];
0198     tf->lbam    = fis[5];
0199     tf->lbah    = fis[6];
0200     tf->device  = fis[7];
0201 
0202     tf->hob_lbal    = fis[8];
0203     tf->hob_lbam    = fis[9];
0204     tf->hob_lbah    = fis[10];
0205 
0206     tf->nsect   = fis[12];
0207     tf->hob_nsect   = fis[13];
0208 }
0209 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
0210 
0211 /**
0212  *  sata_link_debounce - debounce SATA phy status
0213  *  @link: ATA link to debounce SATA phy status for
0214  *  @params: timing parameters { interval, duration, timeout } in msec
0215  *  @deadline: deadline jiffies for the operation
0216  *
0217  *  Make sure SStatus of @link reaches stable state, determined by
0218  *  holding the same value where DET is not 1 for @duration polled
0219  *  every @interval, before @timeout.  Timeout constraints the
0220  *  beginning of the stable state.  Because DET gets stuck at 1 on
0221  *  some controllers after hot unplugging, this functions waits
0222  *  until timeout then returns 0 if DET is stable at 1.
0223  *
0224  *  @timeout is further limited by @deadline.  The sooner of the
0225  *  two is used.
0226  *
0227  *  LOCKING:
0228  *  Kernel thread context (may sleep)
0229  *
0230  *  RETURNS:
0231  *  0 on success, -errno on failure.
0232  */
0233 int sata_link_debounce(struct ata_link *link, const unsigned long *params,
0234                unsigned long deadline)
0235 {
0236     unsigned long interval = params[0];
0237     unsigned long duration = params[1];
0238     unsigned long last_jiffies, t;
0239     u32 last, cur;
0240     int rc;
0241 
0242     t = ata_deadline(jiffies, params[2]);
0243     if (time_before(t, deadline))
0244         deadline = t;
0245 
0246     if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
0247         return rc;
0248     cur &= 0xf;
0249 
0250     last = cur;
0251     last_jiffies = jiffies;
0252 
0253     while (1) {
0254         ata_msleep(link->ap, interval);
0255         if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
0256             return rc;
0257         cur &= 0xf;
0258 
0259         /* DET stable? */
0260         if (cur == last) {
0261             if (cur == 1 && time_before(jiffies, deadline))
0262                 continue;
0263             if (time_after(jiffies,
0264                        ata_deadline(last_jiffies, duration)))
0265                 return 0;
0266             continue;
0267         }
0268 
0269         /* unstable, start over */
0270         last = cur;
0271         last_jiffies = jiffies;
0272 
0273         /* Check deadline.  If debouncing failed, return
0274          * -EPIPE to tell upper layer to lower link speed.
0275          */
0276         if (time_after(jiffies, deadline))
0277             return -EPIPE;
0278     }
0279 }
0280 EXPORT_SYMBOL_GPL(sata_link_debounce);
0281 
0282 /**
0283  *  sata_link_resume - resume SATA link
0284  *  @link: ATA link to resume SATA
0285  *  @params: timing parameters { interval, duration, timeout } in msec
0286  *  @deadline: deadline jiffies for the operation
0287  *
0288  *  Resume SATA phy @link and debounce it.
0289  *
0290  *  LOCKING:
0291  *  Kernel thread context (may sleep)
0292  *
0293  *  RETURNS:
0294  *  0 on success, -errno on failure.
0295  */
0296 int sata_link_resume(struct ata_link *link, const unsigned long *params,
0297              unsigned long deadline)
0298 {
0299     int tries = ATA_LINK_RESUME_TRIES;
0300     u32 scontrol, serror;
0301     int rc;
0302 
0303     if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
0304         return rc;
0305 
0306     /*
0307      * Writes to SControl sometimes get ignored under certain
0308      * controllers (ata_piix SIDPR).  Make sure DET actually is
0309      * cleared.
0310      */
0311     do {
0312         scontrol = (scontrol & 0x0f0) | 0x300;
0313         if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
0314             return rc;
0315         /*
0316          * Some PHYs react badly if SStatus is pounded
0317          * immediately after resuming.  Delay 200ms before
0318          * debouncing.
0319          */
0320         if (!(link->flags & ATA_LFLAG_NO_DEBOUNCE_DELAY))
0321             ata_msleep(link->ap, 200);
0322 
0323         /* is SControl restored correctly? */
0324         if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
0325             return rc;
0326     } while ((scontrol & 0xf0f) != 0x300 && --tries);
0327 
0328     if ((scontrol & 0xf0f) != 0x300) {
0329         ata_link_warn(link, "failed to resume link (SControl %X)\n",
0330                  scontrol);
0331         return 0;
0332     }
0333 
0334     if (tries < ATA_LINK_RESUME_TRIES)
0335         ata_link_warn(link, "link resume succeeded after %d retries\n",
0336                   ATA_LINK_RESUME_TRIES - tries);
0337 
0338     if ((rc = sata_link_debounce(link, params, deadline)))
0339         return rc;
0340 
0341     /* clear SError, some PHYs require this even for SRST to work */
0342     if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
0343         rc = sata_scr_write(link, SCR_ERROR, serror);
0344 
0345     return rc != -EINVAL ? rc : 0;
0346 }
0347 EXPORT_SYMBOL_GPL(sata_link_resume);
0348 
0349 /**
0350  *  sata_link_scr_lpm - manipulate SControl IPM and SPM fields
0351  *  @link: ATA link to manipulate SControl for
0352  *  @policy: LPM policy to configure
0353  *  @spm_wakeup: initiate LPM transition to active state
0354  *
0355  *  Manipulate the IPM field of the SControl register of @link
0356  *  according to @policy.  If @policy is ATA_LPM_MAX_POWER and
0357  *  @spm_wakeup is %true, the SPM field is manipulated to wake up
0358  *  the link.  This function also clears PHYRDY_CHG before
0359  *  returning.
0360  *
0361  *  LOCKING:
0362  *  EH context.
0363  *
0364  *  RETURNS:
0365  *  0 on success, -errno otherwise.
0366  */
0367 int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
0368               bool spm_wakeup)
0369 {
0370     struct ata_eh_context *ehc = &link->eh_context;
0371     bool woken_up = false;
0372     u32 scontrol;
0373     int rc;
0374 
0375     rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
0376     if (rc)
0377         return rc;
0378 
0379     switch (policy) {
0380     case ATA_LPM_MAX_POWER:
0381         /* disable all LPM transitions */
0382         scontrol |= (0x7 << 8);
0383         /* initiate transition to active state */
0384         if (spm_wakeup) {
0385             scontrol |= (0x4 << 12);
0386             woken_up = true;
0387         }
0388         break;
0389     case ATA_LPM_MED_POWER:
0390         /* allow LPM to PARTIAL */
0391         scontrol &= ~(0x1 << 8);
0392         scontrol |= (0x6 << 8);
0393         break;
0394     case ATA_LPM_MED_POWER_WITH_DIPM:
0395     case ATA_LPM_MIN_POWER_WITH_PARTIAL:
0396     case ATA_LPM_MIN_POWER:
0397         if (ata_link_nr_enabled(link) > 0)
0398             /* no restrictions on LPM transitions */
0399             scontrol &= ~(0x7 << 8);
0400         else {
0401             /* empty port, power off */
0402             scontrol &= ~0xf;
0403             scontrol |= (0x1 << 2);
0404         }
0405         break;
0406     default:
0407         WARN_ON(1);
0408     }
0409 
0410     rc = sata_scr_write(link, SCR_CONTROL, scontrol);
0411     if (rc)
0412         return rc;
0413 
0414     /* give the link time to transit out of LPM state */
0415     if (woken_up)
0416         msleep(10);
0417 
0418     /* clear PHYRDY_CHG from SError */
0419     ehc->i.serror &= ~SERR_PHYRDY_CHG;
0420     return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
0421 }
0422 EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
0423 
0424 static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
0425 {
0426     struct ata_link *host_link = &link->ap->link;
0427     u32 limit, target, spd;
0428 
0429     limit = link->sata_spd_limit;
0430 
0431     /* Don't configure downstream link faster than upstream link.
0432      * It doesn't speed up anything and some PMPs choke on such
0433      * configuration.
0434      */
0435     if (!ata_is_host_link(link) && host_link->sata_spd)
0436         limit &= (1 << host_link->sata_spd) - 1;
0437 
0438     if (limit == UINT_MAX)
0439         target = 0;
0440     else
0441         target = fls(limit);
0442 
0443     spd = (*scontrol >> 4) & 0xf;
0444     *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
0445 
0446     return spd != target;
0447 }
0448 
0449 /**
0450  *  sata_set_spd_needed - is SATA spd configuration needed
0451  *  @link: Link in question
0452  *
0453  *  Test whether the spd limit in SControl matches
0454  *  @link->sata_spd_limit.  This function is used to determine
0455  *  whether hardreset is necessary to apply SATA spd
0456  *  configuration.
0457  *
0458  *  LOCKING:
0459  *  Inherited from caller.
0460  *
0461  *  RETURNS:
0462  *  1 if SATA spd configuration is needed, 0 otherwise.
0463  */
0464 static int sata_set_spd_needed(struct ata_link *link)
0465 {
0466     u32 scontrol;
0467 
0468     if (sata_scr_read(link, SCR_CONTROL, &scontrol))
0469         return 1;
0470 
0471     return __sata_set_spd_needed(link, &scontrol);
0472 }
0473 
0474 /**
0475  *  sata_set_spd - set SATA spd according to spd limit
0476  *  @link: Link to set SATA spd for
0477  *
0478  *  Set SATA spd of @link according to sata_spd_limit.
0479  *
0480  *  LOCKING:
0481  *  Inherited from caller.
0482  *
0483  *  RETURNS:
0484  *  0 if spd doesn't need to be changed, 1 if spd has been
0485  *  changed.  Negative errno if SCR registers are inaccessible.
0486  */
0487 int sata_set_spd(struct ata_link *link)
0488 {
0489     u32 scontrol;
0490     int rc;
0491 
0492     if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
0493         return rc;
0494 
0495     if (!__sata_set_spd_needed(link, &scontrol))
0496         return 0;
0497 
0498     if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
0499         return rc;
0500 
0501     return 1;
0502 }
0503 EXPORT_SYMBOL_GPL(sata_set_spd);
0504 
0505 /**
0506  *  sata_link_hardreset - reset link via SATA phy reset
0507  *  @link: link to reset
0508  *  @timing: timing parameters { interval, duration, timeout } in msec
0509  *  @deadline: deadline jiffies for the operation
0510  *  @online: optional out parameter indicating link onlineness
0511  *  @check_ready: optional callback to check link readiness
0512  *
0513  *  SATA phy-reset @link using DET bits of SControl register.
0514  *  After hardreset, link readiness is waited upon using
0515  *  ata_wait_ready() if @check_ready is specified.  LLDs are
0516  *  allowed to not specify @check_ready and wait itself after this
0517  *  function returns.  Device classification is LLD's
0518  *  responsibility.
0519  *
0520  *  *@online is set to one iff reset succeeded and @link is online
0521  *  after reset.
0522  *
0523  *  LOCKING:
0524  *  Kernel thread context (may sleep)
0525  *
0526  *  RETURNS:
0527  *  0 on success, -errno otherwise.
0528  */
0529 int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
0530             unsigned long deadline,
0531             bool *online, int (*check_ready)(struct ata_link *))
0532 {
0533     u32 scontrol;
0534     int rc;
0535 
0536     if (online)
0537         *online = false;
0538 
0539     if (sata_set_spd_needed(link)) {
0540         /* SATA spec says nothing about how to reconfigure
0541          * spd.  To be on the safe side, turn off phy during
0542          * reconfiguration.  This works for at least ICH7 AHCI
0543          * and Sil3124.
0544          */
0545         if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
0546             goto out;
0547 
0548         scontrol = (scontrol & 0x0f0) | 0x304;
0549 
0550         if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
0551             goto out;
0552 
0553         sata_set_spd(link);
0554     }
0555 
0556     /* issue phy wake/reset */
0557     if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
0558         goto out;
0559 
0560     scontrol = (scontrol & 0x0f0) | 0x301;
0561 
0562     if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
0563         goto out;
0564 
0565     /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
0566      * 10.4.2 says at least 1 ms.
0567      */
0568     ata_msleep(link->ap, 1);
0569 
0570     /* bring link back */
0571     rc = sata_link_resume(link, timing, deadline);
0572     if (rc)
0573         goto out;
0574     /* if link is offline nothing more to do */
0575     if (ata_phys_link_offline(link))
0576         goto out;
0577 
0578     /* Link is online.  From this point, -ENODEV too is an error. */
0579     if (online)
0580         *online = true;
0581 
0582     if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
0583         /* If PMP is supported, we have to do follow-up SRST.
0584          * Some PMPs don't send D2H Reg FIS after hardreset if
0585          * the first port is empty.  Wait only for
0586          * ATA_TMOUT_PMP_SRST_WAIT.
0587          */
0588         if (check_ready) {
0589             unsigned long pmp_deadline;
0590 
0591             pmp_deadline = ata_deadline(jiffies,
0592                             ATA_TMOUT_PMP_SRST_WAIT);
0593             if (time_after(pmp_deadline, deadline))
0594                 pmp_deadline = deadline;
0595             ata_wait_ready(link, pmp_deadline, check_ready);
0596         }
0597         rc = -EAGAIN;
0598         goto out;
0599     }
0600 
0601     rc = 0;
0602     if (check_ready)
0603         rc = ata_wait_ready(link, deadline, check_ready);
0604  out:
0605     if (rc && rc != -EAGAIN) {
0606         /* online is set iff link is online && reset succeeded */
0607         if (online)
0608             *online = false;
0609         ata_link_err(link, "COMRESET failed (errno=%d)\n", rc);
0610     }
0611     return rc;
0612 }
0613 EXPORT_SYMBOL_GPL(sata_link_hardreset);
0614 
0615 /**
0616  *  ata_qc_complete_multiple - Complete multiple qcs successfully
0617  *  @ap: port in question
0618  *  @qc_active: new qc_active mask
0619  *
0620  *  Complete in-flight commands.  This functions is meant to be
0621  *  called from low-level driver's interrupt routine to complete
0622  *  requests normally.  ap->qc_active and @qc_active is compared
0623  *  and commands are completed accordingly.
0624  *
0625  *  Always use this function when completing multiple NCQ commands
0626  *  from IRQ handlers instead of calling ata_qc_complete()
0627  *  multiple times to keep IRQ expect status properly in sync.
0628  *
0629  *  LOCKING:
0630  *  spin_lock_irqsave(host lock)
0631  *
0632  *  RETURNS:
0633  *  Number of completed commands on success, -errno otherwise.
0634  */
0635 int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active)
0636 {
0637     u64 done_mask, ap_qc_active = ap->qc_active;
0638     int nr_done = 0;
0639 
0640     /*
0641      * If the internal tag is set on ap->qc_active, then we care about
0642      * bit0 on the passed in qc_active mask. Move that bit up to match
0643      * the internal tag.
0644      */
0645     if (ap_qc_active & (1ULL << ATA_TAG_INTERNAL)) {
0646         qc_active |= (qc_active & 0x01) << ATA_TAG_INTERNAL;
0647         qc_active ^= qc_active & 0x01;
0648     }
0649 
0650     done_mask = ap_qc_active ^ qc_active;
0651 
0652     if (unlikely(done_mask & qc_active)) {
0653         ata_port_err(ap, "illegal qc_active transition (%08llx->%08llx)\n",
0654                  ap->qc_active, qc_active);
0655         return -EINVAL;
0656     }
0657 
0658     while (done_mask) {
0659         struct ata_queued_cmd *qc;
0660         unsigned int tag = __ffs64(done_mask);
0661 
0662         qc = ata_qc_from_tag(ap, tag);
0663         if (qc) {
0664             ata_qc_complete(qc);
0665             nr_done++;
0666         }
0667         done_mask &= ~(1ULL << tag);
0668     }
0669 
0670     return nr_done;
0671 }
0672 EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
0673 
0674 /**
0675  *  ata_slave_link_init - initialize slave link
0676  *  @ap: port to initialize slave link for
0677  *
0678  *  Create and initialize slave link for @ap.  This enables slave
0679  *  link handling on the port.
0680  *
0681  *  In libata, a port contains links and a link contains devices.
0682  *  There is single host link but if a PMP is attached to it,
0683  *  there can be multiple fan-out links.  On SATA, there's usually
0684  *  a single device connected to a link but PATA and SATA
0685  *  controllers emulating TF based interface can have two - master
0686  *  and slave.
0687  *
0688  *  However, there are a few controllers which don't fit into this
0689  *  abstraction too well - SATA controllers which emulate TF
0690  *  interface with both master and slave devices but also have
0691  *  separate SCR register sets for each device.  These controllers
0692  *  need separate links for physical link handling
0693  *  (e.g. onlineness, link speed) but should be treated like a
0694  *  traditional M/S controller for everything else (e.g. command
0695  *  issue, softreset).
0696  *
0697  *  slave_link is libata's way of handling this class of
0698  *  controllers without impacting core layer too much.  For
0699  *  anything other than physical link handling, the default host
0700  *  link is used for both master and slave.  For physical link
0701  *  handling, separate @ap->slave_link is used.  All dirty details
0702  *  are implemented inside libata core layer.  From LLD's POV, the
0703  *  only difference is that prereset, hardreset and postreset are
0704  *  called once more for the slave link, so the reset sequence
0705  *  looks like the following.
0706  *
0707  *  prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
0708  *  softreset(M) -> postreset(M) -> postreset(S)
0709  *
0710  *  Note that softreset is called only for the master.  Softreset
0711  *  resets both M/S by definition, so SRST on master should handle
0712  *  both (the standard method will work just fine).
0713  *
0714  *  LOCKING:
0715  *  Should be called before host is registered.
0716  *
0717  *  RETURNS:
0718  *  0 on success, -errno on failure.
0719  */
0720 int ata_slave_link_init(struct ata_port *ap)
0721 {
0722     struct ata_link *link;
0723 
0724     WARN_ON(ap->slave_link);
0725     WARN_ON(ap->flags & ATA_FLAG_PMP);
0726 
0727     link = kzalloc(sizeof(*link), GFP_KERNEL);
0728     if (!link)
0729         return -ENOMEM;
0730 
0731     ata_link_init(ap, link, 1);
0732     ap->slave_link = link;
0733     return 0;
0734 }
0735 EXPORT_SYMBOL_GPL(ata_slave_link_init);
0736 
0737 /**
0738  *  sata_lpm_ignore_phy_events - test if PHY event should be ignored
0739  *  @link: Link receiving the event
0740  *
0741  *  Test whether the received PHY event has to be ignored or not.
0742  *
0743  *  LOCKING:
0744  *  None:
0745  *
0746  *  RETURNS:
0747  *  True if the event has to be ignored.
0748  */
0749 bool sata_lpm_ignore_phy_events(struct ata_link *link)
0750 {
0751     unsigned long lpm_timeout = link->last_lpm_change +
0752                     msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY);
0753 
0754     /* if LPM is enabled, PHYRDY doesn't mean anything */
0755     if (link->lpm_policy > ATA_LPM_MAX_POWER)
0756         return true;
0757 
0758     /* ignore the first PHY event after the LPM policy changed
0759      * as it is might be spurious
0760      */
0761     if ((link->flags & ATA_LFLAG_CHANGED) &&
0762         time_before(jiffies, lpm_timeout))
0763         return true;
0764 
0765     return false;
0766 }
0767 EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events);
0768 
0769 static const char *ata_lpm_policy_names[] = {
0770     [ATA_LPM_UNKNOWN]       = "max_performance",
0771     [ATA_LPM_MAX_POWER]     = "max_performance",
0772     [ATA_LPM_MED_POWER]     = "medium_power",
0773     [ATA_LPM_MED_POWER_WITH_DIPM]   = "med_power_with_dipm",
0774     [ATA_LPM_MIN_POWER_WITH_PARTIAL] = "min_power_with_partial",
0775     [ATA_LPM_MIN_POWER]     = "min_power",
0776 };
0777 
0778 static ssize_t ata_scsi_lpm_store(struct device *device,
0779                   struct device_attribute *attr,
0780                   const char *buf, size_t count)
0781 {
0782     struct Scsi_Host *shost = class_to_shost(device);
0783     struct ata_port *ap = ata_shost_to_port(shost);
0784     struct ata_link *link;
0785     struct ata_device *dev;
0786     enum ata_lpm_policy policy;
0787     unsigned long flags;
0788 
0789     /* UNKNOWN is internal state, iterate from MAX_POWER */
0790     for (policy = ATA_LPM_MAX_POWER;
0791          policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {
0792         const char *name = ata_lpm_policy_names[policy];
0793 
0794         if (strncmp(name, buf, strlen(name)) == 0)
0795             break;
0796     }
0797     if (policy == ARRAY_SIZE(ata_lpm_policy_names))
0798         return -EINVAL;
0799 
0800     spin_lock_irqsave(ap->lock, flags);
0801 
0802     ata_for_each_link(link, ap, EDGE) {
0803         ata_for_each_dev(dev, &ap->link, ENABLED) {
0804             if (dev->horkage & ATA_HORKAGE_NOLPM) {
0805                 count = -EOPNOTSUPP;
0806                 goto out_unlock;
0807             }
0808         }
0809     }
0810 
0811     ap->target_lpm_policy = policy;
0812     ata_port_schedule_eh(ap);
0813 out_unlock:
0814     spin_unlock_irqrestore(ap->lock, flags);
0815     return count;
0816 }
0817 
0818 static ssize_t ata_scsi_lpm_show(struct device *dev,
0819                  struct device_attribute *attr, char *buf)
0820 {
0821     struct Scsi_Host *shost = class_to_shost(dev);
0822     struct ata_port *ap = ata_shost_to_port(shost);
0823 
0824     if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
0825         return -EINVAL;
0826 
0827     return sysfs_emit(buf, "%s\n",
0828             ata_lpm_policy_names[ap->target_lpm_policy]);
0829 }
0830 DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
0831         ata_scsi_lpm_show, ata_scsi_lpm_store);
0832 EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
0833 
0834 static ssize_t ata_ncq_prio_supported_show(struct device *device,
0835                        struct device_attribute *attr,
0836                        char *buf)
0837 {
0838     struct scsi_device *sdev = to_scsi_device(device);
0839     struct ata_port *ap = ata_shost_to_port(sdev->host);
0840     struct ata_device *dev;
0841     bool ncq_prio_supported;
0842     int rc = 0;
0843 
0844     spin_lock_irq(ap->lock);
0845     dev = ata_scsi_find_dev(ap, sdev);
0846     if (!dev)
0847         rc = -ENODEV;
0848     else
0849         ncq_prio_supported = dev->flags & ATA_DFLAG_NCQ_PRIO;
0850     spin_unlock_irq(ap->lock);
0851 
0852     return rc ? rc : sysfs_emit(buf, "%u\n", ncq_prio_supported);
0853 }
0854 
0855 DEVICE_ATTR(ncq_prio_supported, S_IRUGO, ata_ncq_prio_supported_show, NULL);
0856 EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_supported);
0857 
0858 static ssize_t ata_ncq_prio_enable_show(struct device *device,
0859                     struct device_attribute *attr,
0860                     char *buf)
0861 {
0862     struct scsi_device *sdev = to_scsi_device(device);
0863     struct ata_port *ap = ata_shost_to_port(sdev->host);
0864     struct ata_device *dev;
0865     bool ncq_prio_enable;
0866     int rc = 0;
0867 
0868     spin_lock_irq(ap->lock);
0869     dev = ata_scsi_find_dev(ap, sdev);
0870     if (!dev)
0871         rc = -ENODEV;
0872     else
0873         ncq_prio_enable = dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE;
0874     spin_unlock_irq(ap->lock);
0875 
0876     return rc ? rc : sysfs_emit(buf, "%u\n", ncq_prio_enable);
0877 }
0878 
0879 static ssize_t ata_ncq_prio_enable_store(struct device *device,
0880                      struct device_attribute *attr,
0881                      const char *buf, size_t len)
0882 {
0883     struct scsi_device *sdev = to_scsi_device(device);
0884     struct ata_port *ap;
0885     struct ata_device *dev;
0886     long int input;
0887     int rc = 0;
0888 
0889     rc = kstrtol(buf, 10, &input);
0890     if (rc)
0891         return rc;
0892     if ((input < 0) || (input > 1))
0893         return -EINVAL;
0894 
0895     ap = ata_shost_to_port(sdev->host);
0896     dev = ata_scsi_find_dev(ap, sdev);
0897     if (unlikely(!dev))
0898         return  -ENODEV;
0899 
0900     spin_lock_irq(ap->lock);
0901 
0902     if (!(dev->flags & ATA_DFLAG_NCQ_PRIO)) {
0903         rc = -EINVAL;
0904         goto unlock;
0905     }
0906 
0907     if (input)
0908         dev->flags |= ATA_DFLAG_NCQ_PRIO_ENABLE;
0909     else
0910         dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
0911 
0912 unlock:
0913     spin_unlock_irq(ap->lock);
0914 
0915     return rc ? rc : len;
0916 }
0917 
0918 DEVICE_ATTR(ncq_prio_enable, S_IRUGO | S_IWUSR,
0919         ata_ncq_prio_enable_show, ata_ncq_prio_enable_store);
0920 EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_enable);
0921 
0922 static struct attribute *ata_ncq_sdev_attrs[] = {
0923     &dev_attr_unload_heads.attr,
0924     &dev_attr_ncq_prio_enable.attr,
0925     &dev_attr_ncq_prio_supported.attr,
0926     NULL
0927 };
0928 
0929 static const struct attribute_group ata_ncq_sdev_attr_group = {
0930     .attrs = ata_ncq_sdev_attrs
0931 };
0932 
0933 const struct attribute_group *ata_ncq_sdev_groups[] = {
0934     &ata_ncq_sdev_attr_group,
0935     NULL
0936 };
0937 EXPORT_SYMBOL_GPL(ata_ncq_sdev_groups);
0938 
0939 static ssize_t
0940 ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
0941               const char *buf, size_t count)
0942 {
0943     struct Scsi_Host *shost = class_to_shost(dev);
0944     struct ata_port *ap = ata_shost_to_port(shost);
0945     if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
0946         return ap->ops->em_store(ap, buf, count);
0947     return -EINVAL;
0948 }
0949 
0950 static ssize_t
0951 ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
0952              char *buf)
0953 {
0954     struct Scsi_Host *shost = class_to_shost(dev);
0955     struct ata_port *ap = ata_shost_to_port(shost);
0956 
0957     if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
0958         return ap->ops->em_show(ap, buf);
0959     return -EINVAL;
0960 }
0961 DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
0962         ata_scsi_em_message_show, ata_scsi_em_message_store);
0963 EXPORT_SYMBOL_GPL(dev_attr_em_message);
0964 
0965 static ssize_t
0966 ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
0967                   char *buf)
0968 {
0969     struct Scsi_Host *shost = class_to_shost(dev);
0970     struct ata_port *ap = ata_shost_to_port(shost);
0971 
0972     return sysfs_emit(buf, "%d\n", ap->em_message_type);
0973 }
0974 DEVICE_ATTR(em_message_type, S_IRUGO,
0975           ata_scsi_em_message_type_show, NULL);
0976 EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
0977 
0978 static ssize_t
0979 ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
0980         char *buf)
0981 {
0982     struct scsi_device *sdev = to_scsi_device(dev);
0983     struct ata_port *ap = ata_shost_to_port(sdev->host);
0984     struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
0985 
0986     if (atadev && ap->ops->sw_activity_show &&
0987         (ap->flags & ATA_FLAG_SW_ACTIVITY))
0988         return ap->ops->sw_activity_show(atadev, buf);
0989     return -EINVAL;
0990 }
0991 
0992 static ssize_t
0993 ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
0994     const char *buf, size_t count)
0995 {
0996     struct scsi_device *sdev = to_scsi_device(dev);
0997     struct ata_port *ap = ata_shost_to_port(sdev->host);
0998     struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
0999     enum sw_activity val;
1000     int rc;
1001 
1002     if (atadev && ap->ops->sw_activity_store &&
1003         (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
1004         val = simple_strtoul(buf, NULL, 0);
1005         switch (val) {
1006         case OFF: case BLINK_ON: case BLINK_OFF:
1007             rc = ap->ops->sw_activity_store(atadev, val);
1008             if (!rc)
1009                 return count;
1010             else
1011                 return rc;
1012         }
1013     }
1014     return -EINVAL;
1015 }
1016 DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
1017             ata_scsi_activity_store);
1018 EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
1019 
1020 /**
1021  *  ata_change_queue_depth - Set a device maximum queue depth
1022  *  @ap: ATA port of the target device
1023  *  @dev: target ATA device
1024  *  @sdev: SCSI device to configure queue depth for
1025  *  @queue_depth: new queue depth
1026  *
1027  *  Helper to set a device maximum queue depth, usable with both libsas
1028  *  and libata.
1029  *
1030  */
1031 int ata_change_queue_depth(struct ata_port *ap, struct ata_device *dev,
1032                struct scsi_device *sdev, int queue_depth)
1033 {
1034     unsigned long flags;
1035 
1036     if (!dev || !ata_dev_enabled(dev))
1037         return sdev->queue_depth;
1038 
1039     if (queue_depth < 1 || queue_depth == sdev->queue_depth)
1040         return sdev->queue_depth;
1041 
1042     /* NCQ enabled? */
1043     spin_lock_irqsave(ap->lock, flags);
1044     dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1045     if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
1046         dev->flags |= ATA_DFLAG_NCQ_OFF;
1047         queue_depth = 1;
1048     }
1049     spin_unlock_irqrestore(ap->lock, flags);
1050 
1051     /* limit and apply queue depth */
1052     queue_depth = min(queue_depth, sdev->host->can_queue);
1053     queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
1054     queue_depth = min(queue_depth, ATA_MAX_QUEUE);
1055 
1056     if (sdev->queue_depth == queue_depth)
1057         return -EINVAL;
1058 
1059     return scsi_change_queue_depth(sdev, queue_depth);
1060 }
1061 EXPORT_SYMBOL_GPL(ata_change_queue_depth);
1062 
1063 /**
1064  *  ata_scsi_change_queue_depth - SCSI callback for queue depth config
1065  *  @sdev: SCSI device to configure queue depth for
1066  *  @queue_depth: new queue depth
1067  *
1068  *  This is libata standard hostt->change_queue_depth callback.
1069  *  SCSI will call into this callback when user tries to set queue
1070  *  depth via sysfs.
1071  *
1072  *  LOCKING:
1073  *  SCSI layer (we don't care)
1074  *
1075  *  RETURNS:
1076  *  Newly configured queue depth.
1077  */
1078 int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
1079 {
1080     struct ata_port *ap = ata_shost_to_port(sdev->host);
1081 
1082     return ata_change_queue_depth(ap, ata_scsi_find_dev(ap, sdev),
1083                       sdev, queue_depth);
1084 }
1085 EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
1086 
1087 /**
1088  *  ata_sas_port_alloc - Allocate port for a SAS attached SATA device
1089  *  @host: ATA host container for all SAS ports
1090  *  @port_info: Information from low-level host driver
1091  *  @shost: SCSI host that the scsi device is attached to
1092  *
1093  *  LOCKING:
1094  *  PCI/etc. bus probe sem.
1095  *
1096  *  RETURNS:
1097  *  ata_port pointer on success / NULL on failure.
1098  */
1099 
1100 struct ata_port *ata_sas_port_alloc(struct ata_host *host,
1101                     struct ata_port_info *port_info,
1102                     struct Scsi_Host *shost)
1103 {
1104     struct ata_port *ap;
1105 
1106     ap = ata_port_alloc(host);
1107     if (!ap)
1108         return NULL;
1109 
1110     ap->port_no = 0;
1111     ap->lock = &host->lock;
1112     ap->pio_mask = port_info->pio_mask;
1113     ap->mwdma_mask = port_info->mwdma_mask;
1114     ap->udma_mask = port_info->udma_mask;
1115     ap->flags |= port_info->flags;
1116     ap->ops = port_info->port_ops;
1117     ap->cbl = ATA_CBL_SATA;
1118 
1119     return ap;
1120 }
1121 EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
1122 
1123 /**
1124  *  ata_sas_port_start - Set port up for dma.
1125  *  @ap: Port to initialize
1126  *
1127  *  Called just after data structures for each port are
1128  *  initialized.
1129  *
1130  *  May be used as the port_start() entry in ata_port_operations.
1131  *
1132  *  LOCKING:
1133  *  Inherited from caller.
1134  */
1135 int ata_sas_port_start(struct ata_port *ap)
1136 {
1137     /*
1138      * the port is marked as frozen at allocation time, but if we don't
1139      * have new eh, we won't thaw it
1140      */
1141     if (!ap->ops->error_handler)
1142         ap->pflags &= ~ATA_PFLAG_FROZEN;
1143     return 0;
1144 }
1145 EXPORT_SYMBOL_GPL(ata_sas_port_start);
1146 
1147 /**
1148  *  ata_sas_port_stop - Undo ata_sas_port_start()
1149  *  @ap: Port to shut down
1150  *
1151  *  May be used as the port_stop() entry in ata_port_operations.
1152  *
1153  *  LOCKING:
1154  *  Inherited from caller.
1155  */
1156 
1157 void ata_sas_port_stop(struct ata_port *ap)
1158 {
1159 }
1160 EXPORT_SYMBOL_GPL(ata_sas_port_stop);
1161 
1162 /**
1163  * ata_sas_async_probe - simply schedule probing and return
1164  * @ap: Port to probe
1165  *
1166  * For batch scheduling of probe for sas attached ata devices, assumes
1167  * the port has already been through ata_sas_port_init()
1168  */
1169 void ata_sas_async_probe(struct ata_port *ap)
1170 {
1171     __ata_port_probe(ap);
1172 }
1173 EXPORT_SYMBOL_GPL(ata_sas_async_probe);
1174 
1175 int ata_sas_sync_probe(struct ata_port *ap)
1176 {
1177     return ata_port_probe(ap);
1178 }
1179 EXPORT_SYMBOL_GPL(ata_sas_sync_probe);
1180 
1181 
1182 /**
1183  *  ata_sas_port_init - Initialize a SATA device
1184  *  @ap: SATA port to initialize
1185  *
1186  *  LOCKING:
1187  *  PCI/etc. bus probe sem.
1188  *
1189  *  RETURNS:
1190  *  Zero on success, non-zero on error.
1191  */
1192 
1193 int ata_sas_port_init(struct ata_port *ap)
1194 {
1195     int rc = ap->ops->port_start(ap);
1196 
1197     if (rc)
1198         return rc;
1199     ap->print_id = atomic_inc_return(&ata_print_id);
1200     return 0;
1201 }
1202 EXPORT_SYMBOL_GPL(ata_sas_port_init);
1203 
1204 int ata_sas_tport_add(struct device *parent, struct ata_port *ap)
1205 {
1206     return ata_tport_add(parent, ap);
1207 }
1208 EXPORT_SYMBOL_GPL(ata_sas_tport_add);
1209 
1210 void ata_sas_tport_delete(struct ata_port *ap)
1211 {
1212     ata_tport_delete(ap);
1213 }
1214 EXPORT_SYMBOL_GPL(ata_sas_tport_delete);
1215 
1216 /**
1217  *  ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
1218  *  @ap: SATA port to destroy
1219  *
1220  */
1221 
1222 void ata_sas_port_destroy(struct ata_port *ap)
1223 {
1224     if (ap->ops->port_stop)
1225         ap->ops->port_stop(ap);
1226     kfree(ap);
1227 }
1228 EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
1229 
1230 /**
1231  *  ata_sas_slave_configure - Default slave_config routine for libata devices
1232  *  @sdev: SCSI device to configure
1233  *  @ap: ATA port to which SCSI device is attached
1234  *
1235  *  RETURNS:
1236  *  Zero.
1237  */
1238 
1239 int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
1240 {
1241     ata_scsi_sdev_config(sdev);
1242     ata_scsi_dev_config(sdev, ap->link.device);
1243     return 0;
1244 }
1245 EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
1246 
1247 /**
1248  *  ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
1249  *  @cmd: SCSI command to be sent
1250  *  @ap:    ATA port to which the command is being sent
1251  *
1252  *  RETURNS:
1253  *  Return value from __ata_scsi_queuecmd() if @cmd can be queued,
1254  *  0 otherwise.
1255  */
1256 
1257 int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
1258 {
1259     int rc = 0;
1260 
1261     if (likely(ata_dev_enabled(ap->link.device)))
1262         rc = __ata_scsi_queuecmd(cmd, ap->link.device);
1263     else {
1264         cmd->result = (DID_BAD_TARGET << 16);
1265         scsi_done(cmd);
1266     }
1267     return rc;
1268 }
1269 EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
1270 
1271 /**
1272  *  sata_async_notification - SATA async notification handler
1273  *  @ap: ATA port where async notification is received
1274  *
1275  *  Handler to be called when async notification via SDB FIS is
1276  *  received.  This function schedules EH if necessary.
1277  *
1278  *  LOCKING:
1279  *  spin_lock_irqsave(host lock)
1280  *
1281  *  RETURNS:
1282  *  1 if EH is scheduled, 0 otherwise.
1283  */
1284 int sata_async_notification(struct ata_port *ap)
1285 {
1286     u32 sntf;
1287     int rc;
1288 
1289     if (!(ap->flags & ATA_FLAG_AN))
1290         return 0;
1291 
1292     rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
1293     if (rc == 0)
1294         sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
1295 
1296     if (!sata_pmp_attached(ap) || rc) {
1297         /* PMP is not attached or SNTF is not available */
1298         if (!sata_pmp_attached(ap)) {
1299             /* PMP is not attached.  Check whether ATAPI
1300              * AN is configured.  If so, notify media
1301              * change.
1302              */
1303             struct ata_device *dev = ap->link.device;
1304 
1305             if ((dev->class == ATA_DEV_ATAPI) &&
1306                 (dev->flags & ATA_DFLAG_AN))
1307                 ata_scsi_media_change_notify(dev);
1308             return 0;
1309         } else {
1310             /* PMP is attached but SNTF is not available.
1311              * ATAPI async media change notification is
1312              * not used.  The PMP must be reporting PHY
1313              * status change, schedule EH.
1314              */
1315             ata_port_schedule_eh(ap);
1316             return 1;
1317         }
1318     } else {
1319         /* PMP is attached and SNTF is available */
1320         struct ata_link *link;
1321 
1322         /* check and notify ATAPI AN */
1323         ata_for_each_link(link, ap, EDGE) {
1324             if (!(sntf & (1 << link->pmp)))
1325                 continue;
1326 
1327             if ((link->device->class == ATA_DEV_ATAPI) &&
1328                 (link->device->flags & ATA_DFLAG_AN))
1329                 ata_scsi_media_change_notify(link->device);
1330         }
1331 
1332         /* If PMP is reporting that PHY status of some
1333          * downstream ports has changed, schedule EH.
1334          */
1335         if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
1336             ata_port_schedule_eh(ap);
1337             return 1;
1338         }
1339 
1340         return 0;
1341     }
1342 }
1343 EXPORT_SYMBOL_GPL(sata_async_notification);
1344 
1345 /**
1346  *  ata_eh_read_log_10h - Read log page 10h for NCQ error details
1347  *  @dev: Device to read log page 10h from
1348  *  @tag: Resulting tag of the failed command
1349  *  @tf: Resulting taskfile registers of the failed command
1350  *
1351  *  Read log page 10h to obtain NCQ error details and clear error
1352  *  condition.
1353  *
1354  *  LOCKING:
1355  *  Kernel thread context (may sleep).
1356  *
1357  *  RETURNS:
1358  *  0 on success, -errno otherwise.
1359  */
1360 static int ata_eh_read_log_10h(struct ata_device *dev,
1361                    int *tag, struct ata_taskfile *tf)
1362 {
1363     u8 *buf = dev->link->ap->sector_buf;
1364     unsigned int err_mask;
1365     u8 csum;
1366     int i;
1367 
1368     err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, 0, buf, 1);
1369     if (err_mask)
1370         return -EIO;
1371 
1372     csum = 0;
1373     for (i = 0; i < ATA_SECT_SIZE; i++)
1374         csum += buf[i];
1375     if (csum)
1376         ata_dev_warn(dev, "invalid checksum 0x%x on log page 10h\n",
1377                  csum);
1378 
1379     if (buf[0] & 0x80)
1380         return -ENOENT;
1381 
1382     *tag = buf[0] & 0x1f;
1383 
1384     tf->status = buf[2];
1385     tf->error = buf[3];
1386     tf->lbal = buf[4];
1387     tf->lbam = buf[5];
1388     tf->lbah = buf[6];
1389     tf->device = buf[7];
1390     tf->hob_lbal = buf[8];
1391     tf->hob_lbam = buf[9];
1392     tf->hob_lbah = buf[10];
1393     tf->nsect = buf[12];
1394     tf->hob_nsect = buf[13];
1395     if (dev->class == ATA_DEV_ZAC && ata_id_has_ncq_autosense(dev->id))
1396         tf->auxiliary = buf[14] << 16 | buf[15] << 8 | buf[16];
1397 
1398     return 0;
1399 }
1400 
1401 /**
1402  *  ata_eh_analyze_ncq_error - analyze NCQ error
1403  *  @link: ATA link to analyze NCQ error for
1404  *
1405  *  Read log page 10h, determine the offending qc and acquire
1406  *  error status TF.  For NCQ device errors, all LLDDs have to do
1407  *  is setting AC_ERR_DEV in ehi->err_mask.  This function takes
1408  *  care of the rest.
1409  *
1410  *  LOCKING:
1411  *  Kernel thread context (may sleep).
1412  */
1413 void ata_eh_analyze_ncq_error(struct ata_link *link)
1414 {
1415     struct ata_port *ap = link->ap;
1416     struct ata_eh_context *ehc = &link->eh_context;
1417     struct ata_device *dev = link->device;
1418     struct ata_queued_cmd *qc;
1419     struct ata_taskfile tf;
1420     int tag, rc;
1421 
1422     /* if frozen, we can't do much */
1423     if (ap->pflags & ATA_PFLAG_FROZEN)
1424         return;
1425 
1426     /* is it NCQ device error? */
1427     if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1428         return;
1429 
1430     /* has LLDD analyzed already? */
1431     ata_qc_for_each_raw(ap, qc, tag) {
1432         if (!(qc->flags & ATA_QCFLAG_FAILED))
1433             continue;
1434 
1435         if (qc->err_mask)
1436             return;
1437     }
1438 
1439     /* okay, this error is ours */
1440     memset(&tf, 0, sizeof(tf));
1441     rc = ata_eh_read_log_10h(dev, &tag, &tf);
1442     if (rc) {
1443         ata_link_err(link, "failed to read log page 10h (errno=%d)\n",
1444                  rc);
1445         return;
1446     }
1447 
1448     if (!(link->sactive & (1 << tag))) {
1449         ata_link_err(link, "log page 10h reported inactive tag %d\n",
1450                  tag);
1451         return;
1452     }
1453 
1454     /* we've got the perpetrator, condemn it */
1455     qc = __ata_qc_from_tag(ap, tag);
1456     memcpy(&qc->result_tf, &tf, sizeof(tf));
1457     qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
1458     qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1459     if (dev->class == ATA_DEV_ZAC &&
1460         ((qc->result_tf.status & ATA_SENSE) || qc->result_tf.auxiliary)) {
1461         char sense_key, asc, ascq;
1462 
1463         sense_key = (qc->result_tf.auxiliary >> 16) & 0xff;
1464         asc = (qc->result_tf.auxiliary >> 8) & 0xff;
1465         ascq = qc->result_tf.auxiliary & 0xff;
1466         ata_scsi_set_sense(dev, qc->scsicmd, sense_key, asc, ascq);
1467         ata_scsi_set_sense_information(dev, qc->scsicmd,
1468                            &qc->result_tf);
1469         qc->flags |= ATA_QCFLAG_SENSE_VALID;
1470     }
1471 
1472     ehc->i.err_mask &= ~AC_ERR_DEV;
1473 }
1474 EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);